/
HW#3.cpp
467 lines (400 loc) · 11.2 KB
/
HW#3.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <gl/GLUT.h>
#define WINDOW_WIDTH 500
#define WINDOW_HEIGHT 500
#define TIMER_PERIOD 20 // Period for the timer.
#define TIMER_ON 1 // 0:disable timer, 1:enable timer
#define D2R 0.0174532
#define PI 3.1415
//Vector Library
typedef struct {
float x, y ;
} vec_t ;
float magV( vec_t v ) {
return sqrt( v.x * v.x + v.y * v.y ) ;
}
vec_t addV( vec_t v1, vec_t v2 ) {
vec_t tmp ;
tmp.x = v1.x + v2.x ;
tmp.y = v1.y + v2.y ;
return tmp ;
}
vec_t subV( vec_t v1, vec_t v2 ) {
vec_t tmp ;
tmp.x = v1.x - v2.x ;
tmp.y = v1.y - v2.y ;
return tmp ;
}
vec_t mulV( float k, vec_t v ) {
vec_t tmp ;
tmp.x = k * v.x ;
tmp.y = k * v.y ;
return tmp ;
}
vec_t angle2unit( float angle ) {
vec_t tmp ;
tmp.x = cos( angle * D2R ) ;
tmp.y = sin( angle * D2R ) ;
return tmp ;
}
vec_t unitV( vec_t v ) {
return mulV( 1/magV(v), v ) ;
}
float dotP( vec_t v1, vec_t v2 ) {
return v1.x * v2.x + v1.y * v2.y ;
}
// Angle
float orient( vec_t v ) {
return atan2( v.y, v.x ) ;
}
/////////////////////////////////
void circle( int x, int y, int r )
{
float angle ;
glBegin( GL_POLYGON ) ;
for ( int i = 0 ; i < 360 ; i+=5 )
{
angle = i * D2R ;
glVertex2f( x+r*cos(angle), y+r*sin(angle)) ;
}
glEnd();
}
void circle_wire(int x, int y, int r)
{
float angle;
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 360; i += 5)
{
angle = i * D2R;
glVertex2f(x + r*cos(angle), y + r*sin(angle));
}
glEnd();
}
void print(int x, int y, char *string, void *font )
{
int len, i ;
glRasterPos2f( x, y );
len = (int) strlen( string );
for ( i =0; i<len; i++ )
{
glutBitmapCharacter( font, string[i]);
}
}
void vprint(int x, int y, void *font, char *string , ...)
{
va_list ap;
va_start ( ap, string );
char str[1024] ;
vsprintf( str, string, ap ) ;
va_end(ap) ;
int len, i ;
glRasterPos2f( x, y );
len = (int) strlen( str );
for ( i =0; i<len; i++ )
{
glutBitmapCharacter( font, str[i]);
}
}
bool start=false;
bool up = false, down = false, right = false, left = false;
//Declarations
vec_t pointer;
int magu;
int counter=0;
int x=-300;
typedef struct {
float speed;
vec_t vec;
float r,g,b;
float angle;
bool appear;
float radius;
}obj_t;
vec_t unit;
float angle;
obj_t obj[3];
obj_t fire={10,{0,0},1,0,0,0,0,4};
float distance(obj_t obj){ return sqrt( (obj.vec.x - fire.vec.x ) * (obj.vec.x - fire.vec.x ) +
(obj.vec.y - fire.vec.y ) * (obj.vec.y - fire.vec.y ) );}
float findTimef(obj_t obj){
//printf("%f %f\n"distance(obj),obj.speed );
return distance(obj)/fire.speed;
}
float findangular(obj_t obj){
return (atan(obj.radius/(sqrt(distance(obj)*distance(obj)-obj.radius*obj.radius)))/D2R);
}
void drawObject(){
for(int i = 0;i<3;i++){
if(obj[i].appear)
{
obj[i].radius=15;
obj[i].vec.x=(100+i*50)*cos(D2R*obj[i].angle);
obj[i].vec.y=(100+i*50)*sin(D2R*obj[i].angle);
glColor3ub(obj[i].r,obj[i].g,obj[i].b);
circle(obj[i].vec.x,obj[i].vec.y,obj[i].radius);
glColor3f(1,1,1);
vprint(obj[i].vec.x-8,obj[i].vec.y-8,GLUT_BITMAP_8_BY_13,"%.f",obj[i].angle);
}
}
}
void drawLabel(){
glColor3f(1,1,1);
vprint(-250,220,GLUT_BITMAP_8_BY_13,"HOMEWORK #3");
vprint(-250,210,GLUT_BITMAP_8_BY_13,"by BERKAY OZTURK");
}
void drawAxis(){
//axis
glColor3f(1,1,1);
glBegin(GL_LINES);
glVertex2d(0,250);
glVertex2d(0,-250);
glEnd();
glBegin(GL_LINES);
glVertex2d(-250,0);
glVertex2d(250,0);
glEnd();
//orbits
circle_wire(0,0,200);
circle_wire(0,0,150);
circle_wire(0,0,100);
}
void drawText(){
if(x>500) x-800;
if(!start){
print(x,7*sin(10*x*D2R)-200,"C",GLUT_BITMAP_8_BY_13);
print(x+10,7*sin((10*x+10)*D2R)-200,"L",GLUT_BITMAP_8_BY_13);
print(x+20,7*sin((10*x+20)*D2R)-200,"I",GLUT_BITMAP_8_BY_13);
print(x+30,7*sin((10*x+30)*D2R)-200,"C",GLUT_BITMAP_8_BY_13);
print(x+40,7*sin((10*x+40)*D2R)-200,"K",GLUT_BITMAP_8_BY_13);
print(x+60,7*sin((10*x+60)*D2R)-200,"T",GLUT_BITMAP_8_BY_13);
print(x+70,7*sin((10*x+70)*D2R)-200,"O",GLUT_BITMAP_8_BY_13);
print(x+90,7*sin((10*x+90)*D2R)-200,"S",GLUT_BITMAP_8_BY_13);
print(x+100,7*sin((10*x+100)*D2R)-200,"T",GLUT_BITMAP_8_BY_13);
print(x+110,7*sin((10*x+110)*D2R)-200,"A",GLUT_BITMAP_8_BY_13);
print(x+120,7*sin((10*x+120)*D2R)-200,"R",GLUT_BITMAP_8_BY_13);
print(x+130,7*sin((10*x+130)*D2R)-200,"T",GLUT_BITMAP_8_BY_13);
}
}
void drawPointer(){
unit = unitV(pointer);
unit = mulV(70,unit);
glBegin(GL_LINES);
glColor3d(1,0,0);
glVertex2d(0,0);
glVertex2d(unit.x,unit.y);
glEnd();
angle=orient(unit);
angle=angle/D2R;
if(angle<0)
angle+=360;
vprint(cos(angle*D2R)*220,sin(angle*D2R)*220,GLUT_BITMAP_8_BY_13,"%.f",angle);
}
bool checkCollision(obj_t t, obj_t f ) {
float d = sqrt( (t.vec.x - f.vec.x ) * (t.vec.x - f.vec.x ) +
(t.vec.y - f.vec.y ) * (t.vec.y - f.vec.y ) );
return d <= ( t.radius + 4 ) ;
}
void fire_f()
{
if(fire.appear)
{
glColor3f(fire.r,fire.g,fire.b);
circle(fire.vec.x,fire.vec.y,fire.radius);
fire.vec.x+=fire.speed*cos(fire.angle*PI/180);
fire.vec.y+=fire.speed*sin(fire.angle*PI/180);
if ( fire.vec.x < -250 || fire.vec.x > 250
|| fire.vec.y < -250 || fire.vec.y > 250 )
{
fire.appear =false ;
fire.vec.x=0;
fire.vec.y=0;
}
}
}
void display()
{
glClearColor( 0, 0 , 0 , 0 );
glClear( GL_COLOR_BUFFER_BIT );
drawLabel();
drawAxis();
drawPointer();
drawText();
fire_f();
if(start)
{
//AI
//*************************************************************
for(int i = 0 ; i<3; i ++)
{
if(obj[i].appear)
{
int latency = findTimef(obj[i])*obj[i].speed;
if( (angle-latency) < (obj[i].angle+findangular(obj[i])) && (angle-latency) > (obj[i].angle-findangular(obj[i])))
{glColor3f(1,1,1);
circle(obj[i].vec.x,obj[i].vec.y,17);
}
}
drawObject();
}
}
//*******************************************************************
glutSwapBuffers();
}
void onKeyDown(unsigned char key, int x, int y )
{
// exit when ESC is pressed.
if ( key == 27 )
exit(0);
// to refresh the window it calls display() function
glutPostRedisplay() ;
}
void onKeyUp(unsigned char key, int x, int y )
{
// exit when ESC is pressed.
if ( key == 27 )
exit(0);
// to refresh the window it calls display() function
glutPostRedisplay() ;
}
void onSpecialKeyDown( int key, int x, int y )
{
// Write your codes here.
switch (key) {
case GLUT_KEY_UP: up = true; break;
case GLUT_KEY_DOWN: down = true; break;
case GLUT_KEY_LEFT: left = true; break;
case GLUT_KEY_RIGHT: right = true; break;
}
// to refresh the window it calls display() function
glutPostRedisplay() ;
}
void onSpecialKeyUp( int key, int x, int y )
{
// Write your codes here.
switch (key) {
case GLUT_KEY_UP: up = false; break;
case GLUT_KEY_DOWN: down = false; break;
case GLUT_KEY_LEFT: left = false; break;
case GLUT_KEY_RIGHT: right = false; break;
}
// to refresh the window it calls display() function
glutPostRedisplay() ;
}
void onClick( int button, int stat, int x, int y )
{
// Write your codes here.
if(button==GLUT_LEFT_BUTTON && stat== GLUT_DOWN)
{
if(start && !fire.appear)
{fire.appear=true;
fire.angle=angle;
}
if(!start)
{
start=true;
//random color , random speed , random place
for(int i=0 ; i<3;i++){
obj[i].angle=rand()%360;
obj[i].r=rand()%255;
obj[i].g=rand()%255;
obj[i].b=rand()%255;
obj[i].speed=rand()%4-2;
obj[i].appear=true;
if(obj[i].speed==0) obj[i].speed++; }
}
}
// to refresh the window it calls display() function
glutPostRedisplay() ;
}
void onResize( int w, int h )
{
// To setup coordinate system
glViewport( 0,0,w,h) ;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -w/2, w/2, -h/2, h/2, -1, 1);
glMatrixMode( GL_MODELVIEW);
glLoadIdentity();
}
void onMoveDown( int x, int y ) {
// Write your codes here.
// to refresh the window it calls display() function
//glutPostRedisplay() ;
}
void onMove( int x, int y ) {
// Write your codes here.
pointer.x=x-250;
pointer.y=250-y;
magu=magV(pointer);
// to refresh the window it calls display() function
//glutPostRedisplay() ;
}
#if TIMER_ON == 1
void onTimer( int v ) {
glutTimerFunc( TIMER_PERIOD, onTimer, 0 ) ;
// Write your codes here.
x++;
if(x>250)
x=-250;
if (start)
{
for(int i = 0 ; i <3 ; i++){
if(obj[i].angle>=360)
obj[i].angle-=360;
else if(obj[i].angle<0)
obj[i].angle+=360;
obj[i].angle+=obj[i].speed;
}
}
for(int i=0;i<3;i++){
if(obj[i].appear)
if(checkCollision(obj[i],fire))
{
obj[i].appear=false;
fire.appear=false;
fire.vec.x=0;
fire.vec.y=0;
counter++;
printf("%d\n",counter);
}
}
if(counter==3){
start=false;
counter=0;}
// to refresh the window it calls display() function
glutPostRedisplay() ; // display()
}
#endif
int main( int argc, char *argv[] )
{
glutInit(&argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow( "Template File" ) ;
glutDisplayFunc( display ) ;
glutReshapeFunc( onResize );
//
// keyboard registration
//
glutKeyboardFunc( onKeyDown ) ;
glutSpecialFunc( onSpecialKeyDown ) ;
glutKeyboardUpFunc( onKeyUp ) ;
glutSpecialUpFunc( onSpecialKeyUp ) ;
//
// mouse registration
//
glutMouseFunc( onClick) ;
glutMotionFunc( onMoveDown ) ;
glutPassiveMotionFunc( onMove ) ;
#if TIMER_ON == 1
// timer event
glutTimerFunc( TIMER_PERIOD, onTimer, 0 ) ;
#endif
glutMainLoop();
return 0 ;
}