Sign in to follow this  
woofbluddywoof

vbo not displaying

Recommended Posts

woofbluddywoof    100
Hi guys - I'm just starting out using VBOs (using SDL/glew). I'm trying to start out using the simple example of a cube - actually at the moment just the one face of a cube - but I can't get anything to display

my vertex structure is defined as follows:
[code]
struct Vertex
{
float x, y, z; //Vertex coords
float tx, ty; //Texture coords
float nx, ny, nz; //Normal coords
};
[/code]

The cube is then generated like so:
[code]
Vertex temp;

//NOTE: Perspective is from looking at the cube from the outside
//Just trying to display one face for the moment to simplify
//Back face ------------------------------------------------------------------------------------------
temp.x = 0.f; temp.y = 0.f; temp.z = 0.f; //Bottom Right - 0
temp.nx = 0.f; temp.ny = 0.f; temp.nz = 1.f; // This stays the same for the rest of the face
temp.tx = 1.f; temp.ty = 0.f;
m_vertices.push_back(temp);

temp.x = 0.f; temp.y = m_fHeight; temp.z = 0.f; //Top Right - 1
temp.tx = 1.f; temp.ty = 1.f;
m_vertices.push_back(temp);

temp.x = m_fWidth; temp.y = m_fHeight; temp.z = 0.f; //Top Left - 2
temp.tx = 0.f; temp.ty = 1.f;
m_vertices.push_back(temp);

temp.x = m_fWidth; temp.y = 0.f; temp.z = 0.f; //Bottom Left - 3
temp.tx = 0.f; temp.ty = 0.f;
m_vertices.push_back(temp);

m_indeces.push_back(0); m_indeces.push_back(1); m_indeces.push_back(2);
m_indeces.push_back(2); m_indeces.push_back(3); m_indeces.push_back(0);


//Generate the vertex buffer
glGenBuffers(1, &m_vertexBufferID);
//Bind the vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID);
//Fill the vertex buffer - size is 24*sizeof(Vertex) bcs 6 faces with 4 corners
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 24, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vertex) * 24, &m_vertices); // Actually upload the data

//Set up the pointers
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(20));
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));


//Generate the index buffer
glGenBuffers(1, &m_indexBufferID);
//Bind the index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBufferID);
//Fill the index buffer- size is 36*sizeof(uint) bcs 6 traingle coords in 1 face * 6 faces
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 36, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(unsigned int) * 36, &m_indeces); // Actually upload the data
[/code]

which is then sent to render in a seperate function:
[code]
glBindBuffer(GL_ARRAY_BUFFER, vertexID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

// Resetup the pointers.
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(20));
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));

//Draw the indexed elements
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
[/code]

and BUFFER_OFFSET() is a simaple macro defined as:
[code]
// A helper macro to get a position
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
[/code]

But I just can't get anything to display - applying the same texture etc. to a cube in immediate mode shows up fine.

The weird thing is that occasionaly something very strange will get displayed, which is different every time - so maybe it's some sort of initialization error?

would be much appreciated if anyone could help me out with this.

cheers.

Share this post


Link to post
Share on other sites
Brother Bob    10344
There are at least three errors in your code I immediately spotted.
[list][*]You store 4 vertices in the vertex vector, but upload 24 vertices to the buffer object. The last 20 vertices are read from outside the vector.[*]You store 6 indices in the index vector, but upload 36 indices to the buffer object. The last 30 indices are read from outside the vector.[*]You upload the index array as if the indices are GLuint, but draw the index array as if the indices are GLubyte.[/list]

Share this post


Link to post
Share on other sites
woofbluddywoof    100
Hi - thanks for that.

I figures that as the verctors had not been initialized and I was in debug more it wouldn't make a difference. Have fixed all of that now, but I'm still getting the same error. Nothing displaying except sometimes when I get a weird....thing (sort of a large triangle - which makes sense as I'm rendering triangles but it's in thw wrong plane and is wrong size, no texture etc. ) very occasionaly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this