Browse Source

View and tests

Kajetan Johannes Hammerle 1 month ago
parent
commit
e5496df6e9
5 changed files with 119 additions and 4 deletions
  1. 3 3
      CMakeLists.txt
  2. 21 0
      include/core/View.h
  3. 48 0
      src/View.c
  4. 1 1
      test/Main.c
  5. 46 0
      test/modules/ViewTests.c

+ 3 - 3
CMakeLists.txt

@@ -13,10 +13,10 @@ set(SRC
     "src/SpinLock.c"
     "src/Utility.c"
     "src/Vector.c"
+    "src/View.c"
     #"src/BitArray.cpp"
     #"src/Box.cpp"
     #"src/Frustum.cpp"
-    #"src/View.cpp"
 )
 
 set(SRC_TESTS
@@ -30,6 +30,7 @@ set(SRC_TESTS
     "test/modules/SpinLockTests.c"
     "test/modules/UtilityTests.c"
     "test/modules/VectorTests.c"
+    "test/modules/ViewTests.c"
     #"test/modules/BitArrayTests.cpp"
     #"test/modules/BoxTests.cpp"
     #"test/modules/ComponentsTests.cpp"
@@ -40,7 +41,6 @@ set(SRC_TESTS
     #"test/modules/MatrixStackTests.cpp"
     #"test/modules/RingBufferTests.cpp"
     #"test/modules/StackTests.cpp"
-    #"test/modules/ViewTests.cpp"
 )
 
 set(SRC_PERFORMANCE
@@ -106,6 +106,7 @@ target_sources(core PUBLIC
         ./include/core/SpinLock.h
         ./include/core/Types.h
         ./include/core/Utility.h
+        ./include/core/View.h
 #        ./include/core/BitArray.hpp
 #        ./include/core/Box.hpp
 #        ./include/core/Components.hpp
@@ -118,7 +119,6 @@ target_sources(core PUBLIC
 #        ./include/core/RingBuffer.hpp
 #        ./include/core/Stack.hpp
 #        ./include/core/Vector.hpp
-#        ./include/core/View.hpp
 )
 install(TARGETS core FILE_SET HEADERS)
 

+ 21 - 0
include/core/View.h

@@ -0,0 +1,21 @@
+#ifndef CORE_VIEW_H
+#define CORE_VIEW_H
+
+#include "core/Matrix.h"
+
+typedef struct {
+    CoreMatrix view;
+    CoreVector3 back;
+    CoreVector3 down;
+    CoreVector3 front;
+    CoreVector3 left;
+    CoreVector3 right;
+    CoreVector3 up;
+} CoreView;
+
+#define CORE_VIEW ((CoreView){0})
+void coreUpdateDirections(CoreView* v, float lengthAngle, float widthAngle);
+void coreUpdateDirectionsQ(CoreView* v, const CoreQuaternion* q);
+CoreMatrix* coreUpdateMatrix(CoreView* v, const CoreVector3* pos);
+
+#endif

+ 48 - 0
src/View.c

@@ -0,0 +1,48 @@
+#include "core/View.h"
+
+#define CV3(a, b, c) (&(CoreVector3){{a, b, c}})
+
+void coreUpdateDirections(CoreView* v, float lengthAngle, float widthAngle) {
+    coreAngles(&v->front, lengthAngle, widthAngle);
+
+    coreCross(&v->right, &v->front, CV3(0.0f, 1.0f, 0.0f));
+    coreNormalizeV3(&v->right);
+
+    coreCross(&v->up, &v->right, &v->front);
+    coreNormalizeV3(&v->up);
+
+    coreInvertV3(&v->left, &v->right);
+    coreInvertV3(&v->back, &v->front);
+    coreInvertV3(&v->down, &v->up);
+}
+
+void coreUpdateDirectionsQ(CoreView* v, const CoreQuaternion* q) {
+    coreMulQV3(&v->up, q, CV3(0.0f, 1.0f, 0.0f));
+
+    coreMulQV3(&v->back, q, CV3(-1.0f, 0.0f, 0.0f));
+
+    coreCross(&v->right, &v->up, &v->back);
+    coreNormalizeV3(&v->right);
+
+    coreInvertV3(&v->left, &v->right);
+    coreInvertV3(&v->front, &v->back);
+    coreInvertV3(&v->down, &v->up);
+}
+
+CoreMatrix* coreUpdateMatrix(CoreView* v, const CoreVector3* pos) {
+    v->view.data[0] =
+        (CoreVector4){{v->right.data[0], v->right.data[1], v->right.data[1],
+                       -coreDotV3(&v->right, pos)}};
+    v->view.data[1] = (CoreVector4){
+        {v->up.data[0], v->up.data[1], v->up.data[1], -coreDotV3(&v->up, pos)}};
+    v->view.data[2] =
+        (CoreVector4){{v->back.data[0], v->back.data[1], v->back.data[1],
+                       -coreDotV3(&v->back, pos)}};
+    v->view.data[3] = (CoreVector4){{0.0f, 0.0f, 0.0f, 1.0f}};
+
+    //  view.set(0, Vector4(right[0], right[1], right[2], right.dot(-pos)));
+    //  view.set(1, Vector4(up[0], up[1], up[2], up.dot(-pos)));
+    //  view.set(2, Vector4(back[0], back[1], back[2], back.dot(-pos)));
+    //  view.set(3, Vector4(0.0f, 0.0f, 0.0f, 1.0f));
+    return &v->view;
+}

+ 1 - 1
test/Main.c

@@ -41,7 +41,6 @@ int main(int argAmount, const char** args) {
     // coreTestMatrixStack(light);
     // coreTestRingBuffer();
     // coreTestStack(light);
-    // coreTestView();
     coreTestBuffer(light);
     coreTestMatrix();
     coreTestPlane();
@@ -50,6 +49,7 @@ int main(int argAmount, const char** args) {
     coreTestSpinLock();
     coreTestUtility(light);
     coreTestVector();
+    coreTestView();
 
     coreLogLevel = CORE_LOG_WARNING;
     CORE_LOG_DEBUG("You won't see this!");

+ 46 - 0
test/modules/ViewTests.c

@@ -0,0 +1,46 @@
+#include "../Tests.h"
+#include "core/View.h"
+
+#define CV3(a, b, c) (&(CoreVector3){{a, b, c}})
+
+static void testFromAngles() {
+    CoreView v = {0};
+    coreUpdateDirections(&v, 0.0f, 0.0f);
+    CORE_TEST_V3(CV3(0.0f, 1.0f, 0.0f), &v.up);
+    CORE_TEST_V3(CV3(0.0f, -1.0f, 0.0f), &v.down);
+    CORE_TEST_V3(CV3(0.0f, 0.0f, -1.0f), &v.left);
+    CORE_TEST_V3(CV3(0.0f, 0.0f, 1.0f), &v.right);
+    CORE_TEST_V3(CV3(1.0f, 0.0f, 0.0f), &v.front);
+    CORE_TEST_V3(CV3(-1.0f, 0.0f, 0.0f), &v.back);
+}
+
+static void testFromQuaternion() {
+    CoreView v = {0};
+    coreUpdateDirectionsQ(&v, &CORE_UNIT_QUATERNION);
+    CORE_TEST_V3(CV3(0.0f, 1.0f, 0.0f), &v.up);
+    CORE_TEST_V3(CV3(0.0f, -1.0f, 0.0f), &v.down);
+    CORE_TEST_V3(CV3(0.0f, 0.0f, -1.0f), &v.left);
+    CORE_TEST_V3(CV3(0.0f, 0.0f, 1.0f), &v.right);
+    CORE_TEST_V3(CV3(1.0f, 0.0f, 0.0f), &v.front);
+    CORE_TEST_V3(CV3(-1.0f, 0.0f, 0.0f), &v.back);
+}
+
+static void testUpdateMatrix() {
+    CoreView v = {0};
+    CoreMatrix* m = coreUpdateMatrix(&v, CV3(1.0f, 2.0f, 3.0f));
+
+    char buffer[128];
+    coreToStringMatrix(m, buffer, sizeof(buffer));
+
+    CORE_TEST_STRING("[[0.000, 0.000, 0.000, -0.000], "
+                     "[0.000, 0.000, 0.000, -0.000], "
+                     "[0.000, 0.000, 0.000, -0.000], "
+                     "[0.000, 0.000, 0.000, 1.000]]",
+                     buffer);
+}
+
+void coreTestView() {
+    testFromAngles();
+    testFromQuaternion();
+    testUpdateMatrix();
+}