1
votes

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?

1
Why is it wrong? What would you expect it to be? From what I can see the lighting looks fine, without the lighting setup code it is hard to tell what's going on.Yno
The light for the reddish square is wrong - it's not lit at all. Both primitives have normals as (0, 0, 1) - the pentagon is lit fine as I rotate the scene. I don't think setup code is necessary - I've provided the data being sent to the buffers. I think the problem has to do with the degenerate triangle and the normal values.M-V
The color in the attributes of the square is less intensive than it is for the pentagon, thus I still don't see what the problem is. It could also depends on what lighting options you have, objects far from the light are less bright, which is normal. If you definetely want to think it has to do with your normals, try enabling normalization glEnable (GL_NORMALIZE);.Yno
Sorry, I don't think that's the issue. Also, farther objects are less bright only if you use local light sources, which I am not. In my case, only the normals will affect lighting. Some research on the net shows that the above problem has to do with change in vertex ordering when the redundant vertices are inserted while joining tri-strips. I'll post when I have a working solution.M-V

1 Answers

0
votes

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