feat: entt library
This commit is contained in:
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -58,6 +58,9 @@
|
||||
"typeinfo": "cpp",
|
||||
"variant": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"typeindex": "cpp"
|
||||
"typeindex": "cpp",
|
||||
"ranges": "cpp",
|
||||
"list": "cpp",
|
||||
"unordered_set": "cpp"
|
||||
}
|
||||
}
|
@ -16,6 +16,13 @@ if (UNIX)
|
||||
)
|
||||
FetchContent_MakeAvailable(glm)
|
||||
|
||||
FetchContent_Declare(
|
||||
EnTT
|
||||
GIT_REPOSITORY https://github.com/skypjack/entt.git
|
||||
GIT_TAG d4014c74dc3793aba95ae354d6e23a026c2796db
|
||||
)
|
||||
FetchContent_MakeAvailable(EnTT)
|
||||
|
||||
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLEW REQUIRED)
|
||||
@ -58,6 +65,7 @@ target_link_libraries(CodingGame PRIVATE
|
||||
OpenGL::GL
|
||||
GLEW::GLEW
|
||||
glm::glm
|
||||
EnTT::EnTT
|
||||
)
|
||||
|
||||
# ---------- Visibility (helps optimizer & smaller binaries on Release) ----------
|
||||
|
6
include/components/camera.h
Normal file
6
include/components/camera.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef COMPONENTS_PLAYER_H_
|
||||
#define COMPONENTS_PLAYER_H_
|
||||
|
||||
struct camera {};
|
||||
|
||||
#endif // COMPONENTS_PLAYER_H_
|
6
include/components/light.h
Normal file
6
include/components/light.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef COMPONENTS_LIGHT_H_
|
||||
#define COMPONENTS_LIGHT_H_
|
||||
|
||||
struct light {};
|
||||
|
||||
#endif // COMPONENTS_LIGHT_H_
|
11
include/components/mesh.h
Normal file
11
include/components/mesh.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef COMPONENTS_MESH_H_
|
||||
#define COMPONENTS_MESH_H_
|
||||
|
||||
#include <memory>
|
||||
#include "renderer/wavefront.h"
|
||||
|
||||
struct mesh {
|
||||
std::unique_ptr<Object> object;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_MESH_H_
|
12
include/components/transform.h
Normal file
12
include/components/transform.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef COMPONENTS_TRANSFORM_H_
|
||||
#define COMPONENTS_TRANSFORM_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct transform {
|
||||
glm::vec3 position;
|
||||
glm::vec3 rotation;
|
||||
glm::vec3 scale;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_TRANSFORM_H_
|
@ -1,14 +0,0 @@
|
||||
#ifndef ECS_ENTITY_CAMERA_H_
|
||||
#define ECS_ENTITY_CAMERA_H
|
||||
|
||||
#include "ecs/entity.h"
|
||||
|
||||
namespace ecs {
|
||||
class Camera : Entity {
|
||||
public:
|
||||
Camera() = default;
|
||||
~Camera() = default;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ECS_ENTITY_CAMERA_H_
|
@ -1,9 +0,0 @@
|
||||
#ifndef ECS_COMPONENT_H_
|
||||
#define ECS_COMPONENT_H_
|
||||
|
||||
namespace ecs {
|
||||
class Component {
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ECS_COMPONENT_H_
|
@ -1,16 +0,0 @@
|
||||
#ifndef ECS_COMP_MESH_H_
|
||||
#define ECS_COMP_MESH_H_
|
||||
|
||||
#include "renderer/wavefront.h"
|
||||
#include "ecs/component.h"
|
||||
|
||||
namespace ecs {
|
||||
class Mesh : Component {
|
||||
public:
|
||||
Object* object;
|
||||
|
||||
Mesh(Object* model) : object(model) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ECS_COMP_MESH_H_
|
@ -1,18 +0,0 @@
|
||||
#ifndef ECS_COMP_TRANSFORM_H_
|
||||
#define ECS_COMP_TRANSFORM_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include "ecs/component.h"
|
||||
|
||||
namespace ecs {
|
||||
class Transform : Component {
|
||||
public:
|
||||
Transform() = default;
|
||||
public:
|
||||
glm::vec3 pos;
|
||||
glm::vec3 rot;
|
||||
glm::vec3 scale;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ECS_COMP_TRANSFORM_H_
|
@ -1,18 +0,0 @@
|
||||
#ifndef ECS_ENTITY_H_
|
||||
#define ECS_ENTITY_H_
|
||||
|
||||
#include "ecs/components/transform.h"
|
||||
#include "ecs/components/mesh.h"
|
||||
|
||||
namespace ecs {
|
||||
class Entity {
|
||||
public:
|
||||
Transform transform;
|
||||
Mesh mesh;
|
||||
|
||||
Entity(Mesh m) : mesh(m) {}
|
||||
~Entity() = default;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ECS_ENTITY_H_
|
@ -2,16 +2,16 @@
|
||||
#define RENDERER_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
#include "renderer/shader.h"
|
||||
#include "ecs/entity.h"
|
||||
|
||||
// TODO: make static or singleton
|
||||
class Renderer {
|
||||
public:
|
||||
Renderer(ecs::Entity& light, ecs::Entity& camera);
|
||||
Renderer();
|
||||
|
||||
void RenderLight();
|
||||
void RenderEntity(const ecs::Entity& entity);
|
||||
void Render(entt::registry& registry);
|
||||
|
||||
void OnWindowResized(int w, int h);
|
||||
private:
|
||||
@ -20,9 +20,6 @@ private:
|
||||
glm::mat4 m_model;
|
||||
glm::mat4 m_proj;
|
||||
glm::mat4 m_view;
|
||||
|
||||
ecs::Entity& m_light;
|
||||
ecs::Entity& m_camera;
|
||||
};
|
||||
|
||||
#endif // RENDERER_H_
|
@ -35,6 +35,7 @@ private:
|
||||
void CreateNewMesh(const std::string& materialName);
|
||||
public:
|
||||
void Render(Shader& shader);
|
||||
[[nodiscard]] inline const std::string Name() const { return m_name; }
|
||||
private:
|
||||
std::string m_name;
|
||||
std::vector<glm::vec3> m_vertices;
|
||||
|
55
src/main.cpp
55
src/main.cpp
@ -16,26 +16,31 @@
|
||||
#include "renderer/wavefront.h"
|
||||
#include "renderer/engine.h"
|
||||
#include "renderer/renderer.h"
|
||||
#include "ecs/entity.h"
|
||||
|
||||
#include "IO/file_manager.h"
|
||||
|
||||
#include "components/transform.h"
|
||||
#include "components/light.h"
|
||||
#include "components/camera.h"
|
||||
#include "components/mesh.h"
|
||||
|
||||
class Game : public IApplication {
|
||||
public:
|
||||
Game()
|
||||
: m_light(ecs::Entity(ecs::Mesh(Object::LoadFile("./assets/cube.obj")))),
|
||||
m_camera(ecs::Entity(ecs::Mesh(nullptr))),
|
||||
m_target(ecs::Entity(ecs::Mesh(Object::LoadFile("./assets/monkey.obj")))),
|
||||
m_renderer(m_light, m_camera) {
|
||||
// Object* lightObj = Object::LoadFile("./assets/cube.obj");
|
||||
// ecs::Mesh lightMesh = ecs::Mesh(lightObj);
|
||||
// m_light = ecs::Entity(lightMesh);
|
||||
m_light.transform.pos = glm::vec3(-5.f, 5.f, 5.f);
|
||||
Game() {
|
||||
Object* lightObj = Object::LoadFile("./assets/cube.obj");
|
||||
const auto lightEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(lightEntity, glm::vec3(-5.f, 5.f, 5.f), glm::vec3(0.f));
|
||||
m_registry.emplace<light>(lightEntity);
|
||||
m_registry.emplace<mesh>(lightEntity, std::unique_ptr<Object>(lightObj));
|
||||
|
||||
// Object* cameraObj = nullptr;
|
||||
// ecs::Mesh cameraMesh = ecs::Mesh(cameraObj);
|
||||
// m_camera = ecs::Entity(cameraMesh);
|
||||
m_camera.transform.pos = glm::vec3(0.f, 0.f, 2.f);
|
||||
const auto cameraEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(cameraEntity, glm::vec3(0.f, 0.f, 2.f));
|
||||
m_registry.emplace<camera>(cameraEntity, glm::vec3(0.f, 0.f, 2.f));
|
||||
|
||||
Object* targetObj = Object::LoadFile("./assets/monkey.obj");
|
||||
const auto targetEntity = m_registry.create();
|
||||
m_registry.emplace<transform>(targetEntity, glm::vec3(0.f));
|
||||
m_registry.emplace<mesh>(targetEntity, std::unique_ptr<Object>(targetObj));
|
||||
}
|
||||
~Game() override {}
|
||||
|
||||
@ -100,8 +105,11 @@ public:
|
||||
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
|
||||
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
|
||||
|
||||
m_camera.transform.pos += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||
m_camera.transform.rot = cameraViewDirection;
|
||||
auto view = m_registry.view<camera, transform>();
|
||||
for (auto [cam, camTransform] : view.each()) {
|
||||
camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||
camTransform.rotation = cameraViewDirection;
|
||||
}
|
||||
|
||||
// update rotation
|
||||
if (!m_paused) {
|
||||
@ -111,12 +119,17 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
m_target.transform.rot.y = m_angle;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnRender() override {
|
||||
m_renderer.RenderLight();
|
||||
m_renderer.RenderEntity(m_target);
|
||||
m_renderer.Render(m_registry);
|
||||
|
||||
m_frameCount++;
|
||||
m_currentTicks = SDL_GetTicks();
|
||||
@ -131,9 +144,7 @@ public:
|
||||
}
|
||||
private:
|
||||
Renderer m_renderer;
|
||||
ecs::Entity m_target;
|
||||
ecs::Entity m_light;
|
||||
ecs::Entity m_camera;
|
||||
entt::registry m_registry;
|
||||
|
||||
float m_angle;
|
||||
Uint64 m_lastTicks;
|
||||
|
@ -7,8 +7,12 @@
|
||||
#include "window/window.h"
|
||||
#include "IO/file_manager.h"
|
||||
|
||||
Renderer::Renderer(ecs::Entity& light, ecs::Entity& camera)
|
||||
: m_light(light), m_camera(camera)
|
||||
#include "components/transform.h"
|
||||
#include "components/camera.h"
|
||||
#include "components/light.h"
|
||||
#include "components/mesh.h"
|
||||
|
||||
Renderer::Renderer()
|
||||
{
|
||||
m_proj = glm::perspective(
|
||||
static_cast<float>(M_PI_2),
|
||||
@ -25,6 +29,8 @@ Renderer::Renderer(ecs::Entity& light, ecs::Entity& camera)
|
||||
m_model = glm::mat4(1.f);
|
||||
|
||||
m_shader.use();
|
||||
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
}
|
||||
|
||||
void Renderer::OnWindowResized(int w, int h) {
|
||||
@ -34,53 +40,53 @@ void Renderer::OnWindowResized(int w, int h) {
|
||||
0.01f,
|
||||
100.0f
|
||||
);
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
}
|
||||
|
||||
void Renderer::RenderLight() {
|
||||
void Renderer::Render(entt::registry& registry) {
|
||||
auto view = registry.view<transform, mesh>();
|
||||
|
||||
auto cam = registry.view<transform, camera>().back();
|
||||
auto camTransform = registry.get<transform>(cam);
|
||||
|
||||
auto lightEntt = registry.view<transform, light>().back();
|
||||
auto lightTransform = registry.get<transform>(lightEntt);
|
||||
|
||||
m_view = glm::lookAt(
|
||||
m_camera.transform.pos,
|
||||
m_camera.transform.pos + m_camera.transform.rot,
|
||||
camTransform.position,
|
||||
camTransform.position + camTransform.rotation,
|
||||
glm::vec3(0.f, 1.f, 0.f)
|
||||
);
|
||||
|
||||
m_shader.setMat4("u_view", m_view);
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
|
||||
m_shader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
||||
m_shader.setVec3("lightPos", m_light.transform.pos);
|
||||
m_shader.setVec3("viewPos", m_camera.transform.pos);
|
||||
m_shader.setVec3("lightPos", lightTransform.position);
|
||||
m_shader.setVec3("viewPos", camTransform.position);
|
||||
|
||||
m_model = glm::mat4(1.f);
|
||||
m_model = glm::translate(m_model, m_light.transform.pos);
|
||||
// std::cout << "cam pos: " << "vec(" << camTransform.position.x << ", " << camTransform.position.y << ", " << camTransform.position.z << ")" << std::endl;
|
||||
// std::cout << "cam rot: " << "vec(" << camTransform.rotation.x << ", " << camTransform.rotation.y << ", " << camTransform.rotation.z << ")" << std::endl;
|
||||
|
||||
m_shader.setMat4("u_model", m_model);
|
||||
// std::cout << "light pos: " << "vec(" << lightTransform.position.x << ", " << lightTransform.position.y << ", " << lightTransform.position.z << ")" << std::endl;
|
||||
// std::cout << "light rot: " << "vec(" << lightTransform.rotation.x << ", " << lightTransform.rotation.y << ", " << lightTransform.rotation.z << ")" << std::endl;
|
||||
|
||||
m_light.mesh.object->Render(m_shader);
|
||||
}
|
||||
for (auto [entity, transf, mesh] : view.each()) {
|
||||
if (mesh.object == nullptr) {
|
||||
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
void Renderer::RenderEntity(const ecs::Entity& entity) {
|
||||
if (entity.mesh.object == nullptr) {
|
||||
std::cerr << "WARN: Entity doesn't have a mesh to render" << std::endl;
|
||||
return;
|
||||
m_model = glm::mat4(1.0f);
|
||||
|
||||
// Apply translation
|
||||
m_model = glm::translate(m_model, transf.position);
|
||||
|
||||
// Apply rotations (order matters!)
|
||||
m_model = glm::rotate(m_model, transf.rotation.x, glm::vec3(1, 0, 0)); // pitch
|
||||
m_model = glm::rotate(m_model, transf.rotation.y, glm::vec3(0, 1, 0)); // yaw
|
||||
m_model = glm::rotate(m_model, transf.rotation.z, glm::vec3(0, 0, 1)); // roll
|
||||
|
||||
m_shader.setMat4("u_model", m_model);
|
||||
|
||||
mesh.object->Render(m_shader);
|
||||
}
|
||||
|
||||
m_view = glm::lookAt(
|
||||
m_camera.transform.pos,
|
||||
m_camera.transform.pos + m_camera.transform.rot,
|
||||
glm::vec3(0.f, 1.f, 0.f)
|
||||
);
|
||||
|
||||
m_model = glm::mat4(1.0f);
|
||||
|
||||
// Apply translation
|
||||
m_model = glm::translate(m_model, entity.transform.pos);
|
||||
|
||||
// Apply rotations (order matters!)
|
||||
m_model = glm::rotate(m_model, entity.transform.rot.x, glm::vec3(1, 0, 0)); // pitch
|
||||
m_model = glm::rotate(m_model, entity.transform.rot.y, glm::vec3(0, 1, 0)); // yaw
|
||||
m_model = glm::rotate(m_model, entity.transform.rot.z, glm::vec3(0, 0, 1)); // roll
|
||||
|
||||
m_shader.setMat4("u_model", m_model);
|
||||
|
||||
entity.mesh.object->Render(m_shader);
|
||||
}
|
Reference in New Issue
Block a user