~ | Fixing emissive texture

This commit is contained in:
TheRedShip
2025-02-02 15:16:47 +01:00
parent 328737ac4b
commit f03d48e62d
11 changed files with 73 additions and 32 deletions

View File

@ -24,6 +24,7 @@ typedef struct s_Material
float refraction;
int type;
int texture_index;
int emission_texture_index;
} Material;
class Object

View File

@ -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,7 +127,10 @@ class Scene
std::vector<GPUMaterial> &getMaterialData();
std::vector<GLuint> &getTextureIDs();
std::vector<GLuint> &getEmissionTextureIDs();
std::vector<std::string> &getTextures();
std::vector<std::string> &getEmissionTextures();
std::vector<GPUBvhData> &getBvhData();
std::vector<GPUBvh> &getBvh();
@ -146,7 +151,10 @@ class Scene
std::vector<GPUMaterial> _gpu_materials;
std::vector<std::string> _textures;
std::vector<std::string> _emissive_textures;
std::vector<GLuint> _gpu_textures;
std::vector<GLuint> _gpu_emissive_textures;
std::set<int> _gpu_lights;

View File

@ -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<GLuint> textureIDs);
void set_textures(std::vector<GLuint> texture_ids, std::vector<GLuint> emissive_texture_ids);
GLuint getProgram(void) const;
GLuint getProgramCompute(void) const;

View File

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

View File

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

View File

@ -141,11 +141,10 @@ 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;
@ -156,7 +155,7 @@ hitInfo traverseBVHs(Ray ray)
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);
}
}

View File

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

View File

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

View File

@ -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<GLuint> &Scene::getTextureIDs()
std::vector<GLuint> &Scene::getEmissionTextureIDs()
{
return (_gpu_emission_textures);
return (_gpu_emissive_textures);
}
std::vector<std::string> &Scene::getTextures()
@ -319,7 +348,7 @@ std::vector<std::string> &Scene::getTextures()
std::vector<std::string> &Scene::getEmissionTextures()
{
return (_emission_textures);
return (_emissive_textures);
}
GPUVolume &Scene::getVolume()

View File

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

View File

@ -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<GLuint> textureIDs)
void Shader::set_textures(std::vector<GLuint> texture_ids, std::vector<GLuint> 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<GLuint> 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);