PDA

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

dirk103
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

----------- Image results ----------
http://up.mibbit.com/up/06o2n8oG.png
http://up.mibbit.com/up/8LkRvtkd.png
http://up.mibbit.com/up/uxcfl8sS.png

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

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

dirk103
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);
n.Normalize();
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);
n.Normalize();
m_normals[(x + (z * m_size)) * 2 + 1] = n;

Irena
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