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;
    }
  • 相关阅读:
    Linux下安装Tomcat服务器和部署Web应用
    全链路压测
    性能测试二八原则,响应时间2/5/8原则
    chromedriver、firefox-geckodriver、iedriver下载链接
    selenium3+python3环境搭建
    SQL注入原理
    loadrunner之header相关,token等
    安全测试——利用Burpsuite密码爆破(Intruder入侵)
    性能测试之系统架构分析
    性能测试性能分析与调优的原理
  • 原文地址:https://www.cnblogs.com/xiaobo68688/p/2279916.html
Copyright © 2011-2022 走看看