0
Calculate Terrain Normals
Started by Jun 07 2005 10:17 AM
,
5 replies to this topic
#2 Members  Reputation: 368
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
edge1 = v1v0
edge2 = v2v0
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:
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.
v0v1




v2
edge1 = v1v0
edge2 = v2v0
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:
v0v1v2
\ \ 
 \T1  \T3 
T0\ T2\ 
 \  \ 
v3v4v5
\ \ 
 \T5  \T7 
T4\ T6\ 
 \  \ 
v6v7v8
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 Banned  Reputation: 592
Posted 07 June 2005  01:20 PM
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 (v8v0) with (v6v2) 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.
#4 Members  Reputation: 204
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:
After doing that for each face you'll have to find faces for each vertex :
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. :)
a = v1v0
b = v2v0
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. :)
#6 Banned  Reputation: 592
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?