[C :: OpenGL] 움직이는 4각형 :: OpenGL

simpled 2013.08.05 21:06 조회 수 : 3032

움직이~1.JPG

#include<opengl.h>

 

float x=0,y=0,x_step=1,y_step=1,rec=25,Windowheight=100,WindowWidth=100;

 

void Renderscene (void){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1, 0, 0);
    glRectf(x,y,x+rec,y-rec);
    glutSwapBuffers();
    }

 

void Setuprc(void) {
    glClearColor(1, 1, 0, 1);
    }

 

void Changesize(int w, int h){
    GLfloat aspectratio=(GLfloat) w/ (GLfloat) h;
    //if(h==0) h=1;
    glViewport(0,0,w,h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if(w<=h)
        glOrtho(-100, 100, -100/aspectratio, 100/aspectratio, -1, 1);
     else
        glOrtho(-100*aspectratio, 100*aspectratio, -100, 100, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

 

void Timerfunction (int value){
    if(x> WindowWidth-rec || x< -WindowWidth) x_step=-x_step;
    if(y> Windowheight || y< -Windowheight+rec) y_step=-y_step;
    x += x_step;
    y += y_step;
    glutPostRedisplay();
    glutTimerFunc(100,Timerfunction,1);
    }

 

void main (void){
    glutInitDisplayMode(GL_DOUBLE | GLUT_RGB);
    glutCreateWindow("Main");
    glutDisplayFunc(Renderscene);
    glutReshapeFunc(Changesize);
    glutTimerFunc(100,Timerfunction,1);
    Setuprc();
    glutMainLoop();
    }