Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_view3d / drawobject.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, full recode and added functions
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_view3d/drawobject.c
27  *  \ingroup spview3d
28  */
29
30 #include "DNA_mesh_types.h"
31 #include "DNA_scene_types.h"
32 #include "DNA_object_types.h"
33
34 #include "BLI_math.h"
35
36 #include "BKE_DerivedMesh.h"
37 #include "BKE_global.h"
38
39 #include "BKE_editmesh.h"
40
41 #include "DEG_depsgraph.h"
42 #include "DEG_depsgraph_query.h"
43
44 #include "GPU_draw.h"
45 #include "GPU_shader.h"
46 #include "GPU_immediate.h"
47 #include "GPU_batch.h"
48 #include "GPU_matrix.h"
49 #include "GPU_state.h"
50 #include "GPU_framebuffer.h"
51
52 #include "ED_mesh.h"
53
54 #include "UI_resources.h"
55
56 #include "view3d_intern.h"  /* bad level include */
57
58 #include "../../draw/intern/draw_cache_impl.h"  /* bad level include (temporary) */
59
60 int view3d_effective_drawtype(const struct View3D *v3d)
61 {
62         if (v3d->shading.type == OB_RENDER) {
63                 return v3d->shading.prev_type;
64         }
65         return v3d->shading.type;
66 }
67
68 static bool check_ob_drawface_dot(Scene *sce, View3D *vd, char dt)
69 {
70         if ((sce->toolsettings->selectmode & SCE_SELECT_FACE) == 0)
71                 return false;
72
73         if (G.f & G_BACKBUFSEL)
74                 return false;
75
76         if ((vd->flag & V3D_ZBUF_SELECT) == 0)
77                 return true;
78
79         /* if its drawing textures with zbuf sel, then don't draw dots */
80         if (dt == OB_TEXTURE && vd->shading.type == OB_TEXTURE)
81                 return false;
82
83         if ((vd->shading.type >= OB_SOLID) && (vd->flag2 & V3D_SOLID_TEX))
84                 return false;
85
86         return true;
87 }
88
89 /* ----------------- OpenGL Circle Drawing - Tables for Optimized Drawing Speed ------------------ */
90 /* 32 values of sin function (still same result!) */
91 #define CIRCLE_RESOL 32
92
93 static const float sinval[CIRCLE_RESOL] = {
94         0.00000000,
95         0.20129852,
96         0.39435585,
97         0.57126821,
98         0.72479278,
99         0.84864425,
100         0.93775213,
101         0.98846832,
102         0.99871650,
103         0.96807711,
104         0.89780453,
105         0.79077573,
106         0.65137248,
107         0.48530196,
108         0.29936312,
109         0.10116832,
110         -0.10116832,
111         -0.29936312,
112         -0.48530196,
113         -0.65137248,
114         -0.79077573,
115         -0.89780453,
116         -0.96807711,
117         -0.99871650,
118         -0.98846832,
119         -0.93775213,
120         -0.84864425,
121         -0.72479278,
122         -0.57126821,
123         -0.39435585,
124         -0.20129852,
125         0.00000000
126 };
127
128 /* 32 values of cos function (still same result!) */
129 static const float cosval[CIRCLE_RESOL] = {
130         1.00000000,
131         0.97952994,
132         0.91895781,
133         0.82076344,
134         0.68896691,
135         0.52896401,
136         0.34730525,
137         0.15142777,
138         -0.05064916,
139         -0.25065253,
140         -0.44039415,
141         -0.61210598,
142         -0.75875812,
143         -0.87434661,
144         -0.95413925,
145         -0.99486932,
146         -0.99486932,
147         -0.95413925,
148         -0.87434661,
149         -0.75875812,
150         -0.61210598,
151         -0.44039415,
152         -0.25065253,
153         -0.05064916,
154         0.15142777,
155         0.34730525,
156         0.52896401,
157         0.68896691,
158         0.82076344,
159         0.91895781,
160         0.97952994,
161         1.00000000
162 };
163
164 static void circball_array_fill(float verts[CIRCLE_RESOL][3], const float cent[3], float rad, const float tmat[4][4])
165 {
166         float vx[3], vy[3];
167         float *viter = (float *)verts;
168
169         mul_v3_v3fl(vx, tmat[0], rad);
170         mul_v3_v3fl(vy, tmat[1], rad);
171
172         for (unsigned int a = 0; a < CIRCLE_RESOL; a++, viter += 3) {
173                 viter[0] = cent[0] + sinval[a] * vx[0] + cosval[a] * vy[0];
174                 viter[1] = cent[1] + sinval[a] * vx[1] + cosval[a] * vy[1];
175                 viter[2] = cent[2] + sinval[a] * vx[2] + cosval[a] * vy[2];
176         }
177 }
178
179 void imm_drawcircball(const float cent[3], float rad, const float tmat[4][4], unsigned pos)
180 {
181         float verts[CIRCLE_RESOL][3];
182
183         circball_array_fill(verts, cent, rad, tmat);
184
185         immBegin(GPU_PRIM_LINE_LOOP, CIRCLE_RESOL);
186         for (int i = 0; i < CIRCLE_RESOL; ++i) {
187                 immVertex3fv(pos, verts[i]);
188         }
189         immEnd();
190 }
191
192 #ifdef VIEW3D_CAMERA_BORDER_HACK
193 unsigned char view3d_camera_border_hack_col[3];
194 bool view3d_camera_border_hack_test = false;
195 #endif
196
197 /* ***************** BACKBUF SEL (BBS) ********* */
198
199 static void bbs_obmode_mesh_verts(Object *ob, int offset)
200 {
201         Mesh *me = ob->data;
202         GPUBatch *batch = DRW_mesh_batch_cache_get_verts_with_select_id(me, offset);
203         GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
204         GPU_batch_draw(batch);
205 }
206
207 static void bbs_mesh_verts(BMEditMesh *em, int offset)
208 {
209         GPU_point_size(UI_GetThemeValuef(TH_VERTEX_SIZE));
210
211         Mesh *me = em->ob->data;
212         GPUBatch *batch = DRW_mesh_batch_cache_get_verts_with_select_id(me, offset);
213         GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
214         GPU_batch_draw(batch);
215 }
216
217 static void bbs_mesh_wire(BMEditMesh *em, int offset)
218 {
219         GPU_line_width(1.0f);
220
221         Mesh *me = em->ob->data;
222         GPUBatch *batch = DRW_mesh_batch_cache_get_edges_with_select_id(me, offset);
223         GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
224         GPU_batch_draw(batch);
225 }
226
227 static void bbs_mesh_face(BMEditMesh *em, const bool use_select)
228 {
229         Mesh *me = em->ob->data;
230         GPUBatch *batch;
231
232         if (use_select) {
233                 batch = DRW_mesh_batch_cache_get_triangles_with_select_id(me, true, 1);
234                 GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
235                 GPU_batch_draw(batch);
236         }
237         else {
238                 int selcol;
239                 GPU_select_index_get(0, &selcol);
240                 batch = DRW_mesh_batch_cache_get_triangles_with_select_mask(me, true);
241                 GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_UNIFORM_COLOR_U32);
242                 GPU_batch_uniform_1ui(batch, "color", selcol);
243                 GPU_batch_draw(batch);
244         }
245 }
246
247 static void bbs_mesh_face_dot(BMEditMesh *em)
248 {
249         Mesh *me = em->ob->data;
250         GPUBatch *batch = DRW_mesh_batch_cache_get_facedots_with_select_id(me, 1);
251         GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
252         GPU_batch_draw(batch);
253 }
254
255 /* two options, facecolors or black */
256 static void bbs_mesh_solid_EM(BMEditMesh *em, Scene *scene, View3D *v3d,
257                               Object *ob, bool use_faceselect)
258 {
259         if (use_faceselect) {
260                 bbs_mesh_face(em, true);
261
262                 if (check_ob_drawface_dot(scene, v3d, ob->dt)) {
263                         bbs_mesh_face_dot(em);
264                 }
265         }
266         else {
267                 bbs_mesh_face(em, false);
268         }
269 }
270
271 static void bbs_mesh_solid_verts(Depsgraph *UNUSED(depsgraph), Scene *UNUSED(scene), Object *ob)
272 {
273         Mesh *me = ob->data;
274
275         /* Only draw faces to mask out verts, we don't want their selection ID's. */
276         const int G_f_orig = G.f;
277         G.f &= ~G_BACKBUFSEL;
278
279         {
280                 int selcol;
281                 GPUBatch *batch;
282                 GPU_select_index_get(0, &selcol);
283                 batch = DRW_mesh_batch_cache_get_triangles_with_select_mask(me, true);
284                 GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_UNIFORM_COLOR_U32);
285                 GPU_batch_uniform_1ui(batch, "color", selcol);
286                 GPU_batch_draw(batch);
287         }
288
289         G.f |= (G_f_orig & G_BACKBUFSEL);
290
291         bbs_obmode_mesh_verts(ob, 1);
292         bm_vertoffs = me->totvert + 1;
293 }
294
295 static void bbs_mesh_solid_faces(Scene *UNUSED(scene), Object *ob)
296 {
297         Mesh *me = ob->data;
298         GPUBatch *batch;
299         if ((me->editflag & ME_EDIT_PAINT_FACE_SEL)) {
300                 batch = DRW_mesh_batch_cache_get_triangles_with_select_id(me, true, 1);
301         }
302         else {
303                 batch = DRW_mesh_batch_cache_get_triangles_with_select_id(me, false, 1);
304         }
305         GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR_U32);
306         GPU_batch_draw(batch);
307 }
308
309 void draw_object_backbufsel(
310         Depsgraph *depsgraph, Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob,
311         short select_mode)
312 {
313         ToolSettings *ts = scene->toolsettings;
314         if (select_mode == -1) {
315                 select_mode = ts->selectmode;
316         }
317
318         GPU_matrix_mul(ob->obmat);
319
320         glClearDepth(1.0); GPU_clear(GPU_DEPTH_BIT);
321         GPU_depth_test(true);
322
323         switch (ob->type) {
324                 case OB_MESH:
325                         if (ob->mode & OB_MODE_EDIT) {
326                                 Mesh *me = ob->data;
327                                 BMEditMesh *em = me->edit_btmesh;
328
329                                 BM_mesh_elem_table_ensure(em->bm, BM_VERT | BM_EDGE | BM_FACE);
330
331                                 bbs_mesh_solid_EM(em, scene, v3d, ob, (select_mode & SCE_SELECT_FACE) != 0);
332                                 if (select_mode & SCE_SELECT_FACE)
333                                         bm_solidoffs = 1 + em->bm->totface;
334                                 else {
335                                         bm_solidoffs = 1;
336                                 }
337
338                                 ED_view3d_polygon_offset(rv3d, 1.0);
339
340                                 /* we draw edges if edge select mode */
341                                 if (select_mode & SCE_SELECT_EDGE) {
342                                         bbs_mesh_wire(em, bm_solidoffs);
343                                         bm_wireoffs = bm_solidoffs + em->bm->totedge;
344                                 }
345                                 else {
346                                         /* `bm_vertoffs` is calculated from `bm_wireoffs`. (otherwise see T53512) */
347                                         bm_wireoffs = bm_solidoffs;
348                                 }
349
350                                 /* we draw verts if vert select mode. */
351                                 if (select_mode & SCE_SELECT_VERTEX) {
352                                         bbs_mesh_verts(em, bm_wireoffs);
353                                         bm_vertoffs = bm_wireoffs + em->bm->totvert;
354                                 }
355                                 else {
356                                         bm_vertoffs = bm_wireoffs;
357                                 }
358
359                                 ED_view3d_polygon_offset(rv3d, 0.0);
360                         }
361                         else {
362                                 Mesh *me = ob->data;
363                                 if ((me->editflag & ME_EDIT_PAINT_VERT_SEL) &&
364                                     /* currently vertex select supports weight paint and vertex paint*/
365                                     ((ob->mode & OB_MODE_WEIGHT_PAINT) || (ob->mode & OB_MODE_VERTEX_PAINT)))
366                                 {
367                                         bbs_mesh_solid_verts(depsgraph, scene, ob);
368                                 }
369                                 else {
370                                         bbs_mesh_solid_faces(scene, ob);
371                                 }
372                         }
373                         break;
374                 case OB_CURVE:
375                 case OB_SURF:
376                         break;
377         }
378
379         GPU_matrix_set(rv3d->viewmat);
380 }
381
382
383 void ED_draw_object_facemap(
384         Depsgraph *depsgraph, Object *ob, const float col[4], const int facemap)
385 {
386         /* happens on undo */
387         if (ob->type != OB_MESH || !ob->data) {
388                 return;
389         }
390
391         Mesh *me = ob->data;
392         {
393                 Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
394                 if (ob_eval->runtime.mesh_eval) {
395                         me = ob_eval->runtime.mesh_eval;
396                 }
397         }
398
399         glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);
400
401         /* Just to create the data to pass to immediate mode, grr! */
402         const int *facemap_data = CustomData_get_layer(&me->pdata, CD_FACEMAP);
403         if (facemap_data) {
404                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
405                 GPU_blend(true);
406
407                 const MVert *mvert = me->mvert;
408                 const MPoly *mpoly = me->mpoly;
409                 const MLoop *mloop = me->mloop;
410
411                 int mpoly_len = me->totpoly;
412                 int mloop_len = me->totloop;
413
414                 facemap_data = CustomData_get_layer(&me->pdata, CD_FACEMAP);
415
416                 /* use gawain immediate mode fore now */
417                 const int looptris_len = poly_to_tri_count(mpoly_len, mloop_len);
418                 const int vbo_len_capacity = looptris_len * 3;
419                 int vbo_len_used = 0;
420
421                 GPUVertFormat format_pos = { 0 };
422                 const uint pos_id = GPU_vertformat_attr_add(&format_pos, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
423
424                 GPUVertBuf *vbo_pos = GPU_vertbuf_create_with_format(&format_pos);
425                 GPU_vertbuf_data_alloc(vbo_pos, vbo_len_capacity);
426
427                 GPUVertBufRaw pos_step;
428                 GPU_vertbuf_attr_get_raw_data(vbo_pos, pos_id, &pos_step);
429
430                 const MPoly *mp;
431                 int i;
432                 if (me->runtime.looptris.array) {
433                         MLoopTri *mlt = me->runtime.looptris.array;
434                         for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) {
435                                 if (facemap_data[i] == facemap) {
436                                         for (int j = 2; j < mp->totloop; j++) {
437                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[0]].v].co);
438                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[1]].v].co);
439                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[2]].v].co);
440                                                 vbo_len_used += 3;
441                                                 mlt++;
442                                         }
443                                 }
444                                 else {
445                                         mlt += mp->totloop - 2;
446                                 }
447                         }
448                 }
449                 else {
450                         /* No tessellation data, fan-fill. */
451                         for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) {
452                                 if (facemap_data[i] == facemap) {
453                                         const MLoop *ml_start = &mloop[mp->loopstart];
454                                         const MLoop *ml_a = ml_start + 1;
455                                         const MLoop *ml_b = ml_start + 2;
456                                         for (int j = 2; j < mp->totloop; j++) {
457                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_start->v].co);
458                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_a->v].co);
459                                                 copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_b->v].co);
460                                                 vbo_len_used += 3;
461
462                                                 ml_a++;
463                                                 ml_b++;
464                                         }
465                                 }
466                         }
467                 }
468
469                 if (vbo_len_capacity != vbo_len_used) {
470                         GPU_vertbuf_data_resize(vbo_pos, vbo_len_used);
471                 }
472
473                 GPUBatch *draw_batch = GPU_batch_create(GPU_PRIM_TRIS, vbo_pos, NULL);
474                 GPU_batch_program_set_builtin(draw_batch, GPU_SHADER_3D_UNIFORM_COLOR);
475                 GPU_batch_uniform_4fv(draw_batch, "color", col);
476                 GPU_batch_draw(draw_batch);
477                 GPU_batch_discard(draw_batch);
478                 GPU_vertbuf_discard(vbo_pos);
479
480                 GPU_blend(false);
481         }
482 }