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