开发者

Why does this Drawing glitch happen with GL_TRIANGLE_STRIP in OpenGL ES 1.1

So I'm starting to work with openGL, been following the tutorials over at Jeff LaMarche's blog and I've run into a problem when trying to draw a squar开发者_开发知识库e using the GL_TRIANGLE_STRIP mode with glDrawArrays.

It works, by that I mean that I can draw a square, but I get this weird drawing glitch. You can see it here:

Drawing glitch http://img10.imageshack.us/img10/1631/picture1io.png

I'm using the xcode template that's provided by Jeff, so I assume the setup is all proper. The code I'm using in the drawView function is as follows:

Square2D *square = malloc(sizeof(Square2D));
Square2DSet(square, -0.25, -0.25, -20.0, 0.5, 0.5);

glLoadIdentity();
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, square);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 12);
glDisableClientState(GL_VERTEX_ARRAY);

if(square != NULL)
    free(square);

here's the code for Square2d and Square2DSet:

typedef struct {
    Vertex3D tl;
    Vertex3D bl;
    Vertex3D tr;
    Vertex3D br;
} Square2D;

static inline void Square2DSet(Square2D *sq, GLfloat x, GLfloat y, GLfloat z, GLfloat width, GLfloat height)
{
    sq->tl = Vertex3DMake(x, y, z);
    sq->bl = Vertex3DMake(x, y + height, z);
    sq->tr = Vertex3DMake(x + width, y, z);
    sq->br = Vertex3DMake(x + width, y + height, z);
}

I'm sure I'm doing something wrong, just not sure what. Eventually the glitch will go away after the program has run for a little bit.

Any ideas?

Cheers


glDrawArrays(GL_TRIANGLE_STRIP, 0, 12);

is drawing 12 vertices. You want 4. Its asking how many indices, not how many values to look at. It will multiply the index by the number of values at each index.

The reason why its going crazy is because its drawing junk, then it disappears because I assume that junk gets filled with 0s or something and it just goes away.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜