cgv
Loading...
Searching...
No Matches
drawable.cxx
1#include <cgv/base/base.h>
2#include <cgv/base/node.h>
3#include <cgv/render/drawable.h>
4#include <cgv/base/find_action.h>
5
6namespace cgv {
7 namespace render {
8
10{
11 ctx = _ctx;
12}
13
15{
16 ctx = 0;
17}
18
19
22{
23 active = false;
24}
25
27{
28 active = true;
29}
30
32{
33 return active;
34}
35
38{
39 return ctx;
40}
41
44{
45 if(ctx)
46 ctx->post_redraw();
47}
48
49view* drawable::find_view_as_node(size_t view_idx) const
50{
51 cgv::base::node* node_ptr = const_cast<cgv::base::node*>(dynamic_cast<const cgv::base::node*>(this));
52 std::vector<view*> views;
54 if (views.empty() || view_idx > views.size())
55 return 0;
56 return views[view_idx];
57}
58
59bool drawable::get_world_location(int x, int y, const view& V, dvec3& world_location, double* window_z_ptr) const
60{
61 if (!get_context())
62 return false;
63 // analyze the mouse location
64 context& ctx = *get_context();
65 const dmat4* DPV_ptr, *DPV_other_ptr;
66 int x_other, y_other, vp_col_idx, vp_row_idx, vp_width, vp_height;
67 int eye_panel = V.get_modelview_projection_window_matrices(x, y, ctx.get_width(), ctx.get_height(), &DPV_ptr, &DPV_other_ptr, &x_other, &y_other, &vp_col_idx, &vp_row_idx, &vp_width, &vp_height);
68
69 // get the possibly two (if stereo is enabled) different device z-values
70 double z = ctx.get_window_z(x, y);
71 double z_other = ctx.get_window_z(x_other, y_other);
72 // unproject to world coordinates with smaller (closer to eye) z-value one
73 if (z <= z_other) {
74 if (DPV_ptr->ncols() != 4)
75 return false;
76 // use conversion to (double*) operator to map cgv::math::vec<double> to cgv::math::fvec<float,3>
77 world_location = ctx.get_model_point(x, y, z, *DPV_ptr);
78 if (window_z_ptr)
79 *window_z_ptr = z;
80 }
81 else {
82 if (DPV_other_ptr->ncols() != 4)
83 return false;
84 world_location = ctx.get_model_point(x_other, y_other, z_other, *DPV_other_ptr);
85 if (window_z_ptr)
86 *window_z_ptr = z_other;
87 }
88 return true;
89}
90
91cgv::ray3 drawable::get_world_ray(int x, int y, view* view_ptr, const cgv::dmat4& modelview_projection_window_matrix) const
92{
93 cgv::dmat4 iMPW = inverse(modelview_projection_window_matrix);
94 int height = get_context() ? get_context()->get_height() : 800;
95 cgv::dvec4 p_window(x, height - y - 1, 1, 1);
96 cgv::dvec3 p_world = (iMPW * p_window).clip();
97 cgv::dvec3 eye;
98 if (view_ptr)
99 eye = view_ptr->get_eye();
100 else {
101 p_window[2] = 0.01;
102 eye = (iMPW * p_window).clip();
103 }
104 cgv::dvec3 dir = normalize(p_world-eye);
105 return cgv::ray3(eye, dir);
106}
107
110{
111 if (ctx)
112 ctx->force_redraw();
113}
114
117{
118 return true;
119}
120
121void drawable::resize(unsigned int, unsigned int)
122{
123}
124
128
130{
131}
132
136
140
145
148{
149}
151{
152 clear(ctx);
153}
154
155
158{
159 current_render_pass = -1;
160 render_pass_recursion_depth = 0;
161}
162
164{
165 if (current_render_pass != -1)
166 return false;
167 render_pass_recursion_depth = ctx.get_render_pass_recursion_depth();
168 return true;
169}
170
171void multi_pass_drawable::perform_render_pass(context& ctx, int rp_idx, RenderPass rp, int excluded_flags, int included_flags)
172{
173 current_render_pass = rp_idx;
174 unsigned rpf = (ctx.get_render_pass_flags() & ~excluded_flags) | included_flags;
175 ctx.render_pass(rp, RenderPassFlags(rpf), this, rp_idx);
176}
177
179{
180 current_render_pass = rp_idx;
181}
182
184{
185 if (current_render_pass == -1)
186 return true;
187 if (ctx.get_render_pass_user_data() != this)
188 if (render_pass_recursion_depth != ctx.get_render_pass_recursion_depth())
189 return true;
190 return false;
191}
192
194{
195 if (render_pass_recursion_depth != ctx.get_render_pass_recursion_depth())
196 return false;
197 current_render_pass = -1;
198 return true;
199}
200
201 } // namespace render
202} // namespace cgv
The node class keeps a pointer to its parent.
Definition node.h:19
traverse_policy(int _policy=TP_ALL+TP_STOP_ON_SUCCESS, bool _active=true, int _focus=-1)
construct default traverse policy that visits everything
Definition traverser.cxx:7
static constexpr unsigned ncols()
number of columns
Definition fmat.h:71
base class for all drawables, which is independent of the used rendering API.
Definition context.h:670
virtual unsigned int get_height() const =0
return the height of the window
virtual void post_redraw()=0
the context will be redrawn when the system is idle again
bool get_world_location(int x, int y, const view &V, dvec3 &world_location, double *window_z_ptr=0) const
use given view together with depth buffer of context in order to compute the world location of the po...
Definition drawable.cxx:59
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
virtual void init_frame(context &)
this method is called in one pass over all drawables before the draw method
Definition drawable.cxx:125
cgv::ray3 get_world_ray(int x, int y, view *view_ptr, const cgv::dmat4 &modelview_projection_window_matrix) const
compute ray from eye of given view_ptr through pixel at given mouse location (x,y) according to given...
Definition drawable.cxx:91
void set_context(context *_ctx)
set the current focus context, this should only be called by the context itself
Definition drawable.cxx:9
virtual void finish_draw(context &)
this method is called when the current drawable is left in a tree traversal that calls the draw metho...
Definition drawable.cxx:133
virtual void destruct(context &) final
do not use destruct for a drawable but clear!!
Definition drawable.cxx:150
virtual void draw(context &)
overload to draw the content of this drawable
Definition drawable.cxx:129
void force_redraw()
forces a redraw right now. This cannot be called from init, init_frame, draw, finish_draw,...
Definition drawable.cxx:109
virtual void resize(unsigned int w, unsigned int h)
callback to announce resizing of the output window
Definition drawable.cxx:121
void hide()
hide the drawable
Definition drawable.cxx:21
virtual void after_finish(context &)
this method is called in one pass over all drawables after finish frame
Definition drawable.cxx:142
virtual void finish_frame(context &)
this method is called in one pass over all drawables after drawing
Definition drawable.cxx:137
drawable()
default construction
Definition drawable.cxx:14
virtual bool init(context &)
this method is called after creation or recreation of the context, return whether all necessary funct...
Definition drawable.cxx:116
virtual void clear(context &)
clear all objects living in the context like textures or display lists
Definition drawable.cxx:147
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 show()
show the drawable
Definition drawable.cxx:26
void post_redraw()
posts a redraw event to the current context if one is available
Definition drawable.cxx:43
bool is_visible() const
check whether the drawable is visible
Definition drawable.cxx:31
bool multi_pass_terminate(const context &ctx)
check in after_finish method, whether this was the terminating render pass
Definition drawable.cxx:193
void perform_render_pass(context &ctx, int rp_idx, RenderPass rp=RP_USER_DEFINED, int excluded_flags=RPF_HANDLE_SCREEN_SHOT, int included_flags=0)
call to initiate a render pass in the init_frame method after initiate_render_pass_recursion() has su...
Definition drawable.cxx:171
bool initiate_render_pass_recursion(context &ctx)
call in init_frame method to check whether the recursive render passes need to be initiated
Definition drawable.cxx:163
bool multi_pass_ignore_finish(const context &ctx)
check in after_finish method, whether this should be directly exited with a return statement
Definition drawable.cxx:183
void initiate_terminal_render_pass(int rp_idx)
call after last recursive render pass to use current render pass for last render pass
Definition drawable.cxx:178
multi_pass_drawable()
construct to be not inside of a render pass
Definition drawable.cxx:157
defines a symmetric view with the following quantities:
Definition view.h:22
virtual int get_modelview_projection_window_matrices(int x, int y, int width, int height, const dmat4 **MPW_pptr, const dmat4 **MPW_other_pptr=0, int *x_other_ptr=0, int *y_other_ptr=0, int *vp_col_idx_ptr=0, int *vp_row_idx_ptr=0, int *vp_width_ptr=0, int *vp_height_ptr=0, int *vp_center_x_ptr=0, int *vp_center_y_ptr=0, int *vp_center_x_other_ptr=0, int *vp_center_y_other_ptr=0) const
given an opengl pixel location and the size in pixels of the opengl window, return the modelview_proj...
Definition view.cxx:277
the base namespace holds the base hierarchy, support for plugin registration and signals
Definition action.cxx:4
data::ref_ptr< base, true > base_ptr
ref counted pointer to base
Definition base.h:37
void find_interface(base_ptr start, std::vector< X * > &result)
collect all nodes that implement interface X
Definition find_action.h:38
namespace for api independent GPU programming
RenderPass
Enumeration of different render passes, which can be queried from the context and used to specify a n...
Definition context.h:133
RenderPassFlags
available flags that can be queried from the context and set for a new render pass
Definition context.h:149
this header is dependency free
Definition print.h:11
cgv::math::fray< float, 3 > ray3
3d ray using 32 bit single precision floating point type
Definition fray.h:87
cgv::math::fvec< double, 4 > dvec4
declare type of 4d double precision floating point vectors (used for homogeneous coordinates)
Definition fvec.h:703
cgv::math::fvec< double, 3 > dvec3
declare type of 3d double precision floating point vectors
Definition fvec.h:701
cgv::math::fmat< double, 4, 4 > dmat4
declare type of 4x4 matrices
Definition fmat.h:481