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 switch(me.get_action()) {
83 {
84 if(_captured_mouse)
85 break;
86
87 bool was_hovered = _hovered;
88 InteractionFeature last_feature = _interaction_feature;
89 AxisId last_axis_id = _interaction_axis_id;
90
91 // transform ray to gizmo object space
92 cgv::math::ray3 ray_object = ray;
93 ray_object.origin -= _position;
94 ray_object.origin /= _size;
95
96 if(_orientation == GizmoOrientation::kLocal) {
97 _rotation.inverse_rotate(ray_object.origin);
98 _rotation.inverse_rotate(ray_object.direction);
99 }
100
101 _hovered = intersect_bounding_box(ray_object) && intersect(ray_object);
102
103 if(was_hovered != _hovered || last_feature != _interaction_feature || last_axis_id != _interaction_axis_id) {
104 set_geometry_out_of_date();
105 post_redraw();
106 }
107 break;
108 }
110 {
111 if(_hovered && _interaction_feature != InteractionFeature::kNone) {
112 if(start_drag(ray)) {
113 _drag_start_ray = ray;
114 _captured_mouse = true;
115 return true;
116 }
117 }
118 break;
119 }
121 if(_captured_mouse) {
122 end_drag(ray);
123 _captured_mouse = false;
124 _interaction_feature = InteractionFeature::kNone;
125 set_geometry_out_of_date();
126 return true;
127 }
128 break;
130 if(_captured_mouse && drag(ray))
131 return true;
132 break;
133 default:
134 break;
135 }
136
137 return false;
138}
139
140GizmoOrientation gizmo::get_orientation() const {
141 return _orientation;
142}
143
144void gizmo::set_orientation(GizmoOrientation orientation) {
145 _orientation = orientation;
146 post_redraw();
147}
148
149vec3 gizmo::get_position() const {
150 return _position;
151}
152
153void gizmo::set_position(const vec3& position) {
154 _position = position;
155 post_redraw();
156}
157
158quat gizmo::get_rotation() const {
159 return _rotation;
160}
161
162void gizmo::set_rotation(const quat& rotation) {
163 _rotation = rotation;
164 post_redraw();
165}
166
167float gizmo::get_size() const {
168 return _size;
169}
170
171const cgv::render::view* gizmo::get_view() const {
172 return _view;
173}
174
175void gizmo::set_geometry_out_of_date() {
176 _geometry_out_of_date = true;
177}
178
179bool gizmo::captured_mouse() const {
180 return _captured_mouse;
181}
182
183bool gizmo::is_hovered() const {
184 return _hovered;
185}
186
187vec3 gizmo::get_axis(int index) const {
188 assert(index >= 0 && index < 3);
189 vec3 axis(0.0f);
190 axis[index] = 1.0f;
191 return axis;
192}
193
194vec3 gizmo::get_axis_mask(InteractionFeature feature, AxisId axis_id) const {
195 switch(feature) {
196 case InteractionFeature::kAxis:
197 return get_axis(axis_id_to_index(axis_id));
198 case InteractionFeature::kPlane:
199 return vec3(1.0f) - get_axis(axis_id_to_index(axis_id));
200 case InteractionFeature::kCenter:
201 return vec3(1.0f);
202 default:
203 return vec3(0.0f);
204 }
205}
206
207int gizmo::axis_id_to_index(AxisId axis) const {
208 return static_cast<int>(axis);
209}
210
211gizmo::AxisId gizmo::index_to_axis_id(int idx) const {
212 assert(idx >= 0 && idx < 3);
213 return static_cast<AxisId>(idx);
214}
215
216}
217}
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:163
T & y()
second element
Definition fvec.h:159
T & w()
fourth element
Definition fvec.h:167
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:621
void set_blend_func_back_to_front()
set the default blend function for back to front blending
Definition context.cxx:1752
virtual void mul_modelview_matrix(const dmat4 &MV)
multiply given matrix from right to current modelview matrix
Definition context.cxx:1808
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:1669
void push_blend_state()
push a copy of the current blend state onto the stack saved attributes: blend enablement,...
Definition context.cxx:1716
virtual void disable_depth_test()
disable the depth test
Definition context.cxx:1695
void pop_blend_state()
pop the top of the current culling state from the stack
Definition context.cxx:1720
void pop_depth_test_state()
pop the top of the current depth test state from the stack
Definition context.cxx:1673
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:1915
virtual void enable_blending()
enable blending
Definition context.cxx:1756
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:1814
void push_modelview_matrix()
push the current viewing matrix onto a matrix stack for viewing matrices.
Definition context.cxx:1802
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:667
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:669