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

@@ -1,13 +1,21 @@
set(SANDBOX_TARGET sandbox)
set(MODEL_TARGET model)
add_executable(${SANDBOX_TARGET} src/main.cpp)
add_executable(${MODEL_TARGET} src/model.cpp)
set_target_properties(${SANDBOX_TARGET} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
set_target_properties(${MODEL_TARGET} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
target_link_libraries(${SANDBOX_TARGET} PRIVATE ${ENGINE_TARGET})
target_link_libraries(${MODEL_TARGET} PRIVATE ${ENGINE_TARGET})
# --- Copy engine.dll and all dependent DLLs next to sandbox.exe ---
if (WIN32)
@@ -16,4 +24,10 @@ if (WIN32)
$<TARGET_RUNTIME_DLLS:${SANDBOX_TARGET}> $<TARGET_FILE_DIR:${SANDBOX_TARGET}>
COMMAND_EXPAND_LISTS
)
add_custom_command(TARGET ${MODEL_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_RUNTIME_DLLS:${MODEL_TARGET}> $<TARGET_FILE_DIR:${MODEL_TARGET}>
COMMAND_EXPAND_LISTS
)
endif()

View File

@@ -17,7 +17,8 @@
#include "engine/components/camera.h"
#include "engine/components/mesh.h"
#include "engine/components/rotate.h"
#include "engine/components/batch.h"
#include "engine/3d/prefab.hpp"
#include "engine/scene/scene.h"
#include "engine/input/input.h"
@@ -55,13 +56,13 @@ public:
assert(modelEntity.HasComponent<mesh>() && "model doesn't have any mesh!");
// 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"));
auto cubeObj = Object::LoadFile("./assets/grass_block/grass_block.obj");
auto batchEntt = scene->CreateEntity();
auto& cubeBatch = batchEntt.AddComponent<batch>();
auto& cubeBatch = batchEntt.AddComponent<Prefab>(std::move(*cubeObj));
// auto& cubeBatch = batchEntt.GetComponent<batch>();
batchEntt.AddComponent<mesh>(cubeObj);
assert(batchEntt.HasComponent<batch>() && "batch doesn't have any batch component!");
assert(batchEntt.HasComponent<mesh>() && "batch doesn't have any mesh component!");
// batchEntt.AddComponent<mesh>(cubeObj);
// assert(batchEntt.HasComponent<batch>() && "batch doesn't have any batch component!");
// assert(batchEntt.HasComponent<mesh>() && "batch doesn't have any mesh component!");
// Generate 1000 random cubes
for (int i = 0; i < 100; ++i) {
auto cubeEntity = scene->CreateEntity();
@@ -72,12 +73,12 @@ public:
cubeEntity.AddComponent<Transform>(glm::vec3(x, y, z));
cubeEntity.AddComponent<rotate>();
cubeEntity.AddComponent<batch::item>(cubeBatch.id());
cubeEntity.AddComponent<batch::item>(cubeBatch.GetID());
}
Object* floorObj = Object::LoadFile("./assets/common/plane/plane.obj");
auto floorEntt = scene->CreateEntity();
floorEntt.AddComponent<Transform>(glm::vec3(0.f), glm::vec3(2.f), glm::vec3(5.f));
floorEntt.AddComponent<Transform>(glm::vec3(0.f));
floorEntt.AddComponent<mesh>(std::shared_ptr<Object>(floorObj));
assert(floorEntt.HasComponent<mesh>() && "floor doesn't have any mesh component!");

92
sandbox/src/model.cpp Normal file
View File

@@ -0,0 +1,92 @@
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#ifdef _WIN32
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/extended_min_max.hpp>
#endif
#include "engine/renderer/wavefront.h"
#include "engine/app/app.h"
#include "engine/components/transform.h"
#include "engine/components/light.h"
#include "engine/components/camera.h"
#include "engine/components/mesh.h"
#include "engine/components/rotate.h"
#include "engine/3d/prefab.hpp"
#include "engine/scene/scene.h"
#include "engine/input/input.h"
#include "engine/api.h"
using namespace Core;
class ModelViewer : public IApplication {
public:
ModelViewer() = default;
~ModelViewer() override {}
void OnInit(std::shared_ptr<Scene> scene) override {
m_scene = scene;
Object* lightObj = Object::LoadFile("./assets/common/sphere/sphere.obj");
lightEntity = scene->CreateEntity();
lightEntity.AddComponent<Transform>(glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
lightEntity.AddComponent<light>(light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
lightEntity.AddComponent<mesh>(std::shared_ptr<Renderable>(lightObj));
assert(lightEntity.HasComponent<mesh>() && "light doesn't have any mesh!");
cameraEntity = scene->CreateEntity();
cameraEntity.AddComponent<camera>();
cameraEntity.AddComponent<Transform>(glm::vec3(0.f, 2.f, 2.f));
assert(cameraEntity.HasComponent<camera>() && "Camera doesn't have required 'camera' component");
assert(cameraEntity.HasComponent<Transform>() && "Camera doesn't have 'transform' component");
Object* targetObj = Object::LoadFile("./assets/grass_block/grass_block.obj");
modelEntity = scene->CreateEntity();
modelEntity.AddComponent<Transform>(glm::vec3(0.f, 0.0f, 0.f));
modelEntity.AddComponent<mesh>(std::shared_ptr<Renderable>(targetObj));
assert(modelEntity.HasComponent<mesh>() && "model doesn't have any mesh!");
std::cout << "ModelViewer initialized" << std::endl;
}
void OnUpdate(Timestep dt) override {
m_elapsed += dt.GetMilliseconds();
if (m_elapsed >= 1000) { // one second passed
m_elapsed = 0;
double fps = 1 / dt;
std::cout << "FPS: " << fps << std::endl;
}
}
void OnEvent(const Event& event) override {
if (event.GetType() == EventType::WINDOW_RESIZE) {
auto resizeEvent = static_cast<const WindowResizeEvent&>(event);
std::cout << "[DEBUG] <EVENT> Window resized to " << resizeEvent.GetWidth() << "x" << resizeEvent.GetHeight() << std::endl;
}
else if (event.GetType() == EventType::WINDOW_CLOSE) {
std::cout << "[DEBUG] <EVENT> Window closing" << std::endl;
}
}
private:
// for internal 1-second timer
int m_elapsed;
std::shared_ptr<Scene> m_scene;
Entity lightEntity;
Entity cameraEntity;
Entity modelEntity;
};
IApplication* CreateApplication() {
return new ModelViewer();
}