OpenGL的消隐与双缓冲(2)

#include "stdafx.h"


#include <math.h>

#include <gl/glut.h>

#include <gl/gl.h>


bool mouseisdown=false ;

bool loopr=false ;

int mx,my;

int ry=30;

int rx=30;

 

 

void timer(int p)

{

     ry-=5;

        glutPostRedisplay();

     if (loopr)

          glutTimerFunc(200,timer,0);

 

 

}

 

 

void mouse(int button, int state, int x, int y)

{

    if (button == GLUT_LEFT_BUTTON)

     {

 

 

        if (state == GLUT_DOWN)

         {    mouseisdown=true ; loopr=false ;}

         else

              mouseisdown=false ;

     }

     if (button== GLUT_RIGHT_BUTTON)

         if (state == GLUT_DOWN)

         {loopr=true ; glutTimerFunc(200,timer,0);}

   

}

 

 

void motion(int x, int y)

{

    if (mouseisdown==true )

    {

        ry+=x-mx;

         rx+=y-my;

         mx=x;

         my=y;

         glutPostRedisplay();

    }

}

 

 

void special(int key, int x, int y)

 

 

{

    switch (key)

    {

    case GLUT_KEY_LEFT:

        ry-=5;

        glutPostRedisplay();

        break ;

    case GLUT_KEY_RIGHT:

       ry+=5;

        glutPostRedisplay();

        break ;

    case GLUT_KEY_UP:

        rx+=5;

        glutPostRedisplay();

        break ;

    case GLUT_KEY_DOWN:

        rx-=5;

      

        glutPostRedisplay();

        break ;

    }

}

 

 

 

 

void init()

    //设置OpenGL的一些状态变量的初始值

{

    glEnable(GL_DEPTH_TEST);     //深度测试

    glDepthFunc(GL_LESS);                      //设置深度测试函数

    glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);        //设置多边形显示模式为正反面都是填充显示

    glClearColor(1,1,1,1);          //设置刷新背景色

    glClearDepth(1);          //设置清除深度缓冲区所用的值

}

 

 

void display()

{

       

         float red[3]={1,0,0};

         float blue[3]={0,1,0};

         float green[3]={0,0,1};

         float yellow[3]={1,1,0};   

 

 

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        

        

         glLoadIdentity();

         glRotatef(ry,0,1,0);      

         glRotatef(rx,1,0,0);

        

             

         glBegin(GL_QUADS);

              glColor3fv(green);

              glVertex3f(0.5,0.5,0);

              glVertex3f(-0.5,0.5,0);

              glVertex3f(-0.5,-0.5,0);

              glVertex3f(0.5,-0.5,0);

             

        

         glEnd();

        

         glBegin(GL_QUADS);

              glColor3fv(red);

              glVertex3f(0.5,0.5,0.3);

              glVertex3f(-0.5,0.5,-0.3);

              glVertex3f(-0.5,-0.5,-0.3);

              glVertex3f(0.5,-0.5,0.3);

             

        

         glEnd();

        

         glBegin(GL_QUADS);

             

              glColor3fv(yellow);

              glVertex3f(0.5,0.5,-0.3);

              glVertex3f(-0.5,0.5,0.3);

              glVertex3f(-0.5,-0.5,0.3);

              glVertex3f(0.5,-0.5,-0.3);

             

        

         glEnd();

 

 

        glFlush();

         glutSwapBuffers();


}

   


int main(int argc, char ** argv)

{

  

    glutInit(&argc, argv);

    glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGBA|GLUT_DEPTH);         //设置显示模式:单缓冲区, RGBA颜色模式

    glutInitWindowSize (400, 400);        //设置窗口宽度、高度

    glutInitWindowPosition(100,100);         //设置窗口位置

    glutCreateWindow ("double");        //弹出窗口

     init();

    glutDisplayFunc (display);        //设置窗口刷新的回调函数

     glutMouseFunc(mouse);         //设置鼠标器按键回调函数

    glutMotionFunc(motion);

     glutSpecialFunc(special);

    glutMainLoop();        //开始主循环

    return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值