PDA

View Full Version : Problem using VBOs



myk45
11-07-2012, 10:35 PM
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 = " \n\
#version 330 \n\
\n\
layout (location = 0) in vec3 Position; \n\
layout (location = 1) in vec3 Col; \n\
out vec3 col_out; \n\
\n\
void main() \n\
{ \n\
gl_Position = vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0); \n\
col_out = Col; \n\
}";

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


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!

tonyo_au
11-07-2012, 11:41 PM
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

myk45
11-08-2012, 01:18 AM
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.

Dan Bartlett
11-08-2012, 02:44 AM
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 (http://en.wikipedia.org/wiki/Offsetof) macro.

myk45
11-08-2012, 10:13 PM
Thanks Dan Bartlett!

It works fine.