Skip to content

Commit 6ec7e54

Browse files
committed
Tutorial #3 - Visualización 3D
Uso de matrices para visualizar un cubo 3D
1 parent 0e777a0 commit 6ec7e54

10 files changed

+185
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,5 @@ $RECYCLE.BIN/
7373

7474
# Windows shortcuts
7575
*.lnk
76+
77+
BUILDS/

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ set(GRAPHIC_LIBS ${OPENGL_gl_LIBRARY} ${GLEW_LIBRARIES} ${GLFW3_LIBRARY})
1919
add_subdirectory(Tutorial-00)
2020
add_subdirectory(Tutorial-01)
2121
add_subdirectory(Tutorial-02)
22+
add_subdirectory(Tutorial-03)

Common/OpenGLShader.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ class OpenGLShader {
160160
glUseProgram(program);
161161
}
162162

163+
void unUse() {
164+
glUseProgram(0);
165+
}
166+
163167
private:
164168
GLuint program;
165169
};

Common/OpenGLWindow.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class OpenGLWindow {
1818
{ }
1919

2020
bool init(string title, int width = 1024, int height = 720, bool full_screen = false) {
21+
aspect_ratio = static_cast<float>(width) / static_cast<float>(height);
22+
2123
glfwSetErrorCallback(error_callback);
2224

2325
if (!glfwInit()) return false;

Tutorial-01/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ file(COPY simple.vertex_shader simple.fragment_shader DESTINATION shaders)
33
add_executable( Tutorial-01 shader-opengl.cpp
44
shaders/simple.vertex_shader
55
shaders/simple.fragment_shader
6-
../common/openglwindow.hpp )
6+
../common/openglwindow.hpp
7+
../common/openglshader.hpp )
78

89
target_link_libraries( Tutorial-01 ${GRAPHIC_LIBS} )

Tutorial-02/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ file(COPY simple.vertex_shader simple.fragment_shader DESTINATION shaders)
33
add_executable( Tutorial-02 data-opengl.cpp
44
shaders/simple.vertex_shader
55
shaders/simple.fragment_shader
6-
../common/openglwindow.hpp )
6+
../common/openglwindow.hpp
7+
../common/openglshader.hpp )
78

89
target_link_libraries( Tutorial-02 ${GRAPHIC_LIBS} )

Tutorial-03/3D-OpenGL.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#include "OpenGLWindow.hpp"
2+
#include "OpenGLShader.hpp"
3+
4+
#include <glm/gtc/matrix_transform.hpp>
5+
6+
class Tutorial_03 : public OpenGLWindow {
7+
8+
public:
9+
Tutorial_03() : vao{}, buffer{}, index_buffer{}, MatrixID{} { }
10+
11+
private:
12+
void onstart() override {
13+
// crear, compilar y enlasar el Vertex y Fragment Shader
14+
GLuint program = shader_simple.compile("shaders/simple.vertex_shader", "shaders/simple.fragment_shader");
15+
16+
// Obtener el ID del uniform llamado "MVP"
17+
MatrixID = glGetUniformLocation(program, "MVP");
18+
19+
// crear y enlazar un vao
20+
glGenVertexArrays(1, &vao);
21+
glBindVertexArray(vao);
22+
23+
// vetices para generar un cubo 3D
24+
static const float vertex[] =
25+
{
26+
1.0f, -1.0f, -1.0f, 1.0f,
27+
1.0f, -1.0f, 1.0f, 1.0f,
28+
-1.0f, -1.0f, 1.0f, 1.0f,
29+
-1.0f, -1.0f, -1.0f, 1.0f,
30+
1.0f, 1.0f, -1.0f, 1.0f,
31+
1.0f, 1.0f, 1.0f, 1.0f,
32+
-1.0f, 1.0f, 1.0f, 1.0f,
33+
-1.0f, 1.0f, -1.0f, 1.0f
34+
};
35+
36+
// componetes RGBA para los colores de cada vertice
37+
static const float color[] =
38+
{
39+
1.0f, 0.0f, 0.0f, 1.0f,
40+
0.0f, 1.0f, 0.0f, 1.0f,
41+
0.0f, 0.0f, 1.0f, 1.0f,
42+
1.0f, 0.0f, 0.0f, 1.0f,
43+
0.0f, 1.0f, 0.0f, 1.0f,
44+
1.0f, 0.0f, 0.0f, 1.0f,
45+
0.0f, 1.0f, 0.0f, 1.0f,
46+
0.0f, 0.0f, 1.0f, 1.0f
47+
};
48+
49+
// indices usados para unir los vertices que componen el cubo
50+
static const GLushort indices[] =
51+
{
52+
0, 1, 2,
53+
7, 6, 5,
54+
4, 5, 1,
55+
5, 6, 2,
56+
6, 7, 3,
57+
0, 3, 7,
58+
3, 0, 2,
59+
4, 7, 5,
60+
0, 4, 1,
61+
1, 5, 2,
62+
2, 6, 3,
63+
4, 0, 7
64+
};
65+
66+
// generar, almacenar el buffer de indices
67+
glGenBuffers(1, &index_buffer);
68+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
69+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
70+
71+
// generar dos ids para los buffer
72+
glGenBuffers(2, buffer);
73+
74+
// enlazar el buffer de vertices
75+
glBindBuffer(GL_ARRAY_BUFFER, buffer[0]);
76+
// almacenar datos en el buffer de vertices
77+
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
78+
79+
// describir los datos y activar vPosition (location = 0)
80+
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
81+
glEnableVertexAttribArray(0);
82+
83+
// enlazar el buffer de color
84+
glBindBuffer(GL_ARRAY_BUFFER, buffer[1]);
85+
// almacenar datos en el buffer de color
86+
glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW);
87+
88+
// describir los datos y activar vColor (location = 1)
89+
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL);
90+
glEnableVertexAttribArray(1);
91+
}
92+
93+
void onrender(double time) override {
94+
glClear(GL_COLOR_BUFFER_BIT);
95+
glEnable(GL_CULL_FACE);
96+
97+
// Usar el shader
98+
shader_simple.use();
99+
100+
// Matriz de modelo, se aplica una rotacion sobre el eje Y
101+
glm::mat4 Model = glm::rotate(glm::mat4(1.0f), (float)time, glm::vec3(0.0f, 1.0f, 0.0f));
102+
103+
// Matriz de proyeccion y visualizacion
104+
glm::mat4 Projection = glm::perspective(45.0f, aspect_ratio, 0.1f, 100.0f);
105+
glm::mat4 View = glm::lookAt(glm::vec3(4, 3, -3), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
106+
107+
// Crear la matriz total MVP
108+
glm::mat4 MVP = Projection * View * Model;
109+
110+
// Enviar las tranformaciones al shader
111+
// MatrixID es el ID del uniform MVP obtenida por glGetUniformLocation(program, "MVP");
112+
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
113+
114+
// Dibujar cubo usando los indices almacenados en el buffer,
115+
// 1 triangulo = 3 indices, 1 cara = 2 triangulos, 1 cubo = 6 caras.
116+
// 3 * 2 * 6 = 36 indices a dibujar
117+
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
118+
}
119+
120+
// Liberar memoria usada por los buffers
121+
void onstop() override {
122+
glDeleteBuffers(2, buffer);
123+
glDeleteBuffers(1, &index_buffer);
124+
glDeleteVertexArrays(1, &vao);
125+
}
126+
127+
OpenGLShader shader_simple;
128+
GLuint vao, index_buffer;
129+
GLuint buffer[2];
130+
GLuint MatrixID;
131+
};
132+
133+
int main() {
134+
Tutorial_03 win_app;
135+
if (win_app.init("OpenGL Moderno - Visualizar 3D", 1280, 720)) {
136+
win_app.info();
137+
win_app.run();
138+
}
139+
return 0;
140+
}

Tutorial-03/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
file(COPY simple.vertex_shader simple.fragment_shader DESTINATION shaders)
2+
3+
add_executable( Tutorial-03 3d-opengl.cpp
4+
shaders/simple.vertex_shader
5+
shaders/simple.fragment_shader
6+
../common/openglwindow.hpp
7+
../common/openglshader.hpp )
8+
9+
target_link_libraries( Tutorial-03 ${GRAPHIC_LIBS} )

Tutorial-03/simple.fragment_shader

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#version 330 core
2+
3+
in vec4 color;
4+
out vec4 fColor;
5+
6+
void main()
7+
{
8+
fColor = color;
9+
}

Tutorial-03/simple.vertex_shader

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#version 330 core
2+
3+
layout (location = 0) in vec4 vPosition;
4+
layout (location = 1) in vec4 vColor;
5+
6+
uniform mat4 MVP;
7+
8+
out vec4 color;
9+
10+
void main()
11+
{
12+
gl_Position = MVP * vPosition;
13+
color = vColor;
14+
}

0 commit comments

Comments
 (0)