4

I know, it's quite frustrating. I can't get anything to show up in my OpenGL application - all I see is an empty viewport.

When I first started writing the application, I was manually drawing the vertices (using GL_QUADS) and everything worked fine. Then I decided to switch to VBOs (Vertex Buffer Objects). Now nothing works.

Here is the structure for vertices:

struct SimpleVertex
{
    GLfloat x, y;
    GLbyte r, g, b, a;
};

As you can see, it is very simple - x and y coords for the vertices and RGBA color data. Here is the code that fills the vertex and index buffer:

const SimpleVertex rect_vertices[] = {
    { -0.8,  0.8, 0, 255, 0, 128 },
    {  0.8,  0.8, 0, 255, 0, 128 },
    {  0.8, -0.8, 0, 255, 0, 128 },
    { -0.8, -0.8, 0, 255, 0, 128 }
};

const GLuint rect_indices[] = {
    0, 1, 2, 3
};

GLuint vertices;
GLuint indices;

glGenBuffers(1, &vertices);
glBindBuffer(GL_ARRAY_BUFFER, vertices);
glBufferData(GL_ARRAY_BUFFER,
             sizeof(rect_vertices),
             rect_vertices,
             GL_STATIC_DRAW);

glGenBuffers(1, &indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
             sizeof(rect_indices),
             rect_indices,
             GL_STATIC_DRAW);

And last but certainly not least, here is the code that is supposed to draw the rectangle:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glBindBuffer(GL_ARRAY_BUFFER, vertices);
glVertexPointer(2, GL_FLOAT, 0, NULL);
glColorPointer(4, GL_BYTE, 0,
               (const GLvoid *)(sizeof(GLfloat) * 2));

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, NULL);

glDisable(GL_BLEND);

I can't figure out why nothing is being rendered. The vertex and color data is essentially unchanged from the previous version that used glVertex2f().

Nathan Osman
  • 71,149
  • 71
  • 256
  • 361

2 Answers2

3

Simply calling the gl*Pointer functions is not enough; you need to tell OpenGL that it should pull from those particular arrays. For the built-in arrays (glVertexPointer, glColorPointer, etc), you use glEnableClientState(), for the particular array in question.

For example:

glBindBuffer(GL_ARRAY_BUFFER, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, NULL);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_BYTE, 0, (const GLvoid *)(sizeof(GLfloat) * 2));

That should provide better results.

You should also use glDisableClientState() on those arrays after you are finished rendering with them.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
1

You seem to be missing a few steps:

  • VAO binding
  • enabling client state (VBO), e.g. glEnableClientState(GL_VERTEX_ARRAY)
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I'm not exactly familiar with VAO binding... what does it do and why do I need it? – Nathan Osman Jul 26 '11 at 23:42
  • He's using compatibility mode (hence the GL_QUADS type, which is not allowed in core). Therefore, he doesn't need a VAO. – Nicol Bolas Jul 26 '11 at 23:49
  • @Nicol: He's not getting output. Therefore I see no reason to believe he's in compatibility mode. – Ben Voigt Jul 27 '11 at 00:14
  • @Ben Voigt: I don't see the connection between those two statements. You can get no output in compatibility just as easily as core, so I don't know how you're drawing that conclusion. Particularly since this code wouldn't get output even with VAOs, since he's not enabling his vertex arrays. – Nicol Bolas Jul 27 '11 at 00:17
  • @Nicol: Read the rest of my answer. You know, the part you plagiarized 16 minutes after I posted it. – Ben Voigt Jul 27 '11 at 00:20
  • 1
    @Ben Voigt: I didn't "plagiarize" it; I actually explained what the function does, why it needs to be used, and exactly how he needs to use it, as well as explain about it's equivalent, `glDisableClientState`. I fail to see how this constitutes plagiarism. That still doesn't explain why you think he's using GL core without evidence. – Nicol Bolas Jul 27 '11 at 00:24
  • @Nicol: I didn't say there is any evidence he's using core, I said there was no evidence he was using compatibility (and there wasn't, until he posted a comment to that effect). And it's traditional when expanding on or clarifying someone else's answer to mention their name. Look at some of my answers, you'll find many start with words similar to "As @ somebody said, ... but you must also ..." – Ben Voigt Jul 27 '11 at 00:51
  • For example: http://stackoverflow.com/questions/4521443/how-to-share-a-big-byte-array-between-c-and-c/4521677#4521677 – Ben Voigt Jul 27 '11 at 00:53