Browse Source

Lesson 24.5

Godzil 3 years ago
parent
commit
9fc047fdc9
5 changed files with 101 additions and 3 deletions
  1. 37 0
      source/clipping.c
  2. 42 0
      source/include/clipping.h
  3. 1 1
      source/include/matrix.h
  4. 11 0
      source/include/vector.h
  5. 10 2
      source/main.c

+ 37 - 0
source/clipping.c

@@ -0,0 +1,37 @@
+/*
+ * 3D Engine 
+ * clipping.c: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 11/03/2021.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+#include <clipping.h>
+
+static plane_t frustum[TOTAL_PLANES];
+
+void initFrustumPlanes(double fov, double aspectRation, double zNear, double zFar)
+{
+    frustum[LEFT_PLANE].point = vec3(0, 0, 0);
+    frustum[LEFT_PLANE].normal = vec3( cos(fov) / 2, 0, sin(fov) / 2);
+
+    frustum[RIGHT_PLANE].point = vec3(0, 0, 0);
+    frustum[RIGHT_PLANE].normal = vec3( -cos(fov) / 2, 0, sin(fov) / 2);
+
+    frustum[TOP_PLANE].point = vec3(0, 0, 0);
+    frustum[TOP_PLANE].normal = vec3(0, -cos(fov) / 2, sin(fov) / 2);
+
+    frustum[BOTTOM_PLANE].point = vec3(0, 0, 0);
+    frustum[BOTTOM_PLANE].normal = vec3(0, cos(fov) / 2, sin(fov) / 2);
+
+    frustum[NEAR_PLANE].point = vec3(0, 0, zNear);
+    frustum[NEAR_PLANE].normal = vec3(0, 0, 1);
+
+    frustum[FAR_PLANE].point = vec3(0, 0, zFar);
+    frustum[FAR_PLANE].normal = vec3(0, 0, -1);
+}

+ 42 - 0
source/include/clipping.h

@@ -0,0 +1,42 @@
+    /*
+ * 3D Engine 
+ * clipping.h: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 11/03/2021.
+ */
+
+#ifndef THREEDENGINE_SOURCE_INCLUDE_CLIPPING_H
+#define THREEDENGINE_SOURCE_INCLUDE_CLIPPING_H
+
+#include <vector.h>
+
+/***********************************************************************************************************************
+ * Data types
+ **********************************************************************************************************************/
+enum
+{
+    TOP_PLANE = 0,
+    RIGHT_PLANE,
+    BOTTOM_PLANE,
+    LEFT_PLANE,
+    NEAR_PLANE,
+    FAR_PLANE,
+
+    /* Keep at the end */
+    TOTAL_PLANES
+};
+
+typedef struct plane_t
+{
+  vec3_t point;
+  vec3_t normal;
+} plane_t;
+
+/***********************************************************************************************************************
+ * Prototypes
+ **********************************************************************************************************************/
+void initFrustumPlanes(double fov, double aspectRation, double zNear, double zFar);
+
+#endif /* THREEDENGINE_SOURCE_INCLUDE_CLIPPING_H */

+ 1 - 1
source/include/matrix.h

@@ -51,7 +51,7 @@ matrix4_t mat4RotationX(double angle);
 matrix4_t mat4RotationY(double angle);
 matrix4_t mat4RotationZ(double angle);
 
-matrix4_t mat4Perspective(double aspectRation, double FOV, double zNear, double zFar);
+matrix4_t mat4Perspective(double aspectRatio, double FOV, double zNear, double zFar);
 
 matrix4_t mat4LookAt(vec3_t eye, vec3_t target, vec3_t up);
 

+ 11 - 0
source/include/vector.h

@@ -33,6 +33,12 @@ typedef struct vec4_t
  * Prototypes
  **********************************************************************************************************************/
 /* ---------------------------------------------- 2D Vectors operations --------------------------------------------- */
+static inline vec2_t vec2(double x, double y)
+{
+    vec2_t ret = { x, y };
+    return ret;
+}
+
 double vec2Getlength(vec2_t v);
 vec2_t vec2AddVectors(vec2_t a, vec2_t b);
 vec2_t vec2SubVectors(vec2_t a, vec2_t b);
@@ -44,6 +50,11 @@ void vec2Normalize(vec2_t *a);
 vec2_t vec2FromVec4(vec4_t v);
 
 /* ---------------------------------------------- 3D Vectors operations --------------------------------------------- */
+static inline vec3_t vec3(double x, double y, double z)
+{
+    vec3_t ret = { x, y, z };
+    return ret;
+}
 double vec3Getlength(vec3_t v);
 vec3_t vec3AddVectors(vec3_t a, vec3_t b);
 vec3_t vec3SubVectors(vec3_t a, vec3_t b);

+ 10 - 2
source/main.c

@@ -24,6 +24,7 @@
 #include <mesh.h>
 #include <light.h>
 #include <camera.h>
+#include <clipping.h>
 
 bool isRunning = true;
 
@@ -49,10 +50,15 @@ light_t globalLight;
 double updateTime, renderTime, waitTime;
 int trianglesCount, trianglesTotal;
 triangle_t *projectedTriangles = NULL;
-const vec3_t origin = {0, 0, 0 };
+static const vec3_t origin = {0, 0, 0 };
 
 void setup()
 {
+    double FOV = 90;
+    double aspectRatio = (double)windowHeight / windowWidth;
+    double zNear = 0.1;
+    double zFar = 100;
+
     frameBuffer = (uint32_t *)calloc(windowWidth * windowHeight, sizeof(uint32_t));
     if (!frameBuffer)
     {
@@ -75,7 +81,9 @@ void setup()
         goto exit;
     }
 
-    projectionMatrix = mat4Perspective((double)windowHeight / windowWidth, 90, 0.1, 100);
+    projectionMatrix = mat4Perspective(aspectRatio, FOV, zNear, zFar);
+
+    initFrustumPlanes(FOV, aspectRatio, zNear, zFar);
 
     globalLight.direction.x = 0;
     globalLight.direction.y = 0;