参考网址:http://www.cnblogs.com/MenAngel/p/5630475.html
问题:画圆,然后拖拽窗口的大小,圆会发生变形。
代码如下:
// ClipWindow.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<GL/glut.h>
#include<math.h>
const float Pi = 3.1415926f;
const int n = 1000;
const float R = 0.8f;
void init(void)
{
glClearColor(0.0f,0.0f,0.0f,0.0f); //设置背景色为黑色
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f);
}
void paintCircle()
{
int i;
glColor3f(1.0f, 1.0f, 1.0f); //红色的圆
glBegin(GL_POLYGON);
for (i = 0; i < n; ++i)
{
glVertex2f(R*cos(2 * Pi / n*i), R*sin(2 * Pi / n*i));
}
glEnd();
glFlush();
}
int _tmain(int argv, char * argc[])
{
glutInit(&argv, argc);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(300, 300);
glutInitWindowPosition(800, 300);
init();
glutCreateWindow("检测形状变化");
glutDisplayFunc(paintCircle);
glutMainLoop();
return 0;
}
运行结果:
当拖拽窗口之后得到:
问题产生的原因:
没有正确设置投影矩阵,默认的透视投影矩阵且宽高比为1。因此宽高比改变了,投影就变形了。因此只要宽高比改变了,投影就应该重新计算。