Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am using OpenGL ES 2.0 I have a bunch a quads to be drawn, would love to be able to have to pass only 4 vertices per quad as if I were using GL_QUADS, but basically I just want to know the best way of drawing a bunch of separate quads.

So far what I've found I could do: -GL_TRIANGLES(6 vertices per quad) -Degenerate GL_TRIANGLE_STRIP(6 vertices per quad)

Possibities I've found: -GL_TRIANGLE_STRIPS with a special index value that resets quad strip(this would mean 5 indexes per quad, but I dont think this is possible is OpenGL ES 2.0)

share|improve this question

3 Answers

up vote 13 down vote accepted

Just use index buffer and GL_TRIANGLES. In this case you need 4 vertices + 6 indices per quad (6 additional indices may sound large overhead but in reality it is not - once you have constructed your index buffer you don't have to touch it again). See this page for more information (search for glDrawElements)

Simple example code:

GLfloat vertices[] = {-1, -1, 0, //bottom left corner
                      -1,  1, 0, //top left corner
                       1,  1, 0, //top right corner
                       1, -1, 0}; // bottom right rocner

GLubyte indices[] = {0,1,2, // first triangle (bottom left - top left - top right)
                     0,2,3}; // second triangle (bottom left - top right - bottom right)

glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

Also, if you are drawing just one quad you need only 4 vertices to draw it with triangle strips (as shown in wikipedia) but I guess you already knew this.

share|improve this answer

A GL_TRIANGLE_STRIP will do it in 4 vertices, without needing an index buffer, but if you're drawing more than 1 quad, I'd say to go with the index buffer method.

To steal/adapt from the accepted answer:

GLfloat vertices[] = {-1, -1, 0, //bottom left corner
                       1, -1, 0, //CHANGED! order bottom right "rocner"
                      -1,  1, 0, //top left corner
                       1,  1, 0, //top right corner
                     };

// Create and bind the vertex buffer...

// 2----3
// | \  |
// |  \ |
// 0----1
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 ) ;
share|improve this answer

The big gain on desktop is to move the data into GL_STATIC_DRAW VBOs. Presumably this is true on iPhone's MBX GPU too.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.