I have written an obj class in C++ to parse .obj files, store the vertices, textures, and normals, and display the object using glDrawElements.
I noticed that all objects can be drawn, but not simultaneously. I can draw any object by itself but if I run the program with three or more objects, it crashes during execution. It seems like an issue with memory allocation. Is there a way to properly malloc for all objects? All code is in C++ and uses openGL.
void draw()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
if (textures != NULL)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, textures);
}
if (normals != NULL)
{
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, normals);
}
//glEnableClientState(GL_COLOR_ARRAY);
glColor4f(1,1,1,1);
glDrawElements(GL_TRIANGLES, numOfFaces*3, GL_UNSIGNED_INT, indices);
glDisableClientState(GL_VERTEX_ARRAY); // disable vertex array
glDisableClientState(GL_TEXTURE_COORD_ARRAY); // disable texture array
glDisableClientState(GL_NORMAL_ARRAY); // disable normal array
//glDisableClientState(GL_COLOR_ARRAY);
}
and the push and pop matrices.
glPushMatrix();
glPushMatrix();
glTranslatef(2000,0,5000);
glScalef(85.0f,85.0f,85.0f);
//glRotatef(iconSpin, 0,1,0);
pistol->draw();
glPopMatrix();
glPushMatrix();
glScalef(85.0f, 85.0f, 85.0f);
stormtrooper->draw();
glPopMatrix();
glPushMatrix();
glTranslatef(200.0f, 0, 0);
glScalef(85.0f,85.0f,85.0f);
player->draw();
glPopMatrix();
glTranslatef(0,-1000.0f, 0);
glScalef(40.0f, 40.0f, 40.0f);
building->draw();
glPopMatrix();
Aucun commentaire:
Enregistrer un commentaire