码迷,mamicode.com
首页 > 其他好文 > 详细

Opengl

时间:2014-12-26 18:19:34      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

移动的方块

代码:

#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;
}

 

Opengl

标签:

原文地址:http://www.cnblogs.com/lxk2010012997/p/4187275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!