#include<iostream>
#include <gl/glut.h>
#include <gl/gl.h>
#include <gl/glu.h>
using namespace std;
GLfloat x = 0.0f;
GLfloat y = 0.0f;
GLfloat rsize = 25;
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;
GLfloat windowWidth;
GLfloat windowHeight;
void RenderScene(void){
// 선택된 색상으로 화면 지우기
glClear(GL_COLOR_BUFFER_BIT);
/*
glRotatef(5.0f, 1.0f, 0.0f, 0.0f);
glRotatef(2.0f, 0.0f, 1.0f, 0.0f);
glRotatef(0.0f, 0.0f, 0.0f, 1.0f);
*/
// 드로잉 색상 설정 R G B
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(x,y,x+rsize,y-rsize);
// 드로잉 명령 실행
//glFlush();
//드로잉 명령을 실행하고 버퍼를 교체한다.
glutSwapBuffers();
}
void TimerFunction(int value){
if (x > windowWidth - rsize || x < -windowWidth)
xstep = -xstep;
if (y > windowHeight || y < -windowHeight+rsize)
ystep = -ystep;
x += xstep;
y += ystep;
if (x>(windowWidth - rsize + xstep))
x = windowWidth - rsize - 1;
else if (x < -(windowWidth + xstep))
x = -windowWidth - 1;
if (y>(windowHeight + ystep))
y = windowHeight - 1;
else if (y < -(windowHeight + rsize + ystep))
y = -windowHeight - rsize - 1;
glutPostRedisplay();
glutTimerFunc(33, TimerFunction,1);
}
void SetupRC(void){
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
void ChangeSize(GLsizei w, GLsizei h){
GLfloat aspectRatio;
cout << w << " " << h << endl;
if (h == 0) h = 1;
// 클리핑 영역 : 다시 그려져야 할 영역중에서도 화면에 보이는 가시 영역
// 클리핑 영역의 좌표와 물리적인 창의 좌표간에 맵핑이 이루어질 여역을 설정
glViewport(0, 0, w, h);
// (0,0)에서 화면 창 크기만하게 그리기
//좌표계 초기화
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h){
windowWidth = 100;
windowHeight = 100 / aspectRatio;
glOrtho(-100.0, 100.0, -windowWidth, windowHeight, 1.0, -1.0);
}
else{
windowWidth = 100 * aspectRatio;
windowHeight = 100;
glOrtho(-windowWidth, windowHeight, -100.0, 100.0, 1.0, -1.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main()
{
//glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
//더블 버퍼링
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Simple");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1);
SetupRC();
glutMainLoop();
return 0;
}