add basic ah physicsworld
This commit is contained in:
+143
-106
@@ -3,6 +3,8 @@
|
||||
#include <destrum/FS/AssetFS.h>
|
||||
#include "glm/gtx/transform.hpp"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <destrum/Components/Physics/Rigidbody.h>
|
||||
#include <destrum/Components/Physics/BoxCollider.h>
|
||||
#include <destrum/Scene/Scene.h>
|
||||
|
||||
#include "destrum/Components/MeshRendererComponent.h"
|
||||
@@ -13,12 +15,14 @@
|
||||
#include "destrum/Util/ModelDoc.h"
|
||||
#include "destrum/Components/Animator.h"
|
||||
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
#include "imgui.h"
|
||||
#include "destrum/Util/ModelDocUtils.h"
|
||||
|
||||
|
||||
LightKeeper::LightKeeper() : App(), renderer(meshCache, materialCache) {
|
||||
}
|
||||
|
||||
@@ -226,125 +230,153 @@ void LightKeeper::customInit() {
|
||||
// CharObj->GetTransform().SetWorldScale(0.01f, 0.01f, 0.01f);
|
||||
scene.Add(CharObj);
|
||||
|
||||
ModelDoc::LoadOptions options{};
|
||||
options.meshImportMode = ModelDoc::MeshImportMode::MergedPerNode;
|
||||
// ModelDoc::LoadOptions options{};
|
||||
// options.meshImportMode = ModelDoc::MeshImportMode::MergedPerNode;
|
||||
//
|
||||
// auto model = ModelDoc::LoadModel(
|
||||
// AssetFS::GetInstance()
|
||||
// .GetFullPath("engine://multiple_object_test.fbx")
|
||||
// .generic_string(),
|
||||
// options
|
||||
// );
|
||||
//
|
||||
// ModelDocUtils::LogModelDocSummary(model, "engine://multiple_object_test.fbx");
|
||||
//
|
||||
// auto root = std::make_shared<GameObject>("MultiMeshModel");
|
||||
// root->GetTransform().SetWorldPosition(glm::vec3(0.1f, 0.1f, 0.1f));
|
||||
// scene.Add(root);
|
||||
//
|
||||
// for (std::size_t i = 0; i < model.primitives.size(); ++i) {
|
||||
// const auto &primitive = model.primitives[i];
|
||||
//
|
||||
// const auto meshID = meshCache.addMesh(gfxDevice, primitive.mesh);
|
||||
//
|
||||
// const auto texturePath = ModelDocUtils::PickTexturePath(
|
||||
// model,
|
||||
// primitive,
|
||||
// AssetFS::GetInstance().GetFullPath("engine://textures/white.png")
|
||||
// );
|
||||
//
|
||||
// const auto textureID = gfxDevice.loadImageFromFile(texturePath);
|
||||
//
|
||||
// const auto materialID = materialCache.addMaterial(gfxDevice, {
|
||||
// .baseColor = ModelDocUtils::GetImportedBaseColor(
|
||||
// model, primitive),
|
||||
// .diffuseTexture = textureID,
|
||||
// .name = ModelDocUtils::GetImportedMaterialName(
|
||||
// model,
|
||||
// primitive,
|
||||
// primitive.mesh.name + "_Material"
|
||||
// ),
|
||||
// });
|
||||
//
|
||||
// auto part = std::make_shared<GameObject>(
|
||||
// primitive.mesh.name.empty()
|
||||
// ? "Primitive_" + std::to_string(i)
|
||||
// : primitive.mesh.name
|
||||
// );
|
||||
//
|
||||
// auto meshComp = part->AddComponent<MeshRendererComponent>();
|
||||
// meshComp->SetMeshID(meshID);
|
||||
// meshComp->SetMaterialID(materialID);
|
||||
//
|
||||
// scene.Add(part);
|
||||
// }
|
||||
|
||||
auto model = ModelDoc::LoadModel(
|
||||
AssetFS::GetInstance()
|
||||
.GetFullPath("engine://multiple_object_test.fbx")
|
||||
.generic_string(),
|
||||
options
|
||||
);
|
||||
|
||||
ModelDocUtils::LogModelDocSummary(model, "engine://multiple_object_test.fbx");
|
||||
|
||||
auto root = std::make_shared<GameObject>("MultiMeshModel");
|
||||
root->GetTransform().SetWorldPosition(glm::vec3(0.1f, 0.1f, 0.1f));
|
||||
scene.Add(root);
|
||||
|
||||
for (std::size_t i = 0; i < model.primitives.size(); ++i) {
|
||||
const auto &primitive = model.primitives[i];
|
||||
|
||||
const auto meshID = meshCache.addMesh(gfxDevice, primitive.mesh);
|
||||
|
||||
const auto texturePath = ModelDocUtils::PickTexturePath(
|
||||
model,
|
||||
primitive,
|
||||
AssetFS::GetInstance().GetFullPath("engine://textures/white.png")
|
||||
);
|
||||
|
||||
const auto textureID = gfxDevice.loadImageFromFile(texturePath);
|
||||
|
||||
const auto materialID = materialCache.addMaterial(gfxDevice, {
|
||||
.baseColor = ModelDocUtils::GetImportedBaseColor(
|
||||
model, primitive),
|
||||
.diffuseTexture = textureID,
|
||||
.name = ModelDocUtils::GetImportedMaterialName(
|
||||
model,
|
||||
primitive,
|
||||
primitive.mesh.name + "_Material"
|
||||
),
|
||||
});
|
||||
|
||||
auto part = std::make_shared<GameObject>(
|
||||
primitive.mesh.name.empty()
|
||||
? "Primitive_" + std::to_string(i)
|
||||
: primitive.mesh.name
|
||||
);
|
||||
|
||||
auto meshComp = part->AddComponent<MeshRendererComponent>();
|
||||
meshComp->SetMeshID(meshID);
|
||||
meshComp->SetMaterialID(materialID);
|
||||
|
||||
scene.Add(part);
|
||||
}
|
||||
|
||||
// {
|
||||
// const auto CharObj = std::make_shared<GameObject>("Character");
|
||||
//
|
||||
// ModelDoc::LoadOptions characterOptions{};
|
||||
// characterOptions.meshImportMode = ModelDoc::MeshImportMode::MergedPerNode;
|
||||
// characterOptions.loadMaterials = true;
|
||||
// characterOptions.loadSkeleton = true;
|
||||
// characterOptions.loadAnimations = false;
|
||||
//
|
||||
// auto charModel = ModelDoc::LoadModel(
|
||||
// AssetFS::GetInstance()
|
||||
// .GetFullPath("engine://characterMedium.fbx")
|
||||
// .generic_string(),
|
||||
// characterOptions
|
||||
// );
|
||||
//
|
||||
// const auto &charPrimitive =
|
||||
// ModelDocUtils::GetFirstSkinnedPrimitiveOrFirstOrThrow(
|
||||
// charModel,
|
||||
// "engine://characterMedium.fbx"
|
||||
// );
|
||||
//
|
||||
// const auto charMeshID = meshCache.addMesh(gfxDevice, charPrimitive.mesh);
|
||||
//
|
||||
// const auto charTextureID = gfxDevice.loadImageFromFile(
|
||||
// AssetFS::GetInstance().GetFullPath("engine://textures/criminalMaleA.png")
|
||||
// );
|
||||
//
|
||||
// const auto charMaterialID = materialCache.addMaterial(gfxDevice, {
|
||||
// .baseColor = ModelDocUtils::GetImportedBaseColor(
|
||||
// charModel, charPrimitive),
|
||||
// .diffuseTexture = charTextureID,
|
||||
// .name = ModelDocUtils::GetImportedMaterialName(
|
||||
// charModel,
|
||||
// charPrimitive,
|
||||
// "CharacterMaterial"
|
||||
// ),
|
||||
// });
|
||||
//
|
||||
// const auto charMeshComp = CharObj->AddComponent<MeshRendererComponent>();
|
||||
// charMeshComp->SetMeshID(charMeshID);
|
||||
// charMeshComp->SetMaterialID(charMaterialID);
|
||||
//
|
||||
// const auto animator = CharObj->AddComponent<Animator>();
|
||||
// animator->setSkeleton(charModel.skeleton);
|
||||
//
|
||||
// auto runClips = ModelDoc::LoadAnimationClips(
|
||||
// AssetFS::GetInstance()
|
||||
// .GetFullPath("engine://run.fbx")
|
||||
// .generic_string(),
|
||||
// charModel.skeleton
|
||||
// );
|
||||
//
|
||||
// for (auto &clip: runClips) {
|
||||
// spdlog::info("Loaded animation: '{}' ({:.2f}s)", clip.name, clip.duration);
|
||||
// animator->addClip(std::make_shared<SkeletalAnimation>(std::move(clip)));
|
||||
// }
|
||||
//
|
||||
// if (!runClips.empty()) {
|
||||
// animator->play("Root|Run");
|
||||
// }
|
||||
//
|
||||
// CharObj->GetTransform().SetWorldPosition(glm::vec3(0.f));
|
||||
// CharObj->GetTransform().SetWorldPosition(glm::vec3(5, 0, 0));
|
||||
// scene.Add(CharObj);
|
||||
// }
|
||||
|
||||
{
|
||||
const auto CharObj = std::make_shared<GameObject>("Character");
|
||||
|
||||
ModelDoc::LoadOptions characterOptions{};
|
||||
characterOptions.meshImportMode = ModelDoc::MeshImportMode::MergedPerNode;
|
||||
characterOptions.loadMaterials = true;
|
||||
characterOptions.loadSkeleton = true;
|
||||
characterOptions.loadAnimations = false;
|
||||
|
||||
auto charModel = ModelDoc::LoadModel(
|
||||
auto cubeModel = ModelDoc::LoadModel(
|
||||
AssetFS::GetInstance()
|
||||
.GetFullPath("engine://characterMedium.fbx")
|
||||
.GetFullPath("engine://cube.fbx")
|
||||
.generic_string(),
|
||||
characterOptions
|
||||
staticModelOptions
|
||||
);
|
||||
ModelDocUtils::LogModelDocSummary(cubeModel, "cube.fbx");
|
||||
|
||||
const auto &charPrimitive =
|
||||
ModelDocUtils::GetFirstSkinnedPrimitiveOrFirstOrThrow(
|
||||
charModel,
|
||||
"engine://characterMedium.fbx"
|
||||
);
|
||||
const auto &cubePrimitive = ModelDocUtils::GetFirstPrimitiveOrThrow(cubeModel, "game://cube.fbx");
|
||||
const auto cubeMeshID = meshCache.addMesh(gfxDevice, cubePrimitive.mesh);
|
||||
|
||||
const auto charMeshID = meshCache.addMesh(gfxDevice, charPrimitive.mesh);
|
||||
auto cube = std::make_shared<GameObject>("Cube");
|
||||
|
||||
const auto charTextureID = gfxDevice.loadImageFromFile(
|
||||
AssetFS::GetInstance().GetFullPath("engine://textures/criminalMaleA.png")
|
||||
);
|
||||
cube->AddComponent<BoxCollider>(glm::vec3{0.5f});
|
||||
cube->AddComponent<Rigidbody>();
|
||||
|
||||
const auto charMaterialID = materialCache.addMaterial(gfxDevice, {
|
||||
.baseColor = ModelDocUtils::GetImportedBaseColor(
|
||||
charModel, charPrimitive),
|
||||
.diffuseTexture = charTextureID,
|
||||
.name = ModelDocUtils::GetImportedMaterialName(
|
||||
charModel,
|
||||
charPrimitive,
|
||||
"CharacterMaterial"
|
||||
),
|
||||
});
|
||||
auto meshComp = cube->AddComponent<MeshRendererComponent>();
|
||||
meshComp->SetMeshID(cubeMeshID);
|
||||
meshComp->SetMaterialID(charMaterialID);
|
||||
|
||||
const auto charMeshComp = CharObj->AddComponent<MeshRendererComponent>();
|
||||
charMeshComp->SetMeshID(charMeshID);
|
||||
charMeshComp->SetMaterialID(charMaterialID);
|
||||
cube->GetTransform().SetWorldPosition(glm::vec3(0.0f, 2.0f, 0.0f));
|
||||
cube->GetTransform().SetWorldScale(glm::vec3(0.005f));
|
||||
|
||||
const auto animator = CharObj->AddComponent<Animator>();
|
||||
animator->setSkeleton(charModel.skeleton);
|
||||
|
||||
auto runClips = ModelDoc::LoadAnimationClips(
|
||||
AssetFS::GetInstance()
|
||||
.GetFullPath("engine://run.fbx")
|
||||
.generic_string(),
|
||||
charModel.skeleton
|
||||
);
|
||||
|
||||
for (auto &clip: runClips) {
|
||||
spdlog::info("Loaded animation: '{}' ({:.2f}s)", clip.name, clip.duration);
|
||||
animator->addClip(std::make_shared<SkeletalAnimation>(std::move(clip)));
|
||||
}
|
||||
|
||||
if (!runClips.empty()) {
|
||||
animator->play("Root|Run");
|
||||
}
|
||||
|
||||
CharObj->GetTransform().SetWorldPosition(glm::vec3(0.f));
|
||||
CharObj->GetTransform().SetWorldPosition(glm::vec3(5, 0, 0));
|
||||
scene.Add(CharObj);
|
||||
scene.Add(cube);
|
||||
scene.GetPhysics().RegisterGameObject(*cube);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,6 +440,11 @@ void LightKeeper::customCleanup() {
|
||||
meshCache.cleanup(gfxDevice);
|
||||
}
|
||||
|
||||
void LightKeeper::customFixedUpdate(float dt)
|
||||
{
|
||||
SceneManager::GetInstance().FixedUpdate(dt);
|
||||
}
|
||||
|
||||
void LightKeeper::onWindowResize(int newWidth, int newHeight) {
|
||||
renderer.resize(gfxDevice, glm::ivec2{newWidth, newHeight});
|
||||
const float aspectRatio = static_cast<float>(newWidth) / static_cast<float>(newHeight);
|
||||
|
||||
Reference in New Issue
Block a user