Jump to content
  • Advertisement
Sign in to follow this  
Halsafar

Calculate Terrain Normals

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm not quite sure how to calculate the normals for my terrain. I've never had to calculate normals for anything yet so this is foreign. What should be my first step? I have the vertices and the indices used to formulate the triangles.

Share this post


Link to post
Share on other sites
Advertisement
Firstly you need to create the face normals (normals for each triangle). This is quite easy to do. Just get two edges of the triangle and then crossproduct them tog et the normal, for example


v0-----v1
|
|
|
|
v2



edge1 = v1-v0
edge2 = v2-v0
normal = cross(edge1,edge2)
normalize(normal)

or something similar (I might have the order of the edges in the crossproduct the wrong way around, Im fairly drunk/tired right now).

Then once you have done this for every triangle you can find the vertex normals.

You see, if you just use the faec normals your terrain lighting will seem somewhat odd (plus you will have to render with triangle lists not strips). To get the vertex normals, just take the average of all the normals of the rtiangles surrounding the vertex, for example:


v0----v1----v2
|\ |\ |
| \T1 | \T3 |
|T0\ |T2\ |
| \ | \ |
v3---v4----v5
|\ |\ |
| \T5 | \T7 |
|T4\ |T6\ |
| \ | \ |
v6---v7----v8


v4 would average the face normals from T1,T2,T7,T6 and T5 to find the vertex normal. Ive also heard that you should weight the normals, so T2 and T5 would have greater impact since they have a larger angle at v4, though Ive never tied it myself.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drazgal
Im fairly drunk/tired right now

Damn good job for drunk and tired! Rating++

There are also faster approximations by just crossing the diagonal points and using those. It avoids the need to calculate normals for faces first. So in Drazgal's example, you could cross (v8-v0) with (v6-v2) to get the normal for v4. It's not the same calculation, and can cause problems. It works great for my system, though. The real explanation of this can be found in my old post, here. Credit to oliii of course.

Share this post


Link to post
Share on other sites
I have used many kinds of calculations for terrain normals, but the best is just to calculate first face normals and then per vertex normals, even if it's optimized it will take time (more complex terrain structures, more time it takes). So basically:


a = v1-v0
b = v2-v0
r = a cross product b
r.Normalize();


After doing that for each face you'll have to find faces for each vertex :


for (int v=0; v<num_vertices; v++)
{
int num_adjents=0;
int *storage = new int [num_faces];

for (int f=0; f<num_faces; f++)
{
if (faces[f].a == v)
storage[num_adjents++] = f;
else if (faces[f].b == v)
storage[num_adjents++] = f;
else if (faces[f].c == v)
storage[num_adjents++] = f;
}


for (int a=0; a<num_adjents; a++)
{
vertices[v].n.x += faces[storage[a]].nx;
vertices[v].n.y += faces[storage[a]].ny;
vertices[v].n.z += faces[storage[a]].nz;
}

vertices[v].n.x /= num_adjents;
vertices[v].n.y /= num_adjents;
vertices[v].n.z /= num_adjents;

vertices[v].n.Normalize();

delete [] storage;
}




But that isn't the fastest way to do that, you could have list of faces in every vertex and then add the faces in some loop where you go trough faces. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by _DarkWIng_
But try using search next time.. this is anwserd even on the same page you posted...

You may have not noticed, but search is disabled. You could always train everyone to search gamedev with google. By the way, how can you be sure this post was not posted before the other post on this page?

Why is everyone so damn grumpy?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!