2012-02-10 86 views
1

在OpenGL中,對於2D中的以下情況,我該如何旋轉這一個四邊形 - 並且只有這一個四邊形(場景中的其他所有東西都應該保持在原來的位置)?在2D中旋轉一個Quad

// Draw in immediate mode 
glBegin(GL_QUADS);      // begin drawing quads 
glVertex2f(box.x,box.y);    // top-left corner 
glVertex2f(box.x+box.w,box.y);   // top-right corner 
glVertex2f(box.x+box.w,box.y+box.h); // bottom-right corner 
glVertex2f(box.x,box.y+box.h);   // bottom-left corner 
glEnd();        // end drawing quads 

glRotatef(angle, x,y,z)似乎旋轉我的整個場景。

回答

3

它括在glPushMatrix和popMatrix

glPushMatrix(GL_MODELVIEW); 

glRotatef(angle, x,y,z); 

// Draw in immediate mode 
glBegin(GL_QUADS);      // begin drawing quads 
glVertex2f(box.x,box.y);    // top-left corner 
glVertex2f(box.x+box.w,box.y);   // top-right corner 
glVertex2f(box.x+box.w,box.y+box.h); // bottom-right corner 
glVertex2f(box.x,box.y+box.h);   // bottom-left corner 
glEnd();        // end drawing quads 

glPopMatrix(GL_MODELVIEW); 

基本上在上面的例子中,你正在推動modevliew矩陣的一個位置上放入堆棧,它保存在本質。然後旋轉模型視圖並繪製四邊形。 之後,您在模型視圖中彈出一個位置,以回到旋轉和繪圖之前的位置。

+0

那麼我需要傳遞給'glRotatef'才能使我的四邊形旋轉90deg? – Ben 2012-02-10 20:02:59

+1

@Ben:你甚至懶得看看glRotate的[文檔](http://www.opengl.org/sdk/docs/man/)嗎?它清楚地說:「指定旋轉角度,以度爲單位。」該頁面實際上是在Google中搜索「glRotatef」的第一個結果。 – 2012-02-10 20:05:58

+0

@Nicol我不知道要傳遞什麼'x,y,z',以便它旋轉整個對象而不是一個向量。 – Ben 2012-02-10 20:08:16