svn merge -r39765:39781 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / editors / space_view3d / drawmesh.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, full update, glsl support
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_view3d/drawmesh.c
29  *  \ingroup spview3d
30  */
31
32 #include <string.h>
33 #include <math.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_utildefines.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_math.h"
40 #include "BLI_edgehash.h"
41 #include "BLI_editVert.h"
42 #include "BLI_utildefines.h"
43
44 #include "DNA_material_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_property_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_view3d_types.h"
50 #include "DNA_windowmanager_types.h"
51 #include "DNA_object_types.h"
52
53 #include "BKE_DerivedMesh.h"
54 #include "BKE_effect.h"
55 #include "BKE_image.h"
56 #include "BKE_material.h"
57 #include "BKE_paint.h"
58 #include "BKE_property.h"
59 #include "BKE_tessmesh.h"
60
61
62 #include "BIF_gl.h"
63 #include "BIF_glutil.h"
64
65 #include "UI_resources.h"
66
67 #include "GPU_buffers.h"
68 #include "GPU_extensions.h"
69 #include "GPU_draw.h"
70
71 #include "ED_mesh.h"
72
73 #include "view3d_intern.h"      // own include
74
75 /**************************** Face Select Mode *******************************/
76
77 /* Flags for marked edges */
78 enum {
79         eEdge_Visible = (1<<0),
80         eEdge_Select = (1<<1),
81 };
82
83 /* Creates a hash of edges to flags indicating selected/visible */
84 static void get_marked_edge_info__orFlags(EdgeHash *eh, int v0, int v1, int flags)
85 {
86         int *flags_p;
87
88         if(!BLI_edgehash_haskey(eh, v0, v1))
89                 BLI_edgehash_insert(eh, v0, v1, NULL);
90
91         flags_p = (int*) BLI_edgehash_lookup_p(eh, v0, v1);
92         *flags_p |= flags;
93 }
94
95 static EdgeHash *get_tface_mesh_marked_edge_info(Mesh *me)
96 {
97         EdgeHash *eh = BLI_edgehash_new();
98         MFace *mf;
99         int i;
100         
101         for(i=0; i<me->totface; i++) {
102                 mf = &me->mface[i];
103
104                 if(!(mf->flag & ME_HIDE)) {
105                         unsigned int flags = eEdge_Visible;
106                         if(mf->flag & ME_FACE_SEL) flags |= eEdge_Select;
107
108                         get_marked_edge_info__orFlags(eh, mf->v1, mf->v2, flags);
109                         get_marked_edge_info__orFlags(eh, mf->v2, mf->v3, flags);
110
111                         if(mf->v4) {
112                                 get_marked_edge_info__orFlags(eh, mf->v3, mf->v4, flags);
113                                 get_marked_edge_info__orFlags(eh, mf->v4, mf->v1, flags);
114                         }
115                         else
116                                 get_marked_edge_info__orFlags(eh, mf->v3, mf->v1, flags);
117                 }
118         }
119
120         return eh;
121 }
122
123
124 static int draw_mesh_face_select__setHiddenOpts(void *userData, int index)
125 {
126         struct { Mesh *me; EdgeHash *eh; } *data = userData;
127         Mesh *me= data->me;
128         MEdge *med = &me->medge[index];
129         uintptr_t flags = (intptr_t) BLI_edgehash_lookup(data->eh, med->v1, med->v2);
130
131         if(me->drawflag & ME_DRAWEDGES) { 
132                 if(me->drawflag & ME_HIDDENEDGES)
133                         return 1;
134                 else
135                         return (flags & eEdge_Visible);
136         }
137         else
138                 return (flags & eEdge_Select);
139 }
140
141 static int draw_mesh_face_select__setSelectOpts(void *userData, int index)
142 {
143         struct { Mesh *me; EdgeHash *eh; } *data = userData;
144         MEdge *med = &data->me->medge[index];
145         uintptr_t flags = (intptr_t) BLI_edgehash_lookup(data->eh, med->v1, med->v2);
146
147         return flags & eEdge_Select;
148 }
149
150 /* draws unselected */
151 static int draw_mesh_face_select__drawFaceOptsInv(void *userData, int index)
152 {
153         Mesh *me = (Mesh*)userData;
154
155         MFace *mface = &me->mface[index];
156         if(!(mface->flag&ME_HIDE) && !(mface->flag&ME_FACE_SEL))
157                 return 2; /* Don't set color */
158         else
159                 return 0;
160 }
161
162 static void draw_mesh_face_select(RegionView3D *rv3d, Mesh *me, DerivedMesh *dm)
163 {
164         struct { Mesh *me; EdgeHash *eh; } data;
165
166         data.me = me;
167         data.eh = get_tface_mesh_marked_edge_info(me);
168
169         glEnable(GL_DEPTH_TEST);
170         glDisable(GL_LIGHTING);
171         bglPolygonOffset(rv3d->dist, 1.0);
172
173         /* Draw (Hidden) Edges */
174         setlinestyle(1);
175         UI_ThemeColor(TH_EDGE_FACESEL);
176         dm->drawMappedEdges(dm, draw_mesh_face_select__setHiddenOpts, &data);
177         setlinestyle(0);
178
179         /* Draw Selected Faces */
180         if(me->drawflag & ME_DRAWFACES) {
181                 glEnable(GL_BLEND);
182                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
183                 /* dull unselected faces so as not to get in the way of seeing color */
184                 glColor4ub(96, 96, 96, 64);
185                 dm->drawMappedFacesTex(dm, draw_mesh_face_select__drawFaceOptsInv, (void*)me);
186                 
187                 glDisable(GL_BLEND);
188         }
189         
190         bglPolygonOffset(rv3d->dist, 1.0);
191
192                 /* Draw Stippled Outline for selected faces */
193         glColor3ub(255, 255, 255);
194         setlinestyle(1);
195         dm->drawMappedEdges(dm, draw_mesh_face_select__setSelectOpts, &data);
196         setlinestyle(0);
197
198         bglPolygonOffset(rv3d->dist, 0.0);      // resets correctly now, even after calling accumulated offsets
199
200         BLI_edgehash_free(data.eh, NULL);
201 }
202
203 /***************************** Texture Drawing ******************************/
204
205 static Material *give_current_material_or_def(Object *ob, int matnr)
206 {
207         extern Material defmaterial;    // render module abuse...
208         Material *ma= give_current_material(ob, matnr);
209
210         return ma?ma:&defmaterial;
211 }
212
213 static int set_draw_settings_cached(int clearcache, int textured, MTFace *texface, int lit, Object *litob, int litmatnr, int doublesided)
214 {
215         static int c_textured;
216         static int c_lit;
217         static int c_doublesided;
218         static MTFace *c_texface;
219         static Object *c_litob;
220         static int c_litmatnr;
221         static int c_badtex;
222
223         if (clearcache) {
224                 c_textured= c_lit= c_doublesided= -1;
225                 c_texface= (MTFace*) -1;
226                 c_litob= (Object*) -1;
227                 c_litmatnr= -1;
228                 c_badtex= 0;
229         }
230
231         if (texface) {
232                 lit = lit && (lit==-1 || texface->mode&TF_LIGHT);
233                 textured = textured && (texface->mode&TF_TEX);
234                 doublesided = texface->mode&TF_TWOSIDE;
235         } else {
236                 textured = 0;
237         }
238
239         if (doublesided!=c_doublesided) {
240                 if (doublesided) glDisable(GL_CULL_FACE);
241                 else glEnable(GL_CULL_FACE);
242
243                 c_doublesided= doublesided;
244         }
245
246         if (textured!=c_textured || texface!=c_texface) {
247                 if (textured ) {
248                         c_badtex= !GPU_set_tpage(texface, !(litob->mode & OB_MODE_TEXTURE_PAINT));
249                 } else {
250                         GPU_set_tpage(NULL, 0);
251                         c_badtex= 0;
252                 }
253                 c_textured= textured;
254                 c_texface= texface;
255         }
256
257         if (c_badtex) lit= 0;
258         if (lit!=c_lit || litob!=c_litob || litmatnr!=c_litmatnr) {
259                 if (lit) {
260                         Material *ma= give_current_material_or_def(litob, litmatnr+1);
261                         float spec[4];
262
263                         spec[0]= ma->spec*ma->specr;
264                         spec[1]= ma->spec*ma->specg;
265                         spec[2]= ma->spec*ma->specb;
266                         spec[3]= 1.0;
267
268                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
269                         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
270                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, CLAMPIS(ma->har, 0, 128));
271                         glEnable(GL_LIGHTING);
272                         glEnable(GL_COLOR_MATERIAL);
273                 }
274                 else {
275                         glDisable(GL_LIGHTING); 
276                         glDisable(GL_COLOR_MATERIAL);
277                 }
278                 c_lit= lit;
279                 c_litob= litob;
280                 c_litmatnr= litmatnr;
281         }
282
283         return c_badtex;
284 }
285
286 /* Icky globals, fix with userdata parameter */
287
288 static struct TextureDrawState {
289         Object *ob;
290         int islit, istex;
291         int color_profile;
292         unsigned char obcol[4];
293 } Gtexdraw = {NULL, 0, 0, 0, {0, 0, 0, 0}};
294
295 static void draw_textured_begin(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
296 {
297         unsigned char obcol[4];
298         int istex, solidtex= 0;
299
300         // XXX scene->obedit warning
301         if(v3d->drawtype==OB_SOLID || ((ob->mode & OB_MODE_EDIT) && v3d->drawtype!=OB_TEXTURE)) {
302                 /* draw with default lights in solid draw mode and edit mode */
303                 solidtex= 1;
304                 Gtexdraw.islit= -1;
305         }
306         else {
307                 /* draw with lights in the scene otherwise */
308                 Gtexdraw.islit= GPU_scene_object_lights(scene, ob, v3d->lay, rv3d->viewmat, !rv3d->is_persp);
309         }
310         
311         obcol[0]= CLAMPIS(ob->col[0]*255, 0, 255);
312         obcol[1]= CLAMPIS(ob->col[1]*255, 0, 255);
313         obcol[2]= CLAMPIS(ob->col[2]*255, 0, 255);
314         obcol[3]= CLAMPIS(ob->col[3]*255, 0, 255);
315         
316         glCullFace(GL_BACK); glEnable(GL_CULL_FACE);
317         if(solidtex || v3d->drawtype==OB_TEXTURE) istex= 1;
318         else istex= 0;
319
320         Gtexdraw.ob = ob;
321         Gtexdraw.istex = istex;
322         Gtexdraw.color_profile = scene->r.color_mgt_flag & R_COLOR_MANAGEMENT;
323         memcpy(Gtexdraw.obcol, obcol, sizeof(obcol));
324         set_draw_settings_cached(1, 0, NULL, Gtexdraw.islit, NULL, 0, 0);
325         glShadeModel(GL_SMOOTH);
326 }
327
328 static void draw_textured_end(void)
329 {
330         /* switch off textures */
331         GPU_set_tpage(NULL, 0);
332
333         glShadeModel(GL_FLAT);
334         glDisable(GL_CULL_FACE);
335
336         /* XXX, bad patch - GPU_default_lights() calls
337          * glLightfv(GL_LIGHT_POSITION, ...) which
338          * is transformed by the current matrix... we
339          * need to make sure that matrix is identity.
340          * 
341          * It would be better if drawmesh.c kept track
342          * of and restored the light settings it changed.
343          *  - zr
344          */
345         glPushMatrix();
346         glLoadIdentity();       
347         GPU_default_lights();
348         glPopMatrix();
349 }
350
351 static int draw_tface__set_draw_legacy(MTFace *tface, int has_vcol, int matnr)
352 {
353         if (tface && (tface->mode&TF_INVISIBLE))
354                 return 0;
355
356         if (tface && set_draw_settings_cached(0, Gtexdraw.istex, tface, Gtexdraw.islit, Gtexdraw.ob, matnr, TF_TWOSIDE)) {
357                 glColor3ub(0xFF, 0x00, 0xFF);
358                 return 2; /* Don't set color */
359         } else if (tface && tface->mode&TF_OBCOL) {
360                 glColor3ubv(Gtexdraw.obcol);
361                 return 2; /* Don't set color */
362         } else if (!has_vcol) {
363                 if (tface) glColor3f(1.0, 1.0, 1.0);
364                 else {
365                         Material *ma= give_current_material(Gtexdraw.ob, matnr+1);
366                         if(ma) {
367                                 float col[3];
368                                 if(Gtexdraw.color_profile) linearrgb_to_srgb_v3_v3(col, &ma->r);
369                                 else copy_v3_v3(col, &ma->r);
370                                 
371                                 glColor3fv(col);
372                         }
373                         else glColor3f(1.0, 1.0, 1.0);
374                 }
375                 return 2; /* Don't set color */
376         } else {
377                 return 1; /* Set color from mcol */
378         }
379 }
380 static int draw_tface__set_draw(MTFace *tface, int has_vcol, int matnr)
381 {
382         if (tface && (tface->mode&TF_INVISIBLE)) return 0;
383
384         if (tface && set_draw_settings_cached(0, Gtexdraw.istex, tface, Gtexdraw.islit, Gtexdraw.ob, matnr, TF_TWOSIDE)) {
385                 return 2; /* Don't set color */
386         } else if (tface && tface->mode&TF_OBCOL) {
387                 return 2; /* Don't set color */
388         } else if (!has_vcol) {
389                 return 1; /* Don't set color */
390         } else {
391                 return 1; /* Set color from mcol */
392         }
393 }
394 static void add_tface_color_layer(DerivedMesh *dm)
395 {
396         MTFace *tface = DM_get_face_data_layer(dm, CD_MTFACE);
397         MFace *mface = dm->getTessFaceArray(dm);
398         MCol *finalCol;
399         int i,j;
400         MCol *mcol = dm->getTessFaceDataArray(dm, CD_WEIGHT_MCOL);
401         if(!mcol)
402                 mcol = dm->getTessFaceDataArray(dm, CD_MCOL);
403
404         finalCol = MEM_mallocN(sizeof(MCol)*4*dm->getNumTessFaces(dm),"add_tface_color_layer");
405         for(i=0;i<dm->getNumTessFaces(dm);i++) {
406                 if (tface && (tface->mode&TF_INVISIBLE)) {
407                         if( mcol )
408                                 memcpy(&finalCol[i*4],&mcol[i*4],sizeof(MCol)*4);
409                         else
410                                 for(j=0;j<4;j++) {
411                                         finalCol[i*4+j].b = 255;
412                                         finalCol[i*4+j].g = 255;
413                                         finalCol[i*4+j].r = 255;
414                                 }
415                 }
416                 else if (tface && mface && set_draw_settings_cached(0, Gtexdraw.istex, tface, Gtexdraw.islit, Gtexdraw.ob, mface[i].mat_nr, TF_TWOSIDE)) {
417                         for(j=0;j<4;j++) {
418                                 finalCol[i*4+j].b = 255;
419                                 finalCol[i*4+j].g = 0;
420                                 finalCol[i*4+j].r = 255;
421                         }
422                 } else if (tface && tface->mode&TF_OBCOL) {
423                         for(j=0;j<4;j++) {
424                                 finalCol[i*4+j].r = FTOCHAR(Gtexdraw.obcol[0]);
425                                 finalCol[i*4+j].g = FTOCHAR(Gtexdraw.obcol[1]);
426                                 finalCol[i*4+j].b = FTOCHAR(Gtexdraw.obcol[2]);
427                         }
428                 } else if (!mcol) {
429                         if (tface) {
430                                 for(j=0;j<4;j++) {
431                                         finalCol[i*4+j].b = 255;
432                                         finalCol[i*4+j].g = 255;
433                                         finalCol[i*4+j].r = 255;
434                                 }
435                         }
436                         else {
437                                 float col[3];
438                                 Material *ma= give_current_material(Gtexdraw.ob, mface[i].mat_nr+1);
439                                 
440                                 if(ma) {
441                                         if(Gtexdraw.color_profile) linearrgb_to_srgb_v3_v3(col, &ma->r);
442                                         else copy_v3_v3(col, &ma->r);
443                                         
444                                         for(j=0;j<4;j++) {
445                                                 finalCol[i*4+j].b = FTOCHAR(col[2]);
446                                                 finalCol[i*4+j].g = FTOCHAR(col[1]);
447                                                 finalCol[i*4+j].r = FTOCHAR(col[0]);
448                                         }
449                                 }
450                                 else
451                                         for(j=0;j<4;j++) {
452                                                 finalCol[i*4+j].b = 255;
453                                                 finalCol[i*4+j].g = 255;
454                                                 finalCol[i*4+j].r = 255;
455                                         }
456                         }
457                 } else {
458                         for(j=0;j<4;j++) {
459                                 finalCol[i*4+j].b = mcol[i*4+j].r;
460                                 finalCol[i*4+j].g = mcol[i*4+j].g;
461                                 finalCol[i*4+j].r = mcol[i*4+j].b;
462                         }
463                 }
464         }
465         CustomData_add_layer( &dm->faceData, CD_TEXTURE_MCOL, CD_ASSIGN, finalCol, dm->numFaceData );
466 }
467
468 static int draw_tface_mapped__set_draw(void *userData, int index)
469 {
470         Mesh *me = (Mesh*)userData;
471         MTexPoly *tpoly = (me->mtpoly)? &me->mtpoly[index]: NULL;
472         MPoly *mpoly = (me->mpoly)? &me->mpoly[index]: NULL;
473         MTFace mtf;
474         int matnr = me->mpoly[index].mat_nr;
475
476         if (mpoly && mpoly->flag&ME_HIDE) return 0;
477
478         memset(&mtf, 0, sizeof(mtf));
479         if (tpoly) {
480                 mtf.flag = tpoly->flag;
481                 mtf.tpage = tpoly->tpage;
482                 mtf.transp = tpoly->transp;
483                 mtf.mode = tpoly->mode;
484                 mtf.tile = tpoly->tile;
485                 mtf.unwrap = tpoly->unwrap;
486         }
487
488         return draw_tface__set_draw(&mtf, CustomData_has_layer(&me->ldata, CD_MLOOPCOL), matnr);
489 }
490
491 static int draw_em_tf_mapped__set_draw(void *userData, int index)
492 {
493         BMEditMesh *em = userData;
494         BMFace *efa= EDBM_get_face_for_index(em, index);
495         MTexPoly *tpoly;
496         MTFace mtf;
497         int matnr, has_vcol;
498
499         if (efa==NULL || BM_TestHFlag(efa, BM_HIDDEN))
500                 return 0;
501
502         tpoly = CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_MTEXPOLY);
503         has_vcol = CustomData_has_layer(&em->bm->ldata, CD_MLOOPCOL);
504         matnr = efa->mat_nr;
505
506         memset(&mtf, 0, sizeof(mtf));
507
508         if (tpoly) {
509                 mtf.flag = tpoly->flag;
510                 mtf.tpage = tpoly->tpage;
511                 mtf.transp = tpoly->transp;
512                 mtf.mode = tpoly->mode;
513                 mtf.tile = tpoly->tile;
514                 mtf.unwrap = tpoly->unwrap;
515         }
516
517         return draw_tface__set_draw_legacy(&mtf, has_vcol, matnr);
518 }
519
520 static int wpaint__setSolidDrawOptions(void *userData, int index, int *drawSmooth_r)
521 {
522         Mesh *me = (Mesh*)userData;
523
524         if (    (me->mface && me->mface[index].flag & ME_HIDE) ||
525                         (me->mtface && (me->mtface[index].mode & TF_INVISIBLE))
526         ) {
527                 return 0;
528         }
529
530         *drawSmooth_r = 1;
531         return 1;
532 }
533
534 static void draw_mesh_text(Scene *scene, Object *ob, int glsl)
535 {
536         Mesh *me = ob->data;
537         DerivedMesh *ddm;
538         MFace *mf, *mface= me->mface;
539         MTFace *tface= me->mtface;
540         MCol *mcol= me->mcol;   /* why does mcol exist? */
541         bProperty *prop = get_ob_property(ob, "Text");
542         GPUVertexAttribs gattribs;
543         int a, totface= me->totface;
544
545         /* don't draw without tfaces */
546         if(!tface)
547                 return;
548
549         /* don't draw when editing */
550         if(ob->mode & OB_MODE_EDIT)
551                 return;
552         else if(ob==OBACT)
553                 if(paint_facesel_test(ob))
554                         return;
555
556         ddm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
557
558         for(a=0, mf=mface; a<totface; a++, tface++, mf++) {
559                 int mode= tface->mode;
560                 int matnr= mf->mat_nr;
561                 int mf_smooth= mf->flag & ME_SMOOTH;
562
563                 if (!(mf->flag&ME_HIDE) && !(mode&TF_INVISIBLE) && (mode&TF_BMFONT)) {
564                         float v1[3], v2[3], v3[3], v4[3];
565                         char string[MAX_PROPSTRING];
566                         int characters, i, glattrib= -1, badtex= 0;
567
568                         if(glsl) {
569                                 GPU_enable_material(matnr+1, &gattribs);
570
571                                 for(i=0; i<gattribs.totlayer; i++) {
572                                         if(gattribs.layer[i].type == CD_MTFACE) {
573                                                 glattrib = gattribs.layer[i].glindex;
574                                                 break;
575                                         }
576                                 }
577                         }
578                         else {
579                                 badtex = set_draw_settings_cached(0, Gtexdraw.istex, tface, Gtexdraw.islit, Gtexdraw.ob, matnr, TF_TWOSIDE);
580                                 if (badtex) {
581                                         if (mcol) mcol+=4;
582                                         continue;
583                                 }
584                         }
585
586                         ddm->getVertCo(ddm, mf->v1, v1);
587                         ddm->getVertCo(ddm, mf->v2, v2);
588                         ddm->getVertCo(ddm, mf->v3, v3);
589                         if (mf->v4) ddm->getVertCo(ddm, mf->v4, v4);
590
591                         // The BM_FONT handling is in the gpu module, shared with the
592                         // game engine, was duplicated previously
593
594                         set_property_valstr(prop, string);
595                         characters = strlen(string);
596                         
597                         if(!BKE_image_get_ibuf(tface->tpage, NULL))
598                                 characters = 0;
599
600                         if (!mf_smooth) {
601                                 float nor[3];
602
603                                 normal_tri_v3( nor,v1, v2, v3);
604
605                                 glNormal3fv(nor);
606                         }
607
608                         GPU_render_text(tface, tface->mode, string, characters,
609                                 (unsigned int*)mcol, v1, v2, v3, (mf->v4? v4: NULL), glattrib);
610                 }
611                 if (mcol) {
612                         mcol+=4;
613                 }
614         }
615
616         ddm->release(ddm);
617 }
618
619 void draw_mesh_textured(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob, DerivedMesh *dm, int faceselect)
620 {
621         Mesh *me= ob->data;
622         
623         /* correct for negative scale */
624         if(ob->transflag & OB_NEG_SCALE) glFrontFace(GL_CW);
625         else glFrontFace(GL_CCW);
626         
627         /* draw the textured mesh */
628         draw_textured_begin(scene, v3d, rv3d, ob);
629
630         glColor4f(1.0f,1.0f,1.0f,1.0f);
631
632         if(ob->mode & OB_MODE_EDIT) {
633                 dm->drawMappedFacesTex(dm, draw_em_tf_mapped__set_draw, me->edit_btmesh);
634         }
635         else if(faceselect) {
636                 if(ob->mode & OB_MODE_WEIGHT_PAINT)
637                         dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, me, 1, GPU_enable_material, NULL);
638                 else
639                         dm->drawMappedFacesTex(dm, me->mface ? draw_tface_mapped__set_draw : NULL, me);
640         }
641         else {
642                 if(GPU_buffer_legacy(dm)) {
643                         dm->drawFacesTex(dm, draw_tface__set_draw_legacy);
644                 }
645                 else {
646                         if(!CustomData_has_layer(&dm->faceData,CD_TEXTURE_MCOL))
647                                 add_tface_color_layer(dm);
648
649                         dm->drawFacesTex(dm, draw_tface__set_draw);
650                 }
651         }
652
653         /* draw game engine text hack */
654         if(get_ob_property(ob, "Text")) 
655                 draw_mesh_text(scene, ob, 0);
656
657         draw_textured_end();
658         
659         /* draw edges and selected faces over textured mesh */
660         if(!(ob == scene->obedit) && faceselect)
661                 draw_mesh_face_select(rv3d, me, dm);
662
663         /* reset from negative scale correction */
664         glFrontFace(GL_CCW);
665         
666         /* in editmode, the blend mode needs to be set incase it was ADD */
667         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
668 }
669