1 | #include <glm/glm.hpp> |
---|
2 | #include <iostream> |
---|
3 | #include <ostream> |
---|
4 | |
---|
5 | using namespace glm; |
---|
6 | |
---|
7 | std::ostream& operator<<(std::ostream& os, glm::tvec3<float>& v) { |
---|
8 | os << "<" << v.x << "," << v.y << "," << v.z << ">"; |
---|
9 | return os; |
---|
10 | } |
---|
11 | std::ostream& operator<<(std::ostream& os, glm::tvec3<float>&& v) { |
---|
12 | os << "<" << v.x << "," << v.y << "," << v.z << ">"; |
---|
13 | return os; |
---|
14 | } |
---|
15 | |
---|
16 | float length_squared(glm::tvec3<float> v) { |
---|
17 | return glm::length(v) * glm::length(v); |
---|
18 | } |
---|
19 | |
---|
20 | tvec3<float> project(glm::tvec3<float> u, glm::tvec3<float> v) { |
---|
21 | return normalize(v) * dot(u, normalize(v)); |
---|
22 | } |
---|
23 | |
---|
24 | int main(void) { |
---|
25 | tvec3<float> v1 = {1.f,2.f,3.f}; |
---|
26 | std::cout << "ctor(x,y):" << v1 << std::endl; |
---|
27 | |
---|
28 | tvec3<float> v2 = v1; |
---|
29 | std::cout << "copy ctor:" << v2 << std::endl; |
---|
30 | |
---|
31 | v2 = (tvec3<float>){3.f, 4.2f, -2.f}; |
---|
32 | std::cout << "assignment:" << v2 << std::endl; |
---|
33 | |
---|
34 | v2 = v1; |
---|
35 | std::cout << "move assignment:" << v2 << std::endl; |
---|
36 | |
---|
37 | // tvec3<float> v3 = 0; |
---|
38 | // std::cout << "zero-init:" << v3 << std::endl; |
---|
39 | |
---|
40 | // v1 = 0; |
---|
41 | // std::cout << "zero-assign:" << v1 << std::endl; |
---|
42 | // |
---|
43 | // tvec3<float> v4 = {1.23f}; |
---|
44 | // std::cout << "fill-ctor:" << v4 << std::endl; |
---|
45 | // |
---|
46 | v1 = (tvec3<float>){1.23f, 3.43f, 0.000002f}; |
---|
47 | std::cout << "?-?:" << (v1 - (tvec3<float>){1.21f,3,1}) << std::endl; |
---|
48 | |
---|
49 | v1 -= (tvec3<float>){1.23f, 3.43f, 0.000002f}; |
---|
50 | std::cout << "?-=?:" << v1 << std::endl; |
---|
51 | |
---|
52 | v1 = -v1; |
---|
53 | std::cout << "-?:" << v1 << std::endl; |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | |
---|
59 | v1 = (tvec3<float>){1.5f, 2.75f, -14.2f}; |
---|
60 | std::cout << "?+?:" << (v1 + (tvec3<float>){0.8f, -0.3f, 5}) << std::endl; |
---|
61 | |
---|
62 | v1 += (tvec3<float>){0.8f, -0.3f, 5}; |
---|
63 | std::cout << "?+=?:" << v1 << std::endl; |
---|
64 | |
---|
65 | v1 = (tvec3<float>){1.5f, 2.75f, 100.3f}; |
---|
66 | std::cout << "v*s:" << v1 * 3.f << std::endl; |
---|
67 | |
---|
68 | std::cout << "s*v:" << 3.f * v1 << std::endl; |
---|
69 | |
---|
70 | v1 *= 3; |
---|
71 | std::cout << "?*=?:" << v1 << std::endl; |
---|
72 | |
---|
73 | v1 = (tvec3<float>){2, -0.1f, 45}; |
---|
74 | std::cout << "?/?:" << (v1 / 3.f) << std::endl; |
---|
75 | |
---|
76 | v1 /= 3.f; |
---|
77 | std::cout << "?/=?:" << v1 << std::endl; |
---|
78 | |
---|
79 | |
---|
80 | v1 = (tvec3<float>){4,2,3}; |
---|
81 | v2 = (tvec3<float>){0,-3,2}; |
---|
82 | std::cout << "dot_1:" << dot(v1, v2) << std::endl; |
---|
83 | |
---|
84 | v2 = (tvec3<float>){1.3f, -2, 12.2}; |
---|
85 | std::cout << "dot_2:" << dot(v1, v2) << std::endl; |
---|
86 | |
---|
87 | |
---|
88 | v1 = (tvec3<float>){1,2,3}; |
---|
89 | std::cout << "length:" << length(v1) << std::endl; |
---|
90 | |
---|
91 | std::cout << "length_squared:" << length_squared(v1) << std::endl; |
---|
92 | |
---|
93 | v2 = (tvec3<float>){6, -3.2f, 1}; |
---|
94 | std::cout << "distance:" << distance(v1, v2) << std::endl; |
---|
95 | |
---|
96 | std::cout << "normalize:" << normalize(v2) << std::endl; |
---|
97 | |
---|
98 | std::cout << "project:" << project((tvec3<float>){5,6,0}, (tvec3<float>){0, 0, 1}) << std::endl; |
---|
99 | std::cout << "project_2:" << project((tvec3<float>){5,6,3.2f}, (tvec3<float>){10, 1, 4}) << std::endl; |
---|
100 | |
---|
101 | v1 = (tvec3<float>){5,6,2.333f}; |
---|
102 | v2 = (tvec3<float>){1,0,-13.5f}; |
---|
103 | std::cout << "reflect:" << reflect(v1,normalize(v2)) << std::endl; |
---|
104 | |
---|
105 | v2 = (tvec3<float>){0,-1,2}; |
---|
106 | std::cout << "refract:" << refract(normalize(v1),normalize(v2),1.f) << std::endl; |
---|
107 | std::cout << "refract:" << refract(normalize(v1),normalize(v2),1.f/1.33f) << std::endl; |
---|
108 | |
---|
109 | tvec3<float> geometric_normal = {5,6,1}; |
---|
110 | tvec3<float> perturbed_normal = {4,5.5f,2}; |
---|
111 | tvec3<float> eyeline = {-1,0.002f,-1.0345f}; |
---|
112 | std::cout << "faceforward_nochange:" << faceforward(perturbed_normal, eyeline, geometric_normal) << std::endl; |
---|
113 | |
---|
114 | eyeline = (tvec3<float>){1,0.002f,-1.0345f}; |
---|
115 | std::cout << "faceforward_flip:" << faceforward(perturbed_normal, eyeline, geometric_normal) << std::endl; |
---|
116 | } |
---|
117 | |
---|