|
1 | 1 | #include <application.hpp>
|
2 | 2 | #include <shader.hpp>
|
3 | 3 |
|
4 |
| - |
| 4 | +// The inherited class from "Application" to this example. |
5 | 5 | class UniformsApplication : public our::Application {
|
6 | 6 |
|
7 |
| - our::ShaderProgram program; |
8 |
| - GLuint vertex_array = 0; |
| 7 | + our::ShaderProgram program; // Instance of our shader class. |
| 8 | + GLuint vertex_array = 0; // The address of the vertex_array saved on the VRAM. |
9 | 9 |
|
10 |
| - glm::vec2 scale = glm::vec2(1,1); |
11 |
| - glm::vec2 translation = glm::vec2(0,0); |
12 |
| - glm::vec3 color = glm::vec3(1, 0, 0); |
| 10 | + glm::vec2 scale = glm::vec2(1,1); // This vector of size 2 represents scale(size). |
| 11 | + glm::vec2 translation = glm::vec2(0,0); // This vector of size 2 represents translation(position). |
| 12 | + glm::vec3 color = glm::vec3(1, 0, 0); // This vector of size 3 represents color. |
13 | 13 | bool vibrate = false, flicker = false;
|
14 | 14 |
|
| 15 | + // We need a window with title: "Uniforms", size: {1280, 720}, not full screen. |
15 | 16 | our::WindowConfiguration getWindowConfiguration() override {
|
16 | 17 | return { "Uniforms", {1280, 720}, false };
|
17 | 18 | }
|
18 | 19 |
|
| 20 | + // Remember called once before game loop. |
19 | 21 | void onInitialize() override {
|
20 |
| - program.create(); |
21 |
| - program.attach("assets/shaders/ex03_uniforms/quad.vert", GL_VERTEX_SHADER); |
22 |
| - program.attach("assets/shaders/ex03_uniforms/uniform_color.frag", GL_FRAGMENT_SHADER); |
23 |
| - program.link(); |
| 22 | + program.create(); // Create a "Program". |
| 23 | + program.attach("assets/shaders/ex03_uniforms/quad.vert", GL_VERTEX_SHADER); // Read vertex shader file, compile it, and check for errors. |
| 24 | + program.attach("assets/shaders/ex03_uniforms/uniform_color.frag", GL_FRAGMENT_SHADER); // Read fragment shader file, compile it, and check for errors. |
| 25 | + program.link(); // link to shaders together in the program. |
24 | 26 |
|
25 |
| - glGenVertexArrays(1, &vertex_array); |
| 27 | + glGenVertexArrays(1, &vertex_array); // Create a vertex array and save address in uint vertex array. |
26 | 28 |
|
27 |
| - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 29 | + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set the screen clear color to black. |
28 | 30 | }
|
29 | 31 |
|
| 32 | + // Remember called in game loop phase every frame. |
30 | 33 | void onDraw(double deltaTime) override {
|
31 |
| - glClear(GL_COLOR_BUFFER_BIT); |
32 |
| - glUseProgram(program); |
33 |
| - |
34 |
| - GLuint scale_uniform_location = glGetUniformLocation(program, "scale"); |
35 |
| - glUniform2f(scale_uniform_location, scale.x, scale.y); |
36 |
| - GLuint translation_uniform_location = glGetUniformLocation(program, "translation"); |
37 |
| - glUniform2f(translation_uniform_location, translation.x, translation.y); |
38 |
| - GLuint color_uniform_location = glGetUniformLocation(program, "color"); |
39 |
| - glUniform3f(color_uniform_location, color.r, color.g, color.b); |
| 34 | + |
| 35 | + glClear(GL_COLOR_BUFFER_BIT); // Clear the frame. |
| 36 | + glUseProgram(program); // Set "program" as the GPU drawing program. |
| 37 | + |
| 38 | + |
| 39 | + // Set the GPU parameters every frame as the position, scale, color are not permenant values, |
| 40 | + // they change every frame, unlike the vertex data for example that needs to be set once before |
| 41 | + // application starts. |
| 42 | + GLuint scale_uniform_location = glGetUniformLocation(program, "scale"); // Get the address to the program variable "scale" found |
| 43 | + // in vertex shader "quad.vert file". |
| 44 | + glUniform2f(scale_uniform_location, scale.x, scale.y); // Set the value of vec2 "scale_uniform_location" with scale data. |
| 45 | + |
| 46 | + |
| 47 | + GLuint translation_uniform_location = glGetUniformLocation(program, "translation"); // Get the address to the program variable "translation" found |
| 48 | + // in vertex shader "quad.vert file". |
| 49 | + glUniform2f(translation_uniform_location, translation.x, translation.y); // Set the value of vec2 "translation_uniform_location" with translation data. |
| 50 | + |
| 51 | + |
| 52 | + GLuint color_uniform_location = glGetUniformLocation(program, "color"); // Get the address to the program variable "color" found |
| 53 | + // in fragment shader "uniform_color.frag file". |
| 54 | + glUniform3f(color_uniform_location, color.r, color.g, color.b); // Set the value of vec3 "color_uniform_location" with color data. |
40 | 55 |
|
41 | 56 | GLuint time_uniform_location = glGetUniformLocation(program, "time");
|
42 | 57 | glUniform1f(time_uniform_location, glfwGetTime());
|
| 58 | + |
43 | 59 | GLuint vibrate_uniform_location = glGetUniformLocation(program, "vibrate");
|
44 | 60 | glUniform1i(vibrate_uniform_location, vibrate);
|
| 61 | + |
45 | 62 | GLuint flicker_uniform_location = glGetUniformLocation(program, "flicker");
|
46 | 63 | glUniform1i(flicker_uniform_location, flicker);
|
47 | 64 |
|
48 | 65 |
|
49 |
| - glBindVertexArray(vertex_array); |
50 |
| - glDrawArrays(GL_TRIANGLES, 0, 6); |
51 |
| - glBindVertexArray(0); |
| 66 | + glBindVertexArray(vertex_array); // Bind vertext array (select the buffer to be used by program). |
| 67 | + |
| 68 | + glDrawArrays(GL_TRIANGLES, 0, 6); // Use the buffer and selected program to draw. |
| 69 | + // 1st parameter: Specifies what kind of primitives to render. |
| 70 | + // 2nd parameter: Specifies the starting index in the enabled arrays. |
| 71 | + // 3rd parameter: Specifies the number of indices to be rendered. |
| 72 | + // 3rd parameter is 6 as we draw 2 triangles (each with 3 vertices). |
| 73 | + |
| 74 | + glBindVertexArray(0); // Unbind the array(buffer). |
52 | 75 | }
|
53 | 76 |
|
54 | 77 | void onDestroy() override {
|
55 |
| - program.destroy(); |
56 |
| - glDeleteVertexArrays(1, &vertex_array); |
| 78 | + program.destroy(); // Delete the program. |
| 79 | + glDeleteVertexArrays(1, &vertex_array); // Free allocated memory. |
57 | 80 | }
|
58 | 81 |
|
59 | 82 | void onImmediateGui(ImGuiIO &io) override {
|
|
0 commit comments