Reputation: 329
I've been following the tutorial listed here.
I want to draw a single triangle using an Index Buffer Object, a Vertex Buffer Object, my own Vertex and Fragment Shader, and my own vertex structure.
My problem is that nothing shows up when I draw. I'm not sure what i'm doing wrong. My shaders work fine, I've tested them without the use of ibo/vbo's and they are fine.
Here is my code:
GLuint vao[1], vbo_vertex[1], index_buffer[1];
typedef struct{
GLfloat x,y,z; // Vertex.
GLfloat r,g,b; // Colors.
} spicyVertex;
void initializeBuffers(){
spicyVertex* simple_triangle = new spicyVertex[3];
// V0 - bottom
simple_triangle[0].x = 0.0f;
simple_triangle[0].y = -0.5f;
simple_triangle[0].z = 0.0f;
simple_triangle[0].r = 1.0f;
simple_triangle[0].g = 0.0f;
simple_triangle[0].b = 0.0f;
// V1 - top right
simple_triangle[0].x = 0.5f;
simple_triangle[0].y = 0.5f;
simple_triangle[0].z = 0.0f;
simple_triangle[0].r = 1.0f;
simple_triangle[0].g = 0.0f;
simple_triangle[0].b = 0.0f;
// V2 - top left
simple_triangle[0].x = -0.5f;
simple_triangle[0].y = 0.5f;
simple_triangle[0].z = 0.0f;
simple_triangle[0].r = 1.0f;
simple_triangle[0].g = 0.0f;
simple_triangle[0].b = 0.0f;
// Setup the vertex buffer data.
glGenBuffers(1, &vbo_vertex[0]);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex[0]);
glBufferData(GL_ARRAY_BUFFER, 3*sizeof(spicyVertex), simple_triangle, GL_STATIC_DRAW);
// Index setup
GLushort *indices = new GLushort[3];
indices[0]=0;
indices[1]=1;
indices[2]=2;
glGenBuffers(1, &index_buffer[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3*sizeof(GLushort), indices, GL_STATIC_DRAW);
// By this point all of our data should be on the graphics device.
// VAO setup.
glGenVertexArrays(1, &vao[0]);
glBindVertexArray(vao[0]);
// Bind the vertex buffer and setup pointers for the VAO.
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(spicyVertex), BUFFER_OFFSET(0));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(spicyVertex), BUFFER_OFFSET(sizeof(spicyVertex)*3));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(3);
// Bind the index buffeer for the VAO.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer[0]);
// Cleanup.
delete [] simple_triangle;
delete [] indices;
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
}
void Draw_indexed_Vao(){
glBindVertexArray(vao[0]); // select first VAO
glDrawRangeElements(GL_TRIANGLES,0, 3, 3, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
}
static void display(void){
glUseProgramObjectARB( programObj );
Draw_indexed_Vao();
}
I'm not performing any view transformations, when I use more basic means of drawing everything shows up just fine right in front of the camera. I really do think it's something about the way I'm declaring these buffers.
EDIT 1: The application is double buffered.
EDIT 2: SOLVED. The 3 vertices V0, V1 and V2 were all modifying the same array element. As in, I wasn't using simple_triangle[0]
,simple_triangle[1]
, simple_triangle[2]
, but that I was only working with simple_triangle[0]
. Thank you again for catching my silly error.
Upvotes: 0
Views: 1526
Reputation: 51
You might wanna change
' glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(spicyVertex), BUFFER_OFFSET(sizeof(spicyVertex)*3));'
to
'glColorAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(spicyVertex), BUFFER_OFFSET(sizeof(spicyVertex)*3));'
But I'm not 100% sure.
Upvotes: 0
Reputation: 161617
Adding an actual answer.
V1 and V2 are both modifying simple_triangle[0]
so there is only ever one vertex.
Upvotes: 1
Reputation: 5191
You might need to call glFlush()
in order to push the contents of the buffers you've drawn to onto the screen. Also, depending on whether you're using double buffering, the call required may be glutSwapBuffers()
(if you're using GLUT) or some other swap call.
Upvotes: 0