openGL加载多个对象

时间:2012-01-15 16:42:18

标签: c++ opengl .obj

插入多个对象时遇到问题。我加载对象,但它们都在一个地方,

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 ] );
    }
  } 
}

1 个答案:

答案 0 :(得分:2)

您需要直接指定对象矩阵(就像从3DS文件加载对象一样),如下所示:

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
}

看一下这个,还要注意你不能在显示列表中存储矩阵操作(即,如果你的load_obj()函数无论如何设置矩阵,它都行不通,因为这些操作没有被“记录”)。 / p>

另一种选择是使用一些简单的对象定位方案,例如:

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
}

无论哪种方式,您都需要编写一些额外的函数(pointer_to_object_matrix或object_ [x,y,z,yaw,pitch and roll])。如果您只想显示某些对象,请尝试以下操作:

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
}

希望它有所帮助...