Weird looking basic normal lighting

I made an application that creates a cubic planet. Each side of the planet is a custom mesh generated using my own cuisine and a coherent noise library (Joise, a java port to the accidental noise library). So far, I’ve been able to do the vertex, index and color buffer successfully. I then tried to calculate the normals with two algorithms:

  1. See @t0neg0d code: Calculating vertex normals of custom mesh - #2 by pspeed

  2. Algorithm with no vector cross product.

Each of the two algorithms have artifacts although the algorithm proposed by @t0neg0d was more efficient in my case. Therefore, I chose her algorithm. The artifacts appear as lighter or darker triangles.

I will put a quick video in the comments where artifacts are cleary seen at the given time.

The code that calculates the normals:

private static Vector3f[] generateNormals(final PlanetOrientation orientation, final Vector3f[] vertices,
		final int verticesCountPerAxis, final float halfCubeSize, final float cellLength) {
	Vector3f[] normals = new Vector3f[vertices.length];

	for (int vertexIndex = 0, x = 0; x < verticesCountPerAxis; ++x)
		for (int y = 0; y < verticesCountPerAxis; ++y, ++vertexIndex) {
			Vector3f vertex = vertices[vertexIndex];
			Vector3f[] adjacentVertices = findAdjacentVertices(x, y, verticesCountPerAxis, vertices, orientation,
					halfCubeSize, cellLength);
			Vector3f[] adjacentNormals = new Vector3f[adjacentVertices.length];

			for (int i = 0; i < adjacentVertices.length; ++i)
				adjacentVertices[i].subtractLocal(vertex);

			for (int i = 0; i < adjacentNormals.length; ++i)
				adjacentNormals[i] = adjacentVertices[i]
						.cross(adjacentVertices[MathExt.cycle(0, adjacentNormals.length, i + 1)]).normalizeLocal();

			normals[vertexIndex] = adjacentNormals[0];
			for (int i = 1; i < adjacentNormals.length; ++i)
				normals[vertexIndex].addLocal(adjacentNormals[i]);

			normals[vertexIndex].normalizeLocal();
		}

	return normals;
}

private static Vector3f[] findAdjacentVertices(final int x, final int y, final int verticesCountPerAxis,
		final Vector3f[] vertices, final PlanetOrientation orientation, final float halfCubeSize,
		final float cellLength) {
	return new Vector3f[] {
			findVertex(x, y + 1, verticesCountPerAxis, vertices, orientation, halfCubeSize, cellLength),
			findVertex(x + 1, y, verticesCountPerAxis, vertices, orientation, halfCubeSize, cellLength),
			findVertex(x, y - 1, verticesCountPerAxis, vertices, orientation, halfCubeSize, cellLength),
			findVertex(x - 1, y, verticesCountPerAxis, vertices, orientation, halfCubeSize, cellLength) };
}

private static Vector3f findVertex(final int x, final int y, final int verticesCountPerAxis,
		final Vector3f[] vertices, final PlanetOrientation orientation, final float halfCubeSize,
		final float cellLength) {
	if (x < 0 || y < 0 || x >= verticesCountPerAxis || y >= verticesCountPerAxis)
		return PlanetOrientation.BACK.rotatePositionLocal(
				new Vector3f(x * cellLength - halfCubeSize, y * cellLength - halfCubeSize, halfCubeSize),
				orientation);

	return vertices[y + x * verticesCountPerAxis].clone();
}

I first thought that the lighting shader was only for vertex lighting but I found out that I was wrong. Then I tried to force per-pixel lighting without noticing any difference.

P.S.: Sorry about the links, I am only allowed to put two or less :slight_smile:

Joise a java port for the accidental noise library.

maybe you should test with this material : Common/MatDefs/Misc/ShowNormals.j3md
its maybe a problem with the generation of your normals

Thank you for your reply! I’ve found a bug in the algorithm, which didn’t fix what I call an artifact, but still fixed another bug: my normals were negated. Instead of adding the normals near the end of the algorithm, I subract them. :slight_smile:

How should lighting affect my terrain? Is it really a bug?