Merge branch 'blender2.7'
[blender.git] / source / blender / editors / space_view3d / view3d_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_draw.c
28  *  \ingroup spview3d
29  */
30
31 #include <math.h>
32
33 #include "BLI_listbase.h"
34 #include "BLI_math.h"
35 #include "BLI_rect.h"
36 #include "BLI_string.h"
37 #include "BLI_threads.h"
38 #include "BLI_jitter_2d.h"
39
40 #include "BIF_gl.h"
41 #include "BIF_glutil.h"
42
43 #include "BKE_camera.h"
44 #include "BKE_collection.h"
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_key.h"
48 #include "BKE_main.h"
49 #include "BKE_scene.h"
50 #include "BKE_object.h"
51 #include "BKE_paint.h"
52 #include "BKE_unit.h"
53
54 #include "BLF_api.h"
55
56 #include "BLT_translation.h"
57
58 #include "DNA_armature_types.h"
59 #include "DNA_brush_types.h"
60 #include "DNA_camera_types.h"
61 #include "DNA_key_types.h"
62 #include "DNA_mesh_types.h"
63 #include "DNA_object_types.h"
64 #include "DNA_view3d_types.h"
65 #include "DNA_windowmanager_types.h"
66
67 #include "DRW_engine.h"
68
69 #include "ED_armature.h"
70 #include "ED_keyframing.h"
71 #include "ED_gpencil.h"
72 #include "ED_screen.h"
73 #include "ED_transform.h"
74
75 #include "DEG_depsgraph_query.h"
76
77 #include "GPU_batch.h"
78 #include "GPU_batch_presets.h"
79 #include "GPU_draw.h"
80 #include "GPU_matrix.h"
81 #include "GPU_immediate.h"
82 #include "GPU_immediate_util.h"
83 #include "GPU_material.h"
84 #include "GPU_viewport.h"
85 #include "GPU_state.h"
86 #include "GPU_framebuffer.h"
87
88 #include "MEM_guardedalloc.h"
89
90 #include "UI_interface.h"
91 #include "UI_resources.h"
92
93 #include "RE_engine.h"
94
95 #include "WM_api.h"
96 #include "WM_types.h"
97
98 #include "RNA_access.h"
99
100 #include "IMB_imbuf.h"
101 #include "IMB_imbuf_types.h"
102
103 #include "view3d_intern.h"  /* own include */
104
105 /* ******************** general functions ***************** */
106
107 /**
108  * \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore
109  */
110 void ED_view3d_update_viewmat(
111         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
112         float viewmat[4][4], float winmat[4][4], const rcti *rect)
113 {
114         RegionView3D *rv3d = ar->regiondata;
115
116         /* setup window matrices */
117         if (winmat)
118                 copy_m4_m4(rv3d->winmat, winmat);
119         else
120                 view3d_winmatrix_set(depsgraph, ar, v3d, rect);
121
122         /* setup view matrix */
123         if (viewmat) {
124                 copy_m4_m4(rv3d->viewmat, viewmat);
125         }
126         else {
127                 float rect_scale[2];
128                 if (rect) {
129                         rect_scale[0] = (float)BLI_rcti_size_x(rect) / (float)ar->winx;
130                         rect_scale[1] = (float)BLI_rcti_size_y(rect) / (float)ar->winy;
131                 }
132                 /* note: calls BKE_object_where_is_calc for camera... */
133                 view3d_viewmatrix_set(depsgraph, scene, v3d, rv3d, rect ? rect_scale : NULL);
134         }
135         /* update utility matrices */
136         mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
137         invert_m4_m4(rv3d->persinv, rv3d->persmat);
138         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
139
140         /* calculate GLSL view dependent values */
141
142         /* store window coordinates scaling/offset */
143         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
144                 rctf cameraborder;
145                 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &cameraborder, false);
146                 rv3d->viewcamtexcofac[0] = (float)ar->winx / BLI_rctf_size_x(&cameraborder);
147                 rv3d->viewcamtexcofac[1] = (float)ar->winy / BLI_rctf_size_y(&cameraborder);
148
149                 rv3d->viewcamtexcofac[2] = -rv3d->viewcamtexcofac[0] * cameraborder.xmin / (float)ar->winx;
150                 rv3d->viewcamtexcofac[3] = -rv3d->viewcamtexcofac[1] * cameraborder.ymin / (float)ar->winy;
151         }
152         else {
153                 rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f;
154                 rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f;
155         }
156
157         /* calculate pixelsize factor once, is used for lamps and obcenters */
158         {
159                 /* note:  '1.0f / len_v3(v1)'  replaced  'len_v3(rv3d->viewmat[0])'
160                  * because of float point precision problems at large values [#23908] */
161                 float v1[3], v2[3];
162                 float len_px, len_sc;
163
164                 v1[0] = rv3d->persmat[0][0];
165                 v1[1] = rv3d->persmat[1][0];
166                 v1[2] = rv3d->persmat[2][0];
167
168                 v2[0] = rv3d->persmat[0][1];
169                 v2[1] = rv3d->persmat[1][1];
170                 v2[2] = rv3d->persmat[2][1];
171
172                 len_px = 2.0f / sqrtf(min_ff(len_squared_v3(v1), len_squared_v3(v2)));
173                 len_sc = (float)MAX2(ar->winx, ar->winy);
174
175                 rv3d->pixsize = len_px / len_sc;
176         }
177 }
178
179 static void view3d_main_region_setup_view(
180         Depsgraph *depsgraph, Scene *scene,
181         View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4], const rcti *rect)
182 {
183         RegionView3D *rv3d = ar->regiondata;
184
185         ED_view3d_update_viewmat(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
186
187         /* set for opengl */
188         GPU_matrix_projection_set(rv3d->winmat);
189         GPU_matrix_set(rv3d->viewmat);
190 }
191
192 static bool view3d_stereo3d_active(wmWindow *win, Scene *scene, View3D *v3d, RegionView3D *rv3d)
193 {
194         if ((scene->r.scemode & R_MULTIVIEW) == 0) {
195                 return false;
196         }
197
198         if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) {
199                 return false;
200         }
201
202         switch (v3d->stereo3d_camera) {
203                 case STEREO_MONO_ID:
204                         return false;
205                         break;
206                 case STEREO_3D_ID:
207                         /* win will be NULL when calling this from the selection or draw loop. */
208                         if ((win == NULL) || (WM_stereo3d_enabled(win, true) == false)) {
209                                 return false;
210                         }
211                         if (((scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) != 0) &&
212                             !BKE_scene_multiview_is_stereo3d(&scene->r))
213                         {
214                                 return false;
215                         }
216                         break;
217                 /* We always need the stereo calculation for left and right cameras. */
218                 case STEREO_LEFT_ID:
219                 case STEREO_RIGHT_ID:
220                 default:
221                         break;
222         }
223         return true;
224 }
225
226
227 /* setup the view and win matrices for the multiview cameras
228  *
229  * unlike view3d_stereo3d_setup_offscreen, when view3d_stereo3d_setup is called
230  * we have no winmatrix (i.e., projection matrix) defined at that time.
231  * Since the camera and the camera shift are needed for the winmat calculation
232  * we do a small hack to replace it temporarily so we don't need to change the
233  * view3d)main_region_setup_view() code to account for that.
234  */
235 static void view3d_stereo3d_setup(
236         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar, const rcti *rect)
237 {
238         bool is_left;
239         const char *names[2] = { STEREO_LEFT_NAME, STEREO_RIGHT_NAME };
240         const char *viewname;
241
242         /* show only left or right camera */
243         if (v3d->stereo3d_camera != STEREO_3D_ID)
244                 v3d->multiview_eye = v3d->stereo3d_camera;
245
246         is_left = v3d->multiview_eye == STEREO_LEFT_ID;
247         viewname = names[is_left ? STEREO_LEFT_ID : STEREO_RIGHT_ID];
248
249         /* update the viewport matrices with the new camera */
250         if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
251                 Camera *data, *data_eval;
252                 float viewmat[4][4];
253                 float shiftx;
254
255                 data = (Camera *)v3d->camera->data;
256                 data_eval = (Camera *)DEG_get_evaluated_id(depsgraph, &data->id);
257
258                 shiftx = data_eval->shiftx;
259
260                 BLI_thread_lock(LOCK_VIEW3D);
261                 data_eval->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname);
262
263                 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
264                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
265
266                 data_eval->shiftx = shiftx;
267                 BLI_thread_unlock(LOCK_VIEW3D);
268         }
269         else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
270                 float viewmat[4][4];
271                 Object *view_ob = v3d->camera;
272                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
273
274                 BLI_thread_lock(LOCK_VIEW3D);
275                 v3d->camera = camera;
276
277                 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
278                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
279
280                 v3d->camera = view_ob;
281                 BLI_thread_unlock(LOCK_VIEW3D);
282         }
283 }
284
285 /**
286  * Set the correct matrices
287  */
288 void ED_view3d_draw_setup_view(
289         wmWindow *win, Depsgraph *depsgraph, Scene *scene, ARegion *ar, View3D *v3d,
290         float viewmat[4][4], float winmat[4][4], const rcti *rect)
291 {
292         RegionView3D *rv3d = ar->regiondata;
293
294         /* Setup the view matrix. */
295         if (view3d_stereo3d_active(win, scene, v3d, rv3d)) {
296                 view3d_stereo3d_setup(depsgraph, scene, v3d, ar, rect);
297         }
298         else {
299                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
300         }
301 }
302
303 /* ******************** view border ***************** */
304
305 static void view3d_camera_border(
306         const Scene *scene, struct Depsgraph *depsgraph,
307         const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
308         rctf *r_viewborder, const bool no_shift, const bool no_zoom)
309 {
310         CameraParams params;
311         rctf rect_view, rect_camera;
312         Object *camera_eval = DEG_get_evaluated_object(depsgraph, v3d->camera);
313
314         /* get viewport viewplane */
315         BKE_camera_params_init(&params);
316         BKE_camera_params_from_view3d(&params, depsgraph, v3d, rv3d);
317         if (no_zoom)
318                 params.zoom = 1.0f;
319         BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
320         rect_view = params.viewplane;
321
322         /* get camera viewplane */
323         BKE_camera_params_init(&params);
324         /* fallback for non camera objects */
325         params.clipsta = v3d->near;
326         params.clipend = v3d->far;
327         BKE_camera_params_from_object(&params, camera_eval);
328         if (no_shift) {
329                 params.shiftx = 0.0f;
330                 params.shifty = 0.0f;
331         }
332         BKE_camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
333         rect_camera = params.viewplane;
334
335         /* get camera border within viewport */
336         r_viewborder->xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
337         r_viewborder->xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
338         r_viewborder->ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
339         r_viewborder->ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
340 }
341
342 void ED_view3d_calc_camera_border_size(
343         const Scene *scene, Depsgraph *depsgraph,
344         const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
345         float r_size[2])
346 {
347         rctf viewborder;
348
349         view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, true, true);
350         r_size[0] = BLI_rctf_size_x(&viewborder);
351         r_size[1] = BLI_rctf_size_y(&viewborder);
352 }
353
354 void ED_view3d_calc_camera_border(
355         const Scene *scene, Depsgraph *depsgraph,
356         const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
357         rctf *r_viewborder, const bool no_shift)
358 {
359         view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, r_viewborder, no_shift, false);
360 }
361
362 static void drawviewborder_grid3(uint shdr_pos, float x1, float x2, float y1, float y2, float fac)
363 {
364         float x3, y3, x4, y4;
365
366         x3 = x1 + fac * (x2 - x1);
367         y3 = y1 + fac * (y2 - y1);
368         x4 = x1 + (1.0f - fac) * (x2 - x1);
369         y4 = y1 + (1.0f - fac) * (y2 - y1);
370
371         immBegin(GPU_PRIM_LINES, 8);
372
373         immVertex2f(shdr_pos, x1, y3);
374         immVertex2f(shdr_pos, x2, y3);
375
376         immVertex2f(shdr_pos, x1, y4);
377         immVertex2f(shdr_pos, x2, y4);
378
379         immVertex2f(shdr_pos, x3, y1);
380         immVertex2f(shdr_pos, x3, y2);
381
382         immVertex2f(shdr_pos, x4, y1);
383         immVertex2f(shdr_pos, x4, y2);
384
385         immEnd();
386 }
387
388 /* harmonious triangle */
389 static void drawviewborder_triangle(
390         uint shdr_pos, float x1, float x2, float y1, float y2, const char golden, const char dir)
391 {
392         float ofs;
393         float w = x2 - x1;
394         float h = y2 - y1;
395
396         immBegin(GPU_PRIM_LINES, 6);
397
398         if (w > h) {
399                 if (golden) {
400                         ofs = w * (1.0f - (1.0f / 1.61803399f));
401                 }
402                 else {
403                         ofs = h * (h / w);
404                 }
405                 if (dir == 'B') SWAP(float, y1, y2);
406
407                 immVertex2f(shdr_pos, x1, y1);
408                 immVertex2f(shdr_pos, x2, y2);
409
410                 immVertex2f(shdr_pos, x2, y1);
411                 immVertex2f(shdr_pos, x1 + (w - ofs), y2);
412
413                 immVertex2f(shdr_pos, x1, y2);
414                 immVertex2f(shdr_pos, x1 + ofs, y1);
415         }
416         else {
417                 if (golden) {
418                         ofs = h * (1.0f - (1.0f / 1.61803399f));
419                 }
420                 else {
421                         ofs = w * (w / h);
422                 }
423                 if (dir == 'B') SWAP(float, x1, x2);
424
425                 immVertex2f(shdr_pos, x1, y1);
426                 immVertex2f(shdr_pos, x2, y2);
427
428                 immVertex2f(shdr_pos, x2, y1);
429                 immVertex2f(shdr_pos, x1, y1 + ofs);
430
431                 immVertex2f(shdr_pos, x1, y2);
432                 immVertex2f(shdr_pos, x2, y1 + (h - ofs));
433         }
434
435         immEnd();
436 }
437
438 static void drawviewborder(Scene *scene, Depsgraph *depsgraph, ARegion *ar, View3D *v3d)
439 {
440         float x1, x2, y1, y2;
441         float x1i, x2i, y1i, y2i;
442
443         rctf viewborder;
444         Camera *ca = NULL;
445         RegionView3D *rv3d = ar->regiondata;
446
447         if (v3d->camera == NULL)
448                 return;
449         if (v3d->camera->type == OB_CAMERA)
450                 ca = v3d->camera->data;
451
452         ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, false);
453         /* the offsets */
454         x1 = viewborder.xmin;
455         y1 = viewborder.ymin;
456         x2 = viewborder.xmax;
457         y2 = viewborder.ymax;
458
459         GPU_line_width(1.0f);
460
461         /* apply offsets so the real 3D camera shows through */
462
463         /* note: quite un-scientific but without this bit extra
464          * 0.0001 on the lower left the 2D border sometimes
465          * obscures the 3D camera border */
466         /* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
467          * but keep it here in case we need to remove the workaround */
468         x1i = (int)(x1 - 1.0001f);
469         y1i = (int)(y1 - 1.0001f);
470         x2i = (int)(x2 + (1.0f - 0.0001f));
471         y2i = (int)(y2 + (1.0f - 0.0001f));
472
473         uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
474
475         /* First, solid lines. */
476         {
477                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
478
479                 /* passepartout, specified in camera edit buttons */
480                 if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
481                         const float winx = (ar->winx + 1);
482                         const float winy = (ar->winy + 1);
483
484                         float alpha = 1.0f;
485
486                         if (ca->passepartalpha != 1.0f) {
487                                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
488                                 GPU_blend(true);
489                                 alpha = ca->passepartalpha;
490                         }
491
492                         immUniformColor4f(0.0f, 0.0f, 0.0f, alpha);
493
494                         if (x1i > 0.0f)
495                                 immRectf(shdr_pos, 0.0f, winy, x1i, 0.0f);
496                         if (x2i < winx)
497                                 immRectf(shdr_pos, x2i, winy, winx, 0.0f);
498                         if (y2i < winy)
499                                 immRectf(shdr_pos, x1i, winy, x2i, y2i);
500                         if (y2i > 0.0f)
501                                 immRectf(shdr_pos, x1i, y1i, x2i, 0.0f);
502
503                         GPU_blend(false);
504                 }
505
506                 immUniformThemeColor(TH_BACK);
507                 imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
508
509 #ifdef VIEW3D_CAMERA_BORDER_HACK
510                 if (view3d_camera_border_hack_test == true) {
511                         immUniformColor3ubv(view3d_camera_border_hack_col);
512                         imm_draw_box_wire_2d(shdr_pos, x1i + 1, y1i + 1, x2i - 1, y2i - 1);
513                         view3d_camera_border_hack_test = false;
514                 }
515 #endif
516
517                 immUnbindProgram();
518         }
519
520         /* When overlays are disabled, only show camera outline & passepartout. */
521         if (v3d->flag2 & V3D_RENDER_OVERRIDE) {
522                 return;
523         }
524
525         /* And now, the dashed lines! */
526         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
527
528         {
529                 float viewport_size[4];
530                 GPU_viewport_size_get_f(viewport_size);
531                 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
532
533                 immUniform1i("colors_len", 0);  /* "simple" mode */
534                 immUniform1f("dash_width", 6.0f);
535                 immUniform1f("dash_factor", 0.5f);
536
537                 /* outer line not to confuse with object selection */
538                 if (v3d->flag2 & V3D_LOCK_CAMERA) {
539                         immUniformThemeColor(TH_REDALERT);
540                         imm_draw_box_wire_2d(shdr_pos, x1i - 1, y1i - 1, x2i + 1, y2i + 1);
541                 }
542
543                 immUniformThemeColor(TH_VIEW_OVERLAY);
544                 imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
545         }
546
547         /* Render Border. */
548         if (scene->r.mode & R_BORDER) {
549                 float x3, y3, x4, y4;
550
551                 x3 = floorf(x1 + (scene->r.border.xmin * (x2 - x1))) - 1;
552                 y3 = floorf(y1 + (scene->r.border.ymin * (y2 - y1))) - 1;
553                 x4 = floorf(x1 + (scene->r.border.xmax * (x2 - x1))) + (U.pixelsize - 1);
554                 y4 = floorf(y1 + (scene->r.border.ymax * (y2 - y1))) + (U.pixelsize - 1);
555
556                 immUniformColor3f(1.0f, 0.25f, 0.25f);
557                 imm_draw_box_wire_2d(shdr_pos, x3, y3, x4, y4);
558         }
559
560         /* safety border */
561         if (ca) {
562                 immUniformThemeColorBlend(TH_VIEW_OVERLAY, TH_BACK, 0.25f);
563
564                 if (ca->dtx & CAM_DTX_CENTER) {
565                         float x3, y3;
566
567                         x3 = x1 + 0.5f * (x2 - x1);
568                         y3 = y1 + 0.5f * (y2 - y1);
569
570                         immBegin(GPU_PRIM_LINES, 4);
571
572                         immVertex2f(shdr_pos, x1, y3);
573                         immVertex2f(shdr_pos, x2, y3);
574
575                         immVertex2f(shdr_pos, x3, y1);
576                         immVertex2f(shdr_pos, x3, y2);
577
578                         immEnd();
579                 }
580
581                 if (ca->dtx & CAM_DTX_CENTER_DIAG) {
582                         immBegin(GPU_PRIM_LINES, 4);
583
584                         immVertex2f(shdr_pos, x1, y1);
585                         immVertex2f(shdr_pos, x2, y2);
586
587                         immVertex2f(shdr_pos, x1, y2);
588                         immVertex2f(shdr_pos, x2, y1);
589
590                         immEnd();
591                 }
592
593                 if (ca->dtx & CAM_DTX_THIRDS) {
594                         drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f / 3.0f);
595                 }
596
597                 if (ca->dtx & CAM_DTX_GOLDEN) {
598                         drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f - (1.0f / 1.61803399f));
599                 }
600
601                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
602                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'A');
603                 }
604
605                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
606                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'B');
607                 }
608
609                 if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
610                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'A');
611                 }
612
613                 if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
614                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'B');
615                 }
616
617                 if (ca->flag & CAM_SHOW_SAFE_MARGINS) {
618                         UI_draw_safe_areas(
619                                 shdr_pos, x1, x2, y1, y2,
620                                 scene->safe_areas.title, scene->safe_areas.action);
621
622                         if (ca->flag & CAM_SHOW_SAFE_CENTER) {
623                                 UI_draw_safe_areas(
624                                         shdr_pos, x1, x2, y1, y2,
625                                         scene->safe_areas.title_center, scene->safe_areas.action_center);
626                         }
627                 }
628
629                 if (ca->flag & CAM_SHOWSENSOR) {
630                         /* determine sensor fit, and get sensor x/y, for auto fit we
631                          * assume and square sensor and only use sensor_x */
632                         float sizex = scene->r.xsch * scene->r.xasp;
633                         float sizey = scene->r.ysch * scene->r.yasp;
634                         int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
635                         float sensor_x = ca->sensor_x;
636                         float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
637
638                         /* determine sensor plane */
639                         rctf rect;
640
641                         if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
642                                 float sensor_scale = (x2i - x1i) / sensor_x;
643                                 float sensor_height = sensor_scale * sensor_y;
644
645                                 rect.xmin = x1i;
646                                 rect.xmax = x2i;
647                                 rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
648                                 rect.ymax = rect.ymin + sensor_height;
649                         }
650                         else {
651                                 float sensor_scale = (y2i - y1i) / sensor_y;
652                                 float sensor_width = sensor_scale * sensor_x;
653
654                                 rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
655                                 rect.xmax = rect.xmin + sensor_width;
656                                 rect.ymin = y1i;
657                                 rect.ymax = y2i;
658                         }
659
660                         /* draw */
661                         immUniformThemeColorShade(TH_VIEW_OVERLAY, 100);
662
663                         /* TODO Was using UI_draw_roundbox_4fv(false, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f, color).
664                          * We'll probably need a new imm_draw_line_roundbox_dashed dor that - though in practice the
665                          * 2.0f round corner effect was nearly not visible anyway... */
666                         imm_draw_box_wire_2d(shdr_pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
667                 }
668         }
669
670         immUnbindProgram();
671         /* end dashed lines */
672
673         /* camera name - draw in highlighted text color */
674         if (ca && ((v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) && (ca->flag & CAM_SHOWNAME)) {
675                 UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
676                 BLF_draw_default(
677                         x1i, y1i - (0.7f * U.widget_unit), 0.0f,
678                         v3d->camera->id.name + 2, sizeof(v3d->camera->id.name) - 2);
679         }
680 }
681
682 static void drawrenderborder(ARegion *ar, View3D *v3d)
683 {
684         /* use the same program for everything */
685         uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
686
687         GPU_line_width(1.0f);
688
689         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
690
691         float viewport_size[4];
692         GPU_viewport_size_get_f(viewport_size);
693         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
694
695         immUniform1i("colors_len", 0);  /* "simple" mode */
696         immUniform4f("color", 1.0f, 0.25f, 0.25f, 1.0f);
697         immUniform1f("dash_width", 6.0f);
698         immUniform1f("dash_factor", 0.5f);
699
700         imm_draw_box_wire_2d(shdr_pos,
701                           v3d->render_border.xmin * ar->winx, v3d->render_border.ymin * ar->winy,
702                           v3d->render_border.xmax * ar->winx, v3d->render_border.ymax * ar->winy);
703
704         immUnbindProgram();
705 }
706
707 void ED_view3d_draw_depth(
708         Depsgraph *depsgraph,
709         ARegion *ar, View3D *v3d, bool alphaoverride)
710 {
711         struct bThemeState theme_state;
712         Scene *scene = DEG_get_evaluated_scene(depsgraph);
713         RegionView3D *rv3d = ar->regiondata;
714
715         short flag = v3d->flag;
716         float glalphaclip = U.glalphaclip;
717         int obcenter_dia = U.obcenter_dia;
718         /* temp set drawtype to solid */
719         /* Setting these temporarily is not nice */
720         v3d->flag &= ~V3D_SELECT_OUTLINE;
721
722         /* not that nice but means we wont zoom into billboards */
723         U.glalphaclip = alphaoverride ? 0.5f : glalphaclip;
724
725         U.obcenter_dia = 0;
726
727         /* Tools may request depth outside of regular drawing code. */
728         UI_Theme_Store(&theme_state);
729         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
730
731         ED_view3d_draw_setup_view(NULL, depsgraph, scene, ar, v3d, NULL, NULL, NULL);
732
733         GPU_clear(GPU_DEPTH_BIT);
734
735         if (rv3d->rflag & RV3D_CLIPPING) {
736                 ED_view3d_clipping_set(rv3d);
737         }
738         /* get surface depth without bias */
739         rv3d->rflag |= RV3D_ZOFFSET_DISABLED;
740
741         GPU_depth_test(true);
742
743         DRW_draw_depth_loop(depsgraph, ar, v3d);
744
745         if (rv3d->rflag & RV3D_CLIPPING) {
746                 ED_view3d_clipping_disable();
747         }
748         rv3d->rflag &= ~RV3D_ZOFFSET_DISABLED;
749
750         /* Reset default for UI */
751         GPU_depth_test(false);
752
753         U.glalphaclip = glalphaclip;
754         v3d->flag = flag;
755         U.obcenter_dia = obcenter_dia;
756
757         UI_Theme_Restore(&theme_state);
758 }
759
760 /* ******************** other elements ***************** */
761
762 /** could move this elsewhere, but tied into #ED_view3d_grid_scale */
763 float ED_scene_grid_scale(Scene *scene, const char **grid_unit)
764 {
765         /* apply units */
766         if (scene->unit.system) {
767                 const void *usys;
768                 int len;
769
770                 bUnit_GetSystem(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
771
772                 if (usys) {
773                         int i = bUnit_GetBaseUnit(usys);
774                         if (grid_unit)
775                                 *grid_unit = bUnit_GetNameDisplay(usys, i);
776                         return (float)bUnit_GetScaler(usys, i) / scene->unit.scale_length;
777                 }
778         }
779
780         return 1.0f;
781 }
782
783 float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
784 {
785         return v3d->grid * ED_scene_grid_scale(scene, grid_unit);
786 }
787
788 /* Simulates the grid scale that is actually viewed.
789  * The actual code is seen in `object_grid_frag.glsl` (see `grid_res`).
790  * Currently the simulation is only done when RV3D_VIEW_IS_AXIS. */
791 float ED_view3d_grid_view_scale(
792         Scene *scene, View3D *v3d, RegionView3D *rv3d, const char **grid_unit)
793 {
794         float grid_scale = ED_view3d_grid_scale(scene, v3d, grid_unit);
795         if (!rv3d->is_persp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
796                 /* Decrease the distance between grid snap points depending on zoom. */
797                 float grid_subdiv = v3d->gridsubdiv;
798                 if (grid_subdiv > 1) {
799                         /* Allow 3 more subdivisions (see OBJECT_engine_init). */
800                         grid_scale /= powf(grid_subdiv, 3);
801
802                         float grid_distance = rv3d->dist;
803                         float lvl = (logf(grid_distance / grid_scale) / logf(grid_subdiv));
804
805                         /* 1.3f is a visually chosen offset for the
806                          * subdivision to match the visible grid. */
807                         lvl -= 1.3f;
808                         CLAMP_MIN(lvl, 0.0f);
809
810                         grid_scale *= pow(grid_subdiv, (int)lvl);
811                 }
812         }
813
814         return grid_scale;
815 }
816
817 static void draw_view_axis(RegionView3D *rv3d, const rcti *rect)
818 {
819         const float k = U.rvisize * U.pixelsize;  /* axis size */
820         /* axis alpha offset (rvibright has range 0-10) */
821         const int bright = - 20 * (10 - U.rvibright);
822
823         /* Axis center in screen coordinates.
824          *
825          * - Unit size offset so small text doesn't draw outside the screen
826          * - Extra X offset because of the panel expander.
827          */
828         const float startx = rect->xmax - (k + UI_UNIT_X * 1.5);
829         const float starty = rect->ymax - (k + UI_UNIT_Y);
830
831         float axis_pos[3][2];
832         unsigned char axis_col[3][4];
833
834         int axis_order[3] = {0, 1, 2};
835         axis_sort_v3(rv3d->viewinv[2], axis_order);
836
837         for (int axis_i = 0; axis_i < 3; axis_i++) {
838                 int i = axis_order[axis_i];
839
840                 /* get position of each axis tip on screen */
841                 float vec[3] = { 0.0f };
842                 vec[i] = 1.0f;
843                 mul_qt_v3(rv3d->viewquat, vec);
844                 axis_pos[i][0] = startx + vec[0] * k;
845                 axis_pos[i][1] = starty + vec[1] * k;
846
847                 /* get color of each axis */
848                 UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
849                 axis_col[i][3] = 255 * hypotf(vec[0], vec[1]); /* alpha */
850         }
851
852         /* draw axis lines */
853         GPU_line_width(2.0f);
854         GPU_line_smooth(true);
855         GPU_blend(true);
856         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
857
858         GPUVertFormat *format = immVertexFormat();
859         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
860         uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
861
862         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
863         immBegin(GPU_PRIM_LINES, 6);
864
865         for (int axis_i = 0; axis_i < 3; axis_i++) {
866                 int i = axis_order[axis_i];
867
868                 immAttr4ubv(col, axis_col[i]);
869                 immVertex2f(pos, startx, starty);
870                 immAttr4ubv(col, axis_col[i]);
871                 immVertex2fv(pos, axis_pos[i]);
872         }
873
874         immEnd();
875         immUnbindProgram();
876         GPU_line_smooth(false);
877
878         /* draw axis names */
879         for (int axis_i = 0; axis_i < 3; axis_i++) {
880                 int i = axis_order[axis_i];
881
882                 const char axis_text[2] = {'x' + i, '\0'};
883                 BLF_color4ubv(BLF_default(), axis_col[i]);
884                 BLF_draw_default_ascii(axis_pos[i][0] + 2, axis_pos[i][1] + 2, 0.0f, axis_text, 1);
885         }
886 }
887
888 #ifdef WITH_INPUT_NDOF
889 /* draw center and axis of rotation for ongoing 3D mouse navigation */
890 static void draw_rotation_guide(const RegionView3D *rv3d)
891 {
892         float o[3];    /* center of rotation */
893         float end[3];  /* endpoints for drawing */
894
895         GLubyte color[4] = {0, 108, 255, 255};  /* bright blue so it matches device LEDs */
896
897         negate_v3_v3(o, rv3d->ofs);
898
899         GPU_blend(true);
900         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
901         glDepthMask(GL_FALSE);  /* don't overwrite zbuf */
902
903         GPUVertFormat *format = immVertexFormat();
904         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
905         uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
906
907         immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR);
908
909         if (rv3d->rot_angle != 0.0f) {
910                 /* -- draw rotation axis -- */
911                 float scaled_axis[3];
912                 const float scale = rv3d->dist;
913                 mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
914
915
916                 immBegin(GPU_PRIM_LINE_STRIP, 3);
917                 color[3] = 0; /* more transparent toward the ends */
918                 immAttr4ubv(col, color);
919                 add_v3_v3v3(end, o, scaled_axis);
920                 immVertex3fv(pos, end);
921
922 #if 0
923                 color[3] = 0.2f + fabsf(rv3d->rot_angle);  /* modulate opacity with angle */
924                 /* ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2 */
925 #endif
926
927                 color[3] = 127; /* more opaque toward the center */
928                 immAttr4ubv(col, color);
929                 immVertex3fv(pos, o);
930
931                 color[3] = 0;
932                 immAttr4ubv(col, color);
933                 sub_v3_v3v3(end, o, scaled_axis);
934                 immVertex3fv(pos, end);
935                 immEnd();
936
937                 /* -- draw ring around rotation center -- */
938                 {
939 #define     ROT_AXIS_DETAIL 13
940
941                         const float s = 0.05f * scale;
942                         const float step = 2.0f * (float)(M_PI / ROT_AXIS_DETAIL);
943
944                         float q[4];  /* rotate ring so it's perpendicular to axis */
945                         const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
946                         if (!upright) {
947                                 const float up[3] = {0.0f, 0.0f, 1.0f};
948                                 float vis_angle, vis_axis[3];
949
950                                 cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
951                                 vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
952                                 axis_angle_to_quat(q, vis_axis, vis_angle);
953                         }
954
955                         immBegin(GPU_PRIM_LINE_LOOP, ROT_AXIS_DETAIL);
956                         color[3] = 63; /* somewhat faint */
957                         immAttr4ubv(col, color);
958                         float angle = 0.0f;
959                         for (int i = 0; i < ROT_AXIS_DETAIL; ++i, angle += step) {
960                                 float p[3] = {s * cosf(angle), s * sinf(angle), 0.0f};
961
962                                 if (!upright) {
963                                         mul_qt_v3(q, p);
964                                 }
965
966                                 add_v3_v3(p, o);
967                                 immVertex3fv(pos, p);
968                         }
969                         immEnd();
970
971 #undef      ROT_AXIS_DETAIL
972                 }
973
974                 color[3] = 255;  /* solid dot */
975         }
976         else
977                 color[3] = 127;  /* see-through dot */
978
979         immUnbindProgram();
980
981         /* -- draw rotation center -- */
982         immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
983         GPU_point_size(5.0f);
984         immBegin(GPU_PRIM_POINTS, 1);
985         immAttr4ubv(col, color);
986         immVertex3fv(pos, o);
987         immEnd();
988         immUnbindProgram();
989
990 #if 0
991         /* find screen coordinates for rotation center, then draw pretty icon */
992         mul_m4_v3(rv3d->persinv, rot_center);
993         UI_icon_draw(rot_center[0], rot_center[1], ICON_NDOF_TURN);
994         /* ^^ just playing around, does not work */
995 #endif
996
997         GPU_blend(false);
998         glDepthMask(GL_TRUE);
999 }
1000 #endif /* WITH_INPUT_NDOF */
1001
1002 /* ******************** info ***************** */
1003
1004 /**
1005  * Render and camera border
1006  */
1007 static void view3d_draw_border(const bContext *C, ARegion *ar)
1008 {
1009         Scene *scene = CTX_data_scene(C);
1010         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1011         RegionView3D *rv3d = ar->regiondata;
1012         View3D *v3d = CTX_wm_view3d(C);
1013
1014         if (rv3d->persp == RV3D_CAMOB) {
1015                 drawviewborder(scene, depsgraph, ar, v3d);
1016         }
1017         else if (v3d->flag2 & V3D_RENDER_BORDER) {
1018                 drawrenderborder(ar, v3d);
1019         }
1020 }
1021
1022 /**
1023  * Grease Pencil
1024  */
1025 static void view3d_draw_grease_pencil(const bContext *UNUSED(C))
1026 {
1027         /* TODO viewport */
1028 }
1029
1030 /**
1031  * Viewport Name
1032  */
1033 static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
1034 {
1035         const char *name = NULL;
1036
1037         switch (rv3d->view) {
1038                 case RV3D_VIEW_FRONT:
1039                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Front Orthographic");
1040                         else name = IFACE_("Front Perspective");
1041                         break;
1042                 case RV3D_VIEW_BACK:
1043                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Back Orthographic");
1044                         else name = IFACE_("Back Perspective");
1045                         break;
1046                 case RV3D_VIEW_TOP:
1047                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Top Orthographic");
1048                         else name = IFACE_("Top Perspective");
1049                         break;
1050                 case RV3D_VIEW_BOTTOM:
1051                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Bottom Orthographic");
1052                         else name = IFACE_("Bottom Perspective");
1053                         break;
1054                 case RV3D_VIEW_RIGHT:
1055                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Right Orthographic");
1056                         else name = IFACE_("Right Perspective");
1057                         break;
1058                 case RV3D_VIEW_LEFT:
1059                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Left Orthographic");
1060                         else name = IFACE_("Left Perspective");
1061                         break;
1062
1063                 default:
1064                         if (rv3d->persp == RV3D_CAMOB) {
1065                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
1066                                         Camera *cam;
1067                                         cam = v3d->camera->data;
1068                                         if (cam->type == CAM_PERSP) {
1069                                                 name = IFACE_("Camera Perspective");
1070                                         }
1071                                         else if (cam->type == CAM_ORTHO) {
1072                                                 name = IFACE_("Camera Orthographic");
1073                                         }
1074                                         else {
1075                                                 BLI_assert(cam->type == CAM_PANO);
1076                                                 name = IFACE_("Camera Panoramic");
1077                                         }
1078                                 }
1079                                 else {
1080                                         name = IFACE_("Object as Camera");
1081                                 }
1082                         }
1083                         else {
1084                                 name = (rv3d->persp == RV3D_ORTHO) ? IFACE_("User Orthographic") : IFACE_("User Perspective");
1085                         }
1086         }
1087
1088         return name;
1089 }
1090
1091 static void draw_viewport_name(ARegion *ar, View3D *v3d, int xoffset, int *yoffset)
1092 {
1093         RegionView3D *rv3d = ar->regiondata;
1094         const char *name = view3d_get_name(v3d, rv3d);
1095         const int font_id = BLF_default();
1096
1097         /* increase size for unicode languages (Chinese in utf-8...) */
1098 #ifdef WITH_INTERNATIONAL
1099         char tmpstr[96];
1100 #else
1101         char tmpstr[32];
1102 #endif
1103
1104         BLF_enable(font_id, BLF_SHADOW);
1105         BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
1106         BLF_shadow_offset(font_id, 1, -1);
1107
1108         if (v3d->localvd) {
1109                 BLI_snprintf(tmpstr, sizeof(tmpstr), IFACE_("%s (Local)"), name);
1110                 name = tmpstr;
1111         }
1112
1113         UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1114
1115         *yoffset -= U.widget_unit;
1116
1117 #ifdef WITH_INTERNATIONAL
1118         BLF_draw_default(xoffset, *yoffset, 0.0f, name, sizeof(tmpstr));
1119 #else
1120         BLF_draw_default_ascii(xoffset, *yoffset, 0.0f, name, sizeof(tmpstr));
1121 #endif
1122
1123         BLF_disable(font_id, BLF_SHADOW);
1124 }
1125
1126 /**
1127  * draw info beside axes in bottom left-corner:
1128  * framenum, collection, object name, bone name (if available), marker name (if available)
1129  */
1130
1131 static void draw_selected_name(Scene *scene, ViewLayer *view_layer, Object *ob, int xoffset, int *yoffset)
1132 {
1133         const int cfra = CFRA;
1134         const char *msg_pin = " (Pinned)";
1135         const char *msg_sep = " : ";
1136
1137         const int font_id = BLF_default();
1138
1139         char info[300];
1140         char *s = info;
1141
1142         s += sprintf(s, "(%d)", cfra);
1143
1144         if ((ob == NULL) || (ob->mode == OB_MODE_OBJECT)) {
1145                 LayerCollection *layer_collection = view_layer->active_collection;
1146                 s += sprintf(s, " %s%s", BKE_collection_ui_name_get(layer_collection->collection), (ob == NULL) ? "" : " |");
1147         }
1148
1149         /*
1150          * info can contain:
1151          * - a frame (7 + 2)
1152          * - a collection name (MAX_NAME + 3)
1153          * - 3 object names (MAX_NAME)
1154          * - 2 BREAD_CRUMB_SEPARATORs (6)
1155          * - a SHAPE_KEY_PINNED marker and a trailing '\0' (9+1) - translated, so give some room!
1156          * - a marker name (MAX_NAME + 3)
1157          */
1158
1159         /* get name of marker on current frame (if available) */
1160         const char *markern = BKE_scene_find_marker_name(scene, cfra);
1161
1162         /* check if there is an object */
1163         if (ob) {
1164                 *s++ = ' ';
1165                 s += BLI_strcpy_rlen(s, ob->id.name + 2);
1166
1167                 /* name(s) to display depends on type of object */
1168                 if (ob->type == OB_ARMATURE) {
1169                         bArmature *arm = ob->data;
1170
1171                         /* show name of active bone too (if possible) */
1172                         if (arm->edbo) {
1173                                 if (arm->act_edbone) {
1174                                         s += BLI_strcpy_rlen(s, msg_sep);
1175                                         s += BLI_strcpy_rlen(s, arm->act_edbone->name);
1176                                 }
1177                         }
1178                         else if (ob->mode & OB_MODE_POSE) {
1179                                 if (arm->act_bone) {
1180
1181                                         if (arm->act_bone->layer & arm->layer) {
1182                                                 s += BLI_strcpy_rlen(s, msg_sep);
1183                                                 s += BLI_strcpy_rlen(s, arm->act_bone->name);
1184                                         }
1185                                 }
1186                         }
1187                 }
1188                 else if (ELEM(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
1189                         /* try to display active bone and active shapekey too (if they exist) */
1190
1191                         if (ob->type == OB_MESH && ob->mode & OB_MODE_WEIGHT_PAINT) {
1192                                 Object *armobj = BKE_object_pose_armature_get(ob);
1193                                 if (armobj  && armobj->mode & OB_MODE_POSE) {
1194                                         bArmature *arm = armobj->data;
1195                                         if (arm->act_bone) {
1196                                                 if (arm->act_bone->layer & arm->layer) {
1197                                                         s += BLI_strcpy_rlen(s, msg_sep);
1198                                                         s += BLI_strcpy_rlen(s, arm->act_bone->name);
1199                                                 }
1200                                         }
1201                                 }
1202                         }
1203
1204                         Key *key = BKE_key_from_object(ob);
1205                         if (key) {
1206                                 KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
1207                                 if (kb) {
1208                                         s += BLI_strcpy_rlen(s, msg_sep);
1209                                         s += BLI_strcpy_rlen(s, kb->name);
1210                                         if (ob->shapeflag & OB_SHAPE_LOCK) {
1211                                                 s += BLI_strcpy_rlen(s, IFACE_(msg_pin));
1212                                         }
1213                                 }
1214                         }
1215                 }
1216
1217                 /* color depends on whether there is a keyframe */
1218                 if (id_frame_has_keyframe((ID *)ob, /* BKE_scene_frame_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL)) {
1219                         UI_FontThemeColor(font_id, TH_TIME_KEYFRAME);
1220                 }
1221                 else if (ED_gpencil_has_keyframe_v3d(scene, ob, cfra)) {
1222                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1223                 }
1224                 else {
1225                         UI_FontThemeColor(font_id, TH_TEXT_HI);
1226                 }
1227         }
1228         else {
1229                 /* no object */
1230                 if (ED_gpencil_has_keyframe_v3d(scene, NULL, cfra))
1231                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1232                 else
1233                         UI_FontThemeColor(font_id, TH_TEXT_HI);
1234         }
1235
1236         if (markern) {
1237                 s += sprintf(s, " <%s>", markern);
1238         }
1239
1240         BLF_enable(font_id, BLF_SHADOW);
1241         BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
1242         BLF_shadow_offset(font_id, 1, -1);
1243
1244         *yoffset -= U.widget_unit;
1245         BLF_draw_default(xoffset, *yoffset, 0.0f, info, sizeof(info));
1246
1247         BLF_disable(font_id, BLF_SHADOW);
1248 }
1249
1250 /* ******************** view loop ***************** */
1251
1252 /**
1253  * Information drawn on top of the solid plates and composed data
1254  */
1255 void view3d_draw_region_info(const bContext *C, ARegion *ar)
1256 {
1257         RegionView3D *rv3d = ar->regiondata;
1258         View3D *v3d = CTX_wm_view3d(C);
1259         Scene *scene = CTX_data_scene(C);
1260         wmWindowManager *wm = CTX_wm_manager(C);
1261
1262 #ifdef WITH_INPUT_NDOF
1263         if ((U.ndof_flag & NDOF_SHOW_GUIDE) &&
1264             ((rv3d->viewlock & RV3D_LOCKED) == 0) &&
1265             (rv3d->persp != RV3D_CAMOB))
1266         {
1267                 /* TODO: draw something else (but not this) during fly mode */
1268                 draw_rotation_guide(rv3d);
1269         }
1270 #endif
1271
1272         /* correct projection matrix */
1273         ED_region_pixelspace(ar);
1274
1275         /* local coordinate visible rect inside region, to accommodate overlapping ui */
1276         rcti rect;
1277         ED_region_visible_rect(ar, &rect);
1278
1279
1280         view3d_draw_border(C, ar);
1281         view3d_draw_grease_pencil(C);
1282
1283         BLF_batch_draw_begin();
1284
1285         if ((U.uiflag & USER_SHOW_GIZMO_AXIS) ||
1286             (v3d->flag2 & V3D_RENDER_OVERRIDE) ||
1287             /* No need to display gizmo and this info. */
1288             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_NAVIGATE)))
1289         {
1290                 /* pass */
1291         }
1292         else {
1293                 draw_view_axis(rv3d, &rect);
1294         }
1295
1296         int xoffset = rect.xmin + U.widget_unit;
1297         int yoffset = rect.ymax;
1298
1299         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0 &&
1300             (v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0)
1301         {
1302                 if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
1303                         ED_scene_draw_fps(scene, xoffset, &yoffset);
1304                 }
1305                 else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
1306                         draw_viewport_name(ar, v3d, xoffset, &yoffset);
1307                 }
1308
1309                 if (U.uiflag & USER_DRAWVIEWINFO) {
1310                         ViewLayer *view_layer = CTX_data_view_layer(C);
1311                         Object *ob = OBACT(view_layer);
1312                         draw_selected_name(scene, view_layer, ob, xoffset, &yoffset);
1313                 }
1314
1315 #if 0 /* TODO */
1316                 if (grid_unit) { /* draw below the viewport name */
1317                         char numstr[32] = "";
1318
1319                         UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1320                         if (v3d->grid != 1.0f) {
1321                                 BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
1322                         }
1323
1324                         *yoffset -= U.widget_unit;
1325                         BLF_draw_default_ascii(xoffset, *yoffset, numstr[0] ? numstr : grid_unit, sizeof(numstr));
1326                 }
1327 #endif
1328         }
1329
1330         if ((v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) {
1331                 DRW_draw_region_engine_info(xoffset, yoffset);
1332         }
1333
1334         BLF_batch_draw_end();
1335 }
1336
1337 static void view3d_draw_view(const bContext *C, ARegion *ar)
1338 {
1339         ED_view3d_draw_setup_view(CTX_wm_window(C), CTX_data_depsgraph(C), CTX_data_scene(C), ar, CTX_wm_view3d(C), NULL, NULL, NULL);
1340
1341         /* Only 100% compliant on new spec goes below */
1342         DRW_draw_view(C);
1343 }
1344
1345 RenderEngineType *ED_view3d_engine_type(Scene *scene, int drawtype)
1346 {
1347         /*
1348          * Temporary viewport draw modes until we have a proper system.
1349          * all modes are done in the draw manager, except
1350          * cycles material as it is an external render engine.
1351          */
1352         if (strcmp(scene->r.engine, RE_engine_id_CYCLES) == 0 && drawtype == OB_MATERIAL) {
1353                 return RE_engines_find(RE_engine_id_BLENDER_EEVEE);
1354         }
1355         return RE_engines_find(scene->r.engine);
1356 }
1357
1358 void view3d_main_region_draw(const bContext *C, ARegion *ar)
1359 {
1360         Main *bmain = CTX_data_main(C);
1361         View3D *v3d = CTX_wm_view3d(C);
1362
1363         view3d_draw_view(C, ar);
1364
1365         GPU_free_images_old(bmain);
1366         GPU_pass_cache_garbage_collect();
1367
1368         /* XXX This is in order to draw UI batches with the DRW
1369          * olg context since we now use it for drawing the entire area */
1370         gpu_batch_presets_reset();
1371
1372         /* No depth test for drawing action zones afterwards. */
1373         GPU_depth_test(false);
1374
1375         v3d->flag |= V3D_INVALID_BACKBUF;
1376 }
1377
1378 /* -------------------------------------------------------------------- */
1379
1380 /** \name Offscreen Drawing
1381  * \{ */
1382
1383 static void view3d_stereo3d_setup_offscreen(
1384         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
1385         float winmat[4][4], const char *viewname)
1386 {
1387         /* update the viewport matrices with the new camera */
1388         if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
1389                 float viewmat[4][4];
1390                 const bool is_left = STREQ(viewname, STEREO_LEFT_NAME);
1391
1392                 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
1393                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1394         }
1395         else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
1396                 float viewmat[4][4];
1397                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1398
1399                 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
1400                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1401         }
1402 }
1403
1404 void ED_view3d_draw_offscreen(
1405         Depsgraph *depsgraph, Scene *scene,
1406         int drawtype,
1407         View3D *v3d, ARegion *ar, int winx, int winy,
1408         float viewmat[4][4], float winmat[4][4],
1409         bool do_sky, bool UNUSED(is_persp), const char *viewname,
1410         GPUFXSettings *UNUSED(fx_settings),
1411         GPUOffScreen *ofs, GPUViewport *viewport)
1412 {
1413         RegionView3D *rv3d = ar->regiondata;
1414         RenderEngineType *engine_type = ED_view3d_engine_type(scene, drawtype);
1415
1416         /* set temporary new size */
1417         int bwinx = ar->winx;
1418         int bwiny = ar->winy;
1419         rcti brect = ar->winrct;
1420
1421         ar->winx = winx;
1422         ar->winy = winy;
1423         ar->winrct.xmin = 0;
1424         ar->winrct.ymin = 0;
1425         ar->winrct.xmax = winx;
1426         ar->winrct.ymax = winy;
1427
1428         struct bThemeState theme_state;
1429         UI_Theme_Store(&theme_state);
1430         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
1431
1432         /* set flags */
1433         G.f |= G_RENDER_OGL;
1434
1435         {
1436                 /* free images which can have changed on frame-change
1437                  * warning! can be slow so only free animated images - campbell */
1438                 GPU_free_images_anim(G.main);  /* XXX :((( */
1439         }
1440
1441         GPU_matrix_push_projection();
1442         GPU_matrix_identity_set();
1443         GPU_matrix_push();
1444         GPU_matrix_identity_set();
1445
1446         if ((viewname != NULL && viewname[0] != '\0') && (viewmat == NULL) && rv3d->persp == RV3D_CAMOB && v3d->camera)
1447                 view3d_stereo3d_setup_offscreen(depsgraph, scene, v3d, ar, winmat, viewname);
1448         else
1449                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1450
1451         /* main drawing call */
1452         DRW_draw_render_loop_offscreen(
1453                 depsgraph, engine_type, ar, v3d,
1454                 do_sky, ofs, viewport);
1455
1456         /* restore size */
1457         ar->winx = bwinx;
1458         ar->winy = bwiny;
1459         ar->winrct = brect;
1460
1461         GPU_matrix_pop_projection();
1462         GPU_matrix_pop();
1463
1464         UI_Theme_Restore(&theme_state);
1465
1466         G.f &= ~G_RENDER_OGL;
1467 }
1468
1469 /**
1470  * Utility func for ED_view3d_draw_offscreen
1471  *
1472  * \param ofs: Optional off-screen buffer, can be NULL.
1473  * (avoids re-creating when doing multiple GL renders).
1474  */
1475 ImBuf *ED_view3d_draw_offscreen_imbuf(
1476         Depsgraph *depsgraph, Scene *scene,
1477         int drawtype,
1478         View3D *v3d, ARegion *ar, int sizex, int sizey,
1479         unsigned int flag, unsigned int draw_flags,
1480         int alpha_mode, int samples, const char *viewname,
1481         /* output vars */
1482         GPUOffScreen *ofs, char err_out[256])
1483 {
1484         RegionView3D *rv3d = ar->regiondata;
1485         const bool draw_sky = (alpha_mode == R_ADDSKY);
1486         const bool use_full_sample = (draw_flags & V3D_OFSDRAW_USE_FULL_SAMPLE);
1487
1488         /* view state */
1489         GPUFXSettings fx_settings = v3d->fx_settings;
1490         bool is_ortho = false;
1491         float winmat[4][4];
1492
1493         if (ofs && ((GPU_offscreen_width(ofs) != sizex) || (GPU_offscreen_height(ofs) != sizey))) {
1494                 /* sizes differ, can't reuse */
1495                 ofs = NULL;
1496         }
1497
1498         GPUFrameBuffer *old_fb = GPU_framebuffer_active_get();
1499
1500         if (old_fb)  {
1501                 GPU_framebuffer_restore();
1502         }
1503
1504         const bool own_ofs = (ofs == NULL);
1505         DRW_opengl_context_enable();
1506
1507         if (own_ofs) {
1508                 /* bind */
1509                 ofs = GPU_offscreen_create(sizex, sizey, use_full_sample ? 0 : samples, true, false, err_out);
1510                 if (ofs == NULL) {
1511                         DRW_opengl_context_disable();
1512                         return NULL;
1513                 }
1514         }
1515
1516         GPU_offscreen_bind(ofs, true);
1517
1518         /* read in pixels & stamp */
1519         ImBuf *ibuf = IMB_allocImBuf(sizex, sizey, 32, flag);
1520
1521         /* render 3d view */
1522         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
1523                 CameraParams params;
1524                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1525                 const Object *camera_eval = DEG_get_evaluated_object(
1526                                                 depsgraph,
1527                                                 camera);
1528
1529                 BKE_camera_params_init(&params);
1530                 /* fallback for non camera objects */
1531                 params.clipsta = v3d->near;
1532                 params.clipend = v3d->far;
1533                 BKE_camera_params_from_object(&params, camera_eval);
1534                 BKE_camera_multiview_params(&scene->r, &params, camera_eval, viewname);
1535                 BKE_camera_params_compute_viewplane(&params, sizex, sizey, scene->r.xasp, scene->r.yasp);
1536                 BKE_camera_params_compute_matrix(&params);
1537
1538                 BKE_camera_to_gpu_dof(camera, &fx_settings);
1539
1540                 is_ortho = params.is_ortho;
1541                 copy_m4_m4(winmat, params.winmat);
1542         }
1543         else {
1544                 rctf viewplane;
1545                 float clipsta, clipend;
1546
1547                 is_ortho = ED_view3d_viewplane_get(depsgraph, v3d, rv3d, sizex, sizey, &viewplane, &clipsta, &clipend, NULL);
1548                 if (is_ortho) {
1549                         orthographic_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, -clipend, clipend);
1550                 }
1551                 else {
1552                         perspective_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
1553                 }
1554         }
1555
1556         if ((samples && use_full_sample) == 0) {
1557                 /* Single-pass render, common case */
1558                 ED_view3d_draw_offscreen(
1559                         depsgraph, scene, drawtype,
1560                         v3d, ar, sizex, sizey, NULL, winmat,
1561                         draw_sky, !is_ortho, viewname,
1562                         &fx_settings, ofs, NULL);
1563
1564                 if (ibuf->rect_float) {
1565                         GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
1566                 }
1567                 else if (ibuf->rect) {
1568                         GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
1569                 }
1570         }
1571         else {
1572                 /* Multi-pass render, use accumulation buffer & jitter for 'full' oversampling.
1573                  * Use because OpenGL may use a lower quality MSAA, and only over-sample edges. */
1574                 static float jit_ofs[32][2];
1575                 float winmat_jitter[4][4];
1576                 float *rect_temp = (ibuf->rect_float) ? ibuf->rect_float : MEM_mallocN(sizex * sizey * sizeof(float[4]), "rect_temp");
1577                 float *accum_buffer = MEM_mallocN(sizex * sizey * sizeof(float[4]), "accum_buffer");
1578                 GPUViewport *viewport = GPU_viewport_create_from_offscreen(ofs);
1579
1580                 BLI_jitter_init(jit_ofs, samples);
1581
1582                 /* first sample buffer, also initializes 'rv3d->persmat' */
1583                 ED_view3d_draw_offscreen(
1584                         depsgraph, scene, drawtype,
1585                         v3d, ar, sizex, sizey, NULL, winmat,
1586                         draw_sky, !is_ortho, viewname,
1587                         &fx_settings, ofs, viewport);
1588                 GPU_offscreen_read_pixels(ofs, GL_FLOAT, accum_buffer);
1589
1590                 /* skip the first sample */
1591                 for (int j = 1; j < samples; j++) {
1592                         copy_m4_m4(winmat_jitter, winmat);
1593                         window_translate_m4(
1594                                 winmat_jitter, rv3d->persmat,
1595                                 (jit_ofs[j][0] * 2.0f) / sizex,
1596                                 (jit_ofs[j][1] * 2.0f) / sizey);
1597
1598                         ED_view3d_draw_offscreen(
1599                                 depsgraph, scene, drawtype,
1600                                 v3d, ar, sizex, sizey, NULL, winmat_jitter,
1601                                 draw_sky, !is_ortho, viewname,
1602                                 &fx_settings, ofs, viewport);
1603                         GPU_offscreen_read_pixels(ofs, GL_FLOAT, rect_temp);
1604
1605                         unsigned int i = sizex * sizey * 4;
1606                         while (i--) {
1607                                 accum_buffer[i] += rect_temp[i];
1608                         }
1609                 }
1610
1611                 {
1612                         /* don't free data owned by 'ofs' */
1613                         GPU_viewport_clear_from_offscreen(viewport);
1614                         GPU_viewport_free(viewport);
1615                 }
1616
1617                 if (ibuf->rect_float == NULL) {
1618                         MEM_freeN(rect_temp);
1619                 }
1620
1621                 if (ibuf->rect_float) {
1622                         float *rect_float = ibuf->rect_float;
1623                         unsigned int i = sizex * sizey * 4;
1624                         while (i--) {
1625                                 rect_float[i] = accum_buffer[i] / samples;
1626                         }
1627                 }
1628                 else {
1629                         unsigned char *rect_ub = (unsigned char *)ibuf->rect;
1630                         unsigned int i = sizex * sizey * 4;
1631                         while (i--) {
1632                                 rect_ub[i] = (unsigned char)(255.0f * accum_buffer[i] / samples);
1633                         }
1634                 }
1635
1636                 MEM_freeN(accum_buffer);
1637         }
1638
1639         /* unbind */
1640         GPU_offscreen_unbind(ofs, true);
1641
1642         if (own_ofs) {
1643                 GPU_offscreen_free(ofs);
1644         }
1645
1646         DRW_opengl_context_disable();
1647
1648         if (old_fb)  {
1649                 GPU_framebuffer_bind(old_fb);
1650         }
1651
1652         if (ibuf->rect_float && ibuf->rect)
1653                 IMB_rect_from_float(ibuf);
1654
1655         return ibuf;
1656 }
1657
1658 /**
1659  * Creates own fake 3d views (wrapping #ED_view3d_draw_offscreen_imbuf)
1660  *
1661  * \param ofs: Optional off-screen buffer can be NULL.
1662  * (avoids re-creating when doing multiple GL renders).
1663  *
1664  * \note used by the sequencer
1665  */
1666 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
1667         Depsgraph *depsgraph, Scene *scene,
1668         int drawtype,
1669         Object *camera, int width, int height,
1670         unsigned int flag, unsigned int draw_flags,
1671         int alpha_mode, int samples, const char *viewname,
1672         GPUOffScreen *ofs, char err_out[256])
1673 {
1674         View3D v3d = {NULL};
1675         ARegion ar = {NULL};
1676         RegionView3D rv3d = {{{0}}};
1677
1678         /* connect data */
1679         v3d.regionbase.first = v3d.regionbase.last = &ar;
1680         ar.regiondata = &rv3d;
1681         ar.regiontype = RGN_TYPE_WINDOW;
1682
1683         v3d.camera = camera;
1684         v3d.shading.type = drawtype;
1685         v3d.flag2 = V3D_RENDER_OVERRIDE;
1686
1687         if (draw_flags & V3D_OFSDRAW_USE_GPENCIL) {
1688                 v3d.flag2 |= V3D_SHOW_ANNOTATION;
1689         }
1690
1691         v3d.shading.background_type = V3D_SHADING_BACKGROUND_WORLD;
1692
1693         if (draw_flags & V3D_OFSDRAW_USE_CAMERA_DOF) {
1694                 if (camera->type == OB_CAMERA) {
1695                         v3d.fx_settings.dof = &((Camera *)camera->data)->gpu_dof;
1696                         v3d.fx_settings.fx_flag |= GPU_FX_FLAG_DOF;
1697                 }
1698         }
1699
1700         rv3d.persp = RV3D_CAMOB;
1701
1702         copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
1703         normalize_m4(rv3d.viewinv);
1704         invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
1705
1706         {
1707                 CameraParams params;
1708                 const Object *view_camera_eval = DEG_get_evaluated_object(
1709                                                      depsgraph,
1710                                                      BKE_camera_multiview_render(scene, v3d.camera, viewname));
1711
1712                 BKE_camera_params_init(&params);
1713                 BKE_camera_params_from_object(&params, view_camera_eval);
1714                 BKE_camera_multiview_params(&scene->r, &params, view_camera_eval, viewname);
1715                 BKE_camera_params_compute_viewplane(&params, width, height, scene->r.xasp, scene->r.yasp);
1716                 BKE_camera_params_compute_matrix(&params);
1717
1718                 copy_m4_m4(rv3d.winmat, params.winmat);
1719                 v3d.near = params.clipsta;
1720                 v3d.far = params.clipend;
1721                 v3d.lens = params.lens;
1722         }
1723
1724         mul_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
1725         invert_m4_m4(rv3d.persinv, rv3d.viewinv);
1726
1727         return ED_view3d_draw_offscreen_imbuf(
1728                 depsgraph, scene, drawtype,
1729                 &v3d, &ar, width, height, flag,
1730                 draw_flags, alpha_mode, samples, viewname, ofs, err_out);
1731 }
1732
1733 /** \} */
1734
1735 /* -------------------------------------------------------------------- */
1736 /** \name Viewport Clipping
1737  * \{ */
1738
1739 static bool view3d_clipping_test(const float co[3], const float clip[6][4])
1740 {
1741         if (plane_point_side_v3(clip[0], co) > 0.0f)
1742                 if (plane_point_side_v3(clip[1], co) > 0.0f)
1743                         if (plane_point_side_v3(clip[2], co) > 0.0f)
1744                                 if (plane_point_side_v3(clip[3], co) > 0.0f)
1745                                         return false;
1746
1747         return true;
1748 }
1749
1750 /* for 'local' ED_view3d_clipping_local must run first
1751  * then all comparisons can be done in localspace */
1752 bool ED_view3d_clipping_test(const RegionView3D *rv3d, const float co[3], const bool is_local)
1753 {
1754         return view3d_clipping_test(co, is_local ? rv3d->clip_local : rv3d->clip);
1755 }
1756
1757
1758 /** \} */