mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
~ | Fixing emissive texture
This commit is contained in:
@ -24,6 +24,7 @@ typedef struct s_Material
|
|||||||
float refraction;
|
float refraction;
|
||||||
int type;
|
int type;
|
||||||
int texture_index;
|
int texture_index;
|
||||||
|
int emission_texture_index;
|
||||||
} Material;
|
} Material;
|
||||||
|
|
||||||
class Object
|
class Object
|
||||||
|
@ -56,6 +56,7 @@ struct GPUMaterial
|
|||||||
float refraction;
|
float refraction;
|
||||||
int type;
|
int type;
|
||||||
int texture_index;
|
int texture_index;
|
||||||
|
int emission_texture_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GPUVolume
|
struct GPUVolume
|
||||||
@ -112,6 +113,7 @@ class Scene
|
|||||||
void addObject(Object *object);
|
void addObject(Object *object);
|
||||||
void addMaterial(Material *material);
|
void addMaterial(Material *material);
|
||||||
void addTexture(std::string path);
|
void addTexture(std::string path);
|
||||||
|
void addEmissionTexture(std::string path);
|
||||||
|
|
||||||
bool loadTextures();
|
bool loadTextures();
|
||||||
|
|
||||||
@ -125,7 +127,10 @@ class Scene
|
|||||||
|
|
||||||
std::vector<GPUMaterial> &getMaterialData();
|
std::vector<GPUMaterial> &getMaterialData();
|
||||||
std::vector<GLuint> &getTextureIDs();
|
std::vector<GLuint> &getTextureIDs();
|
||||||
|
std::vector<GLuint> &getEmissionTextureIDs();
|
||||||
|
|
||||||
std::vector<std::string> &getTextures();
|
std::vector<std::string> &getTextures();
|
||||||
|
std::vector<std::string> &getEmissionTextures();
|
||||||
|
|
||||||
std::vector<GPUBvhData> &getBvhData();
|
std::vector<GPUBvhData> &getBvhData();
|
||||||
std::vector<GPUBvh> &getBvh();
|
std::vector<GPUBvh> &getBvh();
|
||||||
@ -146,7 +151,10 @@ class Scene
|
|||||||
std::vector<GPUMaterial> _gpu_materials;
|
std::vector<GPUMaterial> _gpu_materials;
|
||||||
|
|
||||||
std::vector<std::string> _textures;
|
std::vector<std::string> _textures;
|
||||||
|
std::vector<std::string> _emissive_textures;
|
||||||
|
|
||||||
std::vector<GLuint> _gpu_textures;
|
std::vector<GLuint> _gpu_textures;
|
||||||
|
std::vector<GLuint> _gpu_emissive_textures;
|
||||||
|
|
||||||
std::set<int> _gpu_lights;
|
std::set<int> _gpu_lights;
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ class Shader
|
|||||||
// void setVec4(const std::string &name, const RT::Vec4f &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_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 getProgram(void) const;
|
||||||
GLuint getProgramCompute(void) const;
|
GLuint getProgramCompute(void) const;
|
||||||
|
@ -202,8 +202,8 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
|
|||||||
GPUMaterial mat = materials[hit.mat_index];
|
GPUMaterial mat = materials[hit.mat_index];
|
||||||
calculateLightColor(mat, hit, color, light, rng_state);
|
calculateLightColor(mat, hit, color, light, rng_state);
|
||||||
|
|
||||||
if (mat.emission > 0.0)
|
// if (mat.emission > 0.0 && mat.emission_texture_index == -1)
|
||||||
break;
|
// break;
|
||||||
|
|
||||||
ray = newRay(hit, ray, rng_state);
|
ray = newRay(hit, ray, rng_state);
|
||||||
ray.inv_direction = 1.0 / ray.direction;
|
ray.inv_direction = 1.0 / ray.direction;
|
||||||
|
@ -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.;
|
||||||
@ -101,8 +101,8 @@ vec2 getSphereUV(vec3 surfacePoint)
|
|||||||
return vec2(u, v);
|
return vec2(u, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
uniform sampler2D textures[32];
|
uniform sampler2D textures[64];
|
||||||
uniform sampler2D emission_textures[32];
|
uniform sampler2D emissive_textures[64];
|
||||||
|
|
||||||
vec2 getTextureColor(hitInfo hit)
|
vec2 getTextureColor(hitInfo hit)
|
||||||
{
|
{
|
||||||
@ -130,11 +130,14 @@ void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inou
|
|||||||
if (mat.emission_texture_index != -1)
|
if (mat.emission_texture_index != -1)
|
||||||
{
|
{
|
||||||
vec2 uv = getTextureColor(hit);
|
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
|
else
|
||||||
|
{
|
||||||
light += mat.emission * mat.color;
|
light += mat.emission * mat.color;
|
||||||
|
|
||||||
color *= mat.color;
|
color *= mat.color;
|
||||||
|
}
|
||||||
// light += sampleLights(hit.position, rng_state);
|
// light += sampleLights(hit.position, rng_state);
|
||||||
}
|
}
|
@ -141,7 +141,6 @@ hitInfo traverseBVHs(Ray ray)
|
|||||||
|
|
||||||
hitInfo temp_hit = traceBVH(transformedRay, BvhData[i]);
|
hitInfo temp_hit = traceBVH(transformedRay, BvhData[i]);
|
||||||
|
|
||||||
|
|
||||||
float transformed_t = temp_hit.t / bvh_data.scale;
|
float transformed_t = temp_hit.t / bvh_data.scale;
|
||||||
if (transformed_t < hit.t)
|
if (transformed_t < hit.t)
|
||||||
{
|
{
|
||||||
@ -156,7 +155,7 @@ hitInfo traverseBVHs(Ray ray)
|
|||||||
vec3 position = transformedRay.origin + transformedRay.direction * temp_hit.t;
|
vec3 position = transformedRay.origin + transformedRay.direction * temp_hit.t;
|
||||||
hit.position = inverseTransformMatrix * position + bvh_data.offset;
|
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);
|
hit.normal = normalize(inverseTransformMatrix * based_normal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,11 +133,9 @@ int main(int argc, char **argv)
|
|||||||
shader.set_float("u_time", (float)(glfwGetTime()));
|
shader.set_float("u_time", (float)(glfwGetTime()));
|
||||||
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(), scene.getEmissionTextureIDs());
|
||||||
shader.set_emission_textures(scene.getEmissionTextureIDs());
|
|
||||||
|
|
||||||
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
|
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
|
||||||
|
|
||||||
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
|
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
@ -333,6 +333,7 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
|
|||||||
throw std::runtime_error("OBJ: syntax error while getting material texture");
|
throw std::runtime_error("OBJ: syntax error while getting material texture");
|
||||||
|
|
||||||
mat->emission_texture_index = scene.getEmissionTextures().size();
|
mat->emission_texture_index = scene.getEmissionTextures().size();
|
||||||
|
std::cout << "path " << mat->emission_texture_index << " : " << getFilePath(_filename) + path << std::endl;
|
||||||
scene.addEmissionTexture(getFilePath(_filename) + path);
|
scene.addEmissionTexture(getFilePath(_filename) + path);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -228,7 +228,7 @@ void Scene::addTexture(std::string path)
|
|||||||
|
|
||||||
void Scene::addEmissionTexture(std::string path)
|
void Scene::addEmissionTexture(std::string path)
|
||||||
{
|
{
|
||||||
_emission_textures.push_back(path);
|
_emissive_textures.push_back(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Scene::loadTextures()
|
bool Scene::loadTextures()
|
||||||
@ -263,6 +263,35 @@ bool Scene::loadTextures()
|
|||||||
stbi_image_free(image);
|
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);
|
return (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,7 +338,7 @@ std::vector<GLuint> &Scene::getTextureIDs()
|
|||||||
|
|
||||||
std::vector<GLuint> &Scene::getEmissionTextureIDs()
|
std::vector<GLuint> &Scene::getEmissionTextureIDs()
|
||||||
{
|
{
|
||||||
return (_gpu_emission_textures);
|
return (_gpu_emissive_textures);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> &Scene::getTextures()
|
std::vector<std::string> &Scene::getTextures()
|
||||||
@ -319,7 +348,7 @@ std::vector<std::string> &Scene::getTextures()
|
|||||||
|
|
||||||
std::vector<std::string> &Scene::getEmissionTextures()
|
std::vector<std::string> &Scene::getEmissionTextures()
|
||||||
{
|
{
|
||||||
return (_emission_textures);
|
return (_emissive_textures);
|
||||||
}
|
}
|
||||||
|
|
||||||
GPUVolume &Scene::getVolume()
|
GPUVolume &Scene::getVolume()
|
||||||
|
@ -62,6 +62,7 @@ void SceneParser::parseMaterial(std::stringstream &line)
|
|||||||
mat->type = 0;
|
mat->type = 0;
|
||||||
|
|
||||||
mat->texture_index = texture_index;
|
mat->texture_index = texture_index;
|
||||||
|
mat->emission_texture_index = -1;
|
||||||
_scene->addMaterial(mat);
|
_scene->addMaterial(mat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,29 +208,30 @@ 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(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);
|
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) + "]";
|
std::string uniform_name = "textures[" + std::to_string(i) + "]";
|
||||||
glUniform1i(glGetUniformLocation(_program_compute, uniform_name.c_str()), i);
|
glUniform1i(glGetUniformLocation(_program_compute, uniform_name.c_str()), i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t start_texture = texture_ids.size() + 1;
|
||||||
|
|
||||||
|
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) + "]";
|
||||||
|
glUniform1i(glGetUniformLocation(_program_compute, uniform_name.c_str()), currentUnit);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user