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