3

I have a strange issue where my normals just do not work when I render terrain. My terrain renders just fine, so I left out all the code for calculating the terrain points from a height map and how I calculated the indices. I know I should be using shaders, but I want to get this fixed first before I move on. I am assuming the issue comes from something obvious I have overlooked in my normals generation code, which is as follows:

for (currentind = 0; currentind < indices.size() - 3; currentind+=3)
{

indtopt = indices[currentind] * 3;
point1.vects[0]=terrainpoints[indtopt];//x
point1.vects[1]=terrainpoints[indtopt+1];//y
point1.vects[2]=terrainpoints[indtopt+2];//z


indtopt = indices[currentind+1] * 3;
//second indice

//points of that indice
point2.vects[0]=terrainpoints[indtopt];//x
point2.vects[1]=terrainpoints[indtopt+1];//y
point2.vects[2]=terrainpoints[indtopt+2];//z

indtopt = indices[currentind+2] *3;
//third indice

//points of that indice
point3.vects[0]=terrainpoints[indtopt];//x
point3.vects[1]=terrainpoints[indtopt+1];//y
point3.vects[2]=terrainpoints[indtopt+2];//z

//--------------------------------------------------
point4.vects[0]=(point2.vects[0]-point1.vects[0]);
point4.vects[1]=(point2.vects[1]-point1.vects[1]);
point4.vects[2]=(point2.vects[2]-point1.vects[2]);

point5.vects[0]=(point3.vects[0]-point2.vects[0]);
point5.vects[1]=(point3.vects[1]-point2.vects[1]);
point5.vects[2]=(point3.vects[2]-point2.vects[2]);
//-------------------------------------------------

//cross product
point6.vects[0]=point4.vects[1]*point5.vects[2] - point4.vects[2]*point5.vects[1];
point6.vects[1]=point4.vects[2]*point5.vects[0] - point4.vects[0]*point5.vects[2];
point6.vects[2]=point4.vects[0]*point5.vects[1] - point4.vects[1]*point5.vects[0];

point6 = point6.normalize();
ternormals[currentind]=point6.vects[0];
ternormals[currentind+1]=point6.vects[1];
ternormals[currentind+2]=point6.vects[2];
}

Below is a picture of what the issue is in both wireframe and triangle renders:

Terrain render striping issue

I can post more code if need be, but I just wanted to keep this post short, so I tried to find where I thought the issue might be.

datenwolf
  • 159,371
  • 13
  • 185
  • 298

1 Answers1

1

Well, for every "dark" band you're accidently flipping the normal, probably because the surface tangent vectors are passed into the cross product in the wrong order

a × b = - (b × a)

If your terrain is made of triangle strips, then you've got a bidirectional ordering, which means, that you have to either flip the operands or negate the result of the cross product for every odd row.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I thought that might be the case. The terrain is actually rendered of triangles, though, so no need for bidirectional ordering. – user2446445 Aug 12 '13 at 05:12
  • Well, something is flipping your normals. As an easy fix I suggest you simply test if the normal point downward, and in that case just flip the normal; also you should flip the drawing order of the triangle, otherwise it gets culled if culling get enabled. – datenwolf Aug 12 '13 at 07:56