]> git.localhorst.tv Git - blank.git/blobdiff - src/shape.hpp
minor optimizations in chunk
[blank.git] / src / shape.hpp
index 66827d08b27c1c1ed5064be731b1f7dca54adc90..4120f76b938ab0ef5a01431aa6ce08633fabd7f7 100644 (file)
@@ -2,6 +2,7 @@
 #define BLANK_SHAPE_HPP_
 
 #include "geometry.hpp"
+#include "model.hpp"
 
 #include <vector>
 #include <glm/glm.hpp>
@@ -11,14 +12,82 @@ namespace blank {
 
 struct Shape {
 
-       virtual size_t VertexCount() const = 0;
-       virtual void Vertices(std::vector<glm::vec3> &, const glm::vec3 &pos = { 0.0f, 0.0f, 0.0f }) const = 0;
-       virtual void Normals(std::vector<glm::vec3> &) const = 0;
+       /// the number of vertices (and normals) this shape has
+       size_t VertexCount() const noexcept { return vtx_pos.size(); }
+       /// the number of vertex indices this shape has
+       size_t VertexIndexCount() const noexcept { return vtx_idx.size(); }
+
+       const Model::Normal &VertexNormal(size_t idx) const noexcept { return vtx_nrm[idx]; }
+       Model::Normal VertexNormal(
+               size_t idx, const glm::mat4 &transform
+       ) const noexcept {
+               return Model::Normal(transform * glm::vec4(vtx_nrm[idx], 0.0f));
+       }
+
+       /// fill given buffers with this shape's elements with an
+       /// optional transform and offset
+       void Vertices(
+               Model::Positions &vertex,
+               Model::Normals &normal,
+               Model::Indices &index
+       ) const;
+       void Vertices(
+               Model::Positions &vertex,
+               Model::Normals &normal,
+               Model::Indices &index,
+               const glm::mat4 &transform,
+               Model::Index idx_offset = 0
+       ) const;
+       void Vertices(
+               BlockModel::Positions &vertex,
+               BlockModel::Indices &index,
+               const glm::mat4 &transform,
+               BlockModel::Index idx_offset = 0
+       ) const;
+
+       /// the number of vertices this shape's outline has
+       size_t OutlineCount() const { return out_pos.size(); }
+       /// the number of vertex indices this shape's outline has
+       size_t OutlineIndexCount() const { return out_idx.size(); }
+
+       /// fill given buffers with this shape's outline's elements with
+       /// an optional offset
+       void Outline(
+               OutlineModel::Positions &vertex,
+               OutlineModel::Indices &index,
+               const OutlineModel::Position &offset = { 0.0f, 0.0f, 0.0f },
+               OutlineModel::Index idx_offset = 0
+       ) const;
+
+       /// Check if given ray would pass though this shape if it were
+       /// transformed with given matrix.
+       /// If true, dist and normal hold the intersection distance and
+       /// normal, otherwise their content is undefined.
+       virtual bool Intersects(
+               const Ray &,
+               const glm::mat4 &,
+               float &dist,
+               glm::vec3 &normal
+       ) const noexcept = 0;
+
+protected:
+       void SetShape(const Model::Positions &pos, const Model::Normals &nrm, const Model::Indices &idx) {
+               vtx_pos = pos;
+               vtx_nrm = nrm;
+               vtx_idx = idx;
+       }
+       void SetOutline(const OutlineModel::Positions &pos, const OutlineModel::Indices &idx) {
+               out_pos = pos;
+               out_idx = idx;
+       }
 
-       virtual size_t OutlineCount() const = 0;
-       virtual void Outline(std::vector<glm::vec3> &, const glm::vec3 &pos = { 0.0f, 0.0f, 0.0f }) const = 0;
+private:
+       Model::Positions vtx_pos;
+       Model::Normals vtx_nrm;
+       Model::Indices vtx_idx;
 
-       virtual bool Intersects(const Ray &, const glm::mat4 &, float &dist, glm::vec3 &normal) const = 0;
+       OutlineModel::Positions out_pos;
+       OutlineModel::Indices out_idx;
 
 };
 
@@ -27,14 +96,9 @@ class NullShape
 : public Shape {
 
 public:
-       size_t VertexCount() const override;
-       void Vertices(std::vector<glm::vec3> &, const glm::vec3 &) const override;
-       void Normals(std::vector<glm::vec3> &) const override;
-
-       size_t OutlineCount() const override;
-       void Outline(std::vector<glm::vec3> &, const glm::vec3 &) const override;
+       NullShape();
 
-       bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const override;
+       bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override;
 
 };
 
@@ -45,14 +109,7 @@ class CuboidShape
 public:
        CuboidShape(const AABB &bounds);
 
-       size_t VertexCount() const override;
-       void Vertices(std::vector<glm::vec3> &, const glm::vec3 &) const override;
-       void Normals(std::vector<glm::vec3> &) const override;
-
-       size_t OutlineCount() const override;
-       void Outline(std::vector<glm::vec3> &, const glm::vec3 &) const override;
-
-       bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const override;
+       bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override;
 
 private:
        AABB bb;
@@ -66,14 +123,7 @@ class StairShape
 public:
        StairShape(const AABB &bounds, const glm::vec2 &clip);
 
-       size_t VertexCount() const override;
-       void Vertices(std::vector<glm::vec3> &, const glm::vec3 &) const override;
-       void Normals(std::vector<glm::vec3> &) const override;
-
-       size_t OutlineCount() const override;
-       void Outline(std::vector<glm::vec3> &, const glm::vec3 &) const override;
-
-       bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const override;
+       bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override;
 
 private:
        AABB top, bot;