Fix T41414, T41386.
[blender.git] / source / blender / editors / space_view3d / drawmesh.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 update, glsl support
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_view3d/drawmesh.c
27  *  \ingroup spview3d
28  */
29
30 #include <string.h>
31 #include <math.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_utildefines.h"
36 #include "BLI_bitmap.h"
37 #include "BLI_math.h"
38
39 #include "DNA_material_types.h"
40 #include "DNA_mesh_types.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_node_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_property_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_view3d_types.h"
48
49 #include "BKE_DerivedMesh.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_material.h"
53 #include "BKE_paint.h"
54 #include "BKE_property.h"
55 #include "BKE_editmesh.h"
56 #include "BKE_scene.h"
57
58 #include "BIF_gl.h"
59 #include "BIF_glutil.h"
60
61 #include "UI_resources.h"
62
63 #include "GPU_buffers.h"
64 #include "GPU_extensions.h"
65 #include "GPU_draw.h"
66 #include "GPU_material.h"
67
68 #include "RE_engine.h"
69
70 #include "ED_uvedit.h"
71
72 #include "view3d_intern.h"  /* own include */
73
74 /* user data structures for derived mesh callbacks */
75 typedef struct drawMeshFaceSelect_userData {
76         Mesh *me;
77         BLI_bitmap *edge_flags; /* pairs of edge options (visible, select) */
78 } drawMeshFaceSelect_userData;
79
80 typedef struct drawEMTFMapped_userData {
81         BMEditMesh *em;
82         bool has_mcol;
83         bool has_mtface;
84         MFace *mf;
85         MTFace *tf;
86 } drawEMTFMapped_userData;
87
88 typedef struct drawTFace_userData {
89         Mesh *me;
90         MFace *mf;
91         MTFace *tf;
92 } drawTFace_userData;
93
94 /**************************** Face Select Mode *******************************/
95
96 /* mainly to be less confusing */
97 BLI_INLINE int edge_vis_index(const int index) { return index * 2; }
98 BLI_INLINE int edge_sel_index(const int index) { return index * 2 + 1; }
99
100 static BLI_bitmap *get_tface_mesh_marked_edge_info(Mesh *me)
101 {
102         BLI_bitmap *bitmap_edge_flags = BLI_BITMAP_NEW(me->totedge * 2, __func__);
103         MPoly *mp;
104         MLoop *ml;
105         int i, j;
106         bool select_set;
107         
108         for (i = 0; i < me->totpoly; i++) {
109                 mp = &me->mpoly[i];
110
111                 if (!(mp->flag & ME_HIDE)) {
112                         select_set = (mp->flag & ME_FACE_SEL) != 0;
113
114                         ml = me->mloop + mp->loopstart;
115                         for (j = 0; j < mp->totloop; j++, ml++) {
116                                 BLI_BITMAP_ENABLE(bitmap_edge_flags, edge_vis_index(ml->e));
117                                 if (select_set) BLI_BITMAP_ENABLE(bitmap_edge_flags, edge_sel_index(ml->e));
118                         }
119                 }
120         }
121
122         return bitmap_edge_flags;
123 }
124
125
126 static DMDrawOption draw_mesh_face_select__setHiddenOpts(void *userData, int index)
127 {
128         drawMeshFaceSelect_userData *data = userData;
129         Mesh *me = data->me;
130
131         if (me->drawflag & ME_DRAWEDGES) {
132                 if ((me->drawflag & ME_HIDDENEDGES) || (BLI_BITMAP_TEST(data->edge_flags, edge_vis_index(index))))
133                         return DM_DRAW_OPTION_NORMAL;
134                 else
135                         return DM_DRAW_OPTION_SKIP;
136         }
137         else if (BLI_BITMAP_TEST(data->edge_flags, edge_sel_index(index)))
138                 return DM_DRAW_OPTION_NORMAL;
139         else
140                 return DM_DRAW_OPTION_SKIP;
141 }
142
143 static DMDrawOption draw_mesh_face_select__setSelectOpts(void *userData, int index)
144 {
145         drawMeshFaceSelect_userData *data = userData;
146         return (BLI_BITMAP_TEST(data->edge_flags, edge_sel_index(index))) ? DM_DRAW_OPTION_NORMAL : DM_DRAW_OPTION_SKIP;
147 }
148
149 /* draws unselected */
150 static DMDrawOption draw_mesh_face_select__drawFaceOptsInv(void *userData, int index)
151 {
152         Mesh *me = (Mesh *)userData;
153
154         MPoly *mpoly = &me->mpoly[index];
155         if (!(mpoly->flag & ME_HIDE) && !(mpoly->flag & ME_FACE_SEL))
156                 return DM_DRAW_OPTION_NO_MCOL;  /* Don't set color */
157         else
158                 return DM_DRAW_OPTION_SKIP;
159 }
160
161 void draw_mesh_face_select(RegionView3D *rv3d, Mesh *me, DerivedMesh *dm)
162 {
163         drawMeshFaceSelect_userData data;
164
165         data.me = me;
166         data.edge_flags = get_tface_mesh_marked_edge_info(me);
167
168         glEnable(GL_DEPTH_TEST);
169         glDisable(GL_LIGHTING);
170         ED_view3d_polygon_offset(rv3d, 1.0);
171
172         /* Draw (Hidden) Edges */
173         setlinestyle(1);
174         UI_ThemeColor(TH_EDGE_FACESEL);
175         dm->drawMappedEdges(dm, draw_mesh_face_select__setHiddenOpts, &data);
176         setlinestyle(0);
177
178         /* Draw Selected Faces */
179         if (me->drawflag & ME_DRAWFACES) {
180                 glEnable(GL_BLEND);
181                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
182                 /* dull unselected faces so as not to get in the way of seeing color */
183                 glColor4ub(96, 96, 96, 64);
184                 dm->drawMappedFaces(dm, draw_mesh_face_select__drawFaceOptsInv, NULL, NULL, (void *)me, 0);
185                 glDisable(GL_BLEND);
186         }
187         
188         ED_view3d_polygon_offset(rv3d, 1.0);
189
190         /* Draw Stippled Outline for selected faces */
191         glColor3ub(255, 255, 255);
192         setlinestyle(1);
193         dm->drawMappedEdges(dm, draw_mesh_face_select__setSelectOpts, &data);
194         setlinestyle(0);
195
196         ED_view3d_polygon_offset(rv3d, 0.0);  /* resets correctly now, even after calling accumulated offsets */
197
198         MEM_freeN(data.edge_flags);
199 }
200
201 /***************************** Texture Drawing ******************************/
202
203 static Material *give_current_material_or_def(Object *ob, int matnr)
204 {
205         extern Material defmaterial;  /* render module abuse... */
206         Material *ma = give_current_material(ob, matnr);
207
208         return ma ? ma : &defmaterial;
209 }
210
211 /* Icky globals, fix with userdata parameter */
212
213 static struct TextureDrawState {
214         Object *ob;
215         Image *stencil;
216         bool stencil_invert;
217         bool use_game_mat;
218         int is_lit, is_tex;
219         int color_profile;
220         bool use_backface_culling;
221         unsigned char obcol[4];
222         float stencil_col[4];
223         bool is_texpaint;
224 } Gtexdraw = {NULL, NULL, false, false, 0, 0, 0, false, {0, 0, 0, 0}, {0.0f, 0.0f, 0.0f, 1.0f}, false};
225
226 static bool set_draw_settings_cached(int clearcache, MTFace *texface, Material *ma, struct TextureDrawState gtexdraw)
227 {
228         static Material *c_ma;
229         static int c_textured;
230         static MTFace c_texface;
231         static int c_backculled;
232         static bool c_badtex;
233         static int c_lit;
234         static int c_has_texface;
235
236         int backculled = 1;
237         int alphablend = GPU_BLEND_SOLID;
238         int textured = 0;
239         int lit = 0;
240         int has_texface = texface != NULL;
241         bool need_set_tpage = false;
242         bool texpaint = ((gtexdraw.ob->mode & OB_MODE_TEXTURE_PAINT) != 0);
243
244         Image *ima = NULL;
245
246         if (ma != NULL) {
247                 if (ma->mode & MA_TRANSP) {
248                         alphablend = GPU_BLEND_ALPHA;
249                 }
250         }
251
252         if (clearcache) {
253                 c_textured = c_lit = c_backculled = -1;
254                 memset(&c_texface, 0, sizeof(MTFace));
255                 c_badtex = false;
256                 c_has_texface = -1;
257                 c_ma = NULL;
258         }
259         else {
260                 textured = gtexdraw.is_tex;
261         }
262
263         /* convert number of lights into boolean */
264         if (gtexdraw.is_lit) lit = 1;
265
266         backculled = gtexdraw.use_backface_culling;
267         if (ma) {
268                 if (ma->mode & MA_SHLESS) lit = 0;
269                 if (gtexdraw.use_game_mat) {
270                         backculled = backculled || (ma->game.flag & GEMAT_BACKCULL);
271                         alphablend = ma->game.alpha_blend;
272                 }
273         }
274
275         if (texface && !texpaint) {
276                 textured = textured && (texface->tpage);
277
278                 /* no material, render alpha if texture has depth=32 */
279                 if (!ma && BKE_image_has_alpha(texface->tpage))
280                         alphablend = GPU_BLEND_ALPHA;
281         }
282         else if (texpaint && ma) {
283                 ima = ma->texpaintslot ? ma->texpaintslot[ma->paint_active_slot].ima : NULL;
284         }
285         else
286                 textured = 0;
287
288         if (backculled != c_backculled) {
289                 if (backculled) glEnable(GL_CULL_FACE);
290                 else glDisable(GL_CULL_FACE);
291
292                 c_backculled = backculled;
293         }
294
295         /* need to re-set tpage if textured flag changed or existsment of texface changed..  */
296         need_set_tpage = textured != c_textured || has_texface != c_has_texface;
297         /* ..or if settings inside texface were changed (if texface was used) */
298         need_set_tpage |= (texpaint && c_ma != ma) || (texface && memcmp(&c_texface, texface, sizeof(c_texface)));
299
300         if (need_set_tpage) {
301                 if (textured) {
302                         if (texpaint) {
303                                 c_badtex = false;
304                                 if (GPU_verify_image(ima, NULL, 0, 1, 0, false)) {
305                                         glEnable(GL_TEXTURE_2D);
306                                         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
307                                 }
308                                 else {
309                                         c_badtex = true;
310                                         GPU_clear_tpage(true);
311                                         glDisable(GL_TEXTURE_2D);
312                                         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
313                                         glBindTexture(GL_TEXTURE_2D, 0);
314                                 }
315                         }
316                         else {
317                                 c_badtex = !GPU_set_tpage(texface, !texpaint, alphablend);
318                         }
319                 }
320                 else {
321                         GPU_set_tpage(NULL, 0, 0);
322                         c_badtex = false;
323                 }
324                 c_textured = textured;
325                 c_has_texface = has_texface;
326                 if (texface)
327                         memcpy(&c_texface, texface, sizeof(c_texface));
328         }
329
330         if (c_badtex) lit = 0;
331         if (lit != c_lit || ma != c_ma) {
332                 if (lit) {
333                         float spec[4];
334                         if (!ma) ma = give_current_material_or_def(NULL, 0);  /* default material */
335
336                         spec[0] = ma->spec * ma->specr;
337                         spec[1] = ma->spec * ma->specg;
338                         spec[2] = ma->spec * ma->specb;
339                         spec[3] = 1.0;
340
341                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
342                         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
343                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, CLAMPIS(ma->har, 0, 128));
344                         glEnable(GL_LIGHTING);
345                         glEnable(GL_COLOR_MATERIAL);
346                 }
347                 else {
348                         glDisable(GL_LIGHTING); 
349                         glDisable(GL_COLOR_MATERIAL);
350                 }
351                 c_lit = lit;
352                 c_ma = ma;
353         }
354
355         return c_badtex;
356 }
357
358 static void draw_textured_begin(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
359 {
360         unsigned char obcol[4];
361         bool is_tex, solidtex;
362         Mesh *me = ob->data;
363         ImagePaintSettings *imapaint = &scene->toolsettings->imapaint;
364
365         /* XXX scene->obedit warning */
366
367         /* texture draw is abused for mask selection mode, do this so wire draw
368          * with face selection in weight paint is not lit. */
369         if ((v3d->drawtype <= OB_WIRE) && (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))) {
370                 solidtex = false;
371                 Gtexdraw.is_lit = 0;
372         }
373         else if (v3d->drawtype == OB_SOLID || ((ob->mode & OB_MODE_EDIT) && v3d->drawtype != OB_TEXTURE)) {
374                 /* draw with default lights in solid draw mode and edit mode */
375                 solidtex = true;
376                 Gtexdraw.is_lit = -1;
377         }
378         else {
379                 /* draw with lights in the scene otherwise */
380                 solidtex = false;
381                 if (v3d->flag2 & V3D_SHADELESS_TEX)
382                         Gtexdraw.is_lit = 0;
383                 else
384                         Gtexdraw.is_lit = GPU_scene_object_lights(scene, ob, v3d->lay, rv3d->viewmat, !rv3d->is_persp);
385         }
386         
387         rgba_float_to_uchar(obcol, ob->col);
388
389         if (solidtex || v3d->drawtype == OB_TEXTURE) is_tex = true;
390         else is_tex = false;
391
392         Gtexdraw.ob = ob;
393         Gtexdraw.stencil = (imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL) ? imapaint->stencil : NULL;
394         Gtexdraw.stencil_invert = ((imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL_INV) != 0);
395         Gtexdraw.is_texpaint = (ob->mode == OB_MODE_TEXTURE_PAINT);
396         copy_v3_v3(Gtexdraw.stencil_col, imapaint->stencil_col);
397         Gtexdraw.is_tex = is_tex;
398
399         /* load the stencil texture here */
400         if (Gtexdraw.is_texpaint && (Gtexdraw.stencil != NULL)) {
401                 glActiveTexture(GL_TEXTURE1);
402                 if (GPU_verify_image(Gtexdraw.stencil, NULL, false, false, false, false)) {
403                         glEnable(GL_TEXTURE_2D);
404                         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
405                         glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
406                         glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
407                         glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_TEXTURE);
408                         glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
409                         glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);
410                         glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Gtexdraw.stencil_col);
411                         if (!Gtexdraw.stencil_invert) {
412                                 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_ONE_MINUS_SRC_COLOR);
413                         }
414                         else {
415                                 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_COLOR);
416                         }
417                 }
418                 glActiveTexture(GL_TEXTURE0);
419         }
420
421         Gtexdraw.color_profile = BKE_scene_check_color_management_enabled(scene);
422         Gtexdraw.use_game_mat = (RE_engines_find(scene->r.engine)->flag & RE_GAME) != 0;
423         Gtexdraw.use_backface_culling = (v3d->flag2 & V3D_BACKFACE_CULLING) != 0;
424
425         memcpy(Gtexdraw.obcol, obcol, sizeof(obcol));
426         set_draw_settings_cached(1, NULL, NULL, Gtexdraw);
427         glShadeModel(GL_SMOOTH);
428         glCullFace(GL_BACK);
429         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, (me->flag & ME_TWOSIDED) ? GL_TRUE : GL_FALSE);
430 }
431
432 static void draw_textured_end(void)
433 {
434         if (Gtexdraw.ob->mode & OB_MODE_TEXTURE_PAINT) {
435                 if (Gtexdraw.stencil != NULL) {
436                         glActiveTexture(GL_TEXTURE1);
437                         glDisable(GL_TEXTURE_2D);
438                         glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_COLOR);
439                         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
440                         glBindTexture(GL_TEXTURE_2D, 0);
441                         glActiveTexture(GL_TEXTURE0);
442                 }               
443                 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
444                 /* manual reset, since we don't use tpage */
445                 glBindTexture(GL_TEXTURE_2D, 0);
446                 /* force switch off textures */
447                 GPU_clear_tpage(true);
448         }
449         else {
450                 /* switch off textures */
451                 GPU_set_tpage(NULL, 0, 0);
452         }
453
454         glShadeModel(GL_FLAT);
455         glDisable(GL_CULL_FACE);
456         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
457
458         /* XXX, bad patch - GPU_default_lights() calls
459          * glLightfv(GL_POSITION, ...) which
460          * is transformed by the current matrix... we
461          * need to make sure that matrix is identity.
462          * 
463          * It would be better if drawmesh.c kept track
464          * of and restored the light settings it changed.
465          *  - zr
466          */
467         glPushMatrix();
468         glLoadIdentity();
469         GPU_default_lights();
470         glPopMatrix();
471 }
472
473 static DMDrawOption draw_tface__set_draw_legacy(MTFace *tface, const bool has_mcol, int matnr)
474 {
475         Material *ma = give_current_material(Gtexdraw.ob, matnr + 1);
476         bool invalidtexture = false;
477
478         if (ma && (ma->game.flag & GEMAT_INVISIBLE))
479                 return DM_DRAW_OPTION_SKIP;
480
481         invalidtexture = set_draw_settings_cached(0, tface, ma, Gtexdraw);
482
483         if (tface && invalidtexture) {
484                 glColor3ub(0xFF, 0x00, 0xFF);
485                 return DM_DRAW_OPTION_NO_MCOL; /* Don't set color */
486         }
487         else if (ma && (ma->shade_flag & MA_OBCOLOR)) {
488                 glColor3ubv(Gtexdraw.obcol);
489                 return DM_DRAW_OPTION_NO_MCOL; /* Don't set color */
490         }
491         else if (!has_mcol) {
492                 if (tface) {
493                         glColor3f(1.0, 1.0, 1.0);
494                 }
495                 else {
496                         if (ma) {
497                                 float col[3];
498                                 if (Gtexdraw.color_profile) linearrgb_to_srgb_v3_v3(col, &ma->r);
499                                 else copy_v3_v3(col, &ma->r);
500                                 
501                                 glColor3fv(col);
502                         }
503                         else {
504                                 glColor3f(1.0, 1.0, 1.0);
505                         }
506                 }
507                 return DM_DRAW_OPTION_NO_MCOL; /* Don't set color */
508         }
509         else {
510                 return DM_DRAW_OPTION_NORMAL; /* Set color from mcol */
511         }
512 }
513
514 static DMDrawOption draw_mcol__set_draw_legacy(MTFace *UNUSED(tface), const bool has_mcol, int UNUSED(matnr))
515 {
516         if (has_mcol)
517                 return DM_DRAW_OPTION_NORMAL;
518         else
519                 return DM_DRAW_OPTION_NO_MCOL;
520 }
521
522 static DMDrawOption draw_tface__set_draw(MTFace *tface, const bool UNUSED(has_mcol), int matnr)
523 {
524         Material *ma = give_current_material(Gtexdraw.ob, matnr + 1);
525
526         if (ma && (ma->game.flag & GEMAT_INVISIBLE)) return 0;
527
528         if (tface || Gtexdraw.is_texpaint)
529                 set_draw_settings_cached(0, tface, ma, Gtexdraw);
530
531         /* always use color from mcol, as set in update_tface_color_layer */
532         return DM_DRAW_OPTION_NORMAL;
533 }
534
535 static void update_tface_color_layer(DerivedMesh *dm)
536 {
537         MTFace *tface = DM_get_tessface_data_layer(dm, CD_MTFACE);
538         MFace *mface = dm->getTessFaceArray(dm);
539         MCol *finalCol;
540         int i, j;
541         MCol *mcol = dm->getTessFaceDataArray(dm, CD_PREVIEW_MCOL);
542         if (!mcol)
543                 mcol = dm->getTessFaceDataArray(dm, CD_MCOL);
544
545         if (CustomData_has_layer(&dm->faceData, CD_TEXTURE_MCOL)) {
546                 finalCol = CustomData_get_layer(&dm->faceData, CD_TEXTURE_MCOL);
547         }
548         else {
549                 finalCol = MEM_mallocN(sizeof(MCol) * 4 * dm->getNumTessFaces(dm), "add_tface_color_layer");
550
551                 CustomData_add_layer(&dm->faceData, CD_TEXTURE_MCOL, CD_ASSIGN, finalCol, dm->numTessFaceData);
552         }
553
554         for (i = 0; i < dm->getNumTessFaces(dm); i++) {
555                 Material *ma = give_current_material(Gtexdraw.ob, mface[i].mat_nr + 1);
556
557                 if (ma && (ma->game.flag & GEMAT_INVISIBLE)) {
558                         if (mcol)
559                                 memcpy(&finalCol[i * 4], &mcol[i * 4], sizeof(MCol) * 4);
560                         else
561                                 for (j = 0; j < 4; j++) {
562                                         finalCol[i * 4 + j].b = 255;
563                                         finalCol[i * 4 + j].g = 255;
564                                         finalCol[i * 4 + j].r = 255;
565                                 }
566                 }
567                 else if (tface && set_draw_settings_cached(0, tface, ma, Gtexdraw)) {
568                         for (j = 0; j < 4; j++) {
569                                 finalCol[i * 4 + j].b = 255;
570                                 finalCol[i * 4 + j].g = 0;
571                                 finalCol[i * 4 + j].r = 255;
572                         }
573                 }
574                 else if (ma && (ma->shade_flag & MA_OBCOLOR)) {
575                         for (j = 0; j < 4; j++) {
576                                 finalCol[i * 4 + j].b = Gtexdraw.obcol[0];
577                                 finalCol[i * 4 + j].g = Gtexdraw.obcol[1];
578                                 finalCol[i * 4 + j].r = Gtexdraw.obcol[2];
579                         }
580                 }
581                 else if (!mcol) {
582                         if (tface) {
583                                 for (j = 0; j < 4; j++) {
584                                         finalCol[i * 4 + j].b = 255;
585                                         finalCol[i * 4 + j].g = 255;
586                                         finalCol[i * 4 + j].r = 255;
587                                 }
588                         }
589                         else {
590                                 float col[3];
591
592                                 if (ma) {
593                                         if (Gtexdraw.color_profile) linearrgb_to_srgb_v3_v3(col, &ma->r);
594                                         else copy_v3_v3(col, &ma->r);
595                                         
596                                         for (j = 0; j < 4; j++) {
597                                                 finalCol[i * 4 + j].b = FTOCHAR(col[0]);
598                                                 finalCol[i * 4 + j].g = FTOCHAR(col[1]);
599                                                 finalCol[i * 4 + j].r = FTOCHAR(col[2]);
600                                         }
601                                 }
602                                 else
603                                         for (j = 0; j < 4; j++) {
604                                                 finalCol[i * 4 + j].b = 255;
605                                                 finalCol[i * 4 + j].g = 255;
606                                                 finalCol[i * 4 + j].r = 255;
607                                         }
608                         }
609                 }
610                 else {
611                         for (j = 0; j < 4; j++) {
612                                 finalCol[i * 4 + j].r = mcol[i * 4 + j].r;
613                                 finalCol[i * 4 + j].g = mcol[i * 4 + j].g;
614                                 finalCol[i * 4 + j].b = mcol[i * 4 + j].b;
615                         }
616                 }
617         }
618 }
619
620 static DMDrawOption draw_tface_mapped__set_draw(void *userData, int index)
621 {
622         Mesh *me = ((drawTFace_userData *)userData)->me;
623
624         /* array checked for NULL before calling */
625         MPoly *mpoly = &me->mpoly[index];
626
627         BLI_assert(index >= 0 && index < me->totpoly);
628
629         if (mpoly->flag & ME_HIDE) {
630                 return DM_DRAW_OPTION_SKIP;
631         }
632         else {
633                 MTexPoly *tpoly = (me->mtpoly) ? &me->mtpoly[index] : NULL;
634                 MTFace mtf = {{{0}}};
635                 int matnr = mpoly->mat_nr;
636
637                 if (tpoly) {
638                         ME_MTEXFACE_CPY(&mtf, tpoly);
639                 }
640
641                 return draw_tface__set_draw(&mtf, (me->mloopcol != NULL), matnr);
642         }
643 }
644
645 static DMDrawOption draw_em_tf_mapped__set_draw(void *userData, int index)
646 {
647         drawEMTFMapped_userData *data = userData;
648         BMEditMesh *em = data->em;
649         BMFace *efa;
650
651         if (UNLIKELY(index >= em->bm->totface))
652                 return DM_DRAW_OPTION_NORMAL;
653
654         efa = BM_face_at_index(em->bm, index);
655
656         if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
657                 return DM_DRAW_OPTION_SKIP;
658         }
659         else {
660                 MTFace mtf = {{{0}}};
661                 int matnr = efa->mat_nr;
662
663                 if (data->has_mtface) {
664                         MTexPoly *tpoly = CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_MTEXPOLY);
665                         ME_MTEXFACE_CPY(&mtf, tpoly);
666                 }
667
668                 return draw_tface__set_draw_legacy(data->has_mtface ? &mtf : NULL,
669                                                    data->has_mcol, matnr);
670         }
671 }
672
673 /* when face select is on, use face hidden flag */
674 static DMDrawOption wpaint__setSolidDrawOptions_facemask(void *userData, int index)
675 {
676         Mesh *me = (Mesh *)userData;
677         MPoly *mp = &me->mpoly[index];
678         if (mp->flag & ME_HIDE)
679                 return DM_DRAW_OPTION_SKIP;
680         return DM_DRAW_OPTION_NORMAL;
681 }
682
683 static void draw_mesh_text(Scene *scene, Object *ob, int glsl)
684 {
685         Mesh *me = ob->data;
686         DerivedMesh *ddm;
687         MPoly *mp, *mface  = me->mpoly;
688         MTexPoly *mtpoly   = me->mtpoly;
689         MLoopUV *mloopuv   = me->mloopuv;
690         MLoopUV *luv;
691         MLoopCol *mloopcol = me->mloopcol;  /* why does mcol exist? */
692         MLoopCol *lcol;
693
694         bProperty *prop = BKE_bproperty_object_get(ob, "Text");
695         GPUVertexAttribs gattribs;
696         int a, totpoly = me->totpoly;
697
698         /* fake values to pass to GPU_render_text() */
699         MCol  tmp_mcol[4]  = {{0}};
700         MCol *tmp_mcol_pt  = mloopcol ? tmp_mcol : NULL;
701         MTFace tmp_tf      = {{{0}}};
702
703         /* don't draw without tfaces */
704         if (!mtpoly || !mloopuv)
705                 return;
706
707         /* don't draw when editing */
708         if (ob->mode & OB_MODE_EDIT)
709                 return;
710         else if (ob == OBACT)
711                 if (BKE_paint_select_elem_test(ob))
712                         return;
713
714         ddm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
715
716         for (a = 0, mp = mface; a < totpoly; a++, mtpoly++, mp++) {
717                 short matnr = mp->mat_nr;
718                 int mf_smooth = mp->flag & ME_SMOOTH;
719                 Material *mat = (me->mat) ? me->mat[matnr] : NULL;
720                 int mode = mat ? mat->game.flag : GEMAT_INVISIBLE;
721
722
723                 if (!(mode & GEMAT_INVISIBLE) && (mode & GEMAT_TEXT) && mp->totloop >= 3) {
724                         /* get the polygon as a tri/quad */
725                         int mp_vi[4];
726                         float v1[3], v2[3], v3[3], v4[3];
727                         char string[MAX_PROPSTRING];
728                         int characters, i, glattrib = -1, badtex = 0;
729
730
731                         /* TEXFACE */
732                         ME_MTEXFACE_CPY(&tmp_tf, mtpoly);
733
734                         if (glsl) {
735                                 GPU_enable_material(matnr + 1, &gattribs);
736
737                                 for (i = 0; i < gattribs.totlayer; i++) {
738                                         if (gattribs.layer[i].type == CD_MTFACE) {
739                                                 glattrib = gattribs.layer[i].glindex;
740                                                 break;
741                                         }
742                                 }
743                         }
744                         else {
745                                 badtex = set_draw_settings_cached(0, &tmp_tf, mat, Gtexdraw);
746                                 if (badtex) {
747                                         continue;
748                                 }
749                         }
750
751                         mp_vi[0] = me->mloop[mp->loopstart + 0].v;
752                         mp_vi[1] = me->mloop[mp->loopstart + 1].v;
753                         mp_vi[2] = me->mloop[mp->loopstart + 2].v;
754                         mp_vi[3] = (mp->totloop >= 4) ? me->mloop[mp->loopstart + 3].v : 0;
755
756                         /* UV */
757                         luv = &mloopuv[mp->loopstart];
758                         copy_v2_v2(tmp_tf.uv[0], luv->uv); luv++;
759                         copy_v2_v2(tmp_tf.uv[1], luv->uv); luv++;
760                         copy_v2_v2(tmp_tf.uv[2], luv->uv); luv++;
761                         if (mp->totloop >= 4) {
762                                 copy_v2_v2(tmp_tf.uv[3], luv->uv);
763                         }
764
765                         /* COLOR */
766                         if (mloopcol) {
767                                 unsigned int totloop_clamp = min_ii(4, mp->totloop);
768                                 unsigned int j;
769                                 lcol = &mloopcol[mp->loopstart];
770
771                                 for (j = 0; j < totloop_clamp; j++, lcol++) {
772                                         MESH_MLOOPCOL_TO_MCOL(lcol, &tmp_mcol[j]);
773                                 }
774                         }
775
776                         /* LOCATION */
777                         ddm->getVertCo(ddm, mp_vi[0], v1);
778                         ddm->getVertCo(ddm, mp_vi[1], v2);
779                         ddm->getVertCo(ddm, mp_vi[2], v3);
780                         if (mp->totloop >= 4) {
781                                 ddm->getVertCo(ddm, mp_vi[3], v4);
782                         }
783
784
785
786                         /* The BM_FONT handling is in the gpu module, shared with the
787                          * game engine, was duplicated previously */
788
789                         BKE_bproperty_set_valstr(prop, string);
790                         characters = strlen(string);
791                         
792                         if (!BKE_image_has_ibuf(mtpoly->tpage, NULL))
793                                 characters = 0;
794
795                         if (!mf_smooth) {
796                                 float nor[3];
797
798                                 normal_tri_v3(nor, v1, v2, v3);
799
800                                 glNormal3fv(nor);
801                         }
802
803                         GPU_render_text(&tmp_tf, mode, string, characters,
804                                         (unsigned int *)tmp_mcol_pt, v1, v2, v3, (mp->totloop >= 4 ? v4 : NULL), glattrib);
805                 }
806         }
807
808         ddm->release(ddm);
809 }
810
811 static int compareDrawOptions(void *userData, int cur_index, int next_index)
812 {
813         drawTFace_userData *data = userData;
814
815         if (data->mf && data->mf[cur_index].mat_nr != data->mf[next_index].mat_nr)
816                 return 0;
817
818         if (data->tf && data->tf[cur_index].tpage != data->tf[next_index].tpage)
819                 return 0;
820
821         return 1;
822 }
823
824
825 static int compareDrawOptionsEm(void *userData, int cur_index, int next_index)
826 {
827         drawEMTFMapped_userData *data = userData;
828
829         if (data->mf && data->mf[cur_index].mat_nr != data->mf[next_index].mat_nr)
830                 return 0;
831
832         if (data->tf && data->tf[cur_index].tpage != data->tf[next_index].tpage)
833                 return 0;
834
835         return 1;
836 }
837
838 static void draw_mesh_textured_old(Scene *scene, View3D *v3d, RegionView3D *rv3d,
839                                    Object *ob, DerivedMesh *dm, const int draw_flags)
840 {
841         Mesh *me = ob->data;
842         DMDrawFlag uvflag = DM_DRAW_USE_ACTIVE_UV;
843
844         /* correct for negative scale */
845         if (ob->transflag & OB_NEG_SCALE) glFrontFace(GL_CW);
846         else glFrontFace(GL_CCW);
847         
848         /* draw the textured mesh */
849         draw_textured_begin(scene, v3d, rv3d, ob);
850
851         glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
852
853         if (ob->mode & OB_MODE_TEXTURE_PAINT) {
854                 uvflag = DM_DRAW_USE_TEXPAINT_UV;
855         }
856
857         if (ob->mode & OB_MODE_EDIT) {
858                 drawEMTFMapped_userData data;
859
860                 data.em = me->edit_btmesh;
861                 data.has_mcol = CustomData_has_layer(&me->edit_btmesh->bm->ldata, CD_MLOOPCOL);
862                 data.has_mtface = CustomData_has_layer(&me->edit_btmesh->bm->pdata, CD_MTEXPOLY);
863                 data.mf = DM_get_tessface_data_layer(dm, CD_MFACE);
864                 data.tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
865
866                 dm->drawMappedFacesTex(dm, draw_em_tf_mapped__set_draw, compareDrawOptionsEm, &data, 0);
867         }
868         else if (draw_flags & DRAW_FACE_SELECT) {
869                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
870                         dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions_facemask, GPU_enable_material, NULL, me,
871                                             DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
872                 else {
873                         drawTFace_userData userData;
874
875                         userData.mf = DM_get_tessface_data_layer(dm, CD_MFACE);
876                         userData.tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
877                         userData.me = me;
878                         dm->drawMappedFacesTex(dm, me->mpoly ? draw_tface_mapped__set_draw : NULL, compareDrawOptions, &userData, uvflag);
879                 }
880         }
881         else {
882                 if (GPU_buffer_legacy(dm)) {
883                         if (draw_flags & DRAW_MODIFIERS_PREVIEW)
884                                 dm->drawFacesTex(dm, draw_mcol__set_draw_legacy, NULL, NULL, uvflag);
885                         else 
886                                 dm->drawFacesTex(dm, draw_tface__set_draw_legacy, NULL, NULL, uvflag);
887                 }
888                 else {
889                         drawTFace_userData userData;
890
891                         update_tface_color_layer(dm);
892
893                         userData.mf = DM_get_tessface_data_layer(dm, CD_MFACE);
894                         userData.tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
895                         userData.me = NULL;
896
897                         dm->drawFacesTex(dm, draw_tface__set_draw, compareDrawOptions, &userData, uvflag);
898                 }
899         }
900
901         /* draw game engine text hack */
902         if (BKE_bproperty_object_get(ob, "Text"))
903                 draw_mesh_text(scene, ob, 0);
904
905         draw_textured_end();
906         
907         /* draw edges and selected faces over textured mesh */
908         if (!(ob == scene->obedit) && (draw_flags & DRAW_FACE_SELECT))
909                 draw_mesh_face_select(rv3d, me, dm);
910
911         /* reset from negative scale correction */
912         glFrontFace(GL_CCW);
913         
914         /* in editmode, the blend mode needs to be set in case it was ADD */
915         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
916 }
917
918 /************************** NEW SHADING NODES ********************************/
919
920 typedef struct TexMatCallback {
921         Scene *scene;
922         Object *ob;
923         Mesh *me;
924         DerivedMesh *dm;
925 } TexMatCallback;
926
927 static void tex_mat_set_material_cb(void *UNUSED(userData), int mat_nr, void *attribs)
928 {
929         /* all we have to do here is simply enable the GLSL material, but note
930          * that the GLSL code will give different result depending on the drawtype,
931          * in texture draw mode it will output the active texture node, in material
932          * draw mode it will show the full material. */
933         GPU_enable_material(mat_nr, attribs);
934 }
935
936 static void tex_mat_set_texture_cb(void *userData, int mat_nr, void *attribs)
937 {
938         /* texture draw mode without GLSL */
939         TexMatCallback *data = (TexMatCallback *)userData;
940         GPUVertexAttribs *gattribs = attribs;
941         Image *ima;
942         ImageUser *iuser;
943         bNode *node;
944         int texture_set = 0;
945
946         /* draw image texture if we find one */
947         if (ED_object_get_active_image(data->ob, mat_nr, &ima, &iuser, &node, NULL)) {
948                 /* get openl texture */
949                 int mipmap = 1;
950                 int bindcode = (ima) ? GPU_verify_image(ima, iuser, 0, 0, mipmap, false) : 0;
951                 float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
952
953                 if (bindcode) {
954                         NodeTexBase *texbase = node->storage;
955
956                         /* disable existing material */
957                         GPU_disable_material();
958                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero);
959                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
960                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0);
961
962                         /* bind texture */
963                         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
964                         glEnable(GL_COLOR_MATERIAL);
965                         glEnable(GL_TEXTURE_2D);
966
967                         glBindTexture(GL_TEXTURE_2D, ima->bindcode);
968                         glColor3f(1.0f, 1.0f, 1.0f);
969
970                         glMatrixMode(GL_TEXTURE);
971                         glLoadMatrixf(texbase->tex_mapping.mat);
972                         glMatrixMode(GL_MODELVIEW);
973
974                         /* use active UV texture layer */
975                         memset(gattribs, 0, sizeof(*gattribs));
976
977                         gattribs->layer[0].type = CD_MTFACE;
978                         gattribs->layer[0].name[0] = '\0';
979                         gattribs->layer[0].gltexco = 1;
980                         gattribs->totlayer = 1;
981
982                         texture_set = 1;
983                 }
984         }
985
986         if (!texture_set) {
987                 glMatrixMode(GL_TEXTURE);
988                 glLoadIdentity();
989                 glMatrixMode(GL_MODELVIEW);
990
991                 /* disable texture */
992                 glDisable(GL_TEXTURE_2D);
993                 glDisable(GL_COLOR_MATERIAL);
994
995                 /* draw single color */
996                 GPU_enable_material(mat_nr, attribs);
997         }
998 }
999
1000 static bool tex_mat_set_face_mesh_cb(void *userData, int index)
1001 {
1002         /* faceselect mode face hiding */
1003         TexMatCallback *data = (TexMatCallback *)userData;
1004         Mesh *me = (Mesh *)data->me;
1005         MPoly *mp = &me->mpoly[index];
1006
1007         return !(mp->flag & ME_HIDE);
1008 }
1009
1010 static bool tex_mat_set_face_editmesh_cb(void *userData, int index)
1011 {
1012         /* editmode face hiding */
1013         TexMatCallback *data = (TexMatCallback *)userData;
1014         Mesh *me = (Mesh *)data->me;
1015         BMEditMesh *em = me->edit_btmesh;
1016         BMFace *efa;
1017
1018         if (UNLIKELY(index >= em->bm->totface))
1019                 return DM_DRAW_OPTION_NORMAL;
1020
1021         efa = BM_face_at_index(em->bm, index);
1022
1023         return !BM_elem_flag_test(efa, BM_ELEM_HIDDEN);
1024 }
1025
1026 void draw_mesh_textured(Scene *scene, View3D *v3d, RegionView3D *rv3d,
1027                         Object *ob, DerivedMesh *dm, const int draw_flags)
1028 {
1029         /* if not cycles, or preview-modifiers, or drawing matcaps */
1030         if ((draw_flags & DRAW_MODIFIERS_PREVIEW) ||
1031             (v3d->flag2 & V3D_SHOW_SOLID_MATCAP) ||
1032             (BKE_scene_use_new_shading_nodes(scene) == false) ||
1033             ((ob->mode & OB_MODE_TEXTURE_PAINT) && ELEM(v3d->drawtype, OB_TEXTURE, OB_SOLID)))
1034         {
1035                 draw_mesh_textured_old(scene, v3d, rv3d, ob, dm, draw_flags);
1036                 return;
1037         }
1038         else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT)) {
1039                 draw_mesh_paint(v3d, rv3d, ob, dm, draw_flags);
1040                 return;
1041         }
1042
1043         /* set opengl state for negative scale & color */
1044         if (ob->transflag & OB_NEG_SCALE) glFrontFace(GL_CW);
1045         else glFrontFace(GL_CCW);
1046
1047         if ((v3d->drawtype == OB_TEXTURE) && (v3d->flag2 & V3D_SHADELESS_TEX))
1048                 glColor3f(1.0f, 1.0f, 1.0f);
1049         else
1050                 glEnable(GL_LIGHTING);
1051
1052         {
1053                 Mesh *me = ob->data;
1054                 TexMatCallback data = {scene, ob, me, dm};
1055                 bool (*set_face_cb)(void *, int);
1056                 int glsl, picking = (G.f & G_PICKSEL);
1057                 
1058                 /* face hiding callback depending on mode */
1059                 if (ob == scene->obedit)
1060                         set_face_cb = tex_mat_set_face_editmesh_cb;
1061                 else if (draw_flags & DRAW_FACE_SELECT)
1062                         set_face_cb = tex_mat_set_face_mesh_cb;
1063                 else
1064                         set_face_cb = NULL;
1065
1066                 /* test if we can use glsl */
1067                 glsl = (v3d->drawtype == OB_MATERIAL) && GPU_glsl_support() && !picking;
1068
1069                 GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl, NULL);
1070
1071                 if (glsl || picking) {
1072                         /* draw glsl or solid */
1073                         dm->drawMappedFacesMat(dm,
1074                                                tex_mat_set_material_cb,
1075                                                set_face_cb, &data);
1076                 }
1077                 else {
1078                         float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1079
1080                         /* draw textured */
1081                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero);
1082                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
1083                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0);
1084
1085                         dm->drawMappedFacesMat(dm,
1086                                                tex_mat_set_texture_cb,
1087                                                set_face_cb, &data);
1088                 }
1089
1090                 GPU_end_object_materials();
1091         }
1092
1093         /* reset opengl state */
1094         glDisable(GL_COLOR_MATERIAL);
1095         glDisable(GL_TEXTURE_2D);
1096         glDisable(GL_LIGHTING);
1097         glBindTexture(GL_TEXTURE_2D, 0);
1098         glFrontFace(GL_CCW);
1099
1100         glMatrixMode(GL_TEXTURE);
1101         glLoadIdentity();
1102         glMatrixMode(GL_MODELVIEW);
1103
1104         /* faceselect mode drawing over textured mesh */
1105         if (!(ob == scene->obedit) && (draw_flags & DRAW_FACE_SELECT))
1106                 draw_mesh_face_select(rv3d, ob->data, dm);
1107 }
1108
1109 /* Vertex Paint and Weight Paint */
1110 static void draw_mesh_paint_light_begin(void)
1111 {
1112         const float spec[4] = {0.47f, 0.47f, 0.47f, 0.47f};
1113
1114         GPU_enable_material(0, NULL);
1115
1116         /* but set default spec */
1117         glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
1118         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
1119
1120         /* diffuse */
1121         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
1122         glEnable(GL_LIGHTING);
1123         glEnable(GL_COLOR_MATERIAL);
1124 }
1125 static void draw_mesh_paint_light_end(void)
1126 {
1127         glDisable(GL_COLOR_MATERIAL);
1128         glDisable(GL_LIGHTING);
1129
1130         GPU_disable_material();
1131 }
1132
1133 void draw_mesh_paint_weight_faces(DerivedMesh *dm, const bool use_light,
1134                                   void *facemask_cb, void *user_data)
1135 {
1136         if (use_light) {
1137                 draw_mesh_paint_light_begin();
1138         }
1139
1140         dm->drawMappedFaces(dm, (DMSetDrawOptions)facemask_cb, GPU_enable_material, NULL, user_data,
1141                             DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
1142
1143         if (use_light) {
1144                 draw_mesh_paint_light_end();
1145         }
1146 }
1147
1148 void draw_mesh_paint_vcolor_faces(DerivedMesh *dm, const bool use_light,
1149                                   void *facemask_cb, void *user_data,
1150                                   const Mesh *me)
1151 {
1152         if (use_light) {
1153                 draw_mesh_paint_light_begin();
1154         }
1155
1156         if (me->mloopcol) {
1157                 dm->drawMappedFaces(dm, facemask_cb, GPU_enable_material, NULL, user_data,
1158                                     DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
1159         }
1160         else {
1161                 glColor3f(1.0f, 1.0f, 1.0f);
1162                 dm->drawMappedFaces(dm, facemask_cb, GPU_enable_material, NULL, user_data,
1163                                     DM_DRAW_ALWAYS_SMOOTH);
1164         }
1165
1166         if (use_light) {
1167                 draw_mesh_paint_light_end();
1168         }
1169 }
1170
1171 void draw_mesh_paint_weight_edges(RegionView3D *rv3d, DerivedMesh *dm,
1172                                   const bool use_depth, const bool use_alpha,
1173                                   void *edgemask_cb, void *user_data)
1174 {
1175         /* weight paint in solid mode, special case. focus on making the weights clear
1176          * rather than the shading, this is also forced in wire view */
1177
1178         if (use_depth) {
1179                 ED_view3d_polygon_offset(rv3d, 1.0);
1180                 glDepthMask(0);  /* disable write in zbuffer, selected edge wires show better */
1181         }
1182         else {
1183                 glDisable(GL_DEPTH_TEST);
1184         }
1185
1186         if (use_alpha) {
1187                 glEnable(GL_BLEND);
1188         }
1189
1190         glColor4ub(255, 255, 255, 96);
1191         glEnable(GL_LINE_STIPPLE);
1192         glLineStipple(1, 0xAAAA);
1193
1194         dm->drawMappedEdges(dm, (DMSetDrawOptions)edgemask_cb, user_data);
1195
1196         if (use_depth) {
1197                 ED_view3d_polygon_offset(rv3d, 0.0);
1198                 glDepthMask(1);
1199         }
1200         else {
1201                 glEnable(GL_DEPTH_TEST);
1202         }
1203
1204         glDisable(GL_LINE_STIPPLE);
1205
1206         if (use_alpha) {
1207                 glDisable(GL_BLEND);
1208         }
1209 }
1210
1211 void draw_mesh_paint(View3D *v3d, RegionView3D *rv3d,
1212                      Object *ob, DerivedMesh *dm, const int draw_flags)
1213 {
1214         DMSetDrawOptions facemask = NULL;
1215         Mesh *me = ob->data;
1216         const bool use_light = (v3d->drawtype >= OB_SOLID);
1217
1218         /* hide faces in face select mode */
1219         if (me->editflag & (ME_EDIT_PAINT_VERT_SEL | ME_EDIT_PAINT_FACE_SEL))
1220                 facemask = wpaint__setSolidDrawOptions_facemask;
1221
1222         if (ob->mode & OB_MODE_WEIGHT_PAINT) {
1223                 draw_mesh_paint_weight_faces(dm, use_light, facemask, me);
1224         }
1225         else if (ob->mode & OB_MODE_VERTEX_PAINT) {
1226                 draw_mesh_paint_vcolor_faces(dm, use_light, facemask, me, me);
1227         }
1228
1229         /* draw face selection on top */
1230         if (draw_flags & DRAW_FACE_SELECT) {
1231                 draw_mesh_face_select(rv3d, me, dm);
1232         }
1233         else if ((use_light == false) || (ob->dtx & OB_DRAWWIRE)) {
1234                 const bool use_depth = (v3d->flag & V3D_ZBUF_SELECT) || !(ob->mode & OB_MODE_WEIGHT_PAINT);
1235                 const bool use_alpha = (ob->mode & OB_MODE_VERTEX_PAINT) == 0;
1236
1237                 if (use_alpha == false) {
1238                         set_inverted_drawing(1);
1239                 }
1240
1241                 draw_mesh_paint_weight_edges(rv3d, dm, use_depth, use_alpha, NULL, NULL);
1242
1243                 if (use_alpha == false) {
1244                         set_inverted_drawing(0);
1245                 }
1246         }
1247 }
1248