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