2cef10e1981baf1f22af3f6429d551e76a2c560c
[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         bool use_backface_culling;
240         unsigned char obcol[4];
241 } Gtexdraw = {NULL, 0, 0, 0, false, {0, 0, 0, 0}};
242
243 static int set_draw_settings_cached(int clearcache, MTFace *texface, Material *ma, struct TextureDrawState gtexdraw)
244 {
245         static Material *c_ma;
246         static int c_textured;
247         static MTFace c_texface;
248         static int c_backculled;
249         static int c_badtex;
250         static int c_lit;
251         static int c_has_texface;
252
253         Object *litob = NULL;  /* to get mode to turn off mipmap in painting mode */
254         int backculled = GEMAT_BACKCULL || gtexdraw.use_backface_culling;
255         int alphablend = 0;
256         int textured = 0;
257         int lit = 0;
258         int has_texface = texface != NULL;
259         int need_set_tpage = FALSE;
260
261         if (clearcache) {
262                 c_textured = c_lit = c_backculled = -1;
263                 memset(&c_texface, 0, sizeof(MTFace));
264                 c_badtex = 0;
265                 c_has_texface = -1;
266         }
267         else {
268                 textured = gtexdraw.is_tex;
269                 litob = gtexdraw.ob;
270         }
271
272         /* convert number of lights into boolean */
273         if (gtexdraw.is_lit) lit = 1;
274
275         if (ma) {
276                 alphablend = ma->game.alpha_blend;
277                 if (ma->mode & MA_SHLESS) lit = 0;
278                 backculled = (ma->game.flag & GEMAT_BACKCULL) || gtexdraw.use_backface_culling;
279         }
280
281         if (texface) {
282                 textured = textured && (texface->tpage);
283
284                 /* no material, render alpha if texture has depth=32 */
285                 if (!ma && BKE_image_has_alpha(texface->tpage))
286                         alphablend = GPU_BLEND_ALPHA;
287         }
288
289         else
290                 textured = 0;
291
292         if (backculled != c_backculled) {
293                 if (backculled) glEnable(GL_CULL_FACE);
294                 else glDisable(GL_CULL_FACE);
295
296                 c_backculled = backculled;
297         }
298
299         /* need to re-set tpage if textured flag changed or existsment of texface changed..  */
300         need_set_tpage = textured != c_textured || has_texface != c_has_texface;
301         /* ..or if settings inside texface were changed (if texface was used) */
302         need_set_tpage |= texface && memcmp(&c_texface, texface, sizeof(c_texface));
303
304         if (need_set_tpage) {
305                 if (textured) {
306                         c_badtex = !GPU_set_tpage(texface, !(litob->mode & OB_MODE_TEXTURE_PAINT), alphablend);
307                 }
308                 else {
309                         GPU_set_tpage(NULL, 0, 0);
310                         c_badtex = 0;
311                 }
312                 c_textured = textured;
313                 c_has_texface = has_texface;
314                 if (texface)
315                         memcpy(&c_texface, texface, sizeof(c_texface));
316         }
317
318         if (c_badtex) lit = 0;
319         if (lit != c_lit || ma != c_ma) {
320                 if (lit) {
321                         float spec[4];
322                         if (!ma) ma = give_current_material_or_def(NULL, 0);  /* default material */
323
324                         spec[0] = ma->spec * ma->specr;
325                         spec[1] = ma->spec * ma->specg;
326                         spec[2] = ma->spec * ma->specb;
327                         spec[3] = 1.0;
328
329                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
330                         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
331                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, CLAMPIS(ma->har, 0, 128));
332                         glEnable(GL_LIGHTING);
333                         glEnable(GL_COLOR_MATERIAL);
334                 }
335                 else {
336                         glDisable(GL_LIGHTING); 
337                         glDisable(GL_COLOR_MATERIAL);
338                 }
339                 c_lit = lit;
340         }
341
342         return c_badtex;
343 }
344
345 static void draw_textured_begin(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
346 {
347         unsigned char obcol[4];
348         int is_tex, solidtex;
349         Mesh *me = ob->data;
350
351         /* XXX scene->obedit warning */
352
353         /* texture draw is abused for mask selection mode, do this so wire draw
354          * with face selection in weight paint is not lit. */
355         if ((v3d->drawtype <= OB_WIRE) && (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))) {
356                 solidtex = FALSE;
357                 Gtexdraw.is_lit = 0;
358         }
359         else if (v3d->drawtype == OB_SOLID || ((ob->mode & OB_MODE_EDIT) && v3d->drawtype != OB_TEXTURE)) {
360                 /* draw with default lights in solid draw mode and edit mode */
361                 solidtex = TRUE;
362                 Gtexdraw.is_lit = -1;
363         }
364         else {
365                 /* draw with lights in the scene otherwise */
366                 solidtex = FALSE;
367                 Gtexdraw.is_lit = GPU_scene_object_lights(scene, ob, v3d->lay, rv3d->viewmat, !rv3d->is_persp);
368         }
369         
370         rgba_float_to_uchar(obcol, ob->col);
371
372         if (solidtex || v3d->drawtype == OB_TEXTURE) is_tex = 1;
373         else is_tex = 0;
374
375         Gtexdraw.ob = ob;
376         Gtexdraw.is_tex = is_tex;
377
378         Gtexdraw.color_profile = BKE_scene_check_color_management_enabled(scene);
379         Gtexdraw.use_backface_culling = (v3d->flag2 & V3D_BACKFACE_CULLING) != 0;
380
381         memcpy(Gtexdraw.obcol, obcol, sizeof(obcol));
382         set_draw_settings_cached(1, NULL, NULL, Gtexdraw);
383         glShadeModel(GL_SMOOTH);
384         glCullFace(GL_BACK);
385         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, (me->flag & ME_TWOSIDED) ? GL_TRUE : GL_FALSE);
386 }
387
388 static void draw_textured_end(void)
389 {
390         /* switch off textures */
391         GPU_set_tpage(NULL, 0, 0);
392
393         glShadeModel(GL_FLAT);
394         glDisable(GL_CULL_FACE);
395         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
396
397         /* XXX, bad patch - GPU_default_lights() calls
398          * glLightfv(GL_POSITION, ...) which
399          * is transformed by the current matrix... we
400          * need to make sure that matrix is identity.
401          * 
402          * It would be better if drawmesh.c kept track
403          * of and restored the light settings it changed.
404          *  - zr
405          */
406         glPushMatrix();
407         glLoadIdentity();
408         GPU_default_lights();
409         glPopMatrix();
410 }
411
412 static DMDrawOption draw_tface__set_draw_legacy(MTFace *tface, int has_mcol, int matnr)
413 {
414         Material *ma = give_current_material(Gtexdraw.ob, matnr + 1);
415         int invalidtexture = 0;
416
417         if (ma && (ma->game.flag & GEMAT_INVISIBLE))
418                 return DM_DRAW_OPTION_SKIP;
419
420         invalidtexture = set_draw_settings_cached(0, tface, ma, Gtexdraw);
421
422         if (tface && invalidtexture) {
423                 glColor3ub(0xFF, 0x00, 0xFF);
424                 return DM_DRAW_OPTION_NO_MCOL; /* Don't set color */
425         }
426         else if (ma && (ma->shade_flag & MA_OBCOLOR)) {
427                 glColor3ubv(Gtexdraw.obcol);
428                 return DM_DRAW_OPTION_NO_MCOL; /* Don't set color */
429         }
430         else if (!has_mcol) {
431                 if (tface) glColor3f(1.0, 1.0, 1.0);
432                 else {
433                         if (ma) {
434                                 float col[3];
435                                 if (Gtexdraw.color_profile) linearrgb_to_srgb_v3_v3(col, &ma->r);
436                                 else copy_v3_v3(col, &ma->r);
437                                 
438                                 glColor3fv(col);
439                         }
440                         else glColor3f(1.0, 1.0, 1.0);
441                 }
442                 return DM_DRAW_OPTION_NO_MCOL; /* Don't set color */
443         }
444         else {
445                 return DM_DRAW_OPTION_NORMAL; /* Set color from mcol */
446         }
447 }
448
449 static DMDrawOption draw_mcol__set_draw_legacy(MTFace *UNUSED(tface), int has_mcol, int UNUSED(matnr))
450 {
451         if (has_mcol)
452                 return DM_DRAW_OPTION_NORMAL;
453         else
454                 return DM_DRAW_OPTION_NO_MCOL;
455 }
456
457 static DMDrawOption draw_tface__set_draw(MTFace *tface, int UNUSED(has_mcol), int matnr)
458 {
459         Material *ma = give_current_material(Gtexdraw.ob, matnr + 1);
460
461         if (ma && (ma->game.flag & GEMAT_INVISIBLE)) return 0;
462
463         if (tface)
464                 set_draw_settings_cached(0, tface, ma, Gtexdraw);
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 not cycles, or preview-modifiers, or drawing matcaps */
940         if ((!BKE_scene_use_new_shading_nodes(scene)) || (draw_flags & DRAW_MODIFIERS_PREVIEW) || (v3d->flag2 & V3D_SHOW_SOLID_MATCAP)) {
941                 draw_mesh_textured_old(scene, v3d, rv3d, ob, dm, draw_flags);
942                 return;
943         }
944         else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT)) {
945                 draw_mesh_paint(v3d, rv3d, ob, dm, draw_flags);
946                 return;
947         }
948
949         /* set opengl state for negative scale & color */
950         if (ob->transflag & OB_NEG_SCALE) glFrontFace(GL_CW);
951         else glFrontFace(GL_CCW);
952
953         glEnable(GL_LIGHTING);
954
955         {
956                 Mesh *me = ob->data;
957                 TexMatCallback data = {scene, ob, me, dm};
958                 int (*set_face_cb)(void *, int);
959                 int glsl, picking = (G.f & G_PICKSEL);
960                 
961                 /* face hiding callback depending on mode */
962                 if (ob == scene->obedit)
963                         set_face_cb = tex_mat_set_face_editmesh_cb;
964                 else if (draw_flags & DRAW_FACE_SELECT)
965                         set_face_cb = tex_mat_set_face_mesh_cb;
966                 else
967                         set_face_cb = NULL;
968
969                 /* test if we can use glsl */
970                 glsl = (v3d->drawtype == OB_MATERIAL) && GPU_glsl_support() && !picking;
971
972                 GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl, NULL);
973
974                 if (glsl || picking) {
975                         /* draw glsl */
976                         dm->drawMappedFacesMat(dm,
977                                                tex_mat_set_material_cb,
978                                                set_face_cb, &data);
979                 }
980                 else {
981                         float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
982
983                         /* draw textured */
984                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, zero);
985                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
986                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 0);
987
988                         dm->drawMappedFacesMat(dm,
989                                                tex_mat_set_texture_cb,
990                                                set_face_cb, &data);
991                 }
992
993                 GPU_end_object_materials();
994         }
995
996         /* reset opengl state */
997         glDisable(GL_COLOR_MATERIAL);
998         glDisable(GL_TEXTURE_2D);
999         glDisable(GL_LIGHTING);
1000         glBindTexture(GL_TEXTURE_2D, 0);
1001         glFrontFace(GL_CCW);
1002
1003         glMatrixMode(GL_TEXTURE);
1004         glLoadIdentity();
1005         glMatrixMode(GL_MODELVIEW);
1006
1007         /* faceselect mode drawing over textured mesh */
1008         if (!(ob == scene->obedit) && (draw_flags & DRAW_FACE_SELECT))
1009                 draw_mesh_face_select(rv3d, ob->data, dm);
1010 }
1011
1012 /* Vertex Paint and Weight Paint */
1013
1014 void draw_mesh_paint(View3D *v3d, RegionView3D *rv3d,
1015                      Object *ob, DerivedMesh *dm, const int draw_flags)
1016 {
1017         DMSetDrawOptions facemask = NULL;
1018         Mesh *me = ob->data;
1019         const short do_light = (v3d->drawtype >= OB_SOLID);
1020
1021         /* hide faces in face select mode */
1022         if (me->editflag & (ME_EDIT_PAINT_VERT_SEL | ME_EDIT_PAINT_FACE_SEL))
1023                 facemask = wpaint__setSolidDrawOptions_facemask;
1024
1025         if (ob->mode & OB_MODE_WEIGHT_PAINT) {
1026
1027                 if (do_light) {
1028                         const float spec[4] = {0.47f, 0.47f, 0.47f, 0.47f};
1029
1030                         /* enforce default material settings */
1031                         GPU_enable_material(0, NULL);
1032                 
1033                         /* but set default spec */
1034                         glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
1035                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
1036
1037                         /* diffuse */
1038                         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
1039                         glEnable(GL_LIGHTING);
1040                         glEnable(GL_COLOR_MATERIAL);
1041                 }
1042
1043                 dm->drawMappedFaces(dm, facemask, GPU_enable_material, NULL, me,
1044                                     DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
1045
1046                 if (do_light) {
1047                         glDisable(GL_COLOR_MATERIAL);
1048                         glDisable(GL_LIGHTING);
1049
1050                         GPU_disable_material();
1051                 }
1052         }
1053         else if (ob->mode & OB_MODE_VERTEX_PAINT) {
1054                 if (me->mloopcol) {
1055                         dm->drawMappedFaces(dm, facemask, GPU_enable_material, NULL, me,
1056                                             DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
1057                 }
1058                 else {
1059                         glColor3f(1.0f, 1.0f, 1.0f);
1060                         dm->drawMappedFaces(dm, facemask, GPU_enable_material, NULL, me,
1061                                             DM_DRAW_ALWAYS_SMOOTH);
1062                 }
1063         }
1064
1065         /* draw face selection on top */
1066         if (draw_flags & DRAW_FACE_SELECT) {
1067                 draw_mesh_face_select(rv3d, me, dm);
1068         }
1069         else if ((do_light == FALSE) || (ob->dtx & OB_DRAWWIRE)) {
1070                 const int use_depth = (v3d->flag & V3D_ZBUF_SELECT) || !(ob->mode & OB_MODE_WEIGHT_PAINT);
1071
1072                 /* weight paint in solid mode, special case. focus on making the weights clear
1073                  * rather than the shading, this is also forced in wire view */
1074
1075                 if (use_depth) {
1076                         bglPolygonOffset(rv3d->dist, 1.0);
1077                         glDepthMask(0);  /* disable write in zbuffer, selected edge wires show better */
1078                 }
1079                 else {
1080                         glDisable(GL_DEPTH_TEST);
1081                 }
1082
1083                 glEnable(GL_BLEND);
1084                 glColor4ub(255, 255, 255, 96);
1085                 glEnable(GL_LINE_STIPPLE);
1086                 glLineStipple(1, 0xAAAA);
1087
1088                 dm->drawEdges(dm, 1, 1);
1089
1090                 if (use_depth) {
1091                         bglPolygonOffset(rv3d->dist, 0.0);
1092                         glDepthMask(1);
1093                 }
1094                 else {
1095                         glEnable(GL_DEPTH_TEST);
1096                 }
1097
1098                 glDisable(GL_LINE_STIPPLE);
1099                 glDisable(GL_BLEND);
1100         }
1101 }
1102