+ | 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
{
glm::vec3 color;
float emission;
float roughness;
float specular;
} Material;
@ -26,14 +27,14 @@ class Object
{
protected:
glm::vec3 _position;
const Material *_material;
int _mat_index;
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;
int getMaterialIndex() const {return (_mat_index); }
const glm::vec3 &getPosition() const { return (_position); }
const Material *getMaterial() const { return (_material); }
enum class Type {
SPHERE,

View File

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

View File

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

View File

@ -17,58 +17,36 @@
class Sphere : public Object
{
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 {
float x, y, z, radius;
float r, g, b, rough, spec;
float x, y, z, radius;
int mat_index;
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)
throw std::invalid_argument("Radius must be positive");
throw std::runtime_error("Radius must be positive");
if (!(line >> r >> g >> b >> rough >> spec))
throw std::invalid_argument("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");
if (!(line >> mat_index))
throw std::runtime_error("Missing material properties");
_position = glm::vec3(x, y, z);
_radius = radius;
_mat->color = glm::vec3(r / 255.0f, g / 255.0f, b / 255.0f);
_mat->roughness = rough;
_mat->specular = spec;
_material = _mat;
_mat_index = mat_index;
}
catch (const std::exception& e) { throw; }
}
Sphere(const glm::vec3& position, float radius, const Material *material)
: Object(position, material), _radius(radius) {}
Sphere(const glm::vec3& position, float radius, const int mat_index)
: Object(position, mat_index), _radius(radius) {}
float getRadius() const { return (_radius); }
Type getType() const override { return Type::SPHERE; }
private:
Material *_mat;
float _radius;
};