changeset 1289:6e8f02e58606

add: ray aabb-box intersection algorithm
author sam <sam@basx.dev>
date Thu, 01 Aug 2024 05:42:25 +0700
parents 8abddbe609ce
children ad3c6cad7d19
files semicongine/contrib/algorithms/collision.nim semicongine/core/vector.nim semicongine/rendering/renderer.nim semicongine/rendering/shaders.nim
diffstat 4 files changed, 30 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/contrib/algorithms/collision.nim	Tue Jul 30 17:23:41 2024 +0700
+++ b/semicongine/contrib/algorithms/collision.nim	Thu Aug 01 05:42:25 2024 +0700
@@ -382,3 +382,16 @@
       result.transform = translate(center)
       for p in points:
         result.radius = max(result.radius, (p - center).length)
+
+
+
+proc rayIntersectAABB*(origin, dir, boxA, boxB: Vec3f): Option[(Vec3f, float32)] =
+  let
+    tA = (boxA - origin) / dir
+    tB = (boxB - origin) / dir
+    t1 = vec3(min(tA.x, tB.x), min(tA.y, tB.y), min(tA.z, tB.z))
+    t2 = vec3(max(tA.x, tB.x), max(tA.y, tB.y), max(tA.z, tB.z))
+    tNear = max(max(t1.x, t1.y), t1.z)
+    tFar = min(min(t2.x, t2.y), t2.z)
+  if tNear <= tFar:
+    result = some((origin + dir * tNear, tNear))
--- a/semicongine/core/vector.nim	Tue Jul 30 17:23:41 2024 +0700
+++ b/semicongine/core/vector.nim	Thu Aug 01 05:42:25 2024 +0700
@@ -32,7 +32,7 @@
 func ToVec2*[T: SomeNumber](orig: TVec1[T], value: T = default(T)): TVec2[T] =
   TVec2[T]([orig[0], value])
 
-# shortcuts
+# shortcuts Vec3f
 func vec1*[T: SomeNumber](x: T): Vec1f =
   Vec1f([float32(x)])
 func vec2*[T, S: SomeNumber](x: T, y: S): Vec2f =
@@ -51,6 +51,21 @@
 func vec4*[T: SomeNumber](x: T): Vec4f = vec4(x, 0, 0, 0)
 func vec4*(): Vec4f = vec4(0, 0, 0, 0)
 
+# shortcuts Vec3i
+func vec1i*[T: SomeInteger](x: T): Vec1i = Vec1i([int32(x)])
+func vec2i*[T, S: SomeInteger](x: T, y: S): Vec2i = Vec2i([int32(x), int32(y)])
+func vec2i*[T: SomeInteger](x: T): Vec2i = vec2i(x, 0)
+func vec2i*(): Vec2i = vec2i(0, 0)
+func vec3i*[T, S, U: SomeInteger](x: T, y: S, z: U): Vec3i = Vec3i([int32(x), int32(y), int32(z)])
+func vec3i*[T, S: SomeInteger](x: T, y: S): Vec3i = vec3i(x, y, 0)
+func vec3i*[T: SomeInteger](x: T): Vec3i = vec3i(x, 0, 0)
+func vec3i*(): Vec3i = vec3i(0, 0, 0)
+func vec4i*[T, S, U, V: SomeInteger](x: T, y: S, z: U, w: V): Vec4i = Vec4i([int32(x), int32(y), int32(z), int32(w)])
+func vec4i*[T, S, U: SomeInteger](x: T, y: S, z: U): Vec4i = vec4i(x, y, z, 0)
+func vec4i*[T, S: SomeInteger](x: T, y: S): Vec4i = vec4i(x, y, 0, 0)
+func vec4i*[T: SomeInteger](x: T): Vec4i = vec4i(x, 0, 0, 0)
+func vec4i*(): Vec4i = vec4i(0, 0, 0, 0)
+
 const
   X* = vec3(1, 0, 0)
   Y* = vec3(0, 1, 0)
--- a/semicongine/rendering/renderer.nim	Tue Jul 30 17:23:41 2024 +0700
+++ b/semicongine/rendering/renderer.nim	Thu Aug 01 05:42:25 2024 +0700
@@ -691,8 +691,6 @@
       indexType = VK_INDEX_TYPE_UINT32
       elementCount = meshValue.data.len.uint32
 
-  assert elementCount > 0
-
   if indexType != VK_INDEX_TYPE_NONE_KHR:
     vkCmdBindIndexBuffer(
       commandBuffer,
--- a/semicongine/rendering/shaders.nim	Tue Jul 30 17:23:41 2024 +0700
+++ b/semicongine/rendering/shaders.nim	Thu Aug 01 05:42:25 2024 +0700
@@ -134,7 +134,7 @@
   var pushConstants: seq[string]
   var samplers: seq[string]
   var vsInputLocation = 0'u32
-  var passLocation = 0
+  var passLocation {.hint[XDeclaredButNotUsed]: off.} = 0
   var fsOutputLocation = 0
 
   var sawDescriptorSets = false