Constructing the Ground
Quads with similar heights are grouped together in a TexturedPlanes
object (which is a subclass of Shape3D
). All the quads in the object are covered with the same texture, thereby adding detail to the landscape. However, a quad cannot be assigned a texture until it has texture coordinates.
I want quads to reflect light, so patterns of light and dark are shown on the landscape. This requires that each quad coordinate has a normal vector, so the direction and intensity of reflected light at each point can be calculated.
Generating these normals manually would be time-consuming, so I use Java 3D's utility class, NormalGenerator
, to do the job.
TheTexturedPlanes
geometry is a QuadArray
, with fields for the (x, y, z) coordinates, texture coordinates, and the normals. The ArrayList
of (x, y, z) coordinates (stored in coords
) is converted to an array before its points can be used:
int numPoints = coords.size(); QuadArray plane = new QuadArray(numPoints, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2 | GeometryArray.NORMALS ); // obtain points Point3d[] points = new Point3d[numPoints]; coords.toArray( points );
The texture coordinates are created in the same order as the points in a quad and repeated for each quad:
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] ...
Get Killer Game Programming in Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.