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 {
76 struct Object *obedit;
80 struct RegionView3D *rv3d;
81 struct BMEditMesh *em;
85 typedef struct ViewDepths {
87 short x, y; /* only for temp use for sub-rects, added to ar->winx/y */
89 double depth_range[2];
94 float *ED_view3d_cursor3d_get(struct Scene *scene, struct View3D *v3d);
95 void ED_view3d_cursor3d_position(struct bContext *C, const int mval[2], float cursor_co[3]);
96 void ED_view3d_cursor3d_update(struct bContext *C, const int mval[2]);
98 struct Camera *ED_view3d_camera_data_get(struct View3D *v3d, struct RegionView3D *rv3d);
100 void ED_view3d_to_m4(float mat[4][4], const float ofs[3], const float quat[4], const float dist);
101 void ED_view3d_from_m4(float mat[4][4], float ofs[3], float quat[4], float *dist);
103 void ED_view3d_from_object(struct Object *ob, float ofs[3], float quat[4], float *dist, float *lens);
104 void ED_view3d_to_object(struct Object *ob, const float ofs[3], const float quat[4], const float dist);
106 void ED_view3d_lastview_store(struct RegionView3D *rv3d);
109 void ED_view3d_depth_update(struct ARegion *ar);
110 float ED_view3d_depth_read_cached(const struct ViewContext *vc, const int mval[2]);
111 bool ED_view3d_depth_read_cached_normal(
112 const ViewContext *vc, const struct bglMats *mats, const int mval[2],
114 bool ED_view3d_depth_unproject(
115 const struct ARegion *ar, const struct bglMats *mats,
116 const int mval[2], const double depth,
117 float r_location_world[3]);
118 void ED_view3d_depth_tag_update(struct RegionView3D *rv3d);
121 #define IS_CLIPPED 12000
123 /* return values for ED_view3d_project_...() */
126 V3D_PROJ_RET_CLIP_NEAR = 1, /* can't avoid this when in perspective mode, (can't avoid) */
127 V3D_PROJ_RET_CLIP_ZERO = 2, /* so close to zero we can't apply a perspective matrix usefully */
128 V3D_PROJ_RET_CLIP_BB = 3, /* bounding box clip - RV3D_CLIPPING */
129 V3D_PROJ_RET_CLIP_WIN = 4, /* outside window bounds */
130 V3D_PROJ_RET_OVERFLOW = 5 /* outside range (mainly for short), (can't avoid) */
133 /* some clipping tests are optional */
135 V3D_PROJ_TEST_NOP = 0,
136 V3D_PROJ_TEST_CLIP_BB = (1 << 0),
137 V3D_PROJ_TEST_CLIP_WIN = (1 << 1),
138 V3D_PROJ_TEST_CLIP_NEAR = (1 << 2),
139 V3D_PROJ_TEST_CLIP_ZERO = (1 << 3)
142 #define V3D_PROJ_TEST_CLIP_DEFAULT \
143 (V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_WIN | V3D_PROJ_TEST_CLIP_NEAR)
144 #define V3D_PROJ_TEST_ALL \
145 (V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_WIN | V3D_PROJ_TEST_CLIP_NEAR | V3D_PROJ_TEST_CLIP_ZERO)
148 /* view3d_iterators.c */
150 /* foreach iterators */
151 void meshobject_foreachScreenVert(
152 struct ViewContext *vc,
153 void (*func)(void *userData, struct MVert *eve, const float screen_co[2], int index),
154 void *userData, const eV3DProjTest clip_flag);
155 void mesh_foreachScreenVert(
156 struct ViewContext *vc,
157 void (*func)(void *userData, struct BMVert *eve, const float screen_co[2], int index),
158 void *userData, const eV3DProjTest clip_flag);
159 void mesh_foreachScreenEdge(
160 struct ViewContext *vc,
161 void (*func)(void *userData, struct BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2],
163 void *userData, const eV3DProjTest clip_flag);
164 void mesh_foreachScreenFace(
165 struct ViewContext *vc,
166 void (*func)(void *userData, struct BMFace *efa, const float screen_co[2], int index),
167 void *userData, const eV3DProjTest clip_flag);
168 void nurbs_foreachScreenVert(
169 struct ViewContext *vc,
170 void (*func)(void *userData, struct Nurb *nu, struct BPoint *bp, struct BezTriple *bezt,
171 int beztindex, const float screen_co[2]),
172 void *userData, const eV3DProjTest clip_flag);
173 void mball_foreachScreenElem(
174 struct ViewContext *vc,
175 void (*func)(void *userData, struct MetaElem *ml, const float screen_co[2]),
176 void *userData, const eV3DProjTest clip_flag);
177 void lattice_foreachScreenVert(
178 struct ViewContext *vc,
179 void (*func)(void *userData, struct BPoint *bp,
180 const float screen_co[2]),
181 void *userData, const eV3DProjTest clip_flag);
182 void armature_foreachScreenBone(
183 struct ViewContext *vc,
184 void (*func)(void *userData, struct EditBone *ebone,
185 const float screen_co_a[2], const float screen_co_b[2]),
186 void *userData, const eV3DProjTest clip_flag);
187 void pose_foreachScreenBone(
188 struct ViewContext *vc,
189 void (*func)(void *userData, struct bPoseChannel *pchan,
190 const float screen_co_a[2], const float screen_co_b[2]),
191 void *userData, const eV3DProjTest clip_flag);
192 /* *** end iterators *** */
195 /* view3d_project.c */
196 void ED_view3d_project_float_v2_m4(const struct ARegion *ar, const float co[3], float r_co[2], float mat[4][4]);
197 void ED_view3d_project_float_v3_m4(const struct ARegion *ar, const float co[3], float r_co[3], float mat[4][4]);
199 eV3DProjStatus ED_view3d_project_base(const struct ARegion *ar, struct Base *base);
202 eV3DProjStatus ED_view3d_project_short_ex(const struct ARegion *ar, float perspmat[4][4], const bool is_local,
203 const float co[3], short r_co[2], const eV3DProjTest flag);
204 eV3DProjStatus ED_view3d_project_short_global(const struct ARegion *ar, const float co[3], short r_co[2], const eV3DProjTest flag);
205 eV3DProjStatus ED_view3d_project_short_object(const struct ARegion *ar, const float co[3], short r_co[2], const eV3DProjTest flag);
208 eV3DProjStatus ED_view3d_project_int_ex(const struct ARegion *ar, float perspmat[4][4], const bool is_local,
209 const float co[3], int r_co[2], const eV3DProjTest flag);
210 eV3DProjStatus ED_view3d_project_int_global(const struct ARegion *ar, const float co[3], int r_co[2], const eV3DProjTest flag);
211 eV3DProjStatus ED_view3d_project_int_object(const struct ARegion *ar, const float co[3], int r_co[2], const eV3DProjTest flag);
214 eV3DProjStatus ED_view3d_project_float_ex(const struct ARegion *ar, float perspmat[4][4], const bool is_local,
215 const float co[3], float r_co[2], const eV3DProjTest flag);
216 eV3DProjStatus ED_view3d_project_float_global(const struct ARegion *ar, const float co[3], float r_co[2], const eV3DProjTest flag);
217 eV3DProjStatus ED_view3d_project_float_object(const struct ARegion *ar, const float co[3], float r_co[2], const eV3DProjTest flag);
219 float ED_view3d_pixel_size(const struct RegionView3D *rv3d, const float co[3]);
220 float ED_view3d_pixel_size_no_ui_scale(const struct RegionView3D *rv3d, const float co[3]);
222 float ED_view3d_calc_zfac(const struct RegionView3D *rv3d, const float co[3], bool *r_flip);
223 bool ED_view3d_clip_segment(const struct RegionView3D *rv3d, float ray_start[3], float ray_end[3]);
224 bool ED_view3d_win_to_ray_clipped(
225 const struct ARegion *ar, const struct View3D *v3d, const float mval[2],
226 float ray_start[3], float ray_normal[3], const bool do_clip);
227 bool ED_view3d_win_to_ray_clipped_ex(
228 const struct ARegion *ar, const struct View3D *v3d, const float mval[2],
229 float r_ray_co[3], float r_ray_normal[3], float r_ray_start[3], bool do_clip);
230 void ED_view3d_win_to_ray(
231 const struct ARegion *ar, const float mval[2],
232 float r_ray_start[3], float r_ray_normal[3]);
233 void ED_view3d_global_to_vector(const struct RegionView3D *rv3d, const float coord[3], float vec[3]);
234 void ED_view3d_win_to_3d(
235 const struct View3D *v3d, const struct ARegion *ar,
236 const float depth_pt[3], const float mval[2],
238 void ED_view3d_win_to_3d_int(
239 const struct View3D *v3d, const struct ARegion *ar,
240 const float depth_pt[3], const int mval[2],
242 bool ED_view3d_win_to_3d_on_plane(
243 const struct ARegion *ar,
244 const float plane[4], const float mval[2],
246 bool ED_view3d_win_to_3d_on_plane_int(
247 const struct ARegion *ar,
248 const float plane[4], const int mval[2],
250 void ED_view3d_win_to_delta(const struct ARegion *ar, const float mval[2], float out[3], const float zfac);
251 void ED_view3d_win_to_origin(const struct ARegion *ar, const float mval[2], float out[3]);
252 void ED_view3d_win_to_vector(const struct ARegion *ar, const float mval[2], float out[3]);
253 bool ED_view3d_win_to_segment_clipped(
254 const struct ARegion *ar, struct View3D *v3d, const float mval[2],
255 float r_ray_start[3], float r_ray_end[3], const bool do_clip);
256 void ED_view3d_ob_project_mat_get(const struct RegionView3D *v3d, struct Object *ob, float pmat[4][4]);
257 void ED_view3d_ob_project_mat_get_from_obmat(const struct RegionView3D *rv3d, float obmat[4][4], float pmat[4][4]);
258 void ED_view3d_unproject(struct bglMats *mats, float out[3], const float x, const float y, const float z);
263 void ED_view3d_dist_range_get(
264 const struct View3D *v3d,
265 float r_dist_range[2]);
266 bool ED_view3d_clip_range_get(
267 const struct View3D *v3d, const struct RegionView3D *rv3d,
268 float *r_clipsta, float *r_clipend, const bool use_ortho_factor);
269 bool ED_view3d_viewplane_get(
270 const struct View3D *v3d, const struct RegionView3D *rv3d, int winxi, int winyi,
271 struct rctf *r_viewplane, float *r_clipsta, float *r_clipend, float *r_pixsize);
273 void ED_view3d_polygon_offset(const struct RegionView3D *rv3d, const float dist);
275 void ED_view3d_calc_camera_border(
276 const struct Scene *scene, const struct ARegion *ar,
277 const struct View3D *v3d, const struct RegionView3D *rv3d,
278 struct rctf *r_viewborder, const bool no_shift);
279 void ED_view3d_calc_camera_border_size(
280 const struct Scene *scene, const struct ARegion *ar,
281 const struct View3D *v3d, const struct RegionView3D *rv3d,
283 bool ED_view3d_calc_render_border(
284 const struct Scene *scene, const struct View3D *v3d,
285 const struct ARegion *ar, struct rcti *rect);
287 void ED_view3d_clipping_calc_from_boundbox(float clip[6][4], const struct BoundBox *clipbb, const bool is_flip);
288 void ED_view3d_clipping_calc(struct BoundBox *bb, float planes[4][4], struct bglMats *mats, const struct rcti *rect);
289 void ED_view3d_clipping_local(struct RegionView3D *rv3d, float mat[4][4]);
290 bool ED_view3d_clipping_test(const struct RegionView3D *rv3d, const float co[3], const bool is_local);
291 void ED_view3d_clipping_set(struct RegionView3D *rv3d);
292 void ED_view3d_clipping_enable(void);
293 void ED_view3d_clipping_disable(void);
295 float ED_view3d_radius_to_dist_persp(const float angle, const float radius);
296 float ED_view3d_radius_to_dist_ortho(const float lens, const float radius);
297 float ED_view3d_radius_to_dist(
298 const struct View3D *v3d, const struct ARegion *ar,
299 const char persp, const bool use_aspect,
302 void drawcircball(int mode, const float cent[3], float rad, const float tmat[4][4]);
304 /* backbuffer select and draw support */
305 void ED_view3d_backbuf_validate(struct ViewContext *vc);
306 struct ImBuf *ED_view3d_backbuf_read(struct ViewContext *vc, int xmin, int ymin, int xmax, int ymax);
307 unsigned int ED_view3d_backbuf_sample_rect(
308 struct ViewContext *vc, const int mval[2], int size,
309 unsigned int min, unsigned int max, float *r_dist);
310 int ED_view3d_backbuf_sample_size_clamp(struct ARegion *ar, const float dist);
311 unsigned int ED_view3d_backbuf_sample(struct ViewContext *vc, int x, int y);
313 bool ED_view3d_autodist(
314 struct Main *bmain, struct Scene *scene, struct ARegion *ar, struct View3D *v3d,
315 const int mval[2], float mouse_worldloc[3],
316 const bool alphaoverride, const float fallback_depth_pt[3]);
318 /* only draw so ED_view3d_autodist_simple can be called many times after */
319 void ED_view3d_autodist_init(struct Main *bmain, struct Scene *scene, struct ARegion *ar, struct View3D *v3d, int mode);
320 bool ED_view3d_autodist_simple(struct ARegion *ar, const int mval[2], float mouse_worldloc[3], int margin, float *force_depth);
321 bool ED_view3d_autodist_depth(struct ARegion *ar, const int mval[2], int margin, float *depth);
322 bool ED_view3d_autodist_depth_seg(struct ARegion *ar, const int mval_sta[2], const int mval_end[2], int margin, float *depth);
325 #define MAXPICKELEMS 2500
326 #define MAXPICKBUF (4 * MAXPICKELEMS)
329 /* all elements in the region, ignore depth */
330 VIEW3D_SELECT_ALL = 0,
331 /* pick also depth sorts (only for small regions!) */
332 VIEW3D_SELECT_PICK_ALL = 1,
333 /* sorts and only returns visible objects (only for small regions!) */
334 VIEW3D_SELECT_PICK_NEAREST = 2,
337 void view3d_opengl_select_cache_begin(void);
338 void view3d_opengl_select_cache_end(void);
340 int view3d_opengl_select(
341 struct ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const struct rcti *input,
342 eV3DSelectMode select_mode);
344 /* view3d_select.c */
345 float ED_view3d_select_dist_px(void);
346 void ED_view3d_viewcontext_init(struct bContext *C, struct ViewContext *vc);
347 void view3d_operator_needs_opengl(const struct bContext *C);
348 void view3d_region_operator_needs_opengl(struct wmWindow *win, struct ARegion *ar);
349 void view3d_opengl_read_pixels(struct ARegion *ar, int x, int y, int w, int h, int format, int type, void *data);
350 void view3d_get_transformation(const struct ARegion *ar, struct RegionView3D *rv3d, struct Object *ob, struct bglMats *mats);
352 /* XXX should move to BLI_math */
353 bool edge_inside_circle(const float cent[2], float radius, const float screen_co_a[2], const float screen_co_b[2]);
355 /* get 3d region from context, also if mouse is in header or toolbar */
356 struct RegionView3D *ED_view3d_context_rv3d(struct bContext *C);
357 bool ED_view3d_context_user_region(struct bContext *C, struct View3D **r_v3d, struct ARegion **r_ar);
358 bool ED_operator_rv3d_user_region_poll(struct bContext *C);
360 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d);
361 void ED_view3d_init_mats_rv3d_gl(struct Object *ob, struct RegionView3D *rv3d);
363 void ED_view3d_clear_mats_rv3d(struct RegionView3D *rv3d);
364 void ED_view3d_check_mats_rv3d(struct RegionView3D *rv3d);
366 # define ED_view3d_clear_mats_rv3d(rv3d) (void)(rv3d)
367 # define ED_view3d_check_mats_rv3d(rv3d) (void)(rv3d)
369 int ED_view3d_scene_layer_set(int lay, const bool *values, int *active);
371 struct RV3DMatrixStore *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d);
372 void ED_view3d_mats_rv3d_restore(struct RegionView3D *rv3d, struct RV3DMatrixStore *rv3dmat);
374 bool ED_view3d_context_activate(struct bContext *C);
375 void ED_view3d_draw_offscreen_init(struct Main *bmain, struct Scene *scene, struct View3D *v3d);
376 void ED_view3d_draw_offscreen(
377 struct Main *bmain, struct Scene *scene, struct View3D *v3d, struct ARegion *ar, int winx, int winy, float viewmat[4][4],
378 float winmat[4][4], bool do_bgpic, bool do_sky, bool is_persp, const char *viewname,
379 struct GPUFX *fx, struct GPUFXSettings *fx_settings,
380 struct GPUOffScreen *ofs);
381 void ED_view3d_draw_setup_view(
382 struct wmWindow *win, struct Scene *scene, struct ARegion *ar, struct View3D *v3d,
383 float viewmat[4][4], float winmat[4][4], const struct rcti *rect);
386 V3D_OFSDRAW_NONE = (0),
388 V3D_OFSDRAW_USE_BACKGROUND = (1 << 0),
389 V3D_OFSDRAW_USE_FULL_SAMPLE = (1 << 1),
391 /* Only works with ED_view3d_draw_offscreen_imbuf_simple(). */
392 V3D_OFSDRAW_USE_GPENCIL = (1 << 2),
393 V3D_OFSDRAW_USE_SOLID_TEX = (1 << 2),
394 V3D_OFSDRAW_USE_CAMERA_DOF = (1 << 3),
397 struct ImBuf *ED_view3d_draw_offscreen_imbuf(
398 struct Main *bmain, struct Scene *scene,
399 struct View3D *v3d, struct ARegion *ar, int sizex, int sizey,
400 unsigned int flag, unsigned int draw_flags,
401 int alpha_mode, int samples, const char *viewname,
402 struct GPUFX *fx, struct GPUOffScreen *ofs, char err_out[256]);
403 struct ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
404 struct Main *bmain, struct Scene *scene,
405 struct Object *camera, int width, int height,
406 unsigned int flag, unsigned int draw_flags, int drawtype, int alpha_mode,
407 int samples, const char *viewname,
408 struct GPUFX *fx, struct GPUOffScreen *ofs, char err_out[256]);
410 struct Base *ED_view3d_give_base_under_cursor(struct bContext *C, const int mval[2]);
411 void ED_view3d_quadview_update(struct ScrArea *sa, struct ARegion *ar, bool do_clip);
412 void ED_view3d_update_viewmat(
413 struct Scene *scene, struct View3D *v3d, struct ARegion *ar,
414 float viewmat[4][4], float winmat[4][4], const struct rcti *rect);
415 bool ED_view3d_quat_from_axis_view(const char view, float quat[4]);
416 char ED_view3d_quat_to_axis_view(const float quat[4], const float epsilon);
417 char ED_view3d_lock_view_from_index(int index);
418 char ED_view3d_axis_view_opposite(char view);
419 bool ED_view3d_lock(struct RegionView3D *rv3d);
421 uint64_t ED_view3d_datamask(const struct Scene *scene, const struct View3D *v3d);
422 uint64_t ED_view3d_screen_datamask(const struct bScreen *screen);
424 bool ED_view3d_offset_lock_check(const struct View3D *v3d, const struct RegionView3D *rv3d);
425 void ED_view3d_persp_switch_from_camera(struct View3D *v3d, struct RegionView3D *rv3d, const char persp);
426 bool ED_view3d_persp_ensure(struct View3D *v3d, struct ARegion *ar);
429 /* camera lock functions */
430 bool ED_view3d_camera_lock_check(const struct View3D *v3d, const struct RegionView3D *rv3d);
431 /* copy the camera to the view before starting a view transformation */
432 void ED_view3d_camera_lock_init_ex(struct View3D *v3d, struct RegionView3D *rv3d, const bool calc_dist);
433 void ED_view3d_camera_lock_init(struct View3D *v3d, struct RegionView3D *rv3d);
434 /* copy the view to the camera, return true if */
435 bool ED_view3d_camera_lock_sync(struct View3D *v3d, struct RegionView3D *rv3d);
437 bool ED_view3d_camera_autokey(
438 struct Scene *scene, struct ID *id_key,
439 struct bContext *C, const bool do_rotate, const bool do_translate);
440 bool ED_view3d_camera_lock_autokey(
441 struct View3D *v3d, struct RegionView3D *rv3d,
442 struct bContext *C, const bool do_rotate, const bool do_translate);
444 void ED_view3d_lock_clear(struct View3D *v3d);
446 struct BGpic *ED_view3d_background_image_new(struct View3D *v3d);
447 void ED_view3d_background_image_remove(struct View3D *v3d, struct BGpic *bgpic);
448 void ED_view3d_background_image_clear(struct View3D *v3d);
450 #define VIEW3D_MARGIN 1.4f
451 #define VIEW3D_DIST_FALLBACK 1.0f
453 float ED_view3d_offset_distance(float mat[4][4], const float ofs[3], const float dist_fallback);
454 void ED_view3d_distance_set(struct RegionView3D *rv3d, const float dist);
456 float ED_scene_grid_scale(struct Scene *scene, const char **grid_unit);
457 float ED_view3d_grid_scale(struct Scene *scene, struct View3D *v3d, const char **grid_unit);
459 void ED_scene_draw_fps(struct Scene *scene, const struct rcti *rect);
461 /* view matrix properties utilities */
464 void ED_view3d_operator_properties_viewmat(struct wmOperatorType *ot);
465 void ED_view3d_operator_properties_viewmat_set(struct bContext *C, struct wmOperator *op);
466 void ED_view3d_operator_properties_viewmat_get(struct wmOperator *op, int *winx, int *winy, float persmat[4][4]);
470 void ED_view3d_stop_render_preview(struct wmWindowManager *wm, struct ARegion *ar);
471 void ED_view3d_shade_update(struct Main *bmain, struct View3D *v3d, struct ScrArea *sa);
473 #define V3D_IS_ZBUF(v3d) \
474 (((v3d)->flag & V3D_ZBUF_SELECT) && ((v3d)->drawtype > OB_WIRE))
476 void ED_view3d_id_remap(struct View3D *v3d, const struct ID *old_id, struct ID *new_id);
478 #endif /* __ED_VIEW3D_H__ */