doxygen: add newline after \file
[blender.git] / source / blender / draw / intern / draw_view.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file
20  * \ingroup draw
21  *
22  * Contains dynamic drawing using immediate mode
23  */
24
25 #include "DNA_brush_types.h"
26 #include "DNA_screen_types.h"
27 #include "DNA_userdef_types.h"
28 #include "DNA_view3d_types.h"
29
30 #include "ED_screen.h"
31 #include "ED_view3d.h"
32
33 #include "GPU_shader.h"
34 #include "GPU_immediate.h"
35 #include "GPU_matrix.h"
36
37 #include "UI_resources.h"
38
39 #include "WM_types.h"
40
41 #include "BKE_object.h"
42 #include "BKE_paint.h"
43
44 #include "DRW_render.h"
45
46 #include "view3d_intern.h"
47
48 #include "draw_view.h"
49
50 /* ******************** region info ***************** */
51
52 void DRW_draw_region_info(void)
53 {
54         const DRWContextState *draw_ctx = DRW_context_state_get();
55         ARegion *ar = draw_ctx->ar;
56
57         DRW_draw_cursor();
58
59         view3d_draw_region_info(draw_ctx->evil_C, ar);
60 }
61
62 /* ************************* Background ************************** */
63
64 void DRW_draw_background(void)
65 {
66         /* Just to make sure */
67         glDepthMask(GL_TRUE);
68         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
69         glStencilMask(0xFF);
70
71         if (UI_GetThemeValue(TH_SHOW_BACK_GRAD)) {
72                 float m[4][4];
73                 unit_m4(m);
74
75                 /* Gradient background Color */
76                 glDisable(GL_DEPTH_TEST);
77
78                 GPUVertFormat *format = immVertexFormat();
79                 uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
80                 uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
81                 uchar col_hi[3], col_lo[3];
82
83                 GPU_matrix_push();
84                 GPU_matrix_identity_set();
85                 GPU_matrix_projection_set(m);
86
87                 immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR_DITHER);
88
89                 UI_GetThemeColor3ubv(TH_BACK_GRAD, col_lo);
90                 UI_GetThemeColor3ubv(TH_BACK, col_hi);
91
92                 immBegin(GPU_PRIM_TRI_FAN, 4);
93                 immAttr3ubv(color, col_lo);
94                 immVertex2f(pos, -1.0f, -1.0f);
95                 immVertex2f(pos, 1.0f, -1.0f);
96
97                 immAttr3ubv(color, col_hi);
98                 immVertex2f(pos, 1.0f, 1.0f);
99                 immVertex2f(pos, -1.0f, 1.0f);
100                 immEnd();
101
102                 immUnbindProgram();
103
104                 GPU_matrix_pop();
105
106                 glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
107
108                 glEnable(GL_DEPTH_TEST);
109         }
110         else {
111                 /* Solid background Color */
112                 UI_ThemeClearColorAlpha(TH_BACK, 1.0f);
113                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
114         }
115 }
116
117 GPUBatch *DRW_draw_background_clipping_batch_from_rv3d(const RegionView3D *rv3d)
118 {
119         const BoundBox *bb = rv3d->clipbb;
120         const uint clipping_index[6][4] = {
121                 {0, 1, 2, 3},
122                 {0, 4, 5, 1},
123                 {4, 7, 6, 5},
124                 {7, 3, 2, 6},
125                 {1, 5, 6, 2},
126                 {7, 4, 0, 3},
127         };
128         GPUVertBuf *vbo;
129         GPUIndexBuf *el;
130         GPUIndexBufBuilder elb = {0};
131
132         /* Elements */
133         GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, ARRAY_SIZE(clipping_index) * 2, ARRAY_SIZE(bb->vec));
134         for (int i = 0; i < ARRAY_SIZE(clipping_index); i++) {
135                 const uint *idx = clipping_index[i];
136                 GPU_indexbuf_add_tri_verts(&elb, idx[0], idx[1], idx[2]);
137                 GPU_indexbuf_add_tri_verts(&elb, idx[0], idx[2], idx[3]);
138         }
139         el = GPU_indexbuf_build(&elb);
140
141         GPUVertFormat format = {0};
142         uint pos_id = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
143
144         vbo = GPU_vertbuf_create_with_format(&format);
145         GPU_vertbuf_data_alloc(vbo, ARRAY_SIZE(bb->vec));
146         GPU_vertbuf_attr_fill(vbo, pos_id, bb->vec);
147
148         return GPU_batch_create_ex(GPU_PRIM_TRIS, vbo, el, GPU_BATCH_OWNS_VBO | GPU_BATCH_OWNS_INDEX);
149 }
150
151
152 /* **************************** 3D Cursor ******************************** */
153
154 static bool is_cursor_visible(const DRWContextState *draw_ctx, Scene *scene, ViewLayer *view_layer)
155 {
156         View3D *v3d = draw_ctx->v3d;
157         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) || (v3d->overlay.flag & V3D_OVERLAY_HIDE_CURSOR)) {
158                 return false;
159         }
160
161         /* don't draw cursor in paint modes, but with a few exceptions */
162         if (draw_ctx->object_mode & OB_MODE_ALL_PAINT) {
163                 /* exception: object is in weight paint and has deforming armature in pose mode */
164                 if (draw_ctx->object_mode & OB_MODE_WEIGHT_PAINT) {
165                         if (BKE_object_pose_armature_get(draw_ctx->obact) != NULL) {
166                                 return true;
167                         }
168                 }
169                 /* exception: object in texture paint mode, clone brush, use_clone_layer disabled */
170                 else if (draw_ctx->object_mode & OB_MODE_TEXTURE_PAINT) {
171                         const Paint *p = BKE_paint_get_active(scene, view_layer);
172
173                         if (p && p->brush && p->brush->imagepaint_tool == PAINT_TOOL_CLONE) {
174                                 if ((scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_LAYER_CLONE) == 0) {
175                                         return true;
176                                 }
177                         }
178                 }
179
180                 /* no exception met? then don't draw cursor! */
181                 return false;
182         }
183         else if (draw_ctx->object_mode & OB_MODE_WEIGHT_GPENCIL) {
184                 /* grease pencil hide always in some modes */
185                 return false;
186         }
187
188         return true;
189 }
190
191 void DRW_draw_cursor(void)
192 {
193         const DRWContextState *draw_ctx = DRW_context_state_get();
194         ARegion *ar = draw_ctx->ar;
195         Scene *scene = draw_ctx->scene;
196         ViewLayer *view_layer = draw_ctx->view_layer;
197
198         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
199         glDepthMask(GL_FALSE);
200         glDisable(GL_DEPTH_TEST);
201
202         if (is_cursor_visible(draw_ctx, scene, view_layer)) {
203                 int co[2];
204                 const View3DCursor *cursor = &scene->cursor;
205                 if (ED_view3d_project_int_global(
206                             ar, cursor->location, co, V3D_PROJ_TEST_NOP | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
207                 {
208                         RegionView3D *rv3d = ar->regiondata;
209
210                         /* Draw nice Anti Aliased cursor. */
211                         GPU_line_width(1.0f);
212                         glEnable(GL_BLEND);
213                         glEnable(GL_LINE_SMOOTH);
214
215                         float eps = 1e-5f;
216                         rv3d->viewquat[0] = -rv3d->viewquat[0];
217                         const bool is_aligned = compare_v4v4(cursor->rotation, rv3d->viewquat, eps);
218                         rv3d->viewquat[0] = -rv3d->viewquat[0];
219
220                         /* Draw lines */
221                         if  (is_aligned == false) {
222                                 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
223                                 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
224                                 immUniformThemeColor3(TH_VIEW_OVERLAY);
225                                 immBegin(GPU_PRIM_LINES, 12);
226
227                                 const float scale = ED_view3d_pixel_size_no_ui_scale(rv3d, cursor->location) * U.widget_unit;
228
229 #define CURSOR_VERT(axis_vec, axis, fac) \
230                                 immVertex3f( \
231                                         pos, \
232                                         cursor->location[0] + axis_vec[0] * (fac), \
233                                         cursor->location[1] + axis_vec[1] * (fac), \
234                                         cursor->location[2] + axis_vec[2] * (fac))
235
236 #define CURSOR_EDGE(axis_vec, axis, sign) { \
237                                         CURSOR_VERT(axis_vec, axis, sign 1.0f); \
238                                         CURSOR_VERT(axis_vec, axis, sign 0.25f); \
239                                 }
240
241                                 for (int axis = 0; axis < 3; axis++) {
242                                         float axis_vec[3] = {0};
243                                         axis_vec[axis] = scale;
244                                         mul_qt_v3(cursor->rotation, axis_vec);
245                                         CURSOR_EDGE(axis_vec, axis, +);
246                                         CURSOR_EDGE(axis_vec, axis, -);
247                                 }
248
249 #undef CURSOR_VERT
250 #undef CURSOR_EDGE
251
252                                 immEnd();
253                                 immUnbindProgram();
254                         }
255
256                         float original_proj[4][4];
257                         GPU_matrix_projection_get(original_proj);
258                         GPU_matrix_push();
259                         ED_region_pixelspace(ar);
260                         GPU_matrix_translate_2f(co[0] + 0.5f, co[1] + 0.5f);
261                         GPU_matrix_scale_2f(U.widget_unit, U.widget_unit);
262
263                         GPUBatch *cursor_batch = DRW_cache_cursor_get(is_aligned);
264                         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_FLAT_COLOR);
265                         GPU_batch_program_set(cursor_batch, GPU_shader_get_program(shader), GPU_shader_get_interface(shader));
266
267                         GPU_batch_draw(cursor_batch);
268
269                         glDisable(GL_BLEND);
270                         glDisable(GL_LINE_SMOOTH);
271                         GPU_matrix_pop();
272                         GPU_matrix_projection_set(original_proj);
273                 }
274         }
275 }
276
277 /* **************************** 3D Gizmo ******************************** */
278
279 void DRW_draw_gizmo_3d(void)
280 {
281         const DRWContextState *draw_ctx = DRW_context_state_get();
282         ARegion *ar = draw_ctx->ar;
283
284         /* draw depth culled gizmos - gizmos need to be updated *after* view matrix was set up */
285         /* TODO depth culling gizmos is not yet supported, just drawing _3D here, should
286          * later become _IN_SCENE (and draw _3D separate) */
287         WM_gizmomap_draw(
288                 ar->gizmo_map, draw_ctx->evil_C,
289                 WM_GIZMOMAP_DRAWSTEP_3D);
290
291 }
292
293 void DRW_draw_gizmo_2d(void)
294 {
295         const DRWContextState *draw_ctx = DRW_context_state_get();
296         ARegion *ar = draw_ctx->ar;
297
298         WM_gizmomap_draw(
299                 ar->gizmo_map, draw_ctx->evil_C,
300                 WM_GIZMOMAP_DRAWSTEP_2D);
301
302         glDepthMask(GL_TRUE);
303 }