/*
* File: main.c
* Author: leo
*
* Drawing a simple 3D rectangle program with GLUT
* OpenGL ~~
*/
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
//called to draw scene
void RenderScene(void)
{
//clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
//set current drawing color to read
glColor3f(1.0f,0.0f,0.0f); //设置以后绘制操作所用的颜色
//draw a filled rectangle with current color
glRectf(100.f,150.0f,150.0f,100.0f); //绘制一个填充的矩形
//flush drawing commands
glFlush();
}
//set up the rendering state
void SetupRC(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
//called by GLUT library when the window has changed size
void ChangeSize(GLsizei w, GLsizei h)
{
//prevent a divide by zero
if(h == 0)
h = 1;
//set viewport to window dimensions
glViewport(0,0,w,h);
//reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//在每次进行任何矩阵处理之前都“复位”坐标系
//establish clipping volumn(left,right,bottom,top,near,far)
if(w <= h)
glOrtho(0.0f,250.0f,0.0f,250.0f*h/w,1.0,-1.0);
else
glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0,-1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Plot Rect. using GLUT");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);//窗口大小发生变化,就重新设置坐标系
SetupRC();
glutMainLoop();
return (EXIT_SUCCESS);
}