Here's the deal: I wrote (based on Ranger_Ones method) a loader for .obj files. It works well! I am happy. However, we live in a world of 3.3 core these days, and so the Immediate Mode rendering I was doing is now not good enough, so we turn to VAOs and VBOs.
I'm familiar with the idea of how all this works, but not the implementation. I've had a few forays into looking this up, and I've got this far thanks to rastertek (once again), my computer graphics tutor, and the SuperBible and Beginning Game... 2 with.OpenGL.
Enough history! Time for some code, because it's easier to see how it worked. In fact.. I should proably take this time to apologize for posting such a lot of code. Sorry, dude.
This is how I render my client/cpu-side -stored arrays with immediate mode, atm:
//------------------elsewhere-----------------------------------------
struct Vertex3D { float x, y, z; }; typedef Vertex3D Normal3D; struct UV { float u, v;};
class Mesh { //we could assume mesh/model looks like this for clarity
Vertex3D * vertices;
Normal3D * normals;
UV * uvs;
/* for VBO, VAO functionality */
GLuint meshVAO;
GLuint meshVertexBuffer;
GLuint meshNormalBuffer;
GLuint meshTexCoordBuffer;
GLuint meshIndexBuffer;
GLuint meshColB; //? Unused
} //etc
//----------------------------------------------------------
glBegin(GL_TRIANGLES);
for ( int i = 0; i < mesh.noTriangles; i++ ) { //for each triangle in the list
Vector3D vpos; Normal3D npos;
for( int j = 0; j < 3; j++ ) {
// this vertex is the said triangle, verts attached
vpos.x = mesh.vertices[ mesh.triangleList.Vertex[j] ].x;
vpos.y = mesh.vertices[ mesh.triangleList.Vertex[j] ].y;
vpos.z = mesh.vertices[ mesh.triangleList.Vertex[j] ].z;
// this vertex is the said triangle, normals attached
npos.x = mesh.normals[ mesh.triangleList.Normal[j] ].x;
npos.y = mesh.normals[ mesh.triangleList.Normal[j] ].y;
npos.z = mesh.normals[ mesh.triangleList.Normal[j] ].z;
//PS SOMETIMES TEX COORDS TOO
glNormal3f(npos.x, npos.y, npos.z);
glVertex3f(vpos.x, vpos.y, vpos.z);
} //j++
} // i++
glEnd();
And Now I'd like to do it with VBOs and a VAO per model.. So wheres the error in my setup thats causing my model not to render properly?
I've loaded a model into memory, I'm not going to edit it, but I'd like my RAM back.. and so STATIC_DRAW is k for now..
void GLModel::LoadBuffers() {
//if (glGenVertexArrays)
// cout << "okay" << endl;
glGenVertexArrays(1, &meshVAO); //Allocate an OpenGL VAO
glBindVertexArray(meshVAO); //Bind the VAO to store all the buffers and attributes we create here
//position data
glGenBuffers(1, &meshVertexBuffer); //Generate an ID for the Vertex Buffer
glBindBuffer(GL_ARRAY_BUFFER, meshVertexBuffer); //Bind the Vertex Buffer and load the vertex (position, normal, texcoord data) into the Vertex Buffer
glBufferData(GL_ARRAY_BUFFER, sizeof( Vertex3D ) * mesh.noVerts, mesh.vertices, GL_STATIC_DRAW);
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex3D), (const GLvoid * ) 0 ); //attribute 0 gets vertex position data
/*
(const GLvoid * ) 0 refers to a pointer.. a pointer to where? or size of pointer? an offset?
*/
// normal data
glGenBuffers(1, &meshNormalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, meshNormalBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof( Normal3D ) * mesh.noNormals, mesh.normals, GL_STATIC_DRAW);
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, sizeof(Normal3D), (const GLvoid * ) 0 ); //attribute 1 gets normal data
// texcoord data
glGenBuffers(1, &meshTexCoordBuffer);
glBindBuffer(GL_ARRAY_BUFFER, meshTexCoordBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof( UV ) * mesh.noTexCoords, mesh.texcoords, GL_STATIC_DRAW);
glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, sizeof(UV), (const GLvoid * ) 0 ); //attribute 2 gets UV data
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
// Faces / Indices
glGenBuffers(1, &meshIndexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIndexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof( Triangle ) * mesh.noTriangles, mesh.triangleList, GL_STATIC_DRAW);
glBindVertexArray(0); //done binding to meshVAO
}