I am sure this question has been answered before, but I am having trouble locating a previous thread about it. I am generating a terrain which is made up of a grid of squares. Each square consists of two polygons and four vertices:
(1)---------(3)
| \ |
| \ |
| \ |
| \ |
(2)---------(0)
(the numbers represent the index of each vertex).
Each square in the terrain has its own GridVertexController (which extends GenericVertexController and implements IVertexController). The GridVertexController class has four SimpleVectors:
SimpleVector vertex0, vertex1, vertex2, vertex3;
My program changes these four SimpleVectors to represent the new values for the square's vertices.
In the apply() method, I update the getDestinationMesh() array, and then attempt to update the getDestinationNormals() array:
public void apply()
{
SimpleVector dstVertex[] = getDestinationMesh();
SimpleVector dstNormal[] = getDestinationNormals();
SimpleVector A, B, C, D;
dstVertex[0].set( vertex0 );
dstVertex[1].set( vertex1 );
dstVertex[2].set( vertex2 );
dstVertex[3].set( vertex3 );
A = vertex1.calcSub( vertex2 );
B = vertex0.calcSub( vertex2 );
C = A.calcCross( B ).normalize();
dstNormal[2].set( C );
A = vertex0.calcSub( vertex3 );
B = vertex1.calcSub( vertex3 );
D = A.calcCross( B ).normalize();
dstNormal[3].set( D );
D.add( C );
D = D.normalize();
dstNormal[0].set( D );
dstNormal[1].set( D );
}
The vertices are changed correctly, but there seems to be a problem in the algorithm I'm using to calculate the normals, because lighting on the terrain is not correct. For example, if I generate a flat terrain on the x/z plane with a light source directly overhead, the terrain still appears dark.
Oops, I figured out my problem. I was calculating the wrong cross-products (they were backwards). :-[ I have it working now. In case anyone is interested, here is the corrected code:
public void apply()
{
SimpleVector dstVertex[] = getDestinationMesh();
SimpleVector dstNormal[] = getDestinationNormals();
SimpleVector A, B, C, D;
dstVertex[0].set( vertex0 );
dstVertex[1].set( vertex1 );
dstVertex[2].set( vertex2 );
dstVertex[3].set( vertex3 );
A = vertex1.calcSub( vertex2 );
B = vertex0.calcSub( vertex2 );
C = B.calcCross( A ).normalize();
dstNormal[2].set( C );
A = vertex0.calcSub( vertex3 );
B = vertex1.calcSub( vertex3 );
D = B.calcCross( A ).normalize();
dstNormal[3].set( D );
D.add( C );
D = D.normalize();
dstNormal[0].set( D );
dstNormal[1].set( D );
}
Thanks for the code. I might need it in an upcoming project. It's still very early in the idea stage, though. I might just make the terrains in Blender also, but it's a little more expensive for file size, I think. Still, I can see what it really looks like in Blender and the files are pretty small. Congratulations on the promotion to staff sergeant btw.
Quote from: fireside on September 28, 2008, 11:38:46 PM
Congratulations on the promotion to staff sergeant btw.
Thanks!