diff --git a/lib/Ray/sources/Model/Model.cpp b/lib/Ray/sources/Model/Model.cpp index aab5fdc6..e99c151d 100644 --- a/lib/Ray/sources/Model/Model.cpp +++ b/lib/Ray/sources/Model/Model.cpp @@ -106,4 +106,15 @@ namespace RAY::Drawables::Drawables3D { { DrawModelEx(*this->_model, this->_position, this->_rotationAxis, this->_rotationAngle, this->_scale, this->_color); } + + void Model::setShader(const RAY::Shader &shader) + { + this->_originalShader = this->_model->materials[0].shader; + this->_model->materials[0].shader = *shader.getShaderPtr(); + } + + void Model::resetShader() + { + this->_model->materials[0].shader = this->_originalShader; + } } \ No newline at end of file diff --git a/lib/Ray/sources/Model/Model.hpp b/lib/Ray/sources/Model/Model.hpp index 7a7e3fdf..943385b8 100644 --- a/lib/Ray/sources/Model/Model.hpp +++ b/lib/Ray/sources/Model/Model.hpp @@ -11,6 +11,7 @@ #include "Drawables/Texture.hpp" #include "Drawables/ADrawable3D.hpp" #include "Model/ModelAnimation.hpp" +#include "Shaders/Shaders.hpp" #include #include #include @@ -77,6 +78,12 @@ namespace RAY::Drawables::Drawables3D { //! @return Scale const RAY::Vector3 & getScale(void); + //! @brief Set a shader on the model + void setShader(const RAY::Shader &shader); + + //! @brief Set the original shader (used to disable a shader) + void resetShader(); + void drawOn(RAY::Window &) override; private: @@ -90,6 +97,8 @@ namespace RAY::Drawables::Drawables3D { float _rotationAngle; //! @brief Scale of the shape RAY::Vector3 _scale; + //! @brief The original shaderId used to disable a shader effect + ::Shader _originalShader = {}; static RAY::Cache<::Model> _modelsCache; diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index e6a0dc33..79b112d3 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -70,7 +70,7 @@ namespace BBM .addComponent() .addComponent() .addComponent() - .addComponent("assets/shaders/glsl330/predator.fs") + .addComponent("assets/shaders/glsl330/grayscale.fs") .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(1) .addComponent() @@ -81,14 +81,15 @@ namespace BBM scene->addEntity("camera") .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); -// scene->addEntity("cube") -// .addComponent(5, 0, 5) -// .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) -// .addComponent() -// .addComponent() -// .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 3); + scene->addEntity("cube") + .addComponent(5, 0, 5) + .addComponent("assets/shaders/glsl330/grayscale.fs") + .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) + .addComponent() + .addComponent() + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 3); std::srand(std::time(nullptr)); - MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); + //MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; } diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index cdabb452..5ebfe531 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -8,7 +8,9 @@ #include "Component/Renderer/CameraComponent.hpp" #include "Component/Position/PositionComponent.hpp" #include "Component/Renderer/Drawable2DComponent.hpp" +#include #include "Drawables/ADrawable3D.hpp" +#include "Component/Shaders/ShaderComponent.hpp" namespace BBM { @@ -29,8 +31,25 @@ namespace BBM this->_window.useCamera(this->_camera); for (auto &[_, pos, drawable] : this->_wal.scene->view()) { + auto *modelShader = _.tryGetComponent(); + auto *shader = _.tryGetComponent(); + + if (modelShader) { + auto &model = dynamic_cast(*drawable.drawable); + model.setShader(modelShader->getShader()); + } + if (shader) { + RAY::Shader::BeginUsingCustomShader(shader->getShader()); + } drawable.drawable->setPosition(pos.position); drawable.drawable->drawOn(this->_window); + if (modelShader) { + auto &model = dynamic_cast(*drawable.drawable); + model.resetShader(); + } + if (shader) { + RAY::Shader::EndUsingCustomShader(); + } } this->_window.unuseCamera();