-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cpp
More file actions
537 lines (446 loc) · 18.4 KB
/
App.cpp
File metadata and controls
537 lines (446 loc) · 18.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Basic includes
#include <GL/glew.h>
#include <iostream>
#include <chrono>
#include <stack>
#include <random>
#include <sstream>
// OpenCV ? GL independent
#include <opencv2/opencv.hpp>
// OpenGL Extension Wrangler: allow all multiplatform GL functions
// WGLEW = Windows GL Extension Wrangler (change for different platform)
// platform specific functions (in this case Windows)
#include <GL/wglew.h>
// GLFW toolkit
// Uses GL calls to open GL context, i.e. GLEW must be first.
#include <GLFW/glfw3.h>
// OpenGL math
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
// Our app
#include "App.h"
#include "gl_err_callback.h"
#include "Shader.h"
//´------------------------------------------------------------------------------------------
// Freetype library - needs different shaders -> finalPlusText.xxx
#include <ft2build.h>
#include FT_FREETYPE_H
#include "text_fonts_glyphs.h"
//´------------------------------------------------------------------------------------------
// 3D sounds
#include <irrKlang.h>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#pragma comment(lib, "irrKlang.lib")
using namespace irrklang;
#define print(x) std::cout << x << "\n"
// Inicializace statických proměnných --
bool App::is_vsync_on = false;
bool App::is_fullscreen_on = false;
GLFWmonitor* App::monitor;
const GLFWvidmode* App::mode;
int App::window_xcor{};
int App::window_ycor{};
int App::window_width = 800;
int App::window_height = 600;
int App::window_width_return_from_fullscreen{};
int App::window_height_return_from_fullscreen{};
Camera App::camera = Camera(glm::vec3(0, 0, 0));
double App::last_cursor_xpos{};
double App::last_cursor_ypos{};
Projectile App::projectile; // Definice statického členského proměnného mimo třídu
float App::itemPickUpRange = 4.0f;
bool App::holdItem = true;
bool App::isFlashlightOn = false;
bool App::isLampOn = false;
glm::vec3 cameraPosition = glm::vec3(0, 25.0f, 0);
App::App()
{
// default constructor
// nothing to do here (for now...)
std::cout << "App Constructed done\n\n";
}
// App initialization, if returns true then run run()
bool App::Init()
{
try {
// Set GLFW error callback
glfwSetErrorCallback(error_callback);
// Init GLFW :: https://www.glfw.org/documentation.html
if (!glfwInit()) {
return false;
}
// Set OpenGL version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
// Set OpenGL profile
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Core, comment this line for Compatible
// Open window (GL canvas) with no special properties :: https://www.glfw.org/docs/latest/quick.html#quick_create_window
window = glfwCreateWindow(window_width, window_height, "OpenGL 3D Game", NULL, NULL);
if (!window) {
glfwTerminate();
return false;
}
glfwSetWindowUserPointer(window, this);
// Fullscreen On/Off
monitor = glfwGetPrimaryMonitor(); // Get primary monitor
mode = glfwGetVideoMode(monitor); // Get resolution of the monitor
// Setup callbacks
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetScrollCallback(window, scroll_callback);
// Set V-Sync OFF.
//glfwSwapInterval(0);
// Set V-Sync ON.
///*
glfwSwapInterval(1);
is_vsync_on = true;
/**/
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Init GLEW :: http://glew.sourceforge.net/basic.html
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); /* Problem: glewInit failed, something is seriously wrong. */
}
wglewInit();
//...after ALL GLFW & GLEW init ...
if (GLEW_ARB_debug_output)
{
glDebugMessageCallback(MessageCallback, 0);
glEnable(GL_DEBUG_OUTPUT);
//default is asynchronous debug output, use this to simulate glGetError() functionality
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
std::cout << "GL_DEBUG enabled.\n";
}
else
std::cout << "GL_DEBUG NOT SUPPORTED!\n";
// set GL params
glEnable(GL_DEPTH_TEST);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_CULL_FACE); // assuming ALL objects are non-transparent
// Transparency blending function
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// first init OpenGL, THAN init assets: valid context MUST exist
InitAssets();
}
catch (std::exception const& e) {
std::cerr << "Init failed : " << e.what() << "\n";
//throw;
exit(-1);
}
std::cout << "\nApp Initialized\n";
return true;
}
int App::Run(void)
{
try {
//´------------------------------------------------------------------------------------------
// Freetype Text
//
/*FT_Library free_type;
FT_Error error_code = FT_Init_FreeType(&free_type);
if (error_code)
{
std::cout << "\n Error code: " << error_code << " --- " << "An error occurred during initialising the FT_Library";
int keep_console_open;
std::cin >> keep_console_open;
}*/
audio.PlayMusic3D("teapot.wav", 1, true);
//
//Text text_object2(free_type, window_width, window_height, "01234567890Get Rady.Timr:owns&ClBgfb"); // Declare a new text object, passing in your chosen alphabet.
//text_object2.create_text_message("Get Ready... Timer: 000", 100, 50, "./assets/Text Fonts/arialbi.ttf", 130, false); // True indicates that the message will be modified.
//int num_replace = 3;
//size_t vec_size = text_object2.messages[0].characters_quads.size();
//float start_pos = text_object2.messages[0].start_x_current[vec_size - num_replace];
////glUniform1i(glGetUniformLocation(text_shader.ID, "alphabet_texture"), 31);
//shader.setUniform("alphabet_texture", 31);
//shader.setUniform("font_colour", glm::vec3(10.0f, 120.0f, 105.0f));
////shader.setUniform("font_colour", glm::vec3(1.0f, 1.0f, 1.0f));
//
//
//´------------------------------------------------------------------------------------------
double fps_counter_seconds = 0;
int fps_counter_frames = 0;
UpdateProjectionMatrix();
glViewport(0, 0, window_width, window_height);
//camera.position = glm::vec3(0, 0, 0);
camera.position = cameraPosition;
double last_frame_time = glfwGetTime();
glm::vec3 camera_movement{};
glm::vec3 rgb_orange = { 1.0f, 0.5f, 0.0f };
glm::vec3 rgb_white = { 1.0f, 1.0f, 1.0f };
glm::vec4 rgba_white = { 1.0f, 1.0f, 1.0f, 1.0f };
// Set camera position
//camera.position.y = 25.0f;
//camera.position.z = 0.0f;
bool isResettingCursor = false;
bool centered = false;
int width, height, centerX, centerY, window_xpos, window_ypos;
float deltaX, deltaY;
bool isCursorOutOfCenter = false;
float falling_speed = 0;
sound_to_player.x = camera.position.x - sound_model->position.x;
sound_to_player.y = camera.position.z - sound_model->position.z;
sound_to_player = glm::normalize(sound_to_player);
last_sound_to_player = sound_to_player;
glm::vec3 last_position;
double current_timestamp = glfwGetTime();
double walk_last_played_timestamp = current_timestamp;
const double walk_delay = 0.5;
const double sprint_delay = 0.3;
float posOnCircle = 0;
float radius = 5;
while (!glfwWindowShouldClose(window)) {
// Time/FPS measure start
auto fps_frame_start_timestamp = std::chrono::steady_clock::now();
current_timestamp = glfwGetTime();
// Clear OpenGL canvas, both color buffer and Z-buffer
glClearColor(clear_color.r, clear_color.g, clear_color.b, clear_color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// === After clearing the canvas ===
// React to user :: Create View Matrix according to camera settings
double delta_time = glfwGetTime() - last_frame_time;
last_frame_time = glfwGetTime();
//camera_movement = camera.ProcessInput(window, static_cast<float>(delta_time));
camera.onKeyboardEvent(window, static_cast<float>(delta_time)); // process keys etc
//std::cout << delta_time << "\n";
camera.position += camera_movement;
glm::mat4 mx_view = camera.getViewMatrix();
sound_to_player.x = camera.position.x;
sound_to_player.y = camera.position.z;
sound_to_player = glm::normalize(sound_to_player);
// 3D Audio
posOnCircle += 0.04f;
audio.UpdateMusicPosition(posOnCircle, radius);
//audio.UpdateMusicVolume(glm::length(sound_to_player) / glm::length(last_sound_to_player));
last_sound_to_player = sound_to_player;
//steps
if ((camera.getPosition() != last_position) && camera.position.y <= (GetHeightmapY(camera.position.x, camera.position.z) + PLAYER_HEIGHT + 0.03f)) { //0.03f == tolerance
if ((current_timestamp > walk_last_played_timestamp + walk_delay) || ((current_timestamp > walk_last_played_timestamp + sprint_delay) && camera.getSprint() ))
{
audio.Walk();
walk_last_played_timestamp = current_timestamp;
}
}
last_position = camera.getPosition();
POINT p;
if (GetCursorPos(&p)) {
// Získat rozměry okna
glfwGetWindowSize(window, &width, &height);
glfwGetWindowPos(window, &window_xpos, &window_ypos);
centerX = width / 2;
centerY = height / 2;
if (isResettingCursor) {
isResettingCursor = false;
last_cursor_xpos = centerX;
last_cursor_ypos = centerY;
centered = true;
}
deltaX = static_cast<float>(p.x - last_cursor_xpos - window_xpos);
deltaY = static_cast<float>(p.y - last_cursor_ypos - window_ypos);
//isCursorOutOfCenter = p.x != 400 || p.y != 300;
isCursorOutOfCenter = p.x != centerX || p.y != centerY;
// Aktualizovat pøedchozí pozici pro další iteraci
last_cursor_xpos = p.x;
last_cursor_ypos = p.y;
// Pokud došlo ke změně pozice, volat onMouseEvent s relativní změnou
if (isCursorOutOfCenter) {
camera.onMouseEvent(deltaX, deltaY, true);
isResettingCursor = true;
glfwSetCursorPos(window, centerX, centerY);
}
}
//
// Heightmap collision – for our X and Z get Y coordinate for ground level
auto heightmap_y = GetHeightmapY(camera.position.x, camera.position.z);
// Jumping Gravity
float min_hei = heightmap_y + PLAYER_HEIGHT;// Camera's smallest Y coordinate possible
if (camera_movement.y > 0.0f) { // If holding space
camera.position.y += delta_time * 2.0f; // Go up
falling_speed = 0;
if (camera.position.y < min_hei) { // For going up steep hills, so we cannot go into the hill
camera.position.y = min_hei;
}
//is_grounded = false;
}
else { // If not holding space
falling_speed += delta_time * 9.81f; // Gravity
camera.position.y -= delta_time * falling_speed;// Fall
if (camera.position.y < min_hei) { // Do not fall through ground
camera.position.y = min_hei;
falling_speed = 0;
//if (!is_grounded) { // Landing sound
// audio.PlayWalk();
//}
//is_grounded = true;
}
//else if (is_grounded && camera.position.y - min_hei > 1.0f) {
// is_grounded = false; // Do not make step sounds if transitioned from walking to falling w/o jetpack
//}
}
// Update objects
glm::vec2 object_to_player;
// glm::vec3(8.0f, 13.2f, -15.0f)
object_to_player.x = camera.position.x - 8.0f;
object_to_player.y = camera.position.z - ( - 15.0f);
glm::vec2 object_to_player_n = glm::normalize(object_to_player);
// Set Model Matrix
UpdateModels(static_cast<float>(delta_time));
// Activate shader, set uniform vars
shader.activate();
//my_shader.SetUniform("uMx_model", mx_model); // Object local coor space -> World space
shader.setUniform("uMx_view", mx_view); // World space -> Camera space
shader.setUniform("uMx_projection", mx_projection); // Camera space -> Screen
// Setup
shader.setUniform("u_ambient_alpha", 0.0f);
shader.setUniform("u_diffuse_alpha", 0.7f);
shader.setUniform("u_camera_position", camera.getPosition());
// Materials
shader.setUniform("u_material.ambient", glm::vec3(0.1f));
shader.setUniform("u_material.specular", glm::vec3(1.0f));
shader.setUniform("u_material.shininess", 90.0f);
// Directional light - Sun
shader.setUniform("u_directional_light.direction", glm::vec3(0.0f, -1.0f, -0.20f));
shader.setUniform("u_directional_light.diffuse", glm::vec3(1.0f));
shader.setUniform("u_directional_light.specular", glm::vec3(0.14f));
// Spotlight - Flashlight
shader.setUniform("u_spotlight.diffuse", glm::vec3(0.7f));
shader.setUniform("u_spotlight.specular", glm::vec3(0.56f));
shader.setUniform("u_spotlight.position", camera.getPosition());
shader.setUniform("u_spotlight.direction", camera.getFront());
shader.setUniform("u_spotlight.cos_inner_cone", glm::cos(glm::radians(20.0f)));
shader.setUniform("u_spotlight.cos_outer_cone", glm::cos(glm::radians(27.0f)));
shader.setUniform("u_spotlight.constant", 1.0f);
shader.setUniform("u_spotlight.linear", 0.07f);
shader.setUniform("u_spotlight.exponent", 0.017f);
shader.setUniform("u_spotlight.on", isFlashlightOn);
// Point light - Lamp on Table
shader.setUniform("u_point_lights[0].diffuse", glm::vec3(0.0f, 1.0f, 1.0f));
shader.setUniform("u_point_lights[0].specular", glm::vec3(0.07f));
shader.setUniform("u_point_lights[0].on", isLampOn);
glm::vec3 point_light_pos = glm::vec3(8.0f, 13.2f, -15.0f);
shader.setUniform("u_point_lights[0].position", point_light_pos);
shader.setUniform("u_point_lights[0].constant", 1.0f);
shader.setUniform("u_point_lights[0].linear", 1.0f);
shader.setUniform("u_point_lights[0].exponent", 0.5f);
//´------------------------------------------------------------------------------------------
// Freetype Text
//
//shader.setUniform("alphabet_texture", 31);
//shader.setUniform("font_colour", glm::vec3(10.0f, 120.0f, 105.0f));
//// std::cout << "\n display_counting: " << display_counting << " --- display_counting % 10: " << display_counting % 10 << " --- display_counting / 10 % 10: "
// // << display_counting / 10 % 10 << " --- display_counting / 100 % 10: " << display_counting / 100 % 10;
//unsigned num1 = 333 / 100 % 10; // Left digit.
//unsigned num2 = 333 / 10 % 10;
//unsigned num3 = 333 % 10;
//float advance1 = text_object2.messages[0].alphabet_vec[num1].glyph_advance_x;
//float advance2 = advance1 + (text_object2.messages[0].alphabet_vec[num2].glyph_advance_x);
//text_object2.messages[0].characters_quads.resize(vec_size - num_replace);
//text_object2.messages[0].text_start_x = start_pos;
//text_object2.process_text_index(text_object2.messages[0], num1, 0); // Important: the number of calls to: process_text_index(...) must = "num_replace_characters"
//text_object2.process_text_index(text_object2.messages[0], num2, advance1);
//text_object2.process_text_index(text_object2.messages[0], num3, advance2);
//text_object2.update_buffer_data_message(text_object2.messages[0], (int)(vec_size - num_replace));
//text_object2.draw_messages(0);
//text_object2.draw_alphabets();
//
//´------------------------------------------------------------------------------------------
// Draw the scene
// - Draw opaque objects
for (auto& [key, value] : scene_opaque) {
value->Draw(shader);
}
// - Draw transparent objects
glEnable(GL_BLEND); // enable blending
glDisable(GL_CULL_FACE); // no polygon removal
glDepthMask(GL_FALSE); // set Z to read-only
// - - Calculate distace from camera for all transparent objects
for (auto& transparent_pair : scene_transparent_pairs) {
transparent_pair->second->_distance_from_camera = glm::length(camera.position - transparent_pair->second->position);
}
// - - Sort all transparent objects in vector by their distance from camera (far to near)
std::sort(scene_transparent_pairs.begin(), scene_transparent_pairs.end(), [](std::pair<const std::string, Model*>*& a, std::pair<const std::string, Model*>*& b) {
return a->second->_distance_from_camera > b->second->_distance_from_camera;
});
// - - Draw all transparent objects in sorted order
for (auto& transparent_pair : scene_transparent_pairs) {
transparent_pair->second->Draw(shader);
}
glDisable(GL_BLEND);
glEnable(GL_CULL_FACE);
glDepthMask(GL_TRUE);
//projectile.drawAllProjectiles(2.0f);
// === End of frame ===
// Swap front and back buffers
glfwSwapBuffers(window);
// Poll for and process events
glfwPollEvents();
// Time/FPS measure end
auto fps_frame_end_timestamp = std::chrono::steady_clock::now();
std::chrono::duration<double> fps_elapsed_seconds = fps_frame_end_timestamp - fps_frame_start_timestamp;
fps_counter_seconds += fps_elapsed_seconds.count();
fps_counter_frames++;
if (fps_counter_seconds >= 1) {
//std::cout << fps_counter_frames << " FPS\n";
std::stringstream ss;
ss << fps_counter_frames << " FPS";
std::cout << fps_counter_frames << " FPS" << "\n";
glfwSetWindowTitle(window, ss.str().c_str());
fps_counter_seconds = 0;
fps_counter_frames = 0;
}
}
}
catch (std::exception const& e) {
std::cerr << "App failed: " << e.what() << "\n";
return EXIT_FAILURE;
}
GetInformation();
std::cout << "App Finished\n";
return EXIT_SUCCESS;
}
App::~App()
{
// clean-up
shader.clear();
if (window) {
glfwDestroyWindow(window);
}
glfwTerminate();
exit(EXIT_SUCCESS);
std::cout << "Bye User\n";
}
void App::UpdateProjectionMatrix(void)
{
if (window_height < 1) window_height = 1; // avoid division by 0
float ratio = static_cast<float>(window_width) / window_height;
mx_projection = glm::perspective(
glm::radians(FOV), // The vertical Field of View
ratio, // Aspect Ratio. Depends on the size of window.
0.1f, // Near clipping plane. Keep as big as possible, or you'll get precision issues.
20000.0f // Far clipping plane. Keep as little as possible.
);
}
void App::GetInformation()
{
std::cout << "\nGL on this PC info:\n";
std::cout << "GL Version:\t" << glGetString(GL_VERSION) << "\n";
std::cout << "GL Renderer:\t" << glGetString(GL_RENDERER) << "\n";
std::cout << "GL Vendor:\t" << glGetString(GL_VENDOR) << "\n";
std::cout << "GL Shading ver:\t" << glGetString(GL_SHADING_LANGUAGE_VERSION) << "\n\n";
GLint profile;
glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &profile);
if (const auto errorCode = glGetError()) {
std::cout << "[!] Pending GL error while obtaining profile: " << errorCode << "\n";
}
if (profile & GL_CONTEXT_CORE_PROFILE_BIT) {
std::cout << "Core profile" << "\n";
}
else {
std::cout << "Compatibility profile" << "\n";
}
}