Problem using VBOs

Hello All,

I was mostly using the old glBegin() method for a long time. Now, im learning to use modern ways of specifying vertices. Here is some sample code i found online. I modified a little:
What i tried to do is, in the same VBO, use color values also:


static const char* pVS = "                                                    
\
#version 330                                                                  
\
                                                                              
\
layout (location = 0) in vec3 Position;                                       
\
layout (location = 1) in vec3 Col;                                            
\
out vec3 col_out;                                                             
\
                                                                              
\
void main()                                                                   
\
{                                                                             
\
    gl_Position = vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0);  
\
    col_out = Col;                                                            
\
}";

static const char* pFS = "                                                    
\
#version 330                                                                  
\
                                                                              
\
out vec4 FragColor;                                                           
\
in vec3 col_out;                                                              
\
                                                                              
\
void main()                                                                   
\
{                                                                             
\
   FragColor = vec4(col_out, 1.0);                                            
\
}";


typedef struct {
   Vector3f vert;
   Vector3f col;
} Vertex;

Vertex Vertices[3];

static void RenderSceneCB()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);

    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &(Vertices[0].col));

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    glutSwapBuffers();
}


static void InitializeGlutCallbacks()
{
    glutDisplayFunc(RenderSceneCB);
}

static void CreateVertexBuffer()
{     

    Vertices[0].vert = Vector3f(-1.0f, -1.0f, 0.0f);
    Vertices[0].col = Vector3f(1.0f, 0.0f, 0.0f);

    Vertices[1].vert = Vector3f(1.0f, -1.0f, 0.0f);
    Vertices[1].col  = Vector3f(1.0f, 0.0f, 0.0f);

    Vertices[2].vert = Vector3f(0.0f, 1.0f, 0.0f);
    Vertices[2].col  = Vector3f(1.0f, 0.0f, 0.0f);

     glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}

Here is the essential part. But i get a blank screen. Can anyone please let me know what i’m doing wrong?

Thanks in advance!

The only thing I can see that looks odd is your glBindBuffer(GL_ARRAY_BUFFER, VBO);.
I would only do this once prior to enabling and setting any vertex attributes

Thanks for the reply tonyo_au. i tried doing it just once. i added the glBindBuffer() once , just before

glEnableVertexAttribArray(0);

But, it doesn’t seem to work. I still get the black screen.

The last argument of:

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &(Vertices[0].col));

should be the offset of the .col field in the Vertex structure (which is probably 12) as you are using a buffer object, and shouldn’t be a client-side address.

There’s different ways you could get this offset value including manually providing it, calculating the offset using the difference in address between a field and the start of a struct (eg. between &Vertices[0] and &Vertices[0].col) or using the offsetof macro.

Thanks Dan Bartlett!

It works fine.