2012-04-21 148 views
1

這裏是我的代碼:OpenGL的三角形旋轉 - 需要一個簡單的修正

#include <stdio.h> 
#include "glut.h" 

float width = 800.0; 
float height = 600.0; 

void changeSize(int w,int h){ 
    if(h==0) 
     h=1; 
    width = w; 
    height = h; 
    GLdouble ratio = 1.0f*w/h; 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    glViewport(0,0,w,h); 

    gluPerspective(45,ratio,1,1000); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    gluLookAt(0,0,10, 
       0,0,-1, 
       0,1,0); 
} 

void renderScene(){ 
    glClearColor(0,0,255,1); 
    glClearDepth(1.0f); 
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 

    //glPushMatrix(); 


    glRotatef(0.09,0.0,0.0,0.09); 
    glColor3f(1.0,0.0,0.0); 
     glBegin(GL_QUADS); 
     glVertex3f(-1.0f,-1.0f,0.0f); 
     glVertex3f(1.0f,-1.0f,0.0f); 
     glVertex3f(1.0f,1.0f,0.0f); 
     glVertex3f(-1.0f,1.0f,0.0f); 
    glEnd(); 
    //glPopMatrix(); 


    glPushMatrix(); 
    glColor3f(0.0,1.0,0.0); 
    glTranslatef(-3.0,0.0,0.0); 

    glRotatef(0.09,0.0,0.0,-1.0); 
    glBegin(GL_TRIANGLE_STRIP); 
     glVertex2f(-1,-1); 
     glVertex2f(0,1); 
     glVertex2f(1,-1); 
     glEnd(); 

    glPopMatrix(); 



    glutSwapBuffers(); 

} 

int main(int argc, char **argv){ 

    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowPosition(100,200); 
    glutInitWindowSize(width,height); 
    glutCreateWindow("OpenGL"); 

    glutDisplayFunc(renderScene); 
    glutIdleFunc(renderScene); 

    glutReshapeFunc(changeSize); 

    glutMainLoop(); 

    return(0); 

} 

我繪製一個矩形和一個三角形。

他們兩個都在旋轉,但我希望矩形靜止不動,只旋轉三角形。我在這裏做錯了什麼?我需要更改推彈出線或其他東西嗎? 我嘗試了很多組合,但我無法做到。

回答

1

你應該做的是更換

glRotatef(0.09,0.0,0.0,0.09); 
glColor3f(1.0,0.0,0.0); 
    glBegin(GL_QUADS); 
    glVertex3f(-1.0f,-1.0f,0.0f); 
    glVertex3f(1.0f,-1.0f,0.0f); 
    glVertex3f(1.0f,1.0f,0.0f); 
    glVertex3f(-1.0f,1.0f,0.0f); 
glEnd(); 

glColor3f(1.0,0.0,0.0); 
    glBegin(GL_QUADS); 
    glVertex3f(-1.0f,-1.0f,0.0f); 
    glVertex3f(1.0f,-1.0f,0.0f); 
    glVertex3f(1.0f,1.0f,0.0f); 
    glVertex3f(-1.0f,1.0f,0.0f); 
glEnd(); 

入棧/矩陣不會做任何改變矩形的變革,它不僅會防止矩形的變換影響三角形。

+0

謝謝,但是當我嘗試它時,它們都停止了,這就是我不瞭解的。應該就像你告訴我的一樣,但他們都停下來了。 – showbiz 2012-04-22 16:10:57

2

如果您不想旋轉矩形,請在繪製矩形之前移除旋轉調用glRotatef(0.09,0.0,0.0,0.09);。在繪製三角形,矩形之前,您需要先push-pop矩陣。要停止旋轉,你必須刪除旋轉調用,而不是push-pop矩陣。

編輯:要旋轉三角形,你需要改變角度。如果你保持固定的角度0.09那麼將不會有動畫。一種簡單的方法是將角度作爲一個全局變量。事情是這樣的:

double ang = 0; // global variable 

void renderScene(){ 
    // other codes 

    glRotatef(ang++,0.0,0.0,-1.0); // use and change ang instead of using fixed 0.09 
    glBegin(GL_TRIANGLE_STRIP); 

    // remaining codes 
} 
+0

感謝,但是當我嘗試它,他們都停下來,這就是我不理解。應該就像你告訴我的一樣,但他們都停下來了。 – showbiz 2012-04-22 16:11:06

+0

你的三角形在旋轉嗎?您在每個渲染步驟都使用固定角度。爲了製作動畫,您需要跟蹤當前角度並在渲染步驟中應用該旋轉角度。您需要根據您所需的旋轉速度更改該角度的值。 – taskinoor 2012-04-22 17:38:19

+0

@showbiz,檢查編輯。 – taskinoor 2012-04-22 17:42:48

0

你的問題是圍繞廣場的旋轉是唯一一個實際上覆合每一幀。由於您在三角形周圍推動/彈出旋轉,因此每次繪製時都會重置爲默認狀態。你只需要做一個小的旋轉,彈出它(從而消除旋轉),然後當你繪製下一個框架時,你做了完全相同的小旋轉,所以三角形不會「它看起來像它一直在移動,它每次都會以相同的旋轉狀態繪製。

因爲您沒有圍繞四邊形彈出旋轉,所以它會在每一幀中混合。你想要做的是每幀增加一個變量,然後旋轉變量的數量。

首先定義一個float rotationAngle作爲你的類的成員變量。

然後在繪製調用,運行是這樣的:

rotationAngle += rotationSpeed; 
glPushMatrix(); 
    //do quad specific transformations 
    //draw quad 
glPopMatrix(); //remove quad specific transforms from stack 

glPushMatrix(); 
    glRotatef(rotationAngle, 0, 0, 1); 
    //do other triangle specific rotations 
    //draw triangle 
glPopMatrix(); //remove triangle's rotation and other transforms