shaders are working for models

This commit is contained in:
Clément Le Bihan
2021-06-08 15:52:12 +02:00
parent 8a729414f5
commit 7bff82a5c9
4 changed files with 48 additions and 8 deletions
+11
View File
@@ -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;
}
}
+9
View File
@@ -11,6 +11,7 @@
#include "Drawables/Texture.hpp"
#include "Drawables/ADrawable3D.hpp"
#include "Model/ModelAnimation.hpp"
#include "Shaders/Shaders.hpp"
#include <raylib.h>
#include <vector>
#include <optional>
@@ -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;
+9 -8
View File
@@ -70,7 +70,7 @@ namespace BBM
.addComponent<ControllableComponent>()
.addComponent<AnimatorComponent>()
.addComponent<KeyboardComponent>()
.addComponent<ShaderComponent>("assets/shaders/glsl330/predator.fs")
.addComponent<ShaderComponentModel>("assets/shaders/glsl330/grayscale.fs")
.addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 3)
.addComponent<CollisionComponent>(1)
.addComponent<MovableComponent>()
@@ -81,14 +81,15 @@ namespace BBM
scene->addEntity("camera")
.addComponent<PositionComponent>(8, 20, 7)
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
// scene->addEntity("cube")
// .addComponent<PositionComponent>(5, 0, 5)
// .addComponent<Drawable3DComponent, RAY3D::Cube>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED)
// .addComponent<ControllableComponent>()
// .addComponent<KeyboardComponent>()
// .addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, 3);
scene->addEntity("cube")
.addComponent<PositionComponent>(5, 0, 5)
.addComponent<ShaderComponentDrawable>("assets/shaders/glsl330/grayscale.fs")
.addComponent<Drawable3DComponent, RAY3D::Cube>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED)
.addComponent<ControllableComponent>()
.addComponent<KeyboardComponent>()
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &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;
}
+19
View File
@@ -8,7 +8,9 @@
#include "Component/Renderer/CameraComponent.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include <Model/Model.hpp>
#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<PositionComponent, Drawable3DComponent>()) {
auto *modelShader = _.tryGetComponent<ShaderComponentModel>();
auto *shader = _.tryGetComponent<ShaderComponentModel>();
if (modelShader) {
auto &model = dynamic_cast<RAY::Drawables::Drawables3D::Model &>(*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<RAY::Drawables::Drawables3D::Model &>(*drawable.drawable);
model.resetShader();
}
if (shader) {
RAY::Shader::EndUsingCustomShader();
}
}
this->_window.unuseCamera();