《计算机图形学》学习笔记
直接贴代码
#include "glut.h"
void SetupRC(void)
{
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
void ChangeSize(int w, int h)
{
GLfloat aspectratio;
if (h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectratio = (GLfloat)w / (GLfloat)h;
if (w <= h)
glOrtho(-100.0, 100.0, -100.0 / aspectratio, 100.0 / aspectratio, -100, 100);
else
glOrtho(-100.0 * aspectratio, 100.0 * aspectratio, -100.0, 100.0, -100, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(400, 200);
glutInitWindowSize(800, 800);
glutCreateWindow("第一次OpenGL作业");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
SetupRC();
glutMainLoop();
return 0;
}
运行结果 改变窗口大小
|