[Solved] normals in low poly quads Terrain

Started by Andrew Alfazy, January 14, 2018, 07:31:02

Previous topic - Next topic

Andrew Alfazy

Hello,
Need help with normals I'm Trying to make low poly quads Terrain (the normals for all 4 Vertices should be 1) so is this method good?
private static Vector3f getNormal(Vector3f v1, Vector3f v2, Vector3f v3,
			Vector3f v4) {
		Vector3f a = new Vector3f(), b, n1, n2, n3, n4, n;
		b = a;
		n = a;
		// N1
		v1.sub(v2, a);
		v1.sub(v4, b);
		n1 = a.cross(b);
		// N2
		v2.sub(v3, a);
		v2.sub(v1, b);
		n2 = a.cross(b);
		// N3
		v3.sub(v4, a);
		v3.sub(v2, b);
		n3 = a.cross(b);
		// N4
		v4.sub(v1, a);
		v4.sub(v3, b);
		n4 = a.cross(b);
		// N
		n1.half(n2, a);
		n3.half(n4, b);
		a.half(b, n);
		return n;
	}

NOTE: I'm using JOML
thank you and sorry for my bad English :-[

Andrew Alfazy

I found the problem but I can't solve it.
the problem in the calculation how can I calculate normals for quad?
note = distance between point (x or z) and other is 10 the only y is changing.

KaiHH

If it is really a quad, that is, all vertices lie on the same plane, then you just need to take any triangle of the quad and compute the normal of it with a single cross product like so:
// v2 - v1
Vector3f v12 = v2.sub(v1, new Vector3f());
// v3 - v2
Vector3f v23 = v3.sub(v2, new Vector3f());
// (v2 - v1) x (v3 - v2)
return v12.cross(v23).normalize();

While conceptually correct (all cross products on the spanning vectors of a quad's vertices should be the same), your code suffers from aliasing between the 'nX' variables, 'a', 'b' and 'n' (i.e. those are all the same object).

Andrew Alfazy

thanks now I can see how to solve it :)

TIP:Never try to make low poly quads terrain Triangles better