-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample_GL3_2D.cpp
575 lines (465 loc) · 18.4 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
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace std;
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;
int rectangle_xpos = 0;
int flag = 0;
/* Executed when a regular key is pressed/released/held-down */
/* Prefered for Keyboard events */
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_P:
triangle_rot_status = !triangle_rot_status;
break;
case GLFW_KEY_X:
// do something ..
break;
case GLFW_KEY_D:
rectangle_xpos += 1;
break;
case GLFW_KEY_A:
rectangle_xpos -= 1;
break;
default:
break;
}
}
else if (action == GLFW_PRESS) {
switch (key) {
case GLFW_KEY_ESCAPE:
quit(window);
break;
default:
break;
}
}
}
/* Executed for character input (like in text boxes) */
void keyboardChar (GLFWwindow* window, unsigned int key)
{
switch (key) {
case 'Q':
case 'q':
quit(window);
break;
default:
break;
}
}
/* Executed when a mouse button is pressed/released */
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;
break;
case GLFW_MOUSE_BUTTON_RIGHT:
if (action == GLFW_RELEASE) {
rectangle_rot_dir *= -1;
}
break;
default:
break;
}
}
/* 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);
}
VAO *triangle, *rectangle, *trapezium;
// 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);
}
void createTrapezium()
{
static const GLfloat vertex_buffer_data [] = {
-1.2,-1,0, // vertex 1
1.2,-1,0, // vertex 2
0.6, 1,0, // vertex 3
0.6, 1,0, // vertex 3
-0.6, 1,0, // vertex 4
-1.2,-1,0 // vertex 1
};
static const GLfloat color_buffer_data [] = {
1,0,0, // color 1
0,0,1, // color 2
0,1,0, // color 3
0,1,0, // color 3
0.3,0.3,0.3, // color 4
1,0,0 // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
trapezium = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
// Creates the rectangle object used in this sample code
void createRectangle ()
{
// GL3 accepts only Triangles. Quads are not supported
static const GLfloat vertex_buffer_data [] = {
-1.2,-1,0, // vertex 1
1.2,-1,0, // vertex 2
0.6, 1,0, // vertex 3
0.6, 1,0, // vertex 3
-0.6, 1,0, // vertex 4
-1.2,-1,0 // vertex 1
};
static const GLfloat color_buffer_data [] = {
1,0,0, // color 1
0,0,1, // color 2
0,1,0, // color 3
0,1,0, // color 3
0.3,0.3,0.3, // color 4
1,0,0 // color 1
};
// create3DObject creates and returns a handle to a VAO that can be used later
rectangle = create3DObject(GL_TRIANGLES, 6, vertex_buffer_data, color_buffer_data, GL_FILL);
}
float camera_rotation_angle = 90;
float rectangle_rotation = 0;
float triangle_rotation = 0;
/* Render the scene with openGL */
/* Edit this function according to your assignment */
void draw ()
{
// clear the color and depth in the frame buffer
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// use the loaded shader program
// Don't change unless you know what you are doing
glUseProgram (programID);
// Eye - Location of camera. Don't change unless you are sure!!
glm::vec3 eye ( 5*cos(camera_rotation_angle*M_PI/180.0f), 0, 5*sin(camera_rotation_angle*M_PI/180.0f) );
// Target - Where is the camera looking at. Don't change unless you are sure!!
glm::vec3 target (0, 0, 0);
// Up - Up vector defines tilt of camera. Don't change unless you are sure!!
glm::vec3 up (0, 1, 0);
// Compute Camera matrix (view)
// Matrices.view = glm::lookAt( eye, target, up ); // Rotating Camera for 3D
// Don't change unless you are sure!!
Matrices.view = glm::lookAt(glm::vec3(0,0,3), glm::vec3(0,0,0), glm::vec3(0,1,0)); // Fixed camera for 2D (ortho) in XY plane
// Compute ViewProject matrix as view/camera might not be changed for this frame (basic scenario)
// Don't change unless you are sure!!
glm::mat4 VP = Matrices.projection * Matrices.view;
// Send our transformation to the currently bound shader, in the "MVP" uniform
// For each model you render, since the MVP will be different (at least the M part)
// Don't change unless you are sure!!
glm::mat4 MVP; // MVP = Projection * View * Model
// Load identity to model matrix
Matrices.model = glm::mat4(1.0f);
/* Render your scene */
glm::mat4 translateTriangle = glm::translate (glm::vec3(-2.0f, 0.0f, 0.0f)); // glTranslatef
glm::mat4 rotateTriangle = glm::rotate((float)(triangle_rotation*M_PI/180.0f), glm::vec3(0,0,1)); // rotate about vector (1,0,0)
glm::mat4 triangleTransform = translateTriangle * rotateTriangle;
Matrices.model *= triangleTransform;
MVP = VP * Matrices.model; // MVP = p * V * M
// Don't change unless you are sure!!
glUniformMatrix4fv(Matrices.MatrixID, 1, GL_FALSE, &MVP[0][0]);
// draw3DObject draws the VAO given to it using current MVP matrix
draw3DObject(triangle);
// Pop matrix to undo transformations till last push matrix instead of recomputing model matrix
// glPopMatrix ();
Matrices.model = glm::mat4(1.0f);
glm::mat4 translateRectangle = glm::translate (glm::vec3(2+rectangle_xpos, 0, 0)); // glTranslatef
glm::mat4 rotateRectangle = glm::rotate((float)(rectangle_rotation*M_PI/180.0f), glm::vec3(0,0,1)); // rotate about vector (-1,1,1)
Matrices.model *= (translateRectangle * rotateRectangle);
MVP = VP * Matrices.model;
glUniformMatrix4fv(Matrices.MatrixID, 1, GL_FALSE, &MVP[0][0]);
// draw3DObject draws the VAO given to it using current MVP matrix
draw3DObject(rectangle);
// Increment angles
float increments = 1;
//camera_rotation_angle++; // Simulating camera rotation
triangle_rotation = triangle_rotation + increments*triangle_rot_dir*triangle_rot_status;
rectangle_rotation = rectangle_rotation + increments*rectangle_rot_dir*rectangle_rot_status;
// rectangle_translate =
}
/* Initialise glfw window, I/O callbacks and the renderer to use */
/* Nothing to Edit here */
GLFWwindow* initGLFW (int width, int height)
{
GLFWwindow* window; // window desciptor/handle
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
// exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(width, height, "Sample OpenGL 3.3 Application", NULL, NULL);
if (!window) {
glfwTerminate();
// exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glfwSwapInterval( 1 );
/* --- register callbacks with GLFW --- */
/* Register function to handle window resizes */
/* With Retina display on Mac OS X GLFW's FramebufferSize
is different from WindowSize */
glfwSetFramebufferSizeCallback(window, reshapeWindow);
glfwSetWindowSizeCallback(window, reshapeWindow);
/* Register function to handle window close */
glfwSetWindowCloseCallback(window, quit);
/* Register function to handle keyboard input */
glfwSetKeyCallback(window, keyboard); // general keyboard input
glfwSetCharCallback(window, keyboardChar); // simpler specific character handling
/* Register function to handle mouse click */
glfwSetMouseButtonCallback(window, mouseButton); // mouse button clicks
return window;
}
/* Initialize the OpenGL rendering properties */
/* Add all the models to be created here */
void initGL (GLFWwindow* window, int width, int height)
{
/* Objects should be created before any other gl function and shaders */
// Create the models
createTriangle (); // Generate the VAO, VBOs, vertices data & copy into the array buffer
createRectangle ();
// Create and compile our GLSL program from the shaders
programID = LoadShaders( "Sample_GL.vert", "Sample_GL.frag" );
// Get a handle for our "MVP" uniform
Matrices.MatrixID = glGetUniformLocation(programID, "MVP");
reshapeWindow (window, width, height);
// Background color of the scene
glClearColor (0.3f, 0.3f, 0.3f, 0.0f); // R, G, B, A
glClearDepth (1.0f);
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LEQUAL);
cout << "VENDOR: " << glGetString(GL_VENDOR) << endl;
cout << "RENDERER: " << glGetString(GL_RENDERER) << endl;
cout << "VERSION: " << glGetString(GL_VERSION) << endl;
cout << "GLSL: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl;
}
int main (int argc, char** argv)
{
int width = 600;
int height = 600;
GLFWwindow* window = initGLFW(width, height);
initGL (window, width, height);
double last_update_time = glfwGetTime(), current_time;
/* Draw in loop */
while (!glfwWindowShouldClose(window)) {
// OpenGL Draw commands
draw();
// Swap Frame Buffer in double buffering
glfwSwapBuffers(window);
// Poll for Keyboard and mouse events
glfwPollEvents();
// Control based on time (Time based transformation like 5 degrees rotation every 0.5s)
current_time = glfwGetTime(); // Time in seconds
if ((current_time - last_update_time) >= 0.5) { // atleast 0.5s elapsed since last frame
// do something every 0.5 seconds ..
last_update_time = current_time;
}
}
glfwTerminate();
// exit(EXIT_SUCCESS);
}