[CORE][ADD] Add light API & function to renderable

This commit is contained in:
Linloir 2022-12-18 10:03:17 +08:00
parent aeaac1833f
commit 032d346151
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
2 changed files with 32 additions and 1 deletions

View File

@ -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<glm::vec3> temp = {_model->upperBoundVex(),_model->lowerBoundVex()};
_lowerBoundVex = glm::vec3(3e36, 3e36, 3e36);

View File

@ -4,6 +4,7 @@
#include <GLM/ext/matrix_transform.hpp>
#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();
};