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