]> git.localhorst.tv Git - blank.git/blob - src/model/Shape.hpp
textures
[blank.git] / src / model / Shape.hpp
1 #ifndef BLANK_MODEL_SHAPE_HPP_
2 #define BLANK_MODEL_SHAPE_HPP_
3
4 #include "BlockModel.hpp"
5 #include "EntityModel.hpp"
6 #include "OutlineModel.hpp"
7
8 #include <glm/glm.hpp>
9
10
11 namespace blank {
12
13 class AABB;
14 class Ray;
15
16 struct Shape {
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 EntityModel::Normal &VertexNormal(size_t idx) const noexcept { return vtx_nrm[idx]; }
24         EntityModel::Normal VertexNormal(
25                 size_t idx, const glm::mat4 &transform
26         ) const noexcept {
27                 return EntityModel::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                 EntityModel::Buffer &out,
34                 float tex_offset = 0.0f
35         ) const;
36         void Vertices(
37                 EntityModel::Buffer &out,
38                 const glm::mat4 &transform,
39                 float tex_offset = 0.0f,
40                 EntityModel::Index idx_offset = 0
41         ) const;
42         void Vertices(
43                 BlockModel::Buffer &out,
44                 const glm::mat4 &transform,
45                 float tex_offset = 0.0f,
46                 BlockModel::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 with
55         /// an optional offset
56         void Outline(
57                 OutlineModel::Buffer &out,
58                 const OutlineModel::Position &offset = { 0.0f, 0.0f, 0.0f },
59                 OutlineModel::Index idx_offset = 0
60         ) const;
61
62         /// Check if given ray would pass though this shape if it were
63         /// transformed with given matrix.
64         /// If true, dist and normal hold the intersection distance and
65         /// normal, otherwise their content is undefined.
66         virtual bool Intersects(
67                 const Ray &,
68                 const glm::mat4 &,
69                 float &dist,
70                 glm::vec3 &normal
71         ) const noexcept = 0;
72
73         /// Check for intersection with given OBB.
74         /// The OBB is defined by box and box_M, M is applied to the shape.
75         virtual bool Intersects(
76                 const glm::mat4 &M,
77                 const AABB &box,
78                 const glm::mat4 &box_M,
79                 float &depth,
80                 glm::vec3 &normal
81         ) const noexcept = 0;
82
83 protected:
84         void SetShape(
85                 const EntityModel::Positions &pos,
86                 const EntityModel::Normals &nrm,
87                 const EntityModel::Indices &idx);
88         void SetTexture(
89                 const BlockModel::TexCoords &tex_coords);
90         void SetOutline(
91                 const OutlineModel::Positions &pos,
92                 const OutlineModel::Indices &idx);
93
94 private:
95         EntityModel::Positions vtx_pos;
96         EntityModel::Normals vtx_nrm;
97         EntityModel::Indices vtx_idx;
98
99         BlockModel::TexCoords vtx_tex_coords;
100
101         OutlineModel::Positions out_pos;
102         OutlineModel::Indices out_idx;
103
104 };
105
106 }
107
108 #endif