3 #include "../model/geometry.hpp"
4 #include "../model/Shape.hpp"
7 #include <glm/gtx/transform.hpp>
11 blank::Model::Buffer model_buffer;
17 Entity::Entity() noexcept
23 , angular_velocity(1.0f, 0.0f, 0.0f, 0.0f)
29 void Entity::SetShape(const Shape *s, const glm::vec3 &color) {
32 shape->Vertices(model_buffer.vertices, model_buffer.normals, model_buffer.indices);
33 model_buffer.colors.resize(shape->VertexCount(), color);
34 model.Update(model_buffer);
37 void Entity::SetShapeless() noexcept {
42 void Entity::Velocity(const glm::vec3 &vel) noexcept {
46 void Entity::Position(const Block::Pos &pos) noexcept {
48 while (position.x >= Chunk::width) {
49 position.x -= Chunk::width;
52 while (position.x < 0) {
53 position.x += Chunk::width;
56 while (position.y >= Chunk::height) {
57 position.y -= Chunk::height;
60 while (position.y < 0) {
61 position.y += Chunk::height;
64 while (position.z >= Chunk::depth) {
65 position.z -= Chunk::depth;
68 while (position.z < 0) {
69 position.z += Chunk::depth;
74 void Entity::Move(const glm::vec3 &delta) noexcept {
75 Position(position + delta);
78 void Entity::AngularVelocity(const glm::quat &v) noexcept {
82 void Entity::Rotation(const glm::mat4 &rot) noexcept {
86 void Entity::Rotate(const glm::quat &delta) noexcept {
87 Rotation(rotation * glm::mat4_cast(delta));
90 glm::mat4 Entity::Transform(const Chunk::Pos &chunk_offset) const noexcept {
91 const glm::vec3 chunk_pos = (chunk - chunk_offset) * Chunk::Extent();
92 return glm::translate(position + chunk_pos) * rotation;
95 Ray Entity::Aim(const Chunk::Pos &chunk_offset) const noexcept {
96 glm::mat4 transform = Transform(chunk_offset);
97 glm::vec4 from = transform * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
99 glm::vec4 to = transform * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
101 return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
104 void Entity::Update(int dt) noexcept {
105 Move(velocity * float(dt));
106 Rotate(angular_velocity * float(dt));
110 void Entity::Draw() noexcept {