2
\$\begingroup\$

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] = (norm1.add(norm2)).divide(2);
    normals[i+1] = norm1;
    normals[i+2] = (norm1.add(norm2)).divide(2);
    normals[i+3] = norm2;

    }
    return normals;
}

I get the following from this:

enter image description here

Any ideas what is causing this? Thanks in advance.

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

1 Answer 1

2
\$\begingroup\$

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .