mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
+ | Shader reloading working
This commit is contained in:
5
Makefile
5
Makefile
@ -46,9 +46,10 @@ IMGUI_SRCS := imgui/imgui.cpp \
|
||||
imgui/imgui_impl_glfw.cpp \
|
||||
imgui/imgui_impl_opengl3.cpp
|
||||
|
||||
ALL_SRCS := $(IMGUI_SRCS) \
|
||||
RT.cpp gl.cpp \
|
||||
ALL_SRCS := $(IMGUI_SRCS) gl.cpp \
|
||||
RT.cpp RT_utils.cpp \
|
||||
class/Window.cpp \
|
||||
class/ShaderProgram.cpp \
|
||||
class/Shader.cpp \
|
||||
class/Camera.cpp \
|
||||
class/Scene.cpp \
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/13 18:10:10 by TheRed #+# #+# */
|
||||
/* Updated: 2025/02/02 19:42:13 by ycontre ### ########.fr */
|
||||
/* Updated: 2025/02/13 19:10:11 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -26,51 +26,7 @@ class Shader
|
||||
|
||||
GLuint getShader(void) const;
|
||||
|
||||
void attach(void);
|
||||
void setupVertexBuffer();
|
||||
void drawTriangles();
|
||||
|
||||
void flipOutputDenoising(bool pass);
|
||||
|
||||
// void setBool(const std::string &name, bool value) const;
|
||||
void set_int(const std::string &name, int value) const;
|
||||
void set_float(const std::string &name, float value) const;
|
||||
void set_vec2(const std::string &name, const glm::vec2 &value) const;
|
||||
void set_vec3(const std::string &name, const glm::vec3 &value) const;
|
||||
// void setVec4(const std::string &name, const RT::Vec4f &value) const;
|
||||
void set_mat4(const std::string &name, const glm::mat4 &value) const;
|
||||
|
||||
void set_textures(std::vector<GLuint> texture_ids, std::vector<GLuint> emissive_texture_ids);
|
||||
|
||||
GLuint getProgramCompute(void) const;
|
||||
GLuint getProgramComputeDenoising(void) const;
|
||||
|
||||
GLuint getNormalTexture(void) const;
|
||||
GLuint getPositionTexture(void) const;
|
||||
|
||||
std::vector<float> getOutputImage(void);
|
||||
|
||||
|
||||
private:
|
||||
GLuint _screen_VAO, _screen_VBO;
|
||||
|
||||
GLuint _program;
|
||||
GLuint _program_compute;
|
||||
GLuint _program_denoising;
|
||||
|
||||
GLuint _output_texture;
|
||||
GLuint _accumulation_texture;
|
||||
GLuint _denoising_texture;
|
||||
GLuint _normal_texture;
|
||||
GLuint _position_texture;
|
||||
|
||||
GLuint _vertex;
|
||||
GLuint _fragment;
|
||||
GLuint _compute;
|
||||
GLuint _denoising;
|
||||
|
||||
size_t _size;
|
||||
|
||||
void checkCompileErrors();
|
||||
|
||||
//
|
||||
|
@ -3,10 +3,10 @@
|
||||
/* ::: :::::::: */
|
||||
/* ShaderProgram.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/12 22:22:17 by TheRed #+# #+# */
|
||||
/* Updated: 2025/02/12 22:22:17 by TheRed ### ########.fr */
|
||||
/* Updated: 2025/02/13 19:10:15 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -22,6 +22,8 @@ class ShaderProgram
|
||||
~ShaderProgram(void);
|
||||
|
||||
void attachShader(Shader *shader);
|
||||
void clearShaders();
|
||||
|
||||
void link(void);
|
||||
|
||||
void use(void) const;
|
||||
@ -34,8 +36,9 @@ class ShaderProgram
|
||||
void set_int(const std::string &name, int value) const;
|
||||
void set_float(const std::string &name, float value) const;
|
||||
void set_vec2(const std::string &name, const glm::vec2 &value) const;
|
||||
|
||||
|
||||
|
||||
void set_textures(std::map<std::string, std::vector<GLuint>> texture_ids);
|
||||
|
||||
GLuint getProgram(void) const;
|
||||
|
||||
private:
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
layout(local_size_x = 16, local_size_y = 16) in;
|
||||
layout(binding = 0, rgba32f) uniform image2D output_image;
|
||||
layout(binding = 1, rgba32f) uniform image2D accumulation_image;
|
||||
|
||||
struct GPUCamera
|
||||
{
|
||||
@ -121,6 +120,7 @@ struct hitInfo
|
||||
vec3 position;
|
||||
int obj_index;
|
||||
int mat_index;
|
||||
int obj_type;
|
||||
|
||||
float u;
|
||||
float v;
|
||||
@ -238,16 +238,27 @@ hitInfo traverseBVHs(Ray ray, inout Stats stats)
|
||||
|
||||
hitInfo temp_hit = traceBVH(transformedRay, BvhData[i], stats);
|
||||
|
||||
temp_hit.t = temp_hit.t / bvh_data.scale;
|
||||
|
||||
if (temp_hit.t < hit.t)
|
||||
float transformed_t = temp_hit.t / bvh_data.scale;
|
||||
if (transformed_t < hit.t)
|
||||
{
|
||||
hit.t = temp_hit.t;
|
||||
GPUTriangle triangle = triangles[temp_hit.obj_index];
|
||||
|
||||
hit.u = temp_hit.u;
|
||||
hit.v = temp_hit.v;
|
||||
hit.t = transformed_t;
|
||||
hit.obj_index = temp_hit.obj_index;
|
||||
hit.normal = normalize(inverseTransformMatrix * temp_hit.normal);
|
||||
hit.mat_index = triangle.mat_index;
|
||||
|
||||
vec3 position = transformedRay.origin + transformedRay.direction * temp_hit.t;
|
||||
hit.position = inverseTransformMatrix * position + bvh_data.offset;
|
||||
|
||||
vec3 based_normal = triangle.normal * sign(-dot(transformedRay.direction, triangle.normal));
|
||||
hit.normal = normalize(inverseTransformMatrix * based_normal);
|
||||
}
|
||||
}
|
||||
|
||||
hit.obj_type = 3;
|
||||
|
||||
return (hit);
|
||||
}
|
||||
|
||||
@ -294,7 +305,7 @@ void main()
|
||||
|
||||
vec2 uv = ((vec2(pixel_coords)) / u_resolution) * 2.0 - 1.0;;
|
||||
uv.x *= u_resolution.x / u_resolution.y;
|
||||
|
||||
|
||||
vec3 color = debugColor(uv);
|
||||
|
||||
imageStore(output_image, pixel_coords, vec4(color, 1.));
|
||||
|
28
srcs/RT.cpp
28
srcs/RT.cpp
@ -6,7 +6,7 @@
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
|
||||
/* Updated: 2025/02/06 18:02:18 by ycontre ### ########.fr */
|
||||
/* Updated: 2025/02/13 19:22:02 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -38,15 +38,11 @@ int main(int argc, char **argv)
|
||||
|
||||
ShaderProgram raytracing_program;
|
||||
Shader compute = Shader(GL_COMPUTE_SHADER, "shaders/compute.glsl");
|
||||
Shader debug = Shader(GL_COMPUTE_SHADER, "shaders/debug.glsl");
|
||||
|
||||
raytracing_program.attachShader(&compute);
|
||||
raytracing_program.link();
|
||||
|
||||
raytracing_program.use();
|
||||
// raytracing_program.bindImageTexture(output_texture, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
// raytracing_program.bindImageTexture(textures[1], 1, GL_READ_WRITE, GL_RGBA32F);
|
||||
|
||||
|
||||
ShaderProgram render_program;
|
||||
Shader vertex = Shader(GL_VERTEX_SHADER, "shaders/vertex.vert");
|
||||
Shader frag = Shader(GL_FRAGMENT_SHADER, "shaders/frag.frag");
|
||||
@ -57,12 +53,24 @@ int main(int argc, char **argv)
|
||||
|
||||
std::vector<Buffer *> buffers = createDataOnGPU(scene);
|
||||
|
||||
if (!scene.loadTextures())
|
||||
return (-1);
|
||||
|
||||
while (!window.shouldClose())
|
||||
{
|
||||
window.updateDeltaTime();
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
updateDataOnGPU(scene, buffers);
|
||||
|
||||
if (scene.getDebug().enabled)
|
||||
{
|
||||
raytracing_program.clearShaders();
|
||||
|
||||
raytracing_program.attachShader(&debug);
|
||||
raytracing_program.link();
|
||||
scene.getDebug().enabled = 0;
|
||||
}
|
||||
|
||||
raytracing_program.use();
|
||||
raytracing_program.set_int("u_frameCount", window.getFrameCount());
|
||||
@ -72,8 +80,14 @@ int main(int argc, char **argv)
|
||||
raytracing_program.set_int("u_pixelisation", window.getPixelisation());
|
||||
raytracing_program.set_float("u_time", (float)(glfwGetTime()));
|
||||
raytracing_program.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
|
||||
raytracing_program.dispathCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
|
||||
|
||||
std::map<std::string, std::vector<GLuint>> object_textures;
|
||||
object_textures["textures"] = scene.getTextureIDs();
|
||||
object_textures["emissive_textures"] = scene.getEmissionTextureIDs();
|
||||
raytracing_program.set_textures(object_textures);
|
||||
|
||||
raytracing_program.dispathCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
|
||||
|
||||
window.imGuiNewFrame();
|
||||
|
||||
render_program.use();
|
||||
|
@ -3,10 +3,10 @@
|
||||
/* ::: :::::::: */
|
||||
/* Renderer.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/22 16:34:53 by tomoron #+# #+# */
|
||||
/* Updated: 2025/02/07 23:24:53 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/13 19:03:34 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -307,7 +307,8 @@ void Renderer::addImageToRender(Shader &shader)
|
||||
long int videoFrameOffset;
|
||||
long int outputImageOffset;
|
||||
|
||||
image = shader.getOutputImage();
|
||||
(void) shader;
|
||||
// image = shader.getOutputImage();
|
||||
|
||||
for (int x = 0; x < WIDTH; x++)
|
||||
{
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */
|
||||
/* Updated: 2025/02/04 16:43:33 by tomoron ### ########.fr */
|
||||
/* Updated: 2025/02/13 18:13:58 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */
|
||||
/* Updated: 2025/02/06 19:48:22 by ycontre ### ########.fr */
|
||||
/* Updated: 2025/02/13 18:59:18 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -93,73 +93,6 @@ void Shader::reload()
|
||||
this->compile();
|
||||
}
|
||||
|
||||
void Shader::attach(void)
|
||||
{
|
||||
_program = glCreateProgram();
|
||||
_program_compute = glCreateProgram();
|
||||
_program_denoising = glCreateProgram();
|
||||
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
|
||||
|
||||
glAttachShader(_program, _vertex);
|
||||
glAttachShader(_program, _fragment);
|
||||
|
||||
glAttachShader(_program_compute, _compute);
|
||||
|
||||
glAttachShader(_program_denoising, _denoising);
|
||||
|
||||
glLinkProgram(_program);
|
||||
glLinkProgram(_program_compute);
|
||||
glLinkProgram(_program_denoising);
|
||||
|
||||
glGenTextures(1, &_output_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _output_texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(0, _output_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
|
||||
glGenTextures(1, &_accumulation_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _accumulation_texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(1, _accumulation_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
|
||||
glGenTextures(1, &_denoising_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _denoising_texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(2, _denoising_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
|
||||
glGenTextures(1, &_normal_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _normal_texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(3, _normal_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
|
||||
glGenTextures(1, &_position_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _position_texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(4, _position_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
}
|
||||
|
||||
void Shader::checkCompileErrors()
|
||||
{
|
||||
GLint success;
|
||||
@ -173,115 +106,8 @@ void Shader::checkCompileErrors()
|
||||
}
|
||||
}
|
||||
|
||||
void Shader::setupVertexBuffer()
|
||||
{
|
||||
|
||||
Vertex vertices[3] = {{{-1.0f, -1.0f}, {0.0f, 0.0f}},{{3.0f, -1.0f}, {2.0f, 0.0f}},{{-1.0f, 3.0f}, {0.0f, 2.0f}}};
|
||||
_size = sizeof(vertices) / sizeof(Vertex) / 3;
|
||||
|
||||
glGenVertexArrays(1, &_screen_VAO);
|
||||
glGenBuffers(1, &_screen_VBO);
|
||||
|
||||
glBindVertexArray(_screen_VAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _screen_VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, _size * 3 * sizeof(Vertex), vertices, GL_STATIC_DRAW);
|
||||
|
||||
// Position attribute
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// Texture coordinate attribute
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoord));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Shader::drawTriangles()
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, _output_texture);
|
||||
glUniform1i(glGetUniformLocation(_program, "screenTexture"), 0);
|
||||
|
||||
glBindVertexArray(_screen_VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, _size * 3);
|
||||
}
|
||||
|
||||
void Shader::flipOutputDenoising(bool pass)
|
||||
{
|
||||
if (pass)
|
||||
{
|
||||
glBindImageTexture(0, _output_texture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
|
||||
glBindImageTexture(2, _denoising_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
|
||||
}
|
||||
else
|
||||
{
|
||||
glBindImageTexture(0, _denoising_texture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
|
||||
glBindImageTexture(2, _output_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
|
||||
}
|
||||
}
|
||||
|
||||
void Shader::set_int(const std::string &name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(_program_compute, name.c_str()), value);
|
||||
}
|
||||
void Shader::set_float(const std::string &name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(_program_compute, name.c_str()), value);
|
||||
}
|
||||
void Shader::set_vec2(const std::string &name, const glm::vec2 &value) const
|
||||
{
|
||||
glUniform2fv(glGetUniformLocation(_program_compute, name.c_str()), 1, glm::value_ptr(value));
|
||||
}
|
||||
void Shader::set_vec3(const std::string &name, const glm::vec3 &value) const
|
||||
{
|
||||
glUniform3fv(glGetUniformLocation(_program_compute, name.c_str()), 1, glm::value_ptr(value));
|
||||
}
|
||||
void Shader::set_mat4(const std::string &name, const glm::mat4 &value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(_program_compute, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void Shader::set_textures(std::vector<GLuint> texture_ids, std::vector<GLuint> emissive_texture_ids)
|
||||
{
|
||||
for (size_t i = 0; i < texture_ids.size(); i++)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_ids[i]);
|
||||
|
||||
std::string uniform_name = "textures[" + std::to_string(i) + "]";
|
||||
// std::cout << "Loading texture " << uniform_name << " at unit " << i << std::endl;
|
||||
glUniform1i(glGetUniformLocation(_program_compute, uniform_name.c_str()), i);
|
||||
}
|
||||
|
||||
size_t start_texture = texture_ids.size();
|
||||
|
||||
for (size_t i = 0; i < emissive_texture_ids.size(); i++)
|
||||
{
|
||||
GLuint currentUnit = start_texture + i;
|
||||
|
||||
glActiveTexture(GL_TEXTURE0 + currentUnit);
|
||||
glBindTexture(GL_TEXTURE_2D, emissive_texture_ids[i]);
|
||||
std::string uniform_name = "emissive_textures[" + std::to_string(i) + "]";
|
||||
// std::cout << "Loading emissive texture " << uniform_name << " (" << emissive_texture_ids[i] << ") at unit " << currentUnit << std::endl;
|
||||
glUniform1i(glGetUniformLocation(_program_compute, uniform_name.c_str()), currentUnit);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GLuint Shader::getShader(void) const
|
||||
{
|
||||
return (_shader_id);
|
||||
}
|
||||
|
||||
std::vector<float> Shader::getOutputImage(void)
|
||||
{
|
||||
std::vector<float> res(WIDTH * HEIGHT * 4);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, _output_texture);
|
||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, res.data());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
return (res);
|
||||
}
|
||||
|
@ -3,10 +3,10 @@
|
||||
/* ::: :::::::: */
|
||||
/* ShaderProgram.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/12 22:21:46 by TheRed #+# #+# */
|
||||
/* Updated: 2025/02/12 22:21:46 by TheRed ### ########.fr */
|
||||
/* Updated: 2025/02/13 19:16:44 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -28,6 +28,14 @@ void ShaderProgram::attachShader(Shader *shader)
|
||||
glAttachShader(_program, shader->getShader());
|
||||
}
|
||||
|
||||
void ShaderProgram::clearShaders()
|
||||
{
|
||||
for (Shader *shader : _shaders)
|
||||
glDetachShader(_program, shader->getShader());
|
||||
|
||||
_shaders.clear();
|
||||
}
|
||||
|
||||
void ShaderProgram::link()
|
||||
{
|
||||
glLinkProgram(_program);
|
||||
@ -86,6 +94,27 @@ void ShaderProgram::set_vec2(const std::string &name, const glm::vec2 &value) co
|
||||
glUniform2fv(glGetUniformLocation(_program, name.c_str()), 1, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void ShaderProgram::set_textures(std::map<std::string, std::vector<GLuint>> texture_ids)
|
||||
{
|
||||
size_t start_texture = 0;
|
||||
|
||||
for (auto it = texture_ids.begin(); it != texture_ids.end(); it++)
|
||||
{
|
||||
for (unsigned int i = 0; i < it->second.size(); i++)
|
||||
{
|
||||
GLuint current_unit = start_texture + i;
|
||||
|
||||
glActiveTexture(GL_TEXTURE0 + current_unit);
|
||||
glBindTexture(GL_TEXTURE_2D, it->second[i]);
|
||||
|
||||
std::string uniform_name = it->first + "[" + std::to_string(i) + "]";
|
||||
// std::cout << "Loading texture " << uniform_name << " at unit " << i << std::endl;
|
||||
glUniform1i(glGetUniformLocation(_program, uniform_name.c_str()), current_unit);
|
||||
}
|
||||
start_texture = it->second.size();
|
||||
}
|
||||
}
|
||||
|
||||
GLuint ShaderProgram::getProgram(void) const
|
||||
{
|
||||
return (_program);
|
||||
|
Reference in New Issue
Block a user