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 /* **************************** 3D Cursor ******************************** */
130
131 static bool is_cursor_visible(const DRWContextState *draw_ctx, Scene *scene, ViewLayer *view_layer)
132 {
133         View3D *v3d = draw_ctx->v3d;
134         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) || (v3d->overlay.flag & V3D_OVERLAY_HIDE_CURSOR)) {
135                 return false;
136         }
137
138         /* don't draw cursor in paint modes, but with a few exceptions */
139         if (draw_ctx->object_mode & OB_MODE_ALL_PAINT) {
140                 /* exception: object is in weight paint and has deforming armature in pose mode */
141                 if (draw_ctx->object_mode & OB_MODE_WEIGHT_PAINT) {
142                         if (BKE_object_pose_armature_get(draw_ctx->obact) != NULL) {
143                                 return true;
144                         }
145                 }
146                 /* exception: object in texture paint mode, clone brush, use_clone_layer disabled */
147                 else if (draw_ctx->object_mode & OB_MODE_TEXTURE_PAINT) {
148                         const Paint *p = BKE_paint_get_active(scene, view_layer);
149
150                         if (p && p->brush && p->brush->imagepaint_tool == PAINT_TOOL_CLONE) {
151                                 if ((scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_LAYER_CLONE) == 0) {
152                                         return true;
153                                 }
154                         }
155                 }
156
157                 /* no exception met? then don't draw cursor! */
158                 return false;
159         }
160         else if (draw_ctx->object_mode & OB_MODE_WEIGHT_GPENCIL) {
161                 /* grease pencil hide always in some modes */
162                 return false;
163         }
164
165         return true;
166 }
167
168 void DRW_draw_cursor(void)
169 {
170         const DRWContextState *draw_ctx = DRW_context_state_get();
171         ARegion *ar = draw_ctx->ar;
172         Scene *scene = draw_ctx->scene;
173         ViewLayer *view_layer = draw_ctx->view_layer;
174
175         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
176         glDepthMask(GL_FALSE);
177         glDisable(GL_DEPTH_TEST);
178
179         if (is_cursor_visible(draw_ctx, scene, view_layer)) {
180                 int co[2];
181                 const View3DCursor *cursor = &scene->cursor;
182                 if (ED_view3d_project_int_global(
183                             ar, cursor->location, co, V3D_PROJ_TEST_NOP | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
184                 {
185                         RegionView3D *rv3d = ar->regiondata;
186
187                         /* Draw nice Anti Aliased cursor. */
188                         GPU_line_width(1.0f);
189                         glEnable(GL_BLEND);
190                         glEnable(GL_LINE_SMOOTH);
191
192                         float eps = 1e-5f;
193                         rv3d->viewquat[0] = -rv3d->viewquat[0];
194                         const bool is_aligned = compare_v4v4(cursor->rotation, rv3d->viewquat, eps);
195                         rv3d->viewquat[0] = -rv3d->viewquat[0];
196
197                         /* Draw lines */
198                         if  (is_aligned == false) {
199                                 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
200                                 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
201                                 immUniformThemeColor3(TH_VIEW_OVERLAY);
202                                 immBegin(GPU_PRIM_LINES, 12);
203
204                                 const float scale = ED_view3d_pixel_size_no_ui_scale(rv3d, cursor->location) * U.widget_unit;
205
206 #define CURSOR_VERT(axis_vec, axis, fac) \
207                                 immVertex3f( \
208                                         pos, \
209                                         cursor->location[0] + axis_vec[0] * (fac), \
210                                         cursor->location[1] + axis_vec[1] * (fac), \
211                                         cursor->location[2] + axis_vec[2] * (fac))
212
213 #define CURSOR_EDGE(axis_vec, axis, sign) { \
214                                         CURSOR_VERT(axis_vec, axis, sign 1.0f); \
215                                         CURSOR_VERT(axis_vec, axis, sign 0.25f); \
216                                 }
217
218                                 for (int axis = 0; axis < 3; axis++) {
219                                         float axis_vec[3] = {0};
220                                         axis_vec[axis] = scale;
221                                         mul_qt_v3(cursor->rotation, axis_vec);
222                                         CURSOR_EDGE(axis_vec, axis, +);
223                                         CURSOR_EDGE(axis_vec, axis, -);
224                                 }
225
226 #undef CURSOR_VERT
227 #undef CURSOR_EDGE
228
229                                 immEnd();
230                                 immUnbindProgram();
231                         }
232
233                         float original_proj[4][4];
234                         GPU_matrix_projection_get(original_proj);
235                         GPU_matrix_push();
236                         ED_region_pixelspace(ar);
237                         GPU_matrix_translate_2f(co[0] + 0.5f, co[1] + 0.5f);
238                         GPU_matrix_scale_2f(U.widget_unit, U.widget_unit);
239
240                         GPUBatch *cursor_batch = DRW_cache_cursor_get(is_aligned);
241                         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_FLAT_COLOR);
242                         GPU_batch_program_set(cursor_batch, GPU_shader_get_program(shader), GPU_shader_get_interface(shader));
243
244                         GPU_batch_draw(cursor_batch);
245
246                         glDisable(GL_BLEND);
247                         glDisable(GL_LINE_SMOOTH);
248                         GPU_matrix_pop();
249                         GPU_matrix_projection_set(original_proj);
250                 }
251         }
252 }
253
254 /* **************************** 3D Gizmo ******************************** */
255
256 void DRW_draw_gizmo_3d(void)
257 {
258         const DRWContextState *draw_ctx = DRW_context_state_get();
259         ARegion *ar = draw_ctx->ar;
260
261         /* draw depth culled gizmos - gizmos need to be updated *after* view matrix was set up */
262         /* TODO depth culling gizmos is not yet supported, just drawing _3D here, should
263          * later become _IN_SCENE (and draw _3D separate) */
264         WM_gizmomap_draw(
265                 ar->gizmo_map, draw_ctx->evil_C,
266                 WM_GIZMOMAP_DRAWSTEP_3D);
267
268 }
269
270 void DRW_draw_gizmo_2d(void)
271 {
272         const DRWContextState *draw_ctx = DRW_context_state_get();
273         ARegion *ar = draw_ctx->ar;
274
275         WM_gizmomap_draw(
276                 ar->gizmo_map, draw_ctx->evil_C,
277                 WM_GIZMOMAP_DRAWSTEP_2D);
278
279         glDepthMask(GL_TRUE);
280 }