# Thread: Calculating per vertex normals for a mesh

1. ## Calculating per vertex normals for a mesh

Hello,

I am trying to experiment with GLSL lighting and in particular with implementing the Phong lighting algorithm. I am not sure if the shader implementation I have is wrong or not but I have to verify that the normals that are sent to the shader are correct.

My code takes a mesh of vertices and indices and calculates the normal with the following way I figured out by checking various websites around the net:
Code :
```1. Iterates the triangles and calculates surface normals
2. Counts how many surfaces a vertex belongs to and sums all the surface normals each vertex belongs to
3. Using the above calculation the average normal of each vertex is calculated
4. It is reduced to a unit vector.```

The above does not seem to be working. It is quite possible it might be an error in the code, and if so I will post code also but I figured I should first ask if the logic is correct first.

The result of this is some kind of strange lighting which makes the objects look transparent. I tested it with a simple rectangle and below you can observe the results when you are looking at it from the front. The light source is located above the eye coordinates(on our head).

Here is another example, the monkey model found in Blender, the open source 3d modeler.

In both cases the problem is that they all look transparent from some particular angle views. Anyone has any idea as to what might be the problem here?

2. ## Re: Calculating per vertex normals for a mesh

You can merge steps 3 and 4 to simple normalization of the sum. Other than that, the basic idea should work.

To debug this, to see if your triangle normals are right, you could copy the triangle normal to each corner, however you would also need to duplicate each vertex for each triangle.

Do you have depth testing enabled? Is your back face culling setup correctly?

3. ## Re: Calculating per vertex normals for a mesh

Now that you mention it tskuoran when doing the switch from fixed functionality to shaders it seems that I omitted depth testing and back-face culling. Thought I had it on.

How do you accomplish this when working with shader and openGL 3.0?

The code below is fixed functionality and produces a white screen now if I try to input it into my code.
Code :
``` glClearDepth(1.0f);
glClearStencil(0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);```

4. ## Re: Calculating per vertex normals for a mesh

The code below is fixed functionality and produces a white screen now if I try to input it into my code.

Code: glClearDepth(1.0f);
glClearStencil(0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
There is nothing there which produces white. The error is else where in your code.

5. ## Re: Calculating per vertex normals for a mesh

I am sorry, I should have mentioned that white is the color I clear the background to, as can be seen in the above screenshots. So basically if I add the code above the objects do not get drawn.

I will have to check my code as to why and I suspect (and hope) that this is where I will find the problem in the original question too.

6. ## Re: Calculating per vertex normals for a mesh

I found some mistakes in my implementation of the phong lighting shader so I switched to a much simpler shader which is able to showcase the problem better.

As for depth testing I had mismatching values in the glDepthRange() and in gluPerspective zNear and Zfar, this is why it seemed that depth testing was culling everything.

Now having corrected these, the problem of some primitives of the object being drawn over parts it should not draw persists. Anyone has any idea why is that? Here are two additional screenshots that might help clarify

Monkey from good angle:

Teapot from good angle:

Anyone has any pointers to give me? From the shader correction it is now evident that it is not the normals at fault for the problem but something else must be the cause. What would that be?

7. ## Re: Calculating per vertex normals for a mesh

Looks like you do not have a working depth test, so basically, you end up with newly drawn polygons overwriting previous ones.

8. ## Re: Calculating per vertex normals for a mesh

Do you have a depth buffer? Show us your context creation code (or if you're using something like GLFW or FreeGLUT, show us what you're using to initialize them).

9. ## Re: Calculating per vertex normals for a mesh

Thank you for your replies. Yes it has to be the depth test but as far as I can see I am setting it up correctly:

Code :
```    //back face culling
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

//depth testing
glClearDepth(1.0f);
glClearStencil(0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthRange(1,100);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);```

As for the context creation code Reinhart requested it's made by a system I am working on, and well since I am currently working on windows the code at the final level makes winapi calls.

So the main part of the code you would be interested in is the PixelFormatDescriptor. It's basically taken from Nehe's lessons
Code :
```		const	PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
1,											// Version Number
PFD_DRAW_TO_WINDOW |						// Format Must Support Window
PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
PFD_DOUBLEBUFFER,							// Must Support Double Buffering
PFD_TYPE_RGBA,								// Request An RGBA Format
32,										    // Select Our Color Depth
0, 0, 0, 0, 0, 0,							// Color Bits Ignored
0,											// No Alpha Buffer
0,											// Shift Bit Ignored
0,											// No Accumulation Buffer
0, 0, 0, 0,									// Accumulation Bits Ignored
16,											// 16Bit Z-Buffer (Depth Buffer)
0,											// No Stencil Buffer
0,											// No Auxiliary Buffer
PFD_MAIN_PLANE,								// Main Drawing Layer
0,											// Reserved
0, 0, 0										// Layer Masks Ignored
};```

I also have multisampling support so at some point I am remaking the window with a similar pfd used for multisampling which still has the same depth buffer size of 16.

I think the problem is in me not understanding how depth testing actually works. I believe I set it up to work correctly but my values are wrong because I do not understand how it works. What exactly does depth range do?

My gluPerspective call is like this, with e being the model currently drawn.
Code :
`gluPerspective(45.0f,(GLfloat)(e->mWidth/e->mHeight),1,100.0);`

and that is why my glDepthRange call is with the same zNear and Zfar:
Code :
`glDepthRange(1,100);`

But If I am to be honest that is pretty arbitrary. I am guessing as to how it might work. Is the mistake here? How exactly does depth testing work? Any links to any article explaining it sufficiently and also explaining how to setup the right parameters?

I was also wondering ... is there any way to not do it with calls from the main program but somehow move it in a shader?

Thanks for taking the time to reply.

10. ## Re: Calculating per vertex normals for a mesh

Maybe the problem is not with depth testing but with the order of vertices in the triangles.

Have you tried turning back-face culling off or changing the direction from CCW to CW (or vice-versa)?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•