Trouble arranging my 3d model data inside a vertex array

Started by
2 comments, last by jon723 16 years, 8 months ago
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 
{
	sVertex3d();
	float vertex[3];
};

// a single texture coordinate
struct sTexCoord
{
	sTexCoord();
	float coord[2];
};

// a single triangle normal
struct sNormal3d
{
	sNormal3d();
	float normal[3];
};

struct sFace
{
	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 {
public:
	ObjModel();
	~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

protected:
	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
};
www.lefthandinteractive.net
Advertisement
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 vectorfor( 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

Linky
With stl::vector, the elements are stored in an array so a simple

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

or

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

or

pointer=glMapBuffer(.....);

memcpy(pointer, &myvector[0], size);
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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.
www.lefthandinteractive.net

This topic is closed to new replies.

Advertisement