Hi,
long time since I programmed game related stuff. Anyway I had some spare time and wrote up a heightmapping demo that uses glsl, splatting and per-pixel lighting.
It works pretty good already but i have a VERY small problem with the normal calculation:
The terrain is split up in triangles. Each iteration of the grid has 2 triangles. Now I go and calculate the normal like this:
for(int c=0;c<m_totalTriangles;c++) {
CVector3 Triangle[3];
Triangle[0].x = m_vertex[i++];
Triangle[0].y = m_vertex[i++];
Triangle[0].z = m_vertex[i++];
Triangle[1].x = m_vertex[i++];
Triangle[1].y = m_vertex[i++];
Triangle[1].z = m_vertex[i++];
Triangle[2].x = m_vertex[i++];
Triangle[2].y = m_vertex[i++];
Triangle[2].z = m_vertex[i++];
CVector3 Normals[c] = Normal(Triangle);
}
Each Triangle has a calculated normal, the face normal of this triangle is calulcated so:
CVector3 Normal(CVector3 vTriangle[])
{
CVector3 vVector1 = Vector(vTriangle[2], vTriangle[0]);
CVector3 vVector2 = Vector(vTriangle[1], vTriangle[0]);
CVector3 vNormal = Cross(vVector1, vVector2);
vNormal = Normalize(vNormal);
return vNormal;
}
Now to the problem: When I draw the normals using lines from the center of each triangle you already see something is wrong. In some Triangles the line goes up as it should and it others it faces downwards.
I know its a problem coming from the cross product (right hand rule).
But how do I fix it?
My lighting calculation in the shader is right, but as you can see in this image, its just the same as above.
Would be cool if anyone has an idea about how I can fix this :).