Wireframe Overlay: Make use of colorTransform when transforming an object
[blender.git] / source / blender / draw / modes / overlay_mode.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 overlay_mode.c
23  *  \ingroup draw_engine
24  */
25
26 #include "DNA_mesh_types.h"
27 #include "DNA_view3d_types.h"
28
29 #include "BKE_editmesh.h"
30 #include "BKE_object.h"
31 #include "BKE_global.h"
32
33 #include "GPU_shader.h"
34 #include "GPU_extensions.h"
35 #include "DRW_render.h"
36
37 #include "draw_mode_engines.h"
38
39 /* Structures */
40 typedef struct OVERLAY_StorageList {
41         struct OVERLAY_PrivateData *g_data;
42 } OVERLAY_StorageList;
43
44 typedef struct OVERLAY_PassList {
45         struct DRWPass *face_orientation_pass;
46         struct DRWPass *face_wireframe_pass;
47 } OVERLAY_PassList;
48
49 typedef struct OVERLAY_Data {
50         void *engine_type;
51         DRWViewportEmptyList *fbl;
52         DRWViewportEmptyList *txl;
53         OVERLAY_PassList *psl;
54         OVERLAY_StorageList *stl;
55 } OVERLAY_Data;
56
57 typedef struct OVERLAY_PrivateData {
58         DRWShadingGroup *face_orientation_shgrp;
59         DRWShadingGroup *face_wires;
60         DRWShadingGroup *flat_wires;
61         DRWShadingGroup *sculpt_wires;
62         View3DOverlay overlay;
63         float wire_step_param[2];
64         bool ghost_stencil_test;
65         bool show_overlays;
66 } OVERLAY_PrivateData; /* Transient data */
67
68 /* *********** STATIC *********** */
69 static struct {
70         /* Face orientation shader */
71         struct GPUShader *face_orientation_sh;
72         /* Wireframe shader */
73         struct GPUShader *select_wireframe_sh;
74         struct GPUShader *face_wireframe_sh;
75         struct GPUShader *face_wireframe_sculpt_sh;
76 } e_data = {NULL};
77
78 /* Shaders */
79 extern char datatoc_overlay_face_orientation_frag_glsl[];
80 extern char datatoc_overlay_face_orientation_vert_glsl[];
81
82 extern char datatoc_overlay_face_wireframe_vert_glsl[];
83 extern char datatoc_overlay_face_wireframe_geom_glsl[];
84 extern char datatoc_overlay_face_wireframe_frag_glsl[];
85 extern char datatoc_gpu_shader_depth_only_frag_glsl[];
86
87 extern struct GlobalsUboStorage ts; /* draw_common.c */
88
89 /* Functions */
90 static void overlay_engine_init(void *vedata)
91 {
92         OVERLAY_Data *data = vedata;
93         OVERLAY_StorageList *stl = data->stl;
94
95         if (!stl->g_data) {
96                 /* Alloc transient pointers */
97                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
98         }
99         stl->g_data->ghost_stencil_test = false;
100
101         if (!e_data.face_orientation_sh) {
102                 /* Face orientation */
103                 e_data.face_orientation_sh = DRW_shader_create(
104                         datatoc_overlay_face_orientation_vert_glsl, NULL,
105                         datatoc_overlay_face_orientation_frag_glsl, NULL);
106         }
107
108         if (!e_data.face_wireframe_sh) {
109                 e_data.select_wireframe_sh = DRW_shader_create(
110                         datatoc_overlay_face_wireframe_vert_glsl,
111                         datatoc_overlay_face_wireframe_geom_glsl,
112                         datatoc_gpu_shader_depth_only_frag_glsl,
113                         "#define SELECT_EDGES\n");
114
115                 e_data.face_wireframe_sh = DRW_shader_create(
116                         datatoc_overlay_face_wireframe_vert_glsl,
117                         NULL,
118                         datatoc_overlay_face_wireframe_frag_glsl,
119                         NULL);
120
121                 e_data.face_wireframe_sculpt_sh = DRW_shader_create(
122                         datatoc_overlay_face_wireframe_vert_glsl,
123                         datatoc_overlay_face_wireframe_geom_glsl,
124                         datatoc_overlay_face_wireframe_frag_glsl,
125                         "#define USE_SCULPT\n");
126         }
127 }
128
129 static void overlay_cache_init(void *vedata)
130 {
131         OVERLAY_Data *data = vedata;
132         OVERLAY_PassList *psl = data->psl;
133         OVERLAY_StorageList *stl = data->stl;
134         OVERLAY_PrivateData *g_data = stl->g_data;
135
136         const DRWContextState *DCS = DRW_context_state_get();
137
138         View3D *v3d = DCS->v3d;
139         if (v3d) {
140                 g_data->overlay = v3d->overlay;
141                 g_data->show_overlays = (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0;
142         }
143         else {
144                 memset(&g_data->overlay, 0, sizeof(g_data->overlay));
145                 g_data->show_overlays = false;
146         }
147
148         if (g_data->show_overlays == false) {
149                 g_data->overlay.flag = 0;
150         }
151
152         if (v3d->shading.type == OB_WIRE) {
153                 g_data->overlay.flag |= V3D_OVERLAY_WIREFRAMES;
154                 g_data->show_overlays = true;
155         }
156
157         {
158                 /* Face Orientation Pass */
159                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND;
160                 psl->face_orientation_pass = DRW_pass_create("Face Orientation", state);
161                 g_data->face_orientation_shgrp = DRW_shgroup_create(
162                         e_data.face_orientation_sh, psl->face_orientation_pass);
163         }
164
165         {
166                 /* Wireframe */
167                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND | DRW_STATE_FIRST_VERTEX_CONVENTION;
168                 float wire_size = max_ff(0.0f, U.pixelsize - 1.0f) * 0.5f;
169
170                 const bool use_select = (DRW_state_is_select() || DRW_state_is_depth());
171                 GPUShader *sculpt_wire_sh = use_select ? e_data.select_wireframe_sh : e_data.face_wireframe_sculpt_sh;
172                 GPUShader *face_wires_sh = use_select ? e_data.select_wireframe_sh : e_data.face_wireframe_sh;
173                 GPUShader *flat_wires_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
174
175                 psl->face_wireframe_pass = DRW_pass_create("Face Wires", state);
176
177                 g_data->flat_wires = DRW_shgroup_create(flat_wires_sh, psl->face_wireframe_pass);
178
179                 g_data->sculpt_wires = DRW_shgroup_create(sculpt_wire_sh, psl->face_wireframe_pass);
180
181                 g_data->face_wires = DRW_shgroup_create(face_wires_sh, psl->face_wireframe_pass);
182                 DRW_shgroup_uniform_vec2(g_data->face_wires, "wireStepParam", g_data->wire_step_param, 1);
183
184                 if (!use_select) {
185                         DRW_shgroup_uniform_float_copy(g_data->sculpt_wires, "wireSize", wire_size);
186                         DRW_shgroup_uniform_float_copy(g_data->face_wires, "wireSize", wire_size);
187                 }
188
189                 /* Control aspect of the falloff. */
190                 const float sharpness = 4.0f;
191                 /* Scale and bias: Adjust with wiredata encoding. (see mesh_batch_cache_create_edges_wireframe_data) */
192                 const float decompress = (0xFF / (float)(0xFF - 0x20));
193                 g_data->wire_step_param[0] = -sharpness * decompress;
194                 g_data->wire_step_param[1] = decompress + sharpness * stl->g_data->overlay.wireframe_threshold;
195         }
196 }
197
198 static void overlay_cache_populate(void *vedata, Object *ob)
199 {
200         OVERLAY_Data *data = vedata;
201         OVERLAY_StorageList *stl = data->stl;
202         OVERLAY_PrivateData *pd = stl->g_data;
203         const DRWContextState *draw_ctx = DRW_context_state_get();
204         RegionView3D *rv3d = draw_ctx->rv3d;
205         View3D *v3d = draw_ctx->v3d;
206
207         if ((!pd->show_overlays) ||
208             (ob->dt < OB_WIRE) ||
209             (!DRW_object_is_renderable(ob) && (ob->dt != OB_WIRE)))
210         {
211                 return;
212         }
213
214         if (DRW_object_is_renderable(ob) && pd->overlay.flag & V3D_OVERLAY_FACE_ORIENTATION) {
215                 struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
216                 if (geom) {
217                         DRW_shgroup_call_add(pd->face_orientation_shgrp, geom, ob->obmat);
218                 }
219         }
220
221         if ((pd->overlay.flag & V3D_OVERLAY_WIREFRAMES) ||
222             (v3d->shading.type == OB_WIRE) ||
223             (ob->dtx & OB_DRAWWIRE) ||
224             (ob->dt == OB_WIRE))
225         {
226                 const bool is_edit_mode = BKE_object_is_in_editmode(ob);
227                 bool has_edit_mesh_cage = false;
228                 if (ob->type == OB_MESH) {
229                         /* TODO: Should be its own function. */
230                         Mesh *me = (Mesh *)ob->data;
231                         BMEditMesh *embm = me->edit_btmesh;
232                         if (embm) {
233                                 has_edit_mesh_cage = embm->mesh_eval_cage && (embm->mesh_eval_cage != embm->mesh_eval_final);
234                         }
235                 }
236
237                 /* Don't do that in edit Mesh mode, unless there is a modifier preview. */
238                 if ((((ob != draw_ctx->object_edit) && !is_edit_mode) || has_edit_mesh_cage) ||
239                     ob->type != OB_MESH)
240                 {
241                         const bool is_active = (ob == draw_ctx->obact);
242                         const bool is_sculpt_mode = is_active && (draw_ctx->object_mode & OB_MODE_SCULPT) != 0;
243                         const bool all_wires = (pd->overlay.wireframe_threshold == 1.0f) ||
244                                                (ob->dtx & OB_DRAW_ALL_EDGES);
245                         const bool is_wire = (ob->dt < OB_SOLID);
246                         const int stencil_mask = (ob->dtx & OB_DRAWXRAY) ? 0x00 : 0xFF;
247                         DRWShadingGroup *shgrp = NULL;
248
249                         float *rim_col = ts.colorWire;
250                         if (!is_edit_mode && !is_sculpt_mode && !has_edit_mesh_cage &&
251                             ((ob->base_flag & BASE_SELECTED) != 0))
252                         {
253                                 rim_col = (ob == draw_ctx->obact) ? ts.colorActive : ts.colorSelect;
254                                 rim_col = (G.moving & G_TRANSFORM_OBJ) ? ts.colorTransform : rim_col;
255                         }
256
257                         /* This fixes only the biggest case which is a plane in ortho view. */
258                         int flat_axis = 0;
259                         bool is_flat_object_viewed_from_side = (rv3d->persp == RV3D_ORTHO) &&
260                                                                DRW_object_is_flat(ob, &flat_axis) &&
261                                                                DRW_object_axis_orthogonal_to_view(ob, flat_axis);
262
263                         if (is_flat_object_viewed_from_side && !is_sculpt_mode) {
264                                 /* Avoid losing flat objects when in ortho views (see T56549) */
265                                 struct GPUBatch *geom = DRW_cache_object_all_edges_get(ob);
266                                 if (geom) {
267                                         shgrp = pd->flat_wires;
268                                         shgrp = DRW_shgroup_create_sub(shgrp);
269                                         DRW_shgroup_stencil_mask(shgrp, stencil_mask);
270                                         DRW_shgroup_call_object_add(shgrp, geom, ob);
271                                         DRW_shgroup_uniform_vec4(shgrp, "color", rim_col, 1);
272                                 }
273                         }
274                         else {
275                                 struct GPUBatch *geom = DRW_cache_object_face_wireframe_get(ob);
276                                 if (geom || is_sculpt_mode) {
277                                         shgrp = (is_sculpt_mode) ? pd->sculpt_wires : pd->face_wires;
278                                         shgrp = DRW_shgroup_create_sub(shgrp);
279
280                                         static float all_wires_params[2] = {0.0f, 10.0f}; /* Parameters for all wires */
281                                         DRW_shgroup_uniform_vec2(shgrp, "wireStepParam", (all_wires)
282                                                                                          ? all_wires_params
283                                                                                          : pd->wire_step_param, 1);
284
285                                         if (!(DRW_state_is_select() || DRW_state_is_depth())) {
286                                                 DRW_shgroup_stencil_mask(shgrp, stencil_mask);
287                                                 DRW_shgroup_uniform_vec3(shgrp, "wireColor", ts.colorWire, 1);
288                                                 DRW_shgroup_uniform_vec3(shgrp, "rimColor", rim_col, 1);
289                                         }
290
291                                         if (is_sculpt_mode) {
292                                                 DRW_shgroup_call_sculpt_add(shgrp, ob, ob->obmat);
293                                         }
294                                         else {
295                                                 DRW_shgroup_call_add(shgrp, geom, ob->obmat);
296                                         }
297                                 }
298                         }
299                         if (is_wire && shgrp != NULL) {
300                                 /* If object is wireframe, don't try to use stencil test. */
301                                 DRW_shgroup_state_disable(shgrp, DRW_STATE_STENCIL_EQUAL);
302
303                                 if (ob->dtx & OB_DRAWXRAY) {
304                                         DRW_shgroup_state_disable(shgrp, DRW_STATE_DEPTH_LESS_EQUAL);
305                                 }
306                         }
307                         else if ((ob->dtx & OB_DRAWXRAY) && shgrp != NULL) {
308                                 pd->ghost_stencil_test = true;
309                         }
310                 }
311         }
312 }
313
314 static void overlay_cache_finish(void *vedata)
315 {
316         OVERLAY_Data *data = vedata;
317         OVERLAY_PassList *psl = data->psl;
318         OVERLAY_StorageList *stl = data->stl;
319
320         const DRWContextState *ctx = DRW_context_state_get();
321         View3D *v3d = ctx->v3d;
322
323         /* only in solid mode */
324         if (v3d->shading.type == OB_SOLID && (v3d->shading.flag & XRAY_FLAG(v3d)) == 0) {
325                 if (stl->g_data->ghost_stencil_test) {
326                         DRW_pass_state_add(psl->face_wireframe_pass, DRW_STATE_STENCIL_EQUAL);
327                 }
328         }
329 }
330
331 static void overlay_draw_scene(void *vedata)
332 {
333         OVERLAY_Data *data = vedata;
334         OVERLAY_PassList *psl = data->psl;
335         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
336
337         if (DRW_state_is_fbo()) {
338                 GPU_framebuffer_bind(dfbl->default_fb);
339         }
340         DRW_draw_pass(psl->face_orientation_pass);
341         DRW_draw_pass(psl->face_wireframe_pass);
342 }
343
344 static void overlay_engine_free(void)
345 {
346         DRW_SHADER_FREE_SAFE(e_data.face_orientation_sh);
347         DRW_SHADER_FREE_SAFE(e_data.select_wireframe_sh);
348         DRW_SHADER_FREE_SAFE(e_data.face_wireframe_sh);
349         DRW_SHADER_FREE_SAFE(e_data.face_wireframe_sculpt_sh);
350 }
351
352 static const DrawEngineDataSize overlay_data_size = DRW_VIEWPORT_DATA_SIZE(OVERLAY_Data);
353
354 DrawEngineType draw_engine_overlay_type = {
355         NULL, NULL,
356         N_("OverlayEngine"),
357         &overlay_data_size,
358         &overlay_engine_init,
359         &overlay_engine_free,
360         &overlay_cache_init,
361         &overlay_cache_populate,
362         &overlay_cache_finish,
363         NULL,
364         &overlay_draw_scene,
365         NULL,
366         NULL,
367         NULL,
368 };