From 032d346151091ecf8053d0137be5b1810586a13b Mon Sep 17 00:00:00 2001 From: Linloir <3145078758@qq.com> Date: Sun, 18 Dec 2022 10:03:17 +0800 Subject: [PATCH] [CORE][ADD] Add light API & function to renderable --- FinalProject/renderable.cpp | 24 ++++++++++++++++++++++++ FinalProject/renderable.h | 9 ++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/FinalProject/renderable.cpp b/FinalProject/renderable.cpp index 289a8c5..ed3074c 100644 --- a/FinalProject/renderable.cpp +++ b/FinalProject/renderable.cpp @@ -6,6 +6,13 @@ Renderable::Renderable(Model* model) : _model(model) {} Renderable::Renderable(Model* model, glm::vec3 position) : _model(model), _position(position) {} +Renderable::~Renderable() { + if (_light != nullptr) { + delete _light; + _light = nullptr; + } +} + void Renderable::setModel(Model* model) { _model = model; } @@ -34,6 +41,19 @@ void Renderable::setScale(float scale) { _scale = glm::vec3(scale); } +ScopedLight Renderable::transformedLight() const { + // Transform the light position to the world space + return _light->toWorldSpace(modelMatrix()); +} + +ScopedLight* Renderable::originalLight() const { + return _light; +} + +void Renderable::makeLight() { + _light = new ScopedLight(glm::vec3(0.0f)); +} + void Renderable::render(ShaderProgram shader) { // Check if initialized if (_model == nullptr) { @@ -50,6 +70,10 @@ void Renderable::render(ShaderProgram shader) { // check here to get global boundary // must check before get boundary void Renderable::checkBoundary() { + if (_model == nullptr) { + return; + } + std::vector temp = {_model->upperBoundVex(),_model->lowerBoundVex()}; _lowerBoundVex = glm::vec3(3e36, 3e36, 3e36); diff --git a/FinalProject/renderable.h b/FinalProject/renderable.h index 6196c0a..008c64f 100644 --- a/FinalProject/renderable.h +++ b/FinalProject/renderable.h @@ -4,6 +4,7 @@ #include #include "model.h" +#include "illuminer.h" #include "shader.h" class Renderable { @@ -17,6 +18,7 @@ private: private: Model* _model = nullptr; + ScopedLight* _light = nullptr; glm::vec3 _position = glm::vec3(0.0f); glm::mat4 _rotation = glm::mat4(1.0f); glm::vec3 _scale = glm::vec3(1.0f); @@ -26,6 +28,7 @@ private: public: Renderable(Model* model); Renderable(Model* model, glm::vec3 position); + ~Renderable(); public: void setModel(Model* model); @@ -36,6 +39,11 @@ public: void scale(float deltaScale); void setScale(float scale); + ScopedLight transformedLight() const; // pass out the light object to scene manager to gather all light sources + ScopedLight* originalLight() const; // pass out the light object to scene manager to set light attributes + bool hasLight() const { return _light != nullptr; } + void makeLight(); // create a light source in the object + inline glm::mat4 modelMatrix() const; inline glm::vec3 upperBoundVex()const; @@ -43,7 +51,6 @@ public: public: void render(ShaderProgram shader); - // check here to get global boundary void checkBoundary(); };