making prefab work

This commit is contained in:
2025-11-14 18:26:30 +01:00
parent c7b6a79270
commit 113412bb5b
13 changed files with 4038 additions and 58 deletions

View File

@ -19,6 +19,7 @@ void batch::prepare(glm::mat4 *instances, unsigned int count) {
m_instance_count = count;
} else if (count > m_instance_count) {
// Optional: reallocate only if you *really* have more instances than before
// FIXME: what the hell is m_instance_vbo
glBindBuffer(GL_ARRAY_BUFFER, m_instance_vbo);
OpenGL::Buffer::Bind(m_instanceBuffer);
OpenGL::Buffer::Data(m_instanceBuffer, nullptr, sizeof(glm::mat4) * count);

View File

@ -68,6 +68,7 @@ namespace OpenGL {
VertexArray::VertexArray() : m_id(0) {
glGenVertexArrays(1, &m_id);
std::cout << "[DEBUG] VArr initialized: " << m_id << std::endl;
}
VertexArray::~VertexArray() {
@ -81,6 +82,7 @@ namespace OpenGL {
void VertexArray::Bind() {
assert(m_id != 0 && "Vertex Array wasn't initialized.");
std::cout << "[DEBUG] VArr binding: " << m_id << std::endl;
glBindVertexArray(m_id);
}

View File

@ -20,6 +20,7 @@
#include "engine/components/light.h"
#include "engine/components/mesh.h"
#include "engine/components/batch.h"
#include "engine/3d/prefab.hpp"
namespace Core {
@ -61,6 +62,10 @@ void Renderer::Init() {
for (auto [entt, mesh] : m_scene->m_registry.view<mesh>().each()) {
mesh.mesh->Prepare();
}
for (auto [entt, prefab] : m_scene->m_registry.view<Prefab>().each()) {
prefab.Prepare();
}
}
void Renderer::OnWindowResized(int w, int h) {
@ -142,46 +147,54 @@ void Renderer::UpdateView() {
}
void Renderer::RenderScene(Shader &shader) {
std::unordered_map<unsigned int, std::vector<entt::entity>> batches;
// std::unordered_map<unsigned int, std::vector<entt::entity>> batches;
for (auto [entt, item] : m_scene->m_registry.view<batch::item>().each()) {
if (batches.find(item.batchId) == batches.end())
batches.insert(std::make_pair(item.batchId, std::vector<entt::entity>()));
// for (auto [entt, item] : m_scene->m_registry.view<batch::item>().each()) {
// if (batches.find(item.batchId) == batches.end())
// batches.insert(std::make_pair(item.batchId, std::vector<entt::entity>()));
batches[item.batchId].push_back(entt);
}
// batches[item.batchId].push_back(entt);
// }
shader.setBool("u_isInstanced", true);
// shader.setBool("u_isInstanced", true);
// shader.setBool("isLight", false);
// shader.setVec3("currentLightColor", glm::vec3(0.f));
// for (auto [entt, b, m] : m_scene->m_registry.view<batch, mesh>().each()) {
// // check if have items for batch render
// if (batches.find(b.id()) == batches.end()) continue;
// auto &batchItems = batches[b.id()];
// std::vector<glm::mat4> models;
// models.reserve(batchItems.size());
// for (auto item : batchItems) {
// auto &t = m_scene->m_registry.get<Transform>(item);
// glm::mat4 rotation = glm::yawPitchRoll(t.rotation.y, t.rotation.x, t.rotation.z);
// auto itemModel = glm::translate(glm::mat4(1.f), t.position) * rotation;
// models.push_back(itemModel);
// }
// auto prevState = b.Initialized();
// b.prepare(models.data(), models.size());
// if (!prevState) {
// std::cout << "[DEBUG] enabling batch" << std::endl;
// // TODO:
// // m.object->EnableBatch(b.m_instanceBuffer);
// }
// m.mesh->Render(shader);
// }
// shader.setBool("u_isInstanced", false);
// light cannot be batch rendered (yet :3)
shader.setBool("isLight", false);
shader.setVec3("currentLightColor", glm::vec3(0.f));
for (auto [entt, b, m] : m_scene->m_registry.view<batch, mesh>().each()) {
// check if have items for batch render
if (batches.find(b.id()) == batches.end()) continue;
auto &batchItems = batches[b.id()];
std::vector<glm::mat4> models;
models.reserve(batchItems.size());
for (auto item : batchItems) {
auto &t = m_scene->m_registry.get<Transform>(item);
glm::mat4 rotation = glm::yawPitchRoll(t.rotation.y, t.rotation.x, t.rotation.z);
auto itemModel = glm::translate(glm::mat4(1.f), t.position) * rotation;
models.push_back(itemModel);
}
auto prevState = b.Initialized();
b.prepare(models.data(), models.size());
if (!prevState) {
std::cout << "[DEBUG] enabling batch" << std::endl;
// TODO:
// m.object->EnableBatch(b.m_instanceBuffer);
}
m.mesh->Render(shader);
for (auto [entity, prefab] : m_scene->m_registry.view<Prefab>().each()) {
prefab.Render(shader, *m_scene.get(), 1);
}
shader.setBool("u_isInstanced", false);
for (auto [entity, transf, mesh] : m_scene->m_registry.view<Transform, mesh>(entt::exclude<batch, batch::item>).each()) {
// entt::exclude<batch, batch::item>
for (auto [entity, transf, mesh] : m_scene->m_registry.view<Transform, mesh>().each()) {
if (mesh.mesh == nullptr) {
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
return;
@ -201,7 +214,7 @@ void Renderer::RenderScene(Shader &shader) {
shader.setMat4("u_model", m_model);
mesh.mesh->Render(shader);
mesh.mesh->Render(shader, *m_scene.get(), 1);
}
}

View File

@ -344,7 +344,7 @@ Object* Object::LoadFile(const std::string& filename) {
Vertex v;
v.position = obj->m_vertices[vi];
v.normal = (ni >= 0) ? obj->m_normals[ni] : glm::vec3(0.0f);
v.uv = (ti >= 0) ? obj->m_texCoords[ti] : glm::vec3(0.0f);
v.uv = (ti >= 0) ? obj->m_texCoords[ti] : glm::vec2(0.0f);
uint32_t idx = mesh.PushVertex(v);
faceIndices.push_back(idx);
@ -352,6 +352,9 @@ Object* Object::LoadFile(const std::string& filename) {
// mesh.m_indexBuffer.push_back(mesh.m_vertexBuffer.size() - 1);
}
// [0, 1, 2]
// ^
// triangulate polygon (fan)
if (faceIndices.size() >= 3) {
for (size_t i = 1; i + 1 < faceIndices.size(); ++i) {