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

@ -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;
};