Polyphase Game Engine
Loading...
Searching...
No Matches
TriangleBvh.h
Go to the documentation of this file.
1#pragma once
2
3#if EDITOR
4
5#include "EngineTypes.h"
7
8#include <vector>
9
10class Primitive3D;
11
12struct BvhTri
13{
14 glm::vec3 mV0;
15 glm::vec3 mE1;
16 glm::vec3 mE2;
17};
18
19struct BvhNode
20{
21 AABB mBox;
22 uint32_t mFirst = 0; // leaf: first triangle index
23 uint32_t mCount = 0; // leaf: triangle count (0 = interior)
24 uint32_t mRight = 0; // interior: right child (left child is this + 1)
25};
26
27// Static world-space triangle BVH shared by the occlusion baker and viewport
28// surface snapping. Build() reorders the triangles, so indices returned by
29// ClosestHit() are only meaningful against GetTri().
30class TriangleBvh
31{
32public:
33
34 void Build(std::vector<BvhTri>&& tris);
35 void Clear();
36
37 bool IsEmpty() const { return mNodes.empty(); }
38 const BvhTri& GetTri(uint32_t index) const { return mTris[index]; }
39
40 // True if any triangle blocks the segment origin + dir * t, t in (tMin, tMax).
41 bool AnyHit(const glm::vec3& origin, const glm::vec3& dir, float tMin, float tMax) const;
42
43 // Closest hit along dir within (0, tMax).
44 bool ClosestHit(const glm::vec3& origin, const glm::vec3& dir, float tMax, float& outT, uint32_t& outTri) const;
45
46 // Closest hit along dir; returns whether the hit triangle faces away from the ray.
47 bool ClosestHitIsBackFacing(const glm::vec3& origin, const glm::vec3& dir, float tMax, bool& outHit) const;
48
49private:
50
51 uint32_t BuildRecursive(uint32_t first, uint32_t count, std::vector<glm::vec3>& centroids, std::vector<AABB>& boxes);
52
53 std::vector<BvhTri> mTris;
54 std::vector<BvhNode> mNodes;
55};
56
57void AppendTriangles(std::vector<BvhTri>& outTris, const glm::mat4& transform, const glm::vec3* positions, size_t positionStride, uint32_t numVerts, const IndexType* indices, uint32_t numIndices);
58
59// World-space triangles of a StaticMesh3D / InstancedMesh3D / Terrain3D / Voxel3D.
60void GatherPrimitiveTriangles(Primitive3D* prim, std::vector<BvhTri>& outTris);
61
62#endif
uint16_t IndexType
Definition GraphicsTypes.h:122
Definition Primitive3d.h:46
POLYPHASE_API bool Build(const char *pakPath, const std::vector< SourceFile > &files, uint32_t &outEntryCount)
Definition ContentPak.cpp:689
Definition EngineTypes.h:213