diff --git a/Makefile b/Makefile index 0763ec4..a3e10e4 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ else RM := rm -rf DIR_DUP = mkdir -p $(@D) CC := clang++ - CFLAGS := -Ofast -Wall -Wextra -Werror + CFLAGS := -Ofast -Wall -Wextra -Werror -g IFLAGS := -I./includes -I./includes/RT -I/usr/include LDFLAGS := -L/usr/lib/x86_64-linux-gnu -lglfw -lGL -lGLU -lX11 -lpthread -ldl -lstdc++ FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l) diff --git a/includes/RT.hpp b/includes/RT.hpp index 14501c8..3a7c4cf 100644 --- a/includes/RT.hpp +++ b/includes/RT.hpp @@ -22,6 +22,7 @@ # include "glad/gl.h" # include "GLFW/glfw3.h" +# include "ShadInclude.hpp" # include # include diff --git a/shaders/compute.glsl b/shaders/compute.glsl index 2c59680..4be4df4 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -1,4 +1,5 @@ #version 430 core +#include "shaders/utils.glsl" // Work group dimensions layout(local_size_x = 16, local_size_y = 16) in; @@ -30,21 +31,6 @@ uniform int u_frameCount; vec3 lightPos = vec3(5.0, 5.0, 5.0); vec3 lightColor = vec3(1.0, 1.0, 1.0); - -const float PHI = 1.61803398874989484820459; // Φ = Golden Ratio -float getRandom(in vec2 xy, float seed) -{ - return fract(tan(distance(xy*PHI, xy)*seed)*xy.x); -} - -vec3 randomVec3(vec2 uv, int frame) -{ - float x = getRandom(uv, float(frame) + 0.1); - float y = getRandom(uv + vec2(1.0), float(frame) + 0.2); - float z = getRandom(uv + vec2(2.0), float(frame) + 0.3); - return vec3(x, y, z); -} - struct Ray { vec3 origin; vec3 direction; @@ -113,6 +99,6 @@ void main() { vec3 color = pathtrace(ray); - // Write to the output image imageStore(outputImage, pixelCoords, vec4(randomVec3(uv, u_frameCount), 1.0)); -} \ No newline at end of file +} + diff --git a/shaders/utils.glsl b/shaders/utils.glsl new file mode 100644 index 0000000..c009a05 --- /dev/null +++ b/shaders/utils.glsl @@ -0,0 +1,15 @@ + +float seed = 1.0; +float getRandom(vec2 uv, int frameCount) +{ + float seed = dot(uv, vec2(12.9898, 78.233)) + float(frameCount); + return fract(sin(seed) * 43758.5453); +} +vec3 randomVec3(vec2 uv, int frameCount) +{ + return vec3( + getRandom(uv + vec2(0.1, 0.1), frameCount), + getRandom(uv + vec2(0.2, 0.2), frameCount), + getRandom(uv + vec2(0.3, 0.3), frameCount) + ); +} \ No newline at end of file diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 8350bb9..62c72c9 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -70,7 +70,7 @@ int main(int argc, char **argv) glUseProgram(shader.getProgram()); shader.drawTriangles(size); - std::cout << "\rFPS: " << int(window.getFps()) << " " << std::flush; + std::cout << "\rFrame: " << window.getFrameCount() << " Fps: " << int(window.getFps()) << " " << std::flush; window.display(); window.pollEvents(); diff --git a/srcs/class/Shader.cpp b/srcs/class/Shader.cpp index f883950..7caddf7 100644 --- a/srcs/class/Shader.cpp +++ b/srcs/class/Shader.cpp @@ -12,37 +12,48 @@ #include "Shader.hpp" -char* load_file(char const* path) +#include +#include +#include + +const char *loadFileWithIncludes(const std::string& path) { - char* buffer = 0; - long length = 0; - FILE * f = fopen (path, "rb"); + std::ifstream file(path); + if (!file.is_open()) { + std::cerr << "Failed to open file: " << path << std::endl; + return ""; + } - if (f) + std::stringstream fileContent; + std::string line; + + while (std::getline(file, line)) { - fseek (f, 0, SEEK_END); - length = ftell (f); - fseek (f, 0, SEEK_SET); - buffer = (char*)malloc ((length+1)*sizeof(char)); - if (buffer) + if (line.rfind("#include", 0) == 0) { - fread (buffer, sizeof(char), length, f); - } - fclose (f); - } - else - return (NULL); - buffer[length] = '\0'; + size_t start = line.find_first_of("\"<"); + size_t end = line.find_last_of("\">"); + if (start != std::string::npos && end != std::string::npos && end > start) + { + std::string includePath = line.substr(start + 1, end - start - 1); + std::string includedContent = loadFileWithIncludes(includePath); + fileContent << includedContent << "\n"; + } + } + else + fileContent << line << "\n"; + } - return buffer; + return strdup(fileContent.str().c_str()); } + Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string computePath) { - const char *vertexCode = load_file(vertexPath.c_str()); - const char *fragmentCode = load_file(fragmentPath.c_str()); - const char *computeCode = load_file(computePath.c_str()); - + const char *vertexCode = loadFileWithIncludes(vertexPath); + const char *fragmentCode = loadFileWithIncludes(fragmentPath); + const char *computeCode = loadFileWithIncludes(computePath); + _vertex = glCreateShader(GL_VERTEX_SHADER); glShaderSource(_vertex, 1, &vertexCode, NULL); diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 0d8a8f6..feee016 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -15,6 +15,7 @@ Window::Window(Scene *scene, int width, int height, const char *title, int sleep) { _scene = scene; + _frameCount = 0; if (!glfwInit()) {