From 4fbf5a3c1b0e530706023f5fc4be2f68d30ea645 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 3 Nov 2015 13:06:18 +0100 Subject: [PATCH] split geometry lib --- src/ai/AIController.hpp | 2 +- src/ai/ai.cpp | 3 +- src/client/net.cpp | 1 + src/geometry/const.hpp | 18 ++++ src/geometry/distance.hpp | 38 +++++++ src/{model => geometry}/geometry.cpp | 5 +- src/geometry/primitive.hpp | 49 +++++++++ src/geometry/rotation.hpp | 13 +++ src/graphics/mesh.cpp | 2 +- src/graphics/viewport.cpp | 2 +- src/model/Part.hpp | 2 - src/model/Shape.hpp | 2 +- src/model/bounds.hpp | 2 +- src/model/geometry.hpp | 102 ------------------ src/net/net.cpp | 1 + src/server/net.cpp | 1 + src/ui/ui.cpp | 5 +- src/world/Chunk.hpp | 2 +- src/world/Entity.hpp | 2 +- src/world/block.cpp | 2 - src/world/chunk.cpp | 1 + src/world/world.cpp | 6 +- .../IntersectionTest.cpp} | 15 +-- .../IntersectionTest.hpp} | 8 +- tst/net/PacketTest.cpp | 1 + tst/net/PacketTest.hpp | 2 +- 26 files changed, 156 insertions(+), 131 deletions(-) create mode 100644 src/geometry/const.hpp create mode 100644 src/geometry/distance.hpp rename src/{model => geometry}/geometry.cpp (98%) create mode 100644 src/geometry/primitive.hpp create mode 100644 src/geometry/rotation.hpp delete mode 100644 src/model/geometry.hpp rename tst/{model/GeometryTest.cpp => geometry/IntersectionTest.cpp} (90%) rename tst/{model/GeometryTest.hpp => geometry/IntersectionTest.hpp} (68%) diff --git a/src/ai/AIController.hpp b/src/ai/AIController.hpp index 5cbfd0e..15645a2 100644 --- a/src/ai/AIController.hpp +++ b/src/ai/AIController.hpp @@ -2,7 +2,7 @@ #define BLANK_AI_AICONTROLLER_HPP_ #include "../app/IntervalTimer.hpp" -#include "../model/geometry.hpp" +#include "../geometry/primitive.hpp" #include "../world/EntityController.hpp" #include diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp index c47b4d3..8386c59 100644 --- a/src/ai/ai.cpp +++ b/src/ai/ai.cpp @@ -4,7 +4,8 @@ #include "IdleState.hpp" #include "RoamState.hpp" -#include "../model/geometry.hpp" +#include "../geometry/distance.hpp" +#include "../geometry/rotation.hpp" #include "../rand/GaloisLFSR.hpp" #include "../world/Entity.hpp" #include "../world/World.hpp" diff --git a/src/client/net.cpp b/src/client/net.cpp index aba3e76..1c13f9e 100644 --- a/src/client/net.cpp +++ b/src/client/net.cpp @@ -4,6 +4,7 @@ #include "NetworkedInput.hpp" #include "../app/init.hpp" +#include "../geometry/distance.hpp" #include "../io/WorldSave.hpp" #include "../net/Packet.hpp" #include "../world/Chunk.hpp" diff --git a/src/geometry/const.hpp b/src/geometry/const.hpp new file mode 100644 index 0000000..00d33e0 --- /dev/null +++ b/src/geometry/const.hpp @@ -0,0 +1,18 @@ +#ifndef BLANK_GEOMETRY_CONST_HPP_ +#define BLANK_GEOMETRY_CONST_HPP_ + + +namespace blank { + +constexpr float PI = 3.141592653589793238462643383279502884; +constexpr float PI_0p25 = PI * 0.25f; +constexpr float PI_0p5 = PI * 0.5f; +constexpr float PI_1p5 = PI * 1.5f; +constexpr float PI_2p0 = PI * 2.0f; + +constexpr float PI_inv = 1.0f / PI; +constexpr float PI_0p5_inv = 1.0f / PI_0p5; + +} + +#endif diff --git a/src/geometry/distance.hpp b/src/geometry/distance.hpp new file mode 100644 index 0000000..d4bec9b --- /dev/null +++ b/src/geometry/distance.hpp @@ -0,0 +1,38 @@ +#ifndef BLANK_GEOMETRY_DISTANCE_HPP_ +#define BLANK_GEOMETRY_DISTANCE_HPP_ + +#include +#include +#include + + +namespace blank { + +inline float length_squared(const glm::vec3 &v) noexcept { + return dot(v, v); +} + +inline float distance_squared(const glm::vec3 &a, const glm::vec3 &b) noexcept { + return length_squared(a - b); +} + +template +inline bool iszero(const T &v) noexcept { + return length_squared(v) < std::numeric_limits::epsilon(); +} + +template +T manhattan_distance(const glm::tvec3 &a, const glm::tvec3 &b) noexcept { + glm::tvec3 diff(abs(a - b)); + return diff.x + diff.y + diff.z; +} + +template +T manhattan_radius(const glm::tvec3 &v) noexcept { + glm::tvec3 a(abs(v)); + return std::max(a.x, std::max(a.y, a.z)); +} + +} + +#endif diff --git a/src/model/geometry.cpp b/src/geometry/geometry.cpp similarity index 98% rename from src/model/geometry.cpp rename to src/geometry/geometry.cpp index a4ac500..ff28d2e 100644 --- a/src/model/geometry.cpp +++ b/src/geometry/geometry.cpp @@ -1,4 +1,7 @@ -#include "geometry.hpp" +#include "const.hpp" +#include "distance.hpp" +#include "primitive.hpp" +#include "rotation.hpp" #include #include diff --git a/src/geometry/primitive.hpp b/src/geometry/primitive.hpp new file mode 100644 index 0000000..351a3a4 --- /dev/null +++ b/src/geometry/primitive.hpp @@ -0,0 +1,49 @@ +#ifndef BLANK_GEOMETRY_PRIMITIVE_HPP_ +#define BLANK_GEOMETRY_PRIMITIVE_HPP_ + +#include +#include + + +namespace blank { + +struct AABB { + glm::vec3 min; + glm::vec3 max; + + void Adjust() noexcept { + if (max.x < min.x) std::swap(max.x, min.x); + if (max.y < min.y) std::swap(max.y, min.y); + if (max.z < min.z) std::swap(max.z, min.z); + } + + glm::vec3 Center() const noexcept { + return min + (max - min) * 0.5f; + } +}; + +struct Ray { + glm::vec3 orig; + glm::vec3 dir; +}; + +bool Intersection( + const Ray &, + const AABB &, + const glm::mat4 &M, + float *dist = nullptr, + glm::vec3 *normal = nullptr) noexcept; + +bool Intersection( + const AABB &a_box, + const glm::mat4 &a_m, + const AABB &b_box, + const glm::mat4 &b_m, + float &depth, + glm::vec3 &normal) noexcept; + +bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept; + +} + +#endif diff --git a/src/geometry/rotation.hpp b/src/geometry/rotation.hpp new file mode 100644 index 0000000..ac208dc --- /dev/null +++ b/src/geometry/rotation.hpp @@ -0,0 +1,13 @@ +#ifndef BLANK_GEOMETRY_ROTATION_HPP_ +#define BLANK_GEOMETRY_ROTATION_HPP_ + +#include + + +namespace blank { + +glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept; + +} + +#endif diff --git a/src/graphics/mesh.cpp b/src/graphics/mesh.cpp index 4a63d99..5cd3883 100644 --- a/src/graphics/mesh.cpp +++ b/src/graphics/mesh.cpp @@ -4,7 +4,7 @@ #include "SkyBoxMesh.hpp" #include "SpriteMesh.hpp" -#include "../model/geometry.hpp" +#include "../geometry/primitive.hpp" #include #include diff --git a/src/graphics/viewport.cpp b/src/graphics/viewport.cpp index 9abe48f..c46ef28 100644 --- a/src/graphics/viewport.cpp +++ b/src/graphics/viewport.cpp @@ -4,7 +4,7 @@ #include "Viewport.hpp" #include "../app/init.hpp" -#include "../model/geometry.hpp" +#include "../geometry/const.hpp" #include #include diff --git a/src/model/Part.hpp b/src/model/Part.hpp index 8c1db60..9774346 100644 --- a/src/model/Part.hpp +++ b/src/model/Part.hpp @@ -1,8 +1,6 @@ #ifndef BLAMK_MODEL_PART_HPP_ #define BLAMK_MODEL_PART_HPP_ -#include "geometry.hpp" - #include #include #include diff --git a/src/model/Shape.hpp b/src/model/Shape.hpp index 9170475..fe58d01 100644 --- a/src/model/Shape.hpp +++ b/src/model/Shape.hpp @@ -2,7 +2,7 @@ #define BLANK_MODEL_SHAPE_HPP_ #include "CollisionBounds.hpp" -#include "geometry.hpp" +#include "../geometry/primitive.hpp" #include "../graphics/BlockMesh.hpp" #include "../graphics/EntityMesh.hpp" #include "../world/Block.hpp" diff --git a/src/model/bounds.hpp b/src/model/bounds.hpp index 3bbb6d7..df5f77f 100644 --- a/src/model/bounds.hpp +++ b/src/model/bounds.hpp @@ -2,7 +2,7 @@ #define BLANK_MODEL_BOUNDS_HPP_ #include "CollisionBounds.hpp" -#include "geometry.hpp" +#include "../geometry/primitive.hpp" #include #include diff --git a/src/model/geometry.hpp b/src/model/geometry.hpp deleted file mode 100644 index 5065124..0000000 --- a/src/model/geometry.hpp +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef BLANK_MODEL_GEOMETRY_H_ -#define BLANK_MODEL_GEOMETRY_H_ - -#include -#include -#include - - -namespace blank { - -constexpr float PI = 3.141592653589793238462643383279502884; -constexpr float PI_0p25 = PI * 0.25f; -constexpr float PI_0p5 = PI * 0.5f; -constexpr float PI_1p5 = PI * 1.5f; -constexpr float PI_2p0 = PI * 2.0f; - -constexpr float PI_inv = 1.0f / PI; -constexpr float PI_0p5_inv = 1.0f / PI_0p5; - -constexpr float DEG_RAD_FACTOR = PI / 180.0f; -constexpr float RAD_DEG_FACTOR = 180.0f / PI; - -constexpr float deg2rad(float d) noexcept { - return d * DEG_RAD_FACTOR; -} - -constexpr float rad2deg(float r) noexcept { - return r * RAD_DEG_FACTOR; -} - - -inline float length_squared(const glm::vec3 &v) noexcept { - return dot(v, v); -} - -inline float distance_squared(const glm::vec3 &a, const glm::vec3 &b) noexcept { - return length_squared(a - b); -} - - -template -inline bool iszero(const T &v) noexcept { - return length_squared(v) < std::numeric_limits::epsilon(); -} - - -template -T manhattan_distance(const glm::tvec3 &a, const glm::tvec3 &b) noexcept { - glm::tvec3 diff(abs(a - b)); - return diff.x + diff.y + diff.z; -} - -template -T manhattan_radius(const glm::tvec3 &v) noexcept { - glm::tvec3 a(abs(v)); - return std::max(a.x, std::max(a.y, a.z)); -} - - -glm::mat3 find_rotation(const glm::vec3 &a, const glm::vec3 &b) noexcept; - - -struct AABB { - glm::vec3 min; - glm::vec3 max; - - void Adjust() noexcept { - if (max.x < min.x) std::swap(max.x, min.x); - if (max.y < min.y) std::swap(max.y, min.y); - if (max.z < min.z) std::swap(max.z, min.z); - } - - glm::vec3 Center() const noexcept { - return min + (max - min) * 0.5f; - } -}; - -struct Ray { - glm::vec3 orig; - glm::vec3 dir; -}; - -bool Intersection( - const Ray &, - const AABB &, - const glm::mat4 &M, - float *dist = nullptr, - glm::vec3 *normal = nullptr) noexcept; - -bool Intersection( - const AABB &a_box, - const glm::mat4 &a_m, - const AABB &b_box, - const glm::mat4 &b_m, - float &depth, - glm::vec3 &normal) noexcept; - -bool CullTest(const AABB &box, const glm::mat4 &MVP) noexcept; - -} - -#endif diff --git a/src/net/net.cpp b/src/net/net.cpp index 1f4e0a9..9130e8e 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -5,6 +5,7 @@ #include "Packet.hpp" #include "../app/init.hpp" +#include "../geometry/const.hpp" #include "../model/Model.hpp" #include "../world/Entity.hpp" #include "../world/EntityState.hpp" diff --git a/src/server/net.cpp b/src/server/net.cpp index aa1bdeb..8eba857 100644 --- a/src/server/net.cpp +++ b/src/server/net.cpp @@ -3,6 +3,7 @@ #include "Server.hpp" #include "../app/init.hpp" +#include "../geometry/distance.hpp" #include "../io/WorldSave.hpp" #include "../model/Model.hpp" #include "../world/ChunkIndex.hpp" diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index f5664dc..c45b34b 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -13,6 +13,7 @@ #include "../app/init.hpp" #include "../audio/Audio.hpp" #include "../audio/SoundBank.hpp" +#include "../geometry/distance.hpp" #include "../graphics/Font.hpp" #include "../graphics/Viewport.hpp" #include "../io/TokenStreamReader.hpp" @@ -374,8 +375,8 @@ void HUD::UpdatePosition() { void HUD::UpdateOrientation() { std::stringstream s; - s << std::setprecision(3) << "pitch: " << rad2deg(player.GetEntity().Pitch()) - << ", yaw: " << rad2deg(player.GetEntity().Yaw()); + s << std::setprecision(3) << "pitch: " << glm::degrees(player.GetEntity().Pitch()) + << ", yaw: " << glm::degrees(player.GetEntity().Yaw()); orientation_text.Set(env.assets.small_ui_font, s.str()); } diff --git a/src/world/Chunk.hpp b/src/world/Chunk.hpp index eaed28a..b6298b3 100644 --- a/src/world/Chunk.hpp +++ b/src/world/Chunk.hpp @@ -3,7 +3,7 @@ #include "Block.hpp" #include "BlockTypeRegistry.hpp" -#include "../model/geometry.hpp" +#include "../geometry/primitive.hpp" #include #include diff --git a/src/world/Entity.hpp b/src/world/Entity.hpp index 802ea90..4b24944 100644 --- a/src/world/Entity.hpp +++ b/src/world/Entity.hpp @@ -3,8 +3,8 @@ #include "Chunk.hpp" #include "EntityState.hpp" +#include "../geometry/primitive.hpp" #include "../model/Instance.hpp" -#include "../model/geometry.hpp" #include #include diff --git a/src/world/block.cpp b/src/world/block.cpp index 7050da4..d6e5507 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -2,8 +2,6 @@ #include "BlockType.hpp" #include "BlockTypeRegistry.hpp" -#include "../model/geometry.hpp" - #include #include #include diff --git a/src/world/chunk.cpp b/src/world/chunk.cpp index ef9f578..8340d56 100644 --- a/src/world/chunk.cpp +++ b/src/world/chunk.cpp @@ -8,6 +8,7 @@ #include "Generator.hpp" #include "WorldCollision.hpp" #include "../app/Assets.hpp" +#include "../geometry/distance.hpp" #include "../graphics/BlockLighting.hpp" #include "../graphics/BlockMesh.hpp" #include "../graphics/Viewport.hpp" diff --git a/src/world/world.cpp b/src/world/world.cpp index a17b0cb..9372031 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -9,6 +9,8 @@ #include "EntityCollision.hpp" #include "WorldCollision.hpp" #include "../app/Assets.hpp" +#include "../geometry/const.hpp" +#include "../geometry/distance.hpp" #include "../graphics/Format.hpp" #include "../graphics/Viewport.hpp" @@ -204,8 +206,8 @@ void Entity::OrientBody(float dt) noexcept { std::cout << "forward: " << forward << std::endl; std::cout << "facing: " << facing << std::endl; std::cout << "direction: " << direction << std::endl; - std::cout << "difference: " << rad2deg(relative_difference) << "°" << std::endl; - std::cout << "correction: " << rad2deg(correction) << "°" << std::endl; + std::cout << "difference: " << glm::degrees(relative_difference) << "°" << std::endl; + std::cout << "correction: " << glm::degrees(correction) << "°" << std::endl; std::cout << std::endl; } // now rotate body by correction and head by -correction diff --git a/tst/model/GeometryTest.cpp b/tst/geometry/IntersectionTest.cpp similarity index 90% rename from tst/model/GeometryTest.cpp rename to tst/geometry/IntersectionTest.cpp index 82f88ee..41fb81a 100644 --- a/tst/model/GeometryTest.cpp +++ b/tst/geometry/IntersectionTest.cpp @@ -1,25 +1,26 @@ -#include "GeometryTest.hpp" +#include "IntersectionTest.hpp" -#include "model/geometry.hpp" +#include "geometry/const.hpp" +#include "geometry/primitive.hpp" #include #include #include -CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::GeometryTest); +CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::IntersectionTest); namespace blank { namespace test { -void GeometryTest::setUp() { +void IntersectionTest::setUp() { } -void GeometryTest::tearDown() { +void IntersectionTest::tearDown() { } -void GeometryTest::testRayBoxIntersection() { +void IntersectionTest::testRayBoxIntersection() { Ray ray{ { 0, 0, 0 }, { 1, 0, 0 } }; // at origin, pointing right AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin glm::mat4 M(1); // no transformation @@ -63,7 +64,7 @@ void GeometryTest::testRayBoxIntersection() { ); } -void GeometryTest::testBoxBoxIntersection() { +void IntersectionTest::testBoxBoxIntersection() { const float delta = std::numeric_limits::epsilon(); float depth = 0; glm::vec3 normal(0); diff --git a/tst/model/GeometryTest.hpp b/tst/geometry/IntersectionTest.hpp similarity index 68% rename from tst/model/GeometryTest.hpp rename to tst/geometry/IntersectionTest.hpp index 7ba2d2d..7922290 100644 --- a/tst/model/GeometryTest.hpp +++ b/tst/geometry/IntersectionTest.hpp @@ -1,5 +1,5 @@ -#ifndef BLANK_TEST_MODEL_GEOMETRYTEST_H_ -#define BLANK_TEST_MODEL_GEOMETRYTEST_H_ +#ifndef BLANK_TEST_GEOMETRY_INTERSECTIONTEST_H_ +#define BLANK_TEST_GEOMETRY_INTERSECTIONTEST_H_ #include @@ -7,10 +7,10 @@ namespace blank { namespace test { -class GeometryTest +class IntersectionTest : public CppUnit::TestFixture { -CPPUNIT_TEST_SUITE(GeometryTest); +CPPUNIT_TEST_SUITE(IntersectionTest); CPPUNIT_TEST(testRayBoxIntersection); CPPUNIT_TEST(testBoxBoxIntersection); diff --git a/tst/net/PacketTest.cpp b/tst/net/PacketTest.cpp index 248bb61..695010d 100644 --- a/tst/net/PacketTest.cpp +++ b/tst/net/PacketTest.cpp @@ -1,5 +1,6 @@ #include "PacketTest.hpp" +#include "geometry/const.hpp" #include "model/Model.hpp" #include "world/Entity.hpp" diff --git a/tst/net/PacketTest.hpp b/tst/net/PacketTest.hpp index 54839ac..25256d9 100644 --- a/tst/net/PacketTest.hpp +++ b/tst/net/PacketTest.hpp @@ -1,7 +1,7 @@ #ifndef BLANK_TEST_NET_PACKETTEST_HPP_ #define BLANK_TEST_NET_PACKETTEST_HPP_ -#include "model/geometry.hpp" +#include "geometry/primitive.hpp" #include "net/Packet.hpp" #include "world/EntityState.hpp" -- 2.39.2