+ | Shader code include system

This commit is contained in:
TheRedShip
2024-12-27 16:00:39 +01:00
parent dfc37301e4
commit c25c337d1f
7 changed files with 55 additions and 41 deletions

View File

@ -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)

View File

@ -22,6 +22,7 @@
# include "glad/gl.h"
# include "GLFW/glfw3.h"
# include "ShadInclude.hpp"
# include <iostream>
# include <fstream>

View File

@ -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));
}

15
shaders/utils.glsl Normal file
View File

@ -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)
);
}

View File

@ -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();

View File

@ -12,36 +12,47 @@
#include "Shader.hpp"
char* load_file(char const* path)
{
char* buffer = 0;
long length = 0;
FILE * f = fopen (path, "rb");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
if (f)
{
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = (char*)malloc ((length+1)*sizeof(char));
if (buffer)
{
fread (buffer, sizeof(char), length, f);
const char *loadFileWithIncludes(const std::string& path)
{
std::ifstream file(path);
if (!file.is_open()) {
std::cerr << "Failed to open file: " << path << std::endl;
return "";
}
std::stringstream fileContent;
std::string line;
while (std::getline(file, line))
{
if (line.rfind("#include", 0) == 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";
}
fclose (f);
}
else
return (NULL);
buffer[length] = '\0';
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);

View File

@ -15,6 +15,7 @@
Window::Window(Scene *scene, int width, int height, const char *title, int sleep)
{
_scene = scene;
_frameCount = 0;
if (!glfwInit())
{