2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
31 #ifndef __ED_VIEW3D_H__
32 #define __ED_VIEW3D_H__
34 /* ********* exports for space_view3d/ module ********** */
50 struct RV3DMatrixStore;
63 struct wmOperatorType;
65 struct wmWindowManager;
71 /* for derivedmesh drawing callbacks, for view3d_select, .... */
72 typedef struct ViewContext {
75 struct Object *obedit;
79 struct RegionView3D *rv3d;
80 struct BMEditMesh *em;
84 typedef struct ViewDepths {
86 short x, y; /* only for temp use for sub-rects, added to ar->winx/y */
88 double depth_range[2];
93 float *ED_view3d_cursor3d_get(struct Scene *scene, struct View3D *v3d);
94 void ED_view3d_cursor3d_position(struct bContext *C, float fp[3], const int mval[2]);
95 void ED_view3d_cursor3d_update(struct bContext *C, const int mval[2]);
97 struct Camera *ED_view3d_camera_data_get(struct View3D *v3d, struct RegionView3D *rv3d);
99 void ED_view3d_to_m4(float mat[4][4], const float ofs[3], const float quat[4], const float dist);
100 void ED_view3d_from_m4(float mat[4][4], float ofs[3], float quat[4], float *dist);
102 void ED_view3d_from_object(struct Object *ob, float ofs[3], float quat[4], float *dist, float *lens);
103 void ED_view3d_to_object(struct Object *ob, const float ofs[3], const float quat[4], const float dist);
105 void ED_view3d_lastview_store(struct RegionView3D *rv3d);
108 void ED_view3d_depth_update(struct ARegion *ar);
109 float ED_view3d_depth_read_cached(const struct ViewContext *vc, int x, int y);
110 void ED_view3d_depth_tag_update(struct RegionView3D *rv3d);
113 #define IS_CLIPPED 12000
115 /* return values for ED_view3d_project_...() */
118 V3D_PROJ_RET_CLIP_NEAR = 1, /* can't avoid this when in perspective mode, (can't avoid) */
119 V3D_PROJ_RET_CLIP_ZERO = 2, /* so close to zero we can't apply a perspective matrix usefully */
120 V3D_PROJ_RET_CLIP_BB = 3, /* bounding box clip - RV3D_CLIPPING */
121 V3D_PROJ_RET_CLIP_WIN = 4, /* outside window bounds */
122 V3D_PROJ_RET_OVERFLOW = 5 /* outside range (mainly for short), (can't avoid) */
125 /* some clipping tests are optional */
127 V3D_PROJ_TEST_NOP = 0,
128 V3D_PROJ_TEST_CLIP_BB = (1 << 0),
129 V3D_PROJ_TEST_CLIP_WIN = (1 << 1),
130 V3D_PROJ_TEST_CLIP_NEAR = (1 << 2),
131 V3D_PROJ_TEST_CLIP_ZERO = (1 << 3)
134 #define V3D_PROJ_TEST_CLIP_DEFAULT \
135 (V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_WIN | V3D_PROJ_TEST_CLIP_NEAR)
136 #define V3D_PROJ_TEST_ALL \
137 (V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_WIN | V3D_PROJ_TEST_CLIP_NEAR | V3D_PROJ_TEST_CLIP_ZERO)
140 /* view3d_iterators.c */
142 /* foreach iterators */
143 void meshobject_foreachScreenVert(
144 struct ViewContext *vc,
145 void (*func)(void *userData, struct MVert *eve, const float screen_co[2], int index),
146 void *userData, const eV3DProjTest clip_flag);
147 void mesh_foreachScreenVert(
148 struct ViewContext *vc,
149 void (*func)(void *userData, struct BMVert *eve, const float screen_co[2], int index),
150 void *userData, const eV3DProjTest clip_flag);
151 void mesh_foreachScreenEdge(
152 struct ViewContext *vc,
153 void (*func)(void *userData, struct BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2],
155 void *userData, const eV3DProjTest clip_flag);
156 void mesh_foreachScreenFace(
157 struct ViewContext *vc,
158 void (*func)(void *userData, struct BMFace *efa, const float screen_co[2], int index),
159 void *userData, const eV3DProjTest clip_flag);
160 void nurbs_foreachScreenVert(
161 struct ViewContext *vc,
162 void (*func)(void *userData, struct Nurb *nu, struct BPoint *bp, struct BezTriple *bezt,
163 int beztindex, const float screen_co[2]),
164 void *userData, const eV3DProjTest clip_flag);
165 void mball_foreachScreenElem(
166 struct ViewContext *vc,
167 void (*func)(void *userData, struct MetaElem *ml, const float screen_co[2]),
168 void *userData, const eV3DProjTest clip_flag);
169 void lattice_foreachScreenVert(
170 struct ViewContext *vc,
171 void (*func)(void *userData, struct BPoint *bp,
172 const float screen_co[2]),
173 void *userData, const eV3DProjTest clip_flag);
174 void armature_foreachScreenBone(
175 struct ViewContext *vc,
176 void (*func)(void *userData, struct EditBone *ebone,
177 const float screen_co_a[2], const float screen_co_b[2]),
178 void *userData, const eV3DProjTest clip_flag);
179 void pose_foreachScreenBone(
180 struct ViewContext *vc,
181 void (*func)(void *userData, struct bPoseChannel *pchan,
182 const float screen_co_a[2], const float screen_co_b[2]),
183 void *userData, const eV3DProjTest clip_flag);
184 /* *** end iterators *** */
187 /* view3d_project.c */
188 void ED_view3d_project_float_v2_m4(const struct ARegion *ar, const float co[3], float r_co[2], float mat[4][4]);
189 void ED_view3d_project_float_v3_m4(const struct ARegion *ar, const float co[3], float r_co[3], float mat[4][4]);
191 eV3DProjStatus ED_view3d_project_base(const struct ARegion *ar, struct Base *base);
194 eV3DProjStatus ED_view3d_project_short_ex(const struct ARegion *ar, float perspmat[4][4], const bool is_local,
195 const float co[3], short r_co[2], const eV3DProjTest flag);
196 eV3DProjStatus ED_view3d_project_short_global(const struct ARegion *ar, const float co[3], short r_co[2], const eV3DProjTest flag);
197 eV3DProjStatus ED_view3d_project_short_object(const struct ARegion *ar, const float co[3], short r_co[2], const eV3DProjTest flag);
200 eV3DProjStatus ED_view3d_project_int_ex(const struct ARegion *ar, float perspmat[4][4], const bool is_local,
201 const float co[3], int r_co[2], const eV3DProjTest flag);
202 eV3DProjStatus ED_view3d_project_int_global(const struct ARegion *ar, const float co[3], int r_co[2], const eV3DProjTest flag);
203 eV3DProjStatus ED_view3d_project_int_object(const struct ARegion *ar, const float co[3], int r_co[2], const eV3DProjTest flag);
206 eV3DProjStatus ED_view3d_project_float_ex(const struct ARegion *ar, float perspmat[4][4], const bool is_local,
207 const float co[3], float r_co[2], const eV3DProjTest flag);
208 eV3DProjStatus ED_view3d_project_float_global(const struct ARegion *ar, const float co[3], float r_co[2], const eV3DProjTest flag);
209 eV3DProjStatus ED_view3d_project_float_object(const struct ARegion *ar, const float co[3], float r_co[2], const eV3DProjTest flag);
211 float ED_view3d_calc_zfac(const struct RegionView3D *rv3d, const float co[3], bool *r_flip);
212 bool ED_view3d_clip_segment(const struct RegionView3D *rv3d, float ray_start[3], float ray_end[3]);
213 bool ED_view3d_win_to_ray(
214 const struct ARegion *ar, const struct View3D *v3d, const float mval[2],
215 float ray_start[3], float ray_normal[3], const bool do_clip);
216 bool ED_view3d_win_to_ray_ex(
217 const struct ARegion *ar, const struct View3D *v3d, const float mval[2],
218 float r_ray_co[3], float r_ray_normal[3], float r_ray_start[3], bool do_clip);
219 void ED_view3d_global_to_vector(const struct RegionView3D *rv3d, const float coord[3], float vec[3]);
220 void ED_view3d_win_to_3d(
221 const struct View3D *v3d, const struct ARegion *ar,
222 const float depth_pt[3], const float mval[2],
224 void ED_view3d_win_to_3d_int(
225 const struct View3D *v3d, const struct ARegion *ar,
226 const float depth_pt[3], const int mval[2],
228 void ED_view3d_win_to_delta(const struct ARegion *ar, const float mval[2], float out[3], const float zfac);
229 void ED_view3d_win_to_origin(const struct ARegion *ar, const float mval[2], float out[3]);
230 void ED_view3d_win_to_vector(const struct ARegion *ar, const float mval[2], float out[3]);
231 bool ED_view3d_win_to_segment(const struct ARegion *ar, struct View3D *v3d, const float mval[2],
232 float r_ray_start[3], float r_ray_end[3], const bool do_clip);
233 void ED_view3d_ob_project_mat_get(const struct RegionView3D *v3d, struct Object *ob, float pmat[4][4]);
234 void ED_view3d_ob_project_mat_get_from_obmat(const struct RegionView3D *rv3d, float obmat[4][4], float pmat[4][4]);
235 void ED_view3d_unproject(struct bglMats *mats, float out[3], const float x, const float y, const float z);
240 void ED_view3d_dist_range_get(
241 const struct View3D *v3d,
242 float r_dist_range[2]);
243 bool ED_view3d_clip_range_get(
244 const struct View3D *v3d, const struct RegionView3D *rv3d,
245 float *r_clipsta, float *r_clipend, const bool use_ortho_factor);
246 bool ED_view3d_viewplane_get(
247 const struct View3D *v3d, const struct RegionView3D *rv3d, int winxi, int winyi,
248 struct rctf *r_viewplane, float *r_clipsta, float *r_clipend, float *r_pixsize);
250 void ED_view3d_polygon_offset(const struct RegionView3D *rv3d, const float dist);
252 void ED_view3d_calc_camera_border(
253 const struct Scene *scene, const struct ARegion *ar,
254 const struct View3D *v3d, const struct RegionView3D *rv3d,
255 struct rctf *r_viewborder, const bool no_shift);
256 void ED_view3d_calc_camera_border_size(
257 const struct Scene *scene, const struct ARegion *ar,
258 const struct View3D *v3d, const struct RegionView3D *rv3d,
260 bool ED_view3d_calc_render_border(struct Scene *scene, struct View3D *v3d,
261 struct ARegion *ar, struct rcti *rect);
263 void ED_view3d_clipping_calc_from_boundbox(float clip[6][4], const struct BoundBox *clipbb, const bool is_flip);
264 void ED_view3d_clipping_calc(struct BoundBox *bb, float planes[4][4], struct bglMats *mats, const struct rcti *rect);
265 void ED_view3d_clipping_local(struct RegionView3D *rv3d, float mat[4][4]);
266 bool ED_view3d_clipping_test(const struct RegionView3D *rv3d, const float co[3], const bool is_local);
267 void ED_view3d_clipping_set(struct RegionView3D *rv3d);
268 void ED_view3d_clipping_enable(void);
269 void ED_view3d_clipping_disable(void);
271 float ED_view3d_pixel_size(const struct RegionView3D *rv3d, const float co[3]);
273 float ED_view3d_radius_to_dist_persp(const float angle, const float radius);
274 float ED_view3d_radius_to_dist_ortho(const float lens, const float radius);
275 float ED_view3d_radius_to_dist(
276 const struct View3D *v3d, const struct ARegion *ar,
277 const char persp, const bool use_aspect,
280 void drawcircball(int mode, const float cent[3], float rad, const float tmat[4][4]);
282 /* backbuffer select and draw support */
283 void ED_view3d_backbuf_validate(struct ViewContext *vc);
284 struct ImBuf *ED_view3d_backbuf_read(struct ViewContext *vc, int xmin, int ymin, int xmax, int ymax);
285 unsigned int ED_view3d_backbuf_sample_rect(
286 struct ViewContext *vc, const int mval[2], int size,
287 unsigned int min, unsigned int max, float *r_dist);
288 int ED_view3d_backbuf_sample_size_clamp(struct ARegion *ar, const float dist);
289 unsigned int ED_view3d_backbuf_sample(struct ViewContext *vc, int x, int y);
291 bool ED_view3d_autodist(
292 struct Scene *scene, struct ARegion *ar, struct View3D *v3d,
293 const int mval[2], float mouse_worldloc[3],
294 const bool alphaoverride, const float fallback_depth_pt[3]);
296 /* only draw so ED_view3d_autodist_simple can be called many times after */
297 void ED_view3d_autodist_init(struct Scene *scene, struct ARegion *ar, struct View3D *v3d, int mode);
298 bool ED_view3d_autodist_simple(struct ARegion *ar, const int mval[2], float mouse_worldloc[3], int margin, float *force_depth);
299 bool ED_view3d_autodist_depth(struct ARegion *ar, const int mval[2], int margin, float *depth);
300 bool ED_view3d_autodist_depth_seg(struct ARegion *ar, const int mval_sta[2], const int mval_end[2], int margin, float *depth);
303 #define MAXPICKELEMS 2500
304 #define MAXPICKBUF (4 * MAXPICKELEMS)
307 /* all elements in the region, ignore depth */
308 VIEW3D_SELECT_ALL = 0,
309 /* pick also depth sorts (only for small regions!) */
310 VIEW3D_SELECT_PICK_ALL = 1,
311 /* sorts and only returns visible objects (only for small regions!) */
312 VIEW3D_SELECT_PICK_NEAREST = 2,
315 void view3d_opengl_select_cache_begin(void);
316 void view3d_opengl_select_cache_end(void);
318 int view3d_opengl_select(
319 struct ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const struct rcti *input,
320 eV3DSelectMode select_mode);
322 /* view3d_select.c */
323 float ED_view3d_select_dist_px(void);
324 void view3d_set_viewcontext(struct bContext *C, struct ViewContext *vc);
325 void view3d_operator_needs_opengl(const struct bContext *C);
326 void view3d_region_operator_needs_opengl(struct wmWindow *win, struct ARegion *ar);
327 void view3d_opengl_read_pixels(struct ARegion *ar, int x, int y, int w, int h, int format, int type, void *data);
328 void view3d_get_transformation(const struct ARegion *ar, struct RegionView3D *rv3d, struct Object *ob, struct bglMats *mats);
330 /* XXX should move to BLI_math */
331 bool edge_inside_circle(const float cent[2], float radius, const float screen_co_a[2], const float screen_co_b[2]);
333 /* get 3d region from context, also if mouse is in header or toolbar */
334 struct RegionView3D *ED_view3d_context_rv3d(struct bContext *C);
335 bool ED_view3d_context_user_region(struct bContext *C, struct View3D **r_v3d, struct ARegion **r_ar);
336 int ED_operator_rv3d_user_region_poll(struct bContext *C);
338 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d);
339 void ED_view3d_init_mats_rv3d_gl(struct Object *ob, struct RegionView3D *rv3d);
341 void ED_view3d_clear_mats_rv3d(struct RegionView3D *rv3d);
342 void ED_view3d_check_mats_rv3d(struct RegionView3D *rv3d);
344 # define ED_view3d_clear_mats_rv3d(rv3d) (void)(rv3d)
345 # define ED_view3d_check_mats_rv3d(rv3d) (void)(rv3d)
347 int ED_view3d_scene_layer_set(int lay, const int *values, int *active);
349 struct RV3DMatrixStore *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d);
350 void ED_view3d_mats_rv3d_restore(struct RegionView3D *rv3d, struct RV3DMatrixStore *rv3dmat);
352 bool ED_view3d_context_activate(struct bContext *C);
353 void ED_view3d_draw_offscreen_init(struct Scene *scene, struct View3D *v3d);
354 void ED_view3d_draw_offscreen(
355 struct Scene *scene, struct View3D *v3d, struct ARegion *ar, int winx, int winy, float viewmat[4][4],
356 float winmat[4][4], bool do_bgpic, bool do_sky, bool is_persp, const char *viewname,
357 struct GPUFX *fx, struct GPUFXSettings *fx_settings,
358 struct GPUOffScreen *ofs);
359 void ED_view3d_draw_setup_view(
360 struct wmWindow *win, struct Scene *scene, struct ARegion *ar, struct View3D *v3d,
361 float viewmat[4][4], float winmat[4][4]);
363 struct ImBuf *ED_view3d_draw_offscreen_imbuf(
364 struct Scene *scene, struct View3D *v3d, struct ARegion *ar, int sizex, int sizey,
365 unsigned int flag, bool draw_background,
366 int alpha_mode, int samples, bool full_samples, const char *viewname,
367 struct GPUFX *fx, struct GPUOffScreen *ofs, char err_out[256]);
368 struct ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
369 struct Scene *scene, struct Object *camera, int width, int height,
370 unsigned int flag, int drawtype, bool use_solid_tex, bool use_gpencil, bool draw_background,
371 int alpha_mode, int samples, bool full_samples, const char *viewname,
372 struct GPUFX *fx, struct GPUOffScreen *ofs, char err_out[256]);
374 struct Base *ED_view3d_give_base_under_cursor(struct bContext *C, const int mval[2]);
375 void ED_view3d_quadview_update(struct ScrArea *sa, struct ARegion *ar, bool do_clip);
376 void ED_view3d_update_viewmat(struct Scene *scene, struct View3D *v3d, struct ARegion *ar, float viewmat[4][4], float winmat[4][4]);
377 bool ED_view3d_quat_from_axis_view(const char view, float quat[4]);
378 char ED_view3d_quat_to_axis_view(const float quat[4], const float epsilon);
379 char ED_view3d_lock_view_from_index(int index);
380 char ED_view3d_axis_view_opposite(char view);
381 bool ED_view3d_lock(struct RegionView3D *rv3d);
383 uint64_t ED_view3d_datamask(const struct Scene *scene, const struct View3D *v3d);
384 uint64_t ED_view3d_screen_datamask(const struct bScreen *screen);
386 bool ED_view3d_offset_lock_check(const struct View3D *v3d, const struct RegionView3D *rv3d);
388 /* camera lock functions */
389 bool ED_view3d_camera_lock_check(const struct View3D *v3d, const struct RegionView3D *rv3d);
390 /* copy the camera to the view before starting a view transformation */
391 void ED_view3d_camera_lock_init_ex(struct View3D *v3d, struct RegionView3D *rv3d, const bool calc_dist);
392 void ED_view3d_camera_lock_init(struct View3D *v3d, struct RegionView3D *rv3d);
393 /* copy the view to the camera, return true if */
394 bool ED_view3d_camera_lock_sync(struct View3D *v3d, struct RegionView3D *rv3d);
396 bool ED_view3d_camera_autokey(
397 struct Scene *scene, struct ID *id_key,
398 struct bContext *C, const bool do_rotate, const bool do_translate);
399 bool ED_view3d_camera_lock_autokey(
400 struct View3D *v3d, struct RegionView3D *rv3d,
401 struct bContext *C, const bool do_rotate, const bool do_translate);
403 void ED_view3D_lock_clear(struct View3D *v3d);
405 struct BGpic *ED_view3D_background_image_new(struct View3D *v3d);
406 void ED_view3D_background_image_remove(struct View3D *v3d, struct BGpic *bgpic);
407 void ED_view3D_background_image_clear(struct View3D *v3d);
409 #define VIEW3D_MARGIN 1.4f
410 #define VIEW3D_DIST_FALLBACK 1.0f
412 float ED_view3d_offset_distance(float mat[4][4], const float ofs[3], const float dist_fallback);
413 void ED_view3d_distance_set(struct RegionView3D *rv3d, const float dist);
415 float ED_scene_grid_scale(struct Scene *scene, const char **grid_unit);
416 float ED_view3d_grid_scale(struct Scene *scene, struct View3D *v3d, const char **grid_unit);
418 void ED_scene_draw_fps(struct Scene *scene, const struct rcti *rect);
420 /* view matrix properties utilities */
423 void ED_view3d_operator_properties_viewmat(struct wmOperatorType *ot);
424 void ED_view3d_operator_properties_viewmat_set(struct bContext *C, struct wmOperator *op);
425 void ED_view3d_operator_properties_viewmat_get(struct wmOperator *op, int *winx, int *winy, float persmat[4][4]);
429 void ED_view3d_stop_render_preview(struct wmWindowManager *wm, struct ARegion *ar);
430 void ED_view3d_shade_update(struct Main *bmain, struct Scene *scene, struct View3D *v3d, struct ScrArea *sa);
432 #define V3D_IS_ZBUF(v3d) \
433 (((v3d)->flag & V3D_ZBUF_SELECT) && ((v3d)->drawtype > OB_WIRE))
435 void ED_view3d_id_remap(struct View3D *v3d, const struct ID *old_id, struct ID *new_id);
437 #endif /* __ED_VIEW3D_H__ */