0

I'm following this guide and I'm trying to draw a quad to the screen. I also saw the source code, it's the same and it should work, but in my case nothing is displayed on the screen. I'm using OpenGL 2.0 with a vertex shader that just sets the color to be red in a way that the quad should be visible on the screen.

Before callig glutMainLoop I generate the vertex buffer object:

#include <GL/glut.h>
#include <GL/glew.h>

vector<GLfloat> quad; 
GLuint buffer;

void init()
{
    // This routine gets called before glutMainLoop(), I omitted all the code
    // that has to do with shaders, since it's correct.
    glewInit();
    quad= vector<GLfloat>{-1,-1,0, 1,-1,0, 1,1,0, -1,1,0};
    glGenBuffers(1,&buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*12,quad.data(),GL_STATIC_DRAW);
}

This is my rendering routine:

void display()
{
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER,buffer);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
    // I also tried passing quad.data() as last argument, but nothing to do.
    glDrawArrays(GL_QUADS,0,12);
    glDisableVertexAttribArray(0);

    glutSwapBuffers();
}

The problem is that nothing is drawn to the screen, I just see a black window. The quad should be red because I set the red color in the vertex shader.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • *"with a vertex shader that just sets the color to be red"* - Then show us your vertex and fragment shader. By the way, are you sure your vertex shader sets the color? – Christian Rau Jan 30 '13 at 16:28
  • Also show the rest of your setup, where you compile and link the shaders and all that stuff. – Christian Rau Jan 30 '13 at 16:34
  • I have the same problem even if I don't use any shader and I set the color with glColor3f, or with a GL_COLOR_ARRAY. – Ramy Al Zuhouri Jan 30 '13 at 22:03

2 Answers2

3

So maybe the problem is the count in the glDrawArrays(GL_QUADS, 0, 12); which must be glDrawArrays(GL_QUADS, 0, 4);

Anthony
  • 255
  • 3
  • 7
  • Stride if the gap between consecutive elements. – Ramy Al Zuhouri Jan 30 '13 at 13:11
  • 3
    So maybe the problem is the count in the glDrawArrays(GL_QUADS, 0, 12); which must be glDrawArrays(GL_QUADS, 0, 4); – Anthony Jan 30 '13 at 13:38
  • True, but it still doesn't draw the quad. – Ramy Al Zuhouri Jan 30 '13 at 13:54
  • You can also use `0` as stride, if the attributes are tightly packed, like in his case. But in fact your comment is totally correct (and might be the solution to his problem). So I suggest you replace your current answer with your comment (which would at least gain you +2 from me). – Christian Rau Jan 30 '13 at 16:26
  • @RamyAlZuhouri *"Stride is the gap between consecutive elements"* - No, it isn't. It actually is the gap between the *start* of one element and the *start* of the next, so his `3*sizeof(float)` is indeed correct. However you are right in that this is still not the solution to your problem, since a stride of `0` also works in your case. – Christian Rau Jan 30 '13 at 16:30
0

I was missing glEnableClientState like this:

glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_QUADS,0,12);
glDisableClientState(GL_VERTEX_ARRAY);
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187