From 69ddcb7595bac601cd89716f51ae855e0a185f8c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 24 Jun 2024 01:05:28 +0300 Subject: [PATCH] add test model --- res/content/base/models/cube.obj | 46 +++++++++++++++++++++++++++ res/content/base/preload.json | 3 ++ src/graphics/render/WorldRenderer.cpp | 2 +- src/maths/FrustumCulling.hpp | 18 ++++------- src/objects/Entities.cpp | 36 +++++++++++++-------- src/objects/Entities.hpp | 3 +- 6 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 res/content/base/models/cube.obj diff --git a/res/content/base/models/cube.obj b/res/content/base/models/cube.obj new file mode 100644 index 00000000..26a621ff --- /dev/null +++ b/res/content/base/models/cube.obj @@ -0,0 +1,46 @@ +# Blender v2.79 (sub 0) OBJ File: '' +# www.blender.org +mtllib cube.mtl +o Cube +v 0.500000 -0.500000 -0.500000 +v 0.500000 -0.500000 0.500000 +v -0.500000 -0.500000 0.500000 +v -0.500000 -0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v -0.500000 0.500000 -0.500000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +vn 0.0000 -1.0000 0.0000 +vn 0.0000 1.0000 0.0000 +vn 1.0000 0.0000 0.0000 +vn -0.0000 -0.0000 1.0000 +vn -1.0000 -0.0000 -0.0000 +vn 0.0000 0.0000 -1.0000 +usemtl blocks/stone +s off +f 1/1/1 2/2/1 3/3/1 4/4/1 +f 5/5/2 8/6/2 7/7/2 6/8/2 +f 1/1/3 5/9/3 6/10/3 2/11/3 +f 2/12/4 6/13/4 7/7/4 3/14/4 +f 3/15/5 7/16/5 8/17/5 4/4/5 +f 5/5/6 1/18/6 4/19/6 8/20/6 diff --git a/res/content/base/preload.json b/res/content/base/preload.json index 801938a6..238cab00 100644 --- a/res/content/base/preload.json +++ b/res/content/base/preload.json @@ -2,5 +2,8 @@ "sounds": [ "blocks/door_open", "blocks/door_close" + ], + "models": [ + "cube" ] } diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index e6e89b96..c7722fc9 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -196,7 +196,7 @@ void WorldRenderer::renderLevel( drawChunks(level->chunks.get(), camera, shader); shader->uniformMatrix("u_model", glm::mat4(1.0f)); - level->entities->render(assets, *modelBatch); + level->entities->render(assets, *modelBatch, *frustumCulling); modelBatch->render(); skybox->unbind(); diff --git a/src/maths/FrustumCulling.hpp b/src/maths/FrustumCulling.hpp index 309fc1ba..f8372c14 100644 --- a/src/maths/FrustumCulling.hpp +++ b/src/maths/FrustumCulling.hpp @@ -3,8 +3,7 @@ #include -class Frustum -{ +class Frustum { public: Frustum() {}; @@ -12,8 +11,7 @@ public: bool isBoxVisible(const glm::vec3& minp, const glm::vec3& maxp) const; private: - enum Planes - { + enum Planes { Left = 0, Right, Bottom, @@ -25,8 +23,7 @@ private: }; template - struct ij2k - { + struct ij2k { enum { k = i * (9 - i) / 2 + j - 1 }; }; @@ -37,8 +34,7 @@ private: glm::vec3 m_points[8]; }; -inline void Frustum::update(glm::mat4 m) -{ +inline void Frustum::update(glm::mat4 m) { m = glm::transpose(m); m_planes[Left] = m[3] + m[0]; m_planes[Right] = m[3] - m[0]; @@ -78,8 +74,7 @@ inline void Frustum::update(glm::mat4 m) inline bool Frustum::isBoxVisible(const glm::vec3& minp, const glm::vec3& maxp) const { // check box outside/inside of frustum - for (int i = 0; i < Count; i++) - { + for (int i = 0; i < Count; i++) { if ((glm::dot(m_planes[i], glm::vec4(minp.x, minp.y, minp.z, 1.0f)) < 0.0) && (glm::dot(m_planes[i], glm::vec4(maxp.x, minp.y, minp.z, 1.0f)) < 0.0) && (glm::dot(m_planes[i], glm::vec4(minp.x, maxp.y, minp.z, 1.0f)) < 0.0) && @@ -106,8 +101,7 @@ inline bool Frustum::isBoxVisible(const glm::vec3& minp, const glm::vec3& maxp) } template -inline glm::vec3 Frustum::intersection(const glm::vec3* crosses) const -{ +inline glm::vec3 Frustum::intersection(const glm::vec3* crosses) const { float D = glm::dot(glm::vec3(m_planes[a]), crosses[ij2k::k]); glm::vec3 res = glm::mat3(crosses[ij2k::k], -crosses[ij2k::k], crosses[ij2k::k]) * glm::vec3(m_planes[a].w, m_planes[b].w, m_planes[c].w); diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 674ae1f1..bdc59ad6 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -6,21 +6,26 @@ #include "../physics/PhysicsSolver.hpp" #include "../graphics/render/ModelBatch.hpp" #include "../graphics/core/Model.hpp" +#include "../maths/FrustumCulling.hpp" #include void Transform::refresh() { combined = glm::mat4(1.0f); combined = glm::translate(combined, pos); + combined = glm::scale(combined, size); + combined = combined * glm::mat4(rot); } Entities::Entities(Level* level) : level(level) { - auto entity = registry.create(); - glm::vec3 pos(0.5f, 170, 0.5f); - glm::vec3 size(1); - registry.emplace(entity, 1); - registry.emplace(entity, pos, size, glm::mat3(1.0f)); - registry.emplace(entity, pos, size/20.0f); + for (int i = 0; i < 1000; i++) { + auto entity = registry.create(); + glm::vec3 pos(0.5f+rand()%50, 100+i, 0.5f+rand()%50); + glm::vec3 size(1); + registry.emplace(entity, 1); + registry.emplace(entity, pos, size/4.0f, glm::mat3(1.0f)); + registry.emplace(entity, pos, size/2.0f); + } } void Entities::updatePhysics(float delta){ @@ -37,19 +42,24 @@ void Entities::updatePhysics(float delta){ true ); transform.pos = hitbox.position; + transform.rot = glm::rotate(glm::mat4(transform.rot), delta, glm::vec3(0, 1, 0)); if (hitbox.grounded) { - hitbox.velocity.y = 10; + //hitbox.velocity.y = 10; } } } -void Entities::render(Assets* assets, ModelBatch& batch) { +void Entities::render(Assets* assets, ModelBatch& batch, Frustum& frustum) { auto view = registry.view(); - auto model = assets->get("dingus"); + auto model = assets->get("cube"); for (auto [entity, transform] : view.each()) { - transform.refresh(); - batch.pushMatrix(transform.combined); - batch.draw(model); - batch.popMatrix(); + const auto& pos = transform.pos; + const auto& size = transform.size; + if (frustum.isBoxVisible(pos-size, pos+size)) { + transform.refresh(); + batch.pushMatrix(transform.combined); + batch.draw(model); + batch.popMatrix(); + } } } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 0c725f89..b203cfa6 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -23,6 +23,7 @@ struct Transform { class Level; class Assets; class ModelBatch; +class Frustum; class Entity { entt::registry& registry; @@ -51,7 +52,7 @@ class Entities { public: Entities(Level* level); void updatePhysics(float delta); - void render(Assets* assets, ModelBatch& batch); + void render(Assets* assets, ModelBatch& batch, Frustum& frustum); }; #endif // OBJECTS_ENTITIES_HPP_