M-V
M-V

Reputation: 5237

Incorrect lighting when batching triangle strips in OpenGL

I am use vertex buffers (in a fixed function pipeline) to batch triangle strips by repeating first/last vertices. The rendering code is as follows:

    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);

    // enable vertex array
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 3*sizeof(GLfloat), 0);

    // prepare color VBO
    glBindBuffer(GL_ARRAY_BUFFER, _colorBuffer);

    // enable color array
    glEnableClientState(GL_COLOR_ARRAY);
    glColorPointer(3, GL_FLOAT, 3*sizeof(GLfloat), 0);

    // prepare normals VBO
    glBindBuffer(GL_ARRAY_BUFFER, _normalBuffer);

    // enable normal array
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, 0, 0);

    glDrawArrays(mode, 0, _nVertices);      

    // disable/unbind
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

But, although the triangle strips are separated correctly, the lighting is correct only for the first triangle strip.

In the example below, I am rendering a pentagon and a square, and you can see that the lighting is correct only for the pentagon.

enter image description here

This is the data in the 3 (vertex, normal, color) vertex buffers. You can see that the last vertex of the pentagon and the first vertex of the square is repeated, and that the normals are consistent.


vertices:

-2.000000 1.000000 0.000000
-1.000000 0.000000 0.000000
-1.000000 2.000000 0.000000
0.000000 0.000000 0.000000
0.000000 2.000000 0.000000
0.000000 2.000000 0.000000
1.000000 -1.000000 0.000000
1.000000 -1.000000 0.000000
1.000000 -2.000000 0.000000
2.000000 -1.000000 0.000000
2.000000 -2.000000 0.000000

normals:

-0.000000 0.000000 1.000000
-0.000000 0.000000 1.000000
-0.000000 0.000000 1.000000
-0.000000 -0.000000 1.000000
-0.000000 0.000000 1.000000
-0.000000 0.000000 1.000000
-0.000000 0.000000 1.000000
-0.000000 0.000000 1.000000
-0.000000 0.000000 1.000000
-0.000000 0.000000 1.000000
-0.000000 -0.000000 1.000000

colors:

0.317647 0.011765 0.458824
0.317647 0.011765 0.458824
0.317647 0.011765 0.458824
0.317647 0.011765 0.458824
0.317647 0.011765 0.458824
0.317647 0.011765 0.458824
0.678431 0.003922 0.003922
0.678431 0.003922 0.003922
0.678431 0.003922 0.003922
0.678431 0.003922 0.003922
0.678431 0.003922 0.003922

Why is the lighting wrong for the square?

Upvotes: 1

Views: 1072

Answers (1)

M-V
M-V

Reputation: 5237

Apparently the issue has to do with the number of degenerate triangles added when you combine 2 triangle strips. This can change the vertex ordering, thus making the direction of your normals inconsistent.

I solved the above problem by adding adding an extra redundant vertex when the current running length of the triangle strip is odd.

The link below has a lot of useful information on construction and joining of triangle strips:

http://www.codercorner.com/Strips.htm

Upvotes: 0

Related Questions