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