-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_animation_ver1.cpp
240 lines (207 loc) · 5.57 KB
/
2_animation_ver1.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include<glut.h> // glut.h 헤더 파일 불러옴
#include<GL.H> // GL.H 헤더 파일 불러옴
#include<GLU.H> // GLU.h 헤더 파일 불러옴
#include<cstdio>
#include<math.h>
#include<stdlib.h>
#include<time.h>
// 변수 선언부
GLboolean random = false;
GLint tmpChange = 0;
GLint rotX, rotY;
GLfloat point[4][2] = { {-0.4,0.2}, {-0.4,-0.2}, {0.4, -0.2}, {0.4, 0.2} };
// 도형을 그리기위함 Vertex(정점)
GLint change = 0 ; // type 그려지는 타입
GLfloat R = 1.0, G = 1.0, B = 1.0;
bool cnt = true;
GLboolean rt = false;
GLfloat cX, cY;
void doDisplay(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(R, G, B);
glBegin(GL_LINE_LOOP);
for(int idx = 0; idx < 4 ; idx++){
glVertex3f(point[idx][0], point[idx][1], 0.0);
}
glEnd();
glutSwapBuffers(); // 그린 후 버퍼를 바꿈.
}
void doKeyboard(unsigned char kp, int X, int Y){
switch(kp){
case 'q': case 'Q': exit(0); break;
case '1': change = 1; rt = false;break;
case '2': change = 2; rt = false;break;
case '3': change = 3; rt = false;break;
case '4': change = 4; rt = false;break;
case 'a': change = 5; rt = false;break;
case 'd': change = 6; rt = false;break;
case 'w': change = 7; rt = false;break;
case 'x': change = 8; rt = false;break;
case 's': case 'S':
if(change != 0){
tmpChange = change; change = 0 ;
}else{
change = tmpChange;
} if(random){
rt =true;
random = false;
change = 0;
}else if(rt){
rt = false; random = true;
}break;
case 'r': case 'R': change = 10; break; // reset;
// color
case 'g': case 'G': random = true; break;
case 'h': case 'H': random = false; break;
case 'z': case 'Z': R=1.0; G=B=0; break; // Red
}
}
void action(int v){
float angle, tmpPoint;
switch(v){ // clockwise-rot
case 1:
angle = -0.1;
//glTranslatef(cX, cY, 0);
cX = (point[0][0] + point[2][0])/2.0; //problem
cY = (point[1][1] + point[3][1])/2.0;
for(int i = 0; i < 4 ; i++){
tmpPoint = point[i][0];
printf("%f, %f \n",cX, cY);
point[i][0] = (point[i][0]-cX) * cos(angle) - (point[i][1]-cY) * sin(angle) ;
point[i][1] = (tmpPoint-cX) * sin(angle) + (point[i][1]-cY) * cos(angle) ;
point[i][0] += cX;
point[i][1] += cY;
} break;
case 2://counter-clockwise-rot
angle = 1.0;
cX = (point[0][0] + point[2][0])/2.0; //problem
cY = (point[1][1] + point[3][1])/2.0;
for (int i = 0; i<4; i++){
tmpPoint = point[i][0];
printf("%f, %f \n",cX, cY);
point[i][0] = (point[i][0]-cX) * cos(angle) - (point[i][1]-cY) * sin(angle) ;
point[i][1] = (tmpPoint-cX) * sin(angle) + (point[i][1]-cY) * cos(angle) ;
point[i][0] += cX;
point[i][1] += cY;
} break;
case 3:// 확대
for (int i = 0; i<4; i++){
point[i][0] = point[i][0] * 1.02;
point[i][1] = point[i][1] * 1.02;
}break;
case 4:
for (int i = 0 ; i < 4 ; i++){
point[i][0] = point[i][0] * 0.98;
point[i][1] = point[i][1] * 0.98;
} break;
case 5: //
if(point[0][0] > -1 && point[1][0] > -1 && point[2][0] > -1 && point[3][0] > -1){
for(int i = 0 ; i < 4 ; i++){
point[i][0] = point[i][0] - 0.01;
//cX -= 0.01;
}
} break;
case 6:
if(point[0][0] < 1 && point[1][0] < 1 && point[2][0] < 1 && point[3][0] < 1){
for(int i = 0 ; i < 4 ; i++){
point[i][0] = point[i][0] + 0.01;
//cX += 0.01;
}
} break;
case 7:
if(point[0][1] < 1 && point[1][1] < 1 && point[2][1] < 1 && point[3][1] < 1){
for(int i = 0 ; i < 4 ; i++){
point[i][1] = point[i][1] + 0.01;
//cY += 0.01;
}
} break;
case 8:
if(point[0][1] > -1 && point[1][1] > -1 && point[2][1] > -1 && point[3][1] > -1){
for(int i = 0 ; i < 4 ; i++){
point[i][1] = point[i][1] - 0.01;
//cY -= 0.01;
}
}break;
case 9: // un used
break;
case 10:
point[0][0] = -0.4; point[0][1] = 0.2;
point[1][0] = -0.4; point[1][1] = -0.2;
point[2][0] = 0.4; point[2][1] = -0.2;
point[3][0] = 0.4; point[3][1] = 0.2;
R = G = B = 1.0;
break;
}
}
void doTimer(int v){
if(random){
srand(time(NULL));
int changeRandom = rand()%8 + 1;
action(changeRandom);
glutPostRedisplay(); // Redisplay
glutTimerFunc(10, doTimer, 1);
return;
}else{
action(change);
}
glutPostRedisplay(); // Redisplay
glutTimerFunc(20, doTimer, 1);
}
// Mouse Right Buttom Click Menu
void doMainMenu(int entryID){
switch(entryID){
case 1: random = true; break;
case 2: break;
case 0:
exit(0); break;
}
}
void SubMenu1(int eid){
switch(eid){
case 1: change = 1;break;
case 2: change = 2; break;
}
}
void SubMenu2(int eid){
switch(eid){
case 1: change =3 ; break;
case 2: change =4 ;break;
}
}
void SubMenu3(int eid){
switch(eid){
case 1: change = 5;break;
case 2: change = 6; break;
}
}
void menu(){
GLint SubMenu_1 = glutCreateMenu(SubMenu1);
glutAddMenuEntry("시계방향",1);
glutAddMenuEntry("반시계방향",2);
GLint SubMenu_2 = glutCreateMenu(SubMenu2);
glutAddMenuEntry("확대",1);
glutAddMenuEntry("축소",2);
GLint SubMenu_3 = glutCreateMenu(SubMenu3);
glutAddMenuEntry("좌",1);
glutAddMenuEntry("우",2);
GLint MainMenu = glutCreateMenu(doMainMenu);
glutAddSubMenu("회전",SubMenu_1);
glutAddSubMenu("확대/축소",SubMenu_2);
glutAddSubMenu("이동",SubMenu_3);
glutAddMenuEntry("자유변환",1);
glutAddMenuEntry("프로그램 종료",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL 160929 Example");
menu();
glutKeyboardFunc(doKeyboard);
glutDisplayFunc(doDisplay);
glutTimerFunc(20, doTimer, 1);
glutMainLoop();
return 0;
}