PDA

View Full Version : Accessing vbo value in shader



mobeen
12-28-2010, 02:29 AM
Hi all,
I have a simple vbo which is setup as follows;


glGenBuffers( 1, &vbo);
glBindBuffer( GL_ARRAY_BUFFER, vbo);
unsigned int size = total_items * 4;
glBufferData( GL_ARRAY_BUFFER, size, 0, GL_STATIC_DRAW);
glBindBuffer( GL_ARRAY_BUFFER, 0);

Next, I put the data into the vbo as follows;


glBindBuffer(GL_ARRAY_BUFFER, vbo);
float* p =( float*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
for (int i=0; i<total_items; i++){
int index = i*4;
p[index] = data[count];
p[index+1] = data[count+1];
p[index+2] = data[count+2];
p[index+3] = 0;
count+=3;
}
glUnmapBuffer(GL_ARRAY_BUFFER);

However, when I access the vertex position in my vertex shader, it returns the x component of the data instead of the explicit zero that I am setting in. My draw call is as follows


glBindBuffer( GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, total_items);
glDisableVertexAttribArray(0);
glBindBuffer( GL_ARRAY_BUFFER, 0);

Am i doing anything wrong here?

One more thing, I dont know why but after a specific length the post editing pane automatically scrolls back up and its very difficult to edit the message.
Regards,
Mobeen

mobeen
12-28-2010, 05:20 AM
I got it working the problem was else where. Some other part of code was messing the last parameter.

Thanks anyways,
Mobeen

carsten neumann
12-28-2010, 08:27 AM
I know you said you solved it, but



unsigned int size = total_items * 4;


should be



unsigned int size = total_items * 4 * sizeof(float);


as you seem to have total_items many vec4.

mobeen
12-28-2010, 07:22 PM
Thanks carsten for pointing this out. I missed that during the copy from my code.

Regards,
Mobeen