add actual lights
This commit is contained in:
parent
3d2deaf369
commit
3235740333
@ -3,6 +3,8 @@
|
||||
#include "../core/Mesh.hpp"
|
||||
#include "../core/Texture.hpp"
|
||||
#include "../../window/Window.hpp"
|
||||
#include "../../voxels/Chunks.hpp"
|
||||
#include "../../lighting/Lightmap.hpp"
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#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 Z(0, 0, 1);
|
||||
|
||||
ModelBatch::ModelBatch(size_t capacity)
|
||||
ModelBatch::ModelBatch(size_t capacity, Chunks* chunks)
|
||||
: buffer(std::make_unique<float[]>(capacity * VERTEX_SIZE)),
|
||||
capacity(capacity),
|
||||
index(0),
|
||||
mesh(std::make_unique<Mesh>(buffer.get(), 0, attrs)),
|
||||
combined(1.0f)
|
||||
combined(1.0f),
|
||||
chunks(chunks)
|
||||
{
|
||||
ubyte pixels[] = {
|
||||
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) {
|
||||
plane(pos+Z, X*size, Y*size, Z);
|
||||
plane(pos-Z, -X*size, Y*size, -Z);
|
||||
glm::vec3 gpos = combined * glm::vec4(pos, 1.0f);
|
||||
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-Y, X*size, Z*size, -Y);
|
||||
plane(pos+Z, X*size, Y*size, Z, lights);
|
||||
plane(pos-Z, -X*size, Y*size, -Z, lights);
|
||||
|
||||
plane(pos+X, -Z*size, Y*size, X);
|
||||
plane(pos-X, Z*size, Y*size, -X);
|
||||
plane(pos+Y, X*size, -Z*size, Y, lights);
|
||||
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() {
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
|
||||
class Mesh;
|
||||
class Texture;
|
||||
class Chunks;
|
||||
|
||||
struct DecomposedMat4 {
|
||||
glm::vec3 scale;
|
||||
@ -29,6 +30,8 @@ class ModelBatch {
|
||||
|
||||
DecomposedMat4 decomposed {};
|
||||
|
||||
Chunks* chunks;
|
||||
|
||||
static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f};
|
||||
|
||||
inline void vertex(
|
||||
@ -55,15 +58,13 @@ class ModelBatch {
|
||||
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;
|
||||
float d = glm::dot(norm, SUN_VECTOR);
|
||||
d = 0.8f + d * 0.2f;
|
||||
|
||||
glm::vec4 color {d, d, d, 0.0f};
|
||||
color.r = glm::max(0.0f, color.r);
|
||||
color.g = glm::max(0.0f, color.g);
|
||||
color.b = glm::max(0.0f, color.b);
|
||||
glm::vec4 color {d, d, d, 1.0f};
|
||||
color *= light;
|
||||
|
||||
vertex(pos-right-up, {0,0}, color);
|
||||
vertex(pos+right-up, {1,0}, color);
|
||||
@ -74,7 +75,7 @@ class ModelBatch {
|
||||
vertex(pos-right+up, {0,1}, color);
|
||||
}
|
||||
public:
|
||||
ModelBatch(size_t capacity);
|
||||
ModelBatch(size_t capacity, Chunks* chunks);
|
||||
~ModelBatch();
|
||||
|
||||
void pushMatrix(glm::mat4 matrix);
|
||||
|
||||
@ -44,12 +44,12 @@
|
||||
bool WorldRenderer::showChunkBorders = false;
|
||||
|
||||
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player)
|
||||
: engine(engine),
|
||||
level(frontend->getLevel()),
|
||||
player(player),
|
||||
frustumCulling(std::make_unique<Frustum>()),
|
||||
lineBatch(std::make_unique<LineBatch>()),
|
||||
modelBatch(std::make_unique<ModelBatch>(1000))
|
||||
: engine(engine),
|
||||
level(frontend->getLevel()),
|
||||
player(player),
|
||||
frustumCulling(std::make_unique<Frustum>()),
|
||||
lineBatch(std::make_unique<LineBatch>()),
|
||||
modelBatch(std::make_unique<ModelBatch>(1000, level->chunks.get()))
|
||||
{
|
||||
renderer = std::make_unique<ChunksRenderer>(
|
||||
level,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user