Opengl 绘制矩形

使用工具 :VC++ 6.0

 

#include "stdafx.h"
#include <windows.h>     /* 有的书作glos.h,意思是根据编译所在的操作系统include操作系统的头文件根据编译所在的操作系统include操作系统的头文件 */
#include <gl\glut.h>

//Called to draw scene
void RenderSence(void)
{
//Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);

//把绘制颜色设置为红色
glColor3f(1.0f,0.0f,0.0f);

//用当前颜色绘制一个矩形
glRectf(-25.0f,25.0f,25.0f,-25.0f);
//Flush drawing commands  ---刷新绘制命令
glFlush();
}

 

//Set up the rendering state
void SetupRC(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);      //此时背景色为蓝色
}
void  ChangeSize(GLsizei w,GLsizei h){
    GLfloat aspectRatio;

//防止被0所除
    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/aspectRatio,100.0/aspectRatio,1.0,-1.0);
    else
        glOrtho(-100*aspectRatio,100.0*aspectRatio,-100.0,100.0,-1.0,1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, char* argv[])
{
    printf("Hello World!\n");
    glutInitDisplayMode(GLUT_SINGLE|GLUT_SINGLE|GLUT_RGB);
    glutCreateWindow("Simple");        //窗口名为“Simple”
    glutDisplayFunc(RenderSence);
    glutReshapeFunc(ChangeSize);
    SetupRC();
    glutMainLoop();
    return 0;
}

你可能感兴趣的:(Opengl 绘制矩形)