]> git.localhorst.tv Git - blank.git/blob - src/world/Entity.cpp
"streamlined" model/VAO handling
[blank.git] / src / world / Entity.cpp
1 #include "Entity.hpp"
2
3 #include "../model/Shape.hpp"
4
5 #include <cmath>
6 #include <glm/gtx/transform.hpp>
7
8 namespace {
9
10 blank::EntityModel::Buffer model_buffer;
11
12 }
13
14 namespace blank {
15
16 Entity::Entity() noexcept
17 : shape(nullptr)
18 , model()
19 , name("anonymous")
20 , bounds()
21 , velocity(0, 0, 0)
22 , position(0, 0, 0)
23 , chunk(0, 0, 0)
24 , angular_velocity(1.0f, 0.0f, 0.0f, 0.0f)
25 , rotation(1.0f)
26 , world_collision(false) {
27
28 }
29
30
31 void Entity::SetShape(const Shape *s, const glm::vec3 &color) {
32         shape = s;
33         model_buffer.Clear();
34         shape->Vertices(model_buffer.vertices, model_buffer.normals, model_buffer.indices);
35         model_buffer.colors.resize(shape->VertexCount(), color);
36         model.Update(model_buffer);
37 }
38
39 void Entity::SetShapeless() noexcept {
40         shape = nullptr;
41 }
42
43
44 void Entity::Velocity(const glm::vec3 &vel) noexcept {
45         velocity = vel;
46 }
47
48 void Entity::Position(const Block::Pos &pos) noexcept {
49         position = pos;
50         while (position.x >= Chunk::width) {
51                 position.x -= Chunk::width;
52                 ++chunk.x;
53         }
54         while (position.x < 0) {
55                 position.x += Chunk::width;
56                 --chunk.x;
57         }
58         while (position.y >= Chunk::height) {
59                 position.y -= Chunk::height;
60                 ++chunk.y;
61         }
62         while (position.y < 0) {
63                 position.y += Chunk::height;
64                 --chunk.y;
65         }
66         while (position.z >= Chunk::depth) {
67                 position.z -= Chunk::depth;
68                 ++chunk.z;
69         }
70         while (position.z < 0) {
71                 position.z += Chunk::depth;
72                 --chunk.z;
73         }
74 }
75
76 void Entity::Move(const glm::vec3 &delta) noexcept {
77         Position(position + delta);
78 }
79
80 void Entity::AngularVelocity(const glm::quat &v) noexcept {
81         angular_velocity = v;
82 }
83
84 void Entity::Rotation(const glm::mat4 &rot) noexcept {
85         rotation = rot;
86 }
87
88 void Entity::Rotate(const glm::quat &delta) noexcept {
89         Rotation(rotation * glm::mat4_cast(delta));
90 }
91
92 glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const noexcept {
93         const glm::vec3 chunk_pos = (chunk - chunk_offset) * Chunk::Extent();
94         return glm::translate(position + chunk_pos) * rotation;
95 }
96
97 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
98         glm::mat4 transform = Transform(chunk_offset);
99         glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
100         from /= from.w;
101         glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
102         to /= to.w;
103         return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
104 }
105
106 void Entity::Update(int dt) noexcept {
107         Move(velocity * float(dt));
108         Rotate(angular_velocity * float(dt));
109 }
110
111
112 void Entity::Draw() noexcept {
113         model.Draw();
114 }
115
116 }