openMSX
gl_transform.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "gl_transform.hh"
3
4using namespace gl;
5
6// Test approximations.
7static bool approxEq(float x, float y)
8{
9 return fabsf(x - y) < 1.0e-5f;
10}
11static constexpr bool approxEq(const vec4& x, const vec4&y)
12{
13 return length2(x - y) < 1.0e-4f;
14}
15static constexpr bool approxEq(const mat4& x, const mat4&y)
16{
17 return norm2_2(x - y) < 1.0e-3f;
18}
19
20
21TEST_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
39TEST_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
60TEST_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
83TEST_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.05647f, 0.231566f, 2.19778f, 1.0f)));
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
163TEST_CASE("gl_transform: ortho")
164{
165 mat4 O1 = ortho(0, 640, 480, 0, -1, 1);
166 mat4 O2 = ortho(640, 480);
167 CHECK(approxEq(O1, O2));
168 CHECK(approxEq(O1, mat4(vec4(0.003125f, 0.0f, 0.0f, 0.0f),
169 vec4(0.0f, -0.00416667f, 0.0f, 0.0f),
170 vec4(0.0f, 0.0f, -1.0f, 0.0f),
171 vec4(-1.0f, 1.0f, 0.0f, 1.0f))));
172}
173
174TEST_CASE("gl_transform: frustum")
175{
176 mat4 F = frustum(0, 640, 0, 480, -1, 1);
177 CHECK(approxEq(F, mat4(vec4(-0.003125f, 0.0f, 0.0f, 0.0f),
178 vec4(0.0f, 0.00416667f, 0.0f, 0.0f),
179 vec4(1.0f, 1.0f, 0.0f, -1.0f),
180 vec4(0.0f, 0.0f, 1.0f, 0.0f))));
181}
182
183
184#if 0
185
186// The following functions are not part of the actual test. They get compiled,
187// but never executed. I used them to (manually) inspect the quality of the
188// generated code.
189
190void test_scale(float x, float y, float z, mat4& A)
191{
192 A = scale(vec3(x, y, z));
193}
194void test_scale(mat4& A, float x, float y, float z, mat4& B)
195{
196 B = scale(A, vec3(x, y, z));
197}
198
199void test_translate(float x, float y, float z, mat4& A)
200{
201 A = translate(vec3(x, y, z));
202}
203void test_translate(mat4& A, float x, float y, float z, mat4& B)
204{
205 B = translate(A, vec3(x, y, z));
206}
207
208void test_rotate(float a, float x, float y, float z, mat4& A)
209{
210 A = rotate(a, vec3(x, y, z));
211}
212
213void test_ortho(float l, float r, float b, float t, float n, float f, mat4& A)
214{
215 A = ortho(l, r, b, t, n, f);
216}
217
218void test_frustum(float l, float r, float b, float t, float n, float f, mat4& A)
219{
220 A = frustum(l, r, b, t, n, f);
221}
222
223#endif
TclObject t
CHECK(m3==m3)
TEST_CASE("gl_transform: scale")
Definition gl_mat.hh:23
constexpr T norm2_2(const matMxN< M, N, T > &A)
Definition gl_mat.hh:318
mat4 rotateZ(float angle)
vecN< 3, float > vec3
Definition gl_vec.hh:383
mat4 rotate(float angle, const vec3 &axis)
mat4 rotateY(float angle)
mat4 rotateX(float angle)
constexpr mat4 ortho(float left, float right, float bottom, float top, float nearVal, float farVal)
matMxN< 4, 4, float > mat4
Definition gl_mat.hh:174
vecN< 4, float > vec4
Definition gl_vec.hh:384
T length(const vecN< N, T > &x)
Definition gl_vec.hh:505
constexpr T length2(const vecN< N, T > &x)
Definition gl_vec.hh:498
constexpr matMxN< N, M, T > transpose(const matMxN< M, N, T > &A)
Definition gl_mat.hh:188
constexpr T radians(T d)
Definition gl_vec.hh:410
constexpr matMxN< 2, 2, T > inverse(const matMxN< 2, 2, T > &A)
Definition gl_mat.hh:235
constexpr mat4 frustum(float left, float right, float bottom, float top, float nearVal, float farVal)
vecN< N, T > normalize(const vecN< N, T > &x)
Definition gl_vec.hh:512
constexpr mat4 translate(const vec3 &xyz)
constexpr mat4 scale(const vec3 &xyz)
std::array< const EDStorage, 4 > A