PDA

View Full Version : Problem with calculating normal of a quad



Rubenknex
05-24-2010, 06:13 AM
Hello I'm ruben and I'm new to this forum :D

I have a problem with the calculation of the normal vector of a quad, my function for calculating the normal always return a vector with coordinates between -1 and 1, the lighting works and all but I want
to draw a line for each vector to see if everything is correct.

here is my function for getting the normal of a quad (i use the first 3 vertices of the quad because the normal points in the same direction anyway).


public void calculateNormal(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, Vec3f normal)
{
Vec3f a = new Vec3f();
Vec3f b = new Vec3f();

a.x = x1 - x2;
a.y = y1 - y2;
a.z = z1 - z2;

b.x = x2 - x3;
b.y = y2 - y3;
b.z = z2 - z3;

normal.x = (a.y * b.z) - (a.z * b.y);
normal.y = (a.z * b.x) - (a.x * b.z);
normal.z = (a.x * b.y) - (a.y * b.x);

float combined = (normal.x*normal.x) + (normal.y*normal.y) + (normal.z*normal.z);
float length = (float)Math.sqrt(combined);

normal.x /= length;
normal.y /= length;
normal.z /= length;
}

Also the size of the terrain which im lighting is 1024*1024 (with a heightmap)

strattonbrazil
05-24-2010, 08:20 AM
For each vertex on the heightmap, draw a line from that vertex to the vertex offset by the normal. A vertex at (0,10,0) with a normal of (.3,.3,.3) would have a line going from (0,10,0) to (.3,10.3,.3).

Rubenknex
05-24-2010, 09:16 AM
Thanks! it works :D