Compare commits
17 Commits
e38bb50245
...
165073c36d
Author | SHA1 | Date | |
---|---|---|---|
165073c36d | |||
87168d42c3 | |||
4deb22f37d | |||
ec92a3310e | |||
b991a85b6b | |||
282f8e8cb2 | |||
4cc6f0cb26 | |||
a7a4840dd4 | |||
2144b8a03a | |||
8563b424e9 | |||
4326ecd23f | |||
a68b4a85f0 | |||
19988d9c1d | |||
fde96d1419 | |||
6972ca3cb5 | |||
4757ba8e58 | |||
5fa9a04cb2 |
@ -40,8 +40,9 @@ add_executable(CodingGame
|
||||
|
||||
src/window/window.cpp
|
||||
|
||||
src/components/batch.cpp
|
||||
|
||||
src/renderer/debug.cpp
|
||||
src/renderer/basics.cpp
|
||||
src/renderer/mesh.cpp
|
||||
src/renderer/shader.cpp
|
||||
src/renderer/texture.cpp
|
||||
|
27
include/components/batch.h
Normal file
27
include/components/batch.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef COMPONENT_BATCH_H_
|
||||
#define COMPONENT_BATCH_H_
|
||||
|
||||
#include "renderer/renderer.h"
|
||||
|
||||
// requires mesh component
|
||||
struct batch {
|
||||
friend class Renderer;
|
||||
public:
|
||||
// requires transform component
|
||||
struct item {
|
||||
unsigned int batchId;
|
||||
};
|
||||
|
||||
batch();
|
||||
|
||||
inline const unsigned int id() const { return m_id; }
|
||||
protected:
|
||||
static unsigned int LastID;
|
||||
private:
|
||||
unsigned int m_id;
|
||||
unsigned int m_instance_vbo { 0 };
|
||||
private:
|
||||
void prepare(glm::mat4 *instances, unsigned int count);
|
||||
};
|
||||
|
||||
#endif // COMPONENT_BATCH_H_
|
@ -5,7 +5,7 @@
|
||||
#include "renderer/wavefront.h"
|
||||
|
||||
struct mesh {
|
||||
std::unique_ptr<Object> object;
|
||||
std::shared_ptr<Object> object;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_MESH_H_
|
6
include/components/rotate.h
Normal file
6
include/components/rotate.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef COMPONENT_ROTATE_H_
|
||||
#define COMPONENT_ROTATE_H_
|
||||
|
||||
struct rotate {};
|
||||
|
||||
#endif // COMPONENT_ROTATE_H_
|
@ -3,7 +3,10 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "renderer/mesh.h"
|
||||
|
||||
class Vertex {
|
||||
friend class Mesh;
|
||||
private:
|
||||
glm::vec3 m_position;
|
||||
glm::vec3 m_normal;
|
||||
|
@ -21,7 +21,7 @@ public:
|
||||
public:
|
||||
Mesh();
|
||||
public:
|
||||
void Render();
|
||||
void Render(unsigned int count);
|
||||
};
|
||||
|
||||
#endif // MESH_H_
|
@ -9,20 +9,23 @@
|
||||
// TODO: make static or singleton
|
||||
class Renderer {
|
||||
public:
|
||||
Renderer();
|
||||
Renderer(entt::registry& registry);
|
||||
|
||||
void Render(entt::registry& registry);
|
||||
void GenerateShadowMaps(entt::registry& registry);
|
||||
void Render();
|
||||
void Init();
|
||||
void GenerateShadowMaps();
|
||||
|
||||
void OnWindowResized(int w, int h);
|
||||
private:
|
||||
void ApplyLights(entt::registry& registry, Shader &shader);
|
||||
void UpdateView(entt::registry& registry, Shader &shader);
|
||||
void RenderScene(entt::registry& registry, Shader &shader);
|
||||
void ApplyLights(Shader &shader);
|
||||
void UpdateView();
|
||||
void RenderScene(Shader &shader);
|
||||
private:
|
||||
Shader m_shader;
|
||||
Shader m_depthShader;
|
||||
|
||||
entt::registry& m_registry;
|
||||
|
||||
// unsigned int m_depth_fbo;
|
||||
// unsigned int m_depthMap;
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include "shader.h"
|
||||
#include "renderer/renderer.h"
|
||||
#include "renderer/material.h"
|
||||
#include "renderer/mesh.h"
|
||||
|
||||
@ -15,6 +16,7 @@ enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
|
||||
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };
|
||||
|
||||
class Object {
|
||||
friend class Renderer;
|
||||
private:
|
||||
static inline int NormalizeIndex(int idx, int baseCount);
|
||||
|
||||
@ -34,8 +36,10 @@ private:
|
||||
Mesh& GetLastMesh();
|
||||
void CreateNewMesh(const std::string& materialName);
|
||||
public:
|
||||
void Render(Shader& shader);
|
||||
void Render(Shader& shader, unsigned int count);
|
||||
[[nodiscard]] inline const std::string Name() const { return m_name; }
|
||||
protected:
|
||||
void EnableBatch(unsigned int instanceVBO);
|
||||
private:
|
||||
std::string m_name;
|
||||
std::vector<glm::vec3> m_vertices;
|
||||
|
19
src/components/batch.cpp
Normal file
19
src/components/batch.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "components/batch.h"
|
||||
|
||||
unsigned int batch::LastID = 0;
|
||||
|
||||
batch::batch() {
|
||||
m_id = ++LastID;
|
||||
}
|
||||
|
||||
void batch::prepare(glm::mat4 *instances, unsigned int count) {
|
||||
if (m_instance_vbo == 0) {
|
||||
glGenBuffers(1, &m_instance_vbo);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_instance_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * count, reinterpret_cast<void*>(instances), GL_DYNAMIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
52
src/main.cpp
52
src/main.cpp
@ -23,15 +23,17 @@
|
||||
#include "components/light.h"
|
||||
#include "components/camera.h"
|
||||
#include "components/mesh.h"
|
||||
#include "components/rotate.h"
|
||||
#include "components/batch.h"
|
||||
|
||||
class Game : public IApplication {
|
||||
public:
|
||||
Game() {
|
||||
Game() : m_renderer(m_registry) {
|
||||
Object* lightObj = Object::LoadFile("./assets/sphere.obj");
|
||||
const auto lght = m_registry.create();
|
||||
m_registry.emplace<transform>(lght, glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
|
||||
m_registry.emplace<light>(lght, light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
||||
m_registry.emplace<mesh>(lght, std::unique_ptr<Object>(lightObj));
|
||||
m_registry.emplace<mesh>(lght, std::shared_ptr<Object>(lightObj));
|
||||
|
||||
const auto cameraEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 2.f, 2.f));
|
||||
@ -40,17 +42,36 @@ public:
|
||||
Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj");
|
||||
const auto targetEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(targetEntity, glm::vec3(0.f, 0.0f, 0.f));
|
||||
m_registry.emplace<mesh>(targetEntity, std::unique_ptr<Object>(targetObj));
|
||||
m_registry.emplace<mesh>(targetEntity, std::shared_ptr<Object>(targetObj));
|
||||
|
||||
Object* cubeObj = Object::LoadFile("./assets/grass_block/grass_block.obj");
|
||||
Object* grass = Object::LoadFile("./assets/grass_block/grass_block.obj");
|
||||
const auto cubeEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f));
|
||||
m_registry.emplace<mesh>(cubeEntity, std::unique_ptr<Object>(cubeObj));
|
||||
m_registry.emplace<mesh>(cubeEntity, std::shared_ptr<Object>(grass));
|
||||
|
||||
// Cube template (use shared object to avoid reloading 1000 times)
|
||||
std::shared_ptr<Object> cubeObj = std::shared_ptr<Object>(Object::LoadFile("./assets/grass_block/grass_block.obj"));
|
||||
const auto batchEntt = m_registry.create();
|
||||
m_registry.emplace<batch>(batchEntt);
|
||||
m_registry.emplace<mesh>(batchEntt, cubeObj);
|
||||
auto cubeBatch = m_registry.get<batch>(batchEntt);
|
||||
// Generate 1000 random cubes
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
const auto cubeEntity = m_registry.create();
|
||||
|
||||
float x = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
|
||||
float y = static_cast<float>(rand()) / RAND_MAX * 10.f; // range [0, 10]
|
||||
float z = static_cast<float>(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100]
|
||||
|
||||
m_registry.emplace<transform>(cubeEntity, glm::vec3(x, y, z));
|
||||
m_registry.emplace<rotate>(cubeEntity);
|
||||
m_registry.emplace<batch::item>(cubeEntity, cubeBatch.id());
|
||||
}
|
||||
|
||||
Object* floorObj = Object::LoadFile("./assets/plane.obj");
|
||||
const auto floorEntt = m_registry.create();
|
||||
m_registry.emplace<transform>(floorEntt, glm::vec3(0.f));
|
||||
m_registry.emplace<mesh>(floorEntt, std::unique_ptr<Object>(floorObj));
|
||||
m_registry.emplace<mesh>(floorEntt, std::shared_ptr<Object>(floorObj));
|
||||
}
|
||||
~Game() override {}
|
||||
|
||||
@ -69,7 +90,8 @@ public:
|
||||
m_startTicks = SDL_GetTicks();
|
||||
m_frameCount = 0;
|
||||
|
||||
m_renderer.GenerateShadowMaps(m_registry);
|
||||
m_renderer.Init();
|
||||
m_renderer.GenerateShadowMaps();
|
||||
}
|
||||
|
||||
void OnWindowResized(const WindowResized& event) override {
|
||||
@ -165,17 +187,17 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
// auto rotateEntts = m_registry.view<transform, const mesh>();
|
||||
// for (auto [entity, transform, mesh] : rotateEntts.each()) {
|
||||
// // auto targetTransform = rotateEntts.get<transform>(entity);
|
||||
// if (!m_registry.all_of<light>(entity)) {
|
||||
// transform.rotation.y = m_angle;
|
||||
// }
|
||||
// }
|
||||
auto rotateEntts = m_registry.view<transform, rotate>();
|
||||
for (auto [entity, t] : rotateEntts.each()) {
|
||||
// auto targetTransform = rotateEntts.get<transform>(entity);
|
||||
if (!m_registry.all_of<light>(entity)) {
|
||||
t.rotation.y = m_angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnRender() override {
|
||||
m_renderer.Render(m_registry);
|
||||
m_renderer.Render();
|
||||
|
||||
m_frameCount++;
|
||||
m_currentTicks = SDL_GetTicks();
|
||||
|
@ -1,15 +0,0 @@
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "renderer/basics.h"
|
||||
|
||||
void Vertex::DefineAttrib()
|
||||
{
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_position)));
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_normal)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_texCoord)));
|
||||
glEnableVertexAttribArray(2);
|
||||
}
|
@ -10,6 +10,43 @@ void MessageCallback(GLenum source,
|
||||
const GLchar* message,
|
||||
const void* userParam)
|
||||
{
|
||||
if(id == 131169 || id == 131185 || id == 131218 || id == 131204) return;
|
||||
|
||||
std::cout << "---------------" << std::endl;
|
||||
std::cout << "Debug message (" << id << "): " << message << std::endl;
|
||||
|
||||
switch (source)
|
||||
{
|
||||
case GL_DEBUG_SOURCE_API: std::cout << "Source: API"; break;
|
||||
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: std::cout << "Source: Window System"; break;
|
||||
case GL_DEBUG_SOURCE_SHADER_COMPILER: std::cout << "Source: Shader Compiler"; break;
|
||||
case GL_DEBUG_SOURCE_THIRD_PARTY: std::cout << "Source: Third Party"; break;
|
||||
case GL_DEBUG_SOURCE_APPLICATION: std::cout << "Source: Application"; break;
|
||||
case GL_DEBUG_SOURCE_OTHER: std::cout << "Source: Other"; break;
|
||||
} std::cout << std::endl;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case GL_DEBUG_TYPE_ERROR: std::cout << "Type: Error"; break;
|
||||
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: std::cout << "Type: Deprecated Behaviour"; break;
|
||||
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: std::cout << "Type: Undefined Behaviour"; break;
|
||||
case GL_DEBUG_TYPE_PORTABILITY: std::cout << "Type: Portability"; break;
|
||||
case GL_DEBUG_TYPE_PERFORMANCE: std::cout << "Type: Performance"; break;
|
||||
case GL_DEBUG_TYPE_MARKER: std::cout << "Type: Marker"; break;
|
||||
case GL_DEBUG_TYPE_PUSH_GROUP: std::cout << "Type: Push Group"; break;
|
||||
case GL_DEBUG_TYPE_POP_GROUP: std::cout << "Type: Pop Group"; break;
|
||||
case GL_DEBUG_TYPE_OTHER: std::cout << "Type: Other"; break;
|
||||
} std::cout << std::endl;
|
||||
|
||||
switch (severity)
|
||||
{
|
||||
case GL_DEBUG_SEVERITY_HIGH: std::cout << "Severity: high"; break;
|
||||
case GL_DEBUG_SEVERITY_MEDIUM: std::cout << "Severity: medium"; break;
|
||||
case GL_DEBUG_SEVERITY_LOW: std::cout << "Severity: low"; break;
|
||||
case GL_DEBUG_SEVERITY_NOTIFICATION: std::cout << "Severity: notification"; break;
|
||||
} std::cout << std::endl;
|
||||
std::cout << std::endl;
|
||||
return;
|
||||
(void) source;
|
||||
(void) id;
|
||||
(void) length;
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include <cstddef>
|
||||
|
||||
#include "renderer/mesh.h"
|
||||
|
||||
Mesh::Mesh() {
|
||||
@ -13,13 +15,21 @@ Mesh::Mesh() {
|
||||
|
||||
// VBO (vertex buffer)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
||||
|
||||
// EBO (index buffer)
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
|
||||
|
||||
Vertex::DefineAttrib();
|
||||
// attributes
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_position)));
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_normal)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, m_texCoord)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
@ -29,18 +39,22 @@ void Mesh::Upload() const {
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexBuffer.size() * sizeof(Vertex), m_vertexBuffer.data(), GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexBuffer.size() * sizeof(Vertex), m_vertexBuffer.data(), GL_DYNAMIC_DRAW);
|
||||
|
||||
// Upload indices
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer.size() * sizeof(unsigned int), m_indexBuffer.data(), GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer.size() * sizeof(unsigned int), m_indexBuffer.data(), GL_DYNAMIC_DRAW);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Mesh::Render()
|
||||
void Mesh::Render(unsigned int count)
|
||||
{
|
||||
Bind();
|
||||
if (count > 1) {
|
||||
glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(m_indexBuffer.size()), GL_UNSIGNED_INT, 0, count);
|
||||
} else {
|
||||
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_indexBuffer.size()), GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
Unbind();
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#ifdef WIN32
|
||||
@ -16,8 +17,9 @@
|
||||
#include "components/camera.h"
|
||||
#include "components/light.h"
|
||||
#include "components/mesh.h"
|
||||
#include "components/batch.h"
|
||||
|
||||
Renderer::Renderer()
|
||||
Renderer::Renderer(entt::registry& registry) : m_registry(registry)
|
||||
{
|
||||
m_proj = glm::perspective(
|
||||
static_cast<float>(M_PI_2),
|
||||
@ -27,7 +29,7 @@ Renderer::Renderer()
|
||||
);
|
||||
|
||||
m_shader.init(
|
||||
FileManager::read("./src/shaders/simple.vs"),
|
||||
FileManager::read("./src/shaders/main.vs"),
|
||||
FileManager::read("./src/shaders/pbr.fs")
|
||||
);
|
||||
|
||||
@ -42,6 +44,18 @@ Renderer::Renderer()
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
}
|
||||
|
||||
void Renderer::Init() {
|
||||
// auto view = m_registry.view<batch, mesh>();
|
||||
// for (auto [_, b, m] : m_registry.view<batch, mesh>().each()) {
|
||||
// unsigned int items = 0;
|
||||
// for (auto [entt, item] : m_registry.view<batch::item>().each()) {
|
||||
// if (item.batchId == b.id()) ++items;
|
||||
// }
|
||||
// b.prepare()
|
||||
// m.object->EnableBatch(b.m_instance_vbo);
|
||||
// }
|
||||
}
|
||||
|
||||
void Renderer::OnWindowResized(int w, int h) {
|
||||
m_proj = glm::perspective(
|
||||
static_cast<float>(M_PI_2),
|
||||
@ -49,18 +63,16 @@ void Renderer::OnWindowResized(int w, int h) {
|
||||
0.01f,
|
||||
100.0f
|
||||
);
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
m_depthShader.setMat4("u_projection", m_proj);
|
||||
}
|
||||
|
||||
void Renderer::ApplyLights(entt::registry& registry, Shader &shader) {
|
||||
auto lights = registry.view<light>();
|
||||
void Renderer::ApplyLights(Shader &shader) {
|
||||
auto lights = m_registry.view<light>();
|
||||
// TODO: Pass Lights Data to depth shader as well
|
||||
shader.setInt("lightsCount", static_cast<int>(lights.size()));
|
||||
size_t lightIndex = 0;
|
||||
for (auto entity : lights) {
|
||||
auto &l = registry.get<light>(entity);
|
||||
auto &transf = registry.get<transform>(entity);
|
||||
auto &l = m_registry.get<light>(entity);
|
||||
auto &transf = m_registry.get<transform>(entity);
|
||||
|
||||
shader.setInt("lights[" + std::to_string(lightIndex) + "].type", static_cast<int>(l.type));
|
||||
shader.setVec3("lights[" + std::to_string(lightIndex) + "].position", transf.position);
|
||||
@ -75,32 +87,68 @@ void Renderer::ApplyLights(entt::registry& registry, Shader &shader) {
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::UpdateView(entt::registry& registry, Shader &shader) {
|
||||
auto cam = registry.view<transform, camera>().back();
|
||||
auto camTransform = registry.get<transform>(cam);
|
||||
void Renderer::UpdateView() {
|
||||
auto cam = m_registry.view<transform, camera>().back();
|
||||
auto camTransform = m_registry.get<transform>(cam);
|
||||
|
||||
m_view = glm::lookAt(
|
||||
camTransform.position,
|
||||
camTransform.position + camTransform.rotation,
|
||||
glm::vec3(0.f, 1.f, 0.f)
|
||||
);
|
||||
shader.setMat4("u_view", m_view);
|
||||
shader.setMat4("u_projection", m_proj);
|
||||
m_shader.setMat4("u_view", m_view);
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
|
||||
shader.setVec3("viewPos", camTransform.position);
|
||||
m_shader.setVec3("viewPos", camTransform.position);
|
||||
}
|
||||
|
||||
void Renderer::RenderScene(entt::registry& registry, Shader &shader) {
|
||||
auto view = registry.view<transform, mesh>();
|
||||
void Renderer::RenderScene(Shader &shader) {
|
||||
std::unordered_map<unsigned int, std::vector<entt::entity>> batches;
|
||||
|
||||
for (auto [entity, transf, mesh] : view.each()) {
|
||||
for (auto [entt, item] : 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);
|
||||
}
|
||||
|
||||
shader.setBool("u_isInstanced", true);
|
||||
shader.setBool("isLight", false);
|
||||
shader.setVec3("currentLightColor", glm::vec3(0.f));
|
||||
for (auto [entt, b, m] : 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_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 prevInstanceVBO = b.m_instance_vbo;
|
||||
b.prepare(models.data(), models.size());
|
||||
if (prevInstanceVBO <= 0) {
|
||||
std::cout << "[DEBUG] enabling batch"<<std::endl;
|
||||
m.object->EnableBatch(b.m_instance_vbo);
|
||||
}
|
||||
m.object->Render(shader, batchItems.size());
|
||||
}
|
||||
shader.setBool("u_isInstanced", false);
|
||||
|
||||
for (auto [entity, transf, mesh] : m_registry.view<transform, mesh>(entt::exclude<batch, batch::item>).each()) {
|
||||
if (mesh.object == nullptr) {
|
||||
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (registry.all_of<light>(entity)) {
|
||||
auto &l = registry.get<light>(entity);
|
||||
if (m_registry.all_of<light>(entity)) {
|
||||
auto &l = m_registry.get<light>(entity);
|
||||
shader.setBool("isLight", true);
|
||||
shader.setVec3("currentLightColor", l.color);
|
||||
} else {
|
||||
@ -113,16 +161,16 @@ void Renderer::RenderScene(entt::registry& registry, Shader &shader) {
|
||||
|
||||
shader.setMat4("u_model", m_model);
|
||||
|
||||
mesh.object->Render(shader);
|
||||
mesh.object->Render(shader, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::GenerateShadowMaps(entt::registry& registry) {
|
||||
void Renderer::GenerateShadowMaps() {
|
||||
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
|
||||
|
||||
m_depthShader.use();
|
||||
|
||||
auto lights = registry.view<light>();
|
||||
auto lights = m_registry.view<light>();
|
||||
|
||||
for (auto [lEntt, l] : lights.each()) {
|
||||
// TODO: support other light types when ready
|
||||
@ -150,12 +198,12 @@ void Renderer::GenerateShadowMaps(entt::registry& registry) {
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::Render(entt::registry& registry) {
|
||||
void Renderer::Render() {
|
||||
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
|
||||
|
||||
m_depthShader.use();
|
||||
|
||||
auto lights = registry.view<light, transform>();
|
||||
auto lights = m_registry.view<light, transform>();
|
||||
|
||||
for (auto [lEntt, l, t] : lights.each()) {
|
||||
// TODO: support other light types when ready
|
||||
@ -177,7 +225,7 @@ void Renderer::Render(entt::registry& registry) {
|
||||
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, l.fbo);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
RenderScene(registry, m_depthShader);
|
||||
RenderScene(m_depthShader);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glCullFace(GL_BACK);
|
||||
}
|
||||
@ -191,8 +239,8 @@ void Renderer::Render(entt::registry& registry) {
|
||||
|
||||
m_shader.use();
|
||||
|
||||
ApplyLights(registry, m_shader);
|
||||
UpdateView(registry, m_shader);
|
||||
ApplyLights(m_shader);
|
||||
UpdateView();
|
||||
|
||||
RenderScene(registry, m_shader);
|
||||
RenderScene(m_shader);
|
||||
}
|
@ -392,6 +392,24 @@ Object* Object::LoadFile(const std::string& filename) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
void Object::EnableBatch(unsigned int instanceVBO) {
|
||||
for (auto &mesh : m_meshes) {
|
||||
mesh.Bind();
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
|
||||
std::size_t vec4Size = sizeof(glm::vec4);
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
glEnableVertexAttribArray(3 + i); // use locations 3,4,5,6 for instance matrix
|
||||
glVertexAttribPointer(3 + i, 4, GL_FLOAT, GL_FALSE,
|
||||
sizeof(glm::mat4), (void*)(i * vec4Size));
|
||||
glVertexAttribDivisor(3 + i, 1); // IMPORTANT: one per instance, not per vertex
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
mesh.Unbind();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// void Object::Render(Shader& shader)
|
||||
// {
|
||||
@ -420,7 +438,7 @@ Object* Object::LoadFile(const std::string& filename) {
|
||||
// }
|
||||
// }
|
||||
|
||||
void Object::Render(Shader& shader)
|
||||
void Object::Render(Shader& shader, unsigned int count)
|
||||
{
|
||||
for (auto &mesh : m_meshes)
|
||||
{
|
||||
@ -484,7 +502,7 @@ void Object::Render(Shader& shader)
|
||||
}
|
||||
|
||||
// --- Render mesh ---
|
||||
mesh.Render();
|
||||
mesh.Render(count);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
layout (location = 0) in vec3 position; // Vertex position in local space (model space)
|
||||
layout (location = 1) in vec3 normal; // vertex normal
|
||||
layout (location = 2) in vec2 texCoord; // Vertex texture uv
|
||||
layout (location = 3) in mat4 instanceModel; // Vertex texture uv
|
||||
|
||||
// Output to fragment shader
|
||||
out vec3 vertexPos;
|
||||
@ -15,12 +16,15 @@ out vec4 fragPosLightSpace;
|
||||
uniform mat4 u_model; // Model matrix: transforms from local space to world space
|
||||
uniform mat4 u_view; // View matrix: transforms from world space to camera space (view space)
|
||||
uniform mat4 u_projection; // Projection matrix: transforms from camera space to clip space
|
||||
uniform bool u_isInstanced;
|
||||
|
||||
void main()
|
||||
{
|
||||
vertexPos = vec3(u_model * vec4(position, 1.0));
|
||||
mat4 model = u_isInstanced ? instanceModel : u_model;
|
||||
|
||||
mat3 normalMatrix = mat3(transpose(inverse(u_model)));
|
||||
vertexPos = vec3(model * vec4(position, 1.0));
|
||||
|
||||
mat3 normalMatrix = mat3(transpose(inverse(model)));
|
||||
vertexNormal = normalMatrix * normal;
|
||||
// vertexNormal = normal;
|
||||
|
@ -147,7 +147,7 @@ void main()
|
||||
for (int i = 0; i < lightsCount; i++)
|
||||
{
|
||||
// compute light vector L depending on type
|
||||
vec3 L;
|
||||
vec3 L = vec3(0);
|
||||
if (lights[i].type == 0) {
|
||||
// directional light: convention here is that lights[i].position stores the direction
|
||||
// *towards* the light (for example, for sun direction you may upload -sunDir).
|
||||
|
Reference in New Issue
Block a user