Skip to main content
GameDev.net gamedev.net
🔒 Locked

Fast way to calculate heightmap normals

Started by RajanSky Jun 18, 2003 at 1:03 PM 5 replies 26.8k views
Original Post
RajanSky
RajanSky
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
JohnBolton
JohnBolton
It is not a hack. It is actually the result of taking out all the redundant cross-product calculations. Here is my code for comparison:



static Vector3f ComputeGridNormal( HeightField const & hf, int x, int y )
{
// The 4 adjacent points in a uniform grid: A, B, C, D

//

// B

// |

// C--0--A

// |

// D

//

//

// The ratio of XY-scale to Z-scale: s = Sxy / Sz

// The desired normal: N = cross(A,B) + cross(B,C) + cross(C,D) + cross(D,A), (then normalize)

//

// N.x = 2 * s * (C.z - A.z)

// N.y = 2 * s * (D.z - B.z)

// N.z = 4 * s^2

// normalize( N )

//

// Since N is normalized in the end, it can be divided by 2 * s:

//

// N.x = C.z - A.z

// N.y = D.z - B.z

// N.z = 2 * s

// normalize( N )

//


HeightField::Vertex const * const paV = hf.GetData( x, y );
int const sx = hf.GetSizeX();
int const sy = hf.GetSizeY();
float const scale = hf.GetXYScale();

float const z0 = paV[ 0 ].m_Z;

float const Az = ( x + 1 < sx ) ? ( paV[ 1 ].m_Z ) : z0;
float const Bz = ( y + 1 < sy ) ? ( paV[ sx ].m_Z ) : z0;
float const Cz = ( x - 1 >= 0 ) ? ( paV[ -1 ].m_Z ) : z0;
float const Dz = ( y - 1 >= 0 ) ? ( paV[ -sx ].m_Z ) : z0;

return Vector3( Cz - Az, Dz - Bz, 2.f * scale ).Normalize();
}


[edited by - Jambolo on June 20, 2003 1:05:45 PM]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
RajanSky
RajanSky
Cool, thank you very much Jambolo! Now I have some idea where the formula comes from so finding the z component shouldn''t be too hard

Raj
Mastaba
Mastaba
Anyone care to do the algebra to see if what Jambolo says is true? I can''t, I''ve got c.t.s..
JohnBolton
JohnBolton
quote:
Original post by Mastaba
Anyone care to do the algebra to see if what Jambolo says is true? I can't, I've got c.t.s..
         B
|
C--0--A
|
D

N = AxB + BxC + CxD + DxA, then normalize

note: this is an axis-aligned uniform grid and the distance between the points in the XY plane is Sxy, so Ax = By = -Cx = -Dy = Sxy, and Ay = Bx = Cy = Dx = 0
note: Actual z values may be scaled and this factor must be included in the calculation (e.g. if 0-1000 is scaled to 0-255, Sz = .255)


Nx = Ay*Bz/Sz - Az/Sz*By + By*Cz/Sz - Bz/Sz*Cy + Cy*Dz/Sz - Cz/Sz*Dy + Dy*Az/Sz - Dz/Sz*Ay
= 0 - Sxy*Az/Sz + Sxy*Cz/Sz - 0 + 0 - -Sxy*Cz/Sz + -Sxy*Az/Sz - 0
= Sxy / Sz * ( -Az + Cz + Cz - Az )
= 2 * Sxy / Sz * ( Cz - Az )

Ny = Az/Sz*Bx - Ax*Bz/Sz + Bz/Sz*Cx - Bx*Cz/Sz + Cz/Sz*Dx - Cx*Dz/Sz + Dz/Sz*Ax - Dx*Az/Sz
= 0 - Sxy*Bz/Sz + -Sxy*Bz/Sz - 0 + 0 - -Sxy*Dz/Sz + Sxy*Dz/Sz - 0
= Sxy / Sz * ( -Bz - Bz + Dz + Dz )
= 2 * Sxy / Sz * ( Dz - Bz )

Nz = Ax*By - Ay*Bx + Bx*Cy - By*Cx + Cx*Dy - Cy*Dx + Dx*Ay - Dy*Ax
= Sxy*Sxy - 0 + 0 - -Sxy*Sxy + Sxy*Sxy - 0 + 0 - -Sxy*Sxy
= 4 * Sxy * Sxy

Scale N by 1/2 * Sz/Sxy:

Nx = Cz - Az
Ny = Dz - Bz
Nz = 2 * Sxy * Sz

Normalize.

Hmm! The z value is different! I'm going to have to find out if it affects my code.

Also, it may be inconvenient for Sz to be calculated as Sz = zscaled / zActual. If Sz = zActual / zscaled, then
Nz = 2 * Sxy / Sz



[edited by - JohnBolton on June 30, 2003 12:38:03 PM]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.