DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Site Discussions > Code & Snapshot Discussion
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 09-23-2004, 05:29 PM   #1
caesar4
New Member
 
Join Date: Mar 2004
Posts: 9
Default

When you load a heightmap in your game, some of you would want to be able to light it, but in order to have lighting, you must also have the vertex normals.
This method is simple, works by averaging the normals of all adjacent triangle normals

Vertex declaration I'm using
Code:
typedef struct _SVNormVertexIColor{ CVector vecPosition; CVector vecNormal; unsigned int uiColor; static const unsigned int FVF; }SVNormVertexIColor; const unsigned int _SVNormVertexIColor::FVF = (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE);

Normal calualation code:
Code:
// we have 'w'-width and 'h'-lenght of the heightmap // i am assuming that you have already read all the vertices into a linear vertex buffer, with the normal vector set to 0.f int _h = h-1; int _w = w-1; // cycle through each row for(int i = 0; i < _h; i++){ // cycle through each column for(int j = 0; j < _w; j++){ CVector p1,p2,p3,v1,v2,tmp; int v[4]; // calculate the positions int the vertex buffer of the adjacent vertices // v[0] is current vertex v[0] = i*h+j; v[1] = i*h+j+1; v[2] = (i+1)*h+j; v[3] = (i+1)*h+j+1; // store the positions of first triangle into 3 usable vertices p1 = this->vertices[v[0]].vecPosition; p2 = this->vertices[v[3]].vecPosition; p3 = this->vertices[v[2]].vecPosition; // calculate the triangle sides v1 = p2 - p1;v2 = p3 - p1; // calculate normal tmp = v1.Cross(v2); // add normal to the normal of all the vertices in the triangle this->vertices[v[0]].vecNormal+= tmp; this->vertices[v[3]].vecNormal+= tmp; this->vertices[v[2]].vecNormal+= tmp; // store the positions of second triangle into 3 usable vertices p1 = this->vertices[v[1]].vecPosition; p2 = this->vertices[v[3]].vecPosition; p3 = this->vertices[v[0]].vecPosition; // calculate the triangle sides v1 = p2 - p1;v2 = p3 - p1; // calculate normal tmp = v1.Cross(v2); // add normal to the normal of all the vertices in the triangle this->vertices[v[1]].vecNormal+= tmp; this->vertices[v[3]].vecNormal+= tmp; this->vertices[v[0]].vecNormal+= tmp; } } // normalize all vertices for(i = 0; i<this->iVertCount; i++) this->vertices[i].vecNormal.Normalize();
caesar4 is offline   Reply With Quote
Old 09-24-2004, 12:07 AM   #2
Mihail121
Senior Member
 
Mihail121's Avatar
 
Join Date: Jan 2003
Posts: 868
Default

Ok i don't want to be rude but this is the third normal-calculating sample in a month!!!
Mihail121 is offline   Reply With Quote
Old 09-30-2004, 09:20 PM   #3
caesar4
New Member
 
Join Date: Mar 2004
Posts: 9
Default

Quote:
Originally Posted by Mihail121
Ok i don't want to be rude but this is the third normal-calculating sample in a month!!!
[snapback]11899[/snapback]
Sure, but none of the other samples included acurate calculations, theyll just take the normal of a triangle, give it to its 3 vertices, and move on to the next triangle
this sample averages the normals of the adjacent triangles (which is partly wy it runs a little slow) and provides more accurate data, for lighting
caesar4 is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 01:28 AM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.