PDA

View Full Version : Calculating vertex normal for OBJ faces



rassi149
11-24-2015, 02:45 AM
So I have this OBJ exporter that is not including any of the vertex normals. This results in the object being inside out(I think that is the reason?). So I thought that I would add the normals myself. I think I got the calculating done to find a normal but I dont know where to place it.

Lets say I have this face "f v1//vn1 v2//vn2 v3//vn3". Which normal is which?

this is what I have done
P1_2=v2-v1
P2_3=v3-v2
P3_1=v1-v3

Calculated the Cross product (P1_2 x P2_3) and divided with the length and so on. Which vertex normal is this? is it vn1, vn2 or vn3?

GClements
11-24-2015, 05:10 AM
So I have this OBJ exporter that is not including any of the vertex normals. This results in the object being inside out(I think that is the reason?).

That isn't the reason. Vertex normals are only used for lighting. Face culling is based upon face normals, which are calculated from vertex positions. If the object is inside-out, either your Z coordinates are negated, or you're using the wrong depth-comparison function, or you're using the wrong winding direction (use glFrontFace() to change it).



Lets say I have this face "f v1//vn1 v2//vn2 v3//vn3". Which normal is which?

vn1 is the normal to the surface at the vertex v1. Likewise for the other two.



this is what I have done
P1_2=v2-v1
P2_3=v3-v2
P3_1=v1-v3

Calculated the Cross product (P1_2 x P2_3) and divided with the length and so on. Which vertex normal is this? is it vn1, vn2 or vn3?
None of them. That's the face normal.

rassi149
11-24-2015, 05:59 AM
If the object is inside-out, either your Z coordinates are negated, or you're using the wrong depth-comparison function, or you're using the wrong winding direction

I noticed that the z and y coordinate for the vertexpoints were changed but not the faces! Do I need the vertex normals to create the UV map? do you know how I can create it? If you do, do you know how I calculate them since I have calculated the face normal and not the vertex normals?

GClements
11-24-2015, 09:16 AM
Do I need the vertex normals to create the UV map?
No. Vertex normals are only used for lighting calculations.


do you know how I can create it? If you do, do you know how I calculate them since I have calculated the face normal and not the vertex normals?
In the absence of any other source, a common approach is to average the face normals of all of the faces to which a vertex belongs.

rassi149
11-25-2015, 01:33 AM
No. Vertex normals are only used for lighting calculations.
Lightning calculations as in smooth/phong shading?


In the absence of any other source, a common approach is to average the face normals of all of the faces to which a vertex belongs.
How do I know which faces belong to a vertex?

rassi149
11-25-2015, 02:07 AM
Is it like this




//imaginaryFile.obj
v x y z //1
v x y z //2
v x y z //3
v x y z //4
v x y z //5
v x y z //6

f 1 2 1
f 1 3 2
f 4 1 2
f 4 5 6

would this mean that for v1, I should average the face normals (which i've calculated?) for face 1-3 since it contains vertex 1?

GClements
11-25-2015, 02:26 AM
Lightning calculations as in smooth/phong shading?
Yes.

How do I know which faces belong to a vertex?


Is it like this


//imaginaryFile.obj
v x y z //1
v x y z //2
v x y z //3
v x y z //4
v x y z //5
v x y z //6

f 1 2 1
f 1 3 2
f 4 1 2
f 4 5 6


would this mean that for v1, I should average the face normals (which i've calculated?) for face 1-3 since it contains vertex 1?
Yes.

rassi149
11-25-2015, 02:44 AM
Thanks for explaining but this is still very confusing. Is the average of the face normals for a vertex, the vertex normal for those faces? or what should I do with the average of the face normals of one vertex? And this is for the shading and not for the UV-mapping, because that is what the texture coordinates(VT) are for?

GClements
11-25-2015, 03:09 AM
Thanks for explaining but this is still very confusing. Is the average of the face normals for a vertex, the vertex normal for those faces?

No, it's the vertex normal for that vertex.

Vertex normals are used for smooth shading, where the polygon mesh is an approximation to a smooth surface. The vertices are samples of the surface, each having a position and a normal. Visually, we distinguish hard edges by discontinuities (sudden changes) in the lighting. If the lighting at a vertex is the same for all faces which use that vertex, there will be no discontinuity and so we don't perceive hard edges between faces, i.e. the surface appears smooth.



or what should I do with the average of the face normals of one vertex?

Use it as the vertex normal. For smooth shading, each vertex has a position and a normal, specified e.g. by glVertex() and glNormal() or by glVertexPointer() and glNormalPointer().



And this is for the shading and not for the UV-mapping, because that is what the texture coordinates(VT) are for?
Yes. Texture mapping uses texture coordinates.

rassi149
11-25-2015, 06:23 AM
No, it's the vertex normal for that vertex.
What I meant was that the vertex normal vn for the vertex v is "f v//vn..."

But wouldnt this mean that whenever the same vertex appears in a face, it has the same normal?


v x y z //1
v x y z //2
v x y z //3
v x y z //4
v x y z //5
v x y z //6

vn x y z //vn1
vn x y z //vn2
vn x y z //vn3

f 1 2 1 //f1
f 1 3 2 //f2
f 4 1 2 //f3
f 4 5 6 //f4

Lets say the average of the face normals to vertex 1, face 1,2 and 3, is the first normal vector(vn1).
Whenever vertex 1 appears, wouldnt it always be connected to the first vn?


f 1//1 2//? 1//1
f 1//1 3//? 2//?
f 4//? 1//1 2//?
f 4//? 5//? 6//?

Because this is not the case in a OBJ file looking at it can be like this



f 1//1 2//? 1//1
f 1//1 3//? 2//?
f 4//? 1//59 2//?

GClements
11-25-2015, 07:15 AM
What I meant was that the vertex normal vn for the vertex v is "f v//vn..."
If the OBJ file contains vertex normals, there's no need to calculate them. If an OBJ file contains vertex normals, they should normally be preferred to anything you calculate yourself, as a modelling program often has more information available to it than is found in the OBJ file; e.g. it may allow the sharpness of edges to be controlled by the user, or it may have generated the mesh from e.g. a NURBS surface where the normals can be calculated exactly.



But wouldnt this mean that whenever the same vertex appears in a face, it has the same normal?

If faces don't have separate indices for vertex normals (i.e. "f v1 v2 v3" rather than "f v1//vn1 v2//vn2 v3//vn3"), then there is a one-to-one correspondence between vertices and normals, i.e. the surface is completely smooth without any sharp edges.

If faces have separate indices for vertex positions and vertex normals, this allows for two other possibilities: multiple vertex positions with the same normal (e.g. if a surface has a flat region, all vertices within that region will have the same normal), or multiple normals for the same vertex position (i.e. sharp edges).