+ | Poorly done emissive texture

This commit is contained in:
TheRedShip
2025-02-01 21:32:37 +01:00
parent 0bb7315681
commit 328737ac4b
6 changed files with 60 additions and 13 deletions

View File

@ -43,6 +43,7 @@ struct GPUMaterial
float refraction; // 4 float refraction; // 4
int type; // 4 int type; // 4
int texture_index; // 4 int texture_index; // 4
int emission_texture_index; // 4
}; };
struct GPUCamera struct GPUCamera

View File

@ -2,7 +2,7 @@ hitInfo traceRay(Ray ray);
vec3 GetEnvironmentLight(Ray ray) vec3 GetEnvironmentLight(Ray ray)
{ {
// return vec3(0.); return vec3(0.);
vec3 sun_pos = vec3(-0.5, 0.5, 0.5); vec3 sun_pos = vec3(-0.5, 0.5, 0.5);
float SunFocus = 1.5; float SunFocus = 1.5;
float SunIntensity = 1.; float SunIntensity = 1.;
@ -102,8 +102,9 @@ vec2 getSphereUV(vec3 surfacePoint)
} }
uniform sampler2D textures[32]; uniform sampler2D textures[32];
uniform sampler2D emission_textures[32];
vec3 getTextureColor(int texture_index, hitInfo hit) vec2 getTextureColor(hitInfo hit)
{ {
vec2 uv = vec2(0.0); vec2 uv = vec2(0.0);
@ -116,13 +117,24 @@ vec3 getTextureColor(int texture_index, hitInfo hit)
uv = hit.u * tri.texture_vertex2 + hit.v * tri.texture_vertex3 + (1 - (hit.u + hit.v)) * tri.texture_vertex1; uv = hit.u * tri.texture_vertex2 + hit.v * tri.texture_vertex3 + (1 - (hit.u + hit.v)) * tri.texture_vertex1;
uv = vec2(uv.x, 1 - uv.y); uv = vec2(uv.x, 1 - uv.y);
} }
return (texture(textures[texture_index], uv).rgb); return (uv);
} }
void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state) void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state)
{ {
color *= mat.texture_index == -1 ? vec3(1.0) : getTextureColor(mat.texture_index, hit); if (mat.texture_index != -1)
color *= mat.color; {
vec2 uv = getTextureColor(hit);
color *= texture(textures[mat.texture_index], uv).rgb;
}
if (mat.emission_texture_index != -1)
{
vec2 uv = getTextureColor(hit);
light += mat.emission * texture(emission_textures[mat.emission_texture_index], uv).rgb;
}
else
light += mat.emission * mat.color; light += mat.emission * mat.color;
color *= mat.color;
// light += sampleLights(hit.position, rng_state); // light += sampleLights(hit.position, rng_state);
} }

View File

@ -76,12 +76,6 @@ int main(int argc, char **argv)
glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getGPULights().size() * sizeof(int), nullptr, GL_STATIC_DRAW); glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getGPULights().size() * sizeof(int), nullptr, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, lightSSBO); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, lightSSBO);
GLuint textureSSBO;
glGenBuffers(1, &textureSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, textureSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, 1 * sizeof(int), nullptr, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, textureSSBO);
GLuint cameraUBO; GLuint cameraUBO;
glGenBuffers(1, &cameraUBO); glGenBuffers(1, &cameraUBO);
glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO); glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO);
@ -140,6 +134,7 @@ int main(int argc, char **argv)
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT)); shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
shader.set_textures(scene.getTextureIDs()); shader.set_textures(scene.getTextureIDs());
shader.set_emission_textures(scene.getEmissionTextureIDs());
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1); glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);

View File

@ -249,6 +249,7 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
mat = new Material; mat = new Material;
memset(mat, 0, sizeof(Material)); memset(mat, 0, sizeof(Material));
mat->texture_index = -1; mat->texture_index = -1;
mat->emission_texture_index = -1;
mat->refraction = 1.0f; mat->refraction = 1.0f;
mat->roughness = 1.0f; mat->roughness = 1.0f;
mat->metallic = 1.0f; mat->metallic = 1.0f;
@ -324,6 +325,16 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
mat->texture_index = scene.getTextures().size(); mat->texture_index = scene.getTextures().size();
scene.addTexture(getFilePath(_filename) + path); scene.addTexture(getFilePath(_filename) + path);
} }
else if (identifier == "map_Ke")
{
std::string path;
if (!(lineStream >> path))
throw std::runtime_error("OBJ: syntax error while getting material texture");
mat->emission_texture_index = scene.getEmissionTextures().size();
scene.addEmissionTexture(getFilePath(_filename) + path);
}
else else
std::cerr << "unsupported material setting : " << identifier << std::endl; std::cerr << "unsupported material setting : " << identifier << std::endl;
} }

View File

@ -217,6 +217,7 @@ void Scene::addMaterial(Material *material)
gpu_mat.refraction = material->refraction; gpu_mat.refraction = material->refraction;
gpu_mat.type = material->type; gpu_mat.type = material->type;
gpu_mat.texture_index = material->texture_index; gpu_mat.texture_index = material->texture_index;
gpu_mat.emission_texture_index = material->emission_texture_index;
_gpu_materials.push_back(gpu_mat); _gpu_materials.push_back(gpu_mat);
} }
@ -225,6 +226,11 @@ void Scene::addTexture(std::string path)
_textures.push_back(path); _textures.push_back(path);
} }
void Scene::addEmissionTexture(std::string path)
{
_emission_textures.push_back(path);
}
bool Scene::loadTextures() bool Scene::loadTextures()
{ {
for (std::string &path : _textures) for (std::string &path : _textures)
@ -301,11 +307,21 @@ std::vector<GLuint> &Scene::getTextureIDs()
return (_gpu_textures); return (_gpu_textures);
} }
std::vector<GLuint> &Scene::getEmissionTextureIDs()
{
return (_gpu_emission_textures);
}
std::vector<std::string> &Scene::getTextures() std::vector<std::string> &Scene::getTextures()
{ {
return (_textures); return (_textures);
} }
std::vector<std::string> &Scene::getEmissionTextures()
{
return (_emission_textures);
}
GPUVolume &Scene::getVolume() GPUVolume &Scene::getVolume()
{ {
return (_gpu_volume); return (_gpu_volume);

View File

@ -67,7 +67,7 @@ Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string com
const char *fragmentCode = loadFileWithIncludes(fragmentPath); const char *fragmentCode = loadFileWithIncludes(fragmentPath);
const char *computeCode = loadFileWithIncludes(computePath); const char *computeCode = loadFileWithIncludes(computePath);
// printWithLineNumbers(computeCode); printWithLineNumbers(computeCode);
_vertex = glCreateShader(GL_VERTEX_SHADER); _vertex = glCreateShader(GL_VERTEX_SHADER);
@ -208,7 +208,7 @@ 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)); glUniformMatrix4fv(glGetUniformLocation(_program_compute, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
} }
void Shader::set_textures(const std::vector<GLuint> textureIDs) void Shader::set_textures(std::vector<GLuint> textureIDs)
{ {
for (size_t i = 0; i < textureIDs.size(); i++) for (size_t i = 0; i < textureIDs.size(); i++)
{ {
@ -220,6 +220,18 @@ void Shader::set_textures(const std::vector<GLuint> textureIDs)
} }
} }
void Shader::set_emission_textures(std::vector<GLuint> textureIDs)
{
for (size_t i = 0; i < textureIDs.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, textureIDs[i]);
std::string uniform_name = "emission_textures[" + std::to_string(i) + "]";
glUniform1i(glGetUniformLocation(_program_compute, uniform_name.c_str()), i);
}
}
GLuint Shader::getProgram(void) const GLuint Shader::getProgram(void) const
{ {
return (_program); return (_program);