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-02-2004, 01:43 AM   #1
bladder
DevMaster Staff
 
bladder's Avatar
 
Join Date: Sep 2003
Location: Hell
Posts: 1,109
Default

Just thought I'd post something small.

Given a list of vertices and a list of indices..

Code:
// Some standard mesh information that you should have lying around. // vertex is your vertex structure that just contains a normal and position here. // vertices is a pointer to the first vertex // indices is a pointer to the first index // num_verts is number of vertices // num_indices is number of indices // each face of the mesh is made up of three vertices. std::vector<Vector3>* normal_buffer = new std::vector<Vector3>[num_vertices]; for( int i = 0; i < num_indices; i += 3 ) { // get the three vertices that make the faces Vector3 p1 = vertices[indices[i+0]]; Vector3 p2 = vertices[indices[i+1]]; Vector3 p3 = vertices[indices[i+2]]; Vector3 v1 = p2 - p1; Vector3 v2 = p3 - p1; Vector3 normal = v1.Cross( v2 ); normal.Normalize(); // Store the face's normal for each of the vertices that make up the face. normal_buffer[indices[i+0]].push_back( normal ); normal_buffer[indices[i+1]].push_back( normal ); normal_buffer[indices[i+2]].push_back( normal ); } // Now loop through each vertex vector, and avarage out all the normals stored. for( int i = 0; i < num_vertices; ++i ) { for( int j = 0; j < normal_buffer[i].size(); ++j ) vertices[i].normal += normal_buffer[i][j]; vertices[i].normal /= normal_buffer[i].size(); }
___________________________________________
- TripleBuffer
- Me blog
bladder is offline   Reply With Quote
Old 09-02-2004, 05:36 AM   #2
SigKILL
Valued Member
 
Join Date: Aug 2004
Location: Norway
Posts: 200
Default

I'm new here but I remember

http://www.devmaster.net/forums/index.php?showtopic=414

It's a very nice way to calculate vertex normals fast thought. However, that vertex normals should be the average of the connecting triangle normals is purely speculative though. There is a paper that compares different methods for computing vertex normals somewhere (www.google.com and citeseer are our friends), but IIRC they didn't find any "best" solution, but I think averaged normals was good in general (might been weighted average (?)).

Anyways, to get weighted average based on triangle areals, simply don't normalize triangle normals before adding them (and then continue as bladder does)...

just my 0.02kr.

-Si
SigKILL is offline   Reply With Quote
Old 10-26-2004, 10:14 PM   #3
SimmerD
New Member
 
Join Date: Oct 2004
Posts: 5
Default

There seems to be a bug in this example, as well as an optional optimization.

1) The code averages the normals, but doesn't renormalize ( this is the bug ).

2) There is no need to average normals, just sum them up, and then instead of dividing by the # of normals, simply renormalize.

This way you avoid doing two divides, one by the # of normals, and the other by the normal length squared.
SimmerD 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 12:44 PM.


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