add test model

This commit is contained in:
MihailRis
2024-06-24 01:05:28 +03:00
parent ba458be334
commit 69ddcb7595
6 changed files with 81 additions and 27 deletions
+1 -1
View File
@@ -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();
+6 -12
View File
@@ -3,8 +3,7 @@
#include <glm/matrix.hpp>
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<Planes i, Planes j>
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<Frustum::Planes a, Frustum::Planes b, Frustum::Planes c>
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<b, c>::k]);
glm::vec3 res = glm::mat3(crosses[ij2k<b, c>::k], -crosses[ij2k<a, c>::k], crosses[ij2k<a, b>::k]) *
glm::vec3(m_planes[a].w, m_planes[b].w, m_planes[c].w);
+23 -13
View File
@@ -6,21 +6,26 @@
#include "../physics/PhysicsSolver.hpp"
#include "../graphics/render/ModelBatch.hpp"
#include "../graphics/core/Model.hpp"
#include "../maths/FrustumCulling.hpp"
#include <glm/ext/matrix_transform.hpp>
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<EntityId>(entity, 1);
registry.emplace<Transform>(entity, pos, size, glm::mat3(1.0f));
registry.emplace<Hitbox>(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<EntityId>(entity, 1);
registry.emplace<Transform>(entity, pos, size/4.0f, glm::mat3(1.0f));
registry.emplace<Hitbox>(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<Transform>();
auto model = assets->get<model::Model>("dingus");
auto model = assets->get<model::Model>("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();
}
}
}
+2 -1
View File
@@ -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_