Author Topic: Multiple textures on one cube  (Read 1207 times)

Offline Moraff

  • byte
  • *
  • Posts: 2
    • View Profile
Multiple textures on one cube
« on: September 30, 2011, 05:00:20 pm »
I've been trying to put a different picture on each side of a cube. I was able to do it by creating 6 planes and making them a cube, but three planes always faced the wrong way (into the interior of the cube). I am trying to build a 3D gaming framework for creating educational software that uses a lot of images on 3D blocks.

My second question is whether there is a way to transform a cube after it is created that will allow scaling control for each of the x, y, and z axis independently. I created an awesome animation system for Java2D that allows a lot of transforms to unwind, but I want to adapt it for 3D transforms. Should I access the object's matrix? If so, is there documentation on the structure of the jpct 3D matrix?

I am new to real 3D programming and jpct, so thanks for your patience!

Steve Moraff

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Multiple textures on one cube
« Reply #1 on: September 30, 2011, 09:27:15 pm »
There's code for creating a simple cube in the Wiki. This can be extended to assign a texture per polygon by using the appropriate addTriangle()-method. About the scaling...jPCT has a weakness when it comes to non-uniform scaling. You can tweak the rotation matrix yourself...this works, but has the drawback, that lighting will be influenced and maybe doesn't look as good as it is supposed to. Another option is to use an implementation of an IVertexController. This thread has a similar topic (starting somewhere on page 2 or so....): http://www.jpct.net/forum2/index.php/topic,2258.0.html. It also includes a very basic version of such an implementation. If that doesn't help: The forum has multiple threads about this topic...maybe one of them is helpful.

Offline Moraff

  • byte
  • *
  • Posts: 2
    • View Profile
Re: Multiple textures on one cube
« Reply #2 on: October 03, 2011, 07:27:11 am »
Thanks for the quick answer. Your suggestion worked fine for creating the cube. I generalized it so that it can use any x1,x2,y1,y2,z1,z2 and paste the 6 images onto the faces. I also am using buffered images so that I can print onto the sides of the cubes. I haven't started with the transformations yet, but I am looking forward to playing with them. I had a lot of fun with Java2D's transform system.


void createBox() {
   texture[0] = new Texture(image[0].bufferedImage);
   TextureManager.getInstance().addTexture(debugName + "t0", texture[0]);
texture[1] = new Texture(image[1].bufferedImage);
TextureManager.getInstance().addTexture(debugName + "t1", texture[1]);
texture[2] = new Texture(image[2].bufferedImage);
TextureManager.getInstance().addTexture(debugName + "t2", texture[2]);
texture[3] = new Texture(image[3].bufferedImage);
TextureManager.getInstance().addTexture(debugName + "t3", texture[3]);
texture[4] = new Texture(image[4].bufferedImage);
TextureManager.getInstance().addTexture(debugName + "t4", texture[4]);
texture[5] = new Texture(image[5].bufferedImage);
TextureManager.getInstance().addTexture(debugName + "t5", texture[5]);

box = new Object3D(12);

SimpleVector upperLeftFront = new SimpleVector(locationActual.x1, locationActual.y1, locationActual.z1);
SimpleVector upperRightFront = new SimpleVector(locationActual.x2, locationActual.y1, locationActual.z1);
SimpleVector lowerLeftFront = new SimpleVector(locationActual.x1, locationActual.y2, locationActual.z1);
SimpleVector lowerRightFront = new SimpleVector(locationActual.x2, locationActual.y2, locationActual.z1);

SimpleVector upperLeftBack = new SimpleVector(locationActual.x1, locationActual.y1, locationActual.z2);
SimpleVector upperRightBack = new SimpleVector(locationActual.x2, locationActual.y1, locationActual.z2);
SimpleVector lowerLeftBack = new SimpleVector(locationActual.x1, locationActual.y2, locationActual.z2);
SimpleVector lowerRightBack = new SimpleVector(locationActual.x2, locationActual.y2, locationActual.z2);

TextureManager tm = TextureManager.getInstance();

// Front
box.addTriangle(upperLeftFront, 0, 0, lowerLeftFront, 0, 1, upperRightFront, 1, 0, tm.getTextureID(debugName + "t1"));
box.addTriangle(upperRightFront, 1, 0, lowerLeftFront, 0, 1, lowerRightFront, 1, 1, tm.getTextureID(debugName + "t1"));

// Back
box.addTriangle(upperLeftBack, 0, 0, upperRightBack, 1, 0, lowerLeftBack, 0, 1, tm.getTextureID(debugName + "t0"));
box.addTriangle(upperRightBack, 1, 0, lowerRightBack, 1, 1, lowerLeftBack, 0, 1, tm.getTextureID(debugName + "t0"));

// Upper
box.addTriangle(upperLeftBack, 0, 0, upperLeftFront, 0, 1, upperRightBack, 1, 0, tm.getTextureID(debugName + "t4"));
box.addTriangle(upperRightBack, 1, 0, upperLeftFront, 0, 1, upperRightFront, 1, 1, tm.getTextureID(debugName + "t4"));
// box.addTriangle(upperLeftBack, 0, 0, upperLeftFront, 0, 1, upperRightBack, 1, 0);
// box.addTriangle(upperRightBack, 1, 0, upperLeftFront, 0, 1, upperRightFront, 1, 1);

// Lower
box.addTriangle(lowerLeftBack, 0, 0, lowerRightBack, 1, 0, lowerLeftFront, 0, 1, tm.getTextureID(debugName + "t3"));
box.addTriangle(lowerRightBack, 1, 0, lowerRightFront, 1, 1, lowerLeftFront, 0, 1, tm.getTextureID(debugName + "t3"));

// Left
box.addTriangle(upperLeftFront, 0, 0, upperLeftBack, 1, 0, lowerLeftFront, 0, 1, tm.getTextureID(debugName + "t2"));
box.addTriangle(upperLeftBack, 1, 0, lowerLeftBack, 1, 1, lowerLeftFront, 0, 1, tm.getTextureID(debugName + "t2"));

// Right
box.addTriangle(upperRightFront, 0, 0, lowerRightFront, 0, 1, upperRightBack, 1, 0, tm.getTextureID(debugName + "t5"));
box.addTriangle(upperRightBack, 1, 0, lowerRightFront, 0, 1, lowerRightBack, 1, 1, tm.getTextureID(debugName + "t5"));

box.setTexture("dfg");

box.build();

}