-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsingle_entity.cpp
More file actions
166 lines (137 loc) · 3.59 KB
/
Copy pathsingle_entity.cpp
File metadata and controls
166 lines (137 loc) · 3.59 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "graphics/single_entity.h"
#include <string>
#include <string_view>
#include "core/camera_type.h"
#include "core/error_handling.h"
#include "core/matrix4.h"
#include "core/quaternion.h"
#include "core/transform.h"
#include "core/vector3.h"
#include "graphics/render_entity.h"
#include "graphics/render_entity_type.h"
#include "graphics/skeleton.h"
namespace
{
/**
* Helper function to create a normal transformation matrix from a model
* matrix.
*
* @param model
* The model matrix to calculate from.
*
* @returns
* Normal transformation matrix.
*/
iris::Matrix4 create_normal_transform(const iris::Matrix4 &model)
{
auto normal = iris::Matrix4::transpose(iris::Matrix4::invert(model));
// remove the translation components
normal[3] = 0.0f;
normal[7] = 0.0f;
normal[11] = 0.0f;
return normal;
}
}
namespace iris
{
SingleEntity::SingleEntity(
const Mesh *mesh,
const Vector3 &position,
bool has_transparency,
PrimitiveType primitive_type)
: SingleEntity(mesh, {position, {}, {1.0f}}, has_transparency, primitive_type)
{
}
SingleEntity::SingleEntity(
const Mesh *mesh,
const Transform &transform,
bool has_transparency,
PrimitiveType primitive_type)
: SingleEntity(mesh, transform, nullptr, has_transparency, primitive_type)
{
}
SingleEntity::SingleEntity(
const Mesh *mesh,
const Transform &transform,
Skeleton *skeleton,
bool has_transparency,
PrimitiveType primitive_type)
: RenderEntity(mesh, primitive_type)
, transform_(transform)
, normal_()
, skeleton_(skeleton)
, has_transparency_(has_transparency)
{
ensure(mesh != nullptr, "must supply mesh");
normal_ = create_normal_transform(transform_.matrix());
}
RenderEntityType SingleEntity::type() const
{
return RenderEntityType::SINGLE;
}
Vector3 SingleEntity::position() const
{
return transform_.translation();
}
void SingleEntity::set_position(const Vector3 &position)
{
transform_.set_translation(position);
normal_ = create_normal_transform(transform_.matrix());
}
Quaternion SingleEntity::orientation() const
{
return transform_.rotation();
}
void SingleEntity::set_orientation(const Quaternion &orientation)
{
transform_.set_rotation(orientation);
normal_ = create_normal_transform(transform_.matrix());
}
Vector3 SingleEntity::scale() const
{
return transform_.scale();
}
void SingleEntity::set_scale(const Vector3 &scale)
{
transform_.set_scale(scale);
normal_ = create_normal_transform(transform_.matrix());
}
Matrix4 SingleEntity::transform() const
{
return transform_.matrix();
}
void SingleEntity::set_transform(const Matrix4 &transform)
{
transform_.set_matrix(transform);
normal_ = create_normal_transform(transform_.matrix());
}
void SingleEntity::set_transform(const Transform &transform)
{
set_transform(transform.matrix());
}
Matrix4 SingleEntity::normal_transform() const
{
return normal_;
}
void SingleEntity::set_mesh(const Mesh *mesh)
{
mesh_ = mesh;
}
Skeleton *SingleEntity::skeleton()
{
return skeleton_;
}
const Skeleton *SingleEntity::skeleton() const
{
return skeleton_;
}
bool SingleEntity::has_transparency() const
{
return has_transparency_;
}
}