2011-05-10 128 views
2

在下面的程序中,我試圖繪製一個簡單的房子。座標在房屋數組中定義。我需要旋轉房屋並顯示旋轉以及原始房屋。 但是爲什麼不顯示旋轉的房子?OpenGL 2D旋轉問題

//Program to create a house like figure and rotate ir about a given fixed point using OpenGL functions. 
#include <glut.h> 
#include <stdio.h> 

float house [11][2] = {{100,200},{200,250},{300,200},{100,200},{100,100},{175,100},{175,150},{225,150},{225,100},{300,100},{300,200}}; 

void init() 
{ 
    glClearColor(1,1,1,0); 
    glMatrixMode(GL_PROJECTION); 
    gluOrtho2D(0,800,0,800); 
    glMatrixMode(GL_MODELVIEW); 
} 

void display() 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 

    //NORMAL HOUSE 
    glColor3f(1,0,0); 
    glBegin(GL_LINE_LOOP); 

    for(int i=0;i<11;i++) 
     glVertex2fv(house[i]); 
    glEnd(); 
    glFlush(); 


    //ROTATED HOUSE 
    glPushMatrix(); 
    glRotatef(60,0,1,0); 
    glColor3f(1,1,0); 
    glBegin(GL_LINE_LOOP); 

    for(int i=0;i<11;i++) 
     glVertex2fv(house[i]); 
    glEnd(); 
    glFlush(); 
    glPopMatrix(); 
} 

void main(int argc,char** argv) 
{ 
    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(800,800); 
    glutInitWindowPosition(100,100); 
    glutCreateWindow("House rotation"); 
    init(); 
    glutDisplayFunc(display); 
    glutMainLoop(); 
} 

回答

1

嘗試在Z軸代替Y軸旋轉:

//Program to create a house like figure and rotate ir about a given fixed point using OpenGL functions. 
#include <GL/glut.h> 

float house [11][2] = {{100,200},{200,250},{300,200},{100,200},{100,100},{175,100},{175,150},{225,150},{225,100},{300,100},{300,200}}; 

void display() 
{ 
    glClearColor(1,1,1,0); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0,800,0,800); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    //NORMAL HOUSE 
    glColor3f(1,0,0); 
    glBegin(GL_LINE_LOOP); 
    for(int i=0;i<11;i++) 
     glVertex2fv(house[i]); 
    glEnd(); 


    //ROTATED HOUSE 
    glPushMatrix(); 
    glTranslatef(100,100,0); 
    glRotatef(60,0,0,1); 
    glTranslatef(-100,-100,0); 
    glColor3f(1,1,0); 
    glBegin(GL_LINE_LOOP); 
    for(int i=0;i<11;i++) 
     glVertex2fv(house[i]); 
    glEnd(); 
    glPopMatrix(); 
} 

void main(int argc,char** argv) 
{ 
    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(800,800); 
    glutInitWindowPosition(100,100); 
    glutCreateWindow("House rotation"); 
    glutDisplayFunc(display); 
    glutMainLoop(); 
} 

編輯:這應該在左右底角旋轉。

+0

上述修改顯示輸出。但它並沒有圍繞100,100的左下角旋轉房子。所以我試過這個: glTranslatef(-100,-100,0); glRotatef(30,0,0,1); glRotatef(30,0,0,1); glTranslatef(100,100,0); 但它仍然無法正常工作。爲什麼? – footy 2011-05-10 13:46:06

+1

@footy:編輯。看起來這些轉換順序錯誤。 – genpfault 2011-05-10 18:09:54