Browse Source

Lesson 24.14

Godzil 3 years ago
parent
commit
c0916918b7
3 changed files with 13 additions and 10 deletions
  1. 8 6
      source/clipping.c
  2. 1 1
      source/include/clipping.h
  3. 4 3
      source/main.c

+ 8 - 6
source/clipping.c

@@ -16,20 +16,22 @@
 
 static plane_t frustum[TOTAL_PLANES];
 
-void  initFrustumPlanes(double fov, double aspectRation, double zNear, double zFar)
+void initFrustumPlanes(double fov, double aspectRatio, double zNear, double zFar)
 {
-    double radFOV = (fov * M_PI) / 180.;
+    double radFOV_Y = (fov * M_PI) / 180.;
+    double radFOV_X = atan(tan(radFOV_Y/2)* aspectRatio) * 2;
+
     frustum[LEFT_PLANE].point = vec3(0, 0, 0);
-    frustum[LEFT_PLANE].normal = vec3( cos(radFOV / 2.0) , 0, sin(radFOV / 2.0));
+    frustum[LEFT_PLANE].normal = vec3( cos(radFOV_X / 2.0) , 0, sin(radFOV_X / 2.0));
 
     frustum[RIGHT_PLANE].point = vec3(0, 0, 0);
-    frustum[RIGHT_PLANE].normal = vec3( -cos(radFOV / 2.0), 0, sin(radFOV / 2.0));
+    frustum[RIGHT_PLANE].normal = vec3( -cos(radFOV_X / 2.0), 0, sin(radFOV_X / 2.0));
 
     frustum[TOP_PLANE].point = vec3(0, 0, 0);
-    frustum[TOP_PLANE].normal = vec3(0, -cos(radFOV / 2.0), sin(radFOV / 2.0));
+    frustum[TOP_PLANE].normal = vec3(0, -cos(radFOV_Y / 2.0), sin(radFOV_Y / 2.0));
 
     frustum[BOTTOM_PLANE].point = vec3(0, 0, 0);
-    frustum[BOTTOM_PLANE].normal = vec3(0, cos(radFOV / 2.0), sin(radFOV / 2.0));
+    frustum[BOTTOM_PLANE].normal = vec3(0, cos(radFOV_Y / 2.0), sin(radFOV_Y / 2.0));
 
     frustum[NEAR_PLANE].point = vec3(0, 0, zNear);
     frustum[NEAR_PLANE].normal = vec3(0, 0, 1);

+ 1 - 1
source/include/clipping.h

@@ -50,7 +50,7 @@ typedef struct polygon_t
 /***********************************************************************************************************************
  * Prototypes
  **********************************************************************************************************************/
-void initFrustumPlanes(double fov, double aspectRation, double zNear, double zFar);
+void initFrustumPlanes(double fovY, double aspectRatio, double zNear, double zFar);
 
 /* ---- Public polygon stuff ---- */
 void clipPolygon(polygon_t *poly);

+ 4 - 3
source/main.c

@@ -56,7 +56,8 @@ static const vec3_t origin = {0, 0, 0 };
 void setup()
 {
     double FOV = 60;
-    double aspectRatio = (double)windowHeight / windowWidth;
+    double aspectRatioY = (double)windowHeight / windowWidth;
+    double aspectRatioX = (double)windowWidth / windowHeight;
     double zNear = 1;
     double zFar = 20;
 
@@ -82,9 +83,9 @@ void setup()
         goto exit;
     }
 
-    projectionMatrix = mat4Perspective(aspectRatio, FOV, zNear, zFar);
+    projectionMatrix = mat4Perspective(aspectRatioY, FOV, zNear, zFar);
 
-    initFrustumPlanes(FOV, aspectRatio, zNear, zFar);
+    initFrustumPlanes(FOV, aspectRatioX, zNear, zFar);
 
     globalLight.direction.x = 0;
     globalLight.direction.y = 0;