开发者

OpenGL: Using vertex array to draw a cube causes graphical glitch

When trying to draw the following quads in OpenGL using a vertex array (instead of using immediate mode rendering), I get the graphical glitch (line segment) shown in the picture, which can be found in the second link below. The line seems to extend upwards to infinity.

GLdouble vertices[] = {
    // ba开发者_Go百科ck
    0.0, 0.0, 0.0,
    si,  0.0, 0.0,
    si,  -si, 0.0,
    0.0, -si, 0.0,

    // front
    0.0, 0.0, si,
    0.0, -si, si,
    si,  -si, si,
    si,  0.0, si,

    // left
    0.0, 0.0, 0.0,
    0.0, -si, 0.0,
    0.0, -si, si,
    0.0, 0.0, si,

    // right
    si, 0.0, 0.0,
    si, 0.0, si,
    si, -si, si,
    si, -si, 0.0,

    // top
    0.0, 0.0, 0.0,
    0.0, 0.0, si,
    si, 0.0, si,
    si, 0.0, 0.0,

    // bottom
    0.0, -si, 0.0,
    si, -si, 0.0,
    si, -si, si,
    0.0, -si, si,
};

Immediate drawing:

 glBegin(GL_QUADS);
   for (int i = 0; i < sizeof(vertices)/sizeof(*vertices)/3; i++)
     glVertex3d(vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2]);
 glEnd();

Drawing with vertex array:

glVertexPointer(3, GL_DOUBLE, 0, vertices);
glDrawArrays(GL_QUADS, 0, sizeof(vertices)/sizeof(*vertices));

Images:

Correct cube drawn in immediate mode

Glitchy cube drawn with vertex array

What am I doing wrong?


Try changing your draw to

glVertexPointer(3, GL_DOUBLE, 0, vertices);
glDrawArrays(GL_QUADS, 0, 24);

sizeof() doesn't work the way you think it does. It doesn't return the number of elements in an array. You need to somehow keep track of the number of vertices in that array, and then use that counter in your glDrawArrays call.

edit - more information:

The last parameter in glDrawArrays is the number of vertices you are rendering. sizeof(vertices) should return the number of bytes in a GLdouble* (4 bytes), and sizeof(vertices*) should return the number of bytes in a GLdouble (8 bytes). Then 4 / 8 should technically round to zero since they are integers, so I'm not actually sure why it's rendering anything.

However, I'm pretty sure that (assuming your vertices are done correctly), if you change that number to 24, it will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜