Skip to content

Commit

Permalink
[WIP] Add model wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
edunad committed Aug 10, 2023
1 parent fa8be96 commit d2b2273
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 12 deletions.
41 changes: 38 additions & 3 deletions render/include/rawrbox/render/model/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

#include <rawrbox/render/materials/base.hpp>

#ifdef RAWRBOX_SCRIPTING
// #include <rawrbox/render/scripting/wrapper/model_wrapper.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>> {
#else
class ModelBase {
#endif

protected:
bgfx::DynamicVertexBufferHandle _vbdh = BGFX_INVALID_HANDLE; // Vertices - Dynamic
Expand All @@ -17,12 +25,17 @@ namespace rawrbox {
std::unique_ptr<rawrbox::Mesh> _mesh = std::make_unique<rawrbox::Mesh>();
std::unique_ptr<M> _material = std::make_unique<M>();

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

// BGFX DYNAMIC SUPPORT ---
bool _isDynamic = false;
// ----

virtual void updateBuffers() {
if (!this->isDynamicBuffer() || !this->isUploaded()) return;
if (!this->isDynamic() || !this->isUploaded()) return;

const bgfx::Memory* vertMem = bgfx::makeRef(this->_mesh->vertices.data(), static_cast<uint32_t>(this->_mesh->vertices.size()) * M::vLayout().getStride());
const bgfx::Memory* indexMem = bgfx::makeRef(this->_mesh->indices.data(), static_cast<uint32_t>(this->_mesh->indices.size()) * sizeof(uint16_t));
Expand All @@ -45,6 +58,11 @@ namespace rawrbox {
RAWRBOX_DESTROY(this->_ibdh);

this->_mesh.reset();

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

// UTIL ---
Expand All @@ -71,12 +89,12 @@ namespace rawrbox {
return this->_mesh->matrix;
}

[[nodiscard]] virtual bool isDynamicBuffer() const {
[[nodiscard]] virtual bool isDynamic() const {
return this->_isDynamic;
}

[[nodiscard]] virtual bool isUploaded() const {
if (this->isDynamicBuffer()) return bgfx::isValid(this->_ibdh) && bgfx::isValid(this->_vbdh);
if (this->isDynamic()) return bgfx::isValid(this->_ibdh) && bgfx::isValid(this->_vbdh);
return bgfx::isValid(this->_ibh) && bgfx::isValid(this->_vbh);
}

Expand Down Expand Up @@ -114,5 +132,22 @@ namespace rawrbox {
virtual void draw() {
if (!this->isUploaded()) throw std::runtime_error("[RawrBox-Model] Failed to render model, vertex / index buffer is not uploaded");
}

#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() {
return this->_luaWrapper;
}

sol::table& getScriptingTable() {
return this->_table;
}
#endif
};
} // namespace rawrbox
4 changes: 2 additions & 2 deletions render/include/rawrbox/render/model/instanced.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace rawrbox {
if (mesh.empty()) throw std::runtime_error("[RawrBox-InstancedModel] Invalid mesh! Missing vertices / indices!");
this->_mesh = std::make_unique<rawrbox::Mesh>(mesh);

if (this->isUploaded() && this->isDynamicBuffer()) {
if (this->isUploaded() && this->isDynamic()) {
this->updateBuffers();
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@ namespace rawrbox {
ModelBase<M>::draw();
this->_material->process(*this->_mesh); // Set atlas

if (this->isDynamicBuffer()) {
if (this->isDynamic()) {
bgfx::setVertexBuffer(0, this->_vbdh);
bgfx::setIndexBuffer(this->_ibdh);
} else {
Expand Down
4 changes: 4 additions & 0 deletions render/include/rawrbox/render/model/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ namespace rawrbox {
struct Skeleton;
class LightBase;

#ifdef RAWRBOX_SCRIPTING
class Mesh : public std::enable_shared_from_this<rawrbox::Mesh> {
#else
class Mesh {
#endif
private:
bool _canOptimize = true;

Expand Down
8 changes: 4 additions & 4 deletions render/include/rawrbox/render/model/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,22 +350,22 @@ namespace rawrbox {
++it2;
}

if (this->isUploaded() && this->isDynamicBuffer()) this->flattenMeshes(); // Already uploaded? And dynamic? Then update vertices
if (this->isUploaded() && this->isDynamic()) this->flattenMeshes(); // Already uploaded? And dynamic? Then update vertices
}

virtual void removeMesh(size_t index) {
if (index >= this->_meshes.size()) return;
this->_meshes.erase(this->_meshes.begin() + index);

if (this->isUploaded() && this->isDynamicBuffer()) this->flattenMeshes(); // Already uploaded? And dynamic? Then update vertices
if (this->isUploaded() && this->isDynamic()) this->flattenMeshes(); // Already uploaded? And dynamic? Then update vertices
}

virtual rawrbox::Mesh* addMesh(rawrbox::Mesh mesh) {
this->_bbox.combine(mesh.getBBOX());
mesh.owner = this;

auto& a = this->_meshes.emplace_back(std::make_unique<rawrbox::Mesh>(mesh));
if (this->isUploaded() && this->isDynamicBuffer()) {
if (this->isUploaded() && this->isDynamic()) {
this->flattenMeshes(); // Already uploaded? And dynamic? Then update vertices
}

Expand Down Expand Up @@ -451,7 +451,7 @@ namespace rawrbox {
this->animate(*mesh);
// ---

if (this->isDynamicBuffer()) {
if (this->isDynamic()) {
bgfx::setVertexBuffer(0, this->_vbdh, mesh->baseVertex, mesh->totalVertex);
bgfx::setIndexBuffer(this->_ibdh, mesh->baseIndex, mesh->totalIndex);
} else {
Expand Down
2 changes: 1 addition & 1 deletion render/include/rawrbox/render/model/spline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ namespace rawrbox {
ModelBase<M>::draw();
this->_material->process(*this->_mesh);

if (this->isDynamicBuffer()) {
if (this->isDynamic()) {
bgfx::setVertexBuffer(0, this->_vbdh);
bgfx::setIndexBuffer(this->_ibdh);
} else {
Expand Down
2 changes: 1 addition & 1 deletion render/include/rawrbox/render/model/sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace rawrbox {

this->_material->process(*mesh);

if (this->isDynamicBuffer()) {
if (this->isDynamic()) {
bgfx::setVertexBuffer(0, this->_vbdh, mesh->baseVertex, mesh->totalVertex);
bgfx::setIndexBuffer(this->_ibdh, mesh->baseIndex, mesh->totalIndex);
} else {
Expand Down
2 changes: 2 additions & 0 deletions 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_wrapper.hpp>
#include <rawrbox/render/scripting/wrapper/stencil_wrapper.hpp>
#include <rawrbox/render/scripting/wrapper/window_wrapper.hpp>
#include <rawrbox/render/window.hpp>
Expand All @@ -17,6 +18,7 @@ namespace rawrbox {
rawrbox::WindowWrapper::registerLua(lua);
rawrbox::CameraWrapper::registerLua(lua);
rawrbox::StencilWrapper::registerLua(lua);
rawrbox::ModelWrapper<>::registerLua(lua);
}

void registerGlobal(sol::environment& env) override {
Expand Down
15 changes: 15 additions & 0 deletions render/include/rawrbox/render/scripting/wrapper/mesh_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <rawrbox/render/model/mesh.hpp>

#include <sol/sol.hpp>

namespace rawrbox {
class MeshWrapper {
std::shared_ptr<rawrbox::Mesh> ref;

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

#include <rawrbox/render/model/model.hpp>

#include <sol/sol.hpp>

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

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

// UTILS ----
[[nodiscard]] virtual const rawrbox::Vector3f getPos() const {
if (!this->isValid()) return {};
return this->_ref.lock()->getPos();
}

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

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

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

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

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);
}
};
} // namespace rawrbox
2 changes: 1 addition & 1 deletion render/src/model/text3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace rawrbox {

bgfx::setTransform((this->getMatrix() * mesh->matrix).data());

if (this->isDynamicBuffer()) {
if (this->isDynamic()) {
bgfx::setVertexBuffer(0, this->_vbdh, mesh->baseVertex, mesh->totalVertex);
bgfx::setIndexBuffer(this->_ibdh, mesh->baseIndex, mesh->totalIndex);
} else {
Expand Down

0 comments on commit d2b2273

Please sign in to comment.