Original Post
Hi!
My friend and I are working on optimizing a terrain system and a water rendering system which involve dynamic deformations every frame. Besides the fact that we''re constantly locking and unlocking our vertex buffers, one of our slowdowns is that we have to re-calculate the normals whenever the surface deforms, which is very expensive using the standard method. (Take the cross product of two vectors from a triangle)
So, I remember in Yann''s water rendering lecture, he provided some code which calculates the normals directly from a heightmap based on height differentials. We tried this and it works (we got an instant 20% speedup), but we had to create some "fudge factor" to scale the Z component of the vector.
Here''s the basic algorithm... At every vertex of the heightmap, you compute the components of your normal vector as:
for y = 0 to ySize - 1
for x = 0 to xSize - 1
n.x = Height[x-1][y] - Height[x+1][y];
n.y = Height[x][y-1] - Height[x][y+1];
n.z = 2 / xSize + 2 / ySize;
normalize(n);
next x
next y
Where Height is a heightmap, and xSize and ySize are the dimensions of the heightMap...
Anyways, I think this formula makes some assumptions about the ranges of values used for the heights and the spacing of the vertices in world coordinates. So, to get it to work for us, we had to multiply the z component by 100 (fudge factor). This is just a hack though, so does anyone know how this works, and in particular, how the Z-component is derived?
If anyone has any insight on how this works or how to fix it I''d really appreciate it!
Thank you very much,
Raj