View Full Version : Normal calculations look right,but my scene doesnt

02-08-2010, 02:14 AM
Hi there. I'm trying to make a flat shaded heightmap rendering engine, but I can't seem to get it looking the way it should. I've asked around and rechecked but it just refuses to work.
I've posted the code I use to calculate my normals and then render the triangles. Maybe someone with an eye for OpenGL rendering can see what's going on here.

------- Source code ---------
http://utilitybase.com/paste/26026 <-- Vector and cross product classes and functions
http://ec.codepad.org/NsxUim61 <-- Code to calculate normals
http://ec.codepad.org/l3rUCvUR <-- Rendering code(no VBO)

----------- Image results ----------

02-08-2010, 10:21 AM
Maybe a little explanation of the images. The first image is back lit, the white lines and face represents the position of the light source.

The second image is the scene with specular lighting added, and the yellow hairs are vertex[0][0]; vertex[0][0] + the normal for the top left hand triangle.

and the third shows that lighting does somewhat work.

02-09-2010, 02:09 PM
Do you assume that the vertices are give in counter - clockwise

02-09-2010, 03:55 PM
From my understanding everything is stored in CW coordinates. Am I calculating my normals wrong here? From what I just tried when the light is behind something, it lights up, and when it's infront, it gets dark. It's like it's reveresed

a = v1 - v2;
b = v2 - v3;

n = CrossProduct(a, b);
m_normals[(x + (z * m_size)) * 2 + 0] = n;
// Calc the normals of the second
b = v4-v2;
a = v2-v3;

n = CrossProduct(a, b);
m_normals[(x + (z * m_size)) * 2 + 1] = n;

02-10-2010, 01:02 AM
I think that the accepted way to store vertices is CCW
In the application I write now, not all surfaces have the same ordering of vertices

The Hill`s CG book adcices
to calculate normals by Martin Newell method.And then check the directions....If you know that they are CW/CCW the calculation of directions of normals is trivial.

02-12-2010, 01:22 PM
Hi there. I'm trying to make a flat shaded heightmap rendering engine ...

Try putting -


just after you turn lighting back on.