-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.h
More file actions
38 lines (30 loc) · 743 Bytes
/
util.h
File metadata and controls
38 lines (30 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef _UTIL_H_
#define _UTIL_H_
#include "common.h"
inline glm::quat RotateBetweenVectors(glm::vec3 u, glm::vec3 v)
{
u = glm::normalize(u);
v = glm::normalize(v);
float cosine = glm::dot(u, v);
glm::vec3 rotation_axis;
if (cosine < -1 + 0.001f)
{
rotation_axis = glm::cross(glm::vec3(0, 0, 1), u);
if (glm::length2(rotation_axis) < 0.01f)
{
rotation_axis = glm::cross(glm::vec3(1, 0, 0), u);
}
rotation_axis = normalize(rotation_axis);
return glm::angleAxis(glm::radians(180.0f), rotation_axis);
}
rotation_axis = glm::cross(u, v);
float s = sqrt((1 + cosine) * 2);
float invs = 1 / s;
return glm::quat(
s * 0.5f,
rotation_axis.x * invs,
rotation_axis.y * invs,
rotation_axis.z * invs
);
}
#endif