In my current project I am rendering terrain from a height map but when I light the terrain I am getting a striped look. Check out the screenshot to see what I mean. I have even enabled blending, polygon smoothing and and using a GL_SMOOTH shade model but nothing seems to effect the stripes running down the terrain. Here are my surface normal calculations:
private void computeSurfaceNormals(float[][] surfaceNormals)
{
int surfaceNormalsIndex = 0;
for(int y = -1; y <= heightMapLength - 1; y++)
{
float height1, height2, height3;
for(int x = -1; x <= heightMapWidth - 1; x++)
{
if(x == -1 || x == heightMapWidth - 1 || y == -1 || y == heightMapLength - 1)
{
surfaceNormals[surfaceNormalsIndex++] = new float[] {0.0f, 1.0f, 0.0f};
surfaceNormals[surfaceNormalsIndex++] = new float[] {0.0f, 1.0f, 0.0f};
}
else
{
height1 = heightMapData[(y*heightMapWidth)+x];
height2 = heightMapData[((y+1)*heightMapWidth)+x];
height3 = heightMapData[(y*heightMapWidth)+x+1];
float[] vec1 = {0.0f, height2 - height1, 1.0f};
float[] vec2 = {1.0f, height3 - height1, 0.0f};
surfaceNormals[surfaceNormalsIndex++] = GameUtil.crossProduct(vec1, vec2);
height1 = height3;
height3 = heightMapData[((y+1)*heightMapWidth)+x+1];
vec1[1] = height2 - height1;
vec2[1] = height3 - height1;
surfaceNormals[surfaceNormalsIndex++] = GameUtil.crossProduct(vec1, vec2);
} // else
} // for
} // for
} // computeSurfaceNormals
I then normalize the surface normals and calculate the vertex normals with:
private void computeVertexNormals(float[][] surfaceNormals)
{
int triRowOffset = ((heightMapWidth+1)*2);
int triIndex = 0;
int vertexIndex = 0;
float[] currentNormal = new float[3];
float[][] tempVec = new float[6][3];
for(int z = 0; z < heightMapLength; z++)
{
for(int x = 0; x < heightMapWidth; x++)
{
tempVec[0] = surfaceNormals[triIndex];
tempVec[1] = surfaceNormals[triIndex + 1];
tempVec[2] = surfaceNormals[triIndex + 2];
tempVec[3] = surfaceNormals[triIndex + triRowOffset];
tempVec[4] = surfaceNormals[triIndex + triRowOffset + 1];
tempVec[5] = surfaceNormals[triIndex + triRowOffset + 2];
currentNormal[0] = (tempVec[0][0] + tempVec[1][0] + tempVec[2][0] + tempVec[3][0] + tempVec[4][0] + tempVec[5][0]) / 6.0f;
currentNormal[1] = (tempVec[0][1] + tempVec[1][1] + tempVec[2][1] + tempVec[3][1] + tempVec[4][1] + tempVec[5][1]) / 6.0f;
currentNormal[2] = (tempVec[0][2] + tempVec[1][2] + tempVec[2][2] + tempVec[3][2] + tempVec[4][2] + tempVec[5][2]) / 6.0f;
vertexNormals[vertexIndex][0] = currentNormal[0];
vertexNormals[vertexIndex][1] = currentNormal[1];
vertexNormals[vertexIndex][2] = currentNormal[2];
vertexIndex++;
triIndex += 2;
} // for
triIndex += 2;
} // for
} // computeVertexNormals
Anyone know what I am doing wrong here? Thanks for any help in advance.