• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Calculate Terrain Normals

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Halsafar  Members

Posted 07 June 2005 - 10:17 AM

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.

### #2Drazgal  Members

Posted 07 June 2005 - 01:06 PM

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.

### #3/ Jiia   Banned

Posted 07 June 2005 - 01:20 PM

Quote:
 Original post by DrazgalIm 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.

### #4Arex  Members

Posted 07 June 2005 - 08:36 PM

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-v0b = v2-v0r = a cross product br.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. :)

### #5_DarkWIng_  Members

Posted 07 June 2005 - 09:07 PM

But try using search next time.. this is anwserd even on the same page you posted...
You should never let your fears become the boundaries of your dreams.

### #6/ Jiia   Banned

Posted 08 June 2005 - 02:11 AM

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?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.