Change textures from Obj-modell at runtime

What will I do?

first job:

  • load a obj-model (simple cube) whitout texture in the material file
  • load a texture and place it on the cube

second job:

  • load a obj-model (simple cube) whitout texture in the material file
  • place different textures on each side of the cube

I already created that:


 // Constructor
.....

            Texture2D tex = null;
            TextureLoader tl = new TextureLoader();
            tl.registerPath("./");
            tex = (Texture2D) tl.getMinMapTexture("glass2.png");

            BranchGroup model = null;
            try {
                   model = (new OBJLoader()).load("models/cube4.obj");
            } catch (Exception e) {
                   System.err.println(e);
            }
            parseRekurs(0, model);

       Appearance ap = new Appearance();
       ap.setTexture(tex); 
       Shape3D sh =
            new Shape3D(((Shape3D) ((BranchGroup) ((BranchGroup) model.getChild(1)).getChild(0)).getChild(0)).getGeometry(),ap);
       objRotate.addChild(sh);

......
// end constructor

      public void parseRekurs(int index, BranchGroup branchGroup) {
              List list = branchGroup.getChildren();
              for (int i = 0; i < list.size(); i++) {
                      System.out.println(index + ": " + list.get(i).getClass());
                      if (list.get(i) instanceof BranchGroup) {
                                        parseRekurs(index + 1, (BranchGroup) list.get(i));
                      }
              }
      }



… that must be able be done nevertheless more simply.

Or is it better to use the 3ds meshes and the ASE-loader?

For the second problem I imagined that I build the obj-model (cube) from individual planes.
So I can texture each one.

Thx 4 help :wink:

Why do you need to do this…? I’m not sure how the obj loader works… doesn’t it load textures automatically?

[quote]Why do you need to do this…? I’m not sure how the obj loader works… doesn’t it load textures automatically?
[/quote]
The loader can load the textures but …

–> why loading hundreds of cubes when only the texture is different ?

–> I would like to change the textures during the game.

You could implement a NodeFactory that looked at the generated appearances and used cached ones if thats how you wanted it to work.

You could also hold references to the appearances in use and change the texture mid-flight if thats whats you want to do.

NodeFactory - its your best pal :slight_smile:

Kev

sounds good
do you have a small example?

because I have only basic knowledge in java programming …

Thx