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

I am loading an object from . off file . This file format does not define normals for faces or vertices of an object. I found face normals using a vector product. But I find it difficult to find the normals for each vertex, any ideas?

+3
source share
4 answers

Average normals for all faces that share a vertex.

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

+4
source

Some .obj files do not have normals at all. First you must calculate the normals for the face:

, v1, v2, v3, : v1 - v2 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)

:

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).

"" , .

+2

. " " ( 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]);
+1

" , .

.

, , 180.

, :

You have 3 normal vectors: find the bisector for vectors 2 and 3, and then find the bisector for vector 1 and another bisector. Then normalize the result.

0
source

Source: https://habr.com/ru/post/1777570/


All Articles