fix ModelBatch overflow
This commit is contained in:
parent
3235740333
commit
57a0377b36
@ -22,6 +22,14 @@ inline constexpr glm::vec3 X(1, 0, 0);
|
||||
inline constexpr glm::vec3 Y(0, 1, 0);
|
||||
inline constexpr glm::vec3 Z(0, 0, 1);
|
||||
|
||||
struct DecomposedMat4 {
|
||||
glm::vec3 scale;
|
||||
glm::mat3 rotation;
|
||||
glm::vec3 translation;
|
||||
glm::vec3 skew;
|
||||
glm::vec4 perspective;
|
||||
};
|
||||
|
||||
ModelBatch::ModelBatch(size_t capacity, Chunks* chunks)
|
||||
: buffer(std::make_unique<float[]>(capacity * VERTEX_SIZE)),
|
||||
capacity(capacity),
|
||||
@ -55,6 +63,9 @@ void ModelBatch::test(glm::vec3 pos, glm::vec3 size) {
|
||||
}
|
||||
|
||||
void ModelBatch::box(glm::vec3 pos, glm::vec3 size) {
|
||||
if (index + 36 < capacity*VERTEX_SIZE) {
|
||||
flush();
|
||||
}
|
||||
glm::vec3 gpos = combined * glm::vec4(pos, 1.0f);
|
||||
light_t light = chunks->getLight(gpos.x, gpos.y, gpos.z);
|
||||
glm::vec4 lights (
|
||||
@ -84,36 +95,28 @@ void ModelBatch::flush() {
|
||||
index = 0;
|
||||
}
|
||||
|
||||
void ModelBatch::pushMatrix(glm::mat4 matrix) {
|
||||
matrices.push_back(combined);
|
||||
combined = combined * matrix;
|
||||
|
||||
decomposed = {};
|
||||
static glm::mat4 extract_rotation(glm::mat4 matrix) {
|
||||
DecomposedMat4 decomposed = {};
|
||||
glm::quat rotation;
|
||||
glm::decompose(
|
||||
combined,
|
||||
matrix,
|
||||
decomposed.scale,
|
||||
rotation,
|
||||
decomposed.translation,
|
||||
decomposed.skew,
|
||||
decomposed.perspective
|
||||
);
|
||||
decomposed.rotation = glm::toMat3(rotation);
|
||||
return glm::toMat3(rotation);
|
||||
}
|
||||
|
||||
void ModelBatch::pushMatrix(glm::mat4 matrix) {
|
||||
matrices.push_back(combined);
|
||||
combined = combined * matrix;
|
||||
rotation = extract_rotation(combined);
|
||||
}
|
||||
|
||||
void ModelBatch::popMatrix() {
|
||||
combined = matrices[matrices.size()-1];
|
||||
matrices.erase(matrices.end()-1);
|
||||
|
||||
decomposed = {};
|
||||
glm::quat rotation;
|
||||
glm::decompose(
|
||||
combined,
|
||||
decomposed.scale,
|
||||
rotation,
|
||||
decomposed.translation,
|
||||
decomposed.skew,
|
||||
decomposed.perspective
|
||||
);
|
||||
decomposed.rotation = glm::toMat3(rotation);
|
||||
rotation = extract_rotation(combined);
|
||||
}
|
||||
|
||||
@ -9,14 +9,6 @@ class Mesh;
|
||||
class Texture;
|
||||
class Chunks;
|
||||
|
||||
struct DecomposedMat4 {
|
||||
glm::vec3 scale;
|
||||
glm::mat3 rotation;
|
||||
glm::vec3 translation;
|
||||
glm::vec3 skew;
|
||||
glm::vec4 perspective;
|
||||
};
|
||||
|
||||
class ModelBatch {
|
||||
std::unique_ptr<float[]> buffer;
|
||||
size_t capacity;
|
||||
@ -27,15 +19,14 @@ class ModelBatch {
|
||||
|
||||
glm::mat4 combined;
|
||||
std::vector<glm::mat4> matrices;
|
||||
|
||||
DecomposedMat4 decomposed {};
|
||||
glm::mat3 rotation;
|
||||
|
||||
Chunks* chunks;
|
||||
|
||||
static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f};
|
||||
|
||||
inline void vertex(
|
||||
glm::vec3 pos, glm::vec2 uv, glm::vec4 color
|
||||
glm::vec3 pos, glm::vec2 uv, glm::vec4 light
|
||||
) {
|
||||
float* buffer = this->buffer.get();
|
||||
pos = combined * glm::vec4(pos, 1.0f);
|
||||
@ -50,21 +41,20 @@ class ModelBatch {
|
||||
uint32_t integer;
|
||||
} compressed;
|
||||
|
||||
compressed.integer = (static_cast<uint32_t>(color.r * 255) & 0xff) << 24;
|
||||
compressed.integer |= (static_cast<uint32_t>(color.g * 255) & 0xff) << 16;
|
||||
compressed.integer |= (static_cast<uint32_t>(color.b * 255) & 0xff) << 8;
|
||||
compressed.integer |= (static_cast<uint32_t>(color.a * 255) & 0xff);
|
||||
compressed.integer = (static_cast<uint32_t>(light.r * 255) & 0xff) << 24;
|
||||
compressed.integer |= (static_cast<uint32_t>(light.g * 255) & 0xff) << 16;
|
||||
compressed.integer |= (static_cast<uint32_t>(light.b * 255) & 0xff) << 8;
|
||||
compressed.integer |= (static_cast<uint32_t>(light.a * 255) & 0xff);
|
||||
|
||||
buffer[index++] = compressed.floating;
|
||||
}
|
||||
|
||||
inline void plane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm, glm::vec4 light) {
|
||||
norm = decomposed.rotation * norm;
|
||||
norm = rotation * norm;
|
||||
float d = glm::dot(norm, SUN_VECTOR);
|
||||
d = 0.8f + d * 0.2f;
|
||||
|
||||
glm::vec4 color {d, d, d, 1.0f};
|
||||
color *= light;
|
||||
auto color = light * d;
|
||||
|
||||
vertex(pos-right-up, {0,0}, color);
|
||||
vertex(pos+right-up, {1,0}, color);
|
||||
|
||||
@ -195,7 +195,7 @@ void WorldRenderer::renderLevel(
|
||||
|
||||
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->test(glm::vec3(0, 88, 0), glm::vec3(1.0f));
|
||||
modelBatch->flush();
|
||||
|
||||
skybox->unbind();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user