89babf977e15482d3f376e4e736747f998f67a19
[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                                 }
307                                 else {
308                                         c_badtex = true;
309                                         GPU_clear_tpage(true);
310                                         glDisable(GL_TEXTURE_2D);
311                                         glBindTexture(GL_TEXTURE_2D, 0);
312                                 }
313                         }
314                         else {
315                                 c_badtex = !GPU_set_tpage(texface, !texpaint, alphablend);
316                         }
317                 }
318                 else {
319                         GPU_set_tpage(NULL, 0, 0);
320                         c_badtex = false;
321                 }
322                 c_textured = textured;
323                 c_has_texface = has_texface;
324                 if (texface)
325                         memcpy(&c_texface, texface, sizeof(c_texface));
326         }
327
328         if (c_badtex) lit = 0;
329         if (lit != c_lit || ma != c_ma) {
330                 if (lit) {
331                         float spec[4];
332                         if (!ma) ma = give_current_material_or_def(NULL, 0);  /* default material */
333
334                         spec[0] = ma->spec * ma->specr;
335                         spec[1] = ma->spec * ma->specg;
336                         spec[2] = ma->spec * ma->specb;
337                         spec[3] = 1.0;
338
339                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
340                         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
341                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, CLAMPIS(ma->har, 0, 128));
342                         glEnable(GL_LIGHTING);
343                         glEnable(GL_COLOR_MATERIAL);
344                 }
345                 else {
346                         glDisable(GL_LIGHTING); 
347                         glDisable(GL_COLOR_MATERIAL);
348                 }
349                 c_lit = lit;
350                 c_ma = ma;
351         }
352
353         return c_badtex;
354 }
355
356 static void draw_textured_begin(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
357 {
358         unsigned char obcol[4];
359         bool is_tex, solidtex;
360         Mesh *me = ob->data;
361         ImagePaintSettings *imapaint = &scene->toolsettings->imapaint;
362
363         /* XXX scene->obedit warning */
364
365         /* texture draw is abused for mask selection mode, do this so wire draw
366          * with face selection in weight paint is not lit. */
367         if ((v3d->drawtype <= OB_WIRE) && (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))) {
368                 solidtex = false;
369                 Gtexdraw.is_lit = 0;
370         }
371         else if (v3d->drawtype == OB_SOLID || ((ob->mode & OB_MODE_EDIT) && v3d->drawtype != OB_TEXTURE)) {
372                 /* draw with default lights in solid draw mode and edit mode */
373                 solidtex = true;
374                 Gtexdraw.is_lit = -1;
375         }
376         else {
377                 /* draw with lights in the scene otherwise */
378                 solidtex = false;
379                 if (v3d->flag2 & V3D_SHADELESS_TEX)
380                         Gtexdraw.is_lit = 0;
381                 else
382                         Gtexdraw.is_lit = GPU_scene_object_lights(scene, ob, v3d->lay, rv3d->viewmat, !rv3d->is_persp);
383         }
384         
385         rgba_float_to_uchar(obcol, ob->col);
386
387         if (solidtex || v3d->drawtype == OB_TEXTURE) is_tex = true;
388         else is_tex = false;
389
390         Gtexdraw.ob = ob;
391         Gtexdraw.stencil = (imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL) ? imapaint->stencil : NULL;
392         Gtexdraw.stencil_invert = ((imapaint->flag & IMAGEPAINT_PROJECT_LAYER_STENCIL_INV) != 0);
393         Gtexdraw.is_texpaint = (ob->mode == OB_MODE_TEXTURE_PAINT);
394         copy_v3_v3(Gtexdraw.stencil_col, imapaint->stencil_col);
395         Gtexdraw.is_tex = is_tex;
396
397         /* load the stencil texture here */
398         if (Gtexdraw.is_texpaint && (Gtexdraw.stencil != NULL)) {
399                 glActiveTexture(GL_TEXTURE1);
400                 if (GPU_verify_image(Gtexdraw.stencil, NULL, false, false, false, false)) {
401                         glEnable(GL_TEXTURE_2D);
402                         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
403                         glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
404                         glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
405                         glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_TEXTURE);
406                         glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
407                         glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);
408                         glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Gtexdraw.stencil_col);
409                         if (!Gtexdraw.stencil_invert) {
410                                 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_ONE_MINUS_SRC_COLOR);
411                         }
412                         else {
413                                 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_COLOR);
414                         }
415                 }
416                 glActiveTexture(GL_TEXTURE0);
417         }
418
419         Gtexdraw.color_profile = BKE_scene_check_color_management_enabled(scene);
420         Gtexdraw.use_game_mat = (RE_engines_find(scene->r.engine)->flag & RE_GAME) != 0;
421         Gtexdraw.use_backface_culling = (v3d->flag2 & V3D_BACKFACE_CULLING) != 0;
422
423         memcpy(Gtexdraw.obcol, obcol, sizeof(obcol));
424         set_draw_settings_cached(1, NULL, NULL, Gtexdraw);
425         glShadeModel(GL_SMOOTH);
426         glCullFace(GL_BACK);
427         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, (me->flag & ME_TWOSIDED) ? GL_TRUE : GL_FALSE);
428 }
429
430 static void draw_textured_end(void)
431 {
432         if (Gtexdraw.ob->mode & OB_MODE_TEXTURE_PAINT) {
433                 if (Gtexdraw.stencil != NULL) {
434                         glActiveTexture(GL_TEXTURE1);
435                         glDisable(GL_TEXTURE_2D);
436                         glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_COLOR);
437                         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
438                         glBindTexture(GL_TEXTURE_2D, 0);
439                         glActiveTexture(GL_TEXTURE0);
440                 }
441                 /* manual reset, since we don't use tpage */
442                 glBindTexture(GL_TEXTURE_2D, 0);
443                 /* force switch off textures */
444                 GPU_clear_tpage(true);
445         }
446         else {
447                 /* switch off textures */
448                 GPU_set_tpage(NULL, 0, 0);
449         }
450
451         glShadeModel(GL_FLAT);
452         glDisable(GL_CULL_FACE);
453         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
454
455         /* XXX, bad patch - GPU_default_lights() calls
456          * glLightfv(GL_POSITION, ...) which
457          * is transformed by the current matrix... we
458          * need to make sure that matrix is identity.
459          * 
460          * It would be better if drawmesh.c kept track
461          * of and restored the light settings it changed.
462          *  - zr
463          */
464         glPushMatrix();
465         glLoadIdentity();
466         GPU_default_lights();
467         glPopMatrix();
468 }
469
470 static DMDrawOption draw_tface__set_draw_legacy(MTFace *tface, const bool has_mcol, int matnr)
471 {
472         Material *ma = give_current_material(Gtexdraw.ob, matnr + 1);
473         bool invalidtexture = false;
474
475         if (ma && (ma->game.flag & GEMAT_INVISIBLE))
476                 return DM_DRAW_OPTION_SKIP;
477
478         invalidtexture = set_draw_settings_cached(0, tface, ma, Gtexdraw);
479
480         if (tface && invalidtexture) {
481                 glColor3ub(0xFF, 0x00, 0xFF);
482                 return DM_DRAW_OPTION_NO_MCOL; /* Don't set color */
483         }
484         else if (ma && (ma->shade_flag & MA_OBCOLOR)) {
485                 glColor3ubv(Gtexdraw.obcol);
486                 return DM_DRAW_OPTION_NO_MCOL; /* Don't set color */
487         }
488         else if (!has_mcol) {
489                 if (tface) {
490                         glColor3f(1.0, 1.0, 1.0);
491                 }
492                 else {
493                         if (ma) {
494                                 float col[3];
495                                 if (Gtexdraw.color_profile) linearrgb_to_srgb_v3_v3(col, &ma->r);
496                                 else copy_v3_v3(col, &ma->r);
497                                 
498                                 glColor3fv(col);
499                         }
500                         else {
501                                 glColor3f(1.0, 1.0, 1.0);
502                         }
503                 }
504                 return DM_DRAW_OPTION_NO_MCOL; /* Don't set color */
505         }
506         else {
507                 return DM_DRAW_OPTION_NORMAL; /* Set color from mcol */
508         }
509 }
510
511 static DMDrawOption draw_mcol__set_draw_legacy(MTFace *UNUSED(tface), const bool has_mcol, int UNUSED(matnr))
512 {
513         if (has_mcol)
514                 return DM_DRAW_OPTION_NORMAL;
515         else
516                 return DM_DRAW_OPTION_NO_MCOL;
517 }
518
519 static DMDrawOption draw_tface__set_draw(MTFace *tface, const bool UNUSED(has_mcol), int matnr)
520 {
521         Material *ma = give_current_material(Gtexdraw.ob, matnr + 1);
522
523         if (ma && (ma->game.flag & GEMAT_INVISIBLE)) return 0;
524
525         if (tface || Gtexdraw.is_texpaint)
526                 set_draw_settings_cached(0, tface, ma, Gtexdraw);
527
528         /* always use color from mcol, as set in update_tface_color_layer */
529         return DM_DRAW_OPTION_NORMAL;
530 }
531
532 static void update_tface_color_layer(DerivedMesh *dm)
533 {
534         MTFace *tface = DM_get_tessface_data_layer(dm, CD_MTFACE);
535         MFace *mface = dm->getTessFaceArray(dm);
536         MCol *finalCol;
537         int i, j;
538         MCol *mcol = dm->getTessFaceDataArray(dm, CD_PREVIEW_MCOL);
539         if (!mcol)
540                 mcol = dm->getTessFaceDataArray(dm, CD_MCOL);
541
542         if (CustomData_has_layer(&dm->faceData, CD_TEXTURE_MCOL)) {
543                 finalCol = CustomData_get_layer(&dm->faceData, CD_TEXTURE_MCOL);
544         }
545         else {
546                 finalCol = MEM_mallocN(sizeof(MCol) * 4 * dm->getNumTessFaces(dm), "add_tface_color_layer");
547
548                 CustomData_add_layer(&dm->faceData, CD_TEXTURE_MCOL, CD_ASSIGN, finalCol, dm->numTessFaceData);
549         }
550
551         for (i = 0; i < dm->getNumTessFaces(dm); i++) {
552                 Material *ma = give_current_material(Gtexdraw.ob, mface[i].mat_nr + 1);
553
554                 if (ma && (ma->game.flag & GEMAT_INVISIBLE)) {
555                         if (mcol)
556                                 memcpy(&finalCol[i * 4], &mcol[i * 4], sizeof(MCol) * 4);
557                         else
558                                 for (j = 0; j < 4; j++) {
559                                         finalCol[i * 4 + j].b = 255;
560                                         finalCol[i * 4 + j].g = 255;
561                                         finalCol[i * 4 + j].r = 255;
562                                 }
563                 }
564                 else if (tface && set_draw_settings_cached(0, tface, ma, Gtexdraw)) {
565                         for (j = 0; j < 4; j++) {
566                                 finalCol[i * 4 + j].b = 255;
567                                 finalCol[i * 4 + j].g = 0;
568                                 finalCol[i * 4 + j].r = 255;
569                         }
570                 }
571                 else if (ma && (ma->shade_flag & MA_OBCOLOR)) {
572                         for (j = 0; j < 4; j++) {
573                                 finalCol[i * 4 + j].b = Gtexdraw.obcol[0];
574                                 finalCol[i * 4 + j].g = Gtexdraw.obcol[1];
575                                 finalCol[i * 4 + j].r = Gtexdraw.obcol[2];
576                         }
577                 }
578                 else if (!mcol) {
579                         if (tface) {
580                                 for (j = 0; j < 4; j++) {
581                                         finalCol[i * 4 + j].b = 255;
582                                         finalCol[i * 4 + j].g = 255;
583                                         finalCol[i * 4 + j].r = 255;
584                                 }
585                         }
586                         else {
587                                 float col[3];
588
589                                 if (ma) {
590                                         if (Gtexdraw.color_profile) linearrgb_to_srgb_v3_v3(col, &ma->r);
591                                         else copy_v3_v3(col, &ma->r);
592                                         
593                                         for (j = 0; j < 4; j++) {
594                                                 finalCol[i * 4 + j].b = FTOCHAR(col[0]);
595                                                 finalCol[i * 4 + j].g = FTOCHAR(col[1]);
596                                                 finalCol[i * 4 + j].r = FTOCHAR(col[2]);
597                                         }
598                                 }
599                                 else
600                                         for (j = 0; j < 4; j++) {
601                                                 finalCol[i * 4 + j].b = 255;
602                                                 finalCol[i * 4 + j].g = 255;
603                                                 finalCol[i * 4 + j].r = 255;
604                                         }
605                         }
606                 }
607                 else {
608                         for (j = 0; j < 4; j++) {
609                                 finalCol[i * 4 + j].r = mcol[i * 4 + j].r;
610                                 finalCol[i * 4 + j].g = mcol[i * 4 + j].g;
611                                 finalCol[i * 4 + j].b = mcol[i * 4 + j].b;
612                         }
613                 }
614         }
615 }
616
617 static DMDrawOption draw_tface_mapped__set_draw(void *userData, int index)
618 {
619         Mesh *me = ((drawTFace_userData *)userData)->me;
620
621         /* array checked for NULL before calling */
622         MPoly *mpoly = &me->mpoly[index];
623
624         BLI_assert(index >= 0 && index < me->totpoly);
625
626         if (mpoly->flag & ME_HIDE) {
627                 return DM_DRAW_OPTION_SKIP;
628         }
629         else {
630                 MTexPoly *tpoly = (me->mtpoly) ? &me->mtpoly[index] : NULL;
631                 MTFace mtf = {{{0}}};
632                 int matnr = mpoly->mat_nr;
633
634                 if (tpoly) {
635                         ME_MTEXFACE_CPY(&mtf, tpoly);
636                 }
637
638                 return draw_tface__set_draw(&mtf, (me->mloopcol != NULL), matnr);
639         }
640 }
641
642 static DMDrawOption draw_em_tf_mapped__set_draw(void *userData, int index)
643 {
644         drawEMTFMapped_userData *data = userData;
645         BMEditMesh *em = data->em;
646         BMFace *efa;
647
648         if (UNLIKELY(index >= em->bm->totface))
649                 return DM_DRAW_OPTION_NORMAL;
650
651         efa = BM_face_at_index(em->bm, index);
652
653         if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
654                 return DM_DRAW_OPTION_SKIP;
655         }
656         else {
657                 MTFace mtf = {{{0}}};
658                 int matnr = efa->mat_nr;
659
660                 if (data->has_mtface) {
661                         MTexPoly *tpoly = CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_MTEXPOLY);
662                         ME_MTEXFACE_CPY(&mtf, tpoly);
663                 }
664
665                 return draw_tface__set_draw_legacy(data->has_mtface ? &mtf : NULL,
666                                                    data->has_mcol, matnr);
667         }
668 }
669
670 /* when face select is on, use face hidden flag */
671 static DMDrawOption wpaint__setSolidDrawOptions_facemask(void *userData, int index)
672 {
673         Mesh *me = (Mesh *)userData;
674         MPoly *mp = &me->mpoly[index];
675         if (mp->flag & ME_HIDE)
676                 return DM_DRAW_OPTION_SKIP;
677         return DM_DRAW_OPTION_NORMAL;
678 }
679
680 static void draw_mesh_text(Scene *scene, Object *ob, int glsl)
681 {
682         Mesh *me = ob->data;
683         DerivedMesh *ddm;
684         MPoly *mp, *mface  = me->mpoly;
685         MTexPoly *mtpoly   = me->mtpoly;
686         MLoopUV *mloopuv   = me->mloopuv;
687         MLoopUV *luv;
688         MLoopCol *mloopcol = me->mloopcol;  /* why does mcol exist? */
689         MLoopCol *lcol;
690
691         bProperty *prop = BKE_bproperty_object_get(ob, "Text");
692         GPUVertexAttribs gattribs;
693         int a, totpoly = me->totpoly;
694
695         /* fake values to pass to GPU_render_text() */
696         MCol  tmp_mcol[4]  = {{0}};
697         MCol *tmp_mcol_pt  = mloopcol ? tmp_mcol : NULL;
698         MTFace tmp_tf      = {{{0}}};
699
700         /* don't draw without tfaces */
701         if (!mtpoly || !mloopuv)
702                 return;
703
704         /* don't draw when editing */
705         if (ob->mode & OB_MODE_EDIT)
706                 return;
707         else if (ob == OBACT)
708                 if (BKE_paint_select_elem_test(ob))
709                         return;
710
711         ddm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
712
713         for (a = 0, mp = mface; a < totpoly; a++, mtpoly++, mp++) {
714                 short matnr = mp->mat_nr;
715                 int mf_smooth = mp->flag & ME_SMOOTH;
716                 Material *mat = (me->mat) ? me->mat[matnr] : NULL;
717                 int mode = mat ? mat->game.flag : GEMAT_INVISIBLE;
718
719
720                 if (!(mode & GEMAT_INVISIBLE) && (mode & GEMAT_TEXT) && mp->totloop >= 3) {
721                         /* get the polygon as a tri/quad */
722                         int mp_vi[4];
723                         float v1[3], v2[3], v3[3], v4[3];
724                         char string[MAX_PROPSTRING];
725                         int characters, i, glattrib = -1, badtex = 0;
726
727
728                         /* TEXFACE */
729                         ME_MTEXFACE_CPY(&tmp_tf, mtpoly);
730
731                         if (glsl) {
732                                 GPU_enable_material(matnr + 1, &gattribs);
733
734                                 for (i = 0; i < gattribs.totlayer; i++) {
735                                         if (gattribs.layer[i].type == CD_MTFACE) {
736                                                 glattrib = gattribs.layer[i].glindex;
737                                                 break;
738                                         }
739                                 }
740                         }
741                         else {
742                                 badtex = set_draw_settings_cached(0, &tmp_tf, mat, Gtexdraw);
743                                 if (badtex) {
744                                         continue;
745                                 }
746                         }
747
748                         mp_vi[0] = me->mloop[mp->loopstart + 0].v;
749                         mp_vi[1] = me->mloop[mp->loopstart + 1].v;
750                         mp_vi[2] = me->mloop[mp->loopstart + 2].v;
751                         mp_vi[3] = (mp->totloop >= 4) ? me->mloop[mp->loopstart + 3].v : 0;
752
753                         /* UV */
754                         luv = &mloopuv[mp->loopstart];
755                         copy_v2_v2(tmp_tf.uv[0], luv->uv); luv++;
756                         copy_v2_v2(tmp_tf.uv[1], luv->uv); luv++;
757                         copy_v2_v2(tmp_tf.uv[2], luv->uv); luv++;
758                         if (mp->totloop >= 4) {
759                                 copy_v2_v2(tmp_tf.uv[3], luv->uv);
760                         }
761
762                         /* COLOR */
763                         if (mloopcol) {
764                                 unsigned int totloop_clamp = min_ii(4, mp->totloop);
765                                 unsigned int j;
766                                 lcol = &mloopcol[mp->loopstart];
767
768                                 for (j = 0; j < totloop_clamp; j++, lcol++) {
769                                         MESH_MLOOPCOL_TO_MCOL(lcol, &tmp_mcol[j]);
770                                 }
771                         }
772
773                         /* LOCATION */
774                         ddm->getVertCo(ddm, mp_vi[0], v1);
775                         ddm->getVertCo(ddm, mp_vi[1], v2);
776                         ddm->getVertCo(ddm, mp_vi[2], v3);
777                         if (mp->totloop >= 4) {
778                                 ddm->getVertCo(ddm, mp_vi[3], v4);
779                         }
780
781
782
783                         /* The BM_FONT handling is in the gpu module, shared with the
784                          * game engine, was duplicated previously */
785
786                         BKE_bproperty_set_valstr(prop, string);
787                         characters = strlen(string);
788                         
789                         if (!BKE_image_has_ibuf(mtpoly->tpage, NULL))
790                                 characters = 0;
791
792                         if (!mf_smooth) {
793                                 float nor[3];
794
795                                 normal_tri_v3(nor, v1, v2, v3);
796
797                                 glNormal3fv(nor);
798                         }
799
800                         GPU_render_text(&tmp_tf, mode, string, characters,
801                                         (unsigned int *)tmp_mcol_pt, v1, v2, v3, (mp->totloop >= 4 ? v4 : NULL), glattrib);
802                 }
803         }
804
805         ddm->release(ddm);
806 }
807
808 static int compareDrawOptions(void *userData, int cur_index, int next_index)
809 {
810         drawTFace_userData *data = userData;
811
812         if (data->mf && data->mf[cur_index].mat_nr != data->mf[next_index].mat_nr)
813                 return 0;
814
815         if (data->tf && data->tf[cur_index].tpage != data->tf[next_index].tpage)
816                 return 0;
817
818         return 1;
819 }
820
821
822 static int compareDrawOptionsEm(void *userData, int cur_index, int next_index)
823 {
824         drawEMTFMapped_userData *data = userData;
825
826         if (data->mf && data->mf[cur_index].mat_nr != data->mf[next_index].mat_nr)
827                 return 0;
828
829         if (data->tf && data->tf[cur_index].tpage != data->tf[next_index].tpage)
830                 return 0;
831
832         return 1;
833 }
834
835 static void draw_mesh_textured_old(Scene *scene, View3D *v3d, RegionView3D *rv3d,
836                                    Object *ob, DerivedMesh *dm, const int draw_flags)
837 {
838         Mesh *me = ob->data;
839         DMDrawFlag uvflag = DM_DRAW_USE_ACTIVE_UV;
840
841         /* correct for negative scale */
842         if (ob->transflag & OB_NEG_SCALE) glFrontFace(GL_CW);
843         else glFrontFace(GL_CCW);
844         
845         /* draw the textured mesh */
846         draw_textured_begin(scene, v3d, rv3d, ob);
847
848         glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
849
850         if (ob->mode & OB_MODE_TEXTURE_PAINT) {
851                 uvflag = DM_DRAW_USE_TEXPAINT_UV;
852         }
853
854         if (ob->mode & OB_MODE_EDIT) {
855                 drawEMTFMapped_userData data;
856
857                 data.em = me->edit_btmesh;
858                 data.has_mcol = CustomData_has_layer(&me->edit_btmesh->bm->ldata, CD_MLOOPCOL);
859                 data.has_mtface = CustomData_has_layer(&me->edit_btmesh->bm->pdata, CD_MTEXPOLY);
860                 data.mf = DM_get_tessface_data_layer(dm, CD_MFACE);
861                 data.tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
862
863                 dm->drawMappedFacesTex(dm, draw_em_tf_mapped__set_draw, compareDrawOptionsEm, &data, 0);
864         }
865         else if (draw_flags & DRAW_FACE_SELECT) {
866                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
867                         dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions_facemask, GPU_enable_material, NULL, me,
868                                             DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
869                 else {
870                         drawTFace_userData userData;
871
872                         userData.mf = DM_get_tessface_data_layer(dm, CD_MFACE);
873                         userData.tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
874                         userData.me = me;
875                         dm->drawMappedFacesTex(dm, me->mpoly ? draw_tface_mapped__set_draw : NULL, compareDrawOptions, &userData, uvflag);
876                 }
877         }
878         else {
879                 if (GPU_buffer_legacy(dm)) {
880                         if (draw_flags & DRAW_MODIFIERS_PREVIEW)
881                                 dm->drawFacesTex(dm, draw_mcol__set_draw_legacy, NULL, NULL, uvflag);
882                         else 
883                                 dm->drawFacesTex(dm, draw_tface__set_draw_legacy, NULL, NULL, uvflag);
884                 }
885                 else {
886                         drawTFace_userData userData;
887
888                         update_tface_color_layer(dm);
889
890                         userData.mf = DM_get_tessface_data_layer(dm, CD_MFACE);
891                         userData.tf = DM_get_tessface_data_layer(dm, CD_MTFACE);
892                         userData.me = NULL;
893
894                         dm->drawFacesTex(dm, draw_tface__set_draw, compareDrawOptions, &userData, uvflag);
895                 }
896         }
897
898         /* draw game engine text hack */
899         if (BKE_bproperty_object_get(ob, "Text"))
900                 draw_mesh_text(scene, ob, 0);
901
902         draw_textured_end();
903         
904         /* draw edges and selected faces over textured mesh */
905         if (!(ob == scene->obedit) && (draw_flags & DRAW_FACE_SELECT))
906                 draw_mesh_face_select(rv3d, me, dm);
907
908         /* reset from negative scale correction */
909         glFrontFace(GL_CCW);
910         
911         /* in editmode, the blend mode needs to be set in case it was ADD */
912         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
913 }
914
915 /************************** NEW SHADING NODES ********************************/
916
917 typedef struct TexMatCallback {
918         Scene *scene;
919         Object *ob;
920         Mesh *me;
921         DerivedMesh *dm;
922 } TexMatCallback;
923
924 static void tex_mat_set_material_cb(void *UNUSED(userData), int mat_nr, void *attribs)
925 {
926         /* all we have to do here is simply enable the GLSL material, but note
927          * that the GLSL code will give different result depending on the drawtype,
928          * in texture draw mode it will output the active texture node, in material
929          * draw mode it will show the full material. */
930         GPU_enable_material(mat_nr, attribs);
931 }
932
933 static void tex_mat_set_texture_cb(void *userData, int mat_nr, void *attribs)
934 {
935         /* texture draw mode without GLSL */
936         TexMatCallback *data = (TexMatCallback *)userData;
937         GPUVertexAttribs *gattribs = attribs;
938         Image *ima;
939         ImageUser *iuser;
940         bNode *node;
941         int texture_set = 0;
942
943         /* draw image texture if we find one */
944         if (ED_object_get_active_image(data->ob, mat_nr, &ima, &iuser, &node, NULL)) {
945                 /* get openl texture */
946                 int mipmap = 1;
947                 int bindcode = (ima) ? GPU_verify_image(ima, iuser, 0, 0, mipmap, false) : 0;
948                 float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
949
950                 if (bindcode) {
951                         NodeTexBase *texbase = node->storage;
952
953                         /* disable existing material */
954                         GPU_disable_material();
955                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero);
956                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
957                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0);
958
959                         /* bind texture */
960                         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
961                         glEnable(GL_COLOR_MATERIAL);
962                         glEnable(GL_TEXTURE_2D);
963
964                         glBindTexture(GL_TEXTURE_2D, ima->bindcode);
965                         glColor3f(1.0f, 1.0f, 1.0f);
966
967                         glMatrixMode(GL_TEXTURE);
968                         glLoadMatrixf(texbase->tex_mapping.mat);
969                         glMatrixMode(GL_MODELVIEW);
970
971                         /* use active UV texture layer */
972                         memset(gattribs, 0, sizeof(*gattribs));
973
974                         gattribs->layer[0].type = CD_MTFACE;
975                         gattribs->layer[0].name[0] = '\0';
976                         gattribs->layer[0].gltexco = 1;
977                         gattribs->totlayer = 1;
978
979                         texture_set = 1;
980                 }
981         }
982
983         if (!texture_set) {
984                 glMatrixMode(GL_TEXTURE);
985                 glLoadIdentity();
986                 glMatrixMode(GL_MODELVIEW);
987
988                 /* disable texture */
989                 glDisable(GL_TEXTURE_2D);
990                 glDisable(GL_COLOR_MATERIAL);
991
992                 /* draw single color */
993                 GPU_enable_material(mat_nr, attribs);
994         }
995 }
996
997 static bool tex_mat_set_face_mesh_cb(void *userData, int index)
998 {
999         /* faceselect mode face hiding */
1000         TexMatCallback *data = (TexMatCallback *)userData;
1001         Mesh *me = (Mesh *)data->me;
1002         MPoly *mp = &me->mpoly[index];
1003
1004         return !(mp->flag & ME_HIDE);
1005 }
1006
1007 static bool tex_mat_set_face_editmesh_cb(void *userData, int index)
1008 {
1009         /* editmode face hiding */
1010         TexMatCallback *data = (TexMatCallback *)userData;
1011         Mesh *me = (Mesh *)data->me;
1012         BMEditMesh *em = me->edit_btmesh;
1013         BMFace *efa;
1014
1015         if (UNLIKELY(index >= em->bm->totface))
1016                 return DM_DRAW_OPTION_NORMAL;
1017
1018         efa = BM_face_at_index(em->bm, index);
1019
1020         return !BM_elem_flag_test(efa, BM_ELEM_HIDDEN);
1021 }
1022
1023 void draw_mesh_textured(Scene *scene, View3D *v3d, RegionView3D *rv3d,
1024                         Object *ob, DerivedMesh *dm, const int draw_flags)
1025 {
1026         /* if not cycles, or preview-modifiers, or drawing matcaps */
1027         if ((draw_flags & DRAW_MODIFIERS_PREVIEW) ||
1028             (v3d->flag2 & V3D_SHOW_SOLID_MATCAP) ||
1029             (BKE_scene_use_new_shading_nodes(scene) == false) ||
1030             ((ob->mode & OB_MODE_TEXTURE_PAINT) && ELEM(v3d->drawtype, OB_TEXTURE, OB_SOLID)))
1031         {
1032                 draw_mesh_textured_old(scene, v3d, rv3d, ob, dm, draw_flags);
1033                 return;
1034         }
1035         else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT)) {
1036                 draw_mesh_paint(v3d, rv3d, ob, dm, draw_flags);
1037                 return;
1038         }
1039
1040         /* set opengl state for negative scale & color */
1041         if (ob->transflag & OB_NEG_SCALE) glFrontFace(GL_CW);
1042         else glFrontFace(GL_CCW);
1043
1044         if ((v3d->drawtype == OB_TEXTURE) && (v3d->flag2 & V3D_SHADELESS_TEX))
1045                 glColor3f(1.0f, 1.0f, 1.0f);
1046         else
1047                 glEnable(GL_LIGHTING);
1048
1049         {
1050                 Mesh *me = ob->data;
1051                 TexMatCallback data = {scene, ob, me, dm};
1052                 bool (*set_face_cb)(void *, int);
1053                 int glsl, picking = (G.f & G_PICKSEL);
1054                 
1055                 /* face hiding callback depending on mode */
1056                 if (ob == scene->obedit)
1057                         set_face_cb = tex_mat_set_face_editmesh_cb;
1058                 else if (draw_flags & DRAW_FACE_SELECT)
1059                         set_face_cb = tex_mat_set_face_mesh_cb;
1060                 else
1061                         set_face_cb = NULL;
1062
1063                 /* test if we can use glsl */
1064                 glsl = (v3d->drawtype == OB_MATERIAL) && GPU_glsl_support() && !picking;
1065
1066                 GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl, NULL);
1067
1068                 if (glsl || picking) {
1069                         /* draw glsl or solid */
1070                         dm->drawMappedFacesMat(dm,
1071                                                tex_mat_set_material_cb,
1072                                                set_face_cb, &data);
1073                 }
1074                 else {
1075                         float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1076
1077                         /* draw textured */
1078                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero);
1079                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
1080                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0);
1081
1082                         dm->drawMappedFacesMat(dm,
1083                                                tex_mat_set_texture_cb,
1084                                                set_face_cb, &data);
1085                 }
1086
1087                 GPU_end_object_materials();
1088         }
1089
1090         /* reset opengl state */
1091         glDisable(GL_COLOR_MATERIAL);
1092         glDisable(GL_TEXTURE_2D);
1093         glDisable(GL_LIGHTING);
1094         glBindTexture(GL_TEXTURE_2D, 0);
1095         glFrontFace(GL_CCW);
1096
1097         glMatrixMode(GL_TEXTURE);
1098         glLoadIdentity();
1099         glMatrixMode(GL_MODELVIEW);
1100
1101         /* faceselect mode drawing over textured mesh */
1102         if (!(ob == scene->obedit) && (draw_flags & DRAW_FACE_SELECT))
1103                 draw_mesh_face_select(rv3d, ob->data, dm);
1104 }
1105
1106 /* Vertex Paint and Weight Paint */
1107 static void draw_mesh_paint_light_begin(void)
1108 {
1109         const float spec[4] = {0.47f, 0.47f, 0.47f, 0.47f};
1110
1111         GPU_enable_material(0, NULL);
1112
1113         /* but set default spec */
1114         glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
1115         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
1116
1117         /* diffuse */
1118         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
1119         glEnable(GL_LIGHTING);
1120         glEnable(GL_COLOR_MATERIAL);
1121 }
1122 static void draw_mesh_paint_light_end(void)
1123 {
1124         glDisable(GL_COLOR_MATERIAL);
1125         glDisable(GL_LIGHTING);
1126
1127         GPU_disable_material();
1128 }
1129
1130 void draw_mesh_paint_weight_faces(DerivedMesh *dm, const bool use_light,
1131                                   void *facemask_cb, void *user_data)
1132 {
1133         if (use_light) {
1134                 draw_mesh_paint_light_begin();
1135         }
1136
1137         dm->drawMappedFaces(dm, (DMSetDrawOptions)facemask_cb, GPU_enable_material, NULL, user_data,
1138                             DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
1139
1140         if (use_light) {
1141                 draw_mesh_paint_light_end();
1142         }
1143 }
1144
1145 void draw_mesh_paint_vcolor_faces(DerivedMesh *dm, const bool use_light,
1146                                   void *facemask_cb, void *user_data,
1147                                   const Mesh *me)
1148 {
1149         if (use_light) {
1150                 draw_mesh_paint_light_begin();
1151         }
1152
1153         if (me->mloopcol) {
1154                 dm->drawMappedFaces(dm, facemask_cb, GPU_enable_material, NULL, user_data,
1155                                     DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
1156         }
1157         else {
1158                 glColor3f(1.0f, 1.0f, 1.0f);
1159                 dm->drawMappedFaces(dm, facemask_cb, GPU_enable_material, NULL, user_data,
1160                                     DM_DRAW_ALWAYS_SMOOTH);
1161         }
1162
1163         if (use_light) {
1164                 draw_mesh_paint_light_end();
1165         }
1166 }
1167
1168 void draw_mesh_paint_weight_edges(RegionView3D *rv3d, DerivedMesh *dm,
1169                                   const bool use_depth, const bool use_alpha,
1170                                   void *edgemask_cb, void *user_data)
1171 {
1172         /* weight paint in solid mode, special case. focus on making the weights clear
1173          * rather than the shading, this is also forced in wire view */
1174
1175         if (use_depth) {
1176                 ED_view3d_polygon_offset(rv3d, 1.0);
1177                 glDepthMask(0);  /* disable write in zbuffer, selected edge wires show better */
1178         }
1179         else {
1180                 glDisable(GL_DEPTH_TEST);
1181         }
1182
1183         if (use_alpha) {
1184                 glEnable(GL_BLEND);
1185         }
1186
1187         glColor4ub(255, 255, 255, 96);
1188         glEnable(GL_LINE_STIPPLE);
1189         glLineStipple(1, 0xAAAA);
1190
1191         dm->drawMappedEdges(dm, (DMSetDrawOptions)edgemask_cb, user_data);
1192
1193         if (use_depth) {
1194                 ED_view3d_polygon_offset(rv3d, 0.0);
1195                 glDepthMask(1);
1196         }
1197         else {
1198                 glEnable(GL_DEPTH_TEST);
1199         }
1200
1201         glDisable(GL_LINE_STIPPLE);
1202
1203         if (use_alpha) {
1204                 glDisable(GL_BLEND);
1205         }
1206 }
1207
1208 void draw_mesh_paint(View3D *v3d, RegionView3D *rv3d,
1209                      Object *ob, DerivedMesh *dm, const int draw_flags)
1210 {
1211         DMSetDrawOptions facemask = NULL;
1212         Mesh *me = ob->data;
1213         const bool use_light = (v3d->drawtype >= OB_SOLID);
1214
1215         /* hide faces in face select mode */
1216         if (me->editflag & (ME_EDIT_PAINT_VERT_SEL | ME_EDIT_PAINT_FACE_SEL))
1217                 facemask = wpaint__setSolidDrawOptions_facemask;
1218
1219         if (ob->mode & OB_MODE_WEIGHT_PAINT) {
1220                 draw_mesh_paint_weight_faces(dm, use_light, facemask, me);
1221         }
1222         else if (ob->mode & OB_MODE_VERTEX_PAINT) {
1223                 draw_mesh_paint_vcolor_faces(dm, use_light, facemask, me, me);
1224         }
1225
1226         /* draw face selection on top */
1227         if (draw_flags & DRAW_FACE_SELECT) {
1228                 draw_mesh_face_select(rv3d, me, dm);
1229         }
1230         else if ((use_light == false) || (ob->dtx & OB_DRAWWIRE)) {
1231                 const bool use_depth = (v3d->flag & V3D_ZBUF_SELECT) || !(ob->mode & OB_MODE_WEIGHT_PAINT);
1232                 const bool use_alpha = (ob->mode & OB_MODE_VERTEX_PAINT) == 0;
1233
1234                 if (use_alpha == false) {
1235                         set_inverted_drawing(1);
1236                 }
1237
1238                 draw_mesh_paint_weight_edges(rv3d, dm, use_depth, use_alpha, NULL, NULL);
1239
1240                 if (use_alpha == false) {
1241                         set_inverted_drawing(0);
1242                 }
1243         }
1244 }
1245