-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.cpp
365 lines (272 loc) · 9.94 KB
/
example.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
#include "common.hpp"
#include "mesh.hpp"
#define BUFFER_SIZE 65536
// Some global variables
// Window dimensions
int window_width = 800,
window_height = 600;
vec3 light_pos(0, 8, 3);
vec4 light_color(1,1,1,1);
vec4 ambient_color(0.0125,0.025,0.025,1);
float specular_coeff = 1.0;
float shininess = 30.0;
// Shader objects
GLuint vertex_shader;
GLuint fragment_shader;
GLuint shader_program;
// VAOs and VBOs
GLuint mesh_vao;
GLuint vertex_buffer;
GLuint color_buffer;
GLuint normal_buffer;
GLuint index_buffer;
// Function declarations
void init_shaders(const string& vertex_shader_file, const string& fragment_shader_file);
void free_shaders();
void init_buffers(const Mesh& mesh);
void free_buffers();
void display_loop(GLFWwindow* window);
void draw(const Mesh& mesh, const mat4& perspective, const mat4& view, const mat4& model);
int main()
{
// Initialize GLFW
if (!glfwInit()) {
return 1;
}
// Set flags before window creation
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Make window size fixed
// Set OpenGL version from generated flextGL flags
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, FLEXT_MAJOR_VERSION);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, FLEXT_MINOR_VERSION);
#ifndef TARGET_GLES
if (FLEXT_CORE_PROFILE) {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
} else {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
}
#else
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#endif
// Create GLFW window
GLFWwindow* window = glfwCreateWindow(window_width, window_height, "flextGL GLFW3 example", NULL, NULL);
if(!window) {
cerr << "Failed to create OpenGL window" << endl;
glfwTerminate();
return 1;
}
// Make the window's context current
glfwMakeContextCurrent(window);
// Initialize flextGL
if (flextInit(window) != GL_TRUE) {
glfwTerminate();
return 1;
}
// Start display loop
display_loop(window);
glfwTerminate();
return 0;
}
void init_transforms(mat4& perspective, mat4& view, mat4& model)
{
float aspect = float(window_width)/float(window_height);
perspective = glm::perspective(60.0f, aspect, 0.1f, 100.0f);
view = glm::translate(mat4(1.0f), vec3(0,0,-4));
model = mat4(1.0f);
}
void step_simulation(mat4& view, mat4& model, double time_diff)
{
// Do a continuous rotation of the model
model = glm::rotate(model, float(30.0 * time_diff), vec3(0,1,0));
model = glm::rotate(model, float(20.0 * time_diff), vec3(1,0,0));
model = glm::rotate(model, float(10.0 * time_diff), vec3(0,0,1));
}
void display_loop(GLFWwindow* window)
{
Mesh mesh;
create_donut(mesh, 1.0, 0.25, 128, 32);
#ifndef TARGET_GLES
glEnable(GL_FRAMEBUFFER_SRGB);
#endif
glEnable(GL_DEPTH_TEST);
#ifndef TARGET_GLES
init_shaders("example.vert", "example.frag");
#else
init_shaders("example-es.vert", "example-es.frag");
#endif
init_buffers(mesh);
bool running = true;
mat4 perspective, view, model;
init_transforms(perspective, view, model);
double last_time = glfwGetTime();
while(running) {
double time_now = glfwGetTime();
double time_diff = time_now-last_time;
last_time = time_now;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
step_simulation(view, model, time_diff);
draw(mesh, perspective, view, model);
get_errors();
glfwSwapBuffers(window);
glfwPollEvents();
running = running && !glfwGetKey( window, GLFW_KEY_ESCAPE );
running = running && !glfwGetKey( window, 'Q' );
running = running && !glfwWindowShouldClose( window );
}
free_buffers();
free_shaders();
// Mesh destructor is called here..
}
GLuint compile_shader(const string& source, const string& name, GLenum type)
{
GLint status;
GLuint shader = glCreateShader(type);
const char* source_c_string = source.c_str();
glShaderSource(shader, 1, &source_c_string, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (!glIsShader(shader) || !status) {
cout << name << ": Shader compilation failed." << endl;
char* buffer = new char[BUFFER_SIZE];
GLsizei length;
glGetShaderInfoLog(shader, BUFFER_SIZE, &length, buffer);
if (length > 0) {
cout << buffer << endl;
}
delete[] buffer;
exit(1);
}
return shader;
}
GLuint link_program(GLuint vertex, GLuint fragment)
{
GLint status;
GLuint program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (!glIsProgram(program) || !status) {
cout << "Shader linkage failed." << endl;
char* buffer = new char[BUFFER_SIZE];
GLsizei length;
glGetProgramInfoLog(program, BUFFER_SIZE, &length, buffer);
if (length > 0) {
cout << buffer << endl;
}
delete[] buffer;
exit(1);
}
return program;
}
void init_shaders(const string& vertex_shader_file,
const string& fragment_shader_file)
{
if (!file_exists(vertex_shader_file) ||
!file_exists(fragment_shader_file)) {
cerr << "Shader file missing" << endl;
exit(1);
}
string vertex_shader_source = read_file(vertex_shader_file);
string fragment_shader_source = read_file(fragment_shader_file);
vertex_shader = compile_shader(vertex_shader_source, vertex_shader_file, GL_VERTEX_SHADER);
fragment_shader = compile_shader(fragment_shader_source, fragment_shader_file, GL_FRAGMENT_SHADER);
shader_program = link_program(vertex_shader, fragment_shader);
get_errors();
}
void free_shaders()
{
glDeleteProgram(shader_program);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
}
void init_buffers(const Mesh& mesh)
{
// Create and bind VAO
#ifndef TARGET_GLES
glGenVertexArrays(1, &mesh_vao);
glBindVertexArray(mesh_vao);
#else
glGenVertexArraysOES(1, &mesh_vao);
glBindVertexArrayOES(mesh_vao);
#endif
// Create VBOs
glGenBuffers(1, &vertex_buffer);
glGenBuffers(1, &color_buffer);
glGenBuffers(1, &normal_buffer);
glGenBuffers(1, &index_buffer);
// Upload, enable & set VBOs
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, mesh.vertex_count * sizeof(vec4), mesh.vertices, GL_STATIC_DRAW);
GLint vertex_attrib = glGetAttribLocation(shader_program, "vertex");
glEnableVertexAttribArray(vertex_attrib);
glVertexAttribPointer(vertex_attrib, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
glBufferData(GL_ARRAY_BUFFER, mesh.vertex_count * sizeof(vec4), mesh.colors, GL_STATIC_DRAW);
GLint color_attrib = glGetAttribLocation(shader_program, "color");
glEnableVertexAttribArray(color_attrib);
glVertexAttribPointer(color_attrib, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, normal_buffer);
glBufferData(GL_ARRAY_BUFFER, mesh.vertex_count * sizeof(vec3), mesh.normals, GL_STATIC_DRAW);
GLint normal_attrib = glGetAttribLocation(shader_program, "normal");
glEnableVertexAttribArray(normal_attrib);
glVertexAttribPointer(normal_attrib, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.index_count * sizeof(GLuint), mesh.indices, GL_STATIC_DRAW);
#ifndef TARGET_GLES
glBindVertexArray(0);
#else
glBindVertexArrayOES(0);
#endif
}
void free_buffers()
{
#ifndef TARGET_GLES
glDeleteVertexArrays(1, &mesh_vao);
#else
glDeleteVertexArraysOES(1, &mesh_vao);
#endif
glDeleteBuffers(1, &vertex_buffer);
glDeleteBuffers(1, &color_buffer);
glDeleteBuffers(1, &normal_buffer);
glDeleteBuffers(1, &index_buffer);
}
void draw(const Mesh& mesh, const mat4& perspective, const mat4& view, const mat4& model)
{
// Bind shader
glUseProgram(shader_program);
// Set uniforms
GLint perspective_uniform = glGetUniformLocation(shader_program, "perspective");
GLint view_uniform = glGetUniformLocation(shader_program, "view");
GLint view_rot_inv_uniform = glGetUniformLocation(shader_program, "view_rot_inv");
GLint model_uniform = glGetUniformLocation(shader_program, "model");
GLint light_pos_uniform = glGetUniformLocation(shader_program, "light_pos");
GLint light_color_uniform = glGetUniformLocation(shader_program, "light_color");
GLint ambient_color_uniform = glGetUniformLocation(shader_program, "ambient_color");
GLint specular_coeff_uniform = glGetUniformLocation(shader_program, "specular_coeff");
GLint shininess_uniform = glGetUniformLocation(shader_program, "shininess");
glUniformMatrix4fv(perspective_uniform, 1, GL_FALSE, glm::value_ptr(perspective));
glUniformMatrix4fv(view_uniform, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix3fv(view_rot_inv_uniform, 1, GL_FALSE, glm::value_ptr(glm::inverse(glm::mat3(view))));
glUniformMatrix4fv(model_uniform, 1, GL_FALSE, glm::value_ptr(model));
glUniform3fv(light_pos_uniform, 1, glm::value_ptr(light_pos));
glUniform4fv(light_color_uniform, 1, glm::value_ptr(light_color));
glUniform4fv(ambient_color_uniform, 1, glm::value_ptr(ambient_color));
glUniform1f(specular_coeff_uniform, specular_coeff);
glUniform1f(shininess_uniform, shininess);
// Bind VAO
#ifndef TARGET_GLES
glBindVertexArray(mesh_vao);
#else
glBindVertexArrayOES(mesh_vao);
#endif
// Draw mesh
glDrawElements(mesh.primitive_type, mesh.index_count, GL_UNSIGNED_INT, NULL);
// Unbind VAO
#ifndef TARGET_GLES
glBindVertexArray(0);
#else
glBindVertexArrayOES(0);
#endif
// Unbind shaders
glUseProgram(0);
}