Matze
Matze

Reputation: 553

Difference between glNormal3f and glNormalPointer

i'm getting these strange two results when drawing with immediate mode and vertex arrays. In immediate mode i'm passing the normals by glNormal3f. My shader takes the normal and computes something like a shadow, but nothing seriously.

Immediate Mode:

    glBegin(GL_TRIANGLES);
    for (Triangle tri : this.triangles) {
        Vec3d normal = Vec3d.vectorProduct(
                Vec3d.sub(tri.getB(), tri.getA()),
                Vec3d.sub(tri.getC(), tri.getA())); //calculating normalvector
        glNormal3d(normal.x, normal.y, normal.z);
        glColor3d(1.0, 1.0, 1.0);
        glVertex3d(tri.getA().x, tri.getA().y, tri.getA().z);
        glVertex3d(tri.getB().x, tri.getB().y, tri.getB().z);
        glVertex3d(tri.getC().x, tri.getC().y, tri.getC().z);
    }
    glEnd();

Result:

enter image description here

In the VAO variant, i'm storing the normals in a separate buffer, but calculated the same way:

for(Triangle tri : triangles) {

        Vec3d normal = Vec3d.vectorProduct(
                Vec3d.sub(tri.getB(), tri.getA()),
                Vec3d.sub(tri.getC(), tri.getA()));


        normals.put((float) normal.x);
        normals.put((float) normal.y);
        normals.put((float) normal.z);

    }

    normals.flip();

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(4, normals);
    glVertexPointer(3,3*4, vertices);

    glDrawArrays(GL_TRIANGLES, 0, (vertices.capacity() / 3));

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY); 

Result 2:

enter image description here

Obviously the normals get somehow mismatched but i can't find my mistake.

And the final question: What is the difference between glNormal3f and glNormalPointer considering the values passed to the shader?

Upvotes: 1

Views: 1034

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43359

The stride of your normal array is suspect. Why are you passing 4 for the stride to glNormalPointer (...)?

You are telling GL that there are 4-bytes (1 float) worth of space between each of your normals. However, normals is built in your code with 12-bytes between each successive normal. Incidentally, this is what you would call tightly packed and therefore passing 0 for the stride implies the same thing (4-bytes per-component x 3-components).

Your vertex array and normal array should actually have the same stride. 4*3 or simply 0.

Upvotes: 2

Related Questions