diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-10-24 12:28:59 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-15 13:11:11 -0500 |
| commit | bd924c70c7c6e98b53c784f3b06f6b315741b8d0 (patch) | |
| tree | 5577e24b644063918cd95e721acb00b49530aebc | |
| parent | 52d7270ba0a532a759a87ee29043c136811b8736 (diff) | |
checkpoint - remove InstPos from CompInstance
| -rw-r--r-- | src/components.hpp | 1 | ||||
| -rw-r--r-- | src/ecs.cpp | 87 | ||||
| -rw-r--r-- | src/ecs.hpp | 2 | ||||
| -rw-r--r-- | src/game.cpp | 72 |
4 files changed, 99 insertions, 63 deletions
diff --git a/src/components.hpp b/src/components.hpp index 3e7521f..f10dd63 100644 --- a/src/components.hpp +++ b/src/components.hpp @@ -65,7 +65,6 @@ struct CompInstance { GrBindsHandle grBindsHandle = GrBindsHandle_MAX; InstanceHandle instanceHandle = InstanceHandle_MAX; uint64_t index = ECS_UNSET_VAL; - InstPos instPos; struct { btVector3 localInertia; btCollisionShape *collisionShape; diff --git a/src/ecs.cpp b/src/ecs.cpp index 831adfb..9de5c44 100644 --- a/src/ecs.cpp +++ b/src/ecs.cpp @@ -2,16 +2,13 @@ #include "ecs.hpp" #include "game-settings.hpp" +#include "math-helpers.hpp" #include "memory.hpp" #include "vendor/glm_include.hpp" #include "window.hpp" #include <btBulletDynamicsCommon.h> - -#define GLM_TO_BULLET_VEC_3(vec) btVector3(vec.x, vec.y, vec.z) -#define GLM_TO_BULLET_QUAT(quat) btQuaternion(quat.x, quat.y, quat.z, quat.w) -#define BULLET_TO_GLM_VEC_3(vec) glm::vec3(vec.getX(), vec.getY(), vec.getZ()) -#define BULLET_TO_GLM_QUAT(qt) glm::quat(qt.getW(), qt.getX(), qt.getY(), qt.getZ()) +#include <glm/gtc/type_ptr.hpp> TypeSafeInt_B(EntityHandle); const uint64_t maxBucketItemCount = 256; @@ -193,14 +190,11 @@ void ECS_Tick(double delta) { long count = Comp_Instance_BucketContainer.bucketCounter == b ? Comp_Instance_BucketContainer.itemCounter >> 32 : maxBucketItemCount; for (uint32_t i = 0; i < count; ++i) { auto &inst = bkt.instances[i]; - btTransform trans; - inst.bt.defaultMotionState.getWorldTransform(trans); - - InstPos instPos = inst.instPos; - instPos.pos = BULLET_TO_GLM_VEC_3(trans.getOrigin()); - instPos.rot = BULLET_TO_GLM_QUAT(trans.getRotation()); - // TODO add an _INNER since we have a reference to the object that this looks up - ECS_UpdateInstance(inst.entHandle, instPos, false); + auto activationState = inst.bt.rigidBody->getActivationState(); + if (activationState == ISLAND_SLEEPING || activationState == DISABLE_SIMULATION || activationState == WANTS_DEACTIVATION) { + continue; + } + inst.isNeedingUpdated = true; } } } @@ -263,10 +257,16 @@ void ECS_Tick_Late(double delta) { chunk->dstBufferCopy.dstOffset = sizeof(glm::mat4) * inst.index; } - chunk->mats.Push( - glm::translate(glm::mat4(1), inst.instPos.pos) - * glm::mat4_cast(inst.instPos.rot) - * glm::scale(glm::mat4(1), inst.instPos.scale)); + btTransform btMatrix_posRot; + inst.bt.defaultMotionState.getWorldTransform(btMatrix_posRot); + float openglMatrix[16]; + btMatrix_posRot.getOpenGLMatrix(openglMatrix); + glm::mat4 glmMat_posRot = glm::make_mat4(openglMatrix); + + glm::vec3 scale; + BulletToGlm(inst.bt.collisionShape->getLocalScaling(), scale); + + chunk->mats.Push(glm::scale(glmMat_posRot, scale)); bfrUpdate->runningSize += sizeof(glm::mat4); inst.isNeedingUpdated = false; } @@ -365,7 +365,7 @@ CompGrBinds *ECS_GetGrBinds(uint64_t bucketIndex, uint64_t &itemCount) { return Comp_GrBinds_BucketContainer.buckets[bucketIndex].compGrBinds; } -CompInstance &ECS_CreateInstance(EntityHandle entHandle, EntityHandle entityTypeEntityHandle) { +CompInstance &ECS_CreateInstance(EntityHandle entHandle, EntityHandle entityTypeEntityHandle, const InstPos &instPos) { assert(entHandle != EntityHandle_MAX); assert(entityTypeEntityHandle != EntityHandle_MAX); @@ -395,9 +395,6 @@ CompInstance &ECS_CreateInstance(EntityHandle entHandle, EntityHandle entityType comp->grBindsHandle = grBinds->grBindsHandle; comp->instanceHandle = ent->instanceHandle; comp->index = grBinds->instanceCounter++; - comp->instPos.pos = glm::vec3(0, 0, 0); - comp->instPos.rot = glm::quat(1, 0, 0, 0); - comp->instPos.scale = glm::vec3(1, 1, 1); comp->isNeedingUpdated = true; if (grBinds->instanceCounter > grBinds->instanceBufferMaxCount) { @@ -406,15 +403,24 @@ CompInstance &ECS_CreateInstance(EntityHandle entHandle, EntityHandle entityType if (btDynamicsWorld) { comp->bt.localInertia = btVector3(0, 0, 0); + btVector3 scale; + GlmToBullet(instPos.scale, scale); comp->bt.collisionShape = Pke_New<btBoxShape>(bulletBucket); - new (comp->bt.collisionShape) btBoxShape(GLM_TO_BULLET_VEC_3(comp->instPos.scale)); + new (comp->bt.collisionShape) btBoxShape(scale); comp->bt.collisionShape->calculateLocalInertia(btScalar(1.f), comp->bt.localInertia); btTransform transform; transform.setIdentity(); - transform.setOrigin(GLM_TO_BULLET_VEC_3(comp->instPos.pos)); + btVector3 origin; + GlmToBullet(instPos.pos, origin); + transform.setOrigin(origin); + btQuaternion rot; + GlmToBullet(instPos.rot, rot); + transform.setRotation(rot); comp->bt.defaultMotionState = btDefaultMotionState(transform); comp->bt.rigidBody = Pke_New<btRigidBody>(bulletBucket); new (comp->bt.rigidBody) btRigidBody(btScalar(1.f), &comp->bt.defaultMotionState, comp->bt.collisionShape, comp->bt.localInertia); + comp->bt.rigidBody->setLinearVelocity(btVector3(0,0,0)); + comp->bt.rigidBody->setAngularVelocity(btVector3(0,0,0)); btDynamicsWorld->addRigidBody(comp->bt.rigidBody); comp->bt.rigidBody->setUserPointer(reinterpret_cast<void *>(comp->entHandle)); } @@ -449,24 +455,29 @@ void ECS_UpdateInstance(EntityHandle entHandle, const InstPos &instPos, bool ove auto i = Buckets_GetItemIndex(instanceHandle_t); auto *inst = &Comp_Instance_BucketContainer.buckets[b].instances[i]; - if (overridePhysics == false - && instPos.pos == inst->instPos.pos - && instPos.rot == inst->instPos.rot - && instPos.scale == inst->instPos.scale) { - return; - } - - inst->instPos.pos = instPos.pos; - inst->instPos.rot = instPos.rot; - inst->instPos.scale = instPos.scale; - inst->isNeedingUpdated = true; - - if (overridePhysics == true && btDynamicsWorld) { + if (btDynamicsWorld && overridePhysics) { btTransform transform; transform.setIdentity(); - transform.setOrigin(GLM_TO_BULLET_VEC_3(inst->instPos.pos)); - transform.setRotation(GLM_TO_BULLET_QUAT(inst->instPos.rot)); + + btVector3 origin; + GlmToBullet(instPos.pos, origin); + transform.setOrigin(origin); + + btQuaternion rot; + GlmToBullet(instPos.rot, rot); + transform.setRotation(rot); inst->bt.rigidBody->setWorldTransform(transform); + + btVector3 scale; + GlmToBullet(instPos.scale, scale); + inst->bt.collisionShape->setLocalScaling(scale); + + inst->bt.rigidBody->setLinearVelocity(btVector3(0,0,0)); + inst->bt.rigidBody->setAngularVelocity(btVector3(0,0,0)); + btDynamicsWorld->getPairCache()->cleanProxyFromPairs(inst->bt.rigidBody->getBroadphaseProxy(), btDynamicsWorld->getDispatcher()); + inst->bt.rigidBody->activate(); + + inst->isNeedingUpdated = true; } } diff --git a/src/ecs.hpp b/src/ecs.hpp index 480ba0c..4a10b3a 100644 --- a/src/ecs.hpp +++ b/src/ecs.hpp @@ -32,7 +32,7 @@ CompGrBinds *ECS_GetGrBinds(EntityHandle entHandle); uint64_t ECS_GetGrBinds_BucketCount(); CompGrBinds *ECS_GetGrBinds(uint64_t bucketIndex, uint64_t &itemCount); -CompInstance &ECS_CreateInstance(EntityHandle entHandle, EntityHandle entityTypeEntityHandle); +CompInstance &ECS_CreateInstance(EntityHandle entHandle, EntityHandle entityTypeEntityHandle, const InstPos &instPos); CompInstance *ECS_GetInstance(EntityHandle entHandle); void ECS_UpdateInstance(EntityHandle entHandle, const InstPos &instPos, bool overridePhysics = false); uint64_t ECS_GetInstances_BucketCount(); diff --git a/src/game.cpp b/src/game.cpp index a1ba28b..e1accc9 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -8,12 +8,14 @@ #include "game-settings.hpp" #include "helpers.hpp" #include "imgui.h" +#include "math-helpers.hpp" #include "player-input.hpp" #include "vendor/glm_include.hpp" #include "window.hpp" #include <GLFW/glfw3.h> #include <cstring> +#include <glm/gtc/quaternion.hpp> #include <iomanip> const uint64_t consoleBufferCount = 30; @@ -127,25 +129,36 @@ void SerializeInstance(std::ofstream &stream, const CompInstance &comp) { } assert(et != nullptr); CompInstance c{}; + InstPos baseInst{}; + baseInst.pos = glm::vec3(0, 0, 0); + baseInst.rot = glm::quat(1, 0, 0, 0); + baseInst.scale = glm::vec3(1, 1, 1); if (comp.entHandle != c.entHandle) stream << PKE_FILE_INSTANCE_ENTITY_HANDLE << handleStr << std::endl; stream << PKE_FILE_INSTANCE_ENTITY_TYPE_CODE << et->entityTypeCode << std::endl; - if (comp.instPos.pos != c.instPos.pos) + + btTransform trans; + comp.bt.defaultMotionState.getWorldTransform(trans); + InstPos inst; + BulletToGlm(trans.getOrigin(), inst.pos); + BulletToGlm(trans.getRotation(), inst.rot); + BulletToGlm(comp.bt.collisionShape->getLocalScaling(), inst.scale); + if (inst.pos != baseInst.pos) stream << PKE_FILE_INSTANCE_POS_POS << "[" - << std::setw(10) << comp.instPos.pos[0] << "," - << std::setw(10) << comp.instPos.pos[1] << "," - << std::setw(10) << comp.instPos.pos[2] << "]" << std::endl; - if (comp.instPos.rot != c.instPos.rot) + << std::setw(10) << inst.pos[0] << "," + << std::setw(10) << inst.pos[1] << "," + << std::setw(10) << inst.pos[2] << "]" << std::endl; + if (inst.rot != baseInst.rot) stream << PKE_FILE_INSTANCE_POS_ROT << "[" - << std::setw(10) << comp.instPos.rot[0] << "," - << std::setw(10) << comp.instPos.rot[1] << "," - << std::setw(10) << comp.instPos.rot[2] << "," - << std::setw(10) << comp.instPos.rot[3] << "]" << std::endl; - if (comp.instPos.scale != c.instPos.scale) + << std::setw(10) << inst.rot[0] << "," + << std::setw(10) << inst.rot[1] << "," + << std::setw(10) << inst.rot[2] << "," + << std::setw(10) << inst.rot[3] << "]" << std::endl; + if (inst.scale != baseInst.scale) stream << PKE_FILE_INSTANCE_POS_SCALE << "[" - << std::setw(10) << comp.instPos.scale[0] << "," - << std::setw(10) << comp.instPos.scale[1] << "," - << std::setw(10) << comp.instPos.scale[2] << "]" << std::endl; + << std::setw(10) << inst.scale[0] << "," + << std::setw(10) << inst.scale[1] << "," + << std::setw(10) << inst.scale[2] << "]" << std::endl; } void ParseEntityType(std::ifstream &stream) { @@ -231,6 +244,10 @@ void ParseEntityType(std::ifstream &stream) { void ParseInstance(std::ifstream &stream) { CompInstance comp{}; + InstPos instPos{}; + instPos.pos = glm::vec3(0, 0, 0); + instPos.rot = glm::quat(1, 0, 0, 0); + instPos.scale = glm::vec3(1, 1, 1); char entTypeCode[21]; memset(reinterpret_cast<void *>(entTypeCode), '\0', 21); while (stream.getline(readLine, readLineLength)) { @@ -246,8 +263,7 @@ void ParseInstance(std::ifstream &stream) { } const auto &et = GlobalEntityTypes[existingEntityTypeIndex]; auto entityHandle = ECS_CreateEntity(); - ECS_CreateInstance(entityHandle, et.entityHandle); - ECS_UpdateInstance(entityHandle, comp.instPos, true); + ECS_CreateInstance(entityHandle, et.entityHandle, instPos); break; } if (strstr(readLine, PKE_FILE_INSTANCE_ENTITY_HANDLE)) { @@ -272,7 +288,7 @@ void ParseInstance(std::ifstream &stream) { long index = 0; do { assert(index < 3); - STR2NUM_ERROR result = str2num(comp.instPos.pos[index], startingChar, pEnd); + STR2NUM_ERROR result = str2num(instPos.pos[index], startingChar, pEnd); assert(result == STR2NUM_ERROR::SUCCESS); startingChar = pEnd + 1; ++index; @@ -287,7 +303,7 @@ void ParseInstance(std::ifstream &stream) { long index = 0; do { assert(index < 4); - STR2NUM_ERROR result = str2num(comp.instPos.rot[index], startingChar, pEnd); + STR2NUM_ERROR result = str2num(instPos.rot[index], startingChar, pEnd); assert(result == STR2NUM_ERROR::SUCCESS); startingChar = pEnd + 1; ++index; @@ -302,7 +318,7 @@ void ParseInstance(std::ifstream &stream) { long index = 0; do { assert(index < 3); - STR2NUM_ERROR result = str2num(comp.instPos.scale[index], startingChar, pEnd); + STR2NUM_ERROR result = str2num(instPos.scale[index], startingChar, pEnd); assert(result == STR2NUM_ERROR::SUCCESS); startingChar = pEnd + 1; ++index; @@ -420,7 +436,11 @@ void Game_Tick(double delta) { while (entityInstancesToCreate.Count() > 0) { auto createInfo = entityInstancesToCreate.Pop(); EntityHandle newEntity = ECS_CreateEntity(); - ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle); + InstPos instPos; + instPos.pos = glm::vec3(0, 0, 0); + instPos.rot = glm::quat(1, 0, 0, 0); + instPos.scale = glm::vec3(1); + ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle, instPos); } PkeInput_Tick(delta); @@ -810,14 +830,20 @@ void RecordImGui_CompInstPos(bool readonly, CompInstance *component) { ImGui::Separator(); bool changed = false; + btTransform trans; + component->bt.defaultMotionState.getWorldTransform(trans); + InstPos instPos; + BulletToGlm(trans.getOrigin(), instPos.pos); + BulletToGlm(trans.getRotation(), instPos.rot); + BulletToGlm(component->bt.collisionShape->getLocalScaling(), instPos.scale); changed = ImGui::InputScalar("Instance Index", ImGuiDataType_U64, &component->index, nullptr, nullptr, nullptr, ImGuiInputTextFlags_ReadOnly) || changed; - changed = ImGui::InputScalarN("pos", ImGuiDataType_Float, &component->instPos.pos, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed; - changed = ImGui::InputScalarN("rot", ImGuiDataType_Float, &component->instPos.rot, 4, nullptr, nullptr, nullptr, inputTextFlags) || changed; - changed = ImGui::InputScalarN("scale", ImGuiDataType_Float, &component->instPos.scale, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed; + changed = ImGui::InputScalarN("pos", ImGuiDataType_Float, &instPos.pos, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed; + changed = ImGui::InputScalarN("rot", ImGuiDataType_Float, &instPos.rot, 4, nullptr, nullptr, nullptr, inputTextFlags) || changed; + changed = ImGui::InputScalarN("scale", ImGuiDataType_Float, &instPos.scale, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed; if (changed) { - ECS_UpdateInstance(component->entHandle, component->instPos, true); + ECS_UpdateInstance(component->entHandle, instPos, true); } ImGui::Spacing(); |
