zoukankan      html  css  js  c++  java
  • 移动的方块

    移动的方块

    代码:

    #include <windows.h>
    //#include <GLUT/glut.h>
    #include <GL/glut.h>
    #include <iostream>
    using namespace std;
    
    GLfloat x1 = 0.0f;
    GLfloat y1 = 0.0f;
    GLfloat rsize = 25;
    GLfloat xstep = 1.0f;
    GLfloat ystep = 1.0f;
    
    GLfloat windowWidth;
    GLfloat windowHeight;
    void RenderScene()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0f,0.0f,0.0f);
        cout<<"(x1,y1)= ("<<x1<<","<<y1<<")  (x2,y2)= ("<<(x1+rsize)<<","<<y1-rsize<<")"<<endl;
        glRectf(x1,y1,x1+rsize,y1-rsize);
        glutSwapBuffers();
    }
    
    void TimerFunction(int value)
    {
        cout<<"WindowWidth: "<<windowWidth<<" WindowHeight: "<<windowHeight<<endl;
        if(x1 > windowWidth-rsize || x1 < -windowWidth)
            xstep = -xstep;
        if(y1 > windowHeight || y1 < -windowHeight+rsize)
            ystep = -ystep;
    
        x1 += xstep;
        y1 += ystep;
    
        if(x1 > (windowWidth-rsize+xstep))
            x1 = windowWidth-rsize-1;
        else if(x1 < -(windowWidth+xstep))
        x1 = -windowWidth-1;
    
        if(y1 > (windowHeight+ystep))
            y1 = windowHeight-1;
        else if(y1 < -(windowHeight-rsize+ystep))
        y1 = -windowHeight+rsize-1;
    
        glutPostRedisplay();
        glutTimerFunc(33,TimerFunction,1);
    }
    
    void ChangeSize(GLsizei w,GLsizei h)
    {
        if(h==0)
            h = 1;
    
        GLfloat aspectRatio = (GLfloat)w/(GLfloat)h;
    
        glViewport(0,0,w,h);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
    
        if(w<h)
        {
            glOrtho(-100.0,100.0,-100.0/aspectRatio,100.0/aspectRatio,1.0,-1.0);
            windowWidth = 100;
            windowHeight = 100/aspectRatio;
        }
        else
        {
            glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100.0,100.0,1.0,-1.0);
            windowWidth = 100*aspectRatio;
            windowHeight = 100;
        }
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    
    void SetupRC()
    {
        glClearColor(0.0f,0.0f,1.0f,1.0f);
    }
    
    int main(int argc, char *argv[])
    {
       glutInit(&argc,argv);
       glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
       glutInitWindowSize(800,600);
       glutCreateWindow("Simple");
    
       glutDisplayFunc(RenderScene);
       glutReshapeFunc(ChangeSize);
       glutTimerFunc(33,TimerFunction,1);
    
       SetupRC();
       glutMainLoop();
       return 0;
    }
    态度决定高度,细节决定成败,
  • 相关阅读:
    用 tableExcel导出EXCEL数据
    个人作业——软件评测
    结对第二次作业——某次疫情统计可视化的实现
    软工实践寒假作业(1/2)
    java注解和反射
    共享密钥
    鲁棒性验证-第五小组
    维数约减报告--第五小组
    网络1911、1912 D&S第1次作业--线性表批改总结
    网络1911、1912 C语言第4次作业--函数批改总结
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/4187275.html
Copyright © 2011-2022 走看看