|
| 1 | +#include "application.h" |
| 2 | +#include <filesystem> |
| 3 | + |
| 4 | +void Application::init(Systems::RendererSettings settings, Systems::ForwardRendererSettings settings2) { |
| 5 | + m_window = new WindowGLFW("Raytracing Example", 1280, 1024); |
| 6 | + |
| 7 | + m_window->init(); |
| 8 | + |
| 9 | + m_window->set_window_size_callback( |
| 10 | + std::bind(&Application::window_resize_callback, this, std::placeholders::_1, std::placeholders::_2)); |
| 11 | + m_window->set_mouse_callback( |
| 12 | + std::bind(&Application::mouse_callback, this, std::placeholders::_1, std::placeholders::_2)); |
| 13 | + m_window->set_key_callback(std::bind(&Application::keyboard_callback, |
| 14 | + this, |
| 15 | + std::placeholders::_1, |
| 16 | + std::placeholders::_2, |
| 17 | + std::placeholders::_3, |
| 18 | + std::placeholders::_4)); |
| 19 | + |
| 20 | + m_renderer = new Systems::ForwardRenderer(m_window, settings, settings2); |
| 21 | + |
| 22 | + setup(); |
| 23 | + setup_gui(); |
| 24 | +} |
| 25 | + |
| 26 | +void Application::run(int argc, char* argv[]) { |
| 27 | + |
| 28 | + Systems::RendererSettings settings{}; |
| 29 | + settings.samplesMSAA = MSAASamples::_NONE; |
| 30 | + settings.clearColor = Vec4(0.02, 0.02, 0.02, 1.0); |
| 31 | + settings.enableUI = true; |
| 32 | + settings.enableRaytracing = true; |
| 33 | + Systems::ForwardRendererSettings settings2{}; |
| 34 | + settings2.shadowQuality = ShadowResolution::MEDIUM; |
| 35 | + settings2.fxaa = true; |
| 36 | + |
| 37 | + init(settings, settings2); |
| 38 | + while (!m_window->get_window_should_close()) |
| 39 | + { |
| 40 | + |
| 41 | + // I-O |
| 42 | + m_window->poll_events(); |
| 43 | + |
| 44 | + tick(); |
| 45 | + } |
| 46 | + m_renderer->shutdown(m_scene); |
| 47 | +} |
| 48 | + |
| 49 | +void Application::setup() { |
| 50 | + const std::string MESH_PATH(EXAMPLES_RESOURCES_PATH "meshes/"); |
| 51 | + const std::string TEXTURE_PATH(EXAMPLES_RESOURCES_PATH "textures/"); |
| 52 | + const std::string ENGINE_MESH_PATH(ENGINE_RESOURCES_PATH "meshes/"); |
| 53 | + |
| 54 | + camera = new Camera(); |
| 55 | + camera->set_position(Vec3(0.0f, 0.0f, -5.0f)); |
| 56 | + camera->set_far(100.0f); |
| 57 | + camera->set_near(0.1f); |
| 58 | + camera->set_field_of_view(70.0f); |
| 59 | + |
| 60 | + m_scene = new Scene(camera); |
| 61 | + |
| 62 | + Mesh* lightDummy = new Mesh(); |
| 63 | + Tools::Loaders::load_3D_file(lightDummy, ENGINE_MESH_PATH + "sphere.obj"); |
| 64 | + lightDummy->push_material(new UnlitMaterial()); |
| 65 | + lightDummy->set_scale(0.5f); |
| 66 | + lightDummy->set_name("Gizmo"); |
| 67 | + |
| 68 | + PointLight* light = new PointLight(); |
| 69 | + light->set_position({-3.0f, 3.0f, 0.0f}); |
| 70 | + light->set_area_of_effect(20.0f); |
| 71 | + light->set_shadow_fov(115.0f); |
| 72 | + light->add_child(lightDummy); |
| 73 | + light->set_name("Light"); |
| 74 | + |
| 75 | + m_scene->add(light); |
| 76 | + |
| 77 | + Mesh* toriiMesh = new Mesh(); |
| 78 | + auto toriiMat = new PhysicallyBasedMaterial(); |
| 79 | + |
| 80 | + Texture* toriiT = new Texture(); |
| 81 | + Tools::Loaders::load_texture(toriiT, TEXTURE_PATH + "torii_color.png"); |
| 82 | + toriiMat->set_albedo_texture(toriiT); |
| 83 | + |
| 84 | + Texture* toriiN = new Texture(); |
| 85 | + Tools::Loaders::load_texture(toriiN, TEXTURE_PATH + "torii_normal.png", TextureFormatType::NORMAL_FORMAT); |
| 86 | + toriiMat->set_normal_texture(toriiN); |
| 87 | + |
| 88 | + Texture* toriiM = new Texture(); |
| 89 | + // Tools::Loaders::load_texture(toriiM, TEXTURE_PATH + "torii_mask.png"); |
| 90 | + // toriiMat->set_mask_texture(toriiM, UNREAL_ENGINE); |
| 91 | + toriiMat->set_metalness(0.05); |
| 92 | + toriiMat->set_roughness(0.5); |
| 93 | + toriiMesh->push_material(toriiMat); |
| 94 | + |
| 95 | + Tools::Loaders::load_3D_file(toriiMesh, MESH_PATH + "torii.obj", false); |
| 96 | + toriiMesh->set_name("Torii"); |
| 97 | + toriiMesh->set_scale(0.2f); |
| 98 | + toriiMesh->set_position({0.0, -2.3, 0.0}); |
| 99 | + toriiMesh->set_rotation({0.0, 28.0f, 0.0f}); |
| 100 | + m_scene->add(toriiMesh); |
| 101 | + |
| 102 | + Mesh* plane = new Mesh(); |
| 103 | + plane->push_geometry(Geometry::create_quad()); |
| 104 | + auto terrainMat = new PhysicallyBasedMaterial(); |
| 105 | + Texture* floorText = new Texture(); |
| 106 | + Tools::Loaders::load_texture(floorText, TEXTURE_PATH + "floor_diffuse.jpg"); |
| 107 | + Texture* floorNormalText = new Texture(); |
| 108 | + Tools::Loaders::load_texture(floorNormalText, TEXTURE_PATH + "floor_normal.jpg", TextureFormatType::NORMAL_FORMAT); |
| 109 | + Texture* floorRoughText = new Texture(); |
| 110 | + Tools::Loaders::load_texture(floorRoughText, TEXTURE_PATH + "floor_roughness.jpg"); |
| 111 | + terrainMat->set_albedo({0.43f, 0.28f, 0.23f}); |
| 112 | + terrainMat->set_albedo_texture(floorText); |
| 113 | + terrainMat->set_normal_texture(floorNormalText); |
| 114 | + terrainMat->set_roughness_texture(floorRoughText); |
| 115 | + terrainMat->set_tile({3.0f, 3.0f}); |
| 116 | + plane->push_material(terrainMat); |
| 117 | + plane->set_name("Floor"); |
| 118 | + plane->set_position({0.0, -2.3, 0.0}); |
| 119 | + plane->set_rotation({-90.0f, 0.0f, 0.0f}); |
| 120 | + plane->set_scale(5.0f); |
| 121 | + |
| 122 | + m_scene->add(plane); |
| 123 | + |
| 124 | + m_scene->set_ambient_color({0.2, 0.25, 0.61}); |
| 125 | + |
| 126 | + TextureHDR* envMap = new TextureHDR(); |
| 127 | + Tools::Loaders::load_texture(envMap, TEXTURE_PATH + "cloudy.hdr"); |
| 128 | + Skybox* sky = new Skybox(envMap); |
| 129 | + sky->set_color_intensity(0.25f); |
| 130 | + m_scene->set_skybox(sky); |
| 131 | + |
| 132 | + m_controller = new Tools::Controller(camera, m_window); |
| 133 | +} |
| 134 | + |
| 135 | +void Application::setup_gui() { |
| 136 | + m_interface.overlay = new Tools::GUIOverlay( |
| 137 | + (float)m_window->get_extent().width, (float)m_window->get_extent().height, GuiColorProfileType::DARK); |
| 138 | + |
| 139 | + Tools::Panel* tutorialPanel = |
| 140 | + new Tools::Panel("TUTORIAL", 0, 0.8f, 0.2f, 0.2f, PanelWidgetFlags::NoMove, false, true); |
| 141 | + |
| 142 | + tutorialPanel->add_child(new Tools::Space()); |
| 143 | + tutorialPanel->add_child(new Tools::Separator("CONTROLS")); |
| 144 | + tutorialPanel->add_child(new Tools::Separator()); |
| 145 | + tutorialPanel->add_child(new Tools::TextLine("WASD: move camera.", TextWidgetType::BULLET)); |
| 146 | + tutorialPanel->add_child(new Tools::TextLine("QE: camera down/up.", TextWidgetType::BULLET)); |
| 147 | + tutorialPanel->add_child(new Tools::TextLine("Mouse + Left: rotate camera.", TextWidgetType::BULLET)); |
| 148 | + tutorialPanel->add_child(new Tools::TextLine("L: toggle light animation", TextWidgetType::BULLET)); |
| 149 | + tutorialPanel->add_child(new Tools::TextLine("F11: toggle fullscreen/windowed mode.", TextWidgetType::BULLET)); |
| 150 | + tutorialPanel->add_child(new Tools::TextLine("Esc: exit application.", TextWidgetType::BULLET)); |
| 151 | + tutorialPanel->add_child(new Tools::Space()); |
| 152 | + tutorialPanel->add_child(new Tools::Separator()); |
| 153 | + tutorialPanel->add_child(new Tools::TextLine("Enjoy changing the parameters!")); |
| 154 | + |
| 155 | + m_interface.overlay->add_panel(tutorialPanel); |
| 156 | + m_interface.tutorial = tutorialPanel; |
| 157 | + |
| 158 | + Tools::Panel* explorerPanel = new Tools::Panel("EXPLORER", 0, 0, 0.2f, 0.7f, PanelWidgetFlags::NoMove, false); |
| 159 | + m_interface.scene = new Tools::SceneExplorerWidget(m_scene); |
| 160 | + explorerPanel->add_child(m_interface.scene); |
| 161 | + explorerPanel->add_child(new Tools::Space()); |
| 162 | + explorerPanel->add_child(new Tools::RendererSettingsWidget(m_renderer)); |
| 163 | + explorerPanel->add_child(new Tools::Separator()); |
| 164 | + explorerPanel->add_child(new Tools::TextLine(" Application average")); |
| 165 | + explorerPanel->add_child(new Tools::Profiler()); |
| 166 | + explorerPanel->add_child(new Tools::Space()); |
| 167 | + |
| 168 | + m_interface.overlay->add_panel(explorerPanel); |
| 169 | + m_interface.explorer = explorerPanel; |
| 170 | + |
| 171 | + Tools::Panel* propertiesPanel = |
| 172 | + new Tools::Panel("OBJECT PROPERTIES", 0.75f, 0, 0.25f, 0.8f, PanelWidgetFlags::NoMove, true); |
| 173 | + m_interface.object = new Tools::ObjectExplorerWidget(); |
| 174 | + propertiesPanel->add_child(m_interface.object); |
| 175 | + |
| 176 | + m_interface.overlay->add_panel(propertiesPanel); |
| 177 | + m_interface.properties = propertiesPanel; |
| 178 | + |
| 179 | + // m_renderer->set_gui_overlay(m_interface.overlay); |
| 180 | +} |
| 181 | + |
| 182 | +void Application::update() { |
| 183 | + if (!m_interface.overlay->wants_to_handle_input()) |
| 184 | + m_controller->handle_keyboard(0, 0, m_time.delta); |
| 185 | + |
| 186 | + // Rotate the vector around the ZX plane |
| 187 | + auto light = m_scene->get_lights()[0]; |
| 188 | + if (animateLight) |
| 189 | + { |
| 190 | + float rotationAngle = glm::radians(10.0f * m_time.delta); |
| 191 | + float _x = light->get_position().x * cos(rotationAngle) - light->get_position().z * sin(rotationAngle); |
| 192 | + float _z = light->get_position().x * sin(rotationAngle) + light->get_position().z * cos(rotationAngle); |
| 193 | + |
| 194 | + light->set_position({_x, light->get_position().y, _z}); |
| 195 | + } |
| 196 | + |
| 197 | + m_interface.object->set_object(m_interface.scene->get_selected_object()); |
| 198 | +} |
| 199 | + |
| 200 | +void Application::tick() { |
| 201 | + float currentTime = (float)m_window->get_time_elapsed(); |
| 202 | + m_time.delta = currentTime - m_time.last; |
| 203 | + m_time.last = currentTime; |
| 204 | + m_time.framesPerSecond = 1.0f / m_time.delta; |
| 205 | + |
| 206 | + update(); |
| 207 | + |
| 208 | + m_interface.overlay->render(); |
| 209 | + m_renderer->render(m_scene); |
| 210 | +} |
0 commit comments