openMSX
gl_transform.cc
Go to the documentation of this file.
1 #include "catch.hpp"
2 #include "gl_transform.hh"
3 
4 using namespace gl;
5 
6 // Test approximations.
7 static bool approxEq(float x, float y)
8 {
9  return fabsf(x - y) < 1.0e-5f;
10 }
11 static bool approxEq(const vec4& x, const vec4&y)
12 {
13  return length2(x - y) < 1.0e-4f;
14 }
15 static bool approxEq(const mat4& x, const mat4&y)
16 {
17  return norm2_2(x - y) < 1.0e-3f;
18 }
19 
20 
21 TEST_CASE("gl_transform: scale")
22 {
23  mat4 S1 = scale(vec3(1, 2, 3));
24  mat4 S2 = scale(S1, vec3(2, 3, 4)); // first this scale, then S1
25  CHECK(S1 == mat4(vec4(1, 0, 0, 0), vec4(0, 2, 0, 0), vec4(0, 0, 3, 0), vec4(0, 0, 0, 1)));
26  CHECK(S2 == mat4(vec4(2, 0, 0, 0), vec4(0, 6, 0, 0), vec4(0, 0, 12, 0), vec4(0, 0, 0, 1)));
27 
28  vec4 p(4, 5, 6, 1); // point
29  vec4 d(4, 5, 6, 0); // direction
30  CHECK(S1 * p == vec4(4, 10, 18, 1));
31  CHECK(S1 * d == vec4(4, 10, 18, 0));
32  CHECK(S2 * p == vec4(8, 30, 72, 1));
33  CHECK(S2 * d == vec4(8, 30, 72, 0));
34 
35  CHECK(S1 * S2 == S2 * S1); // scaling is commutative
36  CHECK(approxEq(inverse(S1), scale(vec3(1.0f / 1.0f, 1.0f / 2.0f, 1.0f / 3.0f))));
37 }
38 
39 TEST_CASE("gl_transform: translate")
40 {
41  mat4 T1 = translate( vec3( 1, 2, 3));
42  mat4 T2 = translate(T1, vec3(-2, 1, -1)); // first this, then T1
43  mat4 T3 = translate( vec3(-1, 3, 2));
44 
45  CHECK(T1 == mat4(vec4(1, 0, 0, 0), vec4(0, 1, 0, 0), vec4(0, 0, 1, 0), vec4( 1, 2, 3, 1)));
46  CHECK(T2 == mat4(vec4(1, 0, 0, 0), vec4(0, 1, 0, 0), vec4(0, 0, 1, 0), vec4(-1, 3, 2, 1)));
47  CHECK(T2 == T3);
48 
49  vec4 p(4, 5, 6, 1); // point
50  vec4 d(4, 5, 6, 0); // direction
51  CHECK((T1 * p) == (p + vec4( 1, 2, 3, 0)));
52  CHECK((T1 * d) == d);
53  CHECK((T2 * p) == (p + vec4(-1, 3, 2, 0)));
54  CHECK((T2 * d) == d);
55 
56  CHECK(T1 * T2 == T2 * T1); // translation is commutative
57  CHECK(approxEq(inverse(T1), translate(vec3(-1, -2, -3))));
58 }
59 
60 TEST_CASE("gl_transform: scale + translate")
61 {
62  vec3 s(2, 1, 1);
63  vec3 t(1, 1, 0);
64  mat4 S1 = scale(s);
65  mat4 T1 = translate(t);
66  mat4 ST1 = T1 * S1; // first scale, then translate
67  mat4 TS1 = S1 * T1; // first translate, then scale
68  mat4 ST2 = scale(T1, s); // first scale, then translate
69  mat4 TS2 = translate(S1, t); // first translate, then scale
70 
71  CHECK(ST1 == ST2);
72  CHECK(TS1 == TS2);
73  CHECK(ST1 != TS1); // not commutative
74 
75  vec4 p(4, 5, 6, 1); // point
76  vec4 d(4, 5, 6, 0); // direction
77  CHECK(ST1 * p == vec4( 9, 6, 6, 1));
78  CHECK(ST1 * d == vec4( 8, 5, 6, 0));
79  CHECK(TS1 * p == vec4(10, 6, 6, 1));
80  CHECK(TS1 * d == vec4( 8, 5, 6, 0));
81 }
82 
83 TEST_CASE("gl_transform: rotation")
84 {
85  float deg0 = radians( 0.0f);
86  float deg90 = radians( 90.0f);
87  float deg180 = radians(180.0f);
88  float deg270 = radians(270.0f);
89 
90  SECTION("x") {
91  mat4 R0 = rotateX(deg0);
92  mat4 R90 = rotateX(deg90);
93  mat4 R180 = rotateX(deg180);
94  mat4 R270 = rotateX(deg270);
95  CHECK(approxEq(R0, mat4(vec4(1, 0, 0, 0), vec4(0, 1, 0, 0), vec4(0, 0, 1, 0), vec4(0, 0, 0, 1))));
96  CHECK(approxEq(R90, mat4(vec4(1, 0, 0, 0), vec4(0, 0, 1, 0), vec4(0, -1, 0, 0), vec4(0, 0, 0, 1))));
97  CHECK(approxEq(R180, mat4(vec4(1, 0, 0, 0), vec4(0, -1, 0, 0), vec4(0, 0, -1, 0), vec4(0, 0, 0, 1))));
98  CHECK(approxEq(R270, mat4(vec4(1, 0, 0, 0), vec4(0, 0, -1, 0), vec4(0, 1, 0, 0), vec4(0, 0, 0, 1))));
99 
100  CHECK(approxEq(rotateX(R90, deg90 ), R180));
101  CHECK(approxEq(rotateX(R90, deg180), R270));
102  CHECK(approxEq(rotateX(R90, deg270), R0 ));
103  CHECK(approxEq(rotateX(R180, deg270), R90 ));
104 
105  CHECK(approxEq(inverse(R90 ), R270));
106  CHECK(approxEq(inverse(R180), R180));
107  CHECK(approxEq(inverse(R90), transpose(R90)));
108  }
109  SECTION("y") {
110  mat4 R0 = rotateY(deg0);
111  mat4 R90 = rotateY(deg90);
112  mat4 R180 = rotateY(deg180);
113  mat4 R270 = rotateY(deg270);
114  CHECK(approxEq(R0, mat4(vec4( 1, 0, 0, 0), vec4(0, 1, 0, 0), vec4( 0, 0, 1, 0), vec4(0, 0, 0, 1))));
115  CHECK(approxEq(R90, mat4(vec4( 0, 0, -1, 0), vec4(0, 1, 0, 0), vec4( 1, 0, 0, 0), vec4(0, 0, 0, 1))));
116  CHECK(approxEq(R180, mat4(vec4(-1, 0, 0, 0), vec4(0, 1, 0, 0), vec4( 0, 0, -1, 0), vec4(0, 0, 0, 1))));
117  CHECK(approxEq(R270, mat4(vec4( 0, 0, 1, 0), vec4(0, 1, 0, 0), vec4(-1, 0, 0, 0), vec4(0, 0, 0, 1))));
118 
119  CHECK(approxEq(rotateY(R180, deg90 ), R270));
120  CHECK(approxEq(rotateY(R180, deg180), R0 ));
121  CHECK(approxEq(rotateY(R180, deg270), R90 ));
122  CHECK(approxEq(rotateY(R270, deg270), R180));
123 
124  CHECK(approxEq(inverse(R90 ), R270));
125  CHECK(approxEq(inverse(R180), R180));
126  CHECK(approxEq(inverse(R90), transpose(R90)));
127  }
128  SECTION("z") {
129  mat4 R0 = rotateZ(deg0);
130  mat4 R90 = rotateZ(deg90);
131  mat4 R180 = rotateZ(deg180);
132  mat4 R270 = rotateZ(deg270);
133  CHECK(approxEq(R0, mat4(vec4( 1, 0, 0, 0), vec4( 0, 1, 0, 0), vec4(0, 0, 1, 0), vec4(0, 0, 0, 1))));
134  CHECK(approxEq(R90, mat4(vec4( 0, 1, 0, 0), vec4(-1, 0, 0, 0), vec4(0, 0, 1, 0), vec4(0, 0, 0, 1))));
135  CHECK(approxEq(R180, mat4(vec4(-1, 0, 0, 0), vec4( 0, -1, 0, 0), vec4(0, 0, 1, 0), vec4(0, 0, 0, 1))));
136  CHECK(approxEq(R270, mat4(vec4( 0, -1, 0, 0), vec4( 1, 0, 0, 0), vec4(0, 0, 1, 0), vec4(0, 0, 0, 1))));
137 
138  CHECK(approxEq(rotateZ(R270, deg90 ), R0 ));
139  CHECK(approxEq(rotateZ(R270, deg180), R90 ));
140  CHECK(approxEq(rotateZ(R270, deg270), R180));
141  CHECK(approxEq(rotateZ(R90, deg270), R0 ));
142 
143  CHECK(approxEq(inverse(R90 ), R270));
144  CHECK(approxEq(inverse(R180), R180));
145  CHECK(approxEq(inverse(R90), transpose(R90)));
146  }
147  SECTION("arbitrary axis") {
148  vec3 axis = normalize(vec3(1, 2, 3));
149  mat4 Rx90 = rotateX(deg90);
150  mat4 rot1 = rotate(1.23f, axis);
151  mat4 rot2 = rotate(Rx90, 1.23f, axis);
152  mat4 rot3 = Rx90 * rot1;
153  CHECK(approxEq(rot2, rot3));
154  vec4 p(-1, 2, 1, 1);
155  vec4 q = rot1 * p;
156  CHECK(approxEq(q, vec4(-1.05647, 0.231566, 2.19778, 1)));
157  CHECK(approxEq(length(p), length(q)));
158  CHECK(approxEq(inverse(rot1), rotate(-1.23f, axis)));
159  CHECK(approxEq(inverse(rot1), transpose(rot1)));
160  }
161 }
162 
163 TEST_CASE("gl_transform: ortho")
164 {
165  mat4 O = ortho(0, 640, 0, 480, -1, 1);
166  CHECK(approxEq(O, mat4(vec4(0.003125, 0, 0, 0),
167  vec4(0, 0.00416667, 0, 0),
168  vec4(0, 0, -1, 0),
169  vec4(-1, -1, 0, 1))));
170 }
171 
172 TEST_CASE("gl_transform: frustum")
173 {
174  mat4 F = frustum(0, 640, 0, 480, -1, 1);
175  CHECK(approxEq(F, mat4(vec4(-0.003125, 0, 0, 0),
176  vec4(0, 0.00416667, 0, 0),
177  vec4(1, 1, 0, -1),
178  vec4(0, 0, 1, 0))));
179 }
180 
181 
182 #if 0
183 
184 // The following functions are not part of the actual test. They get compiled,
185 // but never executed. I used them to (manually) inspect the quality of the
186 // generated code.
187 
188 void test_scale(float x, float y, float z, mat4& A)
189 {
190  A = scale(vec3(x, y, z));
191 }
192 void test_scale(mat4& A, float x, float y, float z, mat4& B)
193 {
194  B = scale(A, vec3(x, y, z));
195 }
196 
197 void test_translate(float x, float y, float z, mat4& A)
198 {
199  A = translate(vec3(x, y, z));
200 }
201 void test_translate(mat4& A, float x, float y, float z, mat4& B)
202 {
203  B = translate(A, vec3(x, y, z));
204 }
205 
206 void test_rotate(float a, float x, float y, float z, mat4& A)
207 {
208  A = rotate(a, vec3(x, y, z));
209 }
210 
211 void test_ortho(float l, float r, float b, float t, float n, float f, mat4& A)
212 {
213  A = ortho(l, r, b, t, n, f);
214 }
215 
216 void test_frustum(float l, float r, float b, float t, float n, float f, mat4& A)
217 {
218  A = frustum(l, r, b, t, n, f);
219 }
220 
221 #endif
mat4 frustum(float left, float right, float bottom, float top, float nearVal, float farVal)
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:343
mat4 rotateY(float angle)
T length2(const vecN< N, T > &x)
Definition: gl_vec.hh:336
TEST_CASE("gl_transform: scale")
Definition: gl_transform.cc:21
matMxN< 4, 4, float > mat4
Definition: gl_mat.hh:159
vecN< N, T > normalize(const vecN< N, T > &x)
Definition: gl_vec.hh:350
T radians(T d)
Definition: gl_vec.hh:160
T norm2_2(const matMxN< M, N, T > &A)
Definition: gl_mat.hh:376
mat4 scale(const vec3 &xyz)
Definition: gl_transform.hh:19
#define O(a)
Definition: YM2151.cc:137
CHECK(m3==m3)
vecN< 4, float > vec4
Definition: gl_vec.hh:141
mat4 rotateZ(float angle)
matMxN< 2, 2, T > inverse(const matMxN< 2, 2, T > &A)
Definition: gl_mat.hh:293
vecN< 3, float > vec3
Definition: gl_vec.hh:140
matMxN< N, M, T > transpose(const matMxN< M, N, T > &A)
Definition: gl_mat.hh:246
mat4 translate(const vec3 &xyz)
Definition: gl_transform.hh:36
mat4 rotate(float angle, const vec3 &axis)
Definition: gl_transform.hh:58
mat4 ortho(float left, float right, float bottom, float top, float nearVal, float farVal)
mat4 rotateX(float angle)
Definition: gl_transform.hh:93
TclObject t
Definition: gl_mat.hh:24