Skip to content

Commit f42d7aa

Browse files
committed
Ex 3,4,5,6 documentation
1 parent b074bc7 commit f42d7aa

5 files changed

+87
-48
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ This repository contains examples of how to use OpenGL with C++17. It is made fo
1313
| ---- | ----------- | ------ |
1414
| Empty Window | [ex01_empty_window.cpp](source/examples/ex01_empty_window.cpp) | :white_check_mark: |
1515
| Shader Introduction | [ex02_shader_introduction.cpp](source/examples/ex02_shader_introduction.cpp) | :white_check_mark: |
16-
| Uniforms | [ex03_uniforms.cpp](source/examples/ex03_uniforms.cpp) | |
17-
| Varyings | [ex04_varyings.cpp](source/examples/ex04_varyings.cpp) | |
18-
| Attributes | [ex05_attributes.cpp](source/examples/ex05_attributes.cpp) | |
19-
| Multiple Attributes | [ex06_multiple_attributes.cpp](source/examples/ex06_multiple_attributes.cpp) | |
16+
| Uniforms | [ex03_uniforms.cpp](source/examples/ex03_uniforms.cpp) | :white_check_mark: |
17+
| Varyings | [ex04_varyings.cpp](source/examples/ex04_varyings.cpp) | :white_check_mark: |
18+
| Attributes | [ex05_attributes.cpp](source/examples/ex05_attributes.cpp) | :white_check_mark: |
19+
| Multiple Attributes | [ex06_multiple_attributes.cpp](source/examples/ex06_multiple_attributes.cpp) | :white_check_mark: |
2020
| Interleaved Attributes | [ex07_interleaved_attributes.cpp](source/examples/ex07_interleaved_attributes.cpp) | |
2121
| Elements | [ex08_elements.cpp](source/examples/ex08_elements.cpp) | |
2222
| Stream | [ex09_stream.cpp](source/examples/ex09_stream.cpp) | |

source/examples/ex03_uniforms.cpp

+49-26
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,82 @@
11
#include <application.hpp>
22
#include <shader.hpp>
33

4-
4+
// The inherited class from "Application" to this example.
55
class UniformsApplication : public our::Application {
66

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.
99

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.
1313
bool vibrate = false, flicker = false;
1414

15+
// We need a window with title: "Uniforms", size: {1280, 720}, not full screen.
1516
our::WindowConfiguration getWindowConfiguration() override {
1617
return { "Uniforms", {1280, 720}, false };
1718
}
1819

20+
// Remember called once before game loop.
1921
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.
2426

25-
glGenVertexArrays(1, &vertex_array);
27+
glGenVertexArrays(1, &vertex_array); // Create a vertex array and save address in uint vertex array.
2628

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.
2830
}
2931

32+
// Remember called in game loop phase every frame.
3033
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.
4055

4156
GLuint time_uniform_location = glGetUniformLocation(program, "time");
4257
glUniform1f(time_uniform_location, glfwGetTime());
58+
4359
GLuint vibrate_uniform_location = glGetUniformLocation(program, "vibrate");
4460
glUniform1i(vibrate_uniform_location, vibrate);
61+
4562
GLuint flicker_uniform_location = glGetUniformLocation(program, "flicker");
4663
glUniform1i(flicker_uniform_location, flicker);
4764

4865

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).
5275
}
5376

5477
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.
5780
}
5881

5982
void onImmediateGui(ImGuiIO &io) override {

source/examples/ex04_varyings.cpp

+27-17
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
#include <application.hpp>
22
#include <shader.hpp>
33

4-
4+
// The inherited class from "Application" to this example.
55
class VaryingsApplication : public our::Application {
66

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.
99

10+
// We need a window with title: "Varyings", size: {1280, 720}, not full screen.
1011
our::WindowConfiguration getWindowConfiguration() override {
1112
return { "Varyings", {1280, 720}, false };
1213
}
1314

15+
// Remember called once before game loop.
1416
void onInitialize() override {
15-
program.create();
16-
program.attach("assets/shaders/ex04_varyings/colored_triangle.vert", GL_VERTEX_SHADER);
17-
program.attach("assets/shaders/ex04_varyings/varying_color.frag", GL_FRAGMENT_SHADER);
18-
program.link();
17+
program.create(); // Create a "Program".
18+
program.attach("assets/shaders/ex04_varyings/colored_triangle.vert", GL_VERTEX_SHADER); // Read vertex shader file, compile it, and check for errors.
19+
program.attach("assets/shaders/ex04_varyings/varying_color.frag", GL_FRAGMENT_SHADER); // Read fragment shader file, compile it, and check for errors.
20+
program.link(); // link to shaders together in the program.
1921

20-
glGenVertexArrays(1, &vertex_array);
22+
glGenVertexArrays(1, &vertex_array); // Create a vertex array and save address in uint vertex array.
2123

22-
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
24+
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set the screen clear color to black.
2325
}
2426

27+
// Remember called in game loop phase every frame.
2528
void onDraw(double deltaTime) override {
26-
glClear(GL_COLOR_BUFFER_BIT);
27-
glUseProgram(program);
28-
glBindVertexArray(vertex_array);
29-
glDrawArrays(GL_TRIANGLES, 0, 3);
30-
glBindVertexArray(0);
31-
}
29+
30+
glClear(GL_COLOR_BUFFER_BIT); // Clear the frame.
31+
glUseProgram(program); // Set "program" as the GPU drawing program.
32+
glBindVertexArray(vertex_array); // Bind vertext array (select the buffer to be used by program).
33+
34+
glDrawArrays(GL_TRIANGLES, 0, 3); // Use the buffer and selected program to draw.
35+
// 1st parameter: Specifies what kind of primitives to render.
36+
// 2nd parameter: Specifies the starting index in the enabled arrays.
37+
// 3rd parameter: Specifies the number of indices to be rendered.
38+
// 3rd parameter is 3 as we draw 1 triangles (each with 3 vertices).
39+
40+
glBindVertexArray(0); // Unbind the array(buffer).
41+
}
3242

3343
void onDestroy() override {
34-
program.destroy();
35-
glDeleteVertexArrays(1, &vertex_array);
44+
program.destroy(); // Delete the program.
45+
glDeleteVertexArrays(1, &vertex_array); // Free allocated memory.
3646
}
3747

3848
};

source/examples/ex05_attributes.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ class AttributesApplication : public our::Application {
4040
};
4141
#endif
4242

43-
glBufferData(GL_ARRAY_BUFFER, 3*3*sizeof(float), positions, GL_STATIC_DRAW);
43+
glBufferData(GL_ARRAY_BUFFER, 3*3*sizeof(float), positions, GL_STATIC_DRAW); // function specifically targeted to copy user-defined data into the currently bound buffer.
44+
// Its first argument is the type of the buffer we want to copy data into:
45+
// the vertex buffer object currently bound to the GL_ARRAY_BUFFER target.
46+
// The second argument specifies the size of the data (in bytes) we want to pass to the buffer;
47+
// a simple sizeof of the vertex data suffices. The third parameter is the actual data we want to send.
4448

4549
GLuint position_attribute_location = glGetAttribLocation(program, "position");
4650
std::cout << "Position Attribute Location: " << position_attribute_location << std:: endl;

source/examples/ex06_multiple_attributes.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class AttributesApplication : public our::Application {
3232

3333
glBufferData(GL_ARRAY_BUFFER, 3*3*sizeof(float), positions, GL_STATIC_DRAW);
3434

35+
// Setting attribute for (0) the postion.
3536
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, (void*)0);
3637
glEnableVertexAttribArray(0);
3738

@@ -46,6 +47,7 @@ class AttributesApplication : public our::Application {
4647

4748
glBufferData(GL_ARRAY_BUFFER, 3*4*sizeof(glm::uint8), colors, GL_STATIC_DRAW);
4849

50+
// Setting attribute for (1) the color, same as before replicating the process changing the size to 4*sizeof(glm::uint8)
4951
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, true, 0, (void*)0);
5052
glEnableVertexAttribArray(1);
5153

0 commit comments

Comments
 (0)