+ | Parsing textures

This commit is contained in:
TheRedShip
2025-01-27 23:56:32 +01:00
parent 6b64d2188e
commit e9c77cfdd4
12 changed files with 92 additions and 94 deletions

View File

@ -7,7 +7,7 @@ Pos=1645,8
Size=259,200 Size=259,200
[Window][Material] [Window][Material]
Pos=1648,217 Pos=1638,215
Size=262,299 Size=262,299
[Window][Fog settings] [Window][Fog settings]

View File

@ -51,6 +51,7 @@ struct GPUMaterial
float metallic; float metallic;
float refraction; float refraction;
int type; int type;
int texture_index;
}; };
struct GPUVolume struct GPUVolume
@ -108,6 +109,8 @@ class Scene
void addMaterial(Material *material); void addMaterial(Material *material);
void addTexture(std::string path); void addTexture(std::string path);
void loadTextures();
void updateLightAndObjects(int mat_id); void updateLightAndObjects(int mat_id);
std::set<int> getGPULights(); std::set<int> getGPULights();
@ -117,6 +120,7 @@ class Scene
const std::vector<GPUTriangle> &getTriangleData() const; const std::vector<GPUTriangle> &getTriangleData() const;
std::vector<GPUMaterial> &getMaterialData(); std::vector<GPUMaterial> &getMaterialData();
std::vector<GLuint> &getTextureIDs();
std::vector<GPUBvhData> &getBvhData(); std::vector<GPUBvhData> &getBvhData();
std::vector<GPUBvh> &getBvh(); std::vector<GPUBvh> &getBvh();
@ -136,6 +140,9 @@ class Scene
std::vector<GPUMaterial> _gpu_materials; std::vector<GPUMaterial> _gpu_materials;
std::vector<std::string> _textures;
std::vector<GLuint> _gpu_textures;
std::set<int> _gpu_lights; std::set<int> _gpu_lights;
GPUVolume _gpu_volume; GPUVolume _gpu_volume;

View File

@ -1,10 +1,12 @@
CAM 0 -0.5 4 -10.6 -90.2 0.241 4.709 60.399 5 CAM 0 -0.5 4 -10.6 -90.2 0.241 4.709 60.399 5
TEX texture.jpg
MAT 255 010 010 5 0.0 1.0 MAT 255 010 010 5 0.0 1.0
MAT 010 255 010 5 0.0 1.0 MAT 010 255 010 5 0.0 1.0
MAT 010 010 255 5 0.0 1.0 MAT 010 010 255 5 0.0 1.0
MAT 255 255 255 0 1.7 1.0 DIE MAT 255 255 255 0 1.7 1.0 DIE 0
MAT 200 200 200 0 0.0 1.0 MAT 200 200 200 0 0.0 1.0

View File

@ -8,8 +8,7 @@ sp +0.000 +17.00 +20.00 4.0 0
sp +0.000 +17.00 +0.000 4.0 1 sp +0.000 +17.00 +0.000 4.0 1
sp +0.000 +17.00 -20.00 4.0 2 sp +0.000 +17.00 -20.00 4.0 2
MAT 250 250 250 0.0 1.5 0.0 DIE -1 // 3 white 0 0 1
MAT 250 250 250 0.0 1.5 0.0 DIE // 3 white 0 0 1
MAT 250 250 250 0.0 1.0 1.0 // 4 white 0 1 0 MAT 250 250 250 0.0 1.0 1.0 // 4 white 0 1 0
MAT 200 200 200 0.0 0.0 0.0 // 5 white 1 0 0 MAT 200 200 200 0.0 0.0 0.0 // 5 white 1 0 0

View File

@ -65,8 +65,10 @@ cu -9 0.98 3 3 3 3 28
MAT 244 95 28 0.0 0.5 1.0 MAT 244 95 28 0.0 0.5 1.0
cu -9 1.23 -6 3 3 3 29 cu -9 1.23 -6 3 3 3 29
MAT 200 20 20 0.0 1.0 0.05 TEX texture.jpg // 0
MAT 255 255 255 0.0 1.8 0.0 DIE
MAT 200 20 20 0.0 1.0 0.05 LAM 0
MAT 255 255 255 0.0 1.8 0.0 DIE -1
MAT 255 255 255 0.0 1.0 1.0 MAT 255 255 255 0.0 1.0 1.0
sp 0 5 -4 3 30 sp 0 5 -4 3 30

View File

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

View File

@ -103,15 +103,18 @@ vec2 getSphereUV(vec3 surfacePoint)
uniform sampler2D textures[16]; uniform sampler2D textures[16];
vec3 getTextureColor(int texture_index, hitInfo hit)
{
vec2 uv = getSphereUV(hit.normal);
return (texture(textures[texture_index], uv).rgb);
}
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)
{ {
if (objects[hit.obj_index].type == 0) if (mat.texture_index != -1)
{ color *= getTextureColor(mat.texture_index, hit);
vec2 uv = getSphereUV(hit.normal);
color *= texture(textures[0], uv).rgb; color *= mat.color;
}
else
color *= mat.color;
light += mat.emission * mat.color; light += mat.emission * mat.color;
// light += sampleLights(hit.position, rng_state); // light += sampleLights(hit.position, rng_state);
} }

View File

@ -12,59 +12,6 @@
#include "RT.hpp" #include "RT.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
struct GPUTexture {
int width;
int height;
int channels;
int object_id; // to map texture to specific objects
};
std::vector<GLuint> loadTextures(const std::vector<std::string>& texture_paths, std::vector<GPUTexture>& gpu_textures) {
std::vector<GLuint> textureIDs;
for (size_t i = 0; i < texture_paths.size(); i++) {
int width, height, channels;
unsigned char* image = stbi_load(texture_paths[i].c_str(), &width, &height, &channels, STBI_rgb_alpha);
if (!image) {
std::cerr << "Failed to load texture: " << texture_paths[i] << std::endl;
continue;
}
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// Set texture parameters
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);
// Upload texture data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
// Store texture info for GPU
GPUTexture tex_info = {
width,
height,
channels,
static_cast<int>(i) // or map to specific object ID
};
gpu_textures.push_back(tex_info);
textureIDs.push_back(textureID);
stbi_image_free(image);
}
return textureIDs;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
Scene scene; Scene scene;
@ -74,6 +21,7 @@ int main(int argc, char **argv)
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0); Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0);
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl"); Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl"); // Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
GLint max_gpu_size; GLint max_gpu_size;
@ -152,25 +100,18 @@ int main(int argc, char **argv)
glBufferData(GL_UNIFORM_BUFFER, sizeof(GPUDebug), nullptr, GL_STATIC_DRAW); glBufferData(GL_UNIFORM_BUFFER, sizeof(GPUDebug), nullptr, GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 2, debugUBO); glBindBufferBase(GL_UNIFORM_BUFFER, 2, debugUBO);
try
{
// texture scene.loadTextures();
}
std::vector<std::string> texture_paths = {"texture.jpg", "texture2.jpg", "texture.jpg"}; catch(const std::exception& e)
std::vector<GPUTexture> gpu_textures; {
std::vector<GLuint> textureIDs = loadTextures(texture_paths, gpu_textures); std::cerr << e.what() << std::endl;
return (1);
GLuint textureInfoSSBO; }
glGenBuffers(1, &textureInfoSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, textureInfoSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, gpu_textures.size() * sizeof(GPUTexture), gpu_textures.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, textureInfoSSBO); // Using binding point 7
//
shader.attach(); shader.attach();
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}}}; 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_t size = sizeof(vertices) / sizeof(Vertex) / 3; size_t size = sizeof(vertices) / sizeof(Vertex) / 3;
shader.setupVertexBuffer(vertices, size); shader.setupVertexBuffer(vertices, size);
@ -240,10 +181,12 @@ int main(int argc, char **argv)
//texture //texture
// In your render loop // In your render loop
for (size_t i = 0; i < textureIDs.size(); i++) { std::vector<GLuint> textureIDs = scene.getTextureIDs();
for (size_t i = 0; i < textureIDs.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i); glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, textureIDs[i]); glBindTexture(GL_TEXTURE_2D, textureIDs[i]);
// Set uniform for each texture
std::string uniform_name = "textures[" + std::to_string(i) + "]"; std::string uniform_name = "textures[" + std::to_string(i) + "]";
glUniform1i(glGetUniformLocation(shader.getProgramCompute(), uniform_name.c_str()), i); glUniform1i(glGetUniformLocation(shader.getProgramCompute(), uniform_name.c_str()), i);
} }

View File

@ -270,6 +270,7 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
} }
if(mat) if(mat)
{ {
mat->texture_index = -1;
scene.addMaterial(mat); scene.addMaterial(mat);
_matNames[matName] = scene.getMaterialData().size() - 1; _matNames[matName] = scene.getMaterialData().size() - 1;
} }

View File

@ -12,6 +12,9 @@
#include "Scene.hpp" #include "Scene.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
Scene::Scene() Scene::Scene()
{ {
_camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f); _camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f);
@ -204,13 +207,43 @@ void Scene::addMaterial(Material *material)
gpu_mat.metallic = material->metallic; gpu_mat.metallic = material->metallic;
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_materials.push_back(gpu_mat); _gpu_materials.push_back(gpu_mat);
} }
void Scene::addTexture(std::string path) void Scene::addTexture(std::string path)
{ {
(void) path; _textures.push_back(path);
}
void Scene::loadTextures()
{
for (std::string &path : _textures)
{
int width, height, channels;
unsigned char* image = stbi_load(path.c_str(), &width, &height, &channels, STBI_rgb_alpha);
if (!image)
throw std::runtime_error("Failed to load texture");
std::cout << "Loaded 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_textures.push_back(textureID);
stbi_image_free(image);
}
} }
void Scene::updateLightAndObjects(int mat_id) void Scene::updateLightAndObjects(int mat_id)
@ -249,6 +282,11 @@ std::vector<GPUMaterial> &Scene::getMaterialData()
return (_gpu_materials); return (_gpu_materials);
} }
std::vector<GLuint> &Scene::getTextureIDs()
{
return (_gpu_textures);
}
GPUVolume &Scene::getVolume() GPUVolume &Scene::getVolume()
{ {
return (_gpu_volume); return (_gpu_volume);

View File

@ -40,18 +40,14 @@ void SceneParser::parseMaterial(std::stringstream &line)
if (!(line >> type)) if (!(line >> type))
type = "LAM"; type = "LAM";
if (!(line >> texture_index))
texture_index = -1;
mat = new Material; mat = new Material;
mat->color = glm::vec3(r / 255.0f, g / 255.0f, b / 255.0f); mat->color = glm::vec3(r / 255.0f, g / 255.0f, b / 255.0f);
mat->emission = emission; mat->emission = emission;
mat->roughness = rough_refrac; mat->roughness = rough_refrac;
mat->metallic = metallic; mat->metallic = metallic;
mat->refraction = rough_refrac; mat->refraction = rough_refrac;
mat->type = 0; mat->type = -1;
if (type == "LAM") if (type == "LAM")
mat->type = 0; mat->type = 0;
else if (type == "DIE") else if (type == "DIE")
@ -59,8 +55,14 @@ void SceneParser::parseMaterial(std::stringstream &line)
else if (type == "TRN") else if (type == "TRN")
mat->type = 2; mat->type = 2;
mat->texture_index = texture_index; texture_index = -1;
if (mat->type != -1)
line >> texture_index;
else
mat->type = 0;
mat->texture_index = texture_index;
std::cout << "Texture index: " << texture_index << std::endl;
_scene->addMaterial(mat); _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 *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);