UV Mapping models

I got a model from the net with a uvmap.jpg and several textures that could be applied to it. How do I go about texturing the model (it's in MS3D format). I checked the forums and found nothing on UV mapping or Texturing in MS3D.

Trussell said:

I got a model from the net with a uvmap.jpg and several textures that could be applied to it. How do I go about texturing the model (it's in MS3D format). I checked the forums and found nothing on UV mapping or Texturing in MS3D.


Think you might want to search the cumbalumba forums mate .....

Do you mean how do you apply a texture to a MS3D model in jME? Or that the model is not yet uv mapped and you need to do that in Milkshape?

Apply the texture to the model within jME

Ah, in that case look at the test "TestCameraMan", should contain the source you are after




Thanks!  :smiley:

TestCameraMan didn't have anything about applying textures using the UV map in a jpg and then a texture set in a jpg.



Tried to switch to an OBJ file with a .mtl file, but when I follow (almost) exactly the hello model loading tutorial, the model is not textured.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fyrestonev03;

import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.math.FastMath;
import com.jme.math.Matrix3f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.MilkToJme;
import com.jmex.model.converters.ObjToJme;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

/**
 *
 * @author Tyler
 */
public class ModelConverter
{
    private static final String ninjaDir = "models/ninja.obj";
    private static Quaternion pitch90 = new Quaternion();
   
    public static Spatial convertModel()
    {
        // Point to a URL of my model
        URL model = FyrestoneClient.class.getClassLoader().getResource(ninjaDir);
 
        // Create something to convert .obj format to .jme
        FormatConverter converter = new ObjToJme();
        converter.setProperty("ninja", model);
        // This byte array will hold my .jme file
        ByteArrayOutputStream BO = new ByteArrayOutputStream();
        try {
            // Use the format converter to convert .obj to .jme
            converter.convert(model.openStream(), BO);
            Spatial myModel = (Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
 
            // shrink this baby down some
            myModel.setLocalScale(10f);
           
            pitch90.fromAngleNormalAxis(FastMath.PI * 1.5f, Vector3f.UNIT_X);
            myModel.setLocalRotation(pitch90);
            myModel.setModelBound(new BoundingBox());
            myModel.updateModelBound();
            myModel.updateRenderState();
 
            // Put her on the scene graph
            return myModel;
        } catch (IOException e) {   // Just in case anything happens
 
            System.out.println("Damn exceptions!" + e);
            e.printStackTrace();
            System.exit(0);
        }
        return null;
    }
}



I've tried changing this part: "converter.setProperty("mtllib", model);" several times. So far I have tried:

- "ninja.mtl", model
- "ninja" model
- "mtllib" model
- "mtllib" models/ninja.obj

But all to no avail.

OK I thought I understood but now I'm confused again. Isn't a UV Map just a template for creating texture maps? Not something you would actually apply to the model?



I found adding a texture to a milkshape model to work the same as with anything else (except the geometry is attached to child nodes).



file.getChild(0).setRenderState(ts);

Alric said:

OK I thought I understood but now I'm confused again. Isn't a UV Map just a template for creating texture maps? Not something you would actually apply to the model?

I found adding a texture to a milkshape model to work the same as with anything else (except the geometry is attached to child nodes).


file.getChild(0).setRenderState(ts);




Trussell said:

TestCameraMan didn't have anything about applying textures using the UV map in a jpg and then a texture set in a jpg.

Tried to switch to an OBJ file with a .mtl file, but when I follow (almost) exactly the hello model loading tutorial, the model is not textured.


The last post was me trying to texture an OBJ, not an MS3D. But yes, a UV map is just a template.

I don't understand what you need… Do you want to apply a texture to a model within jME? If so, you need to create a TextureState, load a texture with TextureManager and then set it to one of the TextureState's slots.

I had the same problem initially with loading textures from mtl files for obj files, and then found that you have to use the ResourceLocatorTool class to tell jme the paths to your resources, although I don't understand why given an absolute path would not suffice.  Here is a code snippet to demonstrate:



      // get the file selection
      File f = DialogUtil.getUserFileSelection(null, false, "Choose File", new String[] {"obj"}, "Object Files");
      
      // user canceled selection
      if(f == null)
         return;
      
      String filename = f.toString();
      // load the model and add it to the cache
      if(!modelCache.containsEntry(filename))
      {
           FormatConverter converter=new ObjToJme();
           InputStream in = null;
           try
           {
               ResourceLocatorTool.addResourceLocator(
                       ResourceLocatorTool.TYPE_TEXTURE,
                       new SimpleResourceLocator(f.getParentFile().toURI().toURL()));
              // Point the converter to where it will find the .mtl file from
              converter.setProperty("mtllib",new URL(
                    FileUtils.urlEncode(FileUtils.removeFileExt(f.getAbsolutePath()) + ".mtl")));
              ByteArrayOutputStream out = new ByteArrayOutputStream(1024*1024);
              Spatial model;
              in = new BufferedInputStream(new FileInputStream(f));
              // do conversion to jme and write to out
              converter.convert(in, out);
              model = (Spatial)BinaryImporter.getInstance().load(out.toByteArray());
              model.setModelBound(new BoundingBox());
              model.updateModelBound();
              model.updateRenderState();
              // add the model to the cache
              modelCache.addEntry(filename, model);
           }
           catch(Exception e)
           {
              logger.log(Level.SEVERE, "Error loading mode file: " + filename, e);
              return;
           }
           finally
           {
              if(in != null)
              {
                 try { in.close(); }
                 catch(IOException e) {}
              }
           }
   } // end if

Then why does the test work, when it's only giving the exact same path?

Wow… see this thread



That was my problem. I have the character as a separate node, so I have to light it separately… I’m going to go cry in a corner now for not realizing…