]> git.localhorst.tv Git - blank.git/blob - src/model/CollisionBounds.hpp
renamed Shape -> CollisionBounds
[blank.git] / src / model / CollisionBounds.hpp
1 #ifndef BLANK_MODEL_COLLISIONBOUNDS_HPP_
2 #define BLANK_MODEL_COLLISIONBOUNDS_HPP_
3
4 #include "../graphics/BlockMesh.hpp"
5 #include "../graphics/EntityMesh.hpp"
6 #include "../graphics/OutlineMesh.hpp"
7
8 #include <glm/glm.hpp>
9
10
11 namespace blank {
12
13 class AABB;
14 class Ray;
15
16 struct CollisionBounds {
17
18         /// the number of vertices (and normals) this shape has
19         size_t VertexCount() const noexcept { return vtx_pos.size(); }
20         /// the number of vertex indices this shape has
21         size_t VertexIndexCount() const noexcept { return vtx_idx.size(); }
22
23         const EntityMesh::Normal &VertexNormal(size_t idx) const noexcept { return vtx_nrm[idx]; }
24         EntityMesh::Normal VertexNormal(
25                 size_t idx, const glm::mat4 &transform
26         ) const noexcept {
27                 return EntityMesh::Normal(transform * glm::vec4(vtx_nrm[idx], 0.0f));
28         }
29
30         /// fill given buffers with this shape's elements with an
31         /// optional transform and offset
32         void Vertices(
33                 EntityMesh::Buffer &out,
34                 float tex_offset = 0.0f
35         ) const;
36         void Vertices(
37                 EntityMesh::Buffer &out,
38                 const glm::mat4 &transform,
39                 float tex_offset = 0.0f,
40                 EntityMesh::Index idx_offset = 0
41         ) const;
42         void Vertices(
43                 BlockMesh::Buffer &out,
44                 const glm::mat4 &transform,
45                 float tex_offset = 0.0f,
46                 BlockMesh::Index idx_offset = 0
47         ) const;
48
49         /// the number of vertices this shape's outline has
50         size_t OutlineCount() const { return out_pos.size(); }
51         /// the number of vertex indices this shape's outline has
52         size_t OutlineIndexCount() const { return out_idx.size(); }
53
54         /// fill given buffers with this shape's outline's elements
55         void Outline(OutlineMesh::Buffer &out) const;
56
57         /// Check if given ray would pass though this shape if it were
58         /// transformed with given matrix.
59         /// If true, dist and normal hold the intersection distance and
60         /// normal, otherwise their content is undefined.
61         virtual bool Intersects(
62                 const Ray &,
63                 const glm::mat4 &,
64                 float &dist,
65                 glm::vec3 &normal
66         ) const noexcept = 0;
67
68         /// Check for intersection with given OBB.
69         /// The OBB is defined by box and box_M, M is applied to the shape.
70         virtual bool Intersects(
71                 const glm::mat4 &M,
72                 const AABB &box,
73                 const glm::mat4 &box_M,
74                 float &depth,
75                 glm::vec3 &normal
76         ) const noexcept = 0;
77
78 protected:
79         void SetShape(
80                 const EntityMesh::Positions &pos,
81                 const EntityMesh::Normals &nrm,
82                 const EntityMesh::Indices &idx);
83         void SetTexture(
84                 const BlockMesh::TexCoords &tex_coords);
85         void SetOutline(
86                 const OutlineMesh::Positions &pos,
87                 const OutlineMesh::Indices &idx);
88
89 private:
90         EntityMesh::Positions vtx_pos;
91         EntityMesh::Normals vtx_nrm;
92         EntityMesh::Indices vtx_idx;
93
94         BlockMesh::TexCoords vtx_tex_coords;
95
96         OutlineMesh::Positions out_pos;
97         OutlineMesh::Indices out_idx;
98
99 };
100
101 }
102
103 #endif