Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Trouble arranging my 3d model data inside a vertex array

This topic is 4023 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings everyone, I've had my .Obj file loader working for a really long time under immediate mode rendering. I really want to move away from the slow immediate mode calls for sending vertex data through the pipeline and I figured that I should first get my code to work using vertex arrays and then ultimately VBO's. The following is the structure that defines my vertex data and my model data. I use std::vectors to append data but I'm not entirely sure how to deference a vector when specifying the vertex data when creating the vertex array in opengl. Can someone give me some insight as to what I should do in order to get vertex arrays to work?
struct sVertex3d 
	float vertex[3];

// a single texture coordinate
struct sTexCoord
	float coord[2];

// a single triangle normal
struct sNormal3d
	float normal[3];

struct sFace
	vector<int> vertlist;	// the vertices that make up this face
	vector<int> normlist;	// the list of normals that make up this face
	vector<int> texcoordlist;	// the list of texture coordinates that make up this face
	vector<int> combined;	// the combined indices for this face

	string material;	// which material does this face use

class ObjModel {
	bool LoadModel(char *fname);
	bool LoadMtl(char *mtlfilename);	// loads a material library file
	sMaterial *FindMatByName(string name);	// searches the array of materials by name and returns the matching material
	void Draw();	// draw the geometry for the model

	int numObjects;	// the number of objects that make up this model
	string filename;	// the name of the obj model file
	sObject *objects; // the list of objects that make up the model

	int numVerts;
	int numTexCoords;
	int numNormals;
	int numFaces;

	sVertex3d *pvertices;	// array of vertices in the model
	sNormal3d *pnormals;	// array of normals in the model		
	sTexCoord *ptexcoords;  // array of texture coordinates in the model
	sFace *pfaces;	// array of faces in the model. Faces have indices into the normals, texture coords, and vertex lists

	map<string, sMaterial> mats;	// the list of materials from all of the material libraries

Share this post

Link to post
Share on other sites
Hey jon723, you've definitely made the right decision moving to VBO's.

To dereference an stl vector what you need to do is something along the lines of:
// loop through all elements in the vector
for( std::vector<int>::iterator it; it = vertlist.begin(); it != vertlist.end(); it++ ) {
int a_number = (*it); // this dereferences the iterator 'it'

So just to give you an overview of what you *should* be doing here to initialise:
#1.) Create your VBO & bind it
#2.) Map your VBO (From here we get a data pointer)
#3.) Loop through your vertex elements
#4.) Copy the elements into the VBO (Using that data pointer)
#5.) Unmap your VBO


Share this post

Link to post
Share on other sites
With stl::vector, the elements are stored in an array so a simple

glBufferData(........., &myvector[0]);


glBufferSubData(........., &myvector[0]);



memcpy(pointer, &myvector[0], size);

Share this post

Link to post
Share on other sites
Hey, thanks for both of the replies. I've read the nehe article before, but I wasn't exactly sure how to mold it into the vector solution I was using. I'll definitely take your suggestions into account. Thanks again.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!