Browse Source

Lesson 24.9

Godzil 3 years ago
parent
commit
aa12428a8e
5 changed files with 70 additions and 14 deletions
  1. 16 0
      source/clipping.c
  2. 33 3
      source/include/clipping.h
  3. 6 1
      source/include/vector.h
  4. 15 4
      source/main.c
  5. 0 6
      source/vector.c

+ 16 - 0
source/clipping.c

@@ -34,4 +34,20 @@ void initFrustumPlanes(double fov, double aspectRation, double zNear, double zFa
 
     frustum[FAR_PLANE].point = vec3(0, 0, zFar);
     frustum[FAR_PLANE].normal = vec3(0, 0, -1);
+}
+
+
+static void clipPolygonAgainstPlane(polygon_t *poly, faceenum_t plane)
+{
+
+}
+
+void clipPolygon(polygon_t *poly)
+{
+    clipPolygonAgainstPlane(poly, LEFT_PLANE);
+    clipPolygonAgainstPlane(poly, TOP_PLANE);
+    clipPolygonAgainstPlane(poly, RIGHT_PLANE);
+    clipPolygonAgainstPlane(poly, BOTTOM_PLANE);
+    clipPolygonAgainstPlane(poly, NEAR_PLANE);
+    clipPolygonAgainstPlane(poly, FAR_PLANE);
 }

+ 33 - 3
source/include/clipping.h

@@ -1,4 +1,4 @@
-    /*
+/*
  * 3D Engine 
  * clipping.h: 
  * Based on pikuma.com 3D software renderer in C
@@ -11,11 +11,17 @@
 #define THREEDENGINE_SOURCE_INCLUDE_CLIPPING_H
 
 #include <vector.h>
+#include <triangle.h>
+
+/***********************************************************************************************************************
+ * Constants
+ **********************************************************************************************************************/
+#define MAX_NUM_POLYGON_VERTICES (10)
 
 /***********************************************************************************************************************
  * Data types
  **********************************************************************************************************************/
-enum
+typedef enum faceenum_t
 {
     TOP_PLANE = 0,
     RIGHT_PLANE,
@@ -26,7 +32,7 @@ enum
 
     /* Keep at the end */
     TOTAL_PLANES
-};
+} faceenum_t;
 
 typedef struct plane_t
 {
@@ -34,9 +40,33 @@ typedef struct plane_t
   vec3_t normal;
 } plane_t;
 
+typedef struct polygon_t
+{
+    vec3_t vertices[MAX_NUM_POLYGON_VERTICES];
+    int num_verticices;
+} polygon_t;
+
 /***********************************************************************************************************************
  * Prototypes
  **********************************************************************************************************************/
 void initFrustumPlanes(double fov, double aspectRation, double zNear, double zFar);
 
+/* ---- Public polygon stuff ---- */
+void clipPolygon(polygon_t *poly);
+
+static inline polygon_t createPolygonFromTriangle(vec3_t p1, vec3_t p2, vec3_t p3)
+{
+    polygon_t ret =
+    {
+        .vertices =
+        {
+            [0] = p1,
+            [1] = p2,
+            [2] = p3,
+        },
+        .num_verticices = 3
+    };
+    return ret;
+}
+
 #endif /* THREEDENGINE_SOURCE_INCLUDE_CLIPPING_H */

+ 6 - 1
source/include/vector.h

@@ -64,7 +64,12 @@ vec3_t vec3Cross(vec3_t a, vec3_t b);
 double vec3Dot(vec3_t a, vec3_t b);
 void vec3Normalize(vec3_t *a);
 
-vec3_t vec3FromVec4(vec4_t v);
+static inline vec3_t vec3FromVec4(vec4_t v)
+{
+    vec3_t ret = { v.x, v.y, v.z };
+    return ret;
+}
+
 
 vec3_t vec3RotateX(vec3_t original, double angle);
 vec3_t vec3RotateY(vec3_t original, double angle);

+ 15 - 4
source/main.c

@@ -43,7 +43,7 @@ typedef enum displayMode_t
 bool displayVertex = false;
 bool doNotCull = false;
 bool doZBuffer = true;
-enum displayMode_t currentDisplayMode = DISPLAY_MODE_SOLID;
+enum displayMode_t currentDisplayMode = DISPLAY_MODE_WIREFRAME;
 matrix4_t projectionMatrix;
 matrix4_t viewMatrix;
 light_t globalLight;
@@ -92,11 +92,11 @@ void setup()
 
     /* Load texture */
     //meshTexture = (colour_t *)REDBRICK_TEXTURE;
-    loadTextureDateFromPng("assets/f22.png");
+    loadTextureDateFromPng("assets/cube.png");
 
 
     /* Load object */
-    loadOBJFile("assets/f22.obj");
+    loadOBJFile("assets/cube.obj");
     //loadCubeMeshData();
 
     trianglesTotal = arrayGetSize(mesh.faces);
@@ -259,6 +259,10 @@ void update()
     faceCount = arrayGetSize(mesh.faces);
     for(i = 0; i < faceCount; i++)
     {
+        polygon_t polygon;
+        /* TODO: REMOVE ASAP */
+        if (i != 4) continue;
+
         triangle_t currentTriangle =
         {
             .textureCoordinates =
@@ -301,13 +305,20 @@ void update()
 
             double cameraDotTriangle = vec3Dot(triangleNormal, cameraRay);
 
-
             if (cameraDotTriangle < 0)
             {
                 continue;
             }
         }
 
+        polygon = createPolygonFromTriangle(vec3FromVec4(transformedVertices[0]),
+                                            vec3FromVec4(transformedVertices[1]),
+                                            vec3FromVec4(transformedVertices[2]));
+
+        clipPolygon(&polygon);
+
+        // triangles = polyFan(&polygon)
+
         for (j = 0 ; j < 3 ; j++)
         {
             vec4_t projected = mat4ProdVec4Project(projectionMatrix, transformedVertices[j]);

+ 0 - 6
source/vector.c

@@ -109,12 +109,6 @@ void vec3Normalize(vec3_t *a)
     *a = vec3ScalarDiv(*a, length);
 }
 
-vec3_t vec3FromVec4(vec4_t v)
-{
-    vec3_t ret = { v.x, v.y, v.z };
-    return ret;
-}
-
 vec3_t vec3RotateX(vec3_t original, double angle)
 {
     vec3_t ret = {