cgv
Loading...
Searching...
No Matches
gizmo.h
1#pragma once
2
3#include <cgv/base/node.h>
4#include <cgv/gui/event_handler.h>
5#include <cgv/math/fvec.h>
6#include <cgv/math/quaternion.h>
7#include <cgv/math/ray.h>
8#include <cgv/render/drawable.h>
9#include <cgv/render/view.h>
10
11#include "lib_begin.h"
12
13namespace cgv {
14namespace app {
15
16struct plane {
17 vec3 origin = { 0.0f };
18 vec3 normal = { 0.0f };
19
20 bool valid() const {
21 return length(normal) > std::numeric_limits<float>::epsilon();
22 }
23};
24
25enum class GizmoAction {
26 kDragStart,
27 kDrag,
28 kDragEnd
29};
30
31enum class GizmoOrientation {
32 kGlobal,
33 kLocal
34};
35
36class CGV_API gizmo :
37 public cgv::base::node,
40public:
41 std::string get_type_name() const override { return "gizmo"; }
42
43 bool init(cgv::render::context&) override = 0;
44
45 void clear(cgv::render::context&) override = 0;
46
47 void finish_frame(cgv::render::context&) override;
48
49 bool handle(cgv::gui::event& e) override;
50
51 void stream_help(std::ostream& os) override {};
52
53 GizmoOrientation get_orientation() const;
54
55 void set_orientation(GizmoOrientation orientation);
56
57 vec3 get_position() const;
58
59 void set_position(const vec3& position);
60
61 quat get_rotation() const;
62
63 void set_rotation(const quat& rotation);
64
65 float size_scale = 1.0f;
66 bool keep_screen_size_constant = true;
67 bool lock_size_during_interaction = false;
68
69protected:
70 enum class InteractionFeature {
71 kNone = 0,
72 kAxis,
73 kPlane,
74 kCenter
75 };
76
77 enum class AxisId {
78 kX = 0,
79 kY = 1,
80 kZ = 2
81 };
82
83 InteractionFeature _interaction_feature = InteractionFeature::kNone;
84 AxisId _interaction_axis_id = AxisId::kX;
85 plane _interaction_plane;
86 cgv::math::ray3 _drag_start_ray;
87 float _drag_start_t = 0.0f;
88
89 float get_size() const;
90
91 const cgv::render::view* get_view() const;
92
93 void set_geometry_out_of_date();
94
95 bool captured_mouse() const;
96
97 bool is_hovered() const;
98
99 int axis_id_to_index(AxisId axis) const;
100
101 AxisId index_to_axis_id(int idx) const;
102
103 vec3 get_axis(int index) const;
104
105 vec3 get_axis_mask(InteractionFeature feature, AxisId axis_id) const;
106
107private:
108 virtual void create_geometry() = 0;
109
110 virtual void draw_geometry(cgv::render::context& ctx) = 0;
111
112 virtual bool intersect_bounding_box(const cgv::math::ray3& ray) { return true; }
113
114 virtual bool intersect(const cgv::math::ray3& ray) { return false; }
115
116 virtual bool start_drag(const cgv::math::ray3& ray) { return false; }
117
118 virtual bool drag(const cgv::math::ray3& ray) { return false; }
119
120 virtual void end_drag(const cgv::math::ray3& ray) {}
121
122 cgv::render::view* _view = nullptr;
123
124 bool _geometry_out_of_date = true;
125 float _size = -1.0f;
126
127 bool _captured_mouse = false;
128 bool _hovered = false;
129
130 vec3 _position = { 0.0f };
131 quat _rotation = {};
132 GizmoOrientation _orientation = GizmoOrientation::kGlobal;
133};
134
135}
136}
137
138#include <cgv/config/lib_end.h>
void stream_help(std::ostream &os) override
overload to stream help information to the given output stream
Definition gizmo.h:51
void clear(cgv::render::context &) override=0
clear all objects living in the context like textures or display lists
std::string get_type_name() const override
overload to return the type name of this object. By default the type interface is queried over get_ty...
Definition gizmo.h:41
bool init(cgv::render::context &) override=0
this method is called after creation or recreation of the context, return whether all necessary funct...
The node class keeps a pointer to its parent.
Definition node.h:19
interface for all classes that want to receive events
This class defines a template for n-dimensional rays with arbitrary data type defined by origin and d...
Definition ray.h:14
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
base class for all drawables, which is independent of the used rendering API.
Definition drawable.h:15
defines a symmetric view with the following quantities:
Definition view.h:22
the cgv namespace
Definition print.h:11
cgv::math::quaternion< float > quat
declare type of quaternion
Definition quaternion.h:370
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:669