[OpenGL]课后案例07:使用双缓存的程序

//A.7  使用双缓存的程序
/*
*  double.c
*  This program demonstrates double buffering for
*  flicker-free animation.  The left and middle mouse
*  buttons start and stop the spinning motion of the square.
*/
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#define DEGREES_TO_RADIANS 3.14159/180.0
static GLfloat spin = 0.0;
GLfloat x,y;
int singleb, doubleb;
void square()
{
    glBegin(GL_QUADS);
    glVertex2f(x,y);
    glVertex2f(-y,x);
    glVertex2f(-x,-y);
    glVertex2f(y,-x);
    glEnd();
}
void displayd(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    square();
    glutSwapBuffers();
}
void displays(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    square();
    glFlush();
}
void spinDisplay(void)
{
    spin = spin + 2.0;
    if (spin > 360.0) spin = spin - 360.0;
    x=25.0 * cos(DEGREES_TO_RADIANS*spin);
    y=25.0 * sin(DEGREES_TO_RADIANS*spin);
    glutSetWindow(singleb);
    glutPostRedisplay();
    glutSetWindow(doubleb);
    glLoadIdentity();
    glutPostRedisplay();
}
void myinit(void)
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(1.0, 1.0, 1.0);
    glShadeModel(GL_FLAT);
}
void mouse(int btn, int state, int x, int y)
{
    if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
        glutIdleFunc(spinDisplay);
    if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)  
        glutIdleFunc(NULL);
}
void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho(-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w,
        50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
    else
        glOrtho(-50.0*(GLfloat)w/(GLfloat)h,
        50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
/*  Main Loop
*  Open window with initial window size, title bar,
*  RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    singleb=glutCreateWindow("single buffered");
    myinit();
    glutDisplayFunc(displays);
    glutReshapeFunc(myReshape);
    glutIdleFunc(spinDisplay);
    glutMouseFunc(mouse);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    doubleb=glutCreateWindow("double buffered");
    myinit();
    glutDisplayFunc(displayd);
    glutReshapeFunc(myReshape);
    glutIdleFunc(spinDisplay);
    glutMouseFunc(mouse);

    glutMainLoop();
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值