• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

RajanSky

Fast way to calculate heightmap normals

6 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
Anyone care to do the algebra to see if what Jambolo says is true? I can''t, I''ve got c.t.s..
0

Share this post


Link to post
Share on other sites
Maple''s take on it:

> restart;
> with(linalg):
> assume(Ax,real);
> assume(Ay,real);
> assume(Az,real);
> assume(Bx,real);
> assume(By,real);
> assume(Bz,real);
> assume(Cx,real);
> assume(Cy,real);
> assume(Cz,real);
> assume(Fx,real);
> assume(Fy,real);
> assume(Fz,real);
> A:=array(1..3,[Ax,Ay,Az]):
> B:=array(1..3,[Bx,By,Bz]):
> C:=array(1..3,[Cx,Cy,Cz]):
> F:=array(1..3,[Fx,Fy,Fz]):
> N1:=normalize(crossprod(A,B)):
> N2:=normalize(crossprod(B,C)):
> N3:=normalize(crossprod(C,F)):
> N4:=normalize(crossprod(F,A)):
> N0:=normalize(N1+N2+N3+N4);
>
Warning, new definition for norm
Warning, new definition for trace

[/ Ay~ Bz~ - Az~ By~ By~ Cz~ - Bz~ Cy~
N0 := [|--------------------- + --------------------
[| 2 2 2 1/2 2 2 2 1/2
[\(%11 + %4 + %7 ) (%6 + %3 + %9 )

Cy~ Fz~ - Cz~ Fy~ Fy~ Az~ - Fz~ Ay~ \ / 1/2 /
+ --------------------- + ---------------------| / %13 , |
2 2 2 1/2 2 2 2 1/2| / |
(%8 + %2 + %12 ) (%1 + %5 + %10 ) / \

Az~ Bx~ - Ax~ Bz~ Bz~ Cx~ - Bx~ Cz~
--------------------- + --------------------
2 2 2 1/2 2 2 2 1/2
(%11 + %4 + %7 ) (%6 + %3 + %9 )

Cz~ Fx~ - Cx~ Fz~ Fz~ Ax~ - Fx~ Az~ \ / 1/2 /
+ --------------------- + ---------------------| / %13 , |
2 2 2 1/2 2 2 2 1/2| / |
(%8 + %2 + %12 ) (%1 + %5 + %10 ) / \

Ax~ By~ - Ay~ Bx~ Bx~ Cy~ - By~ Cx~
--------------------- + --------------------
2 2 2 1/2 2 2 2 1/2
(%11 + %4 + %7 ) (%6 + %3 + %9 )

Cx~ Fy~ - Cy~ Fx~ Fx~ Ay~ - Fy~ Ax~ \ / 1/2
]
+ --------------------- + ---------------------| / %13 ]
2 2 2 1/2 2 2 2 1/2| / ]
(%8 + %2 + %12 ) (%1 + %5 + %10 ) / ]

%1 := | Fy~ Az~ - Fz~ Ay~ |

%2 := | Cz~ Fx~ - Cx~ Fz~ |

%3 := | Bz~ Cx~ - Bx~ Cz~ |

%4 := | Az~ Bx~ - Ax~ Bz~ |

%5 := | Fz~ Ax~ - Fx~ Az~ |

%6 := | By~ Cz~ - Bz~ Cy~ |

%7 := | Ax~ By~ - Ay~ Bx~ |

%8 := | Cy~ Fz~ - Cz~ Fy~ |

%9 := | Bx~ Cy~ - By~ Cx~ |

%10 := | Fx~ Ay~ - Fy~ Ax~ |

%11 := | Ay~ Bz~ - Az~ By~ |

%12 := | Cx~ Fy~ - Cy~ Fx~ |

2 2 2 1/2 2 2 2 1/2
%13 := (| (%6 + %3 + %9 ) (%8 + %2 + %12 )

2 2 2 1/2 2 2 2 1/2
(%1 + %5 + %10 ) Ay~ Bz~ - (%6 + %3 + %9 )

2 2 2 1/2 2 2 2 1/2
(%8 + %2 + %12 ) (%1 + %5 + %10 ) Az~ By~ +

2 2 2 1/2 2 2 2 1/2 2 2 2 1/2
(%11 + %4 + %7 ) (%8 + %2 + %12 ) (%1 + %5 + %10 )

2 2 2 1/2 2 2 2 1/2
By~ Cz~ - (%11 + %4 + %7 ) (%8 + %2 + %12 )

2 2 2 1/2 2 2 2 1/2
(%1 + %5 + %10 ) Bz~ Cy~ + (%11 + %4 + %7 )

2 2 2 1/2 2 2 2 1/2
(%6 + %3 + %9 ) (%1 + %5 + %10 ) Cy~ Fz~ -

2 2 2 1/2 2 2 2 1/2 2 2 2 1/2
(%11 + %4 + %7 ) (%6 + %3 + %9 ) (%1 + %5 + %10 )

2 2 2 1/2 2 2 2 1/2
Cz~ Fy~ + (%11 + %4 + %7 ) (%6 + %3 + %9 )

2 2 2 1/2 2 2 2 1/2
(%8 + %2 + %12 ) Fy~ Az~ - (%11 + %4 + %7 )

2 2 2 1/2 2 2 2 1/2 2
(%6 + %3 + %9 ) (%8 + %2 + %12 ) Fz~ Ay~ | + |

2 2 2 1/2 2 2 2 1/2 2 2 2 1/2
(%6 + %3 + %9 ) (%8 + %2 + %12 ) (%1 + %5 + %10 )

2 2 2 1/2 2 2 2 1/2
Az~ Bx~ - (%6 + %3 + %9 ) (%8 + %2 + %12 )

2 2 2 1/2 2 2 2 1/2
(%1 + %5 + %10 ) Ax~ Bz~ + (%11 + %4 + %7 )

2 2 2 1/2 2 2 2 1/2
(%8 + %2 + %12 ) (%1 + %5 + %10 ) Bz~ Cx~ -

2 2 2 1/2 2 2 2 1/2 2 2 2 1/2
(%11 + %4 + %7 ) (%8 + %2 + %12 ) (%1 + %5 + %10 )

2 2 2 1/2 2 2 2 1/2
Bx~ Cz~ + (%11 + %4 + %7 ) (%6 + %3 + %9 )

2 2 2 1/2 2 2 2 1/2
(%1 + %5 + %10 ) Cz~ Fx~ - (%11 + %4 + %7 )

2 2 2 1/2 2 2 2 1/2
(%6 + %3 + %9 ) (%1 + %5 + %10 ) Cx~ Fz~ +

2 2 2 1/2 2 2 2 1/2 2 2 2 1/2
(%11 + %4 + %7 ) (%6 + %3 + %9 ) (%8 + %2 + %12 )

2 2 2 1/2 2 2 2 1/2
Fz~ Ax~ - (%11 + %4 + %7 ) (%6 + %3 + %9 )

2 2 2 1/2 2 2 2 2 1/2
(%8 + %2 + %12 ) Fx~ Az~ | + | (%6 + %3 + %9 )

2 2 2 1/2 2 2 2 1/2
(%8 + %2 + %12 ) (%1 + %5 + %10 ) Ax~ By~ -

2 2 2 1/2 2 2 2 1/2 2 2 2 1/2
(%6 + %3 + %9 ) (%8 + %2 + %12 ) (%1 + %5 + %10 )

2 2 2 1/2 2 2 2 1/2
Ay~ Bx~ + (%11 + %4 + %7 ) (%8 + %2 + %12 )

2 2 2 1/2 2 2 2 1/2
(%1 + %5 + %10 ) Bx~ Cy~ - (%11 + %4 + %7 )

2 2 2 1/2 2 2 2 1/2
(%8 + %2 + %12 ) (%1 + %5 + %10 ) By~ Cx~ +

2 2 2 1/2 2 2 2 1/2 2 2 2 1/2
(%11 + %4 + %7 ) (%6 + %3 + %9 ) (%1 + %5 + %10 )

2 2 2 1/2 2 2 2 1/2
Cx~ Fy~ - (%11 + %4 + %7 ) (%6 + %3 + %9 )

2 2 2 1/2 2 2 2 1/2
(%1 + %5 + %10 ) Cy~ Fx~ + (%11 + %4 + %7 )

2 2 2 1/2 2 2 2 1/2
(%6 + %3 + %9 ) (%8 + %2 + %12 ) Fx~ Ay~ -

2 2 2 1/2 2 2 2 1/2 2 2 2 1/2
(%11 + %4 + %7 ) (%6 + %3 + %9 ) (%8 + %2 + %12 )

2 / 2 2 2 2 2 2 2 2
Fy~ Ax~ | ) / (%4 %3 %2 %5 + %4 %3 %2 %10
/

2 2 2 2 2 2 2 2 2 2 2 2
+ %11 %6 %2 %1 + %11 %6 %2 %5 + %11 %6 %2 %10

2 2 2 2 2 2 2 2 2 2 2 2
+ %11 %6 %12 %1 + %11 %6 %12 %5 + %11 %6 %12 %10

2 2 2 2 2 2 2 2 2 2 2 2
+ %7 %6 %2 %10 + %4 %9 %8 %1 + %4 %9 %8 %5

2 2 2 2 2 2 2 2 2 2 2 2
+ %4 %9 %8 %10 + %4 %6 %8 %1 + %4 %6 %8 %5

2 2 2 2 2 2 2 2 2 2 2 2
+ %4 %6 %8 %10 + %4 %6 %2 %1 + %4 %6 %2 %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1

2 2 2 2 2 2 2 2 2 2 2 2
+ %7 %9 %12 %5 + %7 %9 %12 %10 + %11 %3 %8 %1

2 2 2 2 2 2 2 2 2 2 2 2
+ %11 %3 %8 %5 + %11 %3 %2 %1 + %11 %3 %2 %5

2 2 2 2 2 2 2 2 2 2 2 2
+ %11 %3 %2 %10 + %11 %9 %8 %1 + %11 %9 %8 %5

2 2 2 2 2 2 2 2 2 2 2 2
+ %11 %9 %8 %10 + %11 %9 %2 %1 + %11 %9 %2 %5

2 2 2 2 2 2 2 2 2 2 2 2
+ %11 %9 %2 %10 + %7 %9 %8 %1 + %7 %6 %2 %5

2 2 2 2
+ %4 %3 %2 %1 )


No matter what I tried, I couldn''t get it to simplify any farther.
0

Share this post


Link to post
Share on other sites
I should add the reason I couldn''t get Maple to simplify it is that it kept saying the object was too large for the student edition to simplfy.
0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites