Pixelated Normal Generation

I am generating normals for each vertex in a standard 512*512 quad. Yet when I do using the following code:

public Vector3f[] normals(Vector3f[] surface){
Vector3f[] normals = new Vector3f[surface.length];
for(int i = 0; i<normals.length; i+=4){

Vector3f n1 = surface[i];
Vector3f n2 = surface[i+1];
Vector3f n3 = surface[i+2];
Vector3f n4 = surface[i+3];

Vector3f p1 = n1.subtract(n2);
Vector3f p2 = n3.subtract(n2);
Vector3f p3 = n3.subtract(n4);
Vector3f p4 = n1.subtract(n4);

Vector3f norm1 = (p2.cross(p1)).normalize();
Vector3f norm2 = (p4.cross(p3)).normalize();
normals[i+1] = norm1;
normals[i+3] = norm2;

}
return normals;
}


I get the following from this:

Any ideas what is causing this? Thanks in advance.

• Is there any rounding going on anywhere? It's hard to tell from your code. Feb 23, 2013 at 3:40
• normals[i+2] = (norm1.add(norm2)).divide(2) maybe this operation cause integer division try 2f. Feb 23, 2013 at 10:29

It seems that you are calculating p4 incorrectly. Should it not be n2.subtract(n4)? Also it looks like you should swap the assignment for normals[i] and normals[i+1]

This is of course assuming that you have these vertices laid out in a triangle strip fashion and that the 2 triangles that are part of this quad are not necessarily coplanar.

The other thing that bothers me is that you are not taking into account the shared vertices for adjacent edges on the quads. Essentially you should be taking an average of the 4 normals for the 4 planar surfaces that surround each point(the inner points at least... edge points obviously have less surfaces surrounding them). That way after you do the lighting calculations the edges wont appear as sharp lines.

• Thanks a lot! I ended up taking the average of the normals that shared one vertex and it smoothed everything out nicely. Mar 12, 2013 at 23:25
• I'm having the same problem (gamedev.stackexchange.com/questions/102340/…). May I know how you solved the problem please? Jun 13, 2015 at 16:14