-
Notifications
You must be signed in to change notification settings - Fork 2
/
ogl_rendering.cpp
237 lines (182 loc) · 7.49 KB
/
ogl_rendering.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
#include "ogl_rendering.hpp"
#include <cstdio>
// For cleanup purpose
static std::vector<GLuint> gl_vaos;
static std::vector<GLuint> gl_vbos;
////////////////////////////////////////////////////////////////////////////////
/// Drawing loop.
/// The functions indefinitely calls the graphics API drawing calls to display
/// all the objects generated by `he_mesh_data_to_opengl()`.
/// A simple rotation computation is called to animate the display.
void ogl_rendering_loop(GLFWwindow* window, GLuint program, const oglSceneObject* object_start, size_t n_objects)
{
GLint p_location = glGetUniformLocation(program, "P");
GLint v_location = glGetUniformLocation(program, "V");
GLint m_location = glGetUniformLocation(program, "M");
GLint color_location = glGetUniformLocation(program, "Color");
while (!glfwWindowShouldClose(window))
{
int width, height;
mat4x4 m, v, p;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mat4x4_identity(v);
float radius = 150.f;
mat4x4_ortho(p, -radius, radius, -radius, radius, -1000, 1000.0);
float angle = glfwGetTime();
vec3 eye = {cosf(angle) * radius, cosf(angle / 100.0) * radius, sinf(angle) * radius};
vec3 center = {0.0, 0.0, 0.0};
vec3 up = {0.0, 1.0, 0.0};
mat4x4_look_at(v, eye, center, up);
glUseProgram(program);
for (const oglSceneObject* object = object_start; object < object_start + n_objects; ++object) {
glUniformMatrix4fv(p_location, 1, GL_FALSE, (const GLfloat*)p);
glUniformMatrix4fv(v_location, 1, GL_FALSE, (const GLfloat*)v);
glUniformMatrix4fv(m_location, 1, GL_FALSE, (const GLfloat*)object->mat_transform_model);
// Set color uniform
glUniform4f(color_location, object->color[0], object->color[1], object->color[2], object->color[3]);
glBindVertexArray(object->gl_vao);
glDrawElements(GL_TRIANGLES, object->gl_indices_count, GL_UNSIGNED_INT, 0);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
}
////////////////////////////////////////////////////////////////////////////////
/// Initialized the window using GLFW
GLFWwindow* ogl_rendering_prepare_window()
{
glfwSetErrorCallback(ogl_rendering_error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "MeshViewer", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, ogl_rendering_key_callback);
glfwMakeContextCurrent(window);
gladLoadGL();
glfwSwapInterval(1);
return window;
}
void ogl_rendering_error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
void ogl_rendering_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
////////////////////////////////////////////////////////////////////////////////
/// Initializes OpenGL Shader program
GLuint ogl_rendering_prepare()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
const char* vertex_shader_text =
"#version 420 core\n"
"layout (location = 0) in vec3 vPos;\n"
"layout (location = 1) in vec3 vNorm;\n"
"out vec3 FragNormal;\n"
"out vec4 FragColor;\n"
"uniform mat4 M;\n"
"uniform mat4 V;\n"
"uniform mat4 P;\n"
"uniform vec4 Color;\n"
"void main()\n"
"{\n"
"gl_Position = P * V * M * vec4(vPos, 1.0f);\n"
"FragNormal = normalize(mat3(transpose(inverse(M))) * vNorm);\n"
"FragColor = Color;\n"
"}\n";
const char* fragment_shader_text =
"#version 420 core\n"
"out vec4 oFragColor;\n"
"\n"
"in vec3 FragNormal;\n"
"in vec4 FragColor;\n"
"\n"
"void main()\n"
"{\n"
"vec3 normal = normalize(FragNormal);\n"
"vec3 lightDirection = normalize(vec3(1.0, 1.0, 1.0));\n"
"float diff = max(dot(normal, lightDirection), 0.0);\n"
"oFragColor = diff * FragColor;\n"
"}\n";
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return program;
}
////////////////////////////////////////////////////////////////////////////////
/// Takes a vector of vertices and a vector of indices and sends them to the GPU
/// returns OpenGL IDs for reference.
/// Returns value is a pair of the following:
/// - Vertex Array Object ID
/// - Vertex Buffer Object ID
/// - Index Buffer Object ID
GLuint ogl_rendering_to_gpu(const std::vector<GLuint>& index_buffer, const std::vector<GLdouble>& vertex_buffer, const std::vector<GLdouble>& normal_buffer)
{
const GLuint gl_shader_coord_location = 0;
const GLuint gl_shader_normal_location = 1;
GLuint gl_vao = 0;
glGenVertexArrays(1, &gl_vao);
glBindVertexArray(gl_vao);
GLuint gl_bo_vertex = 0;
glGenBuffers(1, &gl_bo_vertex);
glBindBuffer(GL_ARRAY_BUFFER, gl_bo_vertex);
glBufferData(GL_ARRAY_BUFFER, vertex_buffer.size() * sizeof(GLdouble), vertex_buffer.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(gl_shader_coord_location);
glVertexAttribPointer(gl_shader_coord_location, 3, GL_DOUBLE, GL_FALSE, 3 * sizeof(GLdouble), (void*) 0);
GLuint gl_bo_normal = 0;
glGenBuffers(1, &gl_bo_normal);
glBindBuffer(GL_ARRAY_BUFFER, gl_bo_normal);
glBufferData(GL_ARRAY_BUFFER, normal_buffer.size() * sizeof(GLdouble), normal_buffer.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(gl_shader_normal_location);
glVertexAttribPointer(gl_shader_normal_location, 3, GL_DOUBLE, GL_FALSE, 3 * sizeof(GLdouble), (void*) 0);
GLuint gl_bo_index = 0;
glGenBuffers(1, &gl_bo_index);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_bo_index);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_buffer.size() * sizeof(GLuint), index_buffer.data(), GL_STATIC_DRAW);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
gl_vaos.push_back(gl_vao);
gl_vbos.insert(gl_vbos.end(), {gl_bo_vertex, gl_bo_normal, gl_bo_index});
return gl_vao;
}
////////////////////////////////////////////////////////////////////////////////
/// Cleans up all GLFW resources
void ogl_rendering_cleanup(GLuint program)
{
glDeleteProgram(program);
glDeleteVertexArrays(gl_vaos.size(), gl_vaos.data());
glDeleteBuffers(gl_vbos.size(), gl_vbos.data());
gl_vbos.clear();
gl_vaos.clear();
}
////////////////////////////////////////////////////////////////////////////////
/// Cleans up all OpenGL resources
void ogl_rendering_cleanup_window(GLFWwindow* window)
{
glfwDestroyWindow(window);
glfwTerminate();
}