add graphics/core/Model

This commit is contained in:
MihailRis 2024-06-21 02:30:35 +03:00
parent 916d1c1408
commit 6ba38ee167
5 changed files with 112 additions and 5 deletions

View File

@ -0,0 +1,28 @@
#include "Model.hpp"
using namespace model;
inline constexpr glm::vec3 X(1, 0, 0);
inline constexpr glm::vec3 Y(0, 1, 0);
inline constexpr glm::vec3 Z(0, 0, 1);
void Mesh::addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm) {
vertices.push_back({pos-right-up, {0,0}, norm});
vertices.push_back({pos+right-up, {1,0}, norm});
vertices.push_back({pos+right+up, {1,1}, norm});
vertices.push_back({pos-right-up, {0,0}, norm});
vertices.push_back({pos+right+up, {1,1}, norm});
vertices.push_back({pos-right+up, {0,1}, norm});
}
void Mesh::addBox(glm::vec3 pos, glm::vec3 size) {
addPlane(pos+Z*size, X*size, Y*size, Z);
addPlane(pos-Z*size, -X*size, Y*size, -Z);
addPlane(pos+Y*size, X*size, -Z*size, Y);
addPlane(pos-Y*size, X*size, Z*size, -Y);
addPlane(pos+X*size, -Z*size, Y*size, X);
addPlane(pos-X*size, Z*size, Y*size, -X);
}

View File

@ -0,0 +1,33 @@
#ifndef GRAPHICS_CORE_MODEL_HPP_
#define GRAPHICS_CORE_MODEL_HPP_
#include <string>
#include <vector>
#include <glm/glm.hpp>
namespace model {
struct Vertex {
glm::vec3 coord;
glm::vec2 uv;
glm::vec3 normal;
};
struct Mesh {
std::string texture;
std::vector<Vertex> vertices;
void addPlane(glm::vec3 pos, glm::vec3 right, glm::vec3 up, glm::vec3 norm);
void addBox(glm::vec3 pos, glm::vec3 size);
};
struct Model {
std::vector<Mesh> meshes;
Mesh& addMesh(const std::string& texture) {
meshes.push_back({texture, {}});
return meshes[meshes.size()-1];
}
};
}
#endif // GRAPHICS_CORE_MODEL_HPP_

View File

@ -1,7 +1,9 @@
#include "ModelBatch.hpp"
#include "../core/Mesh.hpp"
#include "../core/Model.hpp"
#include "../core/Texture.hpp"
#include "../../assets/Assets.hpp"
#include "../../window/Window.hpp"
#include "../../voxels/Chunks.hpp"
#include "../../lighting/Lightmap.hpp"
@ -30,12 +32,13 @@ struct DecomposedMat4 {
glm::vec4 perspective;
};
ModelBatch::ModelBatch(size_t capacity, Chunks* chunks)
ModelBatch::ModelBatch(size_t capacity, Assets* assets, 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),
assets(assets),
chunks(chunks)
{
ubyte pixels[] = {
@ -47,6 +50,34 @@ ModelBatch::ModelBatch(size_t capacity, Chunks* chunks)
ModelBatch::~ModelBatch() {
}
void ModelBatch::draw(const model::Model& model) {
glm::vec3 gpos = combined * glm::vec4(glm::vec3(), 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
);
for (const auto& mesh : model.meshes) {
auto texture = assets->getTexture(mesh.texture);
if (texture) {
texture->bind();
} else {
blank->bind();
}
for (const auto& vert : mesh.vertices) {
auto norm = rotation * vert.normal;
float d = glm::dot(norm, SUN_VECTOR);
d = 0.8f + d * 0.2f;
auto color = lights * d;
vertex(vert.coord, vert.uv, color);
}
flush();
}
}
void ModelBatch::test(glm::vec3 pos, glm::vec3 size) {
glm::vec3 gpos = combined * glm::vec4(pos, 1.0f);
light_t light = chunks->getLight(gpos.x, gpos.y, gpos.z);

View File

@ -8,6 +8,11 @@
class Mesh;
class Texture;
class Chunks;
class Assets;
namespace model {
struct Model;
}
class ModelBatch {
std::unique_ptr<float[]> buffer;
@ -21,6 +26,7 @@ class ModelBatch {
std::vector<glm::mat4> matrices;
glm::mat3 rotation;
Assets* assets;
Chunks* chunks;
static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f};
@ -65,7 +71,7 @@ class ModelBatch {
vertex(pos-right+up, {0,1}, color);
}
public:
ModelBatch(size_t capacity, Chunks* chunks);
ModelBatch(size_t capacity, Assets* assets, Chunks* chunks);
~ModelBatch();
void pushMatrix(glm::mat4 matrix);
@ -73,6 +79,8 @@ public:
void box(glm::vec3 pos, glm::vec3 size, glm::vec4 lights);
void draw(const model::Model& model);
void test(glm::vec3 pos, glm::vec3 size);
void flush();
};

View File

@ -32,6 +32,7 @@
#include "../core/PostProcessing.hpp"
#include "../core/Shader.hpp"
#include "../core/Texture.hpp"
#include "../core/Model.hpp"
#include <assert.h>
#include <GL/glew.h>
@ -49,7 +50,7 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* pl
player(player),
frustumCulling(std::make_unique<Frustum>()),
lineBatch(std::make_unique<LineBatch>()),
modelBatch(std::make_unique<ModelBatch>(1000, level->chunks.get()))
modelBatch(std::make_unique<ModelBatch>(1000, engine->getAssets(), level->chunks.get()))
{
renderer = std::make_unique<ChunksRenderer>(
level,
@ -193,10 +194,16 @@ void WorldRenderer::renderLevel(
drawChunks(level->chunks.get(), camera, shader);
model::Model model {};
auto& mesh = model.addMesh("gui/warning");
mesh.addBox({}, glm::vec3(1));
assets->getTexture("gui/menubg")->bind();
shader->uniformMatrix("u_model", glm::mat4(1.0f));
modelBatch->test(glm::vec3(0, 88, 0), glm::vec3(1.0f));
modelBatch->flush();
//modelBatch->test(glm::vec3(0, 88, 0), glm::vec3(1.0f));
modelBatch->pushMatrix(glm::translate(glm::mat4(1.0f), glm::vec3(0, 88, 0)));
modelBatch->draw(model);
modelBatch->popMatrix();
skybox->unbind();
}