Skip to content

Commit 1c49557

Browse files
committed
[Renderer] frustum culling
1 parent 530224a commit 1c49557

4 files changed

Lines changed: 51 additions & 12 deletions

File tree

include/openminecraft/renderer/common/basics/om_camera.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef OM_CAMERA_HPP
22
#define OM_CAMERA_HPP
33

4+
#include "glm/fwd.hpp"
45
#include "openminecraft/log/om_log_common.hpp"
56
#include "openminecraft/renderer/common/basics/om_position.hpp"
67
#include "openminecraft/renderer/om_renderer_layer.hpp"
@@ -18,13 +19,32 @@ enum OMCameraMovement
1819
Down
1920
};
2021

22+
struct OMCameraPlane
23+
{
24+
glm::vec3 normal;
25+
float d;
26+
};
27+
2128
class OMCamera
2229
{
2330
public:
2431
OMCamera(OMRenderer *renderer, glm::vec3 location, float yaw, float pitch);
2532
~OMCamera() = default;
2633
auto fetchViewMat() -> glm::mat4;
2734
auto fetchProjMat() -> glm::mat4;
35+
auto visible(OMPosition<16, int64_t, float> p, glm::mat4 &rot) -> bool
36+
{
37+
if (p.chunkx == position.chunkx && p.chunky == position.chunky && p.chunkz == position.chunkz)
38+
{
39+
return true;
40+
}
41+
42+
auto r = p - position;
43+
auto l = rot * glm::vec4(r, 1.0f);
44+
l /= l.w;
45+
46+
return !(l.x > 1.0 || l.x < -1.0 || l.y > 1.0 || l.y < -1.0);
47+
}
2848
void modYaw(float delta);
2949
void modPitch(float delta);
3050

@@ -54,6 +74,7 @@ class OMCamera
5474
OMPosition<16, int64_t, float> position;
5575
float yaw = 0.0f;
5676
float pitch = 0.0f;
77+
float fov = 70.0f;
5778
};
5879
} // namespace openminecraft::renderer::common::basics
5980

include/openminecraft/world/om_world_chunk.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ template <int Cs> class OMChunk
113113

114114
int64_t chunkx, chunky, chunkz;
115115

116+
bool visible = false;
117+
116118
private:
117119
std::array<uint32_t, Cs * Cs * Cs> blocks = {};
118120
bool dirty = true;

src/openminecraft-core/renderer/common/basics/om_camera.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ auto OMCamera::fetchProjMat() -> glm::mat4
2626
{
2727
auto extent = renderer->getExtent();
2828
extent = glm::max(extent, glm::vec2(1.0f, 1.0f));
29-
return glm::perspective(glm::radians(70.0f), extent.x / extent.y, 1000.0f, 0.05f);
29+
return glm::perspectiveRH_ZO(glm::radians(fov), extent.x / extent.y, 1000.0f, 0.05f);
3030
}
3131

3232
void OMCamera::modYaw(float d)

src/openminecraft-core/renderer/common/wrap/om_renderer_voxel_manager.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -434,27 +434,43 @@ auto OMVoxelManager::update(basics::OMCamera &camera) -> void
434434
{
435435
auto &chk = ochk.value();
436436

437-
auto pp = basics::OMPosition<16, int64_t, float>();
438-
pp.chunkx = chk.chunkx;
439-
pp.chunky = chk.chunky;
440-
pp.chunkz = chk.chunkz;
441-
442-
auto r = mat * glm::vec4((pp - camera.getPosRaw()), 1.0);
443-
r /= r.w;
444-
445-
if (r.x > 1.0 || r.x < -1.0 || r.y > 1.0 || r.y < -1.0)
437+
auto cc = basics::OMPosition<16, int64_t, float>();
438+
cc.chunkx = chk.chunkx;
439+
cc.chunky = chk.chunky;
440+
cc.chunkz = chk.chunkz;
441+
442+
auto pp = basics::OMPosition<16, int64_t, float>(cc.chunkx, cc.chunky, cc.chunkz);
443+
auto pp2 = basics::OMPosition<16, int64_t, float>(cc.chunkx + 1, cc.chunky, cc.chunkz);
444+
auto pp3 = basics::OMPosition<16, int64_t, float>(cc.chunkx, cc.chunky + 1, cc.chunkz);
445+
auto pp4 = basics::OMPosition<16, int64_t, float>(cc.chunkx, cc.chunky, cc.chunkz + 1);
446+
auto pp5 = basics::OMPosition<16, int64_t, float>(cc.chunkx + 1, cc.chunky + 1, cc.chunkz);
447+
auto pp6 = basics::OMPosition<16, int64_t, float>(cc.chunkx + 1, cc.chunky, cc.chunkz + 1);
448+
auto pp7 = basics::OMPosition<16, int64_t, float>(cc.chunkx, cc.chunky + 1, cc.chunkz + 1);
449+
auto pp8 = basics::OMPosition<16, int64_t, float>(cc.chunkx + 1, cc.chunky + 1, cc.chunkz + 1);
450+
451+
auto visible = camera.visible(pp, mat) || camera.visible(pp2, mat) || camera.visible(pp3, mat) ||
452+
camera.visible(pp4, mat) || camera.visible(pp5, mat) || camera.visible(pp6, mat) ||
453+
camera.visible(pp7, mat) || camera.visible(pp8, mat);
454+
455+
if (chk.visible && !visible)
446456
{
447457
unloadChunk(i);
448-
chk.markDirty();
449458
}
450-
else
459+
else if (chk.visible && visible)
451460
{
452461
if (chk.isDirty())
453462
{
454463
compilerPool->compile(i);
455464
chk.solveDirty();
456465
}
457466
}
467+
else if (!chk.visible && visible)
468+
{
469+
compilerPool->compile(i);
470+
chk.solveDirty();
471+
}
472+
473+
chk.visible = visible;
458474
}
459475
++i;
460476
}

0 commit comments

Comments
 (0)