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