Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
Gizmo.cpp
Go to the documentation of this file.
1#include "Gizmo.h"
2
3#include <iostream>
4#include <math/MathUtils.h>
7
8#include "ScaleHandle.h"
12
13Gizmo::Gizmo(GizmoType type, SceneManager* sceneManager, SceneObject *tgt) : target(tgt), ownerScene(sceneManager->scene), objectID(sceneManager->scene->allocateObjectID()){
14 sceneManager->addDrawable(this);
15 sceneManager->addPickable(this);
16
17 shader = ResourceManager::loadShader("assets/shaders/gizmo/gizmo.vert", "assets/shaders/gizmo/gizmo.frag", "gizmo");
18
19 switch (type) {
21 handleMesh = ResourceManager::getMesh("gizmo_translate");
22 handles.emplace_back(new TranslateHandle(target, Axis::X));
23 handles.emplace_back(new TranslateHandle(target, Axis::Y));
24 handles.emplace_back(new TranslateHandle(target, Axis::Z));
25 break;
27 handleMesh = ResourceManager::getMesh("gizmo_rotate");
28 handles.emplace_back(new RotateHandle(target, Axis::X));
29 handles.emplace_back(new RotateHandle(target, Axis::Y));
30 handles.emplace_back(new RotateHandle(target, Axis::Z));
31 break;
33 handleMesh = ResourceManager::getMesh("gizmo_scale");
34 handles.emplace_back(new ScaleHandle(target, Axis::X));
35 handles.emplace_back(new ScaleHandle(target, Axis::Y));
36 handles.emplace_back(new ScaleHandle(target, Axis::Z));
37 break;
38 }
39}
40
42 ownerScene->freeObjectID(objectID);
43}
44
45
46void Gizmo::draw() const {
47 glDisable(GL_DEPTH_TEST);
48 getShader()->use();
49
50 const glm::mat4 worldToRender = SceneObject::worldToRenderMatrix();
51 std::vector<Rendering::InstanceData> drawData;
52 for (const auto& handle : handles) {
53 glm::vec3 color = handle->getAxisDir(); // base RGB per axis
54
55 // highlight hovered or active handle
56 if (handle.get() == hoveredHandle || handle.get() == activeHandle) {
57 color = glm::mix(color, glm::vec3(1.0f), 0.7f); // lighten
58 }
59
60 const glm::mat4 renderModel = worldToRender * handle->getModelMatrix();
61 drawData.push_back({ renderModel, objectID, color });
62 }
63
64 getMesh()->drawInstanced(drawData);
65 glEnable(GL_DEPTH_TEST);
66}
67
68std::optional<float> Gizmo::intersectsRay(const Math::Ray& ray) const{
70
71 IHandle* bestHandle = nullptr;
72 float closestT = std::numeric_limits<float>::infinity();
73
74 for (const auto& handle: handles) {
75 auto worldAABB = localAABB.getTransformed(handle->getModelMatrix());
76 if (auto t = worldAABB->intersectRay(ray)) {
77 if (*t < closestT) {
78 closestT = *t;
79 bestHandle = handle.get();
80 }
81 }
82 }
83
84 hoveredHandle = bestHandle;
85
86 if (bestHandle)
87 return closestT;
88
89 return std::nullopt;
90}
91
92void Gizmo::handleClick(const Math::Ray& ray, float distance) {
93 if (!hoveredHandle)
94 return;
95
96 activeHandle = hoveredHandle;
97 activeHandle->setDragState(ray.origin + ray.dir * distance);
98 isDragging = true;
99}
100
102 isDragging = false;
103 activeHandle = nullptr;
104}
105
106void Gizmo::handleDrag(const Math::Ray& ray) {
107 if (!isDragging || !activeHandle)
108 return;
109
110 activeHandle->onDrag(ray);
111}
112
113
115 return shader;
116}
117
118void Gizmo::setHovered(bool hovered) {
119 isHovered = hovered;
120}
121
122bool Gizmo::getHovered() const {
123 return isHovered;
124}
125
126uint32_t Gizmo::getObjectID() const {
127 return objectID;
128}
@ Y
Definition Axis.h:8
@ X
Definition Axis.h:8
@ Z
Definition Axis.h:8
Interactive 3D manipulation gizmo for scene objects.
GizmoType
Types of transformation gizmos.
Definition Gizmo.h:21
Mathematical utility functions for 3D graphics and physics.
void handleClick(const Math::Ray &ray, float distance) override
Handles mouse click on a gizmo handle.
Definition Gizmo.cpp:92
Gizmo(GizmoType type, SceneManager *sceneManager, SceneObject *target)
Constructs a gizmo for the specified target object.
Definition Gizmo.cpp:13
~Gizmo()
Destructor - frees object IDs and cleans up handles.
Definition Gizmo.cpp:41
bool getHovered() const override
Gets the gizmo's hover state.
Definition Gizmo.cpp:122
uint32_t getObjectID() const override
Gets the gizmo's unique identifier.
Definition Gizmo.cpp:126
std::optional< float > intersectsRay(const Math::Ray &ray) const override
Tests if a ray intersects any of the gizmo's handles.
Definition Gizmo.cpp:68
void handleDrag(const Math::Ray &ray)
Updates the active handle during a drag operation.
Definition Gizmo.cpp:106
Mesh * getMesh() const override
Gets the shared mesh used for all handles.
Definition Gizmo.h:103
void draw() const override
Renders all handles with custom depth-test-disabled rendering.
Definition Gizmo.cpp:46
void handleRelease()
Handles mouse release - deactivates the active handle.
Definition Gizmo.cpp:101
void setHovered(bool hovered) override
Sets the gizmo's hover state.
Definition Gizmo.cpp:118
Shader * getShader() const override
Gets the shader used for gizmo rendering.
Definition Gizmo.cpp:114
Abstract interface for gizmo manipulation handles.
Definition IHandle.h:47
virtual void onDrag(const Math::Ray &ray)=0
Handles continuous dragging motion.
virtual void setDragState(glm::vec3 initHitPos)=0
Initializes the drag operation state.
const Physics::Bounding::AABB & getLocalAABB() const
Gets the local-space axis-aligned bounding box.
Definition Mesh.h:163
void drawInstanced(const std::vector< Rendering::InstanceData > &instances)
Draws multiple instances of this mesh in a single draw call.
Definition Mesh.cpp:78
std::unique_ptr< ICollider > getTransformed(const glm::mat4 &modelMatrix) const override
Creates a transformed copy of this collider in world space.
Definition AABB.cpp:11
static Mesh * getMesh(const std::string &name)
static Shader * loadShader(const std::string &vShaderPath, const std::string &fShaderPath, const std::string &name)
void addPickable(IPickable *obj)
void addDrawable(IDrawable *obj) const
static glm::mat4 worldToRenderMatrix()
Definition SceneObject.h:68
void freeObjectID(uint32_t objID)
Definition Scene.cpp:87
Definition Shader.h:6
void use() const
Definition Shader.cpp:55
glm::vec3 origin
Starting point of the ray in world space.
Definition Ray.h:12
glm::vec3 dir
Direction vector (should be normalized)
Definition Ray.h:13
Represents a ray in 3D space.
Definition Ray.h:11