Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
Is there any rounding going on anywhere? It's hard to tell from your code. –  ashes999 Feb 23 '13 at 3:40
    
normals[i+2] = (norm1.add(norm2)).divide(2) maybe this operation cause integer division try 2f. –  user1075940 Feb 23 '13 at 10:29
add comment

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks a lot! I ended up taking the average of the normals that shared one vertex and it smoothed everything out nicely. –  pl12 Mar 12 '13 at 23:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.