From 75a9c87b50cef2f1d749bd9042a9348bc28b4c09 Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 23 Oct 2022 02:48:51 +0200 Subject: Sun lighting --- src/GFX/Binder.cpp | 5 ++++- src/GFX/Mesh.cpp | 8 ++++++++ src/GFX/Mesh.hpp | 27 ++++++++++++++++++++++----- src/World/Chunk.cpp | 19 +++++++++++++++++-- src/World/Chunk.hpp | 1 + src/main.cpp | 5 +++++ 6 files changed, 57 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/GFX/Binder.cpp b/src/GFX/Binder.cpp index e7b7e4c..70d045c 100644 --- a/src/GFX/Binder.cpp +++ b/src/GFX/Binder.cpp @@ -10,7 +10,8 @@ BindableMesh Binder::load(Mesh& mesh) { store_indices(mesh.raw_indices(), mesh.indices_size()); } store_in_attribute_list(0, 3, mesh.raw(), mesh.size() * 3); - store_in_attribute_list(1, 2, mesh.raw_tex_coords(), mesh.tex_coords_size() * 2); + store_in_attribute_list(1, 3, mesh.raw_normals(), mesh.normals_size() * 3); + store_in_attribute_list(2, 2, mesh.raw_tex_coords(), mesh.tex_coords_size() * 2); unbind_vao(); return {vao, mesh.indices_size()}; @@ -50,12 +51,14 @@ void BindableMesh::bind() const { glBindVertexArray(m_vao); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); } void BindableMesh::unbind() { glBindVertexArray(0); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); + glDisableVertexAttribArray(2); } size_t BindableMesh::size() const { diff --git a/src/GFX/Mesh.cpp b/src/GFX/Mesh.cpp index 12f8aaa..1bac9f1 100644 --- a/src/GFX/Mesh.cpp +++ b/src/GFX/Mesh.cpp @@ -10,6 +10,14 @@ size_t Mesh::size() { return m_positions.size(); } +float* Mesh::raw_normals() { + return (float*) m_normals.data(); +} + +size_t Mesh::normals_size() { + return m_normals.size(); +} + uint32_t* Mesh::raw_indices() { return m_indices.data(); } diff --git a/src/GFX/Mesh.hpp b/src/GFX/Mesh.hpp index f027c8c..c20b419 100644 --- a/src/GFX/Mesh.hpp +++ b/src/GFX/Mesh.hpp @@ -9,15 +9,31 @@ namespace MC::GFX { class Mesh { public: - Mesh(std::vector> positions, std::vector> tex_coords, std::vector indices) - : m_positions(std::move(positions)), m_tex_coords(std::move(tex_coords)), m_indices(std::move(indices)) {}; - - Mesh(std::vector> positions, std::vector> tex_coords) - : m_positions(std::move(positions)), m_tex_coords(std::move(tex_coords)), m_indices() {}; + Mesh( + std::vector> positions, + std::vector> normals, + std::vector> tex_coords, + std::vector indices + ) : m_positions(std::move(positions)), + m_normals(std::move(normals)), + m_tex_coords(std::move(tex_coords)), + m_indices(std::move(indices)) {}; + + Mesh( + std::vector> positions, + std::vector> normals, + std::vector> tex_coords + ) : m_positions(std::move(positions)), + m_normals(std::move(normals)), + m_tex_coords(std::move(tex_coords)), + m_indices() {}; float* raw(); size_t size(); + float* raw_normals(); + size_t normals_size(); + uint32_t* raw_indices(); size_t indices_size(); @@ -26,6 +42,7 @@ public: private: std::vector> m_positions; + std::vector> m_normals; std::vector> m_tex_coords; std::vector m_indices; diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp index d02e5e5..ee408ae 100644 --- a/src/World/Chunk.cpp +++ b/src/World/Chunk.cpp @@ -9,6 +9,7 @@ void Chunk::set(uint32_t x, uint32_t y, uint32_t z, BlockType type) { GFX::Mesh Chunk::mesh() { std::vector> positions{}; + std::vector> normals{}; std::vector> tex_coords{}; std::vector indices{}; @@ -25,8 +26,9 @@ GFX::Mesh Chunk::mesh() { continue; } - auto side_tex_coords = Chunk::face_tex_coords(type, side); auto side_positions = side.face(); + auto side_normals = Chunk::face_normals(side); + auto side_tex_coords = Chunk::face_tex_coords(type, side); for (auto& position : side_positions) { position = position + Vector<3>{static_cast(x), static_cast(y), static_cast(z)}; @@ -35,6 +37,7 @@ GFX::Mesh Chunk::mesh() { uint32_t s = positions.size(); positions.insert(positions.end(), side_positions.begin(), side_positions.end()); + normals.insert(normals.end(), side_normals.begin(), side_normals.end()); tex_coords.insert(tex_coords.end(), side_tex_coords.begin(), side_tex_coords.end()); indices.insert(indices.end(), {s, s + 1, s + 3, s + 1, s + 2, s + 3}); } @@ -42,7 +45,7 @@ GFX::Mesh Chunk::mesh() { } } - return {positions, tex_coords, indices}; + return {positions, normals, tex_coords, indices}; } Vector<3> Chunk::position() { @@ -121,4 +124,16 @@ std::array, 4> Chunk::face_tex_coords(BlockType type, BlockSide side) } } +std::array, 4> Chunk::face_normals(BlockSide side) { + auto is_side = [=](BlockSide s) -> float { return s == side; }; + + Vector<3> normal = { + is_side(BlockSide::Right) - is_side(BlockSide::Left), + is_side(BlockSide::Top) - is_side(BlockSide::Bottom), + is_side(BlockSide::Front) - is_side(BlockSide::Back), + }; + + return {normal, normal, normal, normal}; +} + } diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp index 4c7810d..7415d7c 100644 --- a/src/World/Chunk.hpp +++ b/src/World/Chunk.hpp @@ -25,6 +25,7 @@ private: bool is_face_visible(uint32_t x, uint32_t y, uint32_t z, BlockSide side); static std::array, 4> face_tex_coords(BlockType type, BlockSide side); + static std::array, 4> face_normals(BlockSide side); struct BlockData { BlockType type; diff --git a/src/main.cpp b/src/main.cpp index 5d5af2f..40034e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -66,10 +66,15 @@ void run() { auto view_uniform = program.uniform("view_matrix"); auto projection_uniform = program.uniform("projection_matrix"); + auto sun_direction_uniform = program.uniform("sun_direction"); + program.bind(); auto projection = Math::MVP::projection(ASPECT, FOV, 0.1f, 1000.0f); projection_uniform.set(projection); + Vector<3> sun_direction{1.0f, -1.0f, 0.0f}; + sun_direction_uniform.set(sun_direction); + glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); -- cgit 1.4.1