Godzil 3 年 前
コミット
39be588e3e
3 ファイル変更50 行追加0 行削除
  1. 4 0
      source/include/vector.h
  2. 15 0
      source/main.c
  3. 31 0
      source/vector.c

+ 4 - 0
source/include/vector.h

@@ -33,4 +33,8 @@ typedef struct vec4_t
  * Prototypes
  **********************************************************************************************************************/
 
+vec3_t vec3RotateX(vec3_t original, double angle);
+vec3_t vec3RotateY(vec3_t original, double angle);
+vec3_t vec3RotateZ(vec3_t original, double angle);
+
 #endif /* THREEDENGINE_SOURCE_VECTOR_H */

+ 15 - 0
source/main.c

@@ -29,6 +29,12 @@ vec3_t cameraPosition =
     .y = 0,
     .z = -5,
 };
+vec3_t cubeRotation =
+{
+    .x = 0,
+    .y = 0,
+    .z = 0,
+};
 
 double fovFactor = 640 ;
 
@@ -113,10 +119,19 @@ vec2_t perspectivePointProjection(vec3_t point)
 void update()
 {
     int i;
+
+    cubeRotation.x += 0.001;
+    cubeRotation.y += 0.001;
+    cubeRotation.z += 0.001;
+
     for(i = 0; i < N_POINTS; i++)
     {
         vec3_t point = cubePoints[i];
 
+        point = vec3RotateX(point, cubeRotation.x);
+        point = vec3RotateY(point, cubeRotation.y);
+        point = vec3RotateZ(point, cubeRotation.z);
+
         point.z -= cameraPosition.z;
 
         projectedPoints[i] = perspectivePointProjection(point);

+ 31 - 0
source/vector.c

@@ -8,3 +8,34 @@
  */
 
 #include <vector.h>
+#include <math.h>
+
+vec3_t vec3RotateX(vec3_t original, double angle)
+{
+    vec3_t ret = {
+        .x = original.x,
+        .y = original.y * cos(angle) - original.z * sin(angle),
+        .z = original.y * sin(angle) + original.z * cos(angle)
+    };
+    return ret;
+}
+
+vec3_t vec3RotateY(vec3_t original, double angle)
+{
+    vec3_t ret = {
+        .x = original.x * cos(angle) - original.z * sin(angle),
+        .y = original.y,
+        .z = original.x * sin(angle) + original.z * cos(angle)
+    };
+    return ret;
+}
+
+vec3_t vec3RotateZ(vec3_t original, double angle)
+{
+    vec3_t ret = {
+        .x = original.x * cos(angle) - original.y * sin(angle),
+        .y = original.x * sin(angle) + original.y * cos(angle),
+        .z = original.z,
+    };
+    return ret;
+}