![]() |
Path Tracing
|
Main code. More...
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <vector>#include <algorithm>#include <thread>#include <atomic>#include <GL/glew.h>#include <GLFW/glfw3.h>#include <glm/glm.hpp>#include <glm/gtc/matrix_transform.hpp>#include <glm/gtc/type_ptr.hpp>#include "stb_image_write.h"#include "imgui/imgui.h"#include "imgui/imgui_impl_glfw.h"#include "imgui/imgui_impl_opengl3.h"#include "common/shader.hpp"#include "mesh.hpp"#include "bvh.hpp"#include "config.hpp"#include "loader.hpp"#include "texture.hpp"#include "denoiser.hpp"
Classes | |
| struct | Metrics |
Macros | |
| #define | GLEW_NO_GLU |
| #define | STB_IMAGE_WRITE_IMPLEMENTATION |
| #define | VERSION "1.8.1" |
| #define | VERSION_NOTE "" |
| #define | WINDOW_TITLE "Path Tracer" |
| #define | WINDOW_TITLE_VERSION WINDOW_TITLE " v" VERSION " " VERSION_NOTE |
Functions | |
| void | onKeyPress (GLFWwindow *, int key, int, int action, int) |
| void | onMouseMove (GLFWwindow *, double x, double y) |
| void | onMouseButton (GLFWwindow *w, int button, int action, int) |
| void | onMouseScroll (GLFWwindow *w, double, double yoffset) |
| void | onWindowResize (GLFWwindow *, int width, int height) |
| void | toggleFullscreen () |
| vec3 | cameraForward () |
| void | zoomToFit () |
| Repositions the camera to frame the model on scene. | |
| void | setPreviewResolution () |
| Switches the render resolution to the low-res mode. | |
| void | setFullResolution () |
| Switches the render resolution to full render mode. | |
| void | setPreviewMode (bool enabled) |
| Toggle Preview Mode Pins the renderer at the low preview resolution and stops it from auto-upgrading to full res until disabling and starting a fresh render. | |
| void | processMovement () |
| Process WASD movement. | |
| void | formatTime (double seconds, char *buf, int buf_size) |
| Formats a duration in seconds as "Xs" or "XmYs". | |
| bool | initShaders () |
| void | reloadShaders () |
| void | initUniforms () |
| Caches every uniform location of the active shader into Renderer. | |
| void | loadScene () |
| Loads the scene and imports the model on a secondary thread. | |
| void | resetModel () |
| void | uploadConfig () |
| Updates every RenderConfig parameter, called when any changed. | |
| void | applyConfig () |
| Calls uploadConfig and resets the scene. | |
| void | uploadCamera () |
| void | uploadCornellTopLight () |
| void | uploadPrismDemoLight () |
| void | uploadSun () |
| bool | transferDataToGPU (void) |
| First time GPU setup. | |
| void | cleanDataFromGPU () |
| void | display (void) |
| void | clearTextures () |
| void | setDenoiseCheckpoints () |
| Computes at which samples the denoiser should run. | |
| void | createDenoisedTex (int w, int h) |
| Allocates the denoised output texture at a width w and height h. | |
| void | resetAccumulation () |
| Restarts progressive accumulation back to 0. | |
| void | draw (void) |
| Renders one full frame in the order: path trace, denoise, autoEV, display, grid, UI. | |
| void | saveScreenshot () |
| Takes a screenshot of the render. | |
| float | computeTargetExposureEV () |
| void | updateAutoExposure (float speed) |
| void | drawGrid () |
| Renders the world view grid. | |
| void | drawCompositionGuides () |
| Draws photography-style framing guides and an aspect-ratio crop preview mask over the viewport Doesn't touch the shader, render or camera, only a viewfinder to position the camera. | |
| void | syncResolutionDropdown () |
| Syncs the UI to the current window size. | |
| void | drawUI () |
| void | applyResolution (int w, int h) |
| bool | useMoveKeys () |
| Checks if the user pressed the WASD keys. | |
| int | main (void) |
| void | applyLensPreset (int idx) |
| Copies a LensPreset into config and applies it. | |
| vector< string > | scanModels (const string &models_dir) |
| Recursively lists supported model files under the /models directory. | |
| vector< string > | scanHDRI (const string &hdri_dir) |
| Recursively lists HDRI files under the /hdri directory. | |
| void | runDenoiser (int checkpoint_num, int total) |
| Reads back the accumulation texture, runs OIDN and sends the result. | |
| glm::vec3 | displayPixelCPU (glm::vec3 linear_color, float alpha, glm::vec2 uv, float exposure_ev, int tone_mapping, float vignette_strength) |
| Mirror of display.frag's per-pixel pipeline exposure → tone map + gamma → vignette → composite. | |
| void | computeAspectCropRect (int frame_w, int frame_h, int aspect_frame, int &out_x0, int &out_y0, int &out_w, int &out_h) |
| void | addContrastLine (ImDrawList *dl, ImVec2 p1, ImVec2 p2, float thickness) |
| Draws a line that shows clearly in dark and light backgrounds. | |
Variables | |
| GLFWwindow * | window |
| Renderer | renderer |
| CameraConfig | camera |
| Denoiser | denoiser |
| double | mouse_last_x = 0.0 |
| double | mouse_last_y = 0.0 |
| bool | mouse_first = true |
| bool | mouse_captured = false |
| Metrics | metrics |
| RenderConfig | config |
| const char * | txt_sep = "---------------------------------------------" |
| vector< GPULight > | analytic_lights |
| int | sun_light_index = -1 |
| int | current_res_idx = 0 |
| const int | res_w [] = {1280, 1920, 2560, 4096, 0, 0} |
| const int | res_h [] = {720, 1080, 1440, 2160, 0, 0} |
Main code.
| #define GLEW_NO_GLU |
| #define STB_IMAGE_WRITE_IMPLEMENTATION |
| #define VERSION "1.8.1" |
| #define VERSION_NOTE "" |
| #define WINDOW_TITLE "Path Tracer" |
| #define WINDOW_TITLE_VERSION WINDOW_TITLE " v" VERSION " " VERSION_NOTE |
| void addContrastLine | ( | ImDrawList * | dl, |
| ImVec2 | p1, | ||
| ImVec2 | p2, | ||
| float | thickness ) |
Draws a line that shows clearly in dark and light backgrounds.
| void applyConfig | ( | ) |
Calls uploadConfig and resets the scene.
| void applyLensPreset | ( | int | idx | ) |
Copies a LensPreset into config and applies it.
| void applyResolution | ( | int | w, |
| int | h ) |
| vec3 cameraForward | ( | ) |
| void cleanDataFromGPU | ( | ) |
| void clearTextures | ( | ) |
|
inline |
| float computeTargetExposureEV | ( | ) |
subsampling area, defines an estimate average for performance Not a fixed number because different resolutions might not apply well So adaptive stride looks for a constant sample regardless of resolution or Render Mode
pixels below this are excluded
standart photographic middle-gray target, 18%
| void createDenoisedTex | ( | int | w, |
| int | h ) |
Allocates the denoised output texture at a width w and height h.
|
inline |
Mirror of display.frag's per-pixel pipeline exposure → tone map + gamma → vignette → composite.
| linear_color | raw HDR color sampled from render texture |
| alpha | render texture's alpha channel |
| uv | pixel-center position in the frame[0,1] |
Renders one full frame in the order: path trace, denoise, autoEV, display, grid, UI.
| void drawCompositionGuides | ( | ) |
Draws photography-style framing guides and an aspect-ratio crop preview mask over the viewport Doesn't touch the shader, render or camera, only a viewfinder to position the camera.
| void drawGrid | ( | ) |
Renders the world view grid.
| void drawUI | ( | ) |
Environment Map HDRI
See external window for env map selector, outside of the side windows
| void formatTime | ( | double | seconds, |
| char * | buf, | ||
| int | buf_size ) |
Formats a duration in seconds as "Xs" or "XmYs".
| bool initShaders | ( | ) |
Compiles and links all shaders
| void loadScene | ( | ) |
Loads the scene and imports the model on a secondary thread.
| int main | ( | void | ) |
| void onKeyPress | ( | GLFWwindow * | , |
| int | key, | ||
| int | , | ||
| int | action, | ||
| int | ) |
| void onMouseButton | ( | GLFWwindow * | w, |
| int | button, | ||
| int | action, | ||
| int | ) |
| void onMouseMove | ( | GLFWwindow * | , |
| double | x, | ||
| double | y ) |
| void onMouseScroll | ( | GLFWwindow * | w, |
| double | , | ||
| double | yoffset ) |
Simulate Unity's camera control speed multiplier Use SHIFT + Mouse Scroll to change camera speed
| void onWindowResize | ( | GLFWwindow * | , |
| int | width, | ||
| int | height ) |
| void processMovement | ( | ) |
Process WASD movement.
| void reloadShaders | ( | ) |
| void resetAccumulation | ( | ) |
Restarts progressive accumulation back to 0.
| void resetModel | ( | ) |
| void runDenoiser | ( | int | checkpoint_num, |
| int | total ) |
Reads back the accumulation texture, runs OIDN and sends the result.
| checkpoint_num | index of checkpoint |
| total | Total number of checkpoints |
| void saveScreenshot | ( | ) |
Takes a screenshot of the render.
Recursively lists HDRI files under the /hdri directory.
| hdri_dir | Directory to scan |
Recursively lists supported model files under the /models directory.
| models_dir | Directory to scan |
| void setDenoiseCheckpoints | ( | ) |
Computes at which samples the denoiser should run.
| void setFullResolution | ( | ) |
Switches the render resolution to full render mode.
| void setPreviewMode | ( | bool | enabled | ) |
Toggle Preview Mode Pins the renderer at the low preview resolution and stops it from auto-upgrading to full res until disabling and starting a fresh render.
| void setPreviewResolution | ( | ) |
Switches the render resolution to the low-res mode.
| void syncResolutionDropdown | ( | ) |
Syncs the UI to the current window size.
| void toggleFullscreen | ( | ) |
| bool transferDataToGPU | ( | void | ) |
First time GPU setup.
| void updateAutoExposure | ( | float | speed | ) |
Handler for computeTargetExposureEV, gradually changes EV value based on speed
| speed | How quick the EV change applies, 0 = frozen, 1 = instant, 0-1 = gradual |
| void uploadCamera | ( | ) |
| void uploadConfig | ( | ) |
Updates every RenderConfig parameter, called when any changed.
| void uploadCornellTopLight | ( | ) |
Brute-forced way to set the Cornell's top light to follow the light types emissions, since now plain geometry aren't considered LIGHT types
| void uploadPrismDemoLight | ( | ) |
| void uploadSun | ( | ) |
| bool useMoveKeys | ( | ) |
Checks if the user pressed the WASD keys.
| void zoomToFit | ( | ) |
Repositions the camera to frame the model on scene.
| vector<GPULight> analytic_lights |
| CameraConfig camera |
| RenderConfig config |
|
inline |
| Denoiser denoiser |
| Metrics metrics |
| bool mouse_captured = false |
| bool mouse_first = true |
| double mouse_last_x = 0.0 |
| double mouse_last_y = 0.0 |
| Renderer renderer |
| const int res_h[] = {720, 1080, 1440, 2160, 0, 0} |
| const int res_w[] = {1280, 1920, 2560, 4096, 0, 0} |
| int sun_light_index = -1 |
| const char* txt_sep = "---------------------------------------------" |
| GLFWwindow* window |