Problem with calculating the normal vector of a quad

Hello I’m ruben and I’m new to this forum :slight_smile:

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)

I believe it should be:


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

because you want a and b to be the direction vectors both emanating from the same vertex (in this case v2) when you cross them. Off the top of my head, everything else looks okay.

If you want to draw a line for each normal, you’d have to generate geometry (or use glVertex) to create line segments starting at a vertex and ending at (vertex + SCALE * normal).

Yes but is it right that the coordinates of my normals are always something like this:

x:0,071653  y:-0,955364  z:-0,286609

I don’t get how the lighting works with coordinates between -1 and 1 while the maps dimensions are 1024x1024, wouldnt a normal be like

x:234  y:90  z:512

???

Yay it works :D, forgot to add the normal coords :-X

Yes, a normal is a vector that is “perpendicular” to the surface/vertex it’s on. It’s also a normalized vector so it’s length should always be 1, which means each coordinate will be between [-1, 1]. The lighting works by taking the dot product between the normal and the light’s direction (or direction to the point light) and then does some other calculations.