+ | Real material shared system + parsing

This commit is contained in:
TheRedShip
2024-12-27 11:11:01 +01:00
parent 8941169f4a
commit ea3dd738f8
9 changed files with 105 additions and 63 deletions

View File

@ -18,6 +18,7 @@
typedef struct s_Material typedef struct s_Material
{ {
glm::vec3 color; glm::vec3 color;
float emission;
float roughness; float roughness;
float specular; float specular;
} Material; } Material;
@ -26,14 +27,14 @@ class Object
{ {
protected: protected:
glm::vec3 _position; glm::vec3 _position;
const Material *_material; int _mat_index;
public: public:
Object(const glm::vec3& position, const Material *material) : _position(position), _material(material) {} Object(const glm::vec3& position, const int mat_index) : _position(position), _mat_index(mat_index) {}
virtual ~Object() = default; virtual ~Object() = default;
int getMaterialIndex() const {return (_mat_index); }
const glm::vec3 &getPosition() const { return (_position); } const glm::vec3 &getPosition() const { return (_position); }
const Material *getMaterial() const { return (_material); }
enum class Type { enum class Type {
SPHERE, SPHERE,

View File

@ -17,10 +17,9 @@
struct GPUObject struct GPUObject
{ {
glm::vec3 position; alignas(16) glm::vec3 position;
int padding_1; alignas(16) glm::vec3 color;
glm::vec3 color; float emission;
int padding_2;
float roughness; float roughness;
float specular; float specular;
float radius; float radius;
@ -39,16 +38,21 @@ class Scene
bool parseScene(char *name); bool parseScene(char *name);
void addObject(Object *object); void addObject(Object *object);
void addMaterial(Material *material);
void updateGPUData(); void updateGPUData();
const std::vector<GPUObject> &getGPUData() const; const std::vector<GPUObject> &getGPUData() const;
Camera *getCamera(void) const; Camera *getCamera(void) const;
Material *getMaterial(int material_index);
private: private:
std::vector<Object *> _objects; std::vector<Object *> _objects;
std::vector<GPUObject> _gpu_objects; std::vector<GPUObject> _gpu_objects;
std::vector<Material *> _materials;
Camera *_camera; Camera *_camera;
}; };

View File

@ -10,8 +10,8 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef RT_SceneParser__HPP #ifndef RT_SCENEPARSER__HPP
# define RT_SceneParser__HPP # define RT_SCENEPARSER__HPP
# include "RT.hpp" # include "RT.hpp"
@ -23,7 +23,10 @@ class SceneParser
bool parseLine(const std::string &line); bool parseLine(const std::string &line);
private: private:
Scene *_scene; void parseMaterial(std::stringstream &line);
Scene *_scene;
std::map<std::string, std::function<Object *(std::stringstream &)>> object_parsers; std::map<std::string, std::function<Object *(std::stringstream &)>> object_parsers;
}; };

View File

@ -17,58 +17,36 @@
class Sphere : public Object class Sphere : public Object
{ {
public: public:
struct ParseError : public std::runtime_error Sphere(std::stringstream &line) : Object(glm::vec3(0.0f), -1)
{ {
ParseError(const std::string& msg) : std::runtime_error("Sphere parse error: " + msg) {}
};
Sphere(std::stringstream &line) : Object(glm::vec3(0.0f), nullptr)
{
_mat = new Material;
try { try {
float x, y, z, radius; float x, y, z, radius;
float r, g, b, rough, spec; int mat_index;
if (!(line >> x >> y >> z >> radius)) if (!(line >> x >> y >> z >> radius))
throw std::invalid_argument("Missing position or radius values"); throw std::runtime_error("Missing position or radius values");
if (radius <= 0.0f) if (radius <= 0.0f)
throw std::invalid_argument("Radius must be positive"); throw std::runtime_error("Radius must be positive");
if (!(line >> r >> g >> b >> rough >> spec)) if (!(line >> mat_index))
throw std::invalid_argument("Missing material properties"); throw std::runtime_error("Missing material properties");
if (r < 0.0f || r > 255.0f || g < 0.0f || g > 255.0f || b < 0.0f || b > 255.0f)
throw std::invalid_argument("Color values must be between 0 and 255");
if (rough < 0.0f || rough > 1.0f)
throw std::invalid_argument("Roughness must be between 0 and 1");
if (spec < 0.0f || spec > 1.0f)
throw std::invalid_argument("Specular must be between 0 and 1");
_position = glm::vec3(x, y, z); _position = glm::vec3(x, y, z);
_radius = radius; _radius = radius;
_mat->color = glm::vec3(r / 255.0f, g / 255.0f, b / 255.0f); _mat_index = mat_index;
_mat->roughness = rough;
_mat->specular = spec;
_material = _mat;
} }
catch (const std::exception& e) { throw; } catch (const std::exception& e) { throw; }
} }
Sphere(const glm::vec3& position, float radius, const Material *material) Sphere(const glm::vec3& position, float radius, const int mat_index)
: Object(position, material), _radius(radius) {} : Object(position, mat_index), _radius(radius) {}
float getRadius() const { return (_radius); } float getRadius() const { return (_radius); }
Type getType() const override { return Type::SPHERE; } Type getType() const override { return Type::SPHERE; }
private: private:
Material *_mat;
float _radius; float _radius;
}; };

View File

@ -1,4 +1,9 @@
sp 0 -1 -6 2.0 255 255 255 0.0 0.0 MAT 255 255 255 0.0 1.0 2.0
MAT 255 0 0 0.0 1.0 2.0
sp 0 -1 -6 1.0 0
sp 2 -1 -6 1.0 1
sp -2 -1 -6 1.0 1
R 1.0 -2.0 10 R 1.0 -2.0 10

View File

@ -8,13 +8,12 @@ layout(binding = 0, rgba32f) uniform image2D outputImage;
struct GPUObject { struct GPUObject {
vec3 position; // 12 + 4 vec3 position; // 12 + 4
float padding_1; // 4
vec3 color; // 12 + 4 vec3 color; // 12 + 4
float padding_2; // 4 float emission; // 4
float roughness; // 4 float roughness; // 4
float specular; // 4 float specular; // 4
float radius; // 4 float radius; // 4
int type; // 4 int type; // 4 + 12
}; };
layout(std430, binding = 1) buffer ObjectBuffer layout(std430, binding = 1) buffer ObjectBuffer

View File

@ -22,8 +22,8 @@ int main(int argc, char **argv)
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 1); Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 1);
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl"); Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
// Material redMaterial = {glm::vec3(1.0f, 0.2f, 0.2f), 1.0, 1.0, 0.0};
// Material redMaterial = {glm::vec3(1.0f, 0.2f, 0.2f), 1.0, 1.0}; // scene.addMaterial(&redMaterial);
// for (int i = 0; i < 150; i++) // for (int i = 0; i < 150; i++)
// { // {
@ -33,7 +33,7 @@ int main(int argc, char **argv)
// float y = 2.0f * sin(angle * 3.0f); // float y = 2.0f * sin(angle * 3.0f);
// glm::vec3 position(x, y, z); // glm::vec3 position(x, y, z);
// float sphereSize = 0.8f + 0.4f * sin(angle * 2.0f); // float sphereSize = 0.8f + 0.4f * sin(angle * 2.0f);
// scene.addObject(new Sphere(position, sphereSize, &redMaterial)); // scene.addObject(new Sphere(position, sphereSize, 0));
// } // }
GLuint objectSSBO; GLuint objectSSBO;

View File

@ -56,17 +56,26 @@ void Scene::addObject(Object *object)
this->updateGPUData(); this->updateGPUData();
} }
void Scene::addMaterial(Material *material)
{
_materials.push_back(material);
}
void Scene::updateGPUData() void Scene::updateGPUData()
{ {
GPUObject gpu_obj;
Material *mat;
_gpu_objects.clear(); _gpu_objects.clear();
for (const auto& obj : _objects) for (const auto& obj : _objects)
{ {
GPUObject gpu_obj; mat = getMaterial(obj->getMaterialIndex());
gpu_obj.position = obj->getPosition(); gpu_obj.position = obj->getPosition();
gpu_obj.color = obj->getMaterial()->color; gpu_obj.color = mat->color;
gpu_obj.roughness = obj->getMaterial()->roughness; gpu_obj.emission = mat->emission;
gpu_obj.specular = obj->getMaterial()->specular; gpu_obj.roughness = mat->roughness;
gpu_obj.specular = mat->specular;
gpu_obj.type = static_cast<int>(obj->getType()); gpu_obj.type = static_cast<int>(obj->getType());
if (obj->getType() == Object::Type::SPHERE) if (obj->getType() == Object::Type::SPHERE)
@ -75,14 +84,21 @@ void Scene::updateGPUData()
gpu_obj.radius = sphere->getRadius(); gpu_obj.radius = sphere->getRadius();
} }
std::cout << gpu_obj.position.x << " " << gpu_obj.position.y << " " << gpu_obj.position.z << " " << gpu_obj.radius << " " << gpu_obj.roughness << " " << gpu_obj.specular << std::endl;
_gpu_objects.push_back(gpu_obj); _gpu_objects.push_back(gpu_obj);
} }
} }
const std::vector<GPUObject>& Scene::getGPUData() const const std::vector<GPUObject>& Scene::getGPUData() const
{ {
for (const auto& obj : _gpu_objects)
{
std::cout << "objType: " << obj.type << std::endl;
std::cout << "position: " << obj.position.x << " " << obj.position.y << " " << obj.position.z << std::endl;
std::cout << "color: " << obj.color.x << " " << obj.color.y << " " << obj.color.z << std::endl;
std::cout << "mat: " << obj.emission << " " << obj.roughness << " " << obj.specular << std::endl;
std::cout << "Size of GPUObject: " << sizeof(GPUObject) << " bytes" << std::endl;
}
return (_gpu_objects); return (_gpu_objects);
} }
@ -90,3 +106,10 @@ Camera *Scene::getCamera(void) const
{ {
return (_camera); return (_camera);
} }
Material *Scene::getMaterial(int material_index)
{
if (material_index < 0 || material_index >= (int)_materials.size())
throw std::runtime_error("Incorrect material index");
return (_materials[material_index]);
}

View File

@ -21,6 +21,27 @@ SceneParser::SceneParser(Scene *scene) : _scene(scene)
}; };
} }
void SceneParser::parseMaterial(std::stringstream &line)
{
float r,g,b;
float emission;
float roughness;
float specular;
Material *mat;
if (!(line >> r >> g >> b >> emission >> roughness >> specular))
throw std::runtime_error("Material: Missing material properties");
mat = new Material;
mat->color = glm::vec3(r / 255.0f, g / 255.0f, b / 255.0f);
mat->emission = emission;
mat->roughness = roughness;
mat->specular = specular;
_scene->addMaterial(mat);
}
bool SceneParser::parseLine(const std::string &line) bool SceneParser::parseLine(const std::string &line)
{ {
if (line.empty() || line[0] == '#') if (line.empty() || line[0] == '#')
@ -31,16 +52,24 @@ bool SceneParser::parseLine(const std::string &line)
ss >> identifier; ss >> identifier;
auto it = object_parsers.find(identifier); try
if (it != object_parsers.end())
{ {
try { auto it = object_parsers.find(identifier);
if (it != object_parsers.end())
{
Object *obj = it->second(ss); Object *obj = it->second(ss);
(void) _scene->getMaterial(obj->getMaterialIndex()); //verify material
_scene->addObject(obj); _scene->addObject(obj);
} catch (const std::exception& e) {
std::cerr << "Error parsing sphere: " << e.what() << std::endl;
return false;
} }
if (identifier == "MAT")
this->parseMaterial(ss);
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return (false);
} }
return (true); return (true);