aboutsummaryrefslogtreecommitdiff
path: root/test/mat4_test.c
blob: d4f61bcf5bdb8c3c1a737befb1191e8932aeb96b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <math/mat4.h>

#include <math/float.h>

#include "test.h"

#include <stdio.h>

static const float eps = 1e-9;

static inline void print_mat4(mat4 m) {
  printf("\n");
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      printf("%6.3f  ", mat4_at(m, i, j));
    }
    printf("\n");
  }
}

TEST_CASE(lookat_right) {
  const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0),
                             /*target=*/vec3_make(1, 0, 0),
                             /*up=*/vec3_make(0, 1, 0));

  const mat4 expected = mat4_from_array((const float*)(float[4][4]){
      {0, 0, 1, 0},
      {0, 1, 0, 0},
      {1, 0, 0, 0},
      {0, 0, 0, 1},
  });

  TEST_TRUE(mat4_eq(m, expected, eps));
}

TEST_CASE(lookat_left) {
  const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0),
                             /*target=*/vec3_make(-1, 0, 0),
                             /*up=*/vec3_make(0, 1, 0));

  const mat4 expected = mat4_from_array((const float*)(float[4][4]){
      {0, 0, -1, 0},
      {0, 1, 0, 0},
      {-1, 0, 0, 0},
      {0, 0, 0, 1},
  });

  TEST_TRUE(mat4_eq(m, expected, eps));
}

TEST_CASE(lookat_up) {
  const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0),
                             /*target=*/vec3_make(0, 1, 0),
                             /*up=*/vec3_make(0, 0, 1));

  const mat4 expected = mat4_from_array((const float*)(float[4][4]){
      {1, 0, 0, 0},
      {0, 0, 1, 0},
      {0, 1, 0, 0},
      {0, 0, 0, 1},
  });

  TEST_TRUE(mat4_eq(m, expected, eps));
}

TEST_CASE(lookat_down) {
  const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0),
                             /*target=*/vec3_make(0, -1, 0),
                             /*up=*/vec3_make(0, 0, -1));

  const mat4 expected = mat4_from_array((const float*)(float[4][4]){
      {1, 0, 0, 0},
      {0, 0, -1, 0},
      {0, -1, 0, 0},
      {0, 0, 0, 1},
  });

  TEST_TRUE(mat4_eq(m, expected, eps));
}

TEST_CASE(lookat_forward) {
  const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0),
                             /*target=*/vec3_make(0, 0, -1),
                             /*up=*/vec3_make(0, 1, 0));

  const mat4 expected = mat4_from_array((const float*)(float[4][4]){
      {1, 0, 0, 0},
      {0, 1, 0, 0},
      {0, 0, -1, 0},
      {0, 0, 0, 1},
  });

  TEST_TRUE(mat4_eq(m, expected, eps));
}

TEST_CASE(lookat_back) {
  const mat4 m = mat4_lookat(/*position=*/vec3_make(0, 0, 0),
                             /*target=*/vec3_make(0, 0, 1),
                             /*up=*/vec3_make(0, 1, 0));

  const mat4 expected = mat4_from_array((const float*)(float[4][4]){
      {-1, 0, 0, 0},
      {0, 1, 0, 0},
      {0, 0, 1, 0},
      {0, 0, 0, 1},
  });

  TEST_TRUE(mat4_eq(m, expected, eps));
}

int main() { return 0; }