-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinstanced_entity.cpp
More file actions
80 lines (64 loc) · 1.85 KB
/
Copy pathinstanced_entity.cpp
File metadata and controls
80 lines (64 loc) · 1.85 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
////////////////////////////////////////////////////////////////////////////////
// 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/instanced_entity.h"
#include <vector>
#include "core/error_handling.h"
#include "core/matrix4.h"
#include "core/transform.h"
#include "graphics/mesh.h"
#include "graphics/render_entity_type.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
{
InstancedEntity::InstancedEntity(const Mesh *mesh, const std::vector<Transform> &instances)
: RenderEntity(mesh, PrimitiveType::TRIANGLES)
, instance_count_(instances.size())
{
ensure(instances.size() > 1, "must have at least two instances");
for (const auto &instance : instances)
{
data_.emplace_back(instance.matrix());
data_.emplace_back(create_normal_transform(instance.matrix()));
}
}
RenderEntityType InstancedEntity::type() const
{
return RenderEntityType::INSTANCED;
}
const std::vector<Matrix4> &InstancedEntity::data() const
{
return data_;
}
bool InstancedEntity::has_transparency() const
{
return false;
}
std::size_t InstancedEntity::instance_count() const
{
return instance_count_;
}
}