2014-11-15 25 views
1

到目前爲止我的代碼OpenGL,如何相互獨立地旋轉對象?

void display (void) 
{ 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer 
glLoadIdentity(); 
gluLookAt( camera[0][0],  camera[0][1],  camera[0][2], 
      camera[1][0],  camera[1][1],  camera[1][2], 
      camera[2][0],  camera[2][1],  camera[2][2]   );   //Set the point looking at 


glRotatef(cubeRot[0], 1.0f, 0.0f, 0.0f); //rotate on x axis 
glRotatef(cubeRot[1], 0.0f, 1.0f, 0.0f); //rotate on y axis 
glRotatef(cubeRot[2], 0.0f, 0.0f, 1.0f); //rotate on z axis 

switch (Rendermode) { //different render mode 

    case 'f': 
      //Draw object I want to rotate 
    break; 

    case 'v': 
      //Draw object I want to rotate   
    break; 

    case 'e': 
      //Draw object I want to rotate 
    break; 

glLoadIdentity(); 

} 

//Draw object I DO NOT want to rotate 

glutSwapBuffers (); // Swap The Buffers To Not Be Left With A Clear Screen 
} 

然而,此刻我的所有對象的同時旋轉,我怎麼能旋轉,我已經注意到要旋轉,同時使我注意到不是那些對象想旋轉還是?

回答

2

用gl封裝線性變換Push/Pop Matrix()並繪製對象。這些功能保存/恢復矩陣的當前狀態,所以它們不會影響其他圖紙。

glPushMatrix(); 
glRotatef(...); 
// glTranslatef(...), 
//glScalef(...); 
drawObject1(); 
glPopMatrix(); 

glPushMatrix(); 
glRotatef(...); 
// glTranslatef(...), 
//glScalef(...); 
drawObject2(); 
glPopMatrix(); 
+0

我已經把glPush和glPop放在我想旋轉的對象的圖畫周圍,但另一個對象仍在旋轉着它們。你介意C&P我的代碼並把它們放在正確的地方嗎? – sam

+0

現在,我看到在代碼中調用'glRotatef()'3次''cubeRot'作爲參數。如果你想在一個單獨的對象上執行它們中的每一個,不要一次執行這些調用,絕對不能在'switch/case'之前執行。圍繞它自己的push/pop操作調用每個調用以及繪製該特定對象的方法調用。 – karlphillip

+0

順便說一句,在'switch'結尾處刪除'glLoadIdentity()'。沒有必要。 – karlphillip

1

由於@ karlphillip指出您需要將旋轉放入glPush/glPop矩陣封裝中。我認爲這個代碼應該工作:

void display (void) 
{ 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer 
glLoadIdentity(); 
gluLookAt( camera[0][0],  camera[0][1],  camera[0][2], 
      camera[1][0],  camera[1][1],  camera[1][2], 
      camera[2][0],  camera[2][1],  camera[2][2]   );   //Set the point looking at 

glPushMatrix(); //-------------------- Encapsulate Rotation ---------------- 
glRotatef(cubeRot[0], 1.0f, 0.0f, 0.0f); //rotate on x axis 
glRotatef(cubeRot[1], 0.0f, 1.0f, 0.0f); //rotate on y axis 
glRotatef(cubeRot[2], 0.0f, 0.0f, 1.0f); //rotate on z axis 

switch (Rendermode) { //different render mode 

    case 'f': 
      //Draw object I want to rotate 
    break; 

    case 'v': 
      //Draw object I want to rotate   
    break; 

    case 'e': 
      //Draw object I want to rotate 
    break; 
} 
glPopMatrix(); //------------------- Encapsulate Rotation Ends ------- 

//Draw object I DO NOT want to rotate 

glutSwapBuffers (); // Swap The Buffers To Not Be Left With A Clear Screen 
} 
+0

謝謝,我以爲我把它們放在那裏,但它不工作,所以顯然我沒有 – sam