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 *****
34 /* ********* exports for space_view3d/ module ********** */
57 /* for derivedmesh drawing callbacks, for view3d_select, .... */
58 typedef struct ViewContext {
61 struct Object *obedit;
64 struct RegionView3D *rv3d;
69 typedef struct ViewDepths {
71 short x, y; /* only for temp use for sub-rects, added to ar->winx/y */
73 double depth_range[2];
78 /* enum for passing to foreach functions to test RV3D_CLIPPING */
79 typedef enum eV3DClipTest {
80 V3D_CLIP_TEST_OFF = 0, /* clipping is off */
81 V3D_CLIP_TEST_RV3D_CLIPPING = 1, /* clip single points */
82 V3D_CLIP_TEST_REGION = 2 /* use for edges to check if both verts are in the view, but not RV3D_CLIPPING */
85 float *give_cursor(struct Scene *scene, struct View3D *v3d);
87 int initgrabz(struct RegionView3D *rv3d, float x, float y, float z);
90 * Calculate a 3d location from 2d window coordinates.
91 * @param ar The region (used for the window width and height).
92 * @param depth_pt The reference location used to calculate the Z depth.
93 * @param mval The area relative location (such as event->mval converted to floats).
94 * @param out The resulting world-space location.
96 void ED_view3d_win_to_3d(struct ARegion *ar, const float depth_pt[3], const float mval[2], float out[3]);
99 * Calculate a 3d difference vector from 2d window offset.
100 * note that initgrabz() must be called first to determine
101 * the depth used to calculate the delta.
102 * @param ar The region (used for the window width and height).
103 * @param mval The area relative 2d difference (such as event->mval[0] - other_x).
104 * @param out The resulting world-space delta.
106 void ED_view3d_win_to_delta(struct ARegion *ar, const float mval[2], float out[3]);
109 * Calculate a 3d direction vector from 2d window coordinates.
110 * This direction vector starts and the view in the direction of the 2d window coordinates.
111 * In orthographic view all window coordinates yield the same vector.
112 * @param ar The region (used for the window width and height).
113 * @param mval The area relative 2d location (such as event->mval converted to floats).
114 * @param out The resulting normalized world-space direction vector.
116 void ED_view3d_win_to_vector(struct ARegion *ar, const float mval[2], float out[3]);
119 * Calculate a 3d segment from 2d window coordinates.
120 * This ray_start is located at the viewpoint, ray_end is a far point.
121 * ray_start and ray_end are clipped by the view near and far limits
122 * so points along this line are always in view.
123 * In orthographic view all resulting segments will be parallel.
124 * @param ar The region (used for the window width and height).
125 * @param v3d The 3d viewport (used for near and far clipping range).
126 * @param mval The area relative 2d location (such as event->mval, converted into float[2]).
127 * @param ray_start The world-space starting point of the segment.
128 * @param ray_end The world-space end point of the segment.
130 void ED_view3d_win_to_segment_clip(struct ARegion *ar, struct View3D *v3d, const float mval[2], float ray_start[3], float ray_end[3]);
133 * Calculate a 3d viewpoint and direction vector from 2d window coordinates.
134 * This ray_start is located at the viewpoint, ray_normal is the direction towards mval.
135 * ray_start is clipped by the view near limit so points in front of it are always in view.
136 * In orthographic view the resulting ray_normal will match the view vector.
137 * @param ar The region (used for the window width and height).
138 * @param v3d The 3d viewport (used for near clipping value).
139 * @param mval The area relative 2d location (such as event->mval, converted into float[2]).
140 * @param ray_start The world-space starting point of the segment.
141 * @param ray_normal The normalized world-space direction of towards mval.
143 void ED_view3d_win_to_ray(struct ARegion *ar, struct View3D *v3d, const float mval[2], float ray_start[3], float ray_normal[3]);
146 * Calculate a normalized 3d direction vector from the viewpoint towards a global location.
147 * In orthographic view the resulting vector will match the view vector.
148 * @param rv3d The region (used for the window width and height).
149 * @param coord The world-space location.
150 * @param vec The resulting normalized vector.
152 void ED_view3d_global_to_vector(struct RegionView3D *rv3d, const float coord[3], float vec[3]);
155 * Calculate the view transformation matrix from RegionView3D input.
156 * The resulting matrix is equivilent to RegionView3D.viewinv
157 * @param mat The view 4x4 transformation matrix to calculate.
158 * @param ofs The view offset, normally from RegionView3D.ofs.
159 * @param quat The view rotation, quaternion normally from RegionView3D.viewquat.
160 * @param dist The view distance from ofs, normally from RegionView3D.dist.
162 void ED_view3d_to_m4(float mat[][4], const float ofs[3], const float quat[4], const float dist);
165 * Set the view transformation from a 4x4 matrix.
166 * @param mat The view 4x4 transformation matrix to assign.
167 * @param ofs The view offset, normally from RegionView3D.ofs.
168 * @param quat The view rotation, quaternion normally from RegionView3D.viewquat.
169 * @param dist The view distance from ofs, normally from RegionView3D.dist.
171 void ED_view3d_from_m4(float mat[][4], float ofs[3], float quat[4], float *dist);
174 * Set the RegionView3D members from an objects transformation and optionally lens.
175 * @param ob The object to set the view to.
176 * @param ofs The view offset to be set, normally from RegionView3D.ofs.
177 * @param quat The view rotation to be set, quaternion normally from RegionView3D.viewquat.
178 * @param dist The view distance from ofs to be set, normally from RegionView3D.dist.
179 * @param lens The view lens angle set for cameras and lamps, normally from View3D.lens.
181 void ED_view3d_from_object(struct Object *ob, float ofs[3], float quat[4], float *dist, float *lens);
184 * Set the object transformation from RegionView3D members.
185 * @param ob The object which has the transformation assigned.
186 * @param ofs The view offset, normally from RegionView3D.ofs.
187 * @param quat The view rotation, quaternion normally from RegionView3D.viewquat.
188 * @param dist The view distance from ofs, normally from RegionView3D.dist.
190 void ED_view3d_to_object(struct Object *ob, const float ofs[3], const float quat[4], const float dist);
193 void view3d_unproject(struct bglMats *mats, float out[3], const short x, const short y, const float z);
197 void ED_view3d_depth_update(struct ARegion *ar);
198 float ED_view3d_depth_read_cached(struct ViewContext *vc, int x, int y);
199 void ED_view3d_depth_tag_update(struct RegionView3D *rv3d);
202 #define IS_CLIPPED 12000
204 void ED_view3d_calc_clipping(struct BoundBox *bb, float planes[4][4], struct bglMats *mats, const struct rcti *rect);
206 void project_short(struct ARegion *ar, const float vec[3], short adr[2]);
207 void project_short_noclip(struct ARegion *ar, const float vec[3], short adr[2]);
209 void project_int(struct ARegion *ar, const float vec[3], int adr[2]);
210 void project_int_noclip(struct ARegion *ar, const float vec[3], int adr[2]);
212 void project_float(struct ARegion *ar, const float vec[3], float adr[2]);
213 void project_float_noclip(struct ARegion *ar, const float vec[3], float adr[2]);
215 int ED_view3d_clip_range_get(struct View3D *v3d, struct RegionView3D *rv3d, float *clipsta, float *clipend);
216 int ED_view3d_viewplane_get(struct View3D *v3d, struct RegionView3D *rv3d, int winxi, int winyi, struct rctf *viewplane, float *clipsta, float *clipend);
217 void ED_view3d_ob_project_mat_get(struct RegionView3D *v3d, struct Object *ob, float pmat[4][4]);
218 void ED_view3d_project_float(const struct ARegion *a, const float vec[3], float adr[2], float mat[4][4]);
219 void ED_view3d_calc_camera_border(struct Scene *scene, struct ARegion *ar, struct View3D *v3d, struct RegionView3D *rv3d, struct rctf *viewborder_r, short no_shift);
220 void ED_view3d_calc_camera_border_size(struct Scene *scene, struct ARegion *ar, struct View3D *v3d, struct RegionView3D *rv3d, float size_r[2]);
222 /* drawobject.c iterators */
223 void mesh_foreachScreenVert(struct ViewContext *vc, void (*func)(void *userData, struct EditVert *eve, int x, int y, int index), void *userData, eV3DClipTest clipVerts);
224 void mesh_foreachScreenEdge(struct ViewContext *vc, void (*func)(void *userData, struct EditEdge *eed, int x0, int y0, int x1, int y1, int index), void *userData, eV3DClipTest clipVerts);
225 void mesh_foreachScreenFace(struct ViewContext *vc, void (*func)(void *userData, struct EditFace *efa, int x, int y, int index), void *userData);
226 void nurbs_foreachScreenVert(struct ViewContext *vc, void (*func)(void *userData, struct Nurb *nu, struct BPoint *bp, struct BezTriple *bezt, int beztindex, int x, int y), void *userData);
227 void lattice_foreachScreenVert(struct ViewContext *vc, void (*func)(void *userData, struct BPoint *bp, int x, int y), void *userData);
229 void ED_view3d_local_clipping(struct RegionView3D *rv3d, float mat[][4]);
230 int ED_view3d_test_clipping(struct RegionView3D *rv3d, const float vec[3], const int local);
231 void ED_view3d_align_axis_to_vector(struct View3D *v3d, struct RegionView3D *rv3d, int axisidx, float vec[3]);
232 float ED_view3d_pixel_size(struct RegionView3D *rv3d, const float co[3]);
234 void drawcircball(int mode, const float cent[3], float rad, float tmat[][4]);
236 /* backbuffer select and draw support */
237 void view3d_validate_backbuf(struct ViewContext *vc);
238 struct ImBuf *view3d_read_backbuf(struct ViewContext *vc, short xmin, short ymin, short xmax, short ymax);
239 unsigned int view3d_sample_backbuf_rect(struct ViewContext *vc, const int mval[2], int size, unsigned int min, unsigned int max, int *dist, short strict,
240 void *handle, unsigned int (*indextest)(void *handle, unsigned int index));
241 unsigned int view3d_sample_backbuf(struct ViewContext *vc, int x, int y);
243 /* draws and does a 4x4 sample */
244 int ED_view3d_autodist(struct Scene *scene, struct ARegion *ar, struct View3D *v3d, const int mval[2], float mouse_worldloc[3]);
246 /* only draw so ED_view3d_autodist_simple can be called many times after */
247 int ED_view3d_autodist_init(struct Scene *scene, struct ARegion *ar, struct View3D *v3d, int mode);
248 int ED_view3d_autodist_simple(struct ARegion *ar, const int mval[2], float mouse_worldloc[3], int margin, float *force_depth);
249 int ED_view3d_autodist_depth(struct ARegion *ar, const int mval[2], int margin, float *depth);
250 int ED_view3d_autodist_depth_seg(struct ARegion *ar, const int mval_sta[2], const int mval_end[2], int margin, float *depth);
253 #define MAXPICKBUF 10000
254 short view3d_opengl_select(struct ViewContext *vc, unsigned int *buffer, unsigned int bufsize, rcti *input);
256 void view3d_set_viewcontext(struct bContext *C, struct ViewContext *vc);
257 void view3d_operator_needs_opengl(const struct bContext *C);
258 void view3d_region_operator_needs_opengl(struct wmWindow *win, struct ARegion *ar);
259 int view3d_get_view_aligned_coordinate(struct ViewContext *vc, float fp[3], const int mval[2], const short do_fallback);
260 void view3d_get_transformation(const struct ARegion *ar, struct RegionView3D *rv3d, struct Object *ob, struct bglMats *mats);
262 /* XXX should move to BLI_math */
263 int edge_inside_circle(short centx, short centy, short rad, short x1, short y1, short x2, short y2);
264 int lasso_inside(int mcords[][2], short moves, int sx, int sy);
265 int lasso_inside_edge(int mcords[][2], short moves, int x0, int y0, int x1, int y1);
267 /* get 3d region from context, also if mouse is in header or toolbar */
268 struct RegionView3D *ED_view3d_context_rv3d(struct bContext *C);
269 struct ARegion *ED_view3d_context_region_unlock(struct bContext *C);
270 int ED_operator_rv3d_unlock_poll(struct bContext *C);
272 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d);
273 void ED_view3d_init_mats_rv3d_gl(struct Object *ob, struct RegionView3D *rv3d);
275 int ED_view3d_scene_layer_set(int lay, const int *values, int *active);
277 int ED_view3d_context_activate(struct bContext *C);
278 void ED_view3d_draw_offscreen(struct Scene *scene, struct View3D *v3d, struct ARegion *ar,
279 int winx, int winy, float viewmat[][4], float winmat[][4]);
281 struct ImBuf *ED_view3d_draw_offscreen_imbuf(struct Scene *scene, struct View3D *v3d, struct ARegion *ar, int sizex, int sizey, unsigned int flag, char err_out[256]);
282 struct ImBuf *ED_view3d_draw_offscreen_imbuf_simple(Scene *scene, struct Object *camera, int width, int height, unsigned int flag, int drawtype, char err_out[256]);
285 Base *ED_view3d_give_base_under_cursor(struct bContext *C, const int mval[2]);
286 void ED_view3d_quadview_update(struct ScrArea *sa, struct ARegion *ar, short do_clip);
287 int ED_view3d_lock(struct RegionView3D *rv3d);
289 uint64_t ED_view3d_datamask(struct Scene *scene, struct View3D *v3d);
290 uint64_t ED_viewedit_datamask(struct bScreen *screen);
292 /* camera lock functions */
293 int ED_view3d_camera_lock_check(struct View3D *v3d, struct RegionView3D *rv3d);
294 /* copy the camera to the view before starting a view transformation */
295 void ED_view3d_camera_lock_init(struct View3D *v3d, struct RegionView3D *rv3d);
296 /* copy the view to the camera, return TRUE if */
297 int ED_view3d_camera_lock_sync(struct View3D *v3d, struct RegionView3D *rv3d);
299 struct BGpic *ED_view3D_background_image_new(struct View3D *v3d);
300 void ED_view3D_background_image_remove(struct View3D *v3d, struct BGpic *bgpic);
301 void ED_view3D_background_image_clear(struct View3D *v3d);
303 #endif /* ED_VIEW3D_H */