Sign in to follow this  
adawg1414

vbo problem

Recommended Posts

I'm currently working on a project where I have a room with furniture in it. The problem I'm having now is that I'm reading in meshes and aligning the vertex and normal data for the vbo functionality per item and the second furniture item to be drawn is taking on the attributes of the first. For example, I read in a couch mesh (it draws fine using vbo) and then I read in a lamp mesh (when I draw the lamp it draws in the shape of the couch). I've created a class to do all of this so the code looks like:
 	// load the couch and set it up for vbo drawing
        // ObjLoader is my mesh loading class
        ObjLoader oLoader("couch.txt"); 
        // ObjModel is an object that holds all of the meshes data
	ObjModel *oModel = oLoader.ReturnObj();
        // AlignForVBOs sets up the vert/normal/index data for vbo usage
        // it returns the index buffer number
	nCouchVBO = oModel->AlignForVBOs();
	nCouchTriangles = oModel->NumTriangle;

	// load the lamp and set it up for vbo drawing
	oLoader.LoadObj(LAMP_FILE);
	oModel = oLoader.ReturnObj();
	nLampVBO = oModel->AlignForVBOs();
	nLampTriangles = oModel->NumTriangle;

Inside the AlignForVBOs function I generate the buffers and set things up like so:

	unsigned int vbos[3];

	glGenBuffers(3, vbos);
	glEnableClientState(GL_VERTEX_ARRAY);
	glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(ObjVertex) * this->NumVertex, this->Aligned_Vertex_Data, GL_STATIC_DRAW);
	glVertexPointer(3, GL_FLOAT, 0, 0);

	glEnableClientState(GL_NORMAL_ARRAY);
	glBindBuffer(GL_ARRAY_BUFFER, vbos[1]);
	// i'm using numvertex here again because the vertex array and normal array should be the same for the vbo
	glBufferData(GL_ARRAY_BUFFER, sizeof(ObjNormal) * this->NumVertex, this->Aligned_Normal_Data, GL_STATIC_DRAW);
	glNormalPointer(GL_FLOAT, 0, 0);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[2]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ObjIndex) * this->NumTriangle, this->Aligned_Index_Data, GL_STATIC_DRAW);

	return vbos[2];

I then draw like so:

        // draw using vbo's
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, nCouchVBO);
	glDrawElements(GL_TRIANGLES, (nCouchTriangles * 3), GL_UNSIGNED_INT, 0);

	// draw using vbo's
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, nLampVBO);
	glDrawElements(GL_TRIANGLES, (nLampTriangles * 3), GL_UNSIGNED_INT, 0);

I of course skipped code that isn't necessary to my problem like pushmatrix(), translate, rotate, etc.

Share this post


Link to post
Share on other sites
I think you need to bind the vertex and normal data of the object before each draw. The index array need to know which sets of data to use for drawing.

Share this post


Link to post
Share on other sites
Quote:
Original post by GamerYZ
I think you need to bind the vertex and normal data of the object before each draw. The index array need to know which sets of data to use for drawing.


Can you rephrase that, I don't quite understand what you're saying? Perhaps use my example code to show me what you mean.

Share this post


Link to post
Share on other sites
You'll have to store all 3 vbo unsigned int some where. Right now you are only returning the index ID of the vbo (vbos[2]) from the function, you need to store the other 2 uint as well. Then when you draw it, do something like this:

glBindBuffer(GL_ARRAY_BUFFER, couchVertID);
glVertexPointer(3, GL_FLOAT, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, couchNormID);
glNormalPointer(GL_FLOAT, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, nCouchVBO);
glDrawElements(GL_TRIANGLES, (nCouchTriangles * 3), GL_UNSIGNED_INT, 0);


Then you can draw the lamp with the same code, except change the ID and number of triangles:

glBindBuffer(GL_ARRAY_BUFFER, lampVertID);
glVertexPointer(3, GL_FLOAT, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, lampNormID);
glNormalPointer(GL_FLOAT, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, nLampVBO);
glDrawElements(GL_TRIANGLES, (nLampTriangles * 3), GL_UNSIGNED_INT, 0);

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