Applyng texture on a moving node

I have a node (Spatial model) which I have applied a texture to. When the parent node (Node node) rotates the texture does not rotate with the model. It seems that it is floating. How can I get the texture to stick to the mesh?



public class TextureTest extends SimpleGame
{
  private Quaternion q = new Quaternion();
  private float angle = 0;
  private Node node;
 
  public TextureTest()
  {
    setConfigShowMode(ConfigShowMode.AlwaysShow, url);
    Logger.getLogger("").setLevel(Level.ALL);
  }
  public static void main(String[] args)
  {
    TextureTest app = new TextureTest();
    app.start();
  }
  protected void simpleUpdate()
  {
    angle += 0.005f;
    q.fromAngles(0, angle, 0);
    node.setLocalRotation(q);
  }
  @Override
  protected void simpleInitGame()
  {
    node = new Node();
    Spatial model = loadModel("a jME binary file");
    TextureState ts = display.getRenderer().createTextureState();
    URL url = null;
    try
    {
      File file = new File("a normal jpg file...");
      url = file.toURI().toURL();
      System.out.println(url);
    }
    catch (MalformedURLException e)
    {
      e.printStackTrace();
    }
    Texture t = TextureManager.loadTexture(url);
    t.setEnvironmentalMapMode(Texture.EnvironmentalMapMode.SphereMap);
    ts.setTexture(t);
    ts.setEnabled(true);
    model.setRenderState(ts);
   
    node.attachChild(model);
    rootNode.attachChild(node);
  }
 
  private Spatial loadModel(String rawFile)
  {
    Spatial map = null;
    try
    {
      File jmeModel = new File(rawFile);     
      Logger.getLogger("").log(Level.INFO, "Opening " + jmeModel.toString());
      FileInputStream fi = new FileInputStream(jmeModel);
      map = (Spatial) BinaryImporter.getInstance().load(fi);
      fi.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      System.exit(0);
    }
    return map;
  }
}

First thing i see:



rootNode.updateRenderState();


after you set up everything.

added the line in the end of both init() and update() but did not see any change to the texture



(added the texture)


public class TextureTest extends SimpleGame
{
  private Quaternion q = new Quaternion();
  private float angle = 0;
  private Node node;
 
  public TextureTest()
  {
    setConfigShowMode(ConfigShowMode.AlwaysShow, url);
    Logger.getLogger("").setLevel(Level.ALL);
  }
  public static void main(String[] args)
  {
    TextureTest app = new TextureTest();
    app.start();
  }
  protected void simpleUpdate()
  {
    angle += 0.005f;
    q.fromAngles(0, angle, 0);
    node.setLocalRotation(q);
   
    rootNode.updateRenderState();
  }
  protected void simpleInitGame()
  {
    node = new Node();
    Teapot model = new Teapot();
    model.updateGeometryData();
   
   
    TextureState ts = display.getRenderer().createTextureState();
    URL url = null;
    try
    {
      File file = new File("/path/test_texture.jpg");
      url = file.toURI().toURL();
      System.out.println(url);
    }
    catch (MalformedURLException e)
    {
      e.printStackTrace();
    }
    Texture t = TextureManager.loadTexture(url);
    t.setEnvironmentalMapMode(Texture.EnvironmentalMapMode.SphereMap);
    ts.setTexture(t);
    ts.setEnabled(true);
    model.setRenderState(ts);
   
    node.attachChild(model);
    rootNode.attachChild(node);
    rootNode.updateRenderState();
  }
}

Ummm, this line:

t.setEnvironmentalMapMode(Texture.EnvironmentalMapMode.SphereMap);


is what is making the texture 'float'...

Yeah. That is right. But now the texture wraps a bit strange around the object. On other more complex objects I only see the light blue color. Is there a way to "tile" the texture over the mesh.



EDIT: I solved it. adding:

t.setWrap(Texture.WrapMode.Repeat);



An other problem. I've added a Torus from jME and imported one from Blender via .obj. Both have the same properties, and I can see the wireframes (by pressing T) and they match. The normals (pressing N) is a bit shorter on the Blender torus.



wrapping around the texture on the jME torus works fine, but the one from Blender has an odd color. Looks like a blend of all colors in the texture…



Any sugestions?

if the normal are messed up, you get strange colors :wink:



solution1: recalculate the normal vectors in blender before exporting (don't know how)

solution2: use NormalGenerator to recalculate the NVs after import.



gl&hf

I still havent figured out how to properly texture an imported model.



Now I've made a model in Blender which is a simple plane with the size 10x10. and added it. I've also added a jME Quad with the size 10x10. The jME model is textured correctly. The blemder model is not. WHY!?

Ive now ensured that the vertexes, normals and triangles is excactly like in both TriMeshes. The difference is that the Blender model does not have texturecoords, so I added the the ones from the jME model and voila.


blenderModel.setTextureCoords(jmeModel.getTextureCoords());



Now I need to find a way to generate these coords. Maybe I need to do it in UV mapping in blender...

You need to UV map (texture map) your model :wink:



http://www.gamedev.net/reference/articles/article2251.asp

http://www.gamedev.net/reference/articles/article2255.asp



Check out that tutorial (its a 2 parter); its a little old so some of the blender functions have changed but should get you pointed in the right away :slight_smile:

basixs said:

You need to UV map (texture map) your model ;)


I was hoping I didn't have to UV map... My models are so complex... Isn't there a way to just "throw" the texture on.
buestad said:

I was hoping I didn't have to UV map... My models are so complex... Isn't there a way to just "throw" the texture on.

Afraid not, but you don't necessarily have to put a bunch of work into it. You can just add a few seams and let Blender unwrap it. Thats usually good enough for simple texture jobs.

OK. UVmapping was not so difficult. In fact without any seems it looks ok. Thanks a lot folks!  :smiley:



A nice tutorial about UV mapping in Blender can be found on the bottom of this page

http://www.blender.org/education-help/video-tutorials/modelmateriallight/