2012-01-15 78 views
-1

我有插入多個對象的問題。我加載的對象,但他們都在一個地方,openGL加載多個對象

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

能否請你格式化代碼?這很難閱讀。第二件事,load_obj做什麼?如果您有5個具有相同基準座標的物體(例如人物模型),則它們必須位於同一個位置。嘗試在'glCallList'之間使用'glTranslatef'和'glRotatef'。 – Vyktor 2012-01-15 16:46:30

回答

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()函數無論如何設置了矩陣,它將不會工作,因爲這些操作沒有被「記錄」)。

另一種選擇是使用一些簡單的對象定位方案,比如這個:

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,偏航俯仰和滾轉])。如果你只是顯示一些物體,試試這個:

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 
} 

希望它可以幫助...