PDA

View Full Version : Wrong vertices drawing with VBO with glDrawArrays



tonyo_au
01-29-2012, 06:28 AM
Hi All,
I have a problem I do not understand. The following code works in a simple test program on AMD - in a more complex program something is affecting glDrawArrays offset index.

The code works correctly in both programs on nVidia

glGenBuffers(1, &id);
glBindBuffer(GL_ARRAY_BUFFER, id);
glVertexAttribPointer(VERTEX_BINDINGS_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(C3D_VertexC), 0);
glEnableVertexAttribArray(VERTEX_BINDINGS_POSITION );
glVertexAttribPointer(VERTEX_BINDINGS_COLOUR, 4,GL_UNSIGNED_BYTE, GL_TRUE,sizeof(C3D_VertexC),(const void*)(sizeof(float)*3));
glEnableVertexAttribArray(VERTEX_BINDINGS_COLOUR);

{
C3D_VertexC v[20];
for (int i = 0; i < 5; i++)
{
float x = (float)i;
v[i].x = (x-10.0f)/10.0f;
v[i].y = 0;
v[i].z = 0;
v[i].rgba = 255;
}
glBufferData(GL_ARRAY_BUFFER, sizeof(C3D_VertexC)*20, NULL, GL_STATIC_DRAW);


// this draws the 5 points in the correct location on the screen
// glBufferSubData(GL_ARRAY_BUFFER, sizeof(C3D_VertexC)*12, // sizeof(C3D_VertexC)*5, v);

// all verices drawn at 0,0,0 in black
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(C3D_VertexC)*5, v);
}

glDrawArrays(GL_POINTS, 0, 5);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1,&amp;id);

What can affect the first vertex used in glDrawArrays?

V-man
01-29-2012, 12:45 PM
I suggest that you call glBindBuffer followed by glVertexAttrib calls before you draw.

Also, instead of v, try &amp;v[0].x.

Make sure that other arrays are disabled such as texcoord and color.

tonyo_au
01-29-2012, 03:46 PM
Thanks V-man,
I have done all those changes but it has had no effect.

I assume this is what you meant about disabling attributes

glBindBuffer(GL_ARRAY_BUFFER, id);

glGetIntegerv(GL_MAX_VERTEX_ATTRIBS,&amp;maxAttributes );
for (int i = 0; i < maxAttributes; i++)
glDisableVertexAttribArray(i);

glVertexAttribPointer(VERTEX_BINDINGS_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(C3D_VertexC), 0); glEnableVertexAttribArray(VERTEX_BINDINGS_POSITION );
glVertexAttribPointer(VERTEX_BINDINGS_COLOUR, 4,GL_UNSIGNED_BYTE, GL_TRUE,sizeof(C3D_VertexC),(const void*)(sizeof(float)*3));
glEnableVertexAttribArray(VERTEX_BINDINGS_COLOUR);
glDrawArrays(GL_POINTS, 0, 5);

tonyo_au
01-29-2012, 10:26 PM
The problem seems to be caused by using this code prior to the above code

glBindBuffer(GL_DRAW_INDIRECT_BUFFER,c_DrawCommand s);

glDrawElementsIndirect(GL_TRIANGLES,GL_UNSIGNED_IN T,(void* (p_Object*sizeof(C3D_DrawElementsIndirectCommand)) );

glBindBuffer(GL_DRAW_INDIRECT_BUFFER,0);



I changed this to

glDrawElementsIndirect(GL_TRIANGLES,GL_UNSIGNED_IN T,&amp;c_DrawCommand[p_Object]);

and the code now works