일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 빅데이터분석기사필기
- 크롤링
- 필기후기
- 오류
- 예쁜곳
- 정말
- 데이터전처리
- 호주
- 청청구역
- 언제또가보지
- 갈자신이없다
- 가고싶은데
- 애니메이션
- 너무오래됐다
- 방향변경
- 빅데이터분석기사
- 마우스클릭
- 유니코드 제거
- OpenGL
- 파이썬
- 빅데이터분석기사후기
- selenium
- 사각형변형
- 색상변경
- BeautifulSoup
- 멜버른
- 보라카이
Archives
- Today
- Total
wisdiom 아니고 wisdom
[실습 05] 실습 04 변형 - 사각형 튀기기 본문
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Windows.h>
#define PI 3.14
//#define _cos( angle ) cos ( PI * angle / 180. )
//#define _sin( angle ) sin ( PI * angle / 180. )
GLvoid selectdraw(GLvoid);
GLvoid drawScene(GLvoid);
GLvoid Reshape(int w, int h);
GLvoid Mouse(int button, int state, int x, int y);
GLvoid Keyboard(int key, int x, int y);
void Timerfunction(int value);
void MenuFunc(int);
const int fps = 100;
const int Maxsqcount = 21;
int count = 0;
int SpeedMax = 0;
bool IsSquare = true; // 사각형이니
typedef struct sq
{
int x = -10;
int y = -10;
int speedX, speedY;
bool lie = false;
};
struct sq sq[21] = {};
void main(int argc, char *argv[])
{ //초기화 함수들
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // 디스플레이 모드 설정
glutInitWindowPosition(100, 100); // 윈도우의 위치지정
glutInitWindowSize(800, 600); // 윈도우의 크기 지정
glutCreateWindow("Example_5"); // 윈도우 생성 (윈도우 이름)
//메뉴
int mainmenu;
mainmenu = glutCreateMenu(MenuFunc);
glutAddMenuEntry("Square", 1);
glutAddMenuEntry("Circle", 2);
glutAddMenuEntry("Exit", 3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
//
glutDisplayFunc(drawScene); // 출력 함수의 지정
glutTimerFunc(fps, Timerfunction, 1);
glutMouseFunc(Mouse);
glutSpecialFunc(Keyboard);
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 (IsSquare) // 사각형
{
if (sq[i].lie == true)
{
glColor4f(1.0f, 0.0f, 1.0f, 1.0f); // 그리기 색을 분홍색으로 지정
glRectf(sq[i].x - 10, sq[i].y - 20, sq[i].x + 10, sq[i].y + 20); //사각형 그리기
}
else
{
glColor4f(0.0f, 1.0f, 1.0f, 1.0f); // 그리기 색을 하늘색으로 지정
glRectf(sq[i].x - 20, sq[i].y - 10, sq[i].x + 20, sq[i].y + 10); //사각형 그리기
}
}
else //원
{
glColor4f(1.0f, 1.0f, 0.0f, 1.0f); // 그리기 색을 노랑색으로 지정
glBegin(GL_POLYGON);
for (int degree = 0; degree < 360; degree += 10)
{
float radian = (float)degree * 3.141592f / 180.f;
glVertex2f(sq[i].x + cos(radian) * 10
, sq[i].y + sin(radian) * 10);
}
glEnd();
}
}
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);
}
GLvoid 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;
sq[count].speedX = 1;
sq[count].speedY = -1; // 오른쪽 위로 움직이게 값을 초기화;
count++;
printf("%d %d", x, y);
if (count == Maxsqcount) // 초기화
{
for (int i = 0; i < 20; i++)
{
sq[i].x = -10;
sq[i].y = -10;
}
count = 0;
}
if (SpeedMax < Maxsqcount) // 최고 속도 20
SpeedMax++;
}
glutPostRedisplay();
}
void Timerfunction(int value)
{
for (int i = 0; i < Maxsqcount; i++)
{
sq[i].x += sq[i].speedX; // x축으로 움직이기
if (sq[i].x < 0 || sq[i].x>800) // 벽에 부딪히면
{
sq[i].speedX *= -1; // 반대로 움직이기
}
sq[i].y += sq[i].speedY; // y축으로 움직이기
if (sq[i].y < 0 || sq[i].y>600)
{
sq[i].speedY *= -1;
}
sq[i].lie = !(sq[i].lie); // 참, 거짓이 계속 바뀜.
}
glutPostRedisplay(); //화면재출력
glutTimerFunc(fps, Timerfunction, 1); // 타이머함수 재 설정
}
void MenuFunc(int button)
{
switch (button)
{
case 1:
IsSquare = true;
break;
case 2:
IsSquare = false;
break;
case 3:
exit(1);
}
glutPostRedisplay();
}
GLvoid Keyboard(int key, int x, int y)
{
if (key == GLUT_KEY_DOWN) //속도 줄이기
{
if (IsSquare)
{
for (int i = 0; i < SpeedMax; i++)
{
if (sq[i].speedX > 1) // 최소 속도 1
{
if (sq[i].speedX > 0)
sq[i].speedX--;
else
sq[i].speedX++;
if (sq[i].speedY > 0)
sq[i].speedY--;
else
sq[i].speedY++;
}
}
}
}
else if (key == GLUT_KEY_UP) // 속도 증가
{
if (IsSquare)
{
for (int i = 0; i < SpeedMax; i++)
{
if (sq[i].speedX > 0)
sq[i].speedX++;
else
sq[i].speedX--;
if (sq[i].speedY > 0)
sq[i].speedY++;
else
sq[i].speedY--;
}
}
}
}
반응형
'👩💻 > OpenGL' 카테고리의 다른 글
[실습 12] 원의 경로를 따라 이동/회전하는 애니메이션 구현 (미완성) (0) | 2021.06.30 |
---|---|
[실습 11] 화면에 사인, 코사인, 스프링, 직사각형 그리기 (0) | 2021.06.30 |
[실습 04] 사각형 변형 애니메이션 만들기 (색상, 방향) (0) | 2021.06.30 |
[실습 03] 랜덤으로 여러 모양 나오기 (0) | 2021.06.30 |
[실습 02] 랜덤 바둑판 모양 그리기 (0) | 2021.06.30 |
Comments