-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
657 lines (525 loc) · 26.7 KB
/
application.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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// *****************************************************************************
//
// Visitlab Engine
//
// Copyright (c) Visitlab (https://visitlab.fi.muni.cz)
// All rights reserved.
//
// *****************************************************************************
#include "application.hpp"
#include "utils.hpp"
Application::Application(int initial_width, int initial_height, std::vector<std::string> arguments) : PV227Application(initial_width, initial_height, arguments)
{
Application::compile_shaders();
prepare_cameras();
prepare_materials();
prepare_textures();
prepare_lights();
prepare_scene();
prepare_framebuffers();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0);
}
Application::~Application()
{
}
// ----------------------------------------------------------------------------
// Shaderes
// ----------------------------------------------------------------------------
void Application::compile_shaders()
{
PV227Application::compile_shaders();
default_lit_program = ShaderProgram(lecture_shaders_path / "object.vert", lecture_shaders_path / "lit.frag");
display_texture_program = ShaderProgram(lecture_shaders_path / "full_screen_quad.vert", lecture_shaders_path / "display_texture.frag");
display_overlayed_texture_program = ShaderProgram(lecture_shaders_path / "full_screen_quad.vert", lecture_shaders_path / "display_overlay_texture.frag");
display_reflection_texture_program = ShaderProgram(lecture_shaders_path / "full_screen_quad.vert", lecture_shaders_path / "display_reflection_texture.frag");
std::cout << "Shaders are reloaded." << std::endl;
}
// ----------------------------------------------------------------------------
// Initialize Scene
// ----------------------------------------------------------------------------
void Application::prepare_cameras()
{
// Sets the default camera position.
camera.set_eye_position(glm::radians(-45.f), glm::radians(20.f), 100.f);
camera_ubo.set_projection(glm::perspective(glm::radians(45.f), static_cast<float>(this->width) / static_cast<float>(this->height), 0.1f, 1000.0f));
camera_ubo.update_opengl_data();
mirror_camera_ubo.set_projection(glm::perspective(glm::radians(45.f), static_cast<float>(this->width) / static_cast<float>(this->height), 0.1f, 1000.0f));
mirror_camera_ubo.update_opengl_data();
}
void Application::prepare_materials()
{
color_ubos.push_back(red_material_ubo);
color_ubos.push_back(green_material_ubo);
color_ubos.push_back(blue_material_ubo);
color_ubos.push_back(cyan_material_ubo);
color_ubos.push_back(magenta_material_ubo);
color_ubos.push_back(yellow_material_ubo);
color_ubos.push_back(white_material_ubo);
}
void Application::prepare_textures()
{
GLuint wood_tex = TextureUtils::load_texture_2d(framework_textures_path / "wood.png");
TextureUtils::set_texture_2d_parameters(wood_tex, GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR);
GLuint lenna_tex = TextureUtils::load_texture_2d(framework_textures_path / "lenna.png");
TextureUtils::set_texture_2d_parameters(lenna_tex, GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR);
hero_texture = TextureUtils::load_texture_2d(lecture_folder_path / "textures/stone.png");
TextureUtils::set_texture_2d_parameters(hero_texture, GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR);
floor_texture = TextureUtils::load_texture_2d(lecture_folder_path / "textures/floor.png");
TextureUtils::set_texture_2d_parameters(floor_texture, GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR);
// Creates a texture for storing the mirror render.
glCreateTextures(GL_TEXTURE_2D, 1, &mirror_texture);
glTextureStorage2D(mirror_texture, 1, GL_RGBA8, width, height);
TextureUtils::set_texture_2d_parameters(mirror_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
glCreateTextures(GL_TEXTURE_2D, 1, &mirror_depth_stencil_texture);
glTextureStorage2D(mirror_depth_stencil_texture, 1, GL_DEPTH24_STENCIL8, width, height);
TextureUtils::set_texture_2d_parameters(mirror_depth_stencil_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
// Creates a texture for storing the real render.
glCreateTextures(GL_TEXTURE_2D, 1, &real_texture);
glTextureStorage2D(real_texture, 1, GL_RGBA8, width, height);
TextureUtils::set_texture_2d_parameters(real_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
glCreateTextures(GL_TEXTURE_2D, 1, &real_depth_stencil_texture);
glTextureStorage2D(real_depth_stencil_texture, 1, GL_DEPTH24_STENCIL8, width, height);
TextureUtils::set_texture_2d_parameters(real_depth_stencil_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
// Creates a texture for the hero mask
glCreateTextures(GL_TEXTURE_2D, 1, &hero_mask_texture);
glTextureStorage2D(hero_mask_texture, 1, GL_R8, width, height);
TextureUtils::set_texture_2d_parameters(hero_mask_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
glCreateTextures(GL_TEXTURE_2D, 1, &hero_mask_depth_stencil_texture);
glTextureStorage2D(hero_mask_depth_stencil_texture, 1, GL_DEPTH24_STENCIL8, width, height);
TextureUtils::set_texture_2d_parameters(hero_mask_depth_stencil_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
textures.push_back(wood_tex);
textures.push_back(lenna_tex);
}
void Application::prepare_lights()
{
float gui_light_position = 1;
// Computes the light position.
const glm::vec3 light_position = glm::vec3(15, 15, 15) * glm::vec3(cosf(gui_light_position / 6.0f) * sinf(gui_light_position), sinf(gui_light_position / 6.0f), cosf(gui_light_position / 6.0f) * cosf(gui_light_position));
phong_lights_ubo.set_global_ambient(glm::vec3(0.0f));
phong_lights_ubo.add(PhongLightData::CreateDirectionalLight(light_position, glm::vec3(0.1f), glm::vec3(0.9f), glm::vec3(1.0f)));
phong_lights_ubo.update_opengl_data();
}
void Application::prepare_scene()
{
// Creates a random scene. We create a grid of random objects with random materials.
srand(12345);
// Prepares a list of geometries to pick from.
// We also define matrices that describe how to transform the objects so that they "sit" on XZ plane.
std::vector<std::pair<Geometry*, glm::mat4>> geometries;
geometries.push_back(std::make_pair(&cube, glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 1.0f, 0.0f))));
geometries.push_back(std::make_pair(&sphere, glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 1.0f, 0.0f))));
geometries.push_back(std::make_pair(&torus, glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 1.5f, 0.0f)) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f))));
geometries.push_back(std::make_pair(&cylinder, glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.5f, 0.0f)) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f))));
geometries.push_back(std::make_pair(&capsule, glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.5f, 0.0f)) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f))));
geometries.push_back(std::make_pair(&teapot, glm::mat4(1.0f)));
for (int x = 0; x < grid_size.x; x++)
for (int z = 0; z < grid_size.z; z++)
{
if ((x > 7 && x < 13 && z > 8 && z < 12) || (static_cast<float>(rand()) / RAND_MAX) > 0.3f)
continue;
// Chooses randomly the geometry.
const int geometry = rand() % static_cast<int>(geometries.size());
// Rotates the object randomly around the y axis.
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), static_cast<float>(rand()) / static_cast<float>(RAND_MAX) * static_cast<float>(M_PI) * 2.0f, glm::vec3(0.0f, 1.0f, 0.0f));
// Places the object at the proper place in the grid
glm::mat4 translation = glm::translate(glm::mat4(1.0f), glm::vec3(grid_start.x + static_cast<float>(x) * grid_spacing.x, 0.0f, grid_start.z + static_cast<float>(z) * grid_spacing.z));
// Chooses the material randomly, choose randomly between the colors and the textures.
const int material = rand() % static_cast<int>(color_ubos.size() + textures.size());
// Computes the model matrix.
ModelUBO model_ubo(translation * rotation * geometries[geometry].second);
// Creates the scene object and add it into the list.
SceneObject scene_object;
if (material < static_cast<int>(color_ubos.size()))
{
// Objects without textures.
scene_object = SceneObject(*geometries[geometry].first, model_ubo, color_ubos[material]);
}
else
{
// Objects with texture.
scene_object = SceneObject(*geometries[geometry].first, model_ubo, white_material_ubo, textures[material - static_cast<int>(color_ubos.size())]);
}
scene_objects.push_back(scene_object);
}
// Prepares the floor model.
const ModelUBO floor_model_ubo(
glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -0.1f, 0.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(grid_spacing.x * static_cast<float>(grid_size.x) / 2.0f + 5.0f, 0.1f, grid_spacing.z * static_cast<float>(grid_size.z) / 2.0f + 5.0f)));
const SceneObject floor_object = SceneObject(cube, floor_model_ubo, white_material_ubo, floor_texture);
scene_objects.push_back(floor_object);
// Prepares the hero object.
const Geometry hero = Geometry::from_file(lecture_folder_path / "models/golem.obj", false);
hero_object = SceneObject(hero, ModelUBO(glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -0.5f, 5.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(0.05f, 0.05f, 0.05f)) * glm::rotate(glm::mat4(1.0f), glm::pi<float>(), glm::vec3(0, 1, 0))),
white_material_ubo, hero_texture);
// Prepares an object representing the actual mirror.
const ModelUBO mirror_model_ubo(glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -0.1f, 0.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(10.0)));
mirror_object = SceneObject(cube, mirror_model_ubo, black_material_ubo);
// Prepares an object representing the mirror frame.
const ModelUBO mirror_frame_model_ubo(glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -0.1f, 0.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(10.0)));
mirror_frame_object = SceneObject(cube, mirror_frame_model_ubo, white_material_ubo, textures[0]);
}
void Application::prepare_framebuffers()
{
// Creates a framebuffer for rendering the mirror.
glCreateFramebuffers(1, &mirror_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, mirror_fbo);
glNamedFramebufferTexture(mirror_fbo, GL_COLOR_ATTACHMENT0, mirror_texture, 0);
glNamedFramebufferTexture(mirror_fbo, GL_DEPTH_STENCIL_ATTACHMENT, mirror_depth_stencil_texture, 0);
FBOUtils::check_framebuffer_status(mirror_fbo, "MirrorFBO");
// Creates a framebuffer for rendering the real scene.
glCreateFramebuffers(1, &real_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, real_fbo);
glNamedFramebufferTexture(real_fbo, GL_COLOR_ATTACHMENT0, real_texture, 0);
glNamedFramebufferTexture(real_fbo, GL_DEPTH_STENCIL_ATTACHMENT, real_depth_stencil_texture, 0);
FBOUtils::check_framebuffer_status(real_fbo, "RealFBO");
// Creates a framebuffer for rendering the hero mask.
glCreateFramebuffers(1, &hero_mask_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, hero_mask_fbo);
glNamedFramebufferTexture(hero_mask_fbo, GL_COLOR_ATTACHMENT0, hero_mask_texture, 0);
glNamedFramebufferTexture(hero_mask_fbo, GL_DEPTH_STENCIL_ATTACHMENT, hero_mask_depth_stencil_texture, 0);
FBOUtils::check_framebuffer_status(hero_mask_fbo, "MaskFBO");
// Creates and binds the required textures.
resize_fullscreen_textures();
}
void Application::resize_fullscreen_textures()
{
// Removes the previously allocated textures
glDeleteTextures(1, &mirror_texture);
glDeleteTextures(1, &mirror_depth_stencil_texture);
glDeleteTextures(1, &real_texture);
glDeleteTextures(1, &real_depth_stencil_texture);
glDeleteTextures(1, &hero_mask_texture);
glDeleteTextures(1, &hero_mask_depth_stencil_texture);
glCreateTextures(GL_TEXTURE_2D, 1, &mirror_texture);
glTextureStorage2D(mirror_texture, 1, GL_RGBA8, width, height);
TextureUtils::set_texture_2d_parameters(mirror_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
glCreateTextures(GL_TEXTURE_2D, 1, &mirror_depth_stencil_texture);
glTextureStorage2D(mirror_depth_stencil_texture, 1, GL_DEPTH24_STENCIL8, width, height);
TextureUtils::set_texture_2d_parameters(mirror_depth_stencil_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, mirror_fbo);
glNamedFramebufferTexture(mirror_fbo, GL_COLOR_ATTACHMENT0, mirror_texture, 0);
glNamedFramebufferTexture(mirror_fbo, GL_DEPTH_STENCIL_ATTACHMENT, mirror_depth_stencil_texture, 0);
FBOUtils::check_framebuffer_status(mirror_fbo, "MirrorFBO");
glCreateTextures(GL_TEXTURE_2D, 1, &real_texture);
glTextureStorage2D(real_texture, 1, GL_RGBA8, width, height);
TextureUtils::set_texture_2d_parameters(real_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
glCreateTextures(GL_TEXTURE_2D, 1, &real_depth_stencil_texture);
glTextureStorage2D(real_depth_stencil_texture, 1, GL_DEPTH24_STENCIL8, width, height);
TextureUtils::set_texture_2d_parameters(real_depth_stencil_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, real_fbo);
glNamedFramebufferTexture(real_fbo, GL_COLOR_ATTACHMENT0, real_texture, 0);
glNamedFramebufferTexture(real_fbo, GL_DEPTH_STENCIL_ATTACHMENT, real_depth_stencil_texture, 0);
FBOUtils::check_framebuffer_status(real_fbo, "RealFBO");
glCreateTextures(GL_TEXTURE_2D, 1, &hero_mask_texture);
glTextureStorage2D(hero_mask_texture, 1, GL_R8, width, height);
TextureUtils::set_texture_2d_parameters(hero_mask_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
glCreateTextures(GL_TEXTURE_2D, 1, &hero_mask_depth_stencil_texture);
glTextureStorage2D(hero_mask_depth_stencil_texture, 1, GL_DEPTH24_STENCIL8, width, height);
TextureUtils::set_texture_2d_parameters(hero_mask_depth_stencil_texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, hero_mask_fbo);
glNamedFramebufferTexture(hero_mask_fbo, GL_COLOR_ATTACHMENT0, hero_mask_texture, 0);
glNamedFramebufferTexture(hero_mask_fbo, GL_DEPTH_STENCIL_ATTACHMENT, hero_mask_depth_stencil_texture, 0);
FBOUtils::check_framebuffer_status(hero_mask_fbo, "MaskFBO");
}
// ----------------------------------------------------------------------------
// Update
// ----------------------------------------------------------------------------
void Application::update(float delta)
{
PV227Application::update(delta);
// Updates the main camera.
const glm::vec3 eye_position = camera.get_eye_position();
camera_ubo.set_view(lookAt(eye_position, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
camera_ubo.update_opengl_data();
// Updates the position of the mirror.
const glm::vec3 center = glm::vec3(0.0f, 20.0f, -30.0f);
const float angle = auto_rotate ? static_cast<float>(elapsed_time) * 0.001f : mirror_angle;
const glm::mat4 matrix = glm::translate(glm::mat4(1.0f), center) * glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0.0f, 1.0f, 0.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(15.f, 15.f, 0.5f));
mirror_object.get_model_ubo().set_matrix(matrix);
mirror_frame_object.get_model_ubo().set_matrix(matrix * glm::scale(glm::mat4(1.0f), glm::vec3(1.1f, 1.1f, 0.8f)));
mirror_object.get_model_ubo().update_opengl_data();
mirror_frame_object.get_model_ubo().update_opengl_data();
// Calculate Mirror Camera
glm::mat4 reflect = glm::mat4(1.0f);
reflect[2][2] = -1.0f; // Flip the Z-axis for reflection
// Compute the clipping plane (based on the mirror plane equation)
glm::vec3 mirror_normal = glm::vec3(sinf(angle), 0.0f, cosf(angle));
float plane_distance = sinf(angle + glm::half_pi<float>()) * 30.0f;
// Calculate the distance from the camera to the front of the mirror plane
float distance_from_camera_to_front_plane = glm::dot(glm::vec3(eye_position - center), mirror_normal);
clip_plane = (distance_from_camera_to_front_plane > 0.0f) ? glm::vec4(mirror_normal, plane_distance) : glm::vec4(-mirror_normal, -plane_distance);
mirror_camera_view = camera.get_view_matrix() * matrix * reflect * glm::inverse(matrix);
mirror_camera_ubo.set_view(mirror_camera_view);
mirror_camera_ubo.update_opengl_data();
}
// ----------------------------------------------------------------------------
// Render
// ----------------------------------------------------------------------------
void Application::render()
{
// Starts measuring the elapsed time.
glBeginQuery(GL_TIME_ELAPSED, render_time_query);
render_mirror_scene();
render_initial_scene();
render_hero_mask_scene();
// Renders the initial scene.
if (what_to_display == DISPLAY_FINAL_IMAGE)
{
display_reflection_texture(
real_texture,
mirror_texture,
hero_mask_texture
);
}
if (what_to_display == DISPLAY_REAL_IMAGE)
{
display_texture(real_texture);
}
if (what_to_display == DISPLAY_MIRROR_IMAGE)
{
display_texture(mirror_texture);
}
if (what_to_display == DISPLAY_HERO_MASK_IMAGE)
{
display_texture(hero_mask_texture);
}
// Resets the VAO and the program.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindVertexArray(0);
glUseProgram(0);
// Stops measuring the elapsed time.
glEndQuery(GL_TIME_ELAPSED);
// Waits for OpenGL - don't forget OpenGL is asynchronous.
glFinish();
// Evaluates the query.
GLuint64 render_time;
glGetQueryObjectui64v(render_time_query, GL_QUERY_RESULT, &render_time);
fps_gpu = 1000.f / (static_cast<float>(render_time) * 1e-6f);
}
void Application::render_object(const SceneObject& object) const
{
default_lit_program.use();
// Handles the textures.
default_lit_program.uniform("has_texture", object.has_texture());
if (object.has_texture())
{
glBindTextureUnit(0, object.get_texture());
}
if (clipping_enabled)
{
default_lit_program.uniform("clip_plane", clip_plane);
}
// Calls the standard rendering functions.
object.get_model_ubo().bind_buffer_base(ModelUBO::DEFAULT_MODEL_BINDING);
object.get_material().bind_buffer_base(PhongMaterialUBO::DEFAULT_MATERIAL_BINDING);
object.get_geometry().bind_vao();
object.get_geometry().draw();
}
void Application::render_random_objects_and_floor()
{
for (SceneObject& object : scene_objects)
{
render_object(object);
}
}
void Application::render_mirror() const
{
render_object(mirror_frame_object);
render_object(mirror_object);
}
void Application::render_initial_scene()
{
// Renders the scene into the main window.
glBindFramebuffer(GL_FRAMEBUFFER, real_fbo);
glViewport(0, 0, width, height);
// Clears the main window.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Enables the depth test.
glEnable(GL_DEPTH_TEST);
// Sets the data of the camera and the lights
camera_ubo.bind_buffer_base(CameraUBO::DEFAULT_CAMERA_BINDING);
phong_lights_ubo.bind_buffer_base(PhongLightsUBO::DEFAULT_LIGHTS_BINDING);
// Renders the randomly generated objects and floor.
render_random_objects_and_floor();
// Renders the golem.
render_object(hero_object);
// Renders the mirror.
render_mirror();
}
void Application::render_mirror_scene()
{
// Renders the scene into the main window.
glBindFramebuffer(GL_FRAMEBUFFER, mirror_fbo);
glViewport(0, 0, width, height);
// Clears the main window.
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Enables the depth test.
glEnable(GL_DEPTH_TEST);
if (mirror_stencil_tested) glEnable(GL_STENCIL_TEST);
glEnable(GL_CLIP_DISTANCE0);
clipping_enabled = true;
// Sets the data of the camera and the lights
mirror_camera_ubo.bind_buffer_base(CameraUBO::DEFAULT_CAMERA_BINDING);
phong_lights_ubo.bind_buffer_base(PhongLightsUBO::DEFAULT_LIGHTS_BINDING);
if (mirror_stencil_tested) {
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
// Stencil operations to mark the plane's area
glStencilFunc(GL_ALWAYS, 1, 0xFF); // Always pass stencil test
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); // Replace stencil value with 1
glStencilMask(0xFF); // Enable writing to stencil buffer
}
// Renders the mirror.
render_object(mirror_object);
if (mirror_stencil_tested)
{
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
// Only render where stencil value is 1 (i.e., objects behind the mirror)
glStencilFunc(GL_EQUAL, 1, 0xFF); // Pass stencil test if value is 1
glStencilMask(0x00); // Disable stencil buffer writing
}
// Renders the randomly generated objects and floor.
render_random_objects_and_floor();
// Renders the golem.
render_object(hero_object);
if (mirror_stencil_tested)
{
glStencilFunc(GL_ALWAYS, 0, 0xFF); // Reset stencil function
glStencilMask(0xFF); // Re-enable full stencil buffer write (if needed for future use)
glDisable(GL_STENCIL_TEST); // Disable stencil test if no longer needed
}
glDisable(GL_CLIP_DISTANCE0);
clipping_enabled = false;
}
void Application::render_hero_mask_scene()
{
// Renders the scene into the main window.
glBindFramebuffer(GL_FRAMEBUFFER, hero_mask_fbo);
glViewport(0, 0, width, height);
// Clears the main window.
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
if (mirror_stencil_tested) glEnable(GL_STENCIL_TEST);
glEnable(GL_CLIP_DISTANCE0);
clipping_enabled = true;
mirror_camera_ubo.bind_buffer_base(CameraUBO::DEFAULT_CAMERA_BINDING);
if (mirror_stencil_tested)
{
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glStencilMask(0xFF); // Enable writing to the stencil buffer.
}
render_object(mirror_object);
if (mirror_stencil_tested)
{
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
glStencilMask(0x00); // Disable writing to the stencil buffer.
}
// Use the proper program
display_texture_program.use();
// Binds the proper texture.
glBindTextureUnit(0, mirror_texture);
// Renders the full screen quad to evaluate every pixel.
// Binds an empty VAO as we do not need any state.
glBindVertexArray(empty_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
if (mirror_stencil_tested)
{
// Set the stencil function to only allow rendering where the stencil value is 1 (inside the mirror).
glStencilFunc(GL_EQUAL, 1, 0xFF);
}
render_object(hero_object);
if (mirror_stencil_tested)
{
glStencilFunc(GL_ALWAYS, 0, 0xFF); // Reset stencil function
glStencilMask(0xFF); // Re-enable full stencil buffer write (if needed for future use)
glDisable(GL_STENCIL_TEST); // Disable stencil test if no longer needed
}
glDisable(GL_CLIP_DISTANCE0);
clipping_enabled = false;
}
void Application::display_texture(const GLuint& texture) const
{
// Binds the default buffer.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use the proper program
display_texture_program.use();
// Binds the proper texture.
glBindTextureUnit(0, texture);
// Renders the full screen quad to evaluate every pixel.
// Binds an empty VAO as we do not need any state.
glBindVertexArray(empty_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void Application::display_overlayed_texture(const GLuint& texture, const GLuint& overlay) const
{
// Binds the default buffer.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use the proper program
display_overlayed_texture_program.use();
// Binds the proper texture.
glBindTextureUnit(0, texture);
glBindTextureUnit(1, overlay);
// Renders the full screen quad to evaluate every pixel.
// Binds an empty VAO as we do not need any state.
glBindVertexArray(empty_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void Application::display_reflection_texture(const GLuint& texture, const GLuint& overlay, const GLuint& mask) const
{
// Binds the default buffer.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use the proper program
display_reflection_texture_program.use();
display_reflection_texture_program.uniform("blur_enabled", blur_enabled);
// Binds the proper texture.
glBindTextureUnit(0, texture);
glBindTextureUnit(1, overlay);
glBindTextureUnit(2, mask);
// Renders the full screen quad to evaluate every pixel.
// Binds an empty VAO as we do not need any state.
glBindVertexArray(empty_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
// ----------------------------------------------------------------------------
// GUI
// ----------------------------------------------------------------------------
void Application::render_ui()
{
const float unit = ImGui::GetFontSize();
ImGui::Begin("Settings");
std::string fps_cpu_string = "FPS (CPU): ";
ImGui::Text(fps_cpu_string.append(std::to_string(fps_cpu)).c_str());
std::string fps_string = "FPS (GPU): ";
ImGui::Text(fps_string.append(std::to_string(fps_gpu)).c_str());
ImGui::Combo("Display", &what_to_display, DISPLAY_LABELS, IM_ARRAYSIZE(DISPLAY_LABELS));
//ImGui::SliderInt("Gauss Size", &desired_gauss_kernel_size, 1, max_gauss_kernel_size);
//ImGui::SliderFloat("Reflectiveness", &reflectiveness, 0.0, 1.0);
ImGui::Checkbox("Auto Rotate", &auto_rotate);
ImGui::SliderAngle("Angle", &mirror_angle, 0);
ImGui::Checkbox("Mirror Stencil Test", &mirror_stencil_tested);
ImGui::Checkbox("Blurred", &blur_enabled);
ImGui::End();
}
// ----------------------------------------------------------------------------
// Input Events
// ----------------------------------------------------------------------------
void Application::on_resize(int width, int height)
{
PV227Application::on_resize(width, height);
resize_fullscreen_textures();
}