-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample_GL3_2D.cpp
1600 lines (1352 loc) · 52.5 KB
/
Sample_GL3_2D.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <time.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <ao/ao.h>
#include <mpg123.h>
#define BITS 8
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace std;
GLFWwindow* window;
float SCREEN_ZOOM_STEP = 0.03;
int score=0,numberOfBlack=0,numberOfMismatch=0;
float redx=1.5,greenx=-1.5,turret_angle=1,turrety=0;
float screen_left,screen_right,screen_top,screen_bottom;
float camera_rotation_angle = 90,laser_x,laser_y;
float turret_rectangle_rotation = 0,rectangle_rotation=0,laser_rotation;
float screen_x=0,screen_y=0,blockSpeed=0.010,zoom=1.0,CURSOR_X=0,CURSOR_Y=0;
//int draw_flag = 0, number_of_blocks = 0, i;
vector <float> block_x;
vector <float> block_y;
vector <int> block_color;
bool redbucket_clicked = false, greenbucket_clicked = false, turret_clicked = false;
float mirror_y=0.2;
int laserFlag=0;
struct VAO {
GLuint VertexArrayID;
GLuint VertexBuffer;
GLuint ColorBuffer;
GLenum PrimitiveMode;
GLenum FillMode;
int NumVertices;
};
typedef struct VAO VAO;
struct GLMatrices {
glm::mat4 projection;
glm::mat4 model;
glm::mat4 view;
GLuint MatrixID;
} Matrices;
GLuint programID;
/* Function to load Shaders - Use it as it is */
GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path) {
// Create the shaders
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
// Read the Vertex Shader code from the file
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
if(VertexShaderStream.is_open())
{
std::string Line = "";
while(getline(VertexShaderStream, Line))
VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
}
// Read the Fragment Shader code from the file
std::string FragmentShaderCode;
std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
if(FragmentShaderStream.is_open()){
std::string Line = "";
while(getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
FragmentShaderStream.close();
}
GLint Result = GL_FALSE;
int InfoLogLength;
// Compile Vertex Shader
printf("Compiling shader : %s\n", vertex_file_path);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);
// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> VertexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
// Compile Fragment Shader
printf("Compiling shader : %s\n", fragment_file_path);
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
glCompileShader(FragmentShaderID);
// Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
// Link the program
fprintf(stdout, "Linking program\n");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)) );
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
return ProgramID;
}
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
void quit(GLFWwindow *window)
{
glfwDestroyWindow(window);
glfwTerminate();
// exit(EXIT_SUCCESS);
}
/* Generate VAO, VBOs and return VAO handle */
struct VAO* create3DObject (GLenum primitive_mode, int numVertices, const GLfloat* vertex_buffer_data, const GLfloat* color_buffer_data, GLenum fill_mode=GL_FILL)
{
struct VAO* vao = new struct VAO;
vao->PrimitiveMode = primitive_mode;
vao->NumVertices = numVertices;
vao->FillMode = fill_mode;
// Create Vertex Array Object
// Should be done after CreateWindow and before any other GL calls
glGenVertexArrays(1, &(vao->VertexArrayID)); // VAO
glGenBuffers (1, &(vao->VertexBuffer)); // VBO - vertices
glGenBuffers (1, &(vao->ColorBuffer)); // VBO - colors
glBindVertexArray (vao->VertexArrayID); // Bind the VAO
glBindBuffer (GL_ARRAY_BUFFER, vao->VertexBuffer); // Bind the VBO vertices
glBufferData (GL_ARRAY_BUFFER, 3*numVertices*sizeof(GLfloat), vertex_buffer_data, GL_STATIC_DRAW); // Copy the vertices into VBO
glVertexAttribPointer(
0, // attribute 0. Vertices
3, // size (x,y,z)
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glBindBuffer (GL_ARRAY_BUFFER, vao->ColorBuffer); // Bind the VBO colors
glBufferData (GL_ARRAY_BUFFER, 3*numVertices*sizeof(GLfloat), color_buffer_data, GL_STATIC_DRAW); // Copy the vertex colors
glVertexAttribPointer(
1, // attribute 1. Color
3, // size (r,g,b)
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
return vao;
}
/* Generate VAO, VBOs and return VAO handle - Common Color for all vertices */
struct VAO* create3DObject (GLenum primitive_mode, int numVertices, const GLfloat* vertex_buffer_data, const GLfloat red, const GLfloat green, const GLfloat blue, GLenum fill_mode=GL_FILL)
{
GLfloat* color_buffer_data = new GLfloat [3*numVertices];
for (int i=0; i<numVertices; i++) {
color_buffer_data [3*i] = red;
color_buffer_data [3*i + 1] = green;
color_buffer_data [3*i + 2] = blue;
}
return create3DObject(primitive_mode, numVertices, vertex_buffer_data, color_buffer_data, fill_mode);
}
/* Render the VBOs handled by VAO */
void draw3DObject (struct VAO* vao)
{
// Change the Fill Mode for this object
glPolygonMode (GL_FRONT_AND_BACK, vao->FillMode);
// Bind the VAO to use
glBindVertexArray (vao->VertexArrayID);
// Enable Vertex Attribute 0 - 3d Vertices
glEnableVertexAttribArray(0);
// Bind the VBO to use
glBindBuffer(GL_ARRAY_BUFFER, vao->VertexBuffer);
// Enable Vertex Attribute 1 - Color
glEnableVertexAttribArray(1);
// Bind the VBO to use
glBindBuffer(GL_ARRAY_BUFFER, vao->ColorBuffer);
// Draw the geometry !
glDrawArrays(vao->PrimitiveMode, 0, vao->NumVertices); // Starting from vertex 0; 3 vertices total -> 1 triangle
}
/**************************
* Customizable functions *
**************************/
//float triangle_rot_dir = 1;
float rectangle_rot_dir = 1;
//bool triangle_rot_status = true;
bool rectangle_rot_status = true;
/* Executed when a regular key is pressed/released/held-down */
/* Prefered for Keyboard events */
/*void updateZoomAndPan () {
float top = (screen_y + 4) / zoom;
float bottom = (screen_y - 4) / zoom;
float left = (screen_x - 4) / zoom;
float right = (screen_x + 4) / zoom;
Matrices.projection = glm::ortho(left, right, bottom, top, 0.1f, 500.0f);
}*/
void keyboard (GLFWwindow* window, int key, int scancode, int action, int mods)
{
// Function is called first on GLFW_PRESS.
if (action == GLFW_RELEASE) {
switch (key) {
// case GLFW_KEY_C:
// rectangle_rot_status = !rectangle_rot_status;
break;
// case GLFW_KEY_:
// triangle_rot_status = !triangle_rot_status;
// break;
//case GLFW_KEY_X:
// do something ..
// break;
default:
break;
}
}
if (action == GLFW_PRESS) {
switch (key) {
case GLFW_KEY_SPACE:
laserFlag=1;
laser_x=-4;
laser_y=turrety;
laser_rotation=turret_rectangle_rotation;
break;
case GLFW_KEY_ESCAPE:
if(score > 100)
cout << "YOU WON" << endl;
if(score < 100)
cout << "YOU LOST" << endl;
cout << score << endl;
quit(window);
break;
case GLFW_KEY_M:
blockSpeed+=0.001;
if(blockSpeed>=0.020)
blockSpeed=0.020;
break;
case GLFW_KEY_N:
blockSpeed-=0.001;
if(blockSpeed<=0.005)
blockSpeed=0.005;
break;
case GLFW_KEY_LEFT:
if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL))
{
redx=redx-0.1;
if(redx<-2.5)
redx=-2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL))
{
redx=redx-0.1;
if(redx<-2.5)
redx=-2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_RIGHT_ALT))
{
greenx=greenx-0.1;
if(greenx<-2.5)
greenx=-2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_LEFT_ALT))
{
greenx=greenx-0.1;
if(greenx<-2.5)
greenx=-2.5;
break;
}
else
{
screen_x-=0.1;
if(screen_x<-4)
screen_x=-4;
break;
}
case GLFW_KEY_RIGHT:
if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL))
{
redx=redx+0.1;
if(redx>2.5)
redx=2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL))
{
redx=redx+0.1;
if(redx>2.5)
redx=2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_RIGHT_ALT))
{
greenx=greenx+0.1;
if(greenx>2.5)
greenx=2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_LEFT_ALT))
{
greenx=greenx+0.1;
if(greenx>2.5)
greenx=2.5;
break;
}
else
{
screen_x+=0.1;
if(screen_x>=3.0)
screen_x=3.0;
break;
}
case GLFW_KEY_S:
turrety=turrety+0.1;
if(turrety>3.5)
turrety=3.5;
break;
case GLFW_KEY_F:
turrety=turrety-0.1;
if(turrety<-3.5)
turrety=-3.5;
break;
case GLFW_KEY_A:
turret_rectangle_rotation+=3;
if(turret_rectangle_rotation>=90)
turret_rectangle_rotation=90;
break;
case GLFW_KEY_D:
turret_rectangle_rotation-=3;
if(turret_rectangle_rotation<=-90)
turret_rectangle_rotation=-90;
break;
case GLFW_KEY_UP:
zoom+=0.1;
if(zoom>=2.0)
zoom=2.0;
break;
case GLFW_KEY_DOWN:
zoom-=0.1;
if(zoom<=1.0)
zoom=1.0;
break;
default:
break;
}
}
if (action== GLFW_REPEAT) {
switch (key) {
case GLFW_KEY_SPACE:
laserFlag=1;
laser_x=-4;
laser_y=turrety;
laser_rotation=turret_rectangle_rotation;
break;
case GLFW_KEY_ESCAPE:
if(score > 100)
cout << "YOU WON" << endl;
if(score < 100)
cout << "YOU LOST" << endl;
cout << score << endl;
quit(window);
break;
case GLFW_KEY_M:
blockSpeed+=0.001;
if(blockSpeed>=0.020)
blockSpeed=0.020;
break;
case GLFW_KEY_N:
blockSpeed-=0.001;
if(blockSpeed<=0.005)
blockSpeed=0.005;
break;
case GLFW_KEY_LEFT:
if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL))
{
redx=redx-0.2;
if(redx<-2.5)
redx=-2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL))
{
redx=redx-0.2;
if(redx<-2.5)
redx=-2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_RIGHT_ALT))
{
greenx=greenx-0.2;
if(greenx<-2.5)
greenx=-2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_LEFT_ALT))
{
greenx=greenx-0.2;
if(greenx<-2.5)
greenx=-2.5;
break;
}
else
{
screen_x-=0.2;
if(screen_x<=-4.0)
screen_x=-4.0;
break;
}
case GLFW_KEY_RIGHT:
if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL))
{
redx=redx+0.2;
if(redx>2.5)
redx=2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL))
{
redx=redx+0.2;
if(redx>2.5)
redx=2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_RIGHT_ALT))
{
greenx=greenx+0.2;
if(greenx>2.5)
greenx=2.5;
break;
}
else if(glfwGetKey(window, GLFW_KEY_LEFT_ALT))
{
greenx=greenx+0.2;
if(greenx>2.5)
greenx=2.5;
break;
}
else
{
screen_x+=0.2;
if(screen_x>=3.0)
screen_x=3.0;
break;
}
case GLFW_KEY_S:
turrety=turrety+0.2;
if(turrety>3.5)
turrety=3.5;
break;
case GLFW_KEY_F:
turrety=turrety-0.2;
if(turrety<-3.5)
turrety=-3.5;
break;
case GLFW_KEY_A:
turret_rectangle_rotation+=5;
if(turret_rectangle_rotation>=90)
turret_rectangle_rotation=90;
break;
case GLFW_KEY_D:
turret_rectangle_rotation-=5;
if(turret_rectangle_rotation<=-90)
turret_rectangle_rotation=-90;
break;
case GLFW_KEY_UP:
zoom+=0.1;
if(zoom>=2.0)
zoom=2.0;
break;
case GLFW_KEY_DOWN:
zoom-=0.1;
if(zoom<=1.0)
zoom=1.0;
break;
default:
break;
}
}
}
/* Executed for character input (like in text boxes) */
void keyboardChar (GLFWwindow* window, unsigned int key)
{
switch (key) {
case 'Q':
case 'q':
if(score > 100)
cout << "YOU WON" << endl;
if(score < 100)
cout << "YOU LOST" << endl;
cout << score << endl;
quit(window);
break;
default:
break;
}
}
pair<float, float> toRelativeCoords (float screen_x, float screen_y) {
int width, height;
glfwGetWindowSize(window, &width, &height);
return make_pair((8.0 * screen_x / width) - 4, 4 - (8.0 * screen_y / height));
}
void cursorMove (GLFWwindow *window, double scree_x, double scree_y) {
// Convert screen coords to world coords
pair<float, float> p = toRelativeCoords((float) scree_x, (float) scree_y);
float x = p.first, y = p.second;
// Update global cursor position variables
CURSOR_X = x, CURSOR_Y = y;
// Orient the cannon appropriately
turret_angle = atan( (y - turrety) / (x + 4)) * 180.0f / M_PI;
if (laserFlag==0) laser_rotation = turret_angle;
// Handle clicking on the buckets and cannon
if (redbucket_clicked)
redx = CURSOR_X;
if (greenbucket_clicked)
greenx = CURSOR_X;
if (turret_clicked== true )
turrety = CURSOR_Y;
}
/* Executed when window is resized to 'width' and 'height' */
/* Modify the bounds of the screen here in glm::ortho or Field of View in glm::Perspective */
void reshapeWindow (GLFWwindow* window, int width, int height)
{
int fbwidth=width, fbheight=height;
/* With Retina display on Mac OS X, GLFW's FramebufferSize
is different from WindowSize */
glfwGetFramebufferSize(window, &fbwidth, &fbheight);
GLfloat fov = 90.0f;
// sets the viewport of openGL renderer
glViewport (0, 0, (GLsizei) fbwidth, (GLsizei) fbheight);
// set the projection matrix as perspective
/* glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (fov, (GLfloat) fbwidth / (GLfloat) fbheight, 0.1, 500.0); */
// Store the projection matrix in a variable for future use
// Perspective projection for 3D views
// Matrices.projection = glm::perspective (fov, (GLfloat) fbwidth / (GLfloat) fbheight, 0.1f, 500.0f);
// Ortho projection for 2D views
Matrices.projection = glm::ortho(-4.0f, 4.0f, -4.0f, 4.0f, 0.1f, 500.0f);
}
void scroll_callback (GLFWwindow *window, double xoffset, double yoffset) {
if (yoffset <= 0 && zoom <= 1) return;
else {
zoom += yoffset * SCREEN_ZOOM_STEP;
//updateZoomAndPan();
screen_left=(screen_x-4.0)/zoom;
screen_right=(4.0+screen_x)/zoom;
screen_top=-(screen_y-4.0)/zoom;
screen_bottom=-(4+screen_y)/zoom;
}
}
void mouseButton (GLFWwindow* window, int button, int action, int mods)
{
switch (button) {
case GLFW_MOUSE_BUTTON_LEFT:
//if (action == GLFW_RELEASE)
// triangle_rot_dir *= -1;
if (action == GLFW_PRESS)
{
float temp_angle= atan((CURSOR_Y-turrety)/(CURSOR_X+4))*180.0f/M_PI;
if(laserFlag==0)
laserFlag=1;
turret_rectangle_rotation=temp_angle;
laser_rotation=temp_angle;
laser_x=-4;
laser_y=turrety;
}
break;
case GLFW_MOUSE_BUTTON_RIGHT:
if (action == GLFW_PRESS) {
// Check if buckets are being clicked
if (CURSOR_X <= redx + 1.6 / 2.0 && CURSOR_X >= redx - 1.6 / 2.0 && CURSOR_Y >= -4.0 && CURSOR_Y <= -(4.0 - 0.8))
redbucket_clicked = true;
else if (CURSOR_X <= greenx + 1.6 / 2.0 && CURSOR_X >= greenx - 1.6 / 2.0 && CURSOR_Y >= -4.0 && CURSOR_Y <= -(4.0 - 0.8))
greenbucket_clicked = true;
// Check if cannon is being clicked
else if (CURSOR_X >= -4 && CURSOR_X <= 0.5 && CURSOR_Y >= turrety - 0.3 && CURSOR_Y <= turrety + 0.3)
turret_clicked = true;
}
if (action == GLFW_RELEASE) {
// rectangle_rot_dir *= -1;
// Reset everything clickable as unclicked
redbucket_clicked = false;
greenbucket_clicked = false;
turret_clicked = false;
}
break;
default:
break;
}
}
//VAO *triangle, *rectangle;
VAO *triangle, *red_rectangle, *green_rectangle, *turret_rectangle,*mirror1, *mirror2, *mirror3, *mirror4, *rectangle[3],*laser;
//vector <VAO*> rectangle;
// Creates the triangle object used in this sample code
//void createTriangle ()
//{
/* ONLY vertices between the bounds specified in glm::ortho will be visible on screen */
/* Define vertex array as used in glBegin (GL_TRIANGLES) */
// static const GLfloat vertex_buffer_data [] = {
// 0, 1,0, // vertex 0
// -1,-1,0, // vertex 1
// 1,-1,0, // vertex 2
// };
// static const GLfloat color_buffer_data [] = {
// 1,0,0, // color 0
// 0,1,0, // color 1
// 0,0,1, // color 2
// };
// create3DObject creates and returns a handle to a VAO that can be used later
// triangle = create3DObject(GL_TRIANGLES, 3, vertex_buffer_data, color_buffer_data, GL_LINE);
//}
// Creates the rectangle object used in this sample code
void createRedRectangle ()
{
// GL3 accepts only Triangles. Quads are not supported
static const GLfloat vertex_buffer_data [] = {
-0.8,-0.25,0, // vertex 1
0.8,-0.25,0, // vertex 2
0.8, 0.25,0, // vertex 3
0.8, 0.25,0, // vertex 3
-0.8, 0.25,0, // vertex 4
-0.8,-0.25,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
1,0,0, // color 1
1,0,0, // color 2
1,0,0, // color 3
1,0,0, // color 3
1,0,0, // color 4
1,0,0, // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
red_rectangle = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void createLaser()
{
static const GLfloat vertex_buffer_data [] = {
-0.2,-0.01,0, // vertex 1
0.2,-0.01,0, // vertex 2
0.2, 0.01,0, // vertex 3
0.2, 0.01,0, // vertex 3
-0.2, 0.01,0, // vertex 4
-0.2,-0.01,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
0.6,0.2,0.9, // color 1
0.6,0.2,0.9,
0.6,0.2,0.9,
0.6,0.2,0.9,
0.6,0.2,0.9,
0.6,0.2,0.9,
};
// create3DObject creates and returns a handle to a VAO that can be used later
laser = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void createMirror1 ()
{
// GL3 accepts only Triangles. Quads are not supported
static const GLfloat vertex_buffer_data [] = {
-0.4,-0.05,0, // vertex 1
0.4,-0.05,0, // vertex 2
0.4, 0.05,0, // vertex 3
0.4, 0.05,0, // vertex 3
-0.4, 0.05,0, // vertex 4
-0.4,-0.05,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
0,0,0, // color 1
0,0,0, // color 2
0,0,0, // color 3
0,0,0, // color 3
0,0,0, // color 4
0,0,0, // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
mirror1 = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void createMirror2 ()
{
// GL3 accepts only Triangles. Quads are not supported
static const GLfloat vertex_buffer_data [] = {
-0.4,-0.05,0, // vertex 1
0.4,-0.05,0, // vertex 2
0.4, 0.05,0, // vertex 3
0.4, 0.05,0, // vertex 3
-0.4, 0.05,0, // vertex 4
-0.4,-0.05,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
0,0,0, // color 1
0,0,0, // color 2
0,0,0, // color 3
0,0,0, // color 3
0,0,0, // color 4
0,0,0, // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
mirror2 = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void createMirror3 ()
{
// GL3 accepts only Triangles. Quads are not supported
static const GLfloat vertex_buffer_data [] = {
-0.4,-0.05,0, // vertex 1
0.4,-0.05,0, // vertex 2
0.4, 0.05,0, // vertex 3
0.4, 0.05,0, // vertex 3
-0.4, 0.05,0, // vertex 4
-0.4,-0.05,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
0,0,0, // color 1
0,0,0, // color 2
0,0,0, // color 3
0,0,0, // color 3
0,0,0, // color 4
0,0,0, // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
mirror3 = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void createMirror4 ()
{
// GL3 accepts only Triangles. Quads are not supported
static const GLfloat vertex_buffer_data [] = {
-0.4,-0.05,0, // vertex 1
0.4,-0.05,0, // vertex 2
0.4, 0.05,0, // vertex 3
0.4, 0.05,0, // vertex 3
-0.4, 0.05,0, // vertex 4
-0.4,-0.05,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
0,0,0, // color 1
0,0,0, // color 2
0,0,0, // color 3
0,0,0, // color 3
0,0,0, // color 4
0,0,0, // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
mirror4 = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void createGreenRectangle ()
{
// GL3 accepts only Triangles. Quads are not supported
static const GLfloat vertex_buffer_data [] = {
-0.8,-0.25,0, // vertex 1
0.8,-0.25,0, // vertex 2
0.8, 0.25,0, // vertex 3
0.8, 0.25,0, // vertex 3
-0.8, 0.25,0, // vertex 4
-0.8,-0.25,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
0,1,0, // color 1
0,1,0, // color 2
0,1,0, // color 3
0,1,0, // color 3
0,1,0, // color 4
0,1,0 // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
green_rectangle = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void CreateTurret()
{
static const GLfloat vertex_buffer_data [] = {
-0.3,0.05,0, // vertex 1
0.3,0.05,0, // vertex 2
0.3,-0.05,0, // vertex 3
0.3,-0.05,0, // vertex 3
-0.3,-0.05,0, // vertex 4
-0.3,0.05,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
0.23,0.23,1.23, // color 1
0.23,0.23,1.23, // color 2
0.23,0.23,1.23, // color 3
0.23,0.23,1.23, // color 1
0.23,0.23,1.23, // color 2
0.23,0.23,1.23,//color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
turret_rectangle = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void CreateRedBlock()
{
static const GLfloat vertex_buffer_data [] = {
-0.3,0.2,0, // vertex 1
0.3,0.2,0, // vertex 2
0.3,-0.2,0, // vertex 3
0.3,-0.2,0, // vertex 3
-0.3,-0.2,0, // vertex 4
-0.3,0.2,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
1,0,0, // color 1
1,0,0, // color 1
1,0,0, // color 1
1,0,0, // color 1
1,0,0, // color 1
1,0,0, // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
rectangle[0] = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void createGreenBlock()
{
static const GLfloat vertex_buffer_data [] = {
-0.3,0.2,0, // vertex 1
0.3,0.2,0, // vertex 2
0.3,-0.2,0, // vertex 3
0.3,-0.2,0, // vertex 3
-0.3,-0.2,0, // vertex 4
-0.3,0.2,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
0,1,0, // color 1
0,1,0, // color 1
0,1,0, // color 1
0,1,0, // color 1
0,1,0, // color 1
0,1,0, // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
rectangle[1] = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
void createBlackBlock()
{
static const GLfloat vertex_buffer_data [] = {
-0.3,0.2,0, // vertex 1
0.3,0.2,0, // vertex 2
0.3,-0.2,0, // vertex 3
0.3,-0.2,0, // vertex 3
-0.3,-0.2,0, // vertex 4
-0.3,0.2,0, // vertex 1
};
static const GLfloat color_buffer_data [] = {
0,0,0, // color 1
0,0,0, // color 1
0,0,0, // color 1
0,0,0, // color 1
0,0,0, // color 1
0,0,0, // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
rectangle[2] = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
//float camera_rotation_angle = 90;
//float turret_rectangle_rotation = 0,rectangle_rotation=0;
//float triangle_rotation = 0;
/* Render the scene with openGL */