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
6
#include "
botcraft/Renderer/TransparentChunk.hpp
"
7
8
#include <algorithm>
9
10
namespace
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
28
TransparentChunk::TransparentChunk
()
29
{
30
needs_sort
=
false
;
31
}
32
33
TransparentChunk::~TransparentChunk
()
34
{
35
36
}
37
38
void
TransparentChunk::MarkForSort
()
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
70
void
TransparentChunk::OnUploadedLocked
()
71
{
72
display_faces_positions
=
faces
;
73
needs_sort
=
true
;
74
}
75
}
// Renderer
76
}
// Botcraft
TransparentChunk.hpp
Botcraft::Renderer::BlockRenderable::faces
std::vector< Face > faces
Definition
BlockRenderable.hpp:40
Botcraft::Renderer::BlockRenderable::mutex_faces
std::mutex mutex_faces
Definition
BlockRenderable.hpp:43
Botcraft::Renderer::BlockRenderable::data_VBO
unsigned int data_VBO
Definition
BlockRenderable.hpp:37
Botcraft::Renderer::Face
Definition
Face.hpp:18
Botcraft::Renderer::TransparentChunk::Sort
void Sort(const glm::vec3 &cam_pos)
Definition
TransparentChunk.cpp:43
Botcraft::Renderer::TransparentChunk::needs_sort
std::atomic< bool > needs_sort
Definition
TransparentChunk.hpp:28
Botcraft::Renderer::TransparentChunk::~TransparentChunk
~TransparentChunk()
Definition
TransparentChunk.cpp:33
Botcraft::Renderer::TransparentChunk::OnUploadedLocked
void OnUploadedLocked() override
Definition
TransparentChunk.cpp:70
Botcraft::Renderer::TransparentChunk::TransparentChunk
TransparentChunk()
Definition
TransparentChunk.cpp:28
Botcraft::Renderer::TransparentChunk::display_faces_positions
std::vector< Face > display_faces_positions
Definition
TransparentChunk.hpp:27
Botcraft::Renderer::TransparentChunk::MarkForSort
void MarkForSort()
Definition
TransparentChunk.cpp:38
Botcraft
Definition
BaseNode.hpp:6
botcraft
src
Renderer
TransparentChunk.cpp
Generated by
1.9.8