浏览代码

Some fixes

Panagiotis Christopoulos Charitos 4 年之前
父节点
当前提交
991cc6aa59
共有 2 个文件被更改,包括 22 次插入8 次删除
  1. 1 1
      AnKi/Scene/Components/FrustumComponent.cpp
  2. 21 7
      AnKi/Shaders/CollisionFunctions.glsl

+ 1 - 1
AnKi/Scene/Components/FrustumComponent.cpp

@@ -48,7 +48,7 @@ Bool FrustumComponent::updateInternal()
 			m_projMat = Mat4::calculatePerspectiveProjectionMatrix(m_perspective.m_fovX, m_perspective.m_fovY,
 																   m_perspective.m_near, m_perspective.m_far);
 
-			computeEdgesOfFrustum(m_perspective.m_far, m_perspective.m_fovY, m_perspective.m_fovY,
+			computeEdgesOfFrustum(m_perspective.m_far, m_perspective.m_fovX, m_perspective.m_fovY,
 								  &m_perspective.m_edgesL[0]);
 
 			// Planes

+ 21 - 7
AnKi/Shaders/CollisionFunctions.glsl

@@ -6,14 +6,15 @@
 #pragma once
 
 /// https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/moller-trumbore-ray-triangle-intersection
-Bool testRayTriangle(Vec3 orig, Vec3 dir, Vec3 v0, Vec3 v1, Vec3 v2, out F32 t, out F32 u, out F32 v)
+Bool testRayTriangle(Vec3 orig, Vec3 dir, Vec3 v0, Vec3 v1, Vec3 v2, Bool backfaceCulling, out F32 t, out F32 u,
+					 out F32 v)
 {
 	const Vec3 v0v1 = v1 - v0;
 	const Vec3 v0v2 = v2 - v0;
 	const Vec3 pvec = cross(dir, v0v2);
 	const F32 det = dot(v0v1, pvec);
 
-	if(det < EPSILON)
+	if((backfaceCulling && det <= 0.0) || det == 0.0)
 	{
 		return false;
 	}
@@ -68,13 +69,26 @@ Bool testRaySphere(Vec3 rayOrigin, Vec3 rayDir, Vec3 sphereCenter, F32 sphereRad
 	{
 		return false;
 	}
-	else
+
+	const F32 thc = sqrt(diff);
+	t0 = tca - thc;
+	t1 = tca + thc;
+
+	if(t0 < 0.0 && t1 < 0.0)
+	{
+		return false;
+	}
+
+	// Swap
+	if(t0 > t1)
 	{
-		const F32 thc = sqrt(diff);
-		t0 = tca - thc;
-		t1 = tca + thc;
-		return true;
+		const F32 tmp = t0;
+		t0 = t1;
+		t1 = tmp;
 	}
+
+	t0 = max(0.0, t0);
+	return true;
 }
 
 F32 testPlanePoint(Vec3 planeNormal, F32 planeOffset, Vec3 point)