add actual lights

This commit is contained in:
MihailRis 2024-06-20 21:18:40 +03:00
parent 3d2deaf369
commit 3235740333
3 changed files with 33 additions and 20 deletions

View File

@ -3,6 +3,8 @@
#include "../core/Mesh.hpp" #include "../core/Mesh.hpp"
#include "../core/Texture.hpp" #include "../core/Texture.hpp"
#include "../../window/Window.hpp" #include "../../window/Window.hpp"
#include "../../voxels/Chunks.hpp"
#include "../../lighting/Lightmap.hpp"
#define GLM_ENABLE_EXPERIMENTAL #define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_transform.hpp> #include <glm/ext/matrix_transform.hpp>
@ -20,12 +22,13 @@ inline constexpr glm::vec3 X(1, 0, 0);
inline constexpr glm::vec3 Y(0, 1, 0); inline constexpr glm::vec3 Y(0, 1, 0);
inline constexpr glm::vec3 Z(0, 0, 1); inline constexpr glm::vec3 Z(0, 0, 1);
ModelBatch::ModelBatch(size_t capacity) ModelBatch::ModelBatch(size_t capacity, Chunks* chunks)
: buffer(std::make_unique<float[]>(capacity * VERTEX_SIZE)), : buffer(std::make_unique<float[]>(capacity * VERTEX_SIZE)),
capacity(capacity), capacity(capacity),
index(0), index(0),
mesh(std::make_unique<Mesh>(buffer.get(), 0, attrs)), mesh(std::make_unique<Mesh>(buffer.get(), 0, attrs)),
combined(1.0f) combined(1.0f),
chunks(chunks)
{ {
ubyte pixels[] = { ubyte pixels[] = {
255, 255, 255, 255, 255, 255, 255, 255,
@ -52,14 +55,23 @@ void ModelBatch::test(glm::vec3 pos, glm::vec3 size) {
} }
void ModelBatch::box(glm::vec3 pos, glm::vec3 size) { void ModelBatch::box(glm::vec3 pos, glm::vec3 size) {
plane(pos+Z, X*size, Y*size, Z); glm::vec3 gpos = combined * glm::vec4(pos, 1.0f);
plane(pos-Z, -X*size, Y*size, -Z); light_t light = chunks->getLight(gpos.x, gpos.y, gpos.z);
glm::vec4 lights (
Lightmap::extract(light, 0) / 15.0f,
Lightmap::extract(light, 1) / 15.0f,
Lightmap::extract(light, 2) / 15.0f,
Lightmap::extract(light, 3) / 15.0f
);
plane(pos+Y, X*size, -Z*size, Y); plane(pos+Z, X*size, Y*size, Z, lights);
plane(pos-Y, X*size, Z*size, -Y); plane(pos-Z, -X*size, Y*size, -Z, lights);
plane(pos+X, -Z*size, Y*size, X); plane(pos+Y, X*size, -Z*size, Y, lights);
plane(pos-X, Z*size, Y*size, -X); plane(pos-Y, X*size, Z*size, -Y, lights);
plane(pos+X, -Z*size, Y*size, X, lights);
plane(pos-X, Z*size, Y*size, -X, lights);
} }
void ModelBatch::flush() { void ModelBatch::flush() {

View File

@ -7,6 +7,7 @@
class Mesh; class Mesh;
class Texture; class Texture;
class Chunks;
struct DecomposedMat4 { struct DecomposedMat4 {
glm::vec3 scale; glm::vec3 scale;
@ -29,6 +30,8 @@ class ModelBatch {
DecomposedMat4 decomposed {}; DecomposedMat4 decomposed {};
Chunks* chunks;
static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f}; static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f};
inline void vertex( inline void vertex(
@ -55,15 +58,13 @@ class ModelBatch {
buffer[index++] = compressed.floating; buffer[index++] = compressed.floating;
} }
inline void plane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm) { inline void plane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm, glm::vec4 light) {
norm = decomposed.rotation * norm; norm = decomposed.rotation * norm;
float d = glm::dot(norm, SUN_VECTOR); float d = glm::dot(norm, SUN_VECTOR);
d = 0.8f + d * 0.2f; d = 0.8f + d * 0.2f;
glm::vec4 color {d, d, d, 0.0f}; glm::vec4 color {d, d, d, 1.0f};
color.r = glm::max(0.0f, color.r); color *= light;
color.g = glm::max(0.0f, color.g);
color.b = glm::max(0.0f, color.b);
vertex(pos-right-up, {0,0}, color); vertex(pos-right-up, {0,0}, color);
vertex(pos+right-up, {1,0}, color); vertex(pos+right-up, {1,0}, color);
@ -74,7 +75,7 @@ class ModelBatch {
vertex(pos-right+up, {0,1}, color); vertex(pos-right+up, {0,1}, color);
} }
public: public:
ModelBatch(size_t capacity); ModelBatch(size_t capacity, Chunks* chunks);
~ModelBatch(); ~ModelBatch();
void pushMatrix(glm::mat4 matrix); void pushMatrix(glm::mat4 matrix);

View File

@ -49,7 +49,7 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* pl
player(player), player(player),
frustumCulling(std::make_unique<Frustum>()), frustumCulling(std::make_unique<Frustum>()),
lineBatch(std::make_unique<LineBatch>()), lineBatch(std::make_unique<LineBatch>()),
modelBatch(std::make_unique<ModelBatch>(1000)) modelBatch(std::make_unique<ModelBatch>(1000, level->chunks.get()))
{ {
renderer = std::make_unique<ChunksRenderer>( renderer = std::make_unique<ChunksRenderer>(
level, level,