ADD: PickingSystem with AABB
This commit is contained in:
parent
b879f2a636
commit
f6096e2889
@ -4,11 +4,11 @@
|
|||||||
|
|
||||||
#ifndef COLORRACE_PICKABLECOMPONENT_H
|
#ifndef COLORRACE_PICKABLECOMPONENT_H
|
||||||
#define COLORRACE_PICKABLECOMPONENT_H
|
#define COLORRACE_PICKABLECOMPONENT_H
|
||||||
#include "loader/models/AABB.h"
|
|
||||||
|
|
||||||
namespace engine {
|
namespace engine {
|
||||||
struct PickableComponent {
|
struct PickableComponent {
|
||||||
AABB aabb;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,7 @@
|
|||||||
#define GLM_ENABLE_EXPERIMENTAL
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "glm/gtx/string_cast.hpp"
|
#include "ecs/standardComponents/MeshComponent.h"
|
||||||
|
|
||||||
#include "renderer/entities/Camera.h"
|
#include "renderer/entities/Camera.h"
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
@ -32,20 +32,23 @@ namespace engine {
|
|||||||
|
|
||||||
//if (ImGui::GetIO().WantCaptureMouse) return std::nullopt;
|
//if (ImGui::GetIO().WantCaptureMouse) return std::nullopt;
|
||||||
if (!mouse.isClickEvent(MouseButton::LEFT)) return std::nullopt;
|
if (!mouse.isClickEvent(MouseButton::LEFT)) return std::nullopt;
|
||||||
std::cout << "PickingSystem::update" << std::endl;
|
|
||||||
auto [origin, dir] = screenToRay(mouse.getXY(/*ndc=*/true), m_camera);
|
auto [origin, dir] = screenToRay(mouse.getXY(/*ndc=*/true), m_camera);
|
||||||
printf("Ray: %f %f %f\n", origin.x, origin.y, origin.z);
|
|
||||||
spdlog::info("Ray: {} {}", glm::to_string(origin), glm::to_string(dir));
|
|
||||||
|
|
||||||
float closestT = std::numeric_limits<float>::max();
|
float closestT = std::numeric_limits<float>::max();
|
||||||
std::optional<Entity> hit;
|
std::optional<Entity> hit;
|
||||||
|
|
||||||
for (const auto& [entity, pickable] : registry.getPool<PickableComponent>()) {
|
for (const auto& [entity, pickable] : registry.getPool<PickableComponent>()) {
|
||||||
const auto transform = registry.getComponent<TransformComponent>(entity);
|
const auto transform = registry.getComponent<TransformComponent>(entity);
|
||||||
// if (float t; raySphereIntersect(origin, dir, transform.m_position, pickable.radius, t) && t < closestT) {
|
const auto mesh = registry.getComponent<MeshComponent>(entity);
|
||||||
// closestT = t;
|
|
||||||
// hit = entity;
|
AABB transformedAABB = mesh.model->getAABB().transformed(transform.computeModelMatrix());
|
||||||
// }
|
Ray ray = Ray(origin, dir);
|
||||||
|
std::optional<RayHit> ray_hit = transformedAABB.intersects(ray, closestT);
|
||||||
|
if (ray_hit.has_value()) {
|
||||||
|
spdlog::info(std::format("Hit: {} {}", entity.value(), std::to_string(ray_hit.value().distance)));
|
||||||
|
closestT = ray_hit.value().distance;
|
||||||
|
hit = entity;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return hit;
|
return hit;
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "DebugLayer.h"
|
#include "DebugLayer.h"
|
||||||
|
|
||||||
|
#include "ecs/standardComponents/MeshComponent.h"
|
||||||
#include "ecs/standardComponents/PickableComponent.h"
|
#include "ecs/standardComponents/PickableComponent.h"
|
||||||
#include "ecs/standardComponents/TransformComponent.h"
|
#include "ecs/standardComponents/TransformComponent.h"
|
||||||
#include "GLFW/glfw3.h"
|
#include "GLFW/glfw3.h"
|
||||||
@ -24,12 +25,14 @@ void engine::DebugLayer::onRender() {
|
|||||||
if (m_showDebugInfo) {
|
if (m_showDebugInfo) {
|
||||||
auto pickablePool = m_entityRegistry.getPool<PickableComponent>();
|
auto pickablePool = m_entityRegistry.getPool<PickableComponent>();
|
||||||
auto transforms = m_entityRegistry.getPool<engine::TransformComponent>();
|
auto transforms = m_entityRegistry.getPool<engine::TransformComponent>();
|
||||||
|
|
||||||
for (auto& [entity, pickableComponent] : pickablePool) {
|
for (auto& [entity, pickableComponent] : pickablePool) {
|
||||||
if (transforms.has(entity)) {
|
if (transforms.has(entity)) {
|
||||||
glm::vec3 halfExtent = pickableComponent.aabb.getHalfExtent();
|
auto meshComponent = m_entityRegistry.getComponent<engine::MeshComponent>(entity);
|
||||||
spdlog::info(std::to_string(halfExtent.x) + " " + std::to_string(halfExtent.y) + " " + std::to_string(halfExtent.z));
|
glm::vec3 halfExtent = meshComponent.model.get()->getAABB().getHalfExtent();
|
||||||
const auto sphereTransformComp = TransformComponent(transforms.get(entity).m_position, {0,0,0}, halfExtent);
|
//spdlog::info(std::to_string(halfExtent.x) + " " + std::to_string(halfExtent.y) + " " + std::to_string(halfExtent.z));
|
||||||
|
|
||||||
|
AABB transformedAABB = meshComponent.model->getAABB().transformed(transforms.get(entity).computeModelMatrix());
|
||||||
|
const auto sphereTransformComp = TransformComponent(transforms.get(entity).m_position, {0,0,0}, transformedAABB.getHalfExtent());
|
||||||
glm::mat4 modelMatrix = sphereTransformComp.computeModelMatrix();
|
glm::mat4 modelMatrix = sphereTransformComp.computeModelMatrix();
|
||||||
|
|
||||||
RenderCommand renderCommand(m_sphereModel, modelMatrix);
|
RenderCommand renderCommand(m_sphereModel, modelMatrix);
|
||||||
|
|||||||
@ -19,7 +19,7 @@ void ludo::PiecePresenter::spawnPieces(const ludo::LudoGameState &state) {
|
|||||||
{0.334296f, 0.334296f, 0.334296f});
|
{0.334296f, 0.334296f, 0.334296f});
|
||||||
m_registry.addComponent<engine::TransformComponent>(entity, transform);
|
m_registry.addComponent<engine::TransformComponent>(entity, transform);
|
||||||
m_registry.addComponent<engine::MeshComponent>(entity, engine::MeshComponent(m_pieceModels[std::string(ludo::toString(piece.color))]));
|
m_registry.addComponent<engine::MeshComponent>(entity, engine::MeshComponent(m_pieceModels[std::string(ludo::toString(piece.color))]));
|
||||||
m_registry.addComponent<engine::PickableComponent>(entity, engine::PickableComponent(m_pieceModels[std::string(toString(piece.color))]->getAABB()));
|
m_registry.addComponent<engine::PickableComponent>(entity, engine::PickableComponent());
|
||||||
m_pieceEntities.push_back(entity);
|
m_pieceEntities.push_back(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user