Botcraft 26.3
Loading...
Searching...
No Matches
TransparentChunk.cpp
Go to the documentation of this file.
1#include <glm/vec4.hpp>
2#include <glm/gtc/type_ptr.hpp>
3
4#include <glad/glad.h>
5
7
8#include <algorithm>
9
10namespace Botcraft
11{
12 namespace Renderer
13 {
14 namespace
15 {
16 float Distance(const Face& f, const glm::vec3& pos)
17 {
18 const glm::vec3 center = glm::make_mat4(f.GetMatrix().data()) * glm::vec4(0.0f, -1.0f, 0.0f, 1.0f);
19
20 const float delta_x = pos.x - center.x;
21 const float delta_y = pos.y - center.y;
22 const float delta_z = pos.z - center.z;
23
24 return delta_x * delta_x + delta_y * delta_y + delta_z * delta_z;
25 }
26 }
27
32
37
39 {
40 needs_sort = true;
41 }
42
43 void TransparentChunk::Sort(const glm::vec3& cam_pos)
44 {
45 if (!needs_sort.load())
46 {
47 return;
48 }
49
50 std::lock_guard<std::mutex> lock_faces(mutex_faces);
51 // Re-check under the lock. If MarkForSort/OnUploaded set the flag
52 // while we waited, exchange returns true and we sort now.
53 if (!needs_sort.exchange(false))
54 {
55 return;
56 }
57
58 if (display_faces_positions.empty())
59 {
60 return;
61 }
62
63 std::sort(display_faces_positions.begin(), display_faces_positions.end(), [this, cam_pos](const Face& f1, const Face& f2) { return Distance(f1, cam_pos) > Distance(f2, cam_pos); });
64
65 glBindBuffer(GL_ARRAY_BUFFER, data_VBO);
66 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Face) * display_faces_positions.size(), display_faces_positions.data());
67 glBindBuffer(GL_ARRAY_BUFFER, 0);
68 }
69
75 } // Renderer
76} // Botcraft
void Sort(const glm::vec3 &cam_pos)