diff --git a/includes/RT/Object.hpp b/includes/RT/Object.hpp index 583b126..95cea16 100644 --- a/includes/RT/Object.hpp +++ b/includes/RT/Object.hpp @@ -24,6 +24,7 @@ typedef struct s_Material float refraction; int type; int texture_index; + int emission_texture_index; } Material; class Object diff --git a/includes/RT/Scene.hpp b/includes/RT/Scene.hpp index 97173d6..7e78db4 100644 --- a/includes/RT/Scene.hpp +++ b/includes/RT/Scene.hpp @@ -56,6 +56,7 @@ struct GPUMaterial float refraction; int type; int texture_index; + int emission_texture_index; }; struct GPUVolume @@ -112,6 +113,7 @@ class Scene void addObject(Object *object); void addMaterial(Material *material); void addTexture(std::string path); + void addEmissionTexture(std::string path); bool loadTextures(); @@ -125,8 +127,11 @@ class Scene std::vector &getMaterialData(); std::vector &getTextureIDs(); + std::vector &getEmissionTextureIDs(); + std::vector &getTextures(); - + std::vector &getEmissionTextures(); + std::vector &getBvhData(); std::vector &getBvh(); @@ -146,7 +151,10 @@ class Scene std::vector _gpu_materials; std::vector _textures; + std::vector _emissive_textures; + std::vector _gpu_textures; + std::vector _gpu_emissive_textures; std::set _gpu_lights; diff --git a/includes/RT/Shader.hpp b/includes/RT/Shader.hpp index 59a9afe..2f37aff 100644 --- a/includes/RT/Shader.hpp +++ b/includes/RT/Shader.hpp @@ -33,7 +33,7 @@ class Shader // 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 textureIDs); + void set_textures(std::vector texture_ids, std::vector emissive_texture_ids); GLuint getProgram(void) const; GLuint getProgramCompute(void) const; diff --git a/shaders/compute.glsl b/shaders/compute.glsl index 530aebc..a17020b 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -202,8 +202,8 @@ vec3 pathtrace(Ray ray, inout uint rng_state) GPUMaterial mat = materials[hit.mat_index]; calculateLightColor(mat, hit, color, light, rng_state); - if (mat.emission > 0.0) - break; + // if (mat.emission > 0.0 && mat.emission_texture_index == -1) + // break; ray = newRay(hit, ray, rng_state); ray.inv_direction = 1.0 / ray.direction; diff --git a/shaders/light.glsl b/shaders/light.glsl index f3038d6..6b5d1ed 100644 --- a/shaders/light.glsl +++ b/shaders/light.glsl @@ -2,7 +2,7 @@ hitInfo traceRay(Ray ray); vec3 GetEnvironmentLight(Ray ray) { - return vec3(0.); + // return vec3(0.); vec3 sun_pos = vec3(-0.5, 0.5, 0.5); float SunFocus = 1.5; float SunIntensity = 1.; @@ -101,8 +101,8 @@ vec2 getSphereUV(vec3 surfacePoint) return vec2(u, v); } -uniform sampler2D textures[32]; -uniform sampler2D emission_textures[32]; +uniform sampler2D textures[64]; +uniform sampler2D emissive_textures[64]; vec2 getTextureColor(hitInfo hit) { @@ -130,11 +130,14 @@ void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inou if (mat.emission_texture_index != -1) { vec2 uv = getTextureColor(hit); - light += mat.emission * texture(emission_textures[mat.emission_texture_index], uv).rgb; + vec3 emission = mat.emission * texture(emissive_textures[mat.emission_texture_index], uv).rgb; + + light += mat.emission * emission; } else + { light += mat.emission * mat.color; - - color *= mat.color; + color *= mat.color; + } // light += sampleLights(hit.position, rng_state); } \ No newline at end of file diff --git a/shaders/trace.glsl b/shaders/trace.glsl index fe12af8..50067f2 100644 --- a/shaders/trace.glsl +++ b/shaders/trace.glsl @@ -141,22 +141,21 @@ hitInfo traverseBVHs(Ray ray) hitInfo temp_hit = traceBVH(transformedRay, BvhData[i]); - float transformed_t = temp_hit.t / bvh_data.scale; if (transformed_t < hit.t) { - GPUTriangle triangle = triangles[temp_hit.obj_index]; + 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.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)); + vec3 based_normal = triangle.normal * sign(-dot(transformedRay.direction, triangle.normal)); hit.normal = normalize(inverseTransformMatrix * based_normal); } } diff --git a/srcs/RT.cpp b/srcs/RT.cpp index f4237d3..964f009 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -133,11 +133,9 @@ int main(int argc, char **argv) shader.set_float("u_time", (float)(glfwGetTime())); shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT)); - shader.set_textures(scene.getTextureIDs()); - shader.set_emission_textures(scene.getEmissionTextureIDs()); + shader.set_textures(scene.getTextureIDs(), scene.getEmissionTextureIDs()); glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); glClear(GL_COLOR_BUFFER_BIT); diff --git a/srcs/class/ObjParser.cpp b/srcs/class/ObjParser.cpp index 54e3518..5debd8c 100644 --- a/srcs/class/ObjParser.cpp +++ b/srcs/class/ObjParser.cpp @@ -333,6 +333,7 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene) throw std::runtime_error("OBJ: syntax error while getting material texture"); mat->emission_texture_index = scene.getEmissionTextures().size(); + std::cout << "path " << mat->emission_texture_index << " : " << getFilePath(_filename) + path << std::endl; scene.addEmissionTexture(getFilePath(_filename) + path); } else diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index 6f0f1cd..7e6ab5d 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -228,7 +228,7 @@ void Scene::addTexture(std::string path) void Scene::addEmissionTexture(std::string path) { - _emission_textures.push_back(path); + _emissive_textures.push_back(path); } bool Scene::loadTextures() @@ -263,6 +263,35 @@ bool Scene::loadTextures() stbi_image_free(image); } + for (std::string &path : _emissive_textures) + { + int width, height, channels; + unsigned char* image = stbi_load(path.c_str(), &width, &height, &channels, STBI_rgb_alpha); + + if (!image) + { + std::cout << "Failed to load texture " << path << std::endl; + return (false); + } + + std::cout << "Loaded emissive texture: " << path << " (" << width << "x" << height << ")" << std::endl; + + GLuint textureID; + glGenTextures(1, &textureID); + glBindTexture(GL_TEXTURE_2D, textureID); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); + glGenerateMipmap(GL_TEXTURE_2D); + _gpu_emissive_textures.push_back(textureID); + + stbi_image_free(image); + } + return (true); } @@ -309,7 +338,7 @@ std::vector &Scene::getTextureIDs() std::vector &Scene::getEmissionTextureIDs() { - return (_gpu_emission_textures); + return (_gpu_emissive_textures); } std::vector &Scene::getTextures() @@ -319,7 +348,7 @@ std::vector &Scene::getTextures() std::vector &Scene::getEmissionTextures() { - return (_emission_textures); + return (_emissive_textures); } GPUVolume &Scene::getVolume() diff --git a/srcs/class/SceneParser.cpp b/srcs/class/SceneParser.cpp index 9d6ef15..b499c3b 100644 --- a/srcs/class/SceneParser.cpp +++ b/srcs/class/SceneParser.cpp @@ -62,6 +62,7 @@ void SceneParser::parseMaterial(std::stringstream &line) mat->type = 0; mat->texture_index = texture_index; + mat->emission_texture_index = -1; _scene->addMaterial(mat); } diff --git a/srcs/class/Shader.cpp b/srcs/class/Shader.cpp index 5221403..5c00257 100644 --- a/srcs/class/Shader.cpp +++ b/srcs/class/Shader.cpp @@ -67,7 +67,7 @@ Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string com const char *fragmentCode = loadFileWithIncludes(fragmentPath); const char *computeCode = loadFileWithIncludes(computePath); - printWithLineNumbers(computeCode); + // printWithLineNumbers(computeCode); _vertex = glCreateShader(GL_VERTEX_SHADER); @@ -208,30 +208,31 @@ 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 textureIDs) +void Shader::set_textures(std::vector texture_ids, std::vector emissive_texture_ids) { - for (size_t i = 0; i < textureIDs.size(); i++) + for (size_t i = 0; i < texture_ids.size(); i++) { glActiveTexture(GL_TEXTURE0 + i); - glBindTexture(GL_TEXTURE_2D, textureIDs[i]); + glBindTexture(GL_TEXTURE_2D, texture_ids[i]); std::string uniform_name = "textures[" + std::to_string(i) + "]"; glUniform1i(glGetUniformLocation(_program_compute, uniform_name.c_str()), i); } -} -void Shader::set_emission_textures(std::vector textureIDs) -{ - for (size_t i = 0; i < textureIDs.size(); i++) + size_t start_texture = texture_ids.size() + 1; + + for (size_t i = 0; i < emissive_texture_ids.size(); i++) { - glActiveTexture(GL_TEXTURE0 + i); - glBindTexture(GL_TEXTURE_2D, textureIDs[i]); + GLuint currentUnit = start_texture + i; - std::string uniform_name = "emission_textures[" + std::to_string(i) + "]"; - glUniform1i(glGetUniformLocation(_program_compute, uniform_name.c_str()), i); + glActiveTexture(GL_TEXTURE0 + currentUnit); + glBindTexture(GL_TEXTURE_2D, emissive_texture_ids[i]); + std::string uniform_name = "emissive_textures[" + std::to_string(i) + "]"; + glUniform1i(glGetUniformLocation(_program_compute, uniform_name.c_str()), currentUnit); } } + GLuint Shader::getProgram(void) const { return (_program);