
|
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 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
// The texture coordinates is scaled down to 0-1 values in the TEXTURE_ARRAY = new VertexArray(TEXTURES.length / 2, 2, 2); // VertexBuffer holds references to VertexArrays that contain the positions, colors, normals, 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); After the mesh is created, we create our textures. Image texImg = Image.createImage(path); // load the image When the textures are created, we can add them to the appearance used with our mesh. meshAppearance.setTexture(0, brickTexture); // add the first texture.
• PostRotate(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); | |
Copyright © 2001 - 2009 Sony Ericsson Mobile Communications AB. All Rights Reserved.