Skip to content

Commit

Permalink
add ModelBatch pseudo-lights
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Jun 20, 2024
1 parent 6d6313c commit 3d2deaf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/graphics/render/ModelBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../core/Texture.hpp"
#include "../../window/Window.hpp"

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtx/quaternion.hpp>
Expand All @@ -15,7 +16,6 @@ static const vattr attrs[] = {
{3}, {2}, {1}, {0}
};

static glm::vec3 SUN_VECTOR (0.411934f, 0.863868f, -0.279161f);
inline constexpr glm::vec3 X(1, 0, 0);
inline constexpr glm::vec3 Y(0, 1, 0);
inline constexpr glm::vec3 Z(0, 0, 1);
Expand All @@ -39,12 +39,16 @@ ModelBatch::~ModelBatch() {
void ModelBatch::test(glm::vec3 pos, glm::vec3 size) {
float time = static_cast<float>(Window::time());
pushMatrix(glm::translate(glm::mat4(1.0f), pos));
pushMatrix(glm::rotate(glm::mat4(1.0f), time, glm::vec3(0,1,0)));
pushMatrix(glm::rotate(glm::mat4(1.0f), glm::sin(time*7*0.1f), glm::vec3(0,1,0)));
pushMatrix(glm::rotate(glm::mat4(1.0f), glm::sin(time*11*0.1f), glm::vec3(1,0,0)));
pushMatrix(glm::rotate(glm::mat4(1.0f), glm::sin(time*17*0.1f), glm::vec3(0,0,1)));
pushMatrix(glm::translate(glm::mat4(1.0f), glm::vec3(0, glm::sin(time*2), 0)));
box({}, size);
popMatrix();
popMatrix();
popMatrix();
popMatrix();
popMatrix();
}

void ModelBatch::box(glm::vec3 pos, glm::vec3 size) {
Expand All @@ -62,15 +66,15 @@ void ModelBatch::flush() {
if (index == 0) {
return;
}
blank->bind();
// blank->bind();
mesh->reload(buffer.get(), index / VERTEX_SIZE);
mesh->draw();
index = 0;
}

void ModelBatch::pushMatrix(glm::mat4 matrix) {
matrices.push_back(combined);
combined = matrix * combined;
combined = combined * matrix;

decomposed = {};
glm::quat rotation;
Expand Down
9 changes: 7 additions & 2 deletions src/graphics/render/ModelBatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct DecomposedMat4 {
glm::vec3 skew;
glm::vec4 perspective;
};
#include <iostream>

class ModelBatch {
std::unique_ptr<float[]> buffer;
size_t capacity;
Expand All @@ -29,6 +29,8 @@ class ModelBatch {

DecomposedMat4 decomposed {};

static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f};

inline void vertex(
glm::vec3 pos, glm::vec2 uv, glm::vec4 color
) {
Expand All @@ -55,7 +57,10 @@ class ModelBatch {

inline void plane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm) {
norm = decomposed.rotation * norm;
glm::vec4 color {norm.x, norm.y, norm.z, 0.0f};
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);
Expand Down
1 change: 1 addition & 0 deletions src/graphics/render/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ void WorldRenderer::renderLevel(

drawChunks(level->chunks.get(), camera, shader);

assets->getTexture("gui/menubg")->bind();
shader->uniformMatrix("u_model", glm::mat4(1.0f));
modelBatch->test(glm::vec3(0, 68, 0), glm::vec3(1.0f));
modelBatch->flush();
Expand Down

0 comments on commit 3d2deaf

Please sign in to comment.