Botcraft 1.21.4
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 <memory>
9#include <mutex>
10#include <optional>
11#include <string>
12#include <unordered_map>
13#include <vector>
14
15namespace Botcraft
16{
17 class Biome;
18 class Blockstate;
19 class Chunk;
20 class Entity;
21
22 namespace Renderer
23 {
24 class Chunk;
25 class Entity;
26 class TransparentChunk;
27 class Camera;
28 class Atlas;
29
30 // Intersection test for frustum culling
31 enum class FrustumResult
32 {
33 Outside = 0, // Box is outside the frustum
34 Intersect, // Box intersects with the frustum
35 Inside // Box is inside the frustum
36 };
37
39 {
40 public:
41 // Constructor
42 WorldRenderer(const unsigned int section_height_);
44
45 std::shared_ptr<Camera> GetCamera();
46
47 void InitGL();
48 void UpdateViewMatrix();
49 void SetCameraProjection(const glm::mat4& proj);
50 void UpdateFaces();
51 void UpdateChunk(const int x_, const int z_, const std::optional<Botcraft::Chunk>& chunk);
52 void UpdateEntity(const int id, const std::vector<Face>& faces);
53 void UseAtlasTextureGL();
54 void ClearFaces();
55
56 // Set camera position and orientation
57 void SetPosOrientation(const double x_, const double y_, const double z_, const float yaw_, const float pitch_);
58
59 // Render all the faces (chunks + partially transparent chunks + entities)
60 // Optional pointer can be passed to get statistics
61 void RenderFaces(int* num_chunks_ = nullptr, int* num_rendered_chunks_ = nullptr,
62 int* num_entities_ = nullptr, int* num_rendered_entities_ = nullptr,
63 int* num_faces_ = nullptr, int* num_rendered_faces_ = nullptr);
64
65 private:
66 // Add a face to the rendering data
67 // It will not be rendered until the next frame with
68 // blocks_faces_should_be_updated set to true
69 void AddFace(const Position& block_pos, const Vector3<double>& offset, const Face& face_,
70 const std::vector<std::string>& texture_identifiers_,
71 const std::vector<unsigned int>& texture_multipliers_);
72
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 // Each chunk faces are stored in rendering sections,
84 // They don't need to be the same size as real
85 // sections
86 unsigned int section_height;
87
88 std::unordered_map<Position, std::shared_ptr<Chunk> > chunks;
89 std::mutex chunks_mutex;
90 std::unordered_map<Position, std::shared_ptr<TransparentChunk> > transparent_chunks;
93
94 std::unordered_map<int, std::shared_ptr<Entity> > entities;
95 std::mutex entities_mutex;
97
98 std::shared_ptr<Camera> camera;
99 std::mutex m_mutex_camera;
100
101 unsigned int atlas_texture;
102 };
103 } // Renderer
104} // Botcraft
std::shared_ptr< Camera > camera
void AddFace(const Position &block_pos, const Vector3< double > &offset, const Face &face_, const std::vector< std::string > &texture_identifiers_, const std::vector< unsigned int > &texture_multipliers_)
void UpdateChunk(const int x_, const int z_, const std::optional< Botcraft::Chunk > &chunk)
const float DistanceToCamera(const Position &chunk) const
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::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)