Image/UV Editor: Remove show other option
[blender.git] / source / blender / editors / uvedit / uvedit_draw.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, 2002-2009
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/uvedit/uvedit_draw.c
27  *  \ingroup eduv
28  */
29
30
31 #include <float.h>
32 #include <math.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_material_types.h"
39 #include "DNA_mesh_types.h"
40 #include "DNA_meshdata_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_space_types.h"
45
46 #include "BLI_math.h"
47 #include "BLI_utildefines.h"
48 #include "BLI_buffer.h"
49 #include "BLI_bitmap.h"
50
51 #include "BKE_deform.h"
52 #include "BKE_editmesh.h"
53 #include "BKE_material.h"
54 #include "BKE_layer.h"
55
56 #include "BKE_scene.h"
57
58 #include "BIF_glutil.h"
59
60 #include "DEG_depsgraph.h"
61 #include "DEG_depsgraph_query.h"
62
63 #include "GPU_batch.h"
64 #include "GPU_immediate.h"
65 #include "GPU_immediate_util.h"
66 #include "GPU_matrix.h"
67 #include "GPU_state.h"
68
69 #include "ED_image.h"
70 #include "ED_mesh.h"
71 #include "ED_uvedit.h"
72
73 #include "UI_resources.h"
74 #include "UI_interface.h"
75 #include "UI_view2d.h"
76
77 #include "uvedit_intern.h"
78
79 static void draw_uvs_lineloop_bmfaces(BMesh *bm, const int cd_loop_uv_offset, const uint shdr_pos);
80
81 void ED_image_draw_cursor(ARegion *ar, const float cursor[2])
82 {
83         float zoom[2], x_fac, y_fac;
84
85         UI_view2d_scale_get_inverse(&ar->v2d, &zoom[0], &zoom[1]);
86
87         mul_v2_fl(zoom, 256.0f * UI_DPI_FAC);
88         x_fac = zoom[0];
89         y_fac = zoom[1];
90
91         GPU_line_width(1.0f);
92
93         GPU_matrix_translate_2fv(cursor);
94
95         const uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
96
97         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
98
99         float viewport_size[4];
100         GPU_viewport_size_get_f(viewport_size);
101         immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
102
103         immUniform1i("colors_len", 2);  /* "advanced" mode */
104         immUniformArray4fv("colors", (float *)(float[][4]){{1.0f, 0.0f, 0.0f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
105         immUniform1f("dash_width", 8.0f);
106
107         immBegin(GPU_PRIM_LINES, 8);
108
109         immVertex2f(shdr_pos, -0.05f * x_fac, 0.0f);
110         immVertex2f(shdr_pos, 0.0f, 0.05f * y_fac);
111
112         immVertex2f(shdr_pos, 0.0f, 0.05f * y_fac);
113         immVertex2f(shdr_pos, 0.05f * x_fac, 0.0f);
114
115         immVertex2f(shdr_pos, 0.05f * x_fac, 0.0f);
116         immVertex2f(shdr_pos, 0.0f, -0.05f * y_fac);
117
118         immVertex2f(shdr_pos, 0.0f, -0.05f * y_fac);
119         immVertex2f(shdr_pos, -0.05f * x_fac, 0.0f);
120
121         immEnd();
122
123         immUniformArray4fv("colors", (float *)(float[][4]){{1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 0.0f, 1.0f}}, 2);
124         immUniform1f("dash_width", 2.0f);
125
126         immBegin(GPU_PRIM_LINES, 8);
127
128         immVertex2f(shdr_pos, -0.020f * x_fac, 0.0f);
129         immVertex2f(shdr_pos, -0.1f * x_fac, 0.0f);
130
131         immVertex2f(shdr_pos, 0.1f * x_fac, 0.0f);
132         immVertex2f(shdr_pos, 0.020f * x_fac, 0.0f);
133
134         immVertex2f(shdr_pos, 0.0f, -0.020f * y_fac);
135         immVertex2f(shdr_pos, 0.0f, -0.1f * y_fac);
136
137         immVertex2f(shdr_pos, 0.0f, 0.1f * y_fac);
138         immVertex2f(shdr_pos, 0.0f, 0.020f * y_fac);
139
140         immEnd();
141
142         immUnbindProgram();
143
144         GPU_matrix_translate_2f(-cursor[0], -cursor[1]);
145 }
146
147 static int draw_uvs_face_check(Scene *scene)
148 {
149         ToolSettings *ts = scene->toolsettings;
150
151         /* checks if we are selecting only faces */
152         if (ts->uv_flag & UV_SYNC_SELECTION) {
153                 if (ts->selectmode == SCE_SELECT_FACE)
154                         return 2;
155                 else if (ts->selectmode & SCE_SELECT_FACE)
156                         return 1;
157                 else
158                         return 0;
159         }
160         else
161                 return (ts->uv_selectmode == UV_SELECT_FACE);
162 }
163
164 static void draw_uvs_shadow(Object *obedit)
165 {
166         BMEditMesh *em = BKE_editmesh_from_object(obedit);
167         BMesh *bm = em->bm;
168
169         if (bm->totloop == 0) {
170                 return;
171         }
172
173         const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
174
175         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
176
177         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
178
179         /* draws the mesh when painting */
180         immUniformThemeColor(TH_UV_SHADOW);
181
182         draw_uvs_lineloop_bmfaces(bm, cd_loop_uv_offset, pos);
183
184         immUnbindProgram();
185 }
186
187 static void draw_uvs_stretch(SpaceImage *sima, Scene *scene, Object *obedit, BMEditMesh *em, const BMFace *efa_act)
188 {
189         BMesh *bm = em->bm;
190         BMFace *efa;
191         BMLoop *l;
192         BMIter iter, liter;
193         MLoopUV *luv;
194         Image *ima = sima->image;
195         float aspx, aspy, col[4];
196         int i;
197
198         const int cd_loop_uv_offset  = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
199
200         BLI_buffer_declare_static(vec2f, tf_uv_buf, BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
201         BLI_buffer_declare_static(vec2f, tf_uvorig_buf, BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
202
203         ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
204
205         switch (sima->dt_uvstretch) {
206                 case SI_UVDT_STRETCH_AREA:
207                 {
208                         float totarea = 0.0f, totuvarea = 0.0f, areadiff, uvarea, area;
209
210                         BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
211                                 const int efa_len = efa->len;
212                                 float (*tf_uv)[2]     = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf,     vec2f, efa_len);
213                                 float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uvorig_buf, vec2f, efa_len);
214
215                                 BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
216                                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
217                                         copy_v2_v2(tf_uvorig[i], luv->uv);
218                                 }
219
220                                 uv_poly_copy_aspect(tf_uvorig, tf_uv, aspx, aspy, efa->len);
221
222                                 totarea += BM_face_calc_area(efa);
223                                 totuvarea += area_poly_v2(tf_uv, efa->len);
224
225                                 if (uvedit_face_visible_test(scene, obedit, ima, efa)) {
226                                         BM_elem_flag_enable(efa, BM_ELEM_TAG);
227                                 }
228                                 else {
229                                         if (efa == efa_act) {
230                                                 efa_act = NULL;
231                                         }
232                                         BM_elem_flag_disable(efa, BM_ELEM_TAG);
233                                 }
234                         }
235
236                         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
237
238                         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
239
240                         if (totarea < FLT_EPSILON || totuvarea < FLT_EPSILON) {
241                                 col[0] = 1.0;
242                                 col[1] = col[2] = 0.0;
243
244                                 immUniformColor3fv(col);
245
246                                 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
247                                         if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
248                                                 immBegin(GPU_PRIM_TRI_FAN, efa->len);
249
250                                                 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
251                                                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
252                                                         immVertex2fv(pos, luv->uv);
253                                                 }
254
255                                                 immEnd();
256                                         }
257                                 }
258                         }
259                         else {
260                                 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
261                                         if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
262                                                 const int efa_len = efa->len;
263                                                 float (*tf_uv)[2]     = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf,     vec2f, efa_len);
264                                                 float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uvorig_buf, vec2f, efa_len);
265
266                                                 area = BM_face_calc_area(efa) / totarea;
267
268                                                 BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
269                                                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
270                                                         copy_v2_v2(tf_uvorig[i], luv->uv);
271                                                 }
272
273                                                 uv_poly_copy_aspect(tf_uvorig, tf_uv, aspx, aspy, efa->len);
274
275                                                 uvarea = area_poly_v2(tf_uv, efa->len) / totuvarea;
276
277                                                 if (area < FLT_EPSILON || uvarea < FLT_EPSILON)
278                                                         areadiff = 1.0f;
279                                                 else if (area > uvarea)
280                                                         areadiff = 1.0f - (uvarea / area);
281                                                 else
282                                                         areadiff = 1.0f - (area / uvarea);
283
284                                                 BKE_defvert_weight_to_rgb(col, areadiff);
285                                                 immUniformColor3fv(col);
286
287                                                 /* TODO: use editmesh tessface */
288                                                 immBegin(GPU_PRIM_TRI_FAN, efa->len);
289
290                                                 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
291                                                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
292                                                         immVertex2fv(pos, luv->uv);
293                                                 }
294
295                                                 immEnd();
296                                         }
297                                 }
298                         }
299
300                         immUnbindProgram();
301
302                         break;
303                 }
304                 case SI_UVDT_STRETCH_ANGLE:
305                 {
306                         float a;
307
308                         BLI_buffer_declare_static(float, uvang_buf, BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
309                         BLI_buffer_declare_static(float, ang_buf,   BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
310                         BLI_buffer_declare_static(vec3f, av_buf,  BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
311                         BLI_buffer_declare_static(vec2f, auv_buf, BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
312
313                         col[3] = 0.5f; /* hard coded alpha, not that nice */
314
315                         GPUVertFormat *format = immVertexFormat();
316                         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
317                         uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
318
319                         immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR);
320
321                         BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
322                                 if (uvedit_face_visible_test(scene, obedit, ima, efa)) {
323                                         const int efa_len = efa->len;
324                                         float (*tf_uv)[2]     = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf,     vec2f, efa_len);
325                                         float (*tf_uvorig)[2] = (float (*)[2])BLI_buffer_reinit_data(&tf_uvorig_buf, vec2f, efa_len);
326                                         float *uvang = BLI_buffer_reinit_data(&uvang_buf, float, efa_len);
327                                         float *ang   = BLI_buffer_reinit_data(&ang_buf,   float, efa_len);
328                                         float (*av)[3]  = (float (*)[3])BLI_buffer_reinit_data(&av_buf, vec3f, efa_len);
329                                         float (*auv)[2] = (float (*)[2])BLI_buffer_reinit_data(&auv_buf, vec2f, efa_len);
330                                         int j;
331
332                                         BM_elem_flag_enable(efa, BM_ELEM_TAG);
333
334                                         BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
335                                                 luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
336                                                 copy_v2_v2(tf_uvorig[i], luv->uv);
337                                         }
338
339                                         uv_poly_copy_aspect(tf_uvorig, tf_uv, aspx, aspy, efa_len);
340
341                                         j = efa_len - 1;
342                                         BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
343                                                 sub_v2_v2v2(auv[i], tf_uv[j], tf_uv[i]); normalize_v2(auv[i]);
344                                                 sub_v3_v3v3(av[i], l->prev->v->co, l->v->co); normalize_v3(av[i]);
345                                                 j = i;
346                                         }
347
348                                         for (i = 0; i < efa_len; i++) {
349 #if 0
350                                                 /* Simple but slow, better reuse normalized vectors
351                                                  * (Not ported to bmesh, copied for reference) */
352                                                 uvang1 = RAD2DEG(angle_v2v2v2(tf_uv[3], tf_uv[0], tf_uv[1]));
353                                                 ang1 = RAD2DEG(angle_v3v3v3(efa->v4->co, efa->v1->co, efa->v2->co));
354 #endif
355                                                 uvang[i] = angle_normalized_v2v2(auv[i], auv[(i + 1) % efa_len]);
356                                                 ang[i] = angle_normalized_v3v3(av[i], av[(i + 1) % efa_len]);
357                                         }
358
359                                         /* TODO: use editmesh tessface */
360                                         immBegin(GPU_PRIM_TRI_FAN, efa->len);
361                                         BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
362                                                 luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
363                                                 a = fabsf(uvang[i] - ang[i]) / (float)M_PI;
364                                                 BKE_defvert_weight_to_rgb(col, 1.0f - pow2f(1.0f - a));
365                                                 immAttrib3fv(color, col);
366                                                 immVertex2fv(pos, luv->uv);
367                                         }
368                                         immEnd();
369                                 }
370                                 else {
371                                         if (efa == efa_act)
372                                                 efa_act = NULL;
373                                         BM_elem_flag_disable(efa, BM_ELEM_TAG);
374                                 }
375                         }
376
377                         immUnbindProgram();
378
379                         BLI_buffer_free(&uvang_buf);
380                         BLI_buffer_free(&ang_buf);
381                         BLI_buffer_free(&av_buf);
382                         BLI_buffer_free(&auv_buf);
383
384                         break;
385                 }
386         }
387
388         BLI_buffer_free(&tf_uv_buf);
389         BLI_buffer_free(&tf_uvorig_buf);
390 }
391
392 static void draw_uvs_lineloop_bmfaces(BMesh *bm, const int cd_loop_uv_offset, const uint shdr_pos)
393 {
394         BMIter iter, liter;
395         BMFace *efa;
396         BMLoop *l;
397         MLoopUV *luv;
398
399         /* For more efficiency first transfer the entire buffer to vram. */
400         GPUBatch *loop_batch = immBeginBatchAtMost(GPU_PRIM_LINE_LOOP, bm->totloop);
401
402         BM_ITER_MESH(efa, &iter, bm, BM_FACES_OF_MESH) {
403                 if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
404                         continue;
405
406                 BM_ITER_ELEM(l, &liter, efa, BM_LOOPS_OF_FACE) {
407                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
408                         immVertex2fv(shdr_pos, luv->uv);
409                 }
410         }
411         immEnd();
412
413         /* Then draw each face contour separately. */
414         GPU_batch_program_use_begin(loop_batch);
415         unsigned int index = 0;
416         BM_ITER_MESH(efa, &iter, bm, BM_FACES_OF_MESH) {
417                 if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
418                         continue;
419
420                 GPU_batch_draw_range_ex(loop_batch, index, efa->len, false);
421                 index += efa->len;
422         }
423         GPU_batch_program_use_end(loop_batch);
424         GPU_batch_discard(loop_batch);
425 }
426
427 static void draw_uvs_texpaint(Scene *scene, Object *ob)
428 {
429         Mesh *me = ob->data;
430         Material *ma;
431
432         ma = give_current_material(ob, ob->actcol);
433
434         if (me->mloopuv) {
435                 MPoly *mpoly = me->mpoly;
436                 MLoopUV *mloopuv, *mloopuv_base;
437                 int a, b;
438                 if (!(ma && ma->texpaintslot && ma->texpaintslot[ma->paint_active_slot].uvname &&
439                       (mloopuv = CustomData_get_layer_named(&me->ldata, CD_MLOOPUV, ma->texpaintslot[ma->paint_active_slot].uvname))))
440                 {
441                         mloopuv = me->mloopuv;
442                 }
443
444                 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
445
446                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
447
448                 immUniformThemeColor(TH_UV_SHADOW);
449
450                 mloopuv_base = mloopuv;
451
452                 for (a = me->totpoly; a > 0; a--, mpoly++) {
453                         if ((scene->toolsettings->uv_flag & UV_SHOW_SAME_IMAGE) && mpoly->mat_nr != ob->actcol - 1)
454                                 continue;
455
456                         immBegin(GPU_PRIM_LINE_LOOP, mpoly->totloop);
457
458                         mloopuv = mloopuv_base + mpoly->loopstart;
459                         for (b = 0; b < mpoly->totloop; b++, mloopuv++) {
460                                 immVertex2fv(pos, mloopuv->uv);
461                         }
462
463                         immEnd();
464                 }
465
466                 immUnbindProgram();
467         }
468 }
469
470 static void draw_uvs_looptri(BMEditMesh *em, unsigned int *r_loop_index, const int cd_loop_uv_offset, unsigned int pos)
471 {
472         unsigned int i = *r_loop_index;
473         BMFace *f = em->looptris[i][0]->f;
474         do {
475                 unsigned int j;
476                 for (j = 0; j < 3; j++) {
477                         MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(em->looptris[i][j], cd_loop_uv_offset);
478                         immVertex2fv(pos, luv->uv);
479                 }
480                 i++;
481         } while (i != em->tottri && (f == em->looptris[i][0]->f));
482         *r_loop_index = i - 1;
483 }
484
485 /* draws uv's in the image space */
486 static void draw_uvs(SpaceImage *sima, Scene *scene, Object *obedit, Depsgraph *depsgraph)
487 {
488         ToolSettings *ts;
489         Mesh *me = obedit->data;
490         BMEditMesh *em = me->edit_btmesh;
491         BMesh *bm = em->bm;
492         BMFace *efa, *efa_act;
493         BMLoop *l;
494         BMIter iter, liter;
495         MLoopUV *luv;
496         float col1[4], col2[4];
497         float pointsize;
498         int drawfaces, interpedges;
499         Image *ima = sima->image;
500
501         const int cd_loop_uv_offset  = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
502
503         unsigned int pos, color;
504
505         efa_act = EDBM_uv_active_face_get(em, false, false); /* will be set to NULL if hidden */
506         ts = scene->toolsettings;
507
508         drawfaces = draw_uvs_face_check(scene);
509         if (ts->uv_flag & UV_SYNC_SELECTION)
510                 interpedges = (ts->selectmode & SCE_SELECT_VERTEX);
511         else
512                 interpedges = (ts->uv_selectmode == UV_SELECT_VERTEX);
513
514         /* 1. draw shadow mesh */
515
516         if (sima->flag & SI_DRAWSHADOW) {
517                 Object *ob_cage_eval = DEG_get_evaluated_object(depsgraph, obedit);
518                 /* XXX TODO: Need to check if shadow mesh is different than original mesh. */
519                 bool is_cage_like_final_meshes = (ob_cage_eval == obedit);
520
521                 /* When sync selection is enabled, all faces are drawn (except for hidden)
522                  * so if cage is the same as the final, there is no point in drawing this. */
523                 if (((ts->uv_flag & UV_SYNC_SELECTION) == 0) || is_cage_like_final_meshes) {
524                         draw_uvs_shadow(ob_cage_eval);
525                 }
526         }
527
528         if (bm->totloop == 0) {
529                 return;
530         }
531
532         /* 2. draw colored faces */
533
534         if (sima->flag & SI_DRAW_STRETCH) {
535                 draw_uvs_stretch(sima, scene, obedit, em, efa_act);
536         }
537         else {
538                 unsigned int tri_count = 0;
539                 BM_ITER_MESH(efa, &iter, bm, BM_FACES_OF_MESH) {
540                         if (uvedit_face_visible_test(scene, obedit, ima, efa)) {
541                                 BM_elem_flag_enable(efa, BM_ELEM_TAG);
542                                 tri_count += efa->len - 2;
543                         }
544                         else {
545                                 BM_elem_flag_disable(efa, BM_ELEM_TAG);
546                         }
547                 }
548
549                 if (tri_count && !(sima->flag & SI_NO_DRAWFACES)) {
550                         /* draw transparent faces */
551                         UI_GetThemeColor4fv(TH_FACE, col1);
552                         UI_GetThemeColor4fv(TH_FACE_SELECT, col2);
553                         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
554                         GPU_blend(true);
555
556                         GPUVertFormat *format = immVertexFormat();
557                         pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
558                         color = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
559
560                         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
561
562                         GPUBatch *face_batch = immBeginBatch(GPU_PRIM_TRIS, tri_count * 3);
563                         for (unsigned int i = 0; i < em->tottri; i++) {
564                                 efa = em->looptris[i][0]->f;
565                                 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
566                                         const bool is_select = uvedit_face_select_test(scene, efa, cd_loop_uv_offset);
567
568                                         if (efa == efa_act) {
569                                                 /* only once */
570                                                 float tmp_col[4];
571                                                 UI_GetThemeColor4fv(TH_EDITMESH_ACTIVE, tmp_col);
572                                                 immAttrib4fv(color, tmp_col);
573                                         }
574                                         else {
575                                                 immAttrib4fv(color, is_select ? col2 : col1);
576                                         }
577
578                                         draw_uvs_looptri(em, &i, cd_loop_uv_offset, pos);
579                                 }
580                         }
581                         immEnd();
582
583                         /* XXX performance: we should not create and throw away result. */
584                         GPU_batch_draw(face_batch);
585                         GPU_batch_program_use_end(face_batch);
586                         GPU_batch_discard(face_batch);
587
588                         immUnbindProgram();
589
590                         GPU_blend(false);
591                 }
592                 else {
593                         if (efa_act && !uvedit_face_visible_test(scene, obedit, ima, efa_act)) {
594                                 efa_act = NULL;
595                         }
596                 }
597         }
598
599         /* 3. draw active face stippled */
600         /* (removed during OpenGL upgrade, reimplement if needed) */
601
602         /* 4. draw edges */
603
604         if (sima->flag & SI_SMOOTH_UV) {
605                 GPU_line_smooth(true);
606                 GPU_blend(true);
607                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
608         }
609
610         pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
611
612         switch (sima->dt_uv) {
613                 case SI_UVDT_DASH:
614                 {
615                         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
616
617                         float viewport_size[4];
618                         GPU_viewport_size_get_f(viewport_size);
619                         immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
620
621                         immUniform1i("colors_len", 2);  /* "advanced" mode */
622                         immUniformArray4fv("colors", (float *)(float[][4]){{0.56f, 0.56f, 0.56f, 1.0f}, {0.07f, 0.07f, 0.07f, 1.0f}}, 2);
623                         immUniform1f("dash_width", 4.0f);
624                         GPU_line_width(1.0f);
625
626                         break;
627                 }
628                 case SI_UVDT_BLACK: /* black/white */
629                 case SI_UVDT_WHITE:
630                         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
631                         if (sima->dt_uv == SI_UVDT_WHITE) {
632                                 immUniformColor3f(1.0f, 1.0f, 1.0f);
633                         }
634                         else {
635                                 immUniformColor3f(0.0f, 0.0f, 0.0f);
636                         }
637                         GPU_line_width(1.0f);
638
639                         break;
640                 case SI_UVDT_OUTLINE:
641                         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
642                         imm_cpack(0x0);
643                         GPU_line_width(3.0f);
644
645                         break;
646         }
647
648         /* For more efficiency first transfer the entire buffer to vram. */
649         GPUBatch *loop_batch = immBeginBatchAtMost(GPU_PRIM_LINE_LOOP, bm->totloop);
650         GPUVertBuf *loop_vbo = loop_batch->verts[0];
651         BM_ITER_MESH(efa, &iter, bm, BM_FACES_OF_MESH) {
652                 if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
653                         continue;
654
655                 BM_ITER_ELEM(l, &liter, efa, BM_LOOPS_OF_FACE) {
656                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
657                         immVertex2fv(pos, luv->uv);
658                 }
659         }
660         immEnd();
661
662         /* Then draw each face contour separately. */
663         if (loop_vbo->vertex_len != 0) {
664                 GPU_batch_program_use_begin(loop_batch);
665                 unsigned int index = 0, loop_vbo_count;
666                 BM_ITER_MESH(efa, &iter, bm, BM_FACES_OF_MESH) {
667                         if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
668                                 continue;
669
670                         GPU_batch_draw_range_ex(loop_batch, index, efa->len, false);
671                         index += efa->len;
672                 }
673                 loop_vbo_count = index;
674                 GPU_batch_program_use_end(loop_batch);
675                 immUnbindProgram();
676
677
678                 if (sima->dt_uv == SI_UVDT_OUTLINE) {
679                         GPU_line_width(1.0f);
680                         UI_GetThemeColor4fv(TH_WIRE_EDIT, col2);
681
682                         if (me->drawflag & ME_DRAWEDGES) {
683                                 int sel;
684                                 UI_GetThemeColor4fv(TH_EDGE_SELECT, col1);
685
686                                 if (interpedges) {
687                                         /* Create a color buffer. */
688                                         static GPUVertFormat format = { 0 };
689                                         static uint shdr_col;
690                                         if (format.attr_len == 0) {
691                                                 shdr_col = GPU_vertformat_attr_add(&format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
692                                         }
693
694                                         GPUVertBuf *vbo_col = GPU_vertbuf_create_with_format(&format);
695                                         GPU_vertbuf_data_alloc(vbo_col, loop_vbo_count);
696
697                                         index = 0;
698                                         BM_ITER_MESH(efa, &iter, bm, BM_FACES_OF_MESH) {
699                                                 if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
700                                                         continue;
701
702                                                 BM_ITER_ELEM(l, &liter, efa, BM_LOOPS_OF_FACE) {
703                                                         sel = uvedit_uv_select_test(scene, l, cd_loop_uv_offset);
704                                                         GPU_vertbuf_attr_set(vbo_col, shdr_col, index++, sel ? col1 : col2);
705                                                 }
706                                         }
707                                         /* Reuse the UV buffer and add the color buffer. */
708                                         GPU_batch_vertbuf_add_ex(loop_batch, vbo_col, true);
709
710                                         /* Now draw each face contour separately with another builtin program. */
711                                         GPU_batch_program_set_builtin(loop_batch, GPU_SHADER_2D_SMOOTH_COLOR);
712                                         GPU_matrix_bind(loop_batch->interface);
713
714                                         GPU_batch_program_use_begin(loop_batch);
715                                         index = 0;
716                                         BM_ITER_MESH(efa, &iter, bm, BM_FACES_OF_MESH) {
717                                                 if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
718                                                         continue;
719
720                                                 GPU_batch_draw_range_ex(loop_batch, index, efa->len, false);
721                                                 index += efa->len;
722                                         }
723                                         GPU_batch_program_use_end(loop_batch);
724                                 }
725                                 else {
726                                         GPUVertFormat *format = immVertexFormat();
727                                         pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
728                                         color = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
729
730                                         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
731
732                                         /* Use batch here to avoid problems with `IMM_BUFFER_SIZE`. */
733                                         GPUBatch *flat_edges_batch = immBeginBatchAtMost(GPU_PRIM_LINES, loop_vbo_count * 2);
734                                         BM_ITER_MESH(efa, &iter, bm, BM_FACES_OF_MESH) {
735                                                 if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
736                                                         continue;
737
738                                                 BM_ITER_ELEM(l, &liter, efa, BM_LOOPS_OF_FACE) {
739                                                         sel = uvedit_edge_select_test(scene, l, cd_loop_uv_offset);
740                                                         immAttrib4fv(color, sel ? col1 : col2);
741
742                                                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
743                                                         immVertex2fv(pos, luv->uv);
744                                                         luv = BM_ELEM_CD_GET_VOID_P(l->next, cd_loop_uv_offset);
745                                                         immVertex2fv(pos, luv->uv);
746                                                 }
747                                         }
748                                         immEnd();
749
750                                         GPU_batch_draw(flat_edges_batch);
751                                         GPU_batch_discard(flat_edges_batch);
752
753                                         immUnbindProgram();
754                                 }
755                         }
756                         else {
757                                 GPU_batch_uniform_4fv(loop_batch, "color", col2);
758                                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
759
760                                 /* no nice edges */
761                                 GPU_batch_program_use_begin(loop_batch);
762                                 index = 0;
763                                 BM_ITER_MESH(efa, &iter, bm, BM_FACES_OF_MESH) {
764                                         if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
765                                                 continue;
766
767                                         GPU_batch_draw_range_ex(loop_batch, index, efa->len, false);
768                                         index += efa->len;
769                                 }
770                                 GPU_batch_program_use_end(loop_batch);
771                                 immUnbindProgram();
772                         }
773                 }
774         }
775         else {
776                 immUnbindProgram();
777         }
778
779         GPU_batch_discard(loop_batch);
780
781         if (sima->flag & SI_SMOOTH_UV) {
782                 GPU_line_smooth(false);
783                 GPU_blend(false);
784         }
785
786         /* 5. draw face centers */
787
788         if (drawfaces) {
789                 float cent[2];
790                 bool col_set = false;
791
792                 GPUVertFormat *format = immVertexFormat();
793                 pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
794                 color = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
795
796                 immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
797
798                 pointsize = UI_GetThemeValuef(TH_FACEDOT_SIZE);
799                 GPU_point_size(pointsize);
800
801                 immBeginAtMost(GPU_PRIM_POINTS, bm->totface);
802
803                 /* unselected faces */
804
805                 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
806                         if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
807                                 continue;
808
809                         if (!uvedit_face_select_test(scene, efa, cd_loop_uv_offset)) {
810                                 /* Only set color for the first face */
811                                 if (!col_set) {
812                                         UI_GetThemeColor3fv(TH_WIRE, col1);
813                                         immAttrib3fv(color, col1);
814
815                                         col_set = true;
816                                 }
817
818                                 uv_poly_center(efa, cent, cd_loop_uv_offset);
819                                 immVertex2fv(pos, cent);
820                         }
821                 }
822
823                 col_set = false;
824
825                 /* selected faces */
826
827                 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
828                         if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
829                                 continue;
830
831                         if (uvedit_face_select_test(scene, efa, cd_loop_uv_offset)) {
832                                 /* Only set color for the first face */
833                                 if (!col_set) {
834                                         UI_GetThemeColor3fv(TH_FACE_DOT, col1);
835                                         immAttrib3fv(color, col1);
836
837                                         col_set = true;
838                                 }
839
840                                 uv_poly_center(efa, cent, cd_loop_uv_offset);
841                                 immVertex2fv(pos, cent);
842                         }
843                 }
844
845                 immEnd();
846
847                 immUnbindProgram();
848         }
849
850         /* 6. draw uv vertices */
851
852         if (drawfaces != 2) { /* 2 means Mesh Face Mode */
853                 pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
854
855                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
856
857                 /* unselected uvs */
858                 immUniformThemeColor(TH_VERTEX);
859                 pointsize = UI_GetThemeValuef(TH_VERTEX_SIZE);
860                 GPU_point_size(pointsize);
861
862                 immBeginAtMost(GPU_PRIM_POINTS, bm->totloop);
863
864                 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
865                         if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
866                                 continue;
867
868                         BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
869                                 luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
870                                 if (!uvedit_uv_select_test(scene, l, cd_loop_uv_offset))
871                                         immVertex2fv(pos, luv->uv);
872                         }
873                 }
874
875                 immEnd();
876
877                 /* pinned uvs */
878                 /* give odd pointsizes odd pin pointsizes */
879                 GPU_point_size(pointsize * 2 + (((int)pointsize % 2) ? (-1) : 0));
880                 imm_cpack(0xFF);
881
882                 immBeginAtMost(GPU_PRIM_POINTS, bm->totloop);
883
884                 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
885                         if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
886                                 continue;
887
888                         BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
889                                 luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
890
891                                 if (luv->flag & MLOOPUV_PINNED)
892                                         immVertex2fv(pos, luv->uv);
893                         }
894                 }
895
896                 immEnd();
897
898                 /* selected uvs */
899                 immUniformThemeColor(TH_VERTEX_SELECT);
900                 GPU_point_size(pointsize);
901
902                 immBeginAtMost(GPU_PRIM_POINTS, bm->totloop);
903
904                 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
905                         if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
906                                 continue;
907
908                         BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
909                                 luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
910
911                                 if (uvedit_uv_select_test(scene, l, cd_loop_uv_offset))
912                                         immVertex2fv(pos, luv->uv);
913                         }
914                 }
915
916                 immEnd();
917
918                 immUnbindProgram();
919         }
920 }
921
922
923 static void draw_uv_shadows_get(
924         SpaceImage *sima, Object *ob, Object *obedit,
925         bool *show_shadow, bool *show_texpaint)
926 {
927         *show_shadow = *show_texpaint = false;
928
929         if (ED_space_image_show_render(sima) || (sima->flag & SI_NO_DRAW_TEXPAINT))
930                 return;
931
932         if ((sima->mode == SI_MODE_PAINT) && obedit && obedit->type == OB_MESH) {
933                 struct BMEditMesh *em = BKE_editmesh_from_object(obedit);
934
935                 *show_shadow = EDBM_uv_check(em);
936         }
937
938         *show_texpaint = (ob && ob->type == OB_MESH && ob->mode == OB_MODE_TEXTURE_PAINT);
939 }
940
941 void ED_uvedit_draw_main(
942         SpaceImage *sima,
943         ARegion *ar, Scene *scene, ViewLayer *view_layer, Object *obedit, Object *obact, Depsgraph *depsgraph)
944 {
945         ToolSettings *toolsettings = scene->toolsettings;
946         bool show_uvedit, show_uvshadow, show_texpaint_uvshadow;
947
948         show_uvedit = ED_space_image_show_uvedit(sima, obedit);
949         draw_uv_shadows_get(sima, obact, obedit, &show_uvshadow, &show_texpaint_uvshadow);
950
951         if (show_uvedit || show_uvshadow || show_texpaint_uvshadow) {
952                 if (show_uvshadow) {
953                         draw_uvs_shadow(obedit);
954                 }
955                 else if (show_uvedit) {
956                         uint objects_len = 0;
957                         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(view_layer, &objects_len);
958                         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
959                                 Object *ob_iter = objects[ob_index];
960                                 draw_uvs(sima, scene, ob_iter, depsgraph);
961                         }
962                         MEM_freeN(objects);
963                 }
964                 else {
965                         draw_uvs_texpaint(scene, obact);
966                 }
967
968                 if (show_uvedit && !(toolsettings->use_uv_sculpt))
969                         ED_image_draw_cursor(ar, sima->cursor);
970         }
971 }