Original Post
Hi everybody! I'm playing around with OpenGL and .NET and I've run into some difficulties (being a DirectX kind of guy). Basically what I want to do is render a heightmapped terrain. I generate the terrain as follows: and render it like this: The z-value is within the range of 0.0 to 1.0. This works as expected. Now I want to generate normals for my terrain and I do so using the method described here. For some reason however, this looks really broken:
I've re-read the form post explaining the algorithm a couple of times but I can't figure out what I'm doing wrong. So I was hoping that someone in here might be able to point me in the right direction. Thanks a lot in advance!
for (int y = 0; y < patchHeight; y++)
{
for (int x = 0; x < patchWidth; x++)
{
Vertex vertex = new Vertex();
vertex.x = x * quadSize;
vertex.y = -(y * quadSize);
vertex.z = HeightForCoordinate(x, y);
patch[y * patchWidth + x] = vertex;
}
}
for (int y = 0; y < patchHeight - 1; y++)
{
for (int x = 0; x < patchWidth - 1; x++)
{
Vertex topLeft = patch[y * patchWidth + x];
Vertex topRight = patch[y * patchWidth + x + 1];
Vertex bottomLeft = patch[(y + 1) * patchWidth + x];
Vertex bottomRight = patch[(y + 1) * patchWidth + x + 1];
GL.Begin(BeginMode.TriangleStrip);
GL.Vertex3(bottomLeft.x, bottomLeft.y, bottomLeft.z);
GL.Normal3(bottomLeft.normal);
GL.Vertex3(topLeft.x, topLeft.y, topLeft.z);
GL.Normal3(topLeft.normal);
GL.Vertex3(bottomRight.x, bottomRight.y, bottomRight.z);
GL.Normal3(bottomRight.normal);
GL.Vertex3(topRight.x, topRight.y, topRight.z);
GL.Normal3(topRight.normal);
GL.End();
}
}
for (int y = 0; y < patchHeight; y++)
{
for (int x = 0; x < patchWidth; x++)
{
int xPred = Math.Max(0, x - 1);
int xSucc = Math.Min(patchWidth - 1, x + 1);
int yPred = Math.Max(0, y - 1);
int ySucc = Math.Min(patchHeight - 1, y + 1);
float sx = patch[y * patchWidth + xPred].z - patch[y * patchWidth + xSucc].z;
float sy = patch[yPred * patchWidth + x].z - patch[ySucc * patchWidth + x].z;
patch[y * patchWidth + x].normal = new Vector3(sx, sy, 2.0f * quadSize);
patch[y * patchWidth + x].normal.Normalize();
}
}
I've re-read the form post explaining the algorithm a couple of times but I can't figure out what I'm doing wrong. So I was hoping that someone in here might be able to point me in the right direction. Thanks a lot in advance!