-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBillboard.cpp
More file actions
88 lines (67 loc) · 2.43 KB
/
Copy pathBillboard.cpp
File metadata and controls
88 lines (67 loc) · 2.43 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <cpp3ds/Graphics/Billboard.hpp>
#include <cpp3ds/Graphics/Camera.hpp>
#include <cmath>
namespace cpp3ds
{
////////////////////////////////////////////////////////////
Billboard::Billboard() :
m_camera(NULL)
{
}
////////////////////////////////////////////////////////////
Billboard::Billboard(const Texture& texture) :
Sprite (texture),
m_camera(NULL)
{
}
////////////////////////////////////////////////////////////
Billboard::Billboard(const Texture& texture, const IntRect& rectangle) :
Sprite (texture, rectangle),
m_camera(NULL)
{
}
////////////////////////////////////////////////////////////
void Billboard::draw(RenderTarget& target, RenderStates states) const
{
if (m_camera)
{
Vector3f pos = getPosition();
// Construct the basis of our rotation matrix
Vector3f zAxis = m_camera->getPosition() - pos;
float zAxisNorm = std::sqrt(zAxis.x * zAxis.x +
zAxis.y * zAxis.y +
zAxis.z * zAxis.z);
zAxis /= zAxisNorm;
Vector3f xAxis(zAxis.z, 0.f, -zAxis.x);
float xAxisNorm = std::sqrt(xAxis.x * xAxis.x +
xAxis.y * xAxis.y +
xAxis.z * xAxis.z);
xAxis /= xAxisNorm;
Vector3f yAxis(zAxis.y * xAxis.z - zAxis.z * xAxis.y,
zAxis.z * xAxis.x - zAxis.x * xAxis.z,
zAxis.x * xAxis.y - zAxis.y * xAxis.x);
// No need to normalize y axis, x and z orthogonal
// Combine a translation, rotation and translation into 1 transform
pos -= xAxis * pos.x + yAxis * pos.y + zAxis * pos.z;
Transform billboardTransform(xAxis.x, yAxis.x, zAxis.x, pos.x,
xAxis.y, yAxis.y, zAxis.y, pos.y,
xAxis.z, yAxis.z, zAxis.z, pos.z,
0.f, 0.f, 0.f, 1.f);
states.transform *= billboardTransform;
}
Sprite::draw(target, states);
}
////////////////////////////////////////////////////////////
void Billboard::setCamera(const Camera& camera)
{
m_camera = &camera;
}
////////////////////////////////////////////////////////////
const Camera* Billboard::getCamera() const
{
return m_camera;
}
} // namespace cpp3ds