Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
edunad committed Aug 11, 2023
1 parent d2b2273 commit a42d31d
Show file tree
Hide file tree
Showing 20 changed files with 606 additions and 277 deletions.
33 changes: 17 additions & 16 deletions render/include/rawrbox/render/model/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
#include <rawrbox/render/materials/base.hpp>

#ifdef RAWRBOX_SCRIPTING
// #include <rawrbox/render/scripting/wrapper/model_wrapper.hpp>
#include <rawrbox/scripting/scripting.hpp>
#include <rawrbox/render/scripting/wrapper/model_base_wrapper.hpp>
#include <rawrbox/utils/reference.hpp>
#include <sol/sol.hpp>
#endif
namespace rawrbox {

template <typename M = rawrbox::MaterialBase>
#ifdef RAWRBOX_SCRIPTING
class ModelBase : public std::enable_shared_from_this<rawrbox::ModelBase<M>> {
class ModelBase : public rawrbox::ReferenceContainer<rawrbox::ModelBase<M>> {
#else
class ModelBase {
#endif
Expand All @@ -26,7 +28,6 @@ namespace rawrbox {
std::unique_ptr<M> _material = std::make_unique<M>();

#ifdef RAWRBOX_SCRIPTING
sol::table _table;
sol::object _luaWrapper;
#endif

Expand All @@ -44,8 +45,20 @@ namespace rawrbox {
bgfx::update(this->_ibdh, 0, indexMem);
}

#ifdef RAWRBOX_SCRIPTING
virtual void initializeLua() {
if (this->_luaWrapper.valid()) this->_luaWrapper.abandon();
auto ptr = dynamic_cast<rawrbox::ModelBase<>*>(this);
this->_luaWrapper = sol::make_object(rawrbox::SCRIPTING::getLUA(), rawrbox::ModelBaseWrapper(ptr));
}
#endif

public:
#ifdef RAWRBOX_SCRIPTING
ModelBase() : rawrbox::ReferenceContainer<rawrbox::ModelBase<M>>(this){/*this->initializeLua();*/};
#else
ModelBase() = default;
#endif
ModelBase(ModelBase&&) = delete;
ModelBase& operator=(ModelBase&&) = delete;
ModelBase(const ModelBase&) = delete;
Expand All @@ -61,7 +74,6 @@ namespace rawrbox {

#ifdef RAWRBOX_SCRIPTING
if (this->_luaWrapper.valid()) this->_luaWrapper.abandon();
this->_table = {};
#endif
}

Expand Down Expand Up @@ -134,20 +146,9 @@ namespace rawrbox {
}

#ifdef RAWRBOX_SCRIPTING
virtual void initializeLua(sol::state_view lua) {
this->_table = lua.create_table();

if (this->_luaWrapper.valid()) this->_luaWrapper.abandon();
// this->_luaWrapper = sol::make_object(lua, rawrbox::ModelWrapper<M>(this));
}

sol::object& getScriptingWrapper() {
virtual sol::object& getScriptingWrapper() {
return this->_luaWrapper;
}

sol::table& getScriptingTable() {
return this->_table;
}
#endif
};
} // namespace rawrbox
29 changes: 27 additions & 2 deletions render/include/rawrbox/render/model/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
#include <string>
#include <unordered_map>

#ifdef RAWRBOX_SCRIPTING
// #include <rawrbox/render/scripting/wrapper/model_base_wrapper.hpp>
#include <rawrbox/utils/reference.hpp>
#include <sol/sol.hpp>
#endif

namespace rawrbox {
struct Skeleton;
class LightBase;

#ifdef RAWRBOX_SCRIPTING
class Mesh : public std::enable_shared_from_this<rawrbox::Mesh> {
class Mesh : public rawrbox::ReferenceContainer<rawrbox::Mesh> {
#else
class Mesh {
#endif
Expand All @@ -32,13 +38,28 @@ namespace rawrbox {
rawrbox::Vector3f _pos = {};
rawrbox::Vector4f _angle = {};

// SCRIPTING
#ifdef RAWRBOX_SCRIPTING
sol::object _luaWrapper;
#endif
// -------

#ifdef RAWRBOX_SCRIPTING
virtual void initializeLua();
#endif

public:
#ifdef RAWRBOX_SCRIPTING
Mesh();
virtual ~Mesh();
#else
Mesh() = default;
virtual ~Mesh() = default;
#endif
Mesh(const Mesh&) = default;
Mesh(Mesh&&) = default;
Mesh& operator=(const Mesh&) = default;
Mesh& operator=(Mesh&&) = default;
virtual ~Mesh() = default;

std::string name = "mesh";

Expand Down Expand Up @@ -162,5 +183,9 @@ namespace rawrbox {
virtual void merge(const rawrbox::Mesh& other);
virtual void setOptimizable(bool status);
[[nodiscard]] virtual bool canOptimize(const rawrbox::Mesh& other) const;

#ifdef RAWRBOX_SCRIPTING
virtual sol::object& getScriptingWrapper();
#endif
};
} // namespace rawrbox
16 changes: 15 additions & 1 deletion render/include/rawrbox/render/model/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include <rawrbox/render/static.hpp>
#include <rawrbox/render/utils/anim.hpp>

#ifdef RAWRBOX_SCRIPTING
#include <rawrbox/render/scripting/wrapper/model_wrapper.hpp>
#include <rawrbox/utils/reference.hpp>
#include <sol/sol.hpp>
#endif

#define BGFX_STATE_DEFAULT_3D (0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A)

namespace rawrbox {
Expand Down Expand Up @@ -206,6 +212,15 @@ namespace rawrbox {
}
}

#ifdef RAWRBOX_SCRIPTING
void initializeLua() override {
if (this->_luaWrapper.valid()) this->_luaWrapper.abandon();

auto ptr = dynamic_cast<rawrbox::Model<>*>(this);
this->_luaWrapper = sol::make_object(rawrbox::SCRIPTING::getLUA(), rawrbox::ModelWrapper(ptr));
}
#endif

public:
Model() = default;
Model(const Model&) = delete;
Expand All @@ -220,7 +235,6 @@ namespace rawrbox {
}

virtual void setOptimizable(bool status) { this->_canOptimize = status; }

virtual void optimize() {
#ifndef NDEBUG
size_t old = this->_meshes.size();
Expand Down
4 changes: 3 additions & 1 deletion render/include/rawrbox/render/scripting/plugin.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <rawrbox/render/scripting/wrapper/camera_wrapper.hpp>
#include <rawrbox/render/scripting/wrapper/model_base_wrapper.hpp>
#include <rawrbox/render/scripting/wrapper/model_wrapper.hpp>
#include <rawrbox/render/scripting/wrapper/stencil_wrapper.hpp>
#include <rawrbox/render/scripting/wrapper/window_wrapper.hpp>
Expand All @@ -18,7 +19,8 @@ namespace rawrbox {
rawrbox::WindowWrapper::registerLua(lua);
rawrbox::CameraWrapper::registerLua(lua);
rawrbox::StencilWrapper::registerLua(lua);
rawrbox::ModelWrapper<>::registerLua(lua);
rawrbox::ModelBaseWrapper::registerLua(lua);
rawrbox::ModelWrapper::registerLua(lua);
}

void registerGlobal(sol::environment& env) override {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <rawrbox/math/matrix4x4.hpp>
#include <rawrbox/math/vector3.hpp>
#include <rawrbox/math/vector4.hpp>
#include <rawrbox/render/materials/base.hpp>
#include <rawrbox/utils/reference.hpp>

#include <sol/sol.hpp>

namespace rawrbox {
template <typename T>
class ModelBase;

class ModelBaseWrapper {
protected:
rawrbox::Reference<rawrbox::ModelBase<rawrbox::MaterialBase>> _ref;

public:
ModelBaseWrapper(rawrbox::ModelBase<rawrbox::MaterialBase>* ref);
ModelBaseWrapper(const ModelBaseWrapper&) = default;
ModelBaseWrapper(ModelBaseWrapper&&) = default;
ModelBaseWrapper& operator=(const ModelBaseWrapper&) = default;
ModelBaseWrapper& operator=(ModelBaseWrapper&&) = default;
virtual ~ModelBaseWrapper();

// UTILS ----
[[nodiscard]] virtual const rawrbox::Vector3f getPos() const;
virtual void setPos(const rawrbox::Vector3f& pos);

[[nodiscard]] virtual const rawrbox::Vector3f getScale() const;
virtual void setScale(const rawrbox::Vector3f& scale);

[[nodiscard]] virtual const rawrbox::Vector4f getAngle() const;
virtual void setAngle(const rawrbox::Vector4f& ang);

virtual void setEulerAngle(const rawrbox::Vector3f& ang);

[[nodiscard]] virtual const rawrbox::Matrix4x4 getMatrix() const;

[[nodiscard]] virtual bool isDynamic() const;

[[nodiscard]] virtual bool isUploaded() const;
// ------

[[nodiscard]] bool isValid() const;

static void registerLua(sol::state& lua);
};
} // namespace rawrbox
110 changes: 29 additions & 81 deletions render/include/rawrbox/render/scripting/wrapper/model_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,99 +1,47 @@
#pragma once

#include <rawrbox/render/model/model.hpp>
#include <rawrbox/math/matrix4x4.hpp>
#include <rawrbox/math/vector3.hpp>
#include <rawrbox/math/vector4.hpp>
#include <rawrbox/render/materials/base.hpp>
#include <rawrbox/render/scripting/wrapper/model_base_wrapper.hpp>
#include <rawrbox/utils/reference.hpp>

#include <sol/sol.hpp>

namespace rawrbox {
template <typename M = rawrbox::MaterialBase>
class ModelWrapper {
std::weak_ptr<rawrbox::ModelBase<M>> _ref;
class ModelWrapper : public rawrbox::ModelBaseWrapper {

public:
ModelWrapper(rawrbox::ModelBase<M>& ref) : _ref(ref.weak_from_this()) {}
ModelWrapper(rawrbox::ModelBase<rawrbox::MaterialBase>* ref);
ModelWrapper(const ModelWrapper&) = default;
ModelWrapper(ModelWrapper&&) noexcept = default;
ModelWrapper(ModelWrapper&&) = default;
ModelWrapper& operator=(const ModelWrapper&) = default;
ModelWrapper& operator=(ModelWrapper&&) noexcept = default;
virtual ~ModelWrapper() { this->_ref.reset(); }
ModelWrapper& operator=(ModelWrapper&&) = default;
~ModelWrapper() override = default;

// UTILS ----
[[nodiscard]] virtual const rawrbox::Vector3f getPos() const {
if (!this->isValid()) return {};
return this->_ref.lock()->getPos();
}
// ANIMS ---
virtual void playAnimation(const std::string& name, bool loop = true, float speed = 1.F);
virtual void stopAnimation(const std::string& name);
// ---

virtual void setPos(const rawrbox::Vector3f& pos) {
if (!this->isValid()) return;
this->_ref.lock()->setPos(pos);
}
// UTILS ---
virtual void setOptimizable(bool optimize);
virtual void optimize();

[[nodiscard]] virtual const rawrbox::Vector3f getScale() const {
if (!this->isValid()) return {};
return this->_ref.lock()->getScale();
}
void setPos(const rawrbox::Vector3f& pos) override;
void setAngle(const rawrbox::Vector4f& angle) override;
void setEulerAngle(const rawrbox::Vector3f& angle) override;
void setScale(const rawrbox::Vector3f& size) override;

virtual void setScale(const rawrbox::Vector3f& scale) {
if (!this->isValid()) return;
this->_ref.lock()->setScale(scale);
}
[[nodiscard]] virtual const rawrbox::BBOX getBBOX() const;
[[nodiscard]] virtual size_t totalMeshes() const;
[[nodiscard]] virtual bool empty() const;

[[nodiscard]] virtual const rawrbox::Vector4f getAngle() const {
if (!this->isValid()) return {};
return this->_ref.lock()->getAngle();
}
virtual void removeMeshByName(const std::string& id);
virtual void removeMesh(size_t index);
// ---

virtual void setAngle(const rawrbox::Vector4f& ang) {
if (!this->isValid()) return;
this->_ref.lock()->setAngle(ang);
}

virtual void setEulerAngle(const rawrbox::Vector3f& ang) {
if (!this->isValid()) return;
this->_ref.lock()->setEulerAngle(ang);
}

[[nodiscard]] virtual const rawrbox::Matrix4x4 getMatrix() const {
if (!this->isValid()) return {};
return this->_ref.lock()->getMatrix();
}

[[nodiscard]] virtual bool isDynamic() const {
if (!this->isValid()) return false;
return this->_ref.lock()->isDynamic();
}

[[nodiscard]] virtual bool isUploaded() const {
if (!this->isValid()) return false;
return this->_ref.lock()->isUploaded();
}
// ------

[[nodiscard]] bool isValid() const {
return !this->_ref.expired();
}

static void registerLua(sol::state& lua) {
lua.new_usertype<ModelWrapper<M>>("ModelBase<>",
sol::no_constructor,

// UTILS ----
"getPos", &ModelWrapper<M>::getPos,
"setPos", &ModelWrapper<M>::setPos,

"getScale", &ModelWrapper<M>::getScale,
"setScale", &ModelWrapper<M>::setScale,

"getAngle", &ModelWrapper<M>::getAngle,
"setAngle", &ModelWrapper<M>::setAngle,
"setEulerAngle", &ModelWrapper<M>::setEulerAngle,

"getMatrix", &ModelWrapper<M>::getMatrix,
"isDynamic", &ModelWrapper<M>::isDynamic,
"isUploaded", &ModelWrapper<M>::isUploaded,
// --------------

"isValid", &ModelWrapper<M>::isValid);
}
static void registerLua(sol::state& lua);
};
} // namespace rawrbox
Loading

0 comments on commit a42d31d

Please sign in to comment.