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

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

View File

@ -12,6 +12,9 @@
#include "Scene.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
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);
@ -204,13 +207,43 @@ void Scene::addMaterial(Material *material)
gpu_mat.metallic = material->metallic;
gpu_mat.refraction = material->refraction;
gpu_mat.type = material->type;
gpu_mat.texture_index = material->texture_index;
_gpu_materials.push_back(gpu_mat);
}
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)
@ -249,6 +282,11 @@ std::vector<GPUMaterial> &Scene::getMaterialData()
return (_gpu_materials);
}
std::vector<GLuint> &Scene::getTextureIDs()
{
return (_gpu_textures);
}
GPUVolume &Scene::getVolume()
{
return (_gpu_volume);

View File

@ -39,19 +39,15 @@ void SceneParser::parseMaterial(std::stringstream &line)
if (!(line >> type))
type = "LAM";
if (!(line >> texture_index))
texture_index = -1;
mat = new Material;
mat->color = glm::vec3(r / 255.0f, g / 255.0f, b / 255.0f);
mat->emission = emission;
mat->roughness = rough_refrac;
mat->metallic = metallic;
mat->refraction = rough_refrac;
mat->type = 0;
mat->type = -1;
if (type == "LAM")
mat->type = 0;
else if (type == "DIE")
@ -59,8 +55,14 @@ void SceneParser::parseMaterial(std::stringstream &line)
else if (type == "TRN")
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);
}

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