开发者

How can I find the normals for each vertex of an object so that I can apply smooth shading with OpenGL?

开发者 https://www.devze.com 2023-01-28 14:44 出处:网络
I\'m loading an object from a .off file. This file format does not define the normals for an object\'s faces or vertices. I have found the face normals using a vector product. But I\'m at a loss about

I'm loading an object from a .off file. This file format does not define the normals for an object's faces or vertices. I have found the face normals using a vector product. But I'm at a loss about finding the 开发者_如何学Gonormals for each vertex, any ideas?


Average the normals for all faces that share the vertex.

That is, just add all the adjacent face normals and normalize the result.


Some .obj files doesn't have normals at all. You should at first compute per-face normals:

Given a face composed of 3 vertices v1, v2, v3 you can compute the normal : The normal is the normalized cross product between v1 - v2 and v1 - v3

N = Normalize( (v1 - v2) x (v1 - v3) ) 

Normalize(V) = V / length(V)

length(V) = SQRT (V.x * V.x + V.y * V.y + V.z * V.z)

The cross product :

v × u = (v.y * u.z − v.z * u.y, v.z * u.x − v.x * u.z, v.x * u.y − v.y * u.x).

After that you can compute the "smooth" normals by averaging all the normals of adjacent faces.


See example "Computing normals to achieve flat and smooth shading" (method ComputeVerticeNormal):

// Average all adjacent faces normals to get the vertex normal
GLpoint pn;
pn.x = pn.y = pn.z = 0;
for (int jx = 0; jx < nbAdjFaces; jx++) 
{ 
   int ixFace= m_pStorage[jx];
   pn.x += m_pFaceNormals[ixFace].x;
   pn.y += m_pFaceNormals[ixFace].y;
   pn.z += m_pFaceNormals[ixFace].z;
} 
pn.x /= nbAdjFaces;
pn.y /= nbAdjFaces; 
pn.z /= nbAdjFaces; 

// Normalize the vertex normal 
VectorNormalize(&pn, &m_pVertNormals[ixVertice]);


"verage the normals for all faces that share the vertex.

That is, just add all the adjacent face normals and normalize the result."

I've tried this and sometimes you get strange results when at least two of the normals tend to have a 180 angle.

I think is better to use bisectors :

You have 3 normal vectors : find bisector to vector 2 and 3 and then find bisector to vector 1 and the other bisector. THEN normalize the result.

0

精彩评论

暂无评论...
验证码 换一张
取 消