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