ADD: BVH Selection ongoing
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <fastgltf/util.hpp>
|
||||
#include <optional>
|
||||
#include "tangent_space.h"
|
||||
#include "mesh_bvh.h"
|
||||
//> loadimg
|
||||
std::optional<AllocatedImage> load_image(VulkanEngine *engine, fastgltf::Asset &asset, fastgltf::Image &image, bool srgb)
|
||||
{
|
||||
@@ -592,18 +593,25 @@ std::optional<std::shared_ptr<LoadedGLTF> > loadGltf(VulkanEngine *engine, std::
|
||||
newSurface.bounds.origin = (maxpos + minpos) / 2.f;
|
||||
newSurface.bounds.extents = (maxpos - minpos) / 2.f;
|
||||
newSurface.bounds.sphereRadius = glm::length(newSurface.bounds.extents);
|
||||
newSurface.bounds.type = BoundsType::Box;
|
||||
newSurface.bounds.type = BoundsType::Mesh;
|
||||
}
|
||||
else
|
||||
{
|
||||
newSurface.bounds.origin = glm::vec3(0.0f);
|
||||
newSurface.bounds.extents = glm::vec3(0.5f);
|
||||
newSurface.bounds.sphereRadius = glm::length(newSurface.bounds.extents);
|
||||
newSurface.bounds.type = BoundsType::Box;
|
||||
newSurface.bounds.type = BoundsType::Mesh;
|
||||
}
|
||||
newmesh->surfaces.push_back(newSurface);
|
||||
}
|
||||
|
||||
// Build CPU BVH for precise picking over this mesh (triangle-level).
|
||||
{
|
||||
std::span<const Vertex> vSpan(vertices.data(), vertices.size());
|
||||
std::span<const uint32_t> iSpan(indices.data(), indices.size());
|
||||
newmesh->bvh = build_mesh_bvh(*newmesh, vSpan, iSpan);
|
||||
}
|
||||
|
||||
newmesh->meshBuffers = engine->_resourceManager->uploadMesh(indices, vertices);
|
||||
// If CPU vectors ballooned for this mesh, release capacity back to the OS
|
||||
auto shrink_if_huge = [](auto &vec, size_t elemSizeBytes) {
|
||||
|
||||
@@ -45,6 +45,8 @@ struct GeoSurface
|
||||
std::shared_ptr<GLTFMaterial> material;
|
||||
};
|
||||
|
||||
struct MeshBVH;
|
||||
|
||||
struct MeshAsset
|
||||
{
|
||||
std::string name;
|
||||
@@ -52,6 +54,9 @@ struct MeshAsset
|
||||
|
||||
std::vector<GeoSurface> surfaces;
|
||||
GPUMeshBuffers meshBuffers;
|
||||
|
||||
// Optional CPU BVH for precise picking / queries.
|
||||
std::shared_ptr<MeshBVH> bvh;
|
||||
};
|
||||
|
||||
struct LoadedGLTF : public IRenderable
|
||||
|
||||
@@ -115,6 +115,17 @@ public:
|
||||
float scene_update_time = 0.f;
|
||||
} stats;
|
||||
|
||||
struct PickingDebug
|
||||
{
|
||||
bool usedMeshBVH = false;
|
||||
bool meshBVHHit = false;
|
||||
bool meshBVHFallbackBox = false;
|
||||
uint32_t meshBVHPrimCount = 0;
|
||||
uint32_t meshBVHNodeCount = 0;
|
||||
};
|
||||
|
||||
const PickingDebug &getPickingDebug() const { return pickingDebug; }
|
||||
|
||||
private:
|
||||
EngineContext *_context = nullptr;
|
||||
|
||||
@@ -126,4 +137,6 @@ private:
|
||||
std::unordered_map<std::string, std::shared_ptr<Node> > loadedNodes;
|
||||
std::unordered_map<std::string, MeshInstance> dynamicMeshInstances;
|
||||
std::unordered_map<std::string, GLTFInstance> dynamicGLTFInstances;
|
||||
|
||||
PickingDebug pickingDebug{};
|
||||
};
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "vk_swapchain.h"
|
||||
#include "core/engine_context.h"
|
||||
#include "mesh_bvh.h"
|
||||
|
||||
#include "glm/gtx/transform.hpp"
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
@@ -14,6 +15,13 @@
|
||||
|
||||
namespace
|
||||
{
|
||||
struct BoundsHitDebug
|
||||
{
|
||||
bool usedBVH = false;
|
||||
bool bvhHit = false;
|
||||
bool fallbackBox = false;
|
||||
};
|
||||
|
||||
// Ray / oriented-box intersection in world space using object-local AABB.
|
||||
// Returns true when hit; outWorldHit is the closest hit point in world space.
|
||||
bool intersect_ray_box(const glm::vec3 &rayOrigin,
|
||||
@@ -262,14 +270,18 @@ namespace
|
||||
}
|
||||
|
||||
// Ray / oriented-bounds intersection in world space using object-local shape.
|
||||
// Uses a quick sphere test first; on success refines based on BoundsType.
|
||||
// For non-mesh shapes we use a quick world-space bounding-sphere pretest;
|
||||
// for mesh bounds we go directly to the mesh BVH (which already has a root AABB).
|
||||
// Returns true when hit; outWorldHit is the closest hit point in world space.
|
||||
bool intersect_ray_bounds(const glm::vec3 &rayOrigin,
|
||||
const glm::vec3 &rayDir,
|
||||
const Bounds &bounds,
|
||||
const glm::mat4 &worldTransform,
|
||||
glm::vec3 &outWorldHit)
|
||||
const RenderObject &obj,
|
||||
glm::vec3 &outWorldHit,
|
||||
BoundsHitDebug *debug)
|
||||
{
|
||||
const Bounds &bounds = obj.bounds;
|
||||
const glm::mat4 &worldTransform = obj.transform;
|
||||
|
||||
// Non-pickable object.
|
||||
if (bounds.type == BoundsType::None)
|
||||
{
|
||||
@@ -281,32 +293,63 @@ namespace
|
||||
return false;
|
||||
}
|
||||
|
||||
// Early reject using bounding sphere in world space.
|
||||
float sphereT = 0.0f;
|
||||
if (!intersect_ray_sphere(rayOrigin, rayDir, bounds, worldTransform, sphereT))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Shape-specific refinement after the conservative sphere test.
|
||||
switch (bounds.type)
|
||||
{
|
||||
case BoundsType::Sphere:
|
||||
{
|
||||
// Early reject using bounding sphere in world space.
|
||||
float sphereT = 0.0f;
|
||||
if (!intersect_ray_sphere(rayOrigin, rayDir, bounds, worldTransform, sphereT))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// We already have the hit distance along the ray from the sphere test.
|
||||
outWorldHit = rayOrigin + rayDir * sphereT;
|
||||
return true;
|
||||
}
|
||||
case BoundsType::Capsule:
|
||||
{
|
||||
float sphereT = 0.0f;
|
||||
if (!intersect_ray_sphere(rayOrigin, rayDir, bounds, worldTransform, sphereT))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return intersect_ray_capsule(rayOrigin, rayDir, bounds, worldTransform, outWorldHit);
|
||||
}
|
||||
case BoundsType::Mesh:
|
||||
{
|
||||
// Try high-precision mesh BVH first when available, then fall back to box.
|
||||
if (obj.sourceMesh && obj.sourceMesh->bvh)
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
debug->usedBVH = true;
|
||||
}
|
||||
MeshBVHPickHit meshHit{};
|
||||
if (intersect_ray_mesh_bvh(*obj.sourceMesh->bvh, worldTransform, rayOrigin, rayDir, meshHit))
|
||||
{
|
||||
if (debug)
|
||||
{
|
||||
debug->bvhHit = true;
|
||||
}
|
||||
outWorldHit = meshHit.worldPos;
|
||||
return true;
|
||||
}
|
||||
if (debug)
|
||||
{
|
||||
debug->fallbackBox = true;
|
||||
}
|
||||
}
|
||||
// return intersect_ray_box(rayOrigin, rayDir, bounds, worldTransform, outWorldHit);
|
||||
}
|
||||
case BoundsType::Box:
|
||||
case BoundsType::Mesh: // TODO: replace with BVH/mesh query; box is a safe fallback.
|
||||
default:
|
||||
{
|
||||
// For Capsule and Mesh we currently fall back to the oriented box;
|
||||
// this still benefits from tighter AABBs if you author them.
|
||||
float sphereT = 0.0f;
|
||||
if (!intersect_ray_sphere(rayOrigin, rayDir, bounds, worldTransform, sphereT))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return intersect_ray_box(rayOrigin, rayDir, bounds, worldTransform, outWorldHit);
|
||||
}
|
||||
}
|
||||
@@ -397,12 +440,16 @@ bool SceneManager::pick(const glm::vec2 &mousePosPixels, RenderObject &outObject
|
||||
float bestDist2 = std::numeric_limits<float>::max();
|
||||
glm::vec3 bestHitPos{};
|
||||
|
||||
// Reset debug info for this pick.
|
||||
pickingDebug = {};
|
||||
|
||||
auto testList = [&](const std::vector<RenderObject> &list)
|
||||
{
|
||||
for (const RenderObject &obj: list)
|
||||
{
|
||||
glm::vec3 hitPos{};
|
||||
if (!intersect_ray_bounds(rayOrigin, rayDir, obj.bounds, obj.transform, hitPos))
|
||||
BoundsHitDebug localDebug{};
|
||||
if (!intersect_ray_bounds(rayOrigin, rayDir, obj, hitPos, &localDebug))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -414,6 +461,23 @@ bool SceneManager::pick(const glm::vec2 &mousePosPixels, RenderObject &outObject
|
||||
bestHitPos = hitPos;
|
||||
outObject = obj;
|
||||
anyHit = true;
|
||||
|
||||
// Capture debug info for the best hit so far.
|
||||
pickingDebug.usedMeshBVH = localDebug.usedBVH;
|
||||
pickingDebug.meshBVHHit = localDebug.bvhHit;
|
||||
pickingDebug.meshBVHFallbackBox = localDebug.fallbackBox;
|
||||
if (obj.sourceMesh && obj.sourceMesh->bvh)
|
||||
{
|
||||
pickingDebug.meshBVHPrimCount =
|
||||
static_cast<uint32_t>(obj.sourceMesh->bvh->primitives.size());
|
||||
pickingDebug.meshBVHNodeCount =
|
||||
static_cast<uint32_t>(obj.sourceMesh->bvh->nodes.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
pickingDebug.meshBVHPrimCount = 0;
|
||||
pickingDebug.meshBVHNodeCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user