]> git.localhorst.tv Git - tacos.git/blob - src/world/Floor.hpp
mouse cursor mockup
[tacos.git] / src / world / Floor.hpp
1 #ifndef TACOS_WORLD_FLOOR_HPP_
2 #define TACOS_WORLD_FLOOR_HPP_
3
4 #include "../physics/ray.hpp"
5
6 #include <vector>
7 #include <GL/glew.h>
8 #include <glm/glm.hpp>
9
10
11 namespace tacos {
12
13 class Floor {
14
15 public:
16         /// width and depth in vertices, so will have one less
17         /// in each dimension in tiles
18         Floor(int width, int depth);
19         ~Floor() noexcept;
20
21         Floor(const Floor &) = delete;
22         Floor &operator =(const Floor &) = delete;
23
24         static constexpr int VAO_DIVISOR = 32;
25
26         int Width() const noexcept { return width; }
27         int Depth() const noexcept { return depth; }
28
29         int VAOWidth() const noexcept { return vao_width; }
30         int VAODepth() const noexcept { return vao_depth; }
31
32         glm::vec3 VAOPosition(int vao_x, int vao_z) const noexcept {
33                 return glm::vec3(float(vao_x * VAO_DIVISOR), 0.0f, float(vao_z * VAO_DIVISOR));
34         }
35         void DrawVAO(int vao_x, int vao_z) const noexcept;
36
37         void SetElevation(int x, int z, float e) noexcept {
38                 elevation[z * width + x] = e;
39         }
40         float GetElevation(int x, int z) const noexcept {
41                 return elevation[z * width + x];
42         }
43         float ClampedElevation(int x, int z) const noexcept {
44                 return GetElevation(glm::clamp(x, 0, width), glm::clamp(z, 0, depth));
45         }
46         glm::vec3 GetNormal(int x, int z) const noexcept;
47
48         void GenerateVertices();
49
50         /// check if ray intersects floor, write point of intersection to point
51         bool Intersection(const Ray &ray, glm::vec3 &point);
52
53 private:
54         void SetupVAO(int which, GLuint element_buffer, int vertex_count) noexcept;
55         void FillElementBuffer(GLuint which, int tile_width, int tile_depth);
56         void FillAttribBuffer(int vao_x, int vao_z);
57         glm::ivec2 Tiles(int vao_x, int vao_z) const noexcept;
58         int NumTiles(int vao_x, int vao_z) const noexcept;
59         int NumVertices(int vao_x, int vao_z) const noexcept;
60
61 private:
62         int width;
63         int depth;
64         std::vector<float> elevation;
65
66         int tile_width;
67         int tile_depth;
68         int unclean_width;
69         int unclean_depth;
70         int vao_width;
71         int vao_depth;
72         std::vector<GLuint> vaos;
73         // index of general element buffer
74         int general_elements;
75         // index of unclean width element buffer, if any
76         int unclean_width_elements;
77         // index of unclean depth element buffer, if any
78         int unclean_depth_elements;
79         // index of unclean corner element buffer, if any
80         int unclean_corner_elements;
81         std::vector<GLuint> buffers;
82
83         struct Attributes {
84                 glm::vec3 position;
85                 glm::vec3 normal;
86         };
87
88 };
89
90 }
91
92 #endif