cgv
Loading...
Searching...
No Matches
gizmo.cxx
1#include "gizmo.h"
2
3#include <cgv/gui/mouse_event.h>
4#include <cgv/math/ftransform.h>
5#include <cgv/math/intersection.h>
6
7using namespace cgv::render;
8
9namespace cgv {
10namespace app {
11
13 if(!_view)
14 _view = find_view_as_node();
15
16 if(!_view)
17 return;
18
19 if(_geometry_out_of_date) {
20 create_geometry();
21 _geometry_out_of_date = false;
22 }
23
24 if(keep_screen_size_constant) {
25 float adjusted_size = 0.3f * size_scale * static_cast<float>(_view->get_tan_of_half_of_fovy(true)) * dot(_position - _view->get_eye(), _view->get_view_dir());
26
27 if(lock_size_during_interaction) {
28 if(!_captured_mouse || _size < 0.0f)
29 _size = adjusted_size;
30 } else {
31 _size = adjusted_size;
32 }
33 } else {
34 _size = size_scale;
35 }
36
38 ctx.mul_modelview_matrix(cgv::math::translate4(_position));
39 ctx.mul_modelview_matrix(cgv::math::scale4(_size));
40
41 if(_orientation == GizmoOrientation::kLocal)
43
46
47 ctx.push_blend_state();
48 ctx.enable_blending();
50
51 draw_geometry(ctx);
52
53 ctx.pop_blend_state();
54
56
58}
59
62 return false;
63
64 cgv::gui::mouse_event& me = dynamic_cast<cgv::gui::mouse_event&>(e);
65
66 context* ctx = get_context();
67 if(!_view || !ctx)
68 return false;
69
70 const ivec4& viewport = ctx->get_window_transformation_array().front().viewport;
71 vec2 viewport_size = vec2(viewport.z(), viewport.w());
72
73 // get the mouse position in GL space
74 ivec2 mouse_pos(
75 static_cast<int>(me.get_x()),
76 viewport_size.y() - static_cast<int>(me.get_y()) - 1
77 );
78
79 cgv::math::ray3 ray(vec2(mouse_pos), viewport_size, _view->get_eye(), ctx->get_projection_matrix() * ctx->get_modelview_matrix());
80
81 bool evaluate_hover_state = false;
82
83 switch(me.get_action()) {
85 evaluate_hover_state = !_captured_mouse;
86 break;
88 if(_hovered && _interaction_feature != InteractionFeature::kNone) {
89 if(start_drag(ray)) {
90 _drag_start_ray = ray;
91 _captured_mouse = true;
92 return true;
93 }
94 }
95 break;
97 if(_captured_mouse) {
98 end_drag(ray);
99 _captured_mouse = false;
100 evaluate_hover_state = true;
101 }
102 break;
104 if(_captured_mouse && drag(ray))
105 return true;
106 break;
107 default:
108 break;
109 }
110
111 if(evaluate_hover_state) {
112 // transform ray to gizmo object space
113 cgv::math::ray3 ray_object = ray;
114 ray_object.origin -= _position;
115 ray_object.origin /= _size;
116
117 if(_orientation == GizmoOrientation::kLocal) {
118 _rotation.inverse_rotate(ray_object.origin);
119 _rotation.inverse_rotate(ray_object.direction);
120 }
121
122 bool was_hovered = _hovered;
123 InteractionFeature last_feature = _interaction_feature;
124 AxisId last_axis_id = _interaction_axis_id;
125
126 _hovered = intersect_bounding_box(ray_object) && intersect(ray_object);
127
128 if(was_hovered != _hovered || last_feature != _interaction_feature || last_axis_id != _interaction_axis_id) {
129 set_geometry_out_of_date();
130 post_redraw();
131 }
132 }
133
134 return false;
135}
136
137GizmoOrientation gizmo::get_orientation() const {
138 return _orientation;
139}
140
141void gizmo::set_orientation(GizmoOrientation orientation) {
142 _orientation = orientation;
143 post_redraw();
144}
145
146vec3 gizmo::get_position() const {
147 return _position;
148}
149
150void gizmo::set_position(const vec3& position) {
151 _position = position;
152 post_redraw();
153}
154
155quat gizmo::get_rotation() const {
156 return _rotation;
157}
158
159void gizmo::set_rotation(const quat& rotation) {
160 _rotation = rotation;
161 post_redraw();
162}
163
164float gizmo::get_size() const {
165 return _size;
166}
167
168const cgv::render::view* gizmo::get_view() const {
169 return _view;
170}
171
172void gizmo::set_geometry_out_of_date() {
173 _geometry_out_of_date = true;
174}
175
176bool gizmo::captured_mouse() const {
177 return _captured_mouse;
178}
179
180bool gizmo::is_hovered() const {
181 return _hovered;
182}
183
184vec3 gizmo::get_axis(int index) const {
185 assert(index >= 0 && index < 3);
186 vec3 axis(0.0f);
187 axis[index] = 1.0f;
188 return axis;
189}
190
191vec3 gizmo::get_axis_mask(InteractionFeature feature, AxisId axis_id) const {
192 switch(feature) {
193 case InteractionFeature::kAxis:
194 return get_axis(axis_id_to_index(axis_id));
195 case InteractionFeature::kPlane:
196 return vec3(1.0f) - get_axis(axis_id_to_index(axis_id));
197 case InteractionFeature::kCenter:
198 return vec3(1.0f);
199 default:
200 return vec3(0.0f);
201 }
202}
203
204int gizmo::axis_id_to_index(AxisId axis) const {
205 return static_cast<int>(axis);
206}
207
208gizmo::AxisId gizmo::index_to_axis_id(int idx) const {
209 assert(idx >= 0 && idx < 3);
210 return static_cast<AxisId>(idx);
211}
212
213}
214}
void finish_frame(cgv::render::context &) override
this method is called in one pass over all drawables after drawing
Definition gizmo.cxx:12
bool handle(cgv::gui::event &e) override
overload and implement this method to handle events
Definition gizmo.cxx:60
bool get_active() const
return whether the current node is active
Definition traverser.cxx:39
unsigned get_kind() const
return, what kind of event this is, typically a value from the EventId enum
Definition event.cxx:202
class to represent all possible mouse events with the EID_MOUSE
Definition mouse_event.h:33
short get_y() const
current mouse y position (origin is top-left of window)
MouseAction get_action() const
return the mouse action
short get_x() const
current mouse x position (origin is top-left of window)
T & z()
third element
Definition fvec.h:164
T & y()
second element
Definition fvec.h:160
T & w()
fourth element
Definition fvec.h:168
void inverse_rotate(vec_type &image) const
rotate vector according to the inverse quaternion
Definition quaternion.h:217
hmat_type get_homogeneous_matrix() const
return equivalent 4x4 rotation matrix
Definition quaternion.h:169
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:627
void set_blend_func_back_to_front()
set the default blend function for back to front blending
Definition context.cxx:1771
virtual void mul_modelview_matrix(const dmat4 &MV)
multiply given matrix from right to current modelview matrix
Definition context.cxx:1827
void push_depth_test_state()
push a copy of the current depth test state onto the stack saved attributes: depth test enablement,...
Definition context.cxx:1688
void push_blend_state()
push a copy of the current blend state onto the stack saved attributes: blend enablement,...
Definition context.cxx:1735
virtual void disable_depth_test()
disable the depth test
Definition context.cxx:1714
void pop_blend_state()
pop the top of the current culling state from the stack
Definition context.cxx:1739
void pop_depth_test_state()
pop the top of the current depth test state from the stack
Definition context.cxx:1692
virtual dmat4 get_projection_matrix() const =0
return homogeneous 4x4 projection matrix, which transforms from eye to clip space
const std::vector< window_transformation > & get_window_transformation_array() const
return the current window transformation array
Definition context.cxx:1934
virtual void enable_blending()
enable blending
Definition context.cxx:1775
virtual dmat4 get_modelview_matrix() const =0
return homogeneous 4x4 viewing matrix, which transforms from world to eye space
void pop_modelview_matrix()
see push_V for an explanation
Definition context.cxx:1833
void push_modelview_matrix()
push the current viewing matrix onto a matrix stack for viewing matrices.
Definition context.cxx:1821
context * get_context() const
access the current context. The context will be available latestly in the init method but not in the ...
Definition drawable.cxx:37
view * find_view_as_node(size_t view_idx=0) const
convenience function to find the view control in the current hierarchy
Definition drawable.cxx:49
void post_redraw()
posts a redraw event to the current context if one is available
Definition drawable.cxx:43
defines a symmetric view with the following quantities:
Definition view.h:22
const dvec3 get_eye() const
query the eye point, which is computed from focus, view dir, y extent at focus and y view angle
Definition view.cxx:123
const dvec3 & get_view_dir() const
query current view direction
Definition view.cxx:58
double get_tan_of_half_of_fovy(bool ensure_non_zero) const
compute tan of half of y view angle, if ensure_non_zero is true, replace y view angles < 0....
Definition view.cxx:42
@ MA_PRESS
mouse button pressed
Definition mouse_event.h:13
@ MA_MOVE
mouse pointer moved
Definition mouse_event.h:16
@ MA_DRAG
mouse drag action
Definition mouse_event.h:17
@ MA_RELEASE
mouse button released
Definition mouse_event.h:14
@ EID_MOUSE
id for mouse event
Definition event.h:18
namespace for api independent GPU programming
the cgv namespace
Definition print.h:11
cgv::math::fvec< float, 2 > vec2
declare type of 2d single precision floating point vectors
Definition fvec.h:668
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:670