ADD: BVH Selection triangle

This commit is contained in:
2025-11-19 23:33:05 +09:00
parent ac35de6104
commit 0fe7c0f975
10 changed files with 116 additions and 84 deletions

View File

@@ -20,7 +20,7 @@ public:
bool rmbDown { false };
// Field of view in degrees for projection
float fovDegrees { 70.f };
float fovDegrees { 50.f };
glm::mat4 getViewMatrix();
glm::mat4 getRotationMatrix();

View File

@@ -58,10 +58,14 @@ std::unique_ptr<MeshBVH> build_mesh_bvh(const MeshAsset &mesh,
const glm::vec3 &p1 = vertices[i1].position;
const glm::vec3 &p2 = vertices[i2].position;
// BVH2 now expects triangle primitives with explicit vertices.
// Store the triangle in mesh-local space and let the library
// compute/update the AABB used for hierarchy construction.
PrimitiveF prim{};
prim.bounds.expand(Vec3<float>(p0.x, p0.y, p0.z));
prim.bounds.expand(Vec3<float>(p1.x, p1.y, p1.z));
prim.bounds.expand(Vec3<float>(p2.x, p2.y, p2.z));
prim.v0 = Vec3<float>(p0.x, p0.y, p0.z);
prim.v1 = Vec3<float>(p1.x, p1.y, p1.z);
prim.v2 = Vec3<float>(p2.x, p2.y, p2.z);
prim.updateBounds();
result->primitives.push_back(prim);
MeshBVHPrimitiveRef ref{};
@@ -144,4 +148,3 @@ bool intersect_ray_mesh_bvh(const MeshBVH &bvh,
return true;
}

View File

@@ -21,7 +21,7 @@ enum class BoundsType : uint8_t
Box, // oriented box using origin/extents (default)
Sphere, // sphere using origin + sphereRadius
Capsule, // capsule aligned with local Y (derived from extents)
Mesh // full mesh (BVH / ray query); currently falls back to Box
Mesh // full mesh (BVH / ray query using mesh BVH when available)
};
struct Bounds

View File

@@ -149,7 +149,9 @@ void SceneManager::update_scene()
return m;
};
const float fov = glm::radians(70.f);
// Keep projection FOV in sync with the camera so that CPU ray picking
// matches what is rendered on-screen.
const float fov = glm::radians(mainCamera.fovDegrees);
const float aspect = (float) _context->getSwapchain()->windowExtent().width /
(float) _context->getSwapchain()->windowExtent().height;
const float nearPlane = 0.1f;

View File

@@ -89,15 +89,21 @@ namespace
return false;
}
float tHit = (tMin >= 0.0f) ? tMin : tMax;
glm::vec3 localHit = localOrigin + tHit * localDir;
glm::vec3 worldHit = glm::vec3(worldTransform * glm::vec4(localHit, 1.0f));
if (glm::dot(worldHit - rayOrigin, rayDir) <= 0.0f)
// Choose the closest intersection in front of the ray origin.
// If the ray starts inside the box (tMin <= 0), use the exit point tMax.
float tHit = tMin;
if (tHit <= 0.0f)
{
tHit = tMax;
}
if (tHit <= 0.0f)
{
return false;
}
glm::vec3 localHit = localOrigin + tHit * localDir;
glm::vec3 worldHit = glm::vec3(worldTransform * glm::vec4(localHit, 1.0f));
outWorldHit = worldHit;
return true;
}