Header Sep

Java Tips, Tricks & Code

My Rating Score
Login to rate page

November 2004

Introduction to texturing in JSR-184

 

This tip will show how to apply textures to figures in Java Mobile 3D. It will also provide a quick insight into multi-texturing and how to translate textures. Download the application below to get the full source code of the example described in this article.

The main object used is a pyramid. The images show the pyramid without texture, with one texture and with two textures.

  

 

The two textures used on the pyramid:

     

The size of the textures in Java Mobile 3D must always be a non-negative power of two (2,4,8,16,32,64,128,256).

We start by creating the pyramid and by setting the texture coordinates to be used. Although the pyramid only consists of five points, we have to specify three vertices for each side of the pyramid to get the texture coordinates right.

          

Texture values are specified from 0 to 1, but since we have used integer values to specify texture coordinates we enter values from 0 to 255 and scale them down by 1.0f/255.0f.

The top-left corner of the textured object received the texture coordinate (0,0) and the down right corner received the coordinate (1,1).

So, when entering the texture coordinates for a pyramid, keep in mind that the texture map is still a square, so the top-middle texture coordinate will be 0.5, but we enter 127 and
127*(1/255) ≈ 0.5

Make sure the TEXTURES array matches the POINTS array. The point (-1,-1,1) is the down-left point of the front and down-left texture coordinate is (0,1). Notice that the bottom plate of the pyramid is constructed of two triangles.

short []POINTS = new short[] {-1,-1, 1,   1,-1, 1,   0, 1, 0,   //front
                               1,-1, 1,   1,-1,-1,   0, 1, 0,   //right
                               1,-1,-1,  -1,-1,-1,   0, 1, 0,   //back
                              -1,-1,-1,  -1,-1, 1,   0, 1, 0,   //left
                              -1,-1, 1,   1,-1, 1,   1,-1,-1, //bottom right
                              -1,-1, 1,   1,-1,-1,  -1,-1,-1}; // bottom left

 

// The texture coordinates is scaled down to 0-1 values in the
// setTextCoords method
short []TEXTURES = new short[] {0,255,  255,255,  127,0,
                                0,255,  255,255,  127,0,
                                0,255,  255,255,  127,0,
                                0,255,  255,255,  127,0,
                                0,0,    255,0,    255,255,
                                0,0,    255,255,    0,255};
                                                           
       
VertexArray TEXTURE_ARRAY;

TEXTURE_ARRAY = new VertexArray(TEXTURES.length / 2, 2, 2);
TEXTURE_ARRAY.set(0, TEXTURES.length / 2, TEXTURES);

// VertexBuffer holds references to VertexArrays that contain the positions, colors, normals,
// and texture coordinates for a set of vertices
VertexBuffer vertexBuffer = new VertexBuffer();

To be able to use multi-texturing, in this case two textures, we have to set the texture coordinates for each texture unit to use. This is where you specify the down-scaling in the setTexCoords method.

vertexBuffer.setTexCoords(0, TEXTURE_ARRAY, (1.0f/255.0f), null);
vertexBuffer.setTexCoords(1, TEXTURE_ARRAY, (1.0f/255.0f), null);

After the mesh is created, we create our textures.

Image texImg = Image.createImage(path); // load the image
Texture2D texture = new Texture2D(new Image2D(Image2D.RGB, texImg)); // create the texture texture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT); // repeat texture on surface
texture.setBlending(Texture2D.FUNC_DECAL); // Blend mode to use.
texture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);

When the textures are created, we can add them to the appearance used with our mesh.
Set the specific texture index to NULL to remove a texture form the appearance.

meshAppearance.setTexture(0,  brickTexture); // add the first texture.
meshAppearance.setTexture(1,  multiTexture); // add the second texture


Since the Texture2D object extends the Transformable class, it's easy to create nice effects using the texture. You can rotate and move and scale the textures by using the:

• PostRotate(float x, float y, float z)
• Translate(float x, float y, float z)
• scale(float x, float y, float z)

By default, a texture's bitmap is made to fit over the entire surface of the object. So if you have a square figure and want to use a 256 by 32 texture, the texture will shrink the width to fit. To avoid this, you can scale down the width of the texture by eight (32x8=256), scale(0.125f, 1.0f, 1.0f); and now if you want to show other parts of the same texture you should use the translate method, translate(0.5f, 0.0f, 0.0f);

Download 3D source code>> 

My Rating Score
Login to rate page