The Back Facing Camera


Tiling the Floor

The floor is a QuadArray made up of a series of quads, which taken together cover the entire floor. Each quad is assigned a texture and an upward facing normal. The sides of the entire floor have the length FLOOR_LEN, and each quad has sides of length STEP, as shown in Figure 25-7.

The paving of the floor starts at the front, leftmost point (-FLOOR_LEN/2, FLOOR_LEN/2) and continues left to right and forward to back. This work is carried out by TexturedFloor's constructor and createCoords( ):

     public TexturedFloor(  )     // create quad coords, make TexturedPlane, add to floorBG 

Figure 25-7. The floor and its QuadArray (from above)


     {       ArrayList coords = new ArrayList(  );       floorBG = new BranchGroup(  );           // create coords for the quad       for(int z=FLOOR_LEN/2; z >= (-FLOOR_LEN/2)+STEP; z-=STEP) {         for(int x=-FLOOR_LEN/2; x <= (FLOOR_LEN/2)-STEP; x+=STEP)           createCoords(x, z, coords);       }           Vector3f upNormal = new Vector3f(0.0f, 1.0f, 0.0f);   // upwards       floorBG.addChild( new TexturedPlane(coords,FLOOR_IMG,upNormal) );     }             private void createCoords(int x, int z, ArrayList coords)     {       // points created in counter-clockwise order, from front left       // of length STEP       Point3f p1 = new Point3f(x, 0.0f, z);         Point3f p2 = new Point3f(x+STEP, 0.0f, z);       Point3f p3 = new Point3f(x+STEP, 0.0f, z-STEP);       Point3f p4 = new Point3f(x, 0.0f, z-STEP);       coords.add(p1); coords.add(p2);       coords.add(p3); coords.add(p4);     }  // end of createCoords(  ) 

createCoords( ) takes one point and creates the four points for a quad, making sure they're in counterclockwise order to place the front face upward on the XZ plane. Once all the coordinates have been calculated, they're passed to a TexturedPlane object, along with the name of a texture file (TEXTURE_IMG) and a vector that will become each quad's normal.

The Textured Plane

The QuadArray created by TexturedPlane uses textures and normals:

     QuadArray plane  = new QuadArray(numPoints, GeometryArray.COORDINATES |                                GeometryArray.TEXTURE_COORDINATE_2 |                                GeometryArray.NORMALS ); 

The hardest part of the geometry-related code is setting the texture coordinates. The coordinates in the QuadArray are ordered in groups of four, each specifying a quad in counterclockwise order. The texture coordinates are created in the same wayin groups of four in counterclockwise order:

     // assign texture coords to each quad     // counter-clockwise, from bottom left     TexCoord2f[] tcoords = new TexCoord2f[numPoints];     for(int i=0; i < numPoints; i=i+4) {       tcoords[i] = new TexCoord2f(0.0f, 0.0f);   // for 1 point       tcoords[i+1] = new TexCoord2f(1.0f, 0.0f);       tcoords[i+2] = new TexCoord2f(1.0f, 1.0f);       tcoords[i+3] = new TexCoord2f(0.0f, 1.0f);     }     plane.setTextureCoordinates(0, 0, tcoords); 

The Appearance part of the shape creates a modulated texture with a Material node component so lighting can be enabled. The code is similar to makeApp( ) in MazeManager. The end result is that the texture will be tiled across the floor, with each tile having sides of length STEP. Though there appears to be many textures, there's only one, but it's referenced many times. This optimization means that only one Texture2D object needs to be created by the application.

By varying the STEP constant, the number and size of the tiles can be varied. As the STEP value gets bigger, each tile gets bigger, and fewer tiles are needed to cover the floor. A drawback with a larger tile is that the texture will be stretched over a larger area, so it will become more pixilated. Another point to remember is that STEP should be divisible into the floor length value, FLOOR_LEN.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net