Author Topic: Calculating vertice normals in a VertexController  (Read 2171 times)

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Calculating vertice normals in a VertexController
« on: September 28, 2008, 10:00:10 pm »
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:
Code: [Select]
    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:

Code: [Select]
    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.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Calculating vertice normals in a VertexController
« Reply #1 on: September 28, 2008, 10:20:11 pm »
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:

Code: [Select]
    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 );
    }

Offline fireside

  • double
  • *****
  • Posts: 605
    • View Profile
Re: Calculating vertice normals in a VertexController
« Reply #2 on: September 28, 2008, 11:38:46 pm »
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.
« Last Edit: September 28, 2008, 11:54:20 pm by fireside »
click here->Fireside 7 Games<-

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Calculating vertice normals in a VertexController
« Reply #3 on: September 29, 2008, 02:02:34 am »
Congratulations on the promotion to staff sergeant btw.

Thanks!