changeset 1257:e9b8d87b9883

fix: super stupid bug in perspective matrix X(
author sam <sam@basx.dev>
date Sun, 28 Jul 2024 18:55:46 +0700
parents bfb75c934f4e
children 5442d0e9d8ff
files semiconginev2/core/matrix.nim semiconginev2/rendering.nim semiconginev2/rendering/shaders.nim tests/test_gltf.nim
diffstat 4 files changed, 10 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/semiconginev2/core/matrix.nim	Sun Jul 28 17:33:41 2024 +0700
+++ b/semiconginev2/core/matrix.nim	Sun Jul 28 18:55:46 2024 +0700
@@ -444,12 +444,12 @@
 makeRandomMatrixInit(TMat4)
 
 func Perspective*(fovy, aspect, zNear, zFar: float32): Mat4 =
-  let tanHalfFovy = tan(fovy / 2)
+  let tanHalfFovy = 1 / tan(fovy / 2)
   return Mat4(data: [
-    1 / (aspect * tanHalfFovy), 0, 0, 0,
-    0, 1 / tanHalfFovy, 0, 0,
+    tanHalfFovy / aspect, 0, 0, 0,
+    0, tanHalfFovy, 0, 0,
     0, 0, zFar / (zFar - zNear), -(zFar * zNear) / (zFar - zNear),
-    0, 0, 1, 1,
+    0, 0, 1, 0,
   ])
 
 func Ortho*(left, right, top, bottom, zNear, zFar: float32): Mat4 =
--- a/semiconginev2/rendering.nim	Sun Jul 28 17:33:41 2024 +0700
+++ b/semiconginev2/rendering.nim	Sun Jul 28 18:55:46 2024 +0700
@@ -293,6 +293,7 @@
   defer: deallocCStringArray(deviceExtensionsC)
   let enabledFeatures = VkPhysicalDeviceFeatures(
    fillModeNonSolid: true,
+   depthClamp: true,
    wideLines: true,
    largePoints: true,
   )
--- a/semiconginev2/rendering/shaders.nim	Sun Jul 28 17:33:41 2024 +0700
+++ b/semiconginev2/rendering/shaders.nim	Sun Jul 28 18:55:46 2024 +0700
@@ -364,6 +364,7 @@
   cullMode: openArray[VkCullModeFlagBits] = [VK_CULL_MODE_BACK_BIT],
   frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE,
   lineWidth = 1'f32,
+  depthClampEnable = false,
 ): Pipeline[TShader] =
   # create pipeline
 
@@ -445,7 +446,7 @@
     )
     rasterizer = VkPipelineRasterizationStateCreateInfo(
       sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
-      depthClampEnable: VK_FALSE,
+      depthClampEnable: depthClampEnable,
       rasterizerDiscardEnable: VK_FALSE,
       polygonMode: polygonMode,
       lineWidth: lineWidth,
--- a/tests/test_gltf.nim	Sun Jul 28 17:33:41 2024 +0700
+++ b/tests/test_gltf.nim	Sun Jul 28 18:55:46 2024 +0700
@@ -138,8 +138,10 @@
     camPos += camDir * forward * dt
     camPos += camDirSide * sideward * dt
 
+    let fovH = PI / 2
+    let fovV = 2 * arctan(tan(fovH / 2) * 1 / GetAspectRatio())
     descriptors.data.camera.data.viewPerspective = (
-      Perspective(PI/3, aspect = GetAspectRatio(), zNear = 0.1, zFar = 1) *
+      Perspective(fovV, aspect = GetAspectRatio(), zNear = 0.01, zFar = 20) *
       Rotate(-camPitch, X) * Rotate(-camYaw, Y) * Translate(-camPos)
     )