In the picture below you will see my current setup. I am rendering a sphere with heightmap information. The mesh is a square that is wrapped around a sphere. The source of light is located directly at the camera position. The problem that I have at the moment is the calculation of normals.
From what I found, one way of getting the normal vector on a continous surface, is for every vertex, find all vertices that are adjacent to it i nthe mesh (in my case I have 6 neighboring vertices) finding the normals to all six planes that they are forming, then adding them up and normalizing.
You can see the code below. Unfortunately, for some parts of the planet it seems to work, but not for everything. Also notice the strange shadow line that seems to go diagonnaly across the texture.
Any ideas what could be causing this behaviour?
EDIT: the issue is not related to the weight of the adjecent planes in the final normal calculation.
private Vector3f calcNormal(int x, int z)
{
if ( x<1 || x> RESOLUTION-2 || z<1 || z > RESOLUTION -2) {
Vector3f newNormal = new Vector3f();
newNormal = globe[x][z].sub(this.getTransform().GetPos()).normalize();
return newNormal;
}
Vector3f center = globe[x][z];
Vector3f up = globe[x][z-1];
Vector3f down = globe[x][z+1];
Vector3f left = globe[x-1][z];
Vector3f right = globe[x+1][z];
Vector3f rightUp = globe[x+1][z-1];
Vector3f leftDown = globe[x-1][z];
Vector3f dUp = new Vector3f(center).sub(up);
Vector3f dRightUp = new Vector3f(center).sub(rightUp);
Vector3f dRight = new Vector3f(center).sub(right);
Vector3f dDown = new Vector3f(center).sub(down);
Vector3f dLeft = new Vector3f(center).sub(left);
Vector3f dLeftDown = new Vector3f(center).sub(leftDown);
Vector3f normal = new Vector3f(dUp.cross(dRightUp)).
add(new Vector3f(dRightUp.cross(dRight))).
add(new Vector3f(dRight.cross(dDown))).
add(new Vector3f(dDown.cross(dLeftDown))).
add(new Vector3f(dLeftDown.cross(dLeft))).
add(new Vector3f(dLeft.cross(dUp)));
return normal.normalize();
}