Cogs.Core
Math.h
1#pragma once
2
3#include <glm/glm.hpp>
4#include <glm/ext/quaternion_float.hpp>
5
6#include <cmath>
7
8namespace Cogs::Core
9{
10 inline glm::vec3 euclidean(const glm::vec4 h)
11 {
12 return (1.f / h.w) * glm::vec3(h);
13 }
14
15 inline glm::quat getRotation(glm::vec3 src, glm::vec3 dest)
16 {
17 glm::vec3 from = glm::normalize(src);
18 glm::vec3 to = glm::normalize(dest);
19
20 const float dot = glm::dot(from, to);
21 auto crossvec = glm::cross(from, to);
22 const float crosslen = glm::length(crossvec);
23
24 if (crosslen == 0.0f) {
25 if (dot > 0.0f) {
26 return glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
27 }
28 else {
29 glm::vec3 t = glm::cross(from, glm::vec3(1.0f, 0.0f, 0.0f));
30
31 if (glm::length(t) == 0.0f) {
32 t = glm::cross(from, glm::vec3(0.0f, 1.0f, 0.0f));
33 t = glm::normalize(t);
34 }
35 return glm::quat(0.0f, t);
36 }
37 }
38 else {
39 crossvec = glm::normalize(crossvec);
40
41 // Vectors are not parallel
42 // The fabs() wrapping is to avoid problems when `dot' "overflows"
43 // a tiny wee bit, which can lead to sqrt() returning NaN.
44 crossvec *= std::sqrt(0.5f * std::fabs(1.0f - dot));
45
46 // The fabs() wrapping is to avoid problems when `dot' "underflows"
47 // a tiny wee bit, which can lead to sqrt() returning NaN.
48 return glm::quat(std::sqrt(0.5f * std::fabs(1.0f + dot)), crossvec);
49 }
50 }
51}
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....