diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Frustum.cpp | 114 | ||||
-rw-r--r-- | src/Frustum.hpp | 33 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 40 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 1 |
4 files changed, 81 insertions, 107 deletions
diff --git a/src/Frustum.cpp b/src/Frustum.cpp index e928e6b..ef60eb0 100644 --- a/src/Frustum.cpp +++ b/src/Frustum.cpp @@ -1,63 +1,63 @@ #include "Frustum.hpp" -#include <glm/gtc/type_ptr.hpp> - -void Frustum::NormalizePlane(FrustumSide side) { - float magnitude = (float) sqrt(frustum[side][A] * frustum[side][A] + frustum[side][B] * frustum[side][B] + frustum[side][C] * frustum[side][C]); - - frustum[side][A] /= magnitude; - frustum[side][B] /= magnitude; - frustum[side][C] /= magnitude; - frustum[side][D] /= magnitude; +Frustum::Frustum(const glm::mat4 &vpMat) { + planes[RIGHT] = glm::vec4( + vpMat[0][3] - vpMat[0][0], + vpMat[1][3] - vpMat[1][0], + vpMat[2][3] - vpMat[2][0], + vpMat[3][3] - vpMat[3][0]); + + planes[LEFT] = glm::vec4( + vpMat[0][3] + vpMat[0][0], + vpMat[1][3] + vpMat[1][0], + vpMat[2][3] + vpMat[2][0], + vpMat[3][3] + vpMat[3][0]); + + planes[BOTTOM] = glm::vec4( + vpMat[0][3] + vpMat[0][1], + vpMat[1][3] + vpMat[1][1], + vpMat[2][3] + vpMat[2][1], + vpMat[3][3] + vpMat[3][1]); + + planes[TOP] = glm::vec4( + vpMat[0][3] - vpMat[0][1], + vpMat[1][3] - vpMat[1][1], + vpMat[2][3] - vpMat[2][1], + vpMat[3][3] - vpMat[3][1]); + + planes[FAR] = glm::vec4( + vpMat[0][3] - vpMat[0][2], + vpMat[1][3] - vpMat[1][2], + vpMat[2][3] - vpMat[2][2], + vpMat[3][3] - vpMat[3][2]); + + planes[NEAR] = glm::vec4( + vpMat[0][3] + vpMat[0][2], + vpMat[1][3] + vpMat[1][2], + vpMat[2][3] + vpMat[2][2], + vpMat[3][3] + vpMat[3][2]); + + for (auto &plane : planes) { + float magnitude = sqrt(plane.x * plane.x + plane.y * plane.y + plane.z * plane.z); + plane.x /= magnitude; + plane.y /= magnitude; + plane.z /= magnitude; + plane.w /= magnitude; + } } -void Frustum::UpdateFrustum(const glm::mat4& vpmat) { - vp = vpmat; - float *clip = glm::value_ptr(vp); - - frustum[RIGHT][A] = clip[3] - clip[0]; - frustum[RIGHT][B] = clip[7] - clip[4]; - frustum[RIGHT][C] = clip[11] - clip[8]; - frustum[RIGHT][D] = clip[15] - clip[12]; - NormalizePlane(RIGHT); - - - frustum[LEFT][A] = clip[3] + clip[0]; - frustum[LEFT][B] = clip[7] + clip[4]; - frustum[LEFT][C] = clip[11] + clip[8]; - frustum[LEFT][D] = clip[15] + clip[12]; - NormalizePlane(LEFT); - - frustum[BOTTOM][A] = clip[3] + clip[1]; - frustum[BOTTOM][B] = clip[7] + clip[5]; - frustum[BOTTOM][C] = clip[11] + clip[9]; - frustum[BOTTOM][D] = clip[15] + clip[13]; - NormalizePlane(BOTTOM); - - frustum[TOP][A] = clip[3] - clip[1]; - frustum[TOP][B] = clip[7] - clip[5]; - frustum[TOP][C] = clip[11] - clip[9]; - frustum[TOP][D] = clip[15] - clip[13]; - NormalizePlane(TOP); - - frustum[BACK][A] = clip[3] - clip[2]; - frustum[BACK][B] = clip[7] - clip[6]; - frustum[BACK][C] = clip[11] - clip[10]; - frustum[BACK][D] = clip[15] - clip[14]; - NormalizePlane(BACK); - - frustum[FRONT][A] = clip[3] + clip[2]; - frustum[FRONT][B] = clip[7] + clip[6]; - frustum[FRONT][C] = clip[11] + clip[10]; - frustum[FRONT][D] = clip[15] + clip[14]; - NormalizePlane(FRONT); +bool Frustum::TestPoint(const glm::vec3 &pos) { + for (const auto &plane : planes) { + if (GetDistanceToPoint(plane, pos) < 0) + return false; + } + return true; } -bool Frustum::TestPoint(VectorF point) { - for (int i = 0; i < 6; i++) { - if (frustum[i][A] * point.x + frustum[i][B] * point.y + frustum[i][C] * point.z + frustum[i][D] <= 0) { - return false; - } - } - return true; -}
\ No newline at end of file +bool Frustum::TestSphere(const glm::vec3 &pos, float radius) { + for (const auto &plane : planes) { + if (GetDistanceToPoint(plane, pos) < -radius) + return false; + } + return true; +} diff --git a/src/Frustum.hpp b/src/Frustum.hpp index 4817cd4..15ea7e0 100644 --- a/src/Frustum.hpp +++ b/src/Frustum.hpp @@ -1,35 +1,30 @@ #pragma once #include <glm/glm.hpp> -#include "Vector.hpp" class Frustum { enum FrustumSide { - RIGHT = 0, + RIGHT, LEFT, BOTTOM, TOP, - BACK, - FRONT, - }; - enum PlaneData { - A = 0, - B, - C, - D, + FAR, + NEAR, + SIDE_COUNT, }; - glm::mat4 vp; - float frustum[6][4]; - void NormalizePlane(FrustumSide side); + glm::vec4 planes[SIDE_COUNT]; + +public: + Frustum(const glm::mat4 &vpMat); -public: - Frustum() = default; + ~Frustum() = default; - ~Frustum() = default; + inline static float GetDistanceToPoint(const glm::vec4 &plane, const glm::vec3 &pos) { + return plane.x * pos.x + plane.y * pos.y + plane.z * pos.z + plane.w; + } - void UpdateFrustum(const glm::mat4& vpmat); + bool TestPoint(const glm::vec3 &pos); - //Return true, if tested point is visible - bool TestPoint(VectorF point); + bool TestSphere(const glm::vec3 &pos, float radius); };
\ No newline at end of file diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index aa64dc0..5d0d6a8 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -148,7 +148,6 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) { RendererWorld::RendererWorld(GameState* ptr) { gs = ptr; - frustum = std::make_unique<Frustum>(); MaxRenderingDistance = 2; numOfWorkers = _max(1, (signed int) std::thread::hardware_concurrency() - 2); @@ -396,38 +395,19 @@ void RendererWorld::Render(RenderState & renderState) { glUniform2f(windowSizeLoc, renderState.WindowWidth, renderState.WindowHeight); glCheckError(); - frustum->UpdateFrustum(projView); + Frustum frustum(projView); size_t culledSections = sections.size(); - for (auto& section : sections) { - const static Vector sectionCorners[] = { - Vector(0, 0, 0), - Vector(0, 0, 16), - Vector(0, 16, 0), - Vector(0, 16, 16), - Vector(16, 0, 0), - Vector(16, 0, 16), - Vector(16, 16, 0), - Vector(16, 16, 16), - }; - bool isVisible = false; - for (const auto &it : sectionCorners) { - VectorF point(section.second.GetPosition().x * 16 + it.x, - section.second.GetPosition().y * 16 + it.y, - section.second.GetPosition().z * 16 + it.z); - if (frustum->TestPoint(point)) { - isVisible = true; - break; - } - } - - double lengthToSection = (gs->player->pos - - VectorF(section.first.x*16, - section.first.y*16, - section.first.z*16) - ).GetLength(); + for (auto& section : sections) { + glm::vec3 point{ + section.second.GetPosition().x * 16 + 8, + section.second.GetPosition().y * 16 + 8, + section.second.GetPosition().z * 16 + 8 + }; + + bool isVisible = frustum.TestSphere(point, 16.0f); - if (!isVisible && lengthToSection > 30.0f) { + if (!isVisible) { culledSections--; continue; } diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index 77a02fd..2dab4f0 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -45,7 +45,6 @@ class RendererWorld { std::map<Vector, RendererSection> sections; Shader *blockShader; void UpdateAllSections(VectorF playerPos); - std::unique_ptr<Frustum> frustum; //Entities Shader *entityShader; std::vector<RendererEntity> entities; |