Jump to content
  • Advertisement
Sign in to follow this  
moetman

Terrain Normal Calculation Problem

This topic is 4789 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

Trying to calculate face normals and average them I'm getting this: http://www.hotkey.net.au/~paulp_adsl/CAVALRY/images/ter1.gif Does anyone know why ? Is this normal ? Please I could really do with some help. Regards CODE: ///////////////////////////////////////////////////////////////////////////////////////////////////////// void Terrain::CalculateNormals() { VECTOR v1,v2,v3,v4,n,va,vb,facet; for (int x = 0; x < MAP_SIZE - 1; x++) { for (int y = 0; y < MAP_SIZE - 1; y++) { v1.X = x;//vertex 1 v1.Y = y; v1.Z = mesh[x][y]; v2.X = x+1;//vertex 2 v2.Y = y; v2.Z = mesh[x+1][y]; //vertex 1 - vertex 2 va.X = v1.X - v2.X; va.Y = v1.Y - v2.Y ; va.Z = v1.Z - v2.Z ; v3.X = x;//vertex 3 v3.Y = mesh[x][y+1]; v3.Z = y+1; //vertex 3 - vertex 4 vb.X = v3.X - v1.X; vb.Y = v3.Y - v1.Y ; vb.Z = v3.Z - v1.Z ; facet = crossproduct(va,vb); facet = Normalize(facet); normals[x][y] = facet; } } } ///////////////////////////////////////////////////////////////////////////// //BuildTERRain CalculateNormals(); VECTOR t1,t2,t3,t4,t5,t6,vNormal; for (int x = 0; x < MAP_SIZE - 1; x++)//STEP_SIZE { // Draw A Triangle Strip For Each Column Of Our Mesh glBegin(MODE); for (int y = 0; y < MAP_SIZE - 1; y++) { glBindTexture(GL_TEXTURE_2D, texture[0]); t1 = normals[x][y]; t2 = normals[x+1][y]; t3 = normals[x+2][y]; t4 = normals[x][y+1]; t5 = normals[x+1][y+1]; t6 = normals[x+2][y+1]; //vNormal[x][y] = ((t1 + t2 + t3 + t4 + t5 + t6) / 6); vNormal.X = fabs((t1.X + t2.X + t3.X + t4.X + t5.X +t6.X)/6); vNormal.Y = fabs((t1.Y + t2.Y + t3.Y + t4.Y + t5.Y +t6.Y)/6); vNormal.Z = fabs((t1.Z + t2.Z + t3.Z + t4.Z + t5.Z +t6.Z)/6); vNormal = Normalize(vNormal); float height = mesh[x][y]; glNormal3f(vNormal.X,vNormal.Y,vNormal.Z); glVertex3f(float(x),(float)y,height); height = mesh[x][y+1]; glNormal3f(vNormal.X,vNormal.Y,vNormal.Z); glVertex3f(float(x),float(y+1),height); height = mesh[x+1][y]; glNormal3f(vNormal.X,vNormal.Y,vNormal.Z); glVertex3f(float(x+1),float(y),height); height = mesh[x+1][y+1]; glNormal3f(vNormal.X,vNormal.Y,vNormal.Z); glVertex3f(float(x+1),float(y+1),height); } glEnd(); } }

Share this post


Link to post
Share on other sites
Advertisement
Shouldn't


v3.X = x;//vertex 3
v3.Y = mesh[x][y+1];
v3.Z = y+1;


be


v3.X = x;//vertex 3
v3.Y = y+1
v3.Z = mesh[x][y+1];

Share this post


Link to post
Share on other sites
Yea it should ..thanks krum , but unfortunately it didn't help my crap lighting . Are there any examples around of textured triangles with normals calculated ?

Share this post


Link to post
Share on other sites
I really can't work out from that screenshot whether your normals are following the terrain, but the hard edges on the normals are correct. This is because you're calculating face normals and not vertex normals, which would give you the smooth transitions I think you're looking for. Calculating vertex normals is straightforward - find every face sharing a particular vertex & average (or interpolate in some other way) the normals for that set of faces. Do the same for every other vertex, and when it's time to draw, provide these new normals on a per-vertex basis.

The results look similar to this. You can see the original face normals appear around the edges as a border - that's purely because I was too lazy to change my calculations for edges (with less adjacent faces than elsewhere) as it's done on the fly in this example.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I'm calculating a = v1-v2 b = v2-v3 ,taking XProduct, normalizing and setting the normal[] = resulting normal for that vertex x,y. When you say I'm "calculating face normals and not vertex normals" I'm averaging the surrounding face normals and setting it for that particular triangle. Isn't this what you're supposed to do ? So I should give a diffent normal value for EVERY vertex?

Also:

F1 = facet 1
F2 = facet 2

3-------4
| \ |
| \ F2 |
| \ |
| F1 \ |
1-------2

a = v1-v2
b = v2-v3 etc for F1

What about calculating F2 ? do I have to do that as well , how do I do it ?

When I'm averaging normals: Should I take into account F2 ?


Thanks DrewGreen.



Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
So I should give a diffent normal value for EVERY vertex?


Yes.

This image shows a few connected triangles. To calculate the vertex normal of v1, take the face normals of faces A, B, C, D, E, F as they are all sharing that vertex.

If I understood that question correctly you should take F2 into account when calculating the normals for 3 and 2, but not for 1. Likewise don't take F1 into account when calculating 4.

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!