Botcraft 26.3
Loading...
Searching...
No Matches
WorldRenderer.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <glm/glm.hpp>
7
8#include <atomic>
9#include <memory>
10#include <mutex>
11#include <optional>
12#include <string>
13#include <unordered_map>
14#include <vector>
15
16namespace Botcraft
17{
18 class Biome;
19 class Blockstate;
20 class Chunk;
21 class Entity;
22
23 namespace Renderer
24 {
25 class Entity;
26 class BlockRenderable;
28 class TransparentChunk;
29 class Camera;
30 class Atlas;
31
32 // Intersection test for frustum culling
33 enum class FrustumResult
34 {
35 Outside = 0, // Box is outside the frustum
36 Intersect, // Box intersects with the frustum
37 Inside // Box is inside the frustum
38 };
39
40 // Each chunk faces are stored in rendering sections,
41 // They don't need to be the same size as real sections
42 static constexpr unsigned int RENDERING_SECTION_HEIGHT = 16;
43
45 {
46 public:
47 // Constructor
50
51 std::shared_ptr<Camera> GetCamera();
52
53 void InitGL();
54 void ReleaseGL();
55 void UpdateViewMatrix();
56 void SetCameraProjection(const glm::mat4& proj);
57 void UpdateFaces();
58 void UpdateChunk(const int x_, const int z_, const std::optional<Botcraft::Chunk>& chunk);
59 void UpdateEntity(const int id, const std::vector<Face>& faces);
60 void UseAtlasTextureGL();
61 void ClearFaces();
62
63 // Set camera position and orientation
64 void SetPosOrientation(const double x_, const double y_, const double z_, const float yaw_, const float pitch_);
65
66 // Render all the faces (chunks + partially transparent chunks + entities)
67 // Optional pointer can be passed to get statistics
68 void RenderFaces(int* num_chunks_ = nullptr, int* num_rendered_chunks_ = nullptr,
69 int* num_entities_ = nullptr, int* num_rendered_entities_ = nullptr,
70 int* num_faces_ = nullptr, int* num_rendered_faces_ = nullptr);
71
72 private:
73 // Returns the color modifier (for redstone/leaves/water etc...)
74 const std::vector<unsigned int> GetColorModifier(const int y, const Biome* biome, const Blockstate* blockstate, const std::vector<bool>& use_tintindex) const;
75
76 // Returns the distance from the center of the chunk to the camera
77 const float DistanceToCamera(const Position& chunk) const;
78
79
80 private:
81 unsigned int view_uniform_buffer;
82
83 std::unordered_map<Position, std::shared_ptr<Chunk> > chunks;
84 std::mutex chunks_mutex;
85 std::unordered_map<Position, std::shared_ptr<TransparentChunk> > transparent_chunks;
87 std::atomic<bool> chunks_dirty;
88 std::atomic<bool> transparent_chunks_dirty;
89
90 std::unordered_map<int, std::shared_ptr<Entity> > entities;
91 std::mutex entities_mutex;
93
94 std::shared_ptr<Camera> camera;
95
96 unsigned int atlas_texture;
97 };
98 } // Renderer
99} // Botcraft
std::shared_ptr< Camera > camera
void UpdateChunk(const int x_, const int z_, const std::optional< Botcraft::Chunk > &chunk)
const float DistanceToCamera(const Position &chunk) const
std::atomic< bool > transparent_chunks_dirty
void SetPosOrientation(const double x_, const double y_, const double z_, const float yaw_, const float pitch_)
std::shared_ptr< Camera > GetCamera()
std::unordered_map< int, std::shared_ptr< Entity > > entities
std::atomic< bool > entities_faces_should_be_updated
std::unordered_map< Position, std::shared_ptr< TransparentChunk > > transparent_chunks
const std::vector< unsigned int > GetColorModifier(const int y, const Biome *biome, const Blockstate *blockstate, const std::vector< bool > &use_tintindex) const
std::unordered_map< Position, std::shared_ptr< Chunk > > chunks
void UpdateEntity(const int id, const std::vector< Face > &faces)
void SetCameraProjection(const glm::mat4 &proj)
void RenderFaces(int *num_chunks_=nullptr, int *num_rendered_chunks_=nullptr, int *num_entities_=nullptr, int *num_rendered_entities_=nullptr, int *num_faces_=nullptr, int *num_rendered_faces_=nullptr)
static constexpr unsigned int RENDERING_SECTION_HEIGHT
BlockRenderable Chunk