-1

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 ] );
    }
  } 
}
pmr
  • 58,701
  • 10
  • 113
  • 156
quba88
  • 174
  • 4
  • 18
  • 3
    Can you please format the code? It's hard to read. The second thing, what does load_obj do? If you have 5 object with the same base coordinates (for example model of person), they have to be in one place. Try using `glTranslatef` and `glRotatef` between `glCallList`s. – Vyktor Jan 15 '12 at 16:46

1 Answers1

2

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 ...

the swine
  • 10,713
  • 7
  • 58
  • 100