-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
683 lines (543 loc) · 14.2 KB
/
main.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#include<bits/stdc++.h>
#include<stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#include <stdio.h>
#define PI 3.14159265359
using namespace std;
float camera_angle = 0;
float camera_x = -3;
float camera_y = 0;
float camera_height = 6;
float camera_rotate_speed = 0.003;
float camera_distance = 40;
float eye_x = 0;
float eye_y = 0;
float eye_z = 0;
float camera_x_angle = 0;
float camera_y_angle = 0;
float mouse_old_pos_x = 0;
float mouse_old_pos_y = 0;
GLboolean ambient = true;
GLboolean diffuse = true;
GLboolean specular = true;
GLuint texture[10];
GLfloat no_light[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_ambient[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 2.0, 25.0, 3.0, 1.0 };
GLfloat balloon_no_mat[] = {0.0,0.0,0.0,1.0};
GLfloat balloon_ambient[] = {0.859, 0.439, 0.576, 1.0};
//GLfloat balloon_ambient[] = {0.200, 1.0, 1.0, 1.0};
GLfloat balloon_diffuse[] = {0.8, 0.48, 0.6, 0.9};
//GLfloat balloon_diffuse[] = {0.300, 1.0, 1.0, 0.9};
GLfloat balloon_specular[] = {0.86, 0.50, 0.60, 1.0};
//GLfloat balloon_specular[] = {0.2600, 1.0, 1.0, 1.0};
GLfloat balloon_shiness[] = {30};
GLfloat wall_no_mat[] = {0.0,0.0,0.0,1.0};
GLfloat wall_ambient[] = {1, 1, 1, 1};
GLfloat wall_diffuse[] = {1, 1, 1, 1};
GLfloat wall_specular[] = { 1, 1, 1, 1};
GLfloat wall_shiness[] = {13};
void lighting(GLfloat no_mat[4],GLfloat mat_ambient[4],GLfloat mat_diffuse[4],GLfloat mat_specular[4],GLfloat shiness[1])
{
if(ambient)
{
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
}
else
{
glMaterialfv(GL_FRONT,GL_AMBIENT,no_mat);
}
if(diffuse)
{
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
}
else
{
glMaterialfv(GL_FRONT,GL_DIFFUSE,no_mat);
}
if(specular)
{
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
}
else
{
glMaterialfv(GL_FRONT,GL_SPECULAR,no_mat);
}
glMaterialfv(GL_FRONT,GL_SHININESS,shiness);
}
struct Image
{
unsigned long sizeX;
unsigned long sizeY;
char *data;
};
int ImageLoad(char *filename, Image *image)
{
FILE *file;
unsigned long size; //size of the image in bytes
unsigned long i; //standard counter
unsigned short int planes; //number of planes in Image(must be 1)
unsigned short int bpp; //number of bits per pixel(must be 24)
char temp; //temporary color storage for bgr-rgb conversion
//make sure the file in here
file = fopen(filename, "rb");
if(file==NULL)
{
printf("Can't open file!");
}
//seek through the bmp header, up to width/height
fseek(file,18,SEEK_CUR);
//read the width
i = fread(&image->sizeX,4,1,file);
//read the height
i = fread(&image->sizeY,4,1,file);
//calculate the size (assuming 24 bits or 3 bytes per pixel)
size = image->sizeX * image->sizeY * 3;
//read the plane
fread(&planes, 2, 1, file);
//read the bits per pixel
fseek(file, 24, SEEK_CUR);
//read the data
image->data = (char*)malloc(size);
i=fread(image->data, size, 1, file);
for(i=0; i<size; i+=3) //reverse all ol color bgr -> rgb
{
temp = image->data[i];
image->data[i] = image->data[i+1];
image->data[i+1] = temp;
}
fclose(file);
//we're done
return 1;
}
void loadTexture(char* file_name, int tex_id)
{
Image *image1;
//allocate space for texture
image1 = (Image*) malloc(sizeof(Image));
//Image not loaded
if(!ImageLoad(file_name,image1))
{
exit(1);
}
texture[tex_id] = tex_id;
glBindTexture(GL_TEXTURE_2D,tex_id);
glTexImage2D(GL_TEXTURE_2D,0,3,image1->sizeX,image1->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,image1->data);
//scale linearly when image is bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//scale linearly when image is smaller than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
void draw_solid_cube(GLfloat size)
{
float x1,y1,z1;
float x2,y2,z2;
float x3,y3,z3;
float x4,y4,z4;
x1 = -size/2;
y1 = -size/2;
z1 = -size/2;
x2 = size/2;
y2 = -size/2;
z2 = -size/2;
x3 = size/2;
y3 = -size/2;
z3 = size/2;
x4 = -size/2;
y4 = -size/2;
z4 = size/2;
float x5,y5,z5;
float x6,y6,z6;
float x7,y7,z7;
float x8,y8,z8;
x5 = -size/2;
y5 = size/2;
z5 = -size/2;
x6 = size/2;
y6 = size/2;
z6 = -size/2;
x7 = size/2;
y7 = size/2;
z7 = size/2;
x8 = -size/2;
y8 = size/2;
z8 = size/2;
float n1_x = 0, n1_y = -1, n1_z = 0; //face that faces towards bottom
float n2_x = 0, n2_y = 1, n2_z = 0; //up
float n3_x = 0, n3_y = 0, n3_z = -1; //front
float n4_x = 0, n4_y = 0, n4_z = 1; //back
float n5_x = 1, n5_y = 0, n5_z = 0; //right
float n6_x = -1, n6_y = 0, n6_z = 0; //left
glBegin(GL_QUADS);
//Quad 1
glNormal3f(n1_x,n1_y,n1_z);
glTexCoord2f(0,0);
glVertex3f(x1,y1,z1);
glTexCoord2f(1,0);
glVertex3f(x2,y2,z2);
glTexCoord2f(1,1);
glVertex3f(x3,y3,z3);
glTexCoord2f(0,1);
glVertex3f(x4,y4,z4);
//Quad 2
glNormal3f(n2_x,n2_y,n2_z);
glTexCoord2f(0,0);
glVertex3f(x8,y8,z8);
glTexCoord2f(1,0);
glVertex3f(x7,y7,z7);
glTexCoord2f(1,1);
glVertex3f(x6,y6,z6);
glTexCoord2f(0,1);
glVertex3f(x5,y5,z5);
//Quad 3
glNormal3f(n3_x,n3_y,n3_z);
glTexCoord2f(0,0);
glVertex3f(x5,y5,z5);
glTexCoord2f(1,0);
glVertex3f(x6,y6,z6);
glTexCoord2f(1,1);
glVertex3f(x2,y2,z2);
glTexCoord2f(0,1);
glVertex3f(x1,y1,z1);
//Quad 4
glNormal3f(n4_x,n4_y,n4_z);
glTexCoord2f(0,0);
glVertex3f(x7,y7,z7);
glTexCoord2f(1,0);
glVertex3f(x8,y8,z8);
glTexCoord2f(1,1);
glVertex3f(x4,y4,z4);
glTexCoord2f(0,1);
glVertex3f(x3,y3,z3);
//Quad 5
glNormal3f(n5_x,n5_y,n5_z);
glTexCoord2f(0,0);
glVertex3f(x6,y6,z6);
glTexCoord2f(1,0);
glVertex3f(x7,y7,z7);
glTexCoord2f(1,1);
glVertex3f(x3,y3,z3);
glTexCoord2f(0,1);
glVertex3f(x2,y2,z2);
//Quad 6
glNormal3f(n6_x,n6_y,n6_z);
glTexCoord2f(0,0);
glVertex3f(x1,y1,z1);
glTexCoord2f(1,0);
glVertex3f(x4,y4,z4);
glTexCoord2f(1,1);
glVertex3f(x8,y8,z8);
glTexCoord2f(0,1);
glVertex3f(x5,y5,z5);
glEnd();
}
void draw_wall()
{
lighting(wall_no_mat,wall_ambient,wall_diffuse,wall_specular,wall_shiness);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glPushMatrix();
glTranslatef(25,10,0);
glScalef(0.2,40,50);
draw_solid_cube(1);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
int num_balloon = 5;
float balloon_z[] = {2,4,6,8,10};
float balloon_y[] = {2.5,3.5,1.5,4.5,2.5};
float balloon_speed[] = {0.001,0.002,0.005,0.001,0.002};
float balloon_top_height = 20;
float balloon_bottom_start = -5.5;
float screen_right = 12;
float arrow_z = -10;
float arrow_y = -5.5;
int score = 0;
int miss = 7;
bool arrow_on_hand = true;
bool up=true;
bool down=false;
int rand_int_range(int min_v, int max_v)
{
return min_v + (rand() % (max_v - min_v + 1));
}
void showText(string str,float x,float y,float z)
{
glRasterPos3f(x,y,z);
glColor3f(1.0,1.0,1.0);
string s = str;
void * font = GLUT_BITMAP_TIMES_ROMAN_24;
for (std::string::iterator i = s.begin(); i != s.end(); ++i)
{
char c = *i;
//this does nothing, color is fixed for Bitmaps when calling glRasterPos
//glColor3f(1.0, 0.0, 1.0);
glutBitmapCharacter(font, c);
}
}
void draw_arrow()
{
if (arrow_on_hand == true)
{
if(up== true)
{
arrow_y = arrow_y + 0.02;
if (arrow_y > balloon_top_height)
{
arrow_y = -5.5;
}
}
else
{
arrow_y = arrow_y - 0.02;
if (arrow_y < balloon_bottom_start )
{
arrow_y = balloon_top_height;
}
}
}
else if (arrow_on_hand == false)
{
arrow_z = arrow_z + 0.05;
if (arrow_z > screen_right)
{
arrow_z = -10;
arrow_y = -5.5;
arrow_on_hand = true;
}
////////////////////////////////////
for (int i = 0; i < num_balloon; i++)
{
float b_y = balloon_y[i];
float b_z = balloon_z[i];
float y1 = b_y + 1.5;
float z1 = b_z - 0.8;
float y2 = b_y - 1.5;
float z2 = b_z + 0.8;
float a_z = arrow_z+(3.5/2);
float a_y = arrow_y;
if (a_z >= z1 && a_z <= z2)
{
if (a_y >= y2 && a_y <= y1)
{
balloon_y[i] = balloon_bottom_start;
balloon_speed[i] = ((float)rand_int_range(1,10) / 1000.0);
score++;
system("CLS");
printf("Score %d", score);
}
}
}
}
lighting(balloon_no_mat,balloon_ambient,balloon_diffuse,balloon_specular,balloon_shiness);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glPushMatrix();
glTranslatef(20,arrow_y,arrow_z);
glScalef(0.2,0.2,3.5);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(20,arrow_y,arrow_z+1.5);
glScalef(0.2,0.2,3.5);
glutSolidCone(2,0.1,30,30);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
void draw_Sphere()
{
for (int j = 0; j < num_balloon; j++)
{
balloon_y[j] = balloon_y[j] + balloon_speed[j];
if (balloon_y[j] > balloon_top_height)
{
balloon_y[j] = balloon_bottom_start;
balloon_speed[j] = ((float)rand_int_range(1,10) / 1000.0);
miss--;
}
lighting(balloon_no_mat,balloon_ambient,balloon_diffuse,balloon_specular,balloon_shiness);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
// glDisable(GL_LIGHTING);
//glColor3f(.3,.5,.7);
glPushMatrix();
glTranslatef(20,balloon_y[j],balloon_z[j]);
glScalef(0.8,1.5,0.8);
glutSolidSphere(1.0, 50, 50);
glPopMatrix();
glPushMatrix();
glTranslatef(20,balloon_y[j] - 1.5,balloon_z[j]);
glScalef(0.08,1.5,0.08);
glutSolidCube(1);
glPopMatrix();
// glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
}
}
void handleKeyPressed(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
case 'a':
case 'A':
camera_y = camera_y - 0.5;
//center_z = center_z - 0.5;
break;
case 'd':
case 'D':
camera_y = camera_y + 0.5;
//center_z = center_z + 0.5;
break;
case 'w':
case 'W':
up=true;
down=false;
break;
case 's':
case 'S':
// camera_x_angle+=.15;
// camera_y_angle+=.15;
up=false;
down=true;
break;
case 'k':
case 'K':
break;
case 'h':
case 'H':
camera_x_angle-=.15;
camera_y_angle-=.15;
break;
case 'j':
case 'J':
break;
case '1':
ambient=!ambient;
break;
case '2':
diffuse=!diffuse;
break;
case '3':
specular=!specular;
break;
}
}
void initRendering()
{
glEnable(GL_DEPTH_TEST);
}
void handleResize(int w, int h)
{
glViewport(0,0,w,h);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(-10,10,-10,10);
gluPerspective(60,1,5,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera_x, camera_height, camera_y, camera_x + cos(camera_x_angle), camera_height, camera_y + sin(camera_y_angle), 0, 1, 0);
//gluLookAt(camera_x,camera_height,camera_y,1,0,0,0,1,0);
//showing text
draw_wall();
if(miss>0)
{
stringstream ss,ss1;
ss << score;
ss1<<miss;
string str = ss.str();
string str1 = ss1.str();
showText("Score : "+str,4,9.7,-1);
// showText("Life: "+str1,4,9.3,-1);
draw_arrow();
draw_Sphere();
}
else
{
stringstream ss,ss1;
ss << score;
ss1<<miss;
string str = ss.str();
string str1 = ss1.str();
showText("GAME OVER",4,9.7,-1);
showText("SCORE : "+str,4,9,-1);
}
//lighting in scene
glEnable(GL_LIGHT0);
if(ambient)
{
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
}
else
{
glLightfv(GL_LIGHT0, GL_AMBIENT, no_light);
}
if(diffuse)
{
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
}
else
{
glLightfv(GL_LIGHT0, GL_DIFFUSE, no_light);
}
if(specular)
{
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
}
else
{
glLightfv(GL_LIGHT0, GL_SPECULAR, no_light);
}
glutSwapBuffers();
}
static void idle(void)
{
glutPostRedisplay();
}
static void mouse_func(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
if (arrow_on_hand == true)
{
arrow_on_hand = false;
}
}
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1000,800);
glutInitWindowPosition(100,100);
glutCreateWindow("Shooting Balloon");
initRendering();
glClearColor(0,0,0,1);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glGenTextures(10,texture);
loadTexture("C:\\Users\\Prince\\Desktop\\1207056 Shooting_Balloon\\bg2.bmp", 0);
glutDisplayFunc(display);
glutKeyboardFunc(handleKeyPressed);
glutReshapeFunc(handleResize);
glutIdleFunc(idle);
glutMouseFunc(mouse_func);
glutMainLoop();
return 0;
}