OpenGL 2D旋转问题
Posted
技术标签:
【中文标题】OpenGL 2D旋转问题【英文标题】:OpenGL 2D rotation problem 【发布时间】:2011-05-10 13:11:46 【问题描述】:在下面的程序中,我试图画一个简单的房子。坐标在房屋数组中定义。我需要旋转房子并显示旋转的房子和原来的房子。 但是为什么旋转的房子没有显示出来呢?
//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();
编辑:这个应该围绕底角旋转。
【讨论】:
以上修改显示输出。但它不会围绕位于 100,100 的左下角旋转房子。所以我尝试了这个: glTranslatef(-100,-100,0); glRotatef(30,0,0,1); glTranslatef(100,100,0);但它仍然不起作用。为什么? @footy:已编辑。看起来这些转换的顺序有误。以上是关于OpenGL 2D旋转问题的主要内容,如果未能解决你的问题,请参考以下文章