Browse Source

Lesson 17.2

Godzil 3 years ago
parent
commit
2809cc2331
5 changed files with 57 additions and 36 deletions
  1. 3 0
      source/include/matrix.h
  2. 2 0
      source/include/vector.h
  3. 12 35
      source/main.c
  4. 34 1
      source/matrix.c
  5. 6 0
      source/vector.c

+ 3 - 0
source/include/matrix.h

@@ -42,6 +42,7 @@ static const struct matrix4_t mat4Identity =
 
 vec4_t mat4ProdVec4(matrix4_t mat, vec4_t v);
 matrix4_t mat4ProdMat4(matrix4_t a, matrix4_t b);
+vec4_t mat4ProdVec4Project(matrix4_t mat, vec4_t v);
 
 matrix4_t mat4Scale(double scaleX, double scaleY, double scaleZ);
 matrix4_t mat4Translate(double tX, double tY, double tZ);
@@ -50,4 +51,6 @@ 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);
+
 #endif /* THREEDENGINE_SOURCE_INCLUDE_MATRICE_H */

+ 2 - 0
source/include/vector.h

@@ -41,6 +41,8 @@ vec2_t vec2ScalarDiv(vec2_t a, double s);
 double vec2Dot(vec2_t a, vec2_t b);
 void vec2Normalize(vec2_t *a);
 
+vec2_t vec2FromVec4(vec4_t v);
+
 /* ---------------------------------------------- 3D Vectors operations --------------------------------------------- */
 double vec3Getlength(vec3_t v);
 vec3_t vec3AddVectors(vec3_t a, vec3_t b);

+ 12 - 35
source/main.c

@@ -34,6 +34,7 @@ typedef enum displayMode_t
 bool displayVertex = false;
 bool doNotCull = false;
 enum displayMode_t currentDisplayMode = DISPLAY_MODE_SOLID;
+matrix4_t projectionMatrix;
 
 vec3_t cameraPosition =
 {
@@ -44,8 +45,6 @@ vec3_t cameraPosition =
 
 triangle_t *projectedTriangles = NULL;
 
-double fovFactor = 640;
-
 void setup()
 {
     frameBuffer = (uint32_t *)calloc(windowWidth * windowHeight, sizeof(uint32_t));
@@ -63,6 +62,8 @@ void setup()
         goto exit;
     }
 
+    projectionMatrix = mat4Perspective((double)windowHeight / windowWidth, 90, 0.1, 100);
+
     //loadOBJFile("assets/cube.obj");
     loadCubeMeshData();
 
@@ -123,28 +124,6 @@ void processInput()
 
 }
 
-vec2_t orthographicPointProjection(vec3_t point)
-{
-    vec2_t ret;
-
-    ret.x = point.x * fovFactor;
-    ret.y = point.y * fovFactor;
-
-    return ret;
-}
-
-vec2_t perspectivePointProjection(vec3_t point)
-{
-    vec2_t ret;
-    double divisor = point.z;
-    if (divisor == 0) { divisor += 0.00001; }
-
-    ret.x = (point.x / divisor) * (fovFactor/2.);
-    ret.y = (point.y / divisor) * (fovFactor/2.);
-
-    return ret;
-}
-
 void update()
 {
     uint32_t i, j;
@@ -158,13 +137,6 @@ void update()
     previousFrameTime = SDL_GetTicks();
 
     mesh.rotation.x += 0.01;
-    mesh.rotation.y += 0.01;
-    mesh.rotation.z += 0.02;
-
-    //mesh.scale.x += 0.002;
-    //mesh.scale.x += 0.001;
-
-    mesh.translation.x += 0.01;
     mesh.translation.z = 5;
 
     worldMatrix = mat4Identity;
@@ -220,12 +192,17 @@ void update()
 
         for (j = 0 ; j < 3 ; j++)
         {
-            vec2_t projected = perspectivePointProjection(vec3FromVec4(transformedVertices[j]));
+            vec4_t projected = mat4ProdVec4Project(projectionMatrix, transformedVertices[j]);
+
+
+            projected.x *= windowWidth / 2.;
+            projected.y *= windowHeight / 2.;
 
-            projected.x += (windowWidth / 2);
-            projected.y += (windowHeight / 2);
+            projected.x += (windowWidth / 2.);
+            projected.y += (windowHeight / 2.);
 
-            currentTriangle.points[j] = projected;
+            currentTriangle.points[j].x = projected.x;
+            currentTriangle.points[j].y = projected.y;
         }
 
         currentTriangle.colour = mesh.faces[i].colour;

+ 34 - 1
source/matrix.c

@@ -36,6 +36,22 @@ vec4_t mat4ProdVec4(matrix4_t mat, vec4_t v)
     return ret;
 }
 
+vec4_t mat4ProdVec4Project(matrix4_t mat, vec4_t v)
+{
+    vec4_t result = mat4ProdVec4(mat, v);
+
+    /* Normalise the vector if needed */
+    if (result.w != 0.0)
+    {
+        result.x /= result.w;
+        result.y /= result.w;
+        result.z /= result.w;
+        result.w = 0;
+    }
+
+    return result;
+}
+
 /* Matrix operations */
 matrix4_t mat4ProdMat4(matrix4_t a, matrix4_t b)
 {
@@ -129,4 +145,21 @@ matrix4_t mat4RotationZ(double angle)
         }
     };
     return ret;
-}
+}
+
+matrix4_t mat4Perspective(double aspectRatio, double FOV, double zNear, double zFar)
+{
+    double radFOV = (FOV * M_PI) / 180.;
+    double f = 1. / tan(radFOV / 2.);
+    matrix4_t ret =
+    {
+        .v =
+            {
+                aspectRatio * f, 0, 0, 0,
+                0, f, 0, 0,
+                0, 0, zFar / (zFar - zNear), -(zFar * zNear) / (zFar - zNear),
+                0, 0, 1, 0
+            }
+    };
+    return ret;
+}

+ 6 - 0
source/vector.c

@@ -51,6 +51,12 @@ void vec2Normalize(vec2_t *a)
     *a = vec2ScalarDiv(*a, length);
 }
 
+vec2_t vec2FromVec4(vec4_t v)
+{
+    vec2_t ret = { v.x, v.y };
+    return ret;
+}
+
 /* ---------------------------------------------- 3D Vectors operations --------------------------------------------- */
 double vec3Getlength(vec3_t v)
 {