+ | Material system

This commit is contained in:
TheRedShip
2024-12-24 12:02:52 +01:00
parent 216e9a684a
commit 2f217b50e9
5 changed files with 18 additions and 16 deletions

View File

@ -26,14 +26,14 @@ class Object
{ {
protected: protected:
glm::vec3 _position; glm::vec3 _position;
Material _material; const Material *_material;
public: public:
Object(const glm::vec3& position, const Material& material) : _position(position), _material(material) {} Object(const glm::vec3& position, const Material *material) : _position(position), _material(material) {}
virtual ~Object() = default; virtual ~Object() = default;
const glm::vec3 &getPosition() const { return (_position); } const glm::vec3 &getPosition() const { return (_position); }
const Material& getMaterial() const { return (_material); } const Material *getMaterial() const { return (_material); }
enum class Type { enum class Type {
SPHERE, SPHERE,

View File

@ -37,13 +37,13 @@ class Scene
~Scene(); ~Scene();
Camera *getCamera(void) const; Camera *getCamera(void) const;
void addObject(std::unique_ptr<Object> object); void addObject(Object *object);
void updateGPUData(); void updateGPUData();
const std::vector<GPUObject>& getGPUData() const; const std::vector<GPUObject>& getGPUData() const;
private: private:
std::vector<std::unique_ptr<Object>> _objects; std::vector<Object *> _objects;
std::vector<GPUObject> _gpuObjects; std::vector<GPUObject> _gpuObjects;
Camera *_camera; Camera *_camera;

View File

@ -18,7 +18,7 @@
class Sphere : public Object class Sphere : public Object
{ {
public: public:
Sphere(const glm::vec3& position, float radius, const Material& material) Sphere(const glm::vec3& position, float radius, const Material *material)
: Object(position, material), _radius(radius) {} : Object(position, material), _radius(radius) {}
float getRadius() const { return (_radius); } float getRadius() const { return (_radius); }

View File

@ -30,7 +30,7 @@ int main(void)
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);
window.getScene()->addObject(std::make_unique<Sphere>(position, sphereSize, redMaterial)); window.getScene()->addObject(new Sphere(position, sphereSize, &redMaterial));
} }
GLuint objectSSBO; GLuint objectSSBO;
@ -49,6 +49,8 @@ int main(void)
const std::vector<GPUObject> &gpu_data = window.getScene()->getGPUData(); const std::vector<GPUObject> &gpu_data = window.getScene()->getGPUData();
window.getScene()->updateGPUData();
// Update SSBO with latest object data // Update SSBO with latest object data
glBindBuffer(GL_SHADER_STORAGE_BUFFER, objectSSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER, objectSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, gpu_data.size() * sizeof(GPUObject), gpu_data.data(), GL_DYNAMIC_DRAW); glBufferData(GL_SHADER_STORAGE_BUFFER, gpu_data.size() * sizeof(GPUObject), gpu_data.data(), GL_DYNAMIC_DRAW);

View File

@ -27,9 +27,9 @@ Camera *Scene::getCamera(void) const
return (_camera); return (_camera);
} }
void Scene::addObject(std::unique_ptr<Object> object) void Scene::addObject(Object *object)
{ {
_objects.push_back(std::move(object)); _objects.push_back(object);
this->updateGPUData(); this->updateGPUData();
} }
@ -41,13 +41,13 @@ void Scene::updateGPUData()
{ {
GPUObject gpuObj; GPUObject gpuObj;
gpuObj.position = obj->getPosition(); gpuObj.position = obj->getPosition();
gpuObj.color = obj->getMaterial().color; gpuObj.color = obj->getMaterial()->color;
gpuObj.roughness = obj->getMaterial().roughness; gpuObj.roughness = obj->getMaterial()->roughness;
gpuObj.specular = obj->getMaterial().specular; gpuObj.specular = obj->getMaterial()->specular;
gpuObj.type = static_cast<int>(obj->getType()); gpuObj.type = static_cast<int>(obj->getType());
if (obj->getType() == Object::Type::SPHERE) { if (obj->getType() == Object::Type::SPHERE) {
auto sphere = static_cast<const Sphere*>(obj.get()); auto sphere = static_cast<const Sphere*>(obj);
gpuObj.radius = sphere->getRadius(); gpuObj.radius = sphere->getRadius();
} }