summaryrefslogtreecommitdiff
path: root/src/World
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2024-02-12 12:55:11 +0100
committerMel <einebeere@gmail.com>2024-02-12 12:55:11 +0100
commitd2b5fc5b3bc648afffa42375706429685ac63794 (patch)
treea2dfbb241e1d46e5616c5884e5f3d685de2a2cb6 /src/World
parent588c7e87b7cab270698d43ca5c22d67793ae5fc4 (diff)
downloadmeowcraft-d2b5fc5b3bc648afffa42375706429685ac63794.tar.zst
meowcraft-d2b5fc5b3bc648afffa42375706429685ac63794.zip
Split rendering into own thread and sync through render action lists
Diffstat (limited to 'src/World')
-rw-r--r--src/World/Clouds.cpp113
-rw-r--r--src/World/Clouds.hpp22
2 files changed, 32 insertions, 103 deletions
diff --git a/src/World/Clouds.cpp b/src/World/Clouds.cpp
index b30c0ce..58b1919 100644
--- a/src/World/Clouds.cpp
+++ b/src/World/Clouds.cpp
@@ -5,65 +5,47 @@
#include "../Math/AABB.hpp"
#include "../GFX/Util/Primitives.hpp"
#include "../GFX/Util/MeshBuilder.hpp"
-#include <GL/glew.h>
#include <array>
namespace MC::World {
-Clouds::Clouds(Real ascept, Real fov, Real near, Real far, Vector<3, F32> sky_color, Vector<3, F32> sun_direction)
- : m_program(
- {GFX::Shading::Shader::Type::Vertex, vertex},
- {GFX::Shading::Shader::Type::Fragment, fragment}
- ),
- m_mesh(create_mesh(create_cloud_matrix())),
- m_model_uniform(), m_view_uniform(), m_projection_uniform() {
- m_program.bind();
-
- m_model_uniform = m_program.uniform("model_matrix");
- m_view_uniform = m_program.uniform("view_matrix");
- m_projection_uniform = m_program.uniform("projection_matrix");
- auto sky_color_uniform = m_program.uniform("sky_color");
- auto sun_direction_uniform = m_program.uniform("sun_direction");
-
- m_model_uniform.set(Math::MVP::model<F32>({}, {}, {}));
- m_view_uniform.set(Math::MVP::view<F32>({}, {}));
- m_projection_uniform.set(Math::MVP::perspective_projection<F32>(ascept, fov, near, far));
- sky_color_uniform.set(sky_color);
- sun_direction_uniform.set(sun_direction);
-
- m_program.unbind();
-}
-
-void Clouds::update(const Time& time) {
+void Clouds::update(Time const& time) {
m_x_offset += 5.0 * time.delta();
}
-void Clouds::render(const GFX::Camera& camera) {
- auto position = camera.position();
-
- I32 center_x = std::round((position.x() - m_x_offset) / TileSize);
- I32 center_y = std::round(position.z() / TileSize);
+void Clouds::render(GFX::Actions& actions, Position::World player_position) {
+ I32 center_x = std::round((player_position.x() - m_x_offset) / TileSize);
+ I32 center_y = std::round(player_position.z() / TileSize);
for (Int x = -1; x <= 1; x++) {
for (Int y = -1; y <= 1; y++) {
- render_single_instance(camera, center_x + x, center_y + y);
+ render_single_instance(actions, center_x + x, center_y + y);
}
}
}
-void Clouds::render_single_instance(const GFX::Camera& camera, Int x, Int y) {
+void Clouds::render_single_instance(GFX::Actions& actions, Int x, Int y) {
Vector<3> position{TileSize * x + m_x_offset, Height, TileSize * y};
- m_program.bind();
-
- m_view_uniform.set(Math::MVP::view<F32>(camera.position(), camera.angles()));
- m_model_uniform.set(Math::MVP::model<F32>(position, Vector<3>{Scale}, {}));
-
- m_mesh.bind();
- glDrawElements(GL_TRIANGLES, m_mesh.size(), GL_UNSIGNED_INT, nullptr);
- m_mesh.unbind();
-
- m_program.unbind();
+ // m_program.bind();
+ //
+ // m_view_uniform.set(Math::MVP::view<F32>(camera.position(), camera.angles()));
+ // m_model_uniform.set(Math::MVP::model<F32>(position, Vector<3>{Scale}, {}));
+ //
+ // m_mesh.bind();
+ // glDrawElements(GL_TRIANGLES, m_mesh.size(), GL_UNSIGNED_INT, nullptr);
+ // m_mesh.unbind();
+ //
+ // m_program.unbind();
+ actions.add({
+ .mesh = &m_mesh,
+ .program = GFX::Resources::Program::Clouds,
+ .transform = Transform(
+ position,
+ Rotation::zero(),
+ Vec3(Scale)
+ ),
+ });
}
Clouds::CloudMatrix Clouds::create_cloud_matrix() {
@@ -80,7 +62,7 @@ Clouds::CloudMatrix Clouds::create_cloud_matrix() {
return clouds;
}
-GFX::Mesh Clouds::create_mesh(const CloudMatrix& cloud_matrix) {
+GFX::Mesh Clouds::create_mesh(CloudMatrix const& cloud_matrix) {
GFX::Util::MeshBuilder<Vector<3, F32>> builder{};
for (Int x = 0; x < CloudMatrixSize; x++) {
@@ -112,47 +94,4 @@ GFX::Mesh Clouds::create_mesh(const CloudMatrix& cloud_matrix) {
return builder.mesh();
}
-const Char* Clouds::vertex = R"v(
-#version 330 core
-
-uniform mat4 model_matrix;
-uniform mat4 view_matrix;
-uniform mat4 projection_matrix;
-
-layout (location = 0) in vec3 position;
-layout (location = 1) in vec3 normal;
-
-out vec3 surface_normal;
-out float depth;
-
-void main() {
- vec4 world_position = model_matrix * vec4(position, 1.0);
- vec4 view_position = view_matrix * world_position;
- vec4 clip_position = projection_matrix * view_position;
-
- gl_Position = clip_position;
- surface_normal = (model_matrix * vec4(normal, 0.0)).xyz;
- depth = clamp((length(view_position) - 200) / 400, 0.0, 1.0);
-}
-)v";
-
-const Char* Clouds::fragment = R"f(
-#version 330 core
-
-uniform vec3 sky_color;
-uniform vec3 sun_direction;
-
-in vec3 surface_normal;
-in float depth;
-out vec4 color;
-
-void main() {
- float brightness = dot(normalize(surface_normal), normalize(-sun_direction));
- vec3 diffuse = vec3(1 - clamp(brightness, -0.3, 0.2));
- vec3 base = vec3(1.0, 1.0, 1.0) * diffuse;
-
- color = vec4(mix(sky_color, base, 1 - depth), 0.3);
-}
-)f";
-
}
diff --git a/src/World/Clouds.hpp b/src/World/Clouds.hpp
index 4bd0637..7246e7b 100644
--- a/src/World/Clouds.hpp
+++ b/src/World/Clouds.hpp
@@ -1,8 +1,7 @@
#pragma once
#include "../Time.hpp"
-#include "../GFX/Shading/Program.hpp"
-#include "../GFX/Shading/Uniform.hpp"
+#include "../GFX/Actions.hpp"
#include "../GFX/Camera.hpp"
#include "../GFX/Mesh.hpp"
@@ -10,10 +9,10 @@ namespace MC::World {
class Clouds {
public:
- Clouds(Real ascept, Real fov, Real near, Real far, Vector<3, F32> sky_color, Vector<3, F32> sun_direction);
+ Clouds() : m_mesh(create_mesh(create_cloud_matrix())) {}
- void update(const Time& time);
- void render(const GFX::Camera& camera);
+ void update(Time const& time);
+ void render(GFX::Actions& actions, Position::World player_position);
private:
constexpr static U32 CloudMatrixSize = 128;
constexpr static Int Height = 200;
@@ -22,22 +21,13 @@ private:
using CloudMatrix = Matrix<CloudMatrixSize, CloudMatrixSize, Bool>;
- void render_single_instance(const GFX::Camera& camera, Int x, Int y);
+ void render_single_instance(GFX::Actions& actions, Int x, Int y);
static CloudMatrix create_cloud_matrix();
- static GFX::Mesh create_mesh(const CloudMatrix& cloud_matrix);
-
- static const Char* vertex;
- static const Char* fragment;
+ static GFX::Mesh create_mesh(CloudMatrix const& cloud_matrix);
Real m_x_offset = 0.0;
-
- GFX::Shading::Program m_program;
GFX::Mesh m_mesh;
-
- GFX::Shading::Uniform m_model_uniform;
- GFX::Shading::Uniform m_view_uniform;
- GFX::Shading::Uniform m_projection_uniform;
};
}