I'm using VC++2010 with GLFW3 and GLEW for openGL. I've made an OBJ-Loader for loading triangulated meshes. At some point i fill the following Arrays in a VBOMesh2-object.
GLfloat *vertices;
GLfloat *normals;
GLuint *indices;
so as result, i get my data which i use for the VBO:
- Vertices : vertices = [v0x, v0y, v0z, v1x, v1y, v1z, ...]
- (sorted) Normals : normals = [n0x, n0y, n0z, n1x, n1y, n1z, ...]
- Indices : inices = {face0i0, face0i1, face0i2, face1i0, face1i1, face1i2, ...]
The next step is to bind the Buffers with the init()-methode from VBOMesh2 object. vertexVBOID, normalsVBOID and indexVBOID are GLuint.
void VBOMesh2::init(void)
{
vertexVBOID = 0;
glGenBuffers(1, &vertexVBOID);
glBindBuffer(GL_ARRAY_BUFFER, vertexVBOID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*numFaces*3*3, vertices, GL_STATIC_DRAW);
normalsVBOID = 0;
glGenBuffers(1, &normalsVBOID);
glBindBuffer(GL_ARRAY_BUFFER, normalsVBOID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*numFaces*3*3, normals, GL_STATIC_DRAW);
indexVBOID = 0;
glGenBuffers(1, &indexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*numFaces*3, indices, GL_STATIC_DRAW);
}
ok... and i draw with:
void VBOMesh2::draw(void)
{
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vertexVBOID);
glVertexPointer(3, GL_FLOAT, sizeof(float)*3, 0);
glEnableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, normalsVBOID);
glNormalPointer(GL_FLOAT, sizeof(float)*3, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glPushMatrix();
glTranslatef(x, y, z);
glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glRotatef(rz, 0, 0, 1);
glDrawArrays(GL_TRIANGLES, indices[0], numIndices);
glPopMatrix();
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
I can load a Mesh with:
OBJLoader objLoader;
MeshData meshData;
objLoader.loadFile("temp/quad_smooth.obj");
meshData = objLoader.getMeshData();
VBOMesh2 tmpMesh(meshData);
and draw it (in openGL-Loop) with :
tmpMesh.draw();
i get the following result:
wonderful!
now i thought, lets draw 500 Quads with following Code:
OBJLoader objLoader;
MeshData meshData;
objLoader.loadFile("temp/quad_smooth.obj");
meshData = objLoader.getMeshData();
for(int i = 0; i < 500; i++)
{
VBOMesh2 tmpMesh(meshData);
meshes.push_back(tmpMesh);
meshes[i].init();
// Do stuff for position and rotation like meshes[i].setX(x); and so on
}
and in openGL-loop i do:
for(int i = 0; i < meshes.size(); i++)
{
meshes[i].draw();
}
btw. meshes is a vector:
vector<VBOMesh2> meshes;
and i got the following result:
Oh no!!!
And now i've got no opinion why my meshes (not all, but some) are broken... or what i'm doing wrong.... The code can render one quad without any visualisation-error, but when i create 500 quads with the same meshData (same Data from oneQuad) i got broken quads....
Instead of using
glDrawArrays(GL_TRIANGLES, indices[0], numIndices);
i've tried
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0);
for that i've filled the Arrays vertices, normals, indices with an other methode (which i can post if necessary), but i got the same problem... i can draw one Quad with no Errors but when i try to draw 500 Quads i got an access violation 0x000005.
Btw. i updated my driver for my graphic-card (but the problem remain)
Can someone give me a hint?
glPushMatrix
etc ), better go with shaders, own matrices, etc... In my personal opinion this reduces the source of errors a much when everything is setup and maybe even removes this error. – Felix K.