zoukankan      html  css  js  c++  java
  • OpenGL碰到边界返回的方块

    下期预告:Android的OpenGL ES版的碰到边界返回的方块

    #include <GL/glut.h>

    // 方块的初始位置和大小
    GLfloat x1 = 0.0f;
    GLfloat y1 = 0.0f;
    GLfloat rsize = 25;

    // 在x和y方向上的步进大小
    GLfloat xstep = 1.0f;
    GLfloat ystep = 1.0f;

    // 追踪窗口的宽度和高度的变化
    GLfloat windowWidth;
    GLfloat windowHeight;

    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)
    {
    windowWidth = 200;
    windowHeight = 200 / aspectRatio;
    glOrtho(-100.0, 100.0, -100 / aspectRatio, 100 / aspectRatio, 1.0, -1.0);
    }
    else
    {
    windowHeight = 200;
    windowWidth = 200 * aspectRatio;
    glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100, 100, 1.0, -1.0);
    }

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    void SetupRC()
    {
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    }

    void RenderScene()
    {
    // 用当前的清除颜色清除窗口
    glClear(GL_COLOR_BUFFER_BIT);
    // 把绘图颜色设置为红色
    glColor3f(1.0f, 0.0f, 0.0f);
    glRectf(x1, y1, x1 + rsize, y1 - rsize);
    glutSwapBuffers();
    }

    void TimerFunction(int value)
    {
    // 到达边界时翻转方块的方向
    if(x1 > windowWidth / 2 - rsize || x1 < - windowWidth / 2)
    {
    xstep = -xstep;
    }

    if(y1 > windowHeight / 2 || y1 < - windowHeight / 2 - rsize)
    {
    ystep = -ystep;
    }

    // 移动方块
    x1 += xstep;
    y1 += ystep;

    glutPostRedisplay();
    glutTimerFunc(33, TimerFunction, 1);
    }

    int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutCreateWindow("GLUT Example!!!");
    glutDisplayFunc(RenderScene);
    glutReshapeFunc(ChangeSize);
    glutTimerFunc(33, TimerFunction, 1);
    SetupRC();

    glutMainLoop();
    return 0;
    }
  • 相关阅读:
    MSSQL Join的使用
    MSSQL2008 常用sql语句
    Process使用
    c# 多线程 调用带参数函数
    C# 多线程参数传递
    C# 单例模式代码
    C#调用存储过程
    页面布局
    构建:vue项目配置后端接口服务信息
    浏览器工作原理(二):浏览器渲染过程概述
  • 原文地址:https://www.cnblogs.com/xiaobo68688/p/2279916.html
Copyright © 2011-2022 走看看