openGL load multiple object
سؤال
I have problem with insert multiple object. I load object but they are all in one place,
for( int i = 0; i <= 5; i++ )
{
glCallList( OBJECT_LIST[ i ] );
}
glFlush();
glutSwapBuffers();
} // unopened paren
void Reshape( int width, int height ) {
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
for( int i = 0; i <= 5; i++ ) {
if( !load_obj( argv[ i ], OBJECT_LIST[ i ] ) )
{
printf( "error load file %s", argv[ i ] );
}
}
}
المحلول
you need to either specify object matrix directly (as if loading objects from a 3DS file) like this:
glMatrixMode(GL_MODELVIEW); // we are going to manipulate object matrix
for(int i = 0; i <= 5; i++) {
glPushMatrix(); // save the current modelview (assume camera matrix is there)
glMultMatrixf(pointer_to_object_matrix(i)); // apply object matrix
glCallList( OBJECT_LIST[i]);
glPopMatrix(); // restore modelview
}
Looking at this, also note you can't store matrix operations in display lists (ie. if your load_obj() function is setting up matrices anyhow, it won't work because these operations are not "recorded").
The other option is to use some simple object positioning scheme, such as this:
glMatrixMode(GL_MODELVIEW); // we are going to manipulate object matrix
for(int i = 0; i <= 5; i++) {
glPushMatrix(); // save the current modelview (assume camera matrix is there)
glTranslatef(object_x(i), object_y(i), object_z(i)); // change object position
glRotatef(object_yaw(i), 0, 1, 0);
glRotatef(object_pitch(i), 1, 0, 0);
glRotatef(object_roll(i), 0, 0, 1); // change object rotations
glCallList( OBJECT_LIST[i]);
glPopMatrix(); // restore modelview
}
Either way, you need to write some extra functions (pointer_to_object_matrix or object_[x, y, z, yaw, pitch and roll]). If you are just looking to display some objects, try this:
glMatrixMode(GL_MODELVIEW); // we are going to manipulate object matrix
for(int i = 0; i <= 5; i++) {
glPushMatrix(); // save the current modelview (assume camera matrix is there)
const float obj_step = 50; // spacing between the objects
glTranslatef((i - 2.5f) * obj_step, 0, 0); // change object position
glCallList( OBJECT_LIST[i]);
glPopMatrix(); // restore modelview
}
Hope it helps ...
لا تنتمي إلى StackOverflow