-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
466 lines (359 loc) · 14.4 KB
/
Copy pathapp.cpp
File metadata and controls
466 lines (359 loc) · 14.4 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include "app.h"
// Standard libraries
#include <exception>
#include <iostream>
#include <stack>
#include <vector>
#ifdef _DEBUG
#include <chrono>
#endif
// OpenGL libraries
#include <glm/glm.hpp>
#include "glm/gtc/matrix_transform.hpp"
#include <glm/gtc/type_ptr.hpp>
// Custom libraries
#include "sanitize.h"
#include "shader.h"
#include "ObjLoader.h"
#include "directional_light.h"
#include "point_light.h"
#include "Landscape.h"
#include "PrimitiveCube.h"
#include "PrimitiveSphere.h"
#include "texture.h"
/* Uniforms*/
GLuint* modelID = new GLuint[Globals::total_shaders];
GLuint* normal_matrixID = new GLuint[Globals::total_shaders];
glm::mat3 normal_matrix;
GLuint* global_ambientID = new GLuint[Globals::total_shaders];
glm::vec4 global_ambient = { 0.72, 0.86, 0.90, 1.0 };
GLuint* emissive_colourID = new GLuint[Globals::total_shaders];
glm::vec4 emissive_colour = { 1,1,1,1 };
/* Textures */
Texture* skyTexture = new Texture();
// Landscape
Texture* noiseTexture = new Texture();
Texture* groundTexture = new Texture();
Texture* rockTexture = new Texture();
Texture* snowTexture = new Texture();
// Foliage
Texture* treeTexture_diffuse = new Texture();
Texture* treeTexture_specular = new Texture();
/* Lights */
PointLight aPointLight;
/* 3D objects */
Object3D tree;
PrimitiveCube aCube;
PrimitiveSphere aSphere = PrimitiveSphere(16, 16, { 1,1,1,1 });
Landscape* landscape = new Landscape(4, 1.75f, 2.0f);
// Foliage scattering coordinates and scale variations.
std::vector<glm::vec3> scatterpoints;
std::vector<GLfloat> scatterscales;
// Avoidance of repetition - This allows to change shader program easily without having to repeat the same line. Usefull for setting certain objects to use the emissive shader.
#define SET_SHADER_PROGRAM(I) \
glUseProgram(programs[I]); \
glUniformMatrix4fv(camera.viewID[I], 1, GL_FALSE, value_ptr(camera.view)); \
glUniformMatrix4fv(camera.projectionID[I], 1, GL_FALSE, value_ptr(camera.projection)); \
glUniform4fv(aPointLight.positionID[I], 1, value_ptr(aPointLight.position)); \
glUniform3fv(aPointLight.colourID[I], 1, value_ptr(aPointLight.colour)); \
glUniform1fv(aPointLight.intensityID[I], 1, &aPointLight.intensity); \
glUniform4fv(global_ambientID[I], 1, value_ptr(global_ambient))
// This allows to revert back to the currently used program.
#define USE_CURRENT_PROGRAM() \
glUseProgram(programs[current_shader]); \
glUniformMatrix4fv(camera.viewID[current_shader], 1, GL_FALSE, &camera.view[0][0]); \
glUniformMatrix4fv(camera.projectionID[current_shader], 1, GL_FALSE, &camera.projection[0][0]); \
glUniform4fv(aPointLight.positionID[current_shader], 1, value_ptr(aPointLight.position)); \
glUniform3fv(aPointLight.colourID[current_shader], 1, &aPointLight.colour[0]); \
glUniform1fv(aPointLight.intensityID[current_shader], 1, &aPointLight.intensity); \
glUniform4fv(global_ambientID[current_shader], 1, &global_ambient[0])
App::App(int w, int h, const char* title)
{
this->programs = nullptr;
this->window = nullptr;
window_properties = new WindowProperties(w, h, title);
CameraProperties cp = CameraProperties(75.0f, 0.001f, 1000.0f);
camera = Camera(window_properties->aspect_ratio, cp, glm::vec3(0, 0, 4), glm::vec3(0, 0, -1));
cursor = new Cursor(window_properties->width, window_properties->height);
drawmode = 0;
/* Init GLFW */
if (!glfwInit())
exit(EXIT_FAILURE);
}
App::~App()
{
delete skyTexture;
delete noiseTexture;
delete groundTexture;
delete rockTexture;
delete snowTexture;
delete treeTexture_diffuse;
delete treeTexture_specular;
landscape = nullptr;
delete landscape;
delete[] modelID;
delete[] normal_matrixID;
delete[] global_ambientID;
delete[] programs;
cursor = nullptr;
delete cursor;
window_properties = nullptr;
delete window_properties;
}
void App::display_version()
{
int maj_ver, min_ver;
glGetIntegerv(GL_MAJOR_VERSION, &maj_ver);
glGetIntegerv(GL_MINOR_VERSION, &min_ver);
std::cout << "OpenGL Version = " << maj_ver << "." << min_ver << std::endl;
// Hardware details
std::cout << "Vender: " << glGetString(GL_VENDOR) << std::endl;
std::cout << "Version:" << glGetString(GL_VERSION) << std::endl;
std::cout << "Renderer:" << glGetString(GL_RENDERER) << std::endl;
}
/* Our own initialisation function */
void App::init()
{
/* Set the object transformation controls to their initial values */
// Generate index (name) for one vertex array object
glGenVertexArrays(1, &vao);
// Create the vertex array object and make it current
glBindVertexArray(vao);
/* Load and build the vertex and fragment shaders */
try
{
programs = new GLuint[Globals::total_shaders];
programs[0] = Shader::LoadShader("./shaders/UVcheckerboard.vert", "./shaders/UVcheckerboard.frag");
programs[1] = Shader::LoadShader("./shaders/tree.vert", "./shaders/tree.frag");
programs[2] = Shader::LoadShader("./shaders/landscape.vert", "./shaders/landscape.frag");
programs[3] = Shader::LoadShader("./shaders/emissive.vert", "./shaders/emissive.frag");
programs[4] = Shader::LoadShader("./shaders/skysphere.vert", "./shaders/skysphere.frag");
}
catch (std::exception& e)
{
std::cout << "Caught exception: " << e.what() << std::endl;
std::cin.ignore();
exit(0);
}
/* Define uniforms to send to vertex shader */
for (size_t i = 0; i < Globals::total_shaders; i++) {
glUseProgram(this->programs[i]);
modelID[i] = glGetUniformLocation(programs[i], "model");
camera.viewID[i] = glGetUniformLocation(programs[i], "view");
camera.projectionID[i] = glGetUniformLocation(programs[i], "projection");
normal_matrixID[i] = glGetUniformLocation(programs[i], "n_matrix");
aPointLight.positionID[i] = glGetUniformLocation(programs[i], "light_position");
aPointLight.colourID[i] = glGetUniformLocation(programs[i], "light_colour");
aPointLight.intensityID[i] = glGetUniformLocation(programs[i], "light_intensity");
global_ambientID[i] = glGetUniformLocation(programs[i], "global_ambient");
emissive_colourID[i] = glGetUniformLocation(programs[i], "emissive_colour");
}
// Landscape textures
glActiveTexture(GL_TEXTURE1);
createTexture(noiseTexture, "./textures/noise.png", "noise");
loadTexture(noiseTexture, programs, 1);
glActiveTexture(GL_TEXTURE2);
createTexture(rockTexture, "./textures/landscape/rocks_ground_diffuse.png", "D_rocks");
loadTexture(rockTexture, programs, 2);
glActiveTexture(GL_TEXTURE3);
createTexture(groundTexture, "./textures/landscape/forrest_ground_diffuse.png", "D_ground");
loadTexture(groundTexture, programs, 3);
glActiveTexture(GL_TEXTURE4);
createTexture(snowTexture, "./textures/landscape/snow_diffuse.png", "D_snow");
loadTexture(snowTexture, programs, 4);
// Tree textures
glActiveTexture(GL_TEXTURE5);
createTexture(treeTexture_diffuse, "./textures/foliage/firtree_diffuse.jpg", "D_tree");
loadTexture(treeTexture_diffuse, programs, 5);
glActiveTexture(GL_TEXTURE6);
createTexture(treeTexture_specular, "./textures/foliage/firtree_specular.jpg", "S_tree");
loadTexture(treeTexture_specular, programs, 6);
// Sky texture
glActiveTexture(GL_TEXTURE0);
createTexture(skyTexture, "./textures/skybox/sky1.png", "sky");
loadTexture(skyTexture, programs, 0);
importOBJAsset(&tree, "./OBJs/fir_tree.obj", nullptr);
tree.setVertexColour({1,1,1,1});
tree.makeObject();
aSphere.makeObject();
aCube.makeObject();
landscape->generateLanscape({ 100, 100 }, { 80.0f, 80.0f }, 1.25f);
HeightToColour mappings[3] = {
{14, {1,0,0,1}}, // Mountains
{9, {0,1,0,1}}, // Plains
{2.0f, {0,0,1,1}}, // Sand
};
landscape->setColourByHeight(mappings);
landscape->createFoliageScatterPoints(scatterpoints, scatterscales);
landscape->makeObject();
aPointLight.colour = { 1,1,1 };
aPointLight.intensity = 1.0f;
}
/* Rendering function */
void App::display()
{
using namespace glm;
// Delta-time calculation - https://learnopengl.com/code_viewer_gh.php?code=src/1.getting_started/7.2.camera_keyboard_dt/camera_keyboard_dt.cpp - 04/12/2023
current_frame = static_cast<float>(glfwGetTime());
delta_time = current_frame - last_frame;
last_frame = current_frame;
/* Define the background colour */
glClearColor(global_ambient.r, global_ambient.g, global_ambient.b, global_ambient.a);
/* Clear the colour and frame buffers */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Enable depth test */
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
/* Make the compiled shader program current */
glUseProgram(this->programs[current_shader]);
camera.update_perspective(window_properties->aspect_ratio);
// Update drawing modes
tree.draw_mode = drawmode;
aSphere.draw_mode = drawmode;
landscape->draw_mode = drawmode;
// Send camera view and projection matrices to shader
glUniformMatrix4fv(camera.viewID[current_shader], 1, GL_FALSE, &camera.view[0][0]);
glUniformMatrix4fv(camera.projectionID[current_shader], 1, GL_FALSE, &camera.projection[0][0]);
aPointLight.position = glm::vec4(-1, 1, 0, 1);
// Send point light information to shader
glUniform4fv(aPointLight.positionID[current_shader], 1, value_ptr(aPointLight.position));
glUniform3fv(aPointLight.colourID[current_shader], 1, &aPointLight.colour[0]);
glUniform1fv(aPointLight.intensityID[current_shader], 1, &aPointLight.intensity);
// Send global ambient colour to shader
glUniform4fv(global_ambientID[current_shader], 1, &global_ambient[0]);
// Define our model transformation in a stack and
// push the identity matrix onto the stack
std::stack<mat4> model;
model.push(mat4(1.0f));
// Skybox
SET_SHADER_PROGRAM(4);
model.push(model.top());
{
model.top() = rotate(model.top(), -radians(-90.0f), vec3(1, 0, 0));
model.top() = rotate(model.top(), -radians(-45.0f), vec3(0, 0, 1));
model.top() = scale(model.top(), vec3(500.0f));
glUniformMatrix4fv(modelID[4], 1, GL_FALSE, value_ptr(model.top()));
normal_matrix = transpose(inverse(mat3(camera.view * model.top())));
glUniformMatrix3fv(normal_matrixID[4], 1, GL_FALSE, value_ptr(normal_matrix));
aSphere.drawObject(false);
}
model.pop();
// Point light
SET_SHADER_PROGRAM(3);
model.push(model.top());
{
model.top() = translate(model.top(), vec3(aPointLight.position.x, aPointLight.position.y, aPointLight.position.z));
model.top() = scale(model.top(), vec3(0.05f, 0.05f, 0.05f));
glUniformMatrix4fv(modelID[3], 1, GL_FALSE, &(model.top()[0][0]));
emissive_colour = { aPointLight.colour,1 };
glUniform4fv(emissive_colourID[3], 1, value_ptr(emissive_colour));
aSphere.drawObject(true);
}
model.pop();
// Landscape
SET_SHADER_PROGRAM(2);
model.push(model.top());
{
// Define the model transformations for the cube
model.top() = translate(model.top(), vec3(0, -10.0f, 0));
model.top() = rotate(model.top(), -radians(0.0f), vec3(1, 0, 0));
model.top() = rotate(model.top(), -radians(0.0f), vec3(0, 1, 0));
model.top() = rotate(model.top(), -radians(0.0f), vec3(0, 0, 1));
model.top() = scale(model.top(), vec3(1.0f));
// Send the model uniform to the currently bound shader,
glUniformMatrix4fv(modelID[2], 1, GL_FALSE, &(model.top()[0][0]));
normal_matrix = transpose(inverse(mat3(camera.view * model.top())));
glUniformMatrix3fv(normal_matrixID[2], 1, GL_FALSE, value_ptr(normal_matrix));
landscape->drawObject(false);
}
model.pop();
// Scatter trees
for (size_t i = 0; i < scatterpoints.size(); i++) {
glm::vec3& point = scatterpoints[i];
GLfloat& point_scale = scatterscales[i];
SET_SHADER_PROGRAM(1);
model.push(model.top());
{
// Define the model transformations for the cube
model.top() = translate(model.top(), vec3(point.x, point.y-12, point.z));
model.top() = rotate(model.top(), -radians(0.0f), vec3(1, 0, 0));
model.top() = rotate(model.top(), -radians(0.0f), vec3(0, 1, 0));
model.top() = rotate(model.top(), -radians(0.0f), vec3(0, 0, 1));
model.top() = scale(model.top(), vec3(point_scale));//scale equally in all axis
// Send the model uniform to the currently bound shader,
glUniformMatrix4fv(modelID[1], 1, GL_FALSE, &(model.top()[0][0]));
normal_matrix = transpose(inverse(mat3(camera.view * model.top())));
glUniformMatrix3fv(normal_matrixID[1], 1, GL_FALSE, value_ptr(normal_matrix));
tree.drawObject(true);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)tree.vertex_positions.size());
}
model.pop();
}
glDisableVertexAttribArray(0);
glUseProgram(0);
}
int App::run() {
#ifdef _DEBUG
auto start = std::chrono::system_clock::now();
#endif
glfwWindowHint(GLFW_SAMPLES, 8);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Register the error callback first to enable any GLFW errors to be processed*/
glfwSetErrorCallback(error_callback);
/* Create a GLFW window, bail out if it doesn't work */
this->window = glfwCreateWindow(window_properties->width, window_properties->height, window_properties->title, NULL, NULL);
if (!this->window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
/* Associate an OpenGL context with the recently created GLFW window */
glfwMakeContextCurrent(this->window);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD. Exiting." << std::endl;
return -1;
}
// Register function pointer for cursor position
glfwSetCursorPosCallback(this->window, cursor_position_callback);
// Register function pointer for mouse button input
glfwSetMouseButtonCallback(this->window, mouse_btn_callback);
// Register function pointer for key input.
glfwSetKeyCallback(this->window, key_callback);
// Register function pointer for window resizing.
glfwSetFramebufferSizeCallback(this->window, reshape);
display_version();
// Setup
init();
#ifdef _DEBUG
bool doOnce = false;
#endif
/* The main event loop */
while (!glfwWindowShouldClose(this->window))
{
camera.updateView();
/* Call our own drawing function */
display();
/* Swap buffers: GLFW is double buffered as standard */
glfwSwapBuffers(this->window);
#ifdef _DEBUG
if (!doOnce) {
auto end = std::chrono::system_clock::now();
std::cout << "Took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start) << " to render the first frame.\n";
doOnce = true;
}
#endif
/* Processes registered events, causes the callbacks to be called.*/
glfwPollEvents();
}
/* Clean up */
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
return 0;
}