a0a76bb6d15e08624962c737992703418e8440c0
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <string.h>
29 #include <math.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_arithb.h"
35 #include "BLI_edgehash.h"
36 #include "BLI_editVert.h"
37
38 #include "IMB_imbuf.h"
39 #include "IMB_imbuf_types.h"
40
41 #include "DNA_image_types.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_property_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_userdef_types.h"
52
53 #include "BKE_bmfont.h"
54 #include "BKE_displist.h"
55 #include "BKE_DerivedMesh.h"
56 #include "BKE_effect.h"
57 #include "BKE_global.h"
58 #include "BKE_image.h"
59 #include "BKE_main.h"
60 #include "BKE_material.h"
61 #include "BKE_mesh.h"
62 #include "BKE_object.h"
63 #include "BKE_property.h"
64 #include "BKE_utildefines.h"
65
66 #include "BIF_gl.h"
67 #include "BIF_glutil.h"
68
69 #include "UI_resources.h"
70 #include "UI_interface_icons.h"
71
72 #include "GPU_extensions.h"
73 #include "GPU_draw.h"
74
75 #include "view3d_intern.h"      // own include
76
77 /***/
78
79         /* Flags for marked edges */
80 enum {
81         eEdge_Visible = (1<<0),
82         eEdge_Select = (1<<1),
83 };
84
85         /* Creates a hash of edges to flags indicating
86          * adjacent tface select/active/etc flags.
87          */
88 static void get_marked_edge_info__orFlags(EdgeHash *eh, int v0, int v1, int flags)
89 {
90         int *flags_p;
91
92         if (!BLI_edgehash_haskey(eh, v0, v1)) {
93                 BLI_edgehash_insert(eh, v0, v1, 0);
94         }
95
96         flags_p = (int*) BLI_edgehash_lookup_p(eh, v0, v1);
97         *flags_p |= flags;
98 }
99
100 static EdgeHash *get_tface_mesh_marked_edge_info(Mesh *me)
101 {
102         EdgeHash *eh = BLI_edgehash_new();
103         int i;
104         MFace *mf;
105         MTFace *tf = NULL;
106         
107         for (i=0; i<me->totface; i++) {
108                 mf = &me->mface[i];
109                 if (me->mtface)
110                         tf = &me->mtface[i];
111                 
112                 if (mf->v3) {
113                         if (!(mf->flag&ME_HIDE)) {
114                                 unsigned int flags = eEdge_Visible;
115                                 if (mf->flag&ME_FACE_SEL) flags |= eEdge_Select;
116
117                                 get_marked_edge_info__orFlags(eh, mf->v1, mf->v2, flags);
118                                 get_marked_edge_info__orFlags(eh, mf->v2, mf->v3, flags);
119                                 if (mf->v4) {
120                                         get_marked_edge_info__orFlags(eh, mf->v3, mf->v4, flags);
121                                         get_marked_edge_info__orFlags(eh, mf->v4, mf->v1, flags);
122                                 } else {
123                                         get_marked_edge_info__orFlags(eh, mf->v3, mf->v1, flags);
124                                 }
125                         }
126                 }
127         }
128
129         return eh;
130 }
131
132
133 static int draw_tfaces3D__setHiddenOpts(void *userData, int index)
134 {
135         struct { Mesh *me; EdgeHash *eh; } *data = userData;
136         MEdge *med = &data->me->medge[index];
137         uintptr_t flags = (intptr_t) BLI_edgehash_lookup(data->eh, med->v1, med->v2);
138
139         if((G.f & G_DRAWSEAMS) && (med->flag&ME_SEAM)) {
140                 return 0;
141         } else if(G.f & G_DRAWEDGES){ 
142                 if (G.f&G_HIDDENEDGES) {
143                         return 1;
144                 } else {
145                         return (flags & eEdge_Visible);
146                 }
147         } else {
148                 return (flags & eEdge_Select);
149         }
150 }
151 static int draw_tfaces3D__setSeamOpts(void *userData, int index)
152 {
153         struct { Mesh *me; EdgeHash *eh; } *data = userData;
154         MEdge *med = &data->me->medge[index];
155         uintptr_t flags = (intptr_t) BLI_edgehash_lookup(data->eh, med->v1, med->v2);
156
157         if (med->flag&ME_SEAM) {
158                 if (G.f&G_HIDDENEDGES) {
159                         return 1;
160                 } else {
161                         return (flags & eEdge_Visible);
162                 }
163         } else {
164                 return 0;
165         }
166 }
167 static int draw_tfaces3D__setSelectOpts(void *userData, int index)
168 {
169         struct { Mesh *me; EdgeHash *eh; } *data = userData;
170         MEdge *med = &data->me->medge[index];
171         uintptr_t flags = (intptr_t) BLI_edgehash_lookup(data->eh, med->v1, med->v2);
172
173         return flags & eEdge_Select;
174 }
175 static int draw_tfaces3D__setActiveOpts(void *userData, int index)
176 {
177         struct { Mesh *me; EdgeHash *eh; } *data = userData;
178         MEdge *med = &data->me->medge[index];
179         uintptr_t flags = (intptr_t) BLI_edgehash_lookup(data->eh, med->v1, med->v2);
180
181         if (flags & eEdge_Select) {
182                 return 1;
183         } else {
184                 return 0;
185         }
186 }
187 static int draw_tfaces3D__drawFaceOpts(void *userData, int index)
188 {
189         Mesh *me = (Mesh*)userData;
190
191         MFace *mface = &me->mface[index];
192         if (!(mface->flag&ME_HIDE) && (mface->flag&ME_FACE_SEL))
193                 return 2; /* Don't set color */
194         else
195                 return 0;
196 }
197 static void draw_tfaces3D(Object *ob, Mesh *me, DerivedMesh *dm)
198 {
199         struct { Mesh *me; EdgeHash *eh; } data;
200
201         data.me = me;
202         data.eh = get_tface_mesh_marked_edge_info(me);
203
204         glEnable(GL_DEPTH_TEST);
205         glDisable(GL_LIGHTING);
206         bglPolygonOffset(1.0);
207
208                 /* Draw (Hidden) Edges */
209         UI_ThemeColor(TH_EDGE_FACESEL);
210         dm->drawMappedEdges(dm, draw_tfaces3D__setHiddenOpts, &data);
211
212                 /* Draw Seams */
213         if(G.f & G_DRAWSEAMS) {
214                 UI_ThemeColor(TH_EDGE_SEAM);
215                 glLineWidth(2);
216
217                 dm->drawMappedEdges(dm, draw_tfaces3D__setSeamOpts, &data);
218
219                 glLineWidth(1);
220         }
221
222         /* Draw Selected Faces */
223         if(G.f & G_DRAWFACES) {
224                 glEnable(GL_BLEND);
225                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
226                 UI_ThemeColor4(TH_FACE_SELECT);
227
228                 dm->drawMappedFacesTex(dm, draw_tfaces3D__drawFaceOpts, (void*)me);
229
230                 glDisable(GL_BLEND);
231         }
232         
233         bglPolygonOffset(1.0);
234
235                 /* Draw Stippled Outline for selected faces */
236         glColor3ub(255, 255, 255);
237         setlinestyle(1);
238         dm->drawMappedEdges(dm, draw_tfaces3D__setSelectOpts, &data);
239         setlinestyle(0);
240
241         dm->drawMappedEdges(dm, draw_tfaces3D__setActiveOpts, &data);
242
243         bglPolygonOffset(0.0);  // resets correctly now, even after calling accumulated offsets
244
245         BLI_edgehash_free(data.eh, NULL);
246 }
247
248 static Material *give_current_material_or_def(Object *ob, int matnr)
249 {
250         extern Material defmaterial;    // render module abuse...
251         Material *ma= give_current_material(ob, matnr);
252
253         return ma?ma:&defmaterial;
254 }
255
256 static int set_draw_settings_cached(int clearcache, int textured, MTFace *texface, int lit, Object *litob, int litmatnr, int doublesided)
257 {
258         static int c_textured;
259         static int c_lit;
260         static int c_doublesided;
261         static MTFace *c_texface;
262         static Object *c_litob;
263         static int c_litmatnr;
264         static int c_badtex;
265
266         if (clearcache) {
267                 c_textured= c_lit= c_doublesided= -1;
268                 c_texface= (MTFace*) -1;
269                 c_litob= (Object*) -1;
270                 c_litmatnr= -1;
271                 c_badtex= 0;
272         }
273
274         if (texface) {
275                 lit = lit && (lit==-1 || texface->mode&TF_LIGHT);
276                 textured = textured && (texface->mode&TF_TEX);
277                 doublesided = texface->mode&TF_TWOSIDE;
278         } else {
279                 textured = 0;
280         }
281
282         if (doublesided!=c_doublesided) {
283                 if (doublesided) glDisable(GL_CULL_FACE);
284                 else glEnable(GL_CULL_FACE);
285
286                 c_doublesided= doublesided;
287         }
288
289         if (textured!=c_textured || texface!=c_texface) {
290                 if (textured ) {
291                         c_badtex= !GPU_set_tpage(texface);
292                 } else {
293                         GPU_set_tpage(0);
294                         c_badtex= 0;
295                 }
296                 c_textured= textured;
297                 c_texface= texface;
298         }
299
300         if (c_badtex) lit= 0;
301         if (lit!=c_lit || litob!=c_litob || litmatnr!=c_litmatnr) {
302                 if (lit) {
303                         Material *ma= give_current_material_or_def(litob, litmatnr+1);
304                         float spec[4];
305
306                         spec[0]= ma->spec*ma->specr;
307                         spec[1]= ma->spec*ma->specg;
308                         spec[2]= ma->spec*ma->specb;
309                         spec[3]= 1.0;
310
311                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
312                         glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
313                         glEnable(GL_LIGHTING);
314                         glEnable(GL_COLOR_MATERIAL);
315                 }
316                 else {
317                         glDisable(GL_LIGHTING); 
318                         glDisable(GL_COLOR_MATERIAL);
319                 }
320                 c_lit= lit;
321                 c_litob= litob;
322                 c_litmatnr= litmatnr;
323         }
324
325         return c_badtex;
326 }
327
328 /* Icky globals, fix with userdata parameter */
329
330 struct TextureDrawState {
331         Object *ob;
332         int islit, istex;
333         unsigned char obcol[4];
334 } Gtexdraw = {NULL, 0, 0, {0, 0, 0, 0}};
335
336 static void draw_textured_begin(Scene *scene, View3D *v3d, Object *ob)
337 {
338         unsigned char obcol[4];
339         int istex, solidtex= 0;
340
341         if(v3d->drawtype==OB_SOLID || (ob==G.obedit && v3d->drawtype!=OB_TEXTURE)) {
342                 /* draw with default lights in solid draw mode and edit mode */
343                 solidtex= 1;
344                 Gtexdraw.islit= -1;
345         }
346         else
347                 /* draw with lights in the scene otherwise */
348                 Gtexdraw.islit= GPU_scene_object_lights(scene, ob, v3d->lay, v3d->viewmat);
349         
350         obcol[0]= CLAMPIS(ob->col[0]*255, 0, 255);
351         obcol[1]= CLAMPIS(ob->col[1]*255, 0, 255);
352         obcol[2]= CLAMPIS(ob->col[2]*255, 0, 255);
353         obcol[3]= CLAMPIS(ob->col[3]*255, 0, 255);
354         
355         glCullFace(GL_BACK); glEnable(GL_CULL_FACE);
356         if(solidtex || v3d->drawtype==OB_TEXTURE) istex= 1;
357         else istex= 0;
358
359         Gtexdraw.ob = ob;
360         Gtexdraw.istex = istex;
361         memcpy(Gtexdraw.obcol, obcol, sizeof(obcol));
362         set_draw_settings_cached(1, 0, 0, Gtexdraw.islit, 0, 0, 0);
363         glShadeModel(GL_SMOOTH);
364 }
365
366 static void draw_textured_end()
367 {
368         /* switch off textures */
369         GPU_set_tpage(0);
370
371         glShadeModel(GL_FLAT);
372         glDisable(GL_CULL_FACE);
373
374         /* XXX, bad patch - GPU_default_lights() calls
375          * glLightfv(GL_LIGHT_POSITION, ...) which
376          * is transformed by the current matrix... we
377          * need to make sure that matrix is identity.
378          * 
379          * It would be better if drawmesh.c kept track
380          * of and restored the light settings it changed.
381          *  - zr
382          */
383         glPushMatrix();
384         glLoadIdentity();       
385         GPU_default_lights();
386         glPopMatrix();
387 }
388
389
390 static int draw_tface__set_draw(MTFace *tface, MCol *mcol, int matnr)
391 {
392         if (tface && (tface->mode&TF_INVISIBLE)) return 0;
393
394         if (tface && set_draw_settings_cached(0, Gtexdraw.istex, tface, Gtexdraw.islit, Gtexdraw.ob, matnr, TF_TWOSIDE)) {
395                 glColor3ub(0xFF, 0x00, 0xFF);
396                 return 2; /* Don't set color */
397         } else if (tface && tface->mode&TF_OBCOL) {
398                 glColor3ubv(Gtexdraw.obcol);
399                 return 2; /* Don't set color */
400         } else if (!mcol) {
401                 if (tface) glColor3f(1.0, 1.0, 1.0);
402                 else {
403                         Material *ma= give_current_material(Gtexdraw.ob, matnr+1);
404                         if(ma) glColor3f(ma->r, ma->g, ma->b);
405                         else glColor3f(1.0, 1.0, 1.0);
406                 }
407                 return 2; /* Don't set color */
408         } else {
409                 return 1; /* Set color from mcol */
410         }
411 }
412
413 static int draw_tface_mapped__set_draw(void *userData, int index)
414 {
415         Mesh *me = (Mesh*)userData;
416         MTFace *tface = (me->mtface)? &me->mtface[index]: NULL;
417         MFace *mface = (me->mface)? &me->mface[index]: NULL;
418         MCol *mcol = (me->mcol)? &me->mcol[index]: NULL;
419         int matnr = me->mface[index].mat_nr;
420         if (mface && mface->flag&ME_HIDE) return 0;
421         return draw_tface__set_draw(tface, mcol, matnr);
422 }
423
424 static int draw_em_tf_mapped__set_draw(void *userData, int index)
425 {
426         EditMesh *em = userData;
427         EditFace *efa= NULL; // XXX = EM_get_face_for_index(index);
428         MTFace *tface;
429         MCol *mcol;
430         int matnr;
431
432         if (efa==NULL || efa->h)
433                 return 0;
434
435         tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
436         mcol = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
437         matnr = efa->mat_nr;
438
439         return draw_tface__set_draw(tface, mcol, matnr);
440 }
441
442 static int wpaint__setSolidDrawOptions(void *userData, int index, int *drawSmooth_r)
443 {
444         Mesh *me = (Mesh*)userData;
445         MTFace *tface = (me->mtface)? &me->mtface[index]: NULL;
446         MFace *mface = (me->mface)? &me->mface[index]: NULL;
447         
448         if ((mface->flag&ME_HIDE) || (tface && (tface->mode&TF_INVISIBLE))) 
449                         return 0;
450         
451         *drawSmooth_r = 1;
452         return 1;
453 }
454
455 void draw_mesh_text(Object *ob, int glsl)
456 {
457         Mesh *me = ob->data;
458         DerivedMesh *ddm;
459         MFace *mf, *mface= me->mface;
460         MTFace *tface= me->mtface;
461         MCol *mcol= me->mcol;   /* why does mcol exist? */
462         bProperty *prop = get_ob_property(ob, "Text");
463         GPUVertexAttribs gattribs;
464         int a, totface= me->totface;
465
466         /* don't draw without tfaces */
467         if(!tface)
468                 return;
469
470         /* don't draw when editing */
471         if(ob==G.obedit)
472                 return;
473         else if(ob==OBACT)
474                 if(FACESEL_PAINT_TEST)
475                         return;
476
477         ddm = mesh_get_derived_deform(ob, CD_MASK_BAREMESH);
478
479         for(a=0, mf=mface; a<totface; a++, tface++, mf++) {
480                 int mode= tface->mode;
481                 int matnr= mf->mat_nr;
482                 int mf_smooth= mf->flag & ME_SMOOTH;
483
484                 if (!(mf->flag&ME_HIDE) && !(mode&TF_INVISIBLE) && (mode&TF_BMFONT)) {
485                         float v1[3], v2[3], v3[3], v4[3];
486                         char string[MAX_PROPSTRING];
487                         int characters, i, glattrib= -1, badtex= 0;
488
489                         if(glsl) {
490                                 GPU_enable_material(matnr+1, &gattribs);
491
492                                 for(i=0; i<gattribs.totlayer; i++) {
493                                         if(gattribs.layer[i].type == CD_MTFACE) {
494                                                 glattrib = gattribs.layer[i].glindex;
495                                                 break;
496                                         }
497                                 }
498                         }
499                         else {
500                                 badtex = set_draw_settings_cached(0, Gtexdraw.istex, tface, Gtexdraw.islit, Gtexdraw.ob, matnr, TF_TWOSIDE);
501                                 if (badtex) {
502                                         if (mcol) mcol+=4;
503                                         continue;
504                                 }
505                         }
506
507                         ddm->getVertCo(ddm, mf->v1, v1);
508                         ddm->getVertCo(ddm, mf->v2, v2);
509                         ddm->getVertCo(ddm, mf->v3, v3);
510                         if (mf->v4) ddm->getVertCo(ddm, mf->v4, v4);
511
512                         // The BM_FONT handling is in the gpu module, shared with the
513                         // game engine, was duplicated previously
514
515                         set_property_valstr(prop, string);
516                         characters = strlen(string);
517                         
518                         if(!BKE_image_get_ibuf(tface->tpage, NULL))
519                                 characters = 0;
520
521                         if (!mf_smooth) {
522                                 float nor[3];
523
524                                 CalcNormFloat(v1, v2, v3, nor);
525
526                                 glNormal3fv(nor);
527                         }
528
529                         GPU_render_text(tface, tface->mode, string, characters,
530                                 (unsigned int*)mcol, v1, v2, v3, (mf->v4? v4: NULL), glattrib);
531                 }
532                 if (mcol) {
533                         mcol+=4;
534                 }
535         }
536
537         ddm->release(ddm);
538 }
539
540 void draw_mesh_textured(Scene *scene, View3D *v3d, Object *ob, DerivedMesh *dm, int faceselect)
541 {
542         Mesh *me= ob->data;
543         
544         /* correct for negative scale */
545         if(ob->transflag & OB_NEG_SCALE) glFrontFace(GL_CW);
546         else glFrontFace(GL_CCW);
547         
548         /* draw the textured mesh */
549         draw_textured_begin(scene, v3d, ob);
550
551         if(ob==G.obedit) {
552                 dm->drawMappedFacesTex(dm, draw_em_tf_mapped__set_draw, G.editMesh);
553         } else if(faceselect) {
554                 if(G.f & G_WEIGHTPAINT)
555                         dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, me, 1);
556                 else
557                         dm->drawMappedFacesTex(dm, draw_tface_mapped__set_draw, me);
558         }
559         else
560                 dm->drawFacesTex(dm, draw_tface__set_draw);
561
562         /* draw game engine text hack */
563         if(get_ob_property(ob, "Text")) 
564                 draw_mesh_text(ob, 0);
565
566         draw_textured_end();
567         
568         /* draw edges and selected faces over textured mesh */
569         if(!G.obedit && faceselect)
570                 draw_tfaces3D(ob, me, dm);
571
572         /* reset from negative scale correction */
573         glFrontFace(GL_CCW);
574         
575         /* in editmode, the blend mode needs to be set incase it was ADD */
576         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
577 }
578