👩💻/OpenGL
[실습 04] 사각형 변형 애니메이션 만들기 (색상, 방향)
글로랴
2021. 6. 30. 20:25
#include <GL/glut.h>
#include <stdio.h>
GLvoid drawScene(GLvoid);
GLvoid Reshape(int w, int h);
void Mouse(int button, int state, int x, int y);
void Timerfunction(int value);
const int fps = 100;
const int Maxsqcount = 11;
int count = 0;
typedef struct sq
{
int x = -100; // 초기값 설정
int y = -100;
bool lie = false; // 누웠니
};
struct sq sq[11] = { };
void main(int argc, char *argv[])
{ //초기화 함수들
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // 디스플레이 모드 설정
glutInitWindowPosition(100, 100); // 윈도우의 위치지정
glutInitWindowSize(800, 600); // 윈도우의 크기 지정
glutCreateWindow("Example_4"); // 윈도우 생성 (윈도우 이름)
glutDisplayFunc(drawScene); // 출력 함수의 지정
glutTimerFunc(fps, Timerfunction, 1);
glutMouseFunc(Mouse);
glutReshapeFunc(Reshape);
glutMainLoop();
}
// 윈도우 출력 함수
GLvoid drawScene(GLvoid)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // 바탕색을 흰 색으로 지정
glClear(GL_COLOR_BUFFER_BIT); // 설정된 색으로 전체를 칠하기
for (int i = 0; i < Maxsqcount; i++)
{
if (sq[i].lie == true)
{
glColor4f(1.0f, 0.0f, 1.0f, 1.0f); // 그리기 색을 빨간색으로 지정
glRectf(sq[i].x - 20, sq[i].y - 10, sq[i].x + 20, sq[i].y + 10); //사각형 그리기
}
else
{
glColor4f(0.0f, 1.0f, 1.0f, 1.0f); // 그리기 색을 빨간색으로 지정
glRectf(sq[i].x - 10, sq[i].y - 20, sq[i].x + 10, sq[i].y + 20); //사각형 그리기
}
}
glutSwapBuffers(); // 화면에 출력하기
}
GLvoid Reshape(int w, int h)
{
glViewport(0, 0, w, h);
glOrtho(0.0, 800.0, 600.0, 0.0, -1.0, 1.0);
}
void Mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_LEFT)
{
sq[count].x = x;
sq[count].y = y;
count++;
if (count == Maxsqcount) // count값과 Maxcount값이 같으면 초기화
{
for (int i = 0; i < Maxsqcount; i++)
{
sq[i].x = -100;
sq[i].y = -100;
}
count = 0;
}
}
glutPostRedisplay();
}
void Timerfunction(int value)
{
for (int i = 0; i < Maxsqcount; i++)
{
sq[i].lie = !(sq[i].lie); // 참, 거짓이 계속 바뀜.
}
glutPostRedisplay(); //화면재출력
glutTimerFunc(fps, Timerfunction, 1); // 타이머함수 재 설정
}
반응형