summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2023-11-07 16:32:51 -0500
committerJonathan Bradley <jcb@pikum.xyz>2023-11-15 13:23:41 -0500
commitbdcaefc871fe8efcfa3920109e8a52d6a194955d (patch)
treecabc444b7acc6d7ca97341798466c5d3d68ad526
parent546e179d6095d07342b9b8f29f3b20eaaf3cf274 (diff)
display debug box for hovered entity
-rw-r--r--src/game.cpp51
-rw-r--r--src/window.cpp16
-rw-r--r--src/window.hpp4
3 files changed, 70 insertions, 1 deletions
diff --git a/src/game.cpp b/src/game.cpp
index cd27812..bd13a22 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -15,9 +15,9 @@
#include "window.hpp"
#include <BulletCollision/CollisionShapes/btConvexHullShape.h>
+#include <BulletCollision/NarrowPhaseCollision/btRaycastCallback.h>
#include <GLFW/glfw3.h>
#include <cstring>
-#include <glm/gtc/quaternion.hpp>
#include <iomanip>
#include <ostream>
@@ -491,6 +491,22 @@ void teardownEditor() {
PkeInput_DeactivateSet(debugControlsHandle);
}
+glm::vec3 unproject(glm::vec3 windowCoords) {
+ double xDevNorm = (2.0f * windowCoords.x) / Extent.width - 1.0f;
+ double yDevNorm = 1.0f - (2.0f * windowCoords.y) / Extent.height;
+ yDevNorm *= -1;
+ glm::vec4 clipCoords(xDevNorm, yDevNorm, windowCoords.z, 1.f);
+
+ glm::vec4 rayEye = glm::inverse(UBO.proj) * clipCoords;
+
+ glm::vec4 worldCoords = glm::inverse(UBO.model) * glm::inverse(UBO.view) * rayEye;
+ if (std::abs(0.0f - worldCoords.w) > 0.00001f) {
+ worldCoords *= 1.0f / worldCoords.w;
+ }
+
+ return glm::vec3(worldCoords);
+}
+
void Game_Tick(double delta) {
/*
* ECS_Tick() gets called first because it updates the public
@@ -512,6 +528,39 @@ void Game_Tick(double delta) {
ActiveCamera = &cameraDbg;
}
ECS_Tick_Early(delta);
+
+ // raycast for hovering
+ do // while(0)
+ {
+ double xMousePos, yMousePos;
+ glfwGetCursorPos(window, &xMousePos, &yMousePos);
+
+ glm::vec3 fromCoords{unproject(glm::vec3(xMousePos, yMousePos, -1.f))};
+ glm::vec3 toCoords{unproject(glm::vec3(xMousePos, yMousePos, 1.f))};
+
+ btVector3 rayOrigin{};
+ btVector3 rayDestination{};
+ GlmToBullet(fromCoords, rayOrigin);
+ GlmToBullet(toCoords, rayDestination);
+
+ btCollisionWorld::ClosestRayResultCallback rayResult{rayOrigin, rayDestination};
+ rayResult.m_flags |= btTriangleRaycastCallback::kF_FilterBackfaces;
+
+ BtDynamicsWorld->rayTest(rayOrigin, rayDestination, rayResult);
+ if (rayResult.hasHit()) {
+ EntityHandle_T hoveredEntity_T{reinterpret_cast<EntityHandle_T>(rayResult.m_collisionObject->getUserPointer())};
+ EntityHandle hoveredEntity{hoveredEntity_T};
+ const auto *inst = ECS_GetInstance(hoveredEntity);
+ const auto *grBinds = ECS_GetGrBinds(inst->grBindsHandle);
+ pkeDebugHitbox.instanceBuffer = grBinds->instanceBuffer;
+ pkeDebugHitbox.instanceStartingIndex = inst->index;
+ } else {
+ pkeDebugHitbox.instanceBuffer = VK_NULL_HANDLE;
+ pkeDebugHitbox.instanceStartingIndex = 0;
+ }
+ }
+ while (0);
+
if (shouldCreateEntityType) {
assert(entityTypeToCreate != nullptr);
assert(entityTypeToCreate != CAFE_BABE(EntityType));
diff --git a/src/window.cpp b/src/window.cpp
index bb83b32..6e285b5 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -1877,6 +1877,7 @@ void CreateGraphicsPipelines() {
dbgModelBufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
vkCreateBuffer(vkDevice, &dbgModelBufferCI, vkAllocator, &pkeDebugHitbox.indexBuffer);
vkGetBufferMemoryRequirements(vkDevice, pkeDebugHitbox.indexBuffer, &vkMemoryRequirements[index]);
+ pkeDebugHitbox.indexCount = 36;
// index++;
VkMemoryRequirements combinedMemReqs{};
@@ -2305,6 +2306,21 @@ void RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) {
}
}
+ if (pkeDebugHitbox.instanceBuffer != VK_NULL_HANDLE) {
+ vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkePipelines.pipelines.TextureWireframe);
+ vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pkePipelines.vkPipelineLayout_Texture, 0, 1, &pkeDebugHitbox.vkDescriptorSets[imageIndex], 0, {});
+ vkCmdBindVertexBuffers(commandBuffer, 0, 1, &UniformBuffers[imageIndex], offsets);
+
+ // TODO don't hardcode firstBinding
+ vkCmdBindVertexBuffers(commandBuffer, 0, 1, &pkeDebugHitbox.vertexBuffer, offsets);
+ vkCmdBindVertexBuffers(commandBuffer, 1, 1, &pkeDebugHitbox.normalsBuffer, offsets);
+ vkCmdBindVertexBuffers(commandBuffer, 2, 1, &pkeDebugHitbox.uvBuffer, offsets);
+ vkCmdBindIndexBuffer(commandBuffer, pkeDebugHitbox.indexBuffer, offsets[0], VK_INDEX_TYPE_UINT16);
+
+ vkCmdBindVertexBuffers(commandBuffer, 3, 1, &pkeDebugHitbox.instanceBuffer, offsets);
+ vkCmdDrawIndexed(commandBuffer, pkeDebugHitbox.indexCount, pkeDebugHitbox.instanceCount, 0, 0, pkeDebugHitbox.instanceStartingIndex);
+ }
+
// present pass
vkCmdEndRenderPass(commandBuffer);
diff --git a/src/window.hpp b/src/window.hpp
index 5f640f9..7f42766 100644
--- a/src/window.hpp
+++ b/src/window.hpp
@@ -68,6 +68,10 @@ struct DebugHitbox {
VkBuffer normalsBuffer;
VkBuffer uvBuffer;
VkBuffer indexBuffer;
+ VkBuffer instanceBuffer = VK_NULL_HANDLE;
+ uint32_t indexCount = 0;
+ uint32_t instanceCount = 1;
+ uint32_t instanceStartingIndex = 0;
VkDescriptorPool vkDescriptorPool;
VkDescriptorSet *vkDescriptorSets = nullptr;
};