]> git.localhorst.tv Git - blobs.git/blob - src/world/Planet.hpp
simple planet render
[blobs.git] / src / world / Planet.hpp
1 #ifndef BLOBS_WORLD_PLANET_HPP_
2 #define BLOBS_WORLD_PLANET_HPP_
3
4 #include "Body.hpp"
5
6 #include "Tile.hpp"
7 #include "../graphics/glm.hpp"
8 #include "../graphics/SimpleVAO.hpp"
9
10 #include <cassert>
11 #include <memory>
12 #include <GL/glew.h>
13
14
15 namespace blobs {
16 namespace world {
17
18 struct Tile;
19
20 /// A planet has six surfaces, numbered 0 to 5, each filled with
21 /// sidelength² tiles.
22 class Planet
23 : public Body {
24
25 public:
26         explicit Planet(int sidelength);
27         ~Planet();
28
29         Planet(const Planet &) = delete;
30         Planet &operator =(const Planet &) = delete;
31
32         Planet(Planet &&) = delete;
33         Planet &operator =(Planet &&) = delete;
34
35 public:
36         /// Get the tile at given surface and coordinates.
37         Tile &TileAt(int surface, int x, int y) {
38                 return tiles[IndexOf(surface, x, y)];
39         }
40         const Tile &TileAt(int surface, int x, int y) const {
41                 return tiles[IndexOf(surface, x, y)];
42         }
43
44         /// Convert coordinates into a tile index.
45         int IndexOf(int surface, int x, int y) const {
46                 assert(0 <= surface && surface <= 5);
47                 assert(0 <= x && x <= sidelength);
48                 assert(0 <= y && y <= sidelength);
49                 return surface * TilesPerSurface() + y * SideLength() + x;
50         }
51         /// The length of the side of each surface.
52         int SideLength() const {
53                 return sidelength;
54         }
55         /// The number of tiles of one surface.
56         int TilesPerSurface() const {
57                 return SideLength() * SideLength();
58         }
59         /// Total number of tiles of all surfaces combined.
60         int TilesTotal() const {
61                 return 6 * TilesPerSurface();
62         }
63
64         void BuildVAOs();
65         void Draw(app::Assets &, graphics::Viewport &) override;
66
67 private:
68         int sidelength;
69         std::unique_ptr<Tile []> tiles;
70
71         struct Attributes {
72                 glm::vec3 position;
73                 glm::vec3 tex_coord;
74         };
75         graphics::SimpleVAO<Attributes, unsigned int> vao;
76
77 };
78
79 void GenerateTest(Planet &);
80
81 }
82 }
83
84 #endif