One free too many on shading displaylists, causes material-less objects not
[blender.git] / source / blender / blenkernel / intern / displist.c
1
2 /*  displist.c
3  * 
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version. The Blender
13  * Foundation also sells licenses for use in proprietary software under
14  * the Blender License.  See http://www.blender.org/BL/ for information
15  * about this.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
27  * All rights reserved.
28  *
29  * The Original Code is: all of this file.
30  *
31  * Contributor(s): none yet.
32  *
33  * ***** END GPL/BL DUAL LICENSE BLOCK *****
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <math.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #ifdef WIN32
45 #include "BLI_winstuff.h"
46 #endif
47 #include "MEM_guardedalloc.h"
48
49 #include "IMB_imbuf_types.h"
50
51 #include "DNA_texture_types.h"
52 #include "DNA_meta_types.h"
53 #include "DNA_curve_types.h"
54 #include "DNA_effect_types.h"
55 #include "DNA_listBase.h"
56 #include "DNA_lamp_types.h"
57 #include "DNA_object_types.h"
58 #include "DNA_mesh_types.h"
59 #include "DNA_meshdata_types.h"
60 #include "DNA_scene_types.h"
61 #include "DNA_image_types.h"
62 #include "DNA_material_types.h"
63 #include "DNA_view3d_types.h"
64 #include "DNA_lattice_types.h"
65
66 #include "BLI_blenlib.h"
67 #include "BLI_arithb.h"
68 #include "BLI_editVert.h"
69
70 #include "BKE_bad_level_calls.h"
71 #include "BKE_utildefines.h"
72 #include "BKE_global.h"
73 #include "BKE_displist.h"
74 #include "BKE_deform.h"
75 #include "BKE_object.h"
76 #include "BKE_world.h"
77 #include "BKE_mesh.h"
78 #include "BKE_effect.h"
79 #include "BKE_mball.h"
80 #include "BKE_material.h"
81 #include "BKE_curve.h"
82 #include "BKE_anim.h"
83 #include "BKE_screen.h"
84 #include "BKE_texture.h"
85 #include "BKE_library.h"
86 #include "BKE_font.h"
87 #include "BKE_lattice.h"
88 #include "BKE_scene.h"
89 #include "BKE_subsurf.h"
90
91 #include "nla.h" /* For __NLA: Please do not remove yet */
92 #include "render.h"
93
94
95 /***/
96
97 typedef struct _FastLamp FastLamp;
98 struct _FastLamp {
99         FastLamp *next;
100         
101         short type, mode, lay, rt;
102         float co[3];
103         float vec[3];
104         float dist, distkw, att1, att2, spotsi, spotbl, r, g, b;
105 };
106
107 /***/
108
109 static FastLamp *fastlamplist= NULL;
110 static float fviewmat[4][4];
111
112 /* only called from subsurf.c when levels is zero */
113 DispListMesh *displistmesh_from_editmesh(EditMesh *em) 
114 {
115         DispListMesh *dlm= MEM_callocN(sizeof(*dlm),"dlm");
116         EditVert *eve, *evePrev;
117         EditEdge *eed;
118         EditFace *evl;
119         MFace *mfNew;
120         int i;
121
122         dlm->flag= 0;
123         dlm->totvert= BLI_countlist(&em->verts);
124         dlm->totface= BLI_countlist(&em->edges)+BLI_countlist(&em->faces);
125         dlm->mvert= MEM_callocN(sizeof(*dlm->mface)*dlm->totvert, "dlm->mvert");
126         dlm->mcol= NULL;
127         dlm->tface= NULL;
128         dlm->mface= MEM_mallocN(sizeof(*dlm->mface)*dlm->totface, "dlm->mface");
129
130         i=0;
131         for (eve= em->verts.first; eve; eve= eve->next, i++) {
132                 MVert *mvNew= &dlm->mvert[i];
133                 VECCOPY(mvNew->co, eve->co);
134                 eve->ssco= eve->co;             /* no subsurf coordinate, but we add a save pointer in it */
135                 eve->prev= (void*) i;   /* hack to fetch indices */
136         }
137
138         mfNew= dlm->mface;
139         for (evl= em->faces.first; evl; evl= evl->next) {
140                 // we skip hidden faces
141                 if(evl->v1->h || evl->v2->h || evl->v3->h || (evl->v4 && evl->v4->h)) dlm->totface--;
142                 else {
143                         mfNew->v1= (int) evl->v1->prev;
144                         mfNew->v2= (int) evl->v2->prev;
145                         mfNew->v3= (int) evl->v3->prev;
146                         mfNew->v4= evl->v4?(int) evl->v4->prev:0;
147                         mfNew->flag= evl->flag;
148                         mfNew->mat_nr= evl->mat_nr;
149                         mfNew->puno= 0;
150                         mfNew->edcode= 0;
151         
152                         if (evl->v4 && !mfNew->v4) {
153                                 mfNew->v1^= mfNew->v3^= mfNew->v1^= mfNew->v3;
154                                 mfNew->v2^= mfNew->v4^= mfNew->v2^= mfNew->v4;
155                         }
156                         mfNew++;
157                 }
158         }
159         for (eed= em->edges.first; eed; eed= eed->next, mfNew++) {
160
161                 mfNew->v1= (int) eed->v1->prev;
162                 mfNew->v2= (int) eed->v2->prev;
163                 mfNew->v3= 0;
164                 mfNew->v4= 0;
165                 mfNew->flag= 0;
166                 mfNew->mat_nr= 0;
167                 mfNew->puno= 0;
168                 mfNew->edcode= 0;
169         }
170
171                 /* restore prev links */
172         for (evePrev=NULL, eve= em->verts.first; eve; evePrev=eve, eve= eve->next)
173                 eve->prev= evePrev;
174
175         displistmesh_calc_vert_normals(dlm);
176
177         return dlm;
178 }
179 DispListMesh *displistmesh_from_mesh(Mesh *me, float *extverts) 
180 {
181         DispListMesh *dlm= MEM_callocN(sizeof(*dlm),"dlm");
182         int i;
183         dlm->flag= 0;
184         dlm->totvert= me->totvert;
185         dlm->totface= me->totface;
186         dlm->mvert= MEM_dupallocN(me->mvert);
187         dlm->mcol= me->mcol?MEM_dupallocN(me->mcol):NULL;
188         dlm->tface= me->tface?MEM_dupallocN(me->tface):NULL;
189         dlm->mface= MEM_mallocN(sizeof(*dlm->mface)*dlm->totface, "dlm->mface");
190
191         if (extverts) {
192                 for (i=0; i<dlm->totvert; i++) {
193                         VECCOPY(dlm->mvert[i].co, &extverts[i*3]);
194                 }
195         }
196         for (i=0; i<dlm->totface; i++) {
197                 MFace *mfOld= &((MFace*) me->mface)[i];
198                 MFace *mfNew= &dlm->mface[i];
199
200                 mfNew->v1= mfOld->v1;
201                 mfNew->v2= mfOld->v2;
202                 mfNew->v3= mfOld->v3;
203                 mfNew->v4= mfOld->v4;
204                 mfNew->flag= mfOld->flag;
205                 mfNew->mat_nr= mfOld->mat_nr;
206                 mfNew->puno= 0;
207                 mfNew->edcode= 0;
208         }
209
210         return dlm;
211 }
212
213 void displistmesh_free(DispListMesh *dlm) 
214 {
215         // also check on mvert and mface, can be NULL after decimator (ton)
216         if (dlm->mvert) MEM_freeN(dlm->mvert);
217         if (dlm->medge) MEM_freeN(dlm->medge);
218         if (dlm->mface) MEM_freeN(dlm->mface);
219         if (dlm->mcol) MEM_freeN(dlm->mcol);
220         if (dlm->tface) MEM_freeN(dlm->tface);
221         if (dlm->editedge) MEM_freeN(dlm->editedge);
222         if (dlm->editface) MEM_freeN(dlm->editface);
223         MEM_freeN(dlm);
224 }
225
226 static DispListMesh *displistmesh_copy(DispListMesh *odlm) 
227 {
228         DispListMesh *ndlm= MEM_dupallocN(odlm);
229         ndlm->mvert= MEM_dupallocN(odlm->mvert);
230         ndlm->medge= MEM_dupallocN(odlm->medge);
231         ndlm->mface= MEM_dupallocN(odlm->mface);
232         if (odlm->mcol) ndlm->mcol= MEM_dupallocN(odlm->mcol);
233         if (odlm->tface) ndlm->tface= MEM_dupallocN(odlm->tface);
234         
235         return ndlm;
236 }
237
238 void displistmesh_calc_vert_normals(DispListMesh *dlm) 
239 {
240         MVert *mverts= dlm->mvert;
241         int nmverts= dlm->totvert;
242         MFace *mfaces= dlm->mface;
243         int nmfaces= dlm->totface;
244         float (*tnorms)[3]= MEM_callocN(nmverts*sizeof(*tnorms), "tnorms");
245         int i;
246         
247         for (i=0; i<nmfaces; i++) {
248                 MFace *mf= &mfaces[i];
249                 float f_no[3];
250
251                 if (!mf->v3)
252                         continue;
253                         
254                 if (mf->v4)
255                         CalcNormFloat4(mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co, f_no);
256                 else
257                         CalcNormFloat(mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, f_no);
258                 
259                 VecAddf(tnorms[mf->v1], tnorms[mf->v1], f_no);
260                 VecAddf(tnorms[mf->v2], tnorms[mf->v2], f_no);
261                 VecAddf(tnorms[mf->v3], tnorms[mf->v3], f_no);
262                 if (mf->v4)
263                         VecAddf(tnorms[mf->v4], tnorms[mf->v4], f_no);
264         }
265         for (i=0; i<nmverts; i++) {
266                 MVert *mv= &mverts[i];
267                 float *no= tnorms[i];
268                 
269                 Normalise(no);
270                 mv->no[0]= (short)(no[0]*32767.0);
271                 mv->no[1]= (short)(no[1]*32767.0);
272                 mv->no[2]= (short)(no[2]*32767.0);
273         }
274         
275         MEM_freeN(tnorms);
276 }
277
278 void displistmesh_to_mesh(DispListMesh *dlm, Mesh *me) 
279 {
280         MFace *mfaces;
281         int i;
282         
283         if (dlm->totvert>MESH_MAX_VERTS) {
284                 error("Too many vertices");
285         } else {
286                 me->totface= dlm->totface;
287                 me->totvert= dlm->totvert;
288
289                 me->mvert= MEM_dupallocN(dlm->mvert);
290                 me->mface= mfaces= MEM_mallocN(sizeof(*mfaces)*me->totface, "me->mface");
291                 me->tface= MEM_dupallocN(dlm->tface);
292                 me->mcol= MEM_dupallocN(dlm->mcol);
293
294                 if(dlm->medge) {
295                         me->totedge= dlm->totedge;
296                         me->medge= MEM_dupallocN(dlm->medge);
297                 }
298
299                 for (i=0; i<me->totface; i++) {
300                         MFace *mf= &mfaces[i];
301                         MFace *oldmf= &dlm->mface[i];
302                 
303                         mf->v1= oldmf->v1;
304                         mf->v2= oldmf->v2;
305                         mf->v3= oldmf->v3;
306                         mf->v4= oldmf->v4;
307                         mf->flag= oldmf->flag;
308                         mf->mat_nr= oldmf->mat_nr;
309                         mf->puno= 0;
310                         mf->edcode= ME_V1V2|ME_V2V3|ME_V3V4|ME_V4V1;
311                 }
312         }
313 }
314
315 void free_disp_elem(DispList *dl)
316 {
317         if(dl) {
318                 if(dl->verts) MEM_freeN(dl->verts);
319                 if(dl->nors) MEM_freeN(dl->nors);
320                 if(dl->index) MEM_freeN(dl->index);
321                 if(dl->col1) MEM_freeN(dl->col1);
322                 if(dl->col2) MEM_freeN(dl->col2);
323                 if(dl->mesh) displistmesh_free(dl->mesh);
324                 MEM_freeN(dl);
325         }
326 }
327
328 void freedisplist(ListBase *lb)
329 {
330         DispList *dl;
331
332         dl= lb->first;
333         while(dl) {
334                 BLI_remlink(lb, dl);
335                 free_disp_elem(dl);
336                 dl= lb->first;
337         }
338 }
339
340 static void freedisplist_object(Object *ob)
341 {
342         freedisplist(&ob->disp);
343
344         if(ob->type==OB_MESH) {
345                 Mesh *me= ob->data;
346                 freedisplist(&me->disp);
347         }
348         else if(ob->type==OB_CURVE || ob->type==OB_SURF || ob->type==OB_FONT) {
349                 Curve *cu= ob->data;
350                 freedisplist(&cu->disp);
351         }
352 }
353
354 void free_displist_by_type(ListBase *lb, int type) 
355 {
356         DispList *dl;
357         
358         for (dl= lb->first; dl; ) {
359                 DispList *next= dl->next;
360                 
361                 if (dl->type==type) {
362                         BLI_remlink(lb, dl);
363                         free_disp_elem(dl);
364                 }
365                 
366                 dl= next;
367         }       
368 }
369
370 DispList *find_displist_create(ListBase *lb, int type)
371 {
372         DispList *dl;
373         
374         dl= lb->first;
375         while(dl) {
376                 if(dl->type==type) return dl;
377                 dl= dl->next;
378         }
379
380         dl= MEM_callocN(sizeof(DispList), "find_disp");
381         dl->type= type;
382         BLI_addtail(lb, dl);
383
384         return dl;
385 }
386
387 DispList *find_displist(ListBase *lb, int type)
388 {
389         DispList *dl;
390         
391         dl= lb->first;
392         while(dl) {
393                 if(dl->type==type) return dl;
394                 dl= dl->next;
395         }
396
397         return 0;
398 }
399
400 int displist_has_faces(ListBase *lb)
401 {
402         DispList *dl;
403         
404         dl= lb->first;
405         while(dl) {
406                 if ELEM6(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF, DL_MESH, DL_TRIA, DL_POLY)
407                         return 1;
408                 dl= dl->next;
409         }
410         return 0;
411 }
412
413 void copy_displist(ListBase *lbn, ListBase *lb)
414 {
415         DispList *dln, *dl;
416         
417         lbn->first= lbn->last= 0;
418         
419         dl= lb->first;
420         while(dl) {
421                 
422                 dln= MEM_dupallocN(dl);
423                 BLI_addtail(lbn, dln);
424                 dln->verts= MEM_dupallocN(dl->verts);
425                 dln->nors= MEM_dupallocN(dl->nors);
426                 dln->index= MEM_dupallocN(dl->index);
427                 dln->col1= MEM_dupallocN(dl->col1);
428                 dln->col2= MEM_dupallocN(dl->col2);
429                 if (dl->mesh)
430                         dln->mesh= displistmesh_copy(dl->mesh);
431                 
432                 dl= dl->next;
433         }
434 }
435
436 static void initfastshade(void)
437 {
438         Base *base;
439         Object *ob;
440         Lamp *la;
441         FastLamp *fl;
442         float mat[4][4];
443
444         init_render_world();
445         
446         if(fastlamplist) return;
447         if(G.scene->camera==0) G.scene->camera= scene_find_camera(G.scene);
448         if(G.scene->camera==0) return;
449
450         /* copied from 'roteerscene' (does that function still exist? (ton) */
451         where_is_object(G.scene->camera);
452         Mat4CpyMat4(R.viewinv, G.scene->camera->obmat);
453         Mat4Ortho(R.viewinv);
454         Mat4Invert(fviewmat, R.viewinv);
455
456         /* initrendertexture(); */
457
458         base= G.scene->base.first;
459         while(base) {
460                 ob= base->object;
461                 if( ob->type==OB_LAMP && (base->lay & G.scene->lay)) {
462                         
463                         Mat4MulMat4(mat, ob->obmat, fviewmat);
464                         
465                         la= ob->data;
466                         fl= MEM_mallocN(sizeof(FastLamp), "initfastshade2");
467
468                         fl->next= fastlamplist;
469                         fastlamplist= fl;
470
471                         fl->type= la->type;
472                         fl->mode= la->mode;
473                         fl->lay= base->lay;
474                         
475                         fl->vec[0]= mat[2][0];
476                         fl->vec[1]= mat[2][1];
477                         fl->vec[2]= mat[2][2];
478                         Normalise(fl->vec);
479                         
480                         fl->co[0]= mat[3][0];
481                         fl->co[1]= mat[3][1];
482                         fl->co[2]= mat[3][2];
483
484                         fl->dist= la->dist;
485                         fl->distkw= fl->dist*fl->dist;
486                         fl->att1= la->att1;
487                         fl->att2= la->att2;
488
489                         fl->spotsi= (float)cos( M_PI*la->spotsize/360.0 );
490                         fl->spotbl= (1.0f-fl->spotsi)*la->spotblend;
491         
492                         fl->r= la->energy*la->r;
493                         fl->g= la->energy*la->g;
494                         fl->b= la->energy*la->b;
495                 }
496                 
497                 if(base->next==0 && G.scene->set && base==G.scene->base.last) base= G.scene->set->base.first;
498                 else base= base->next;
499         }
500 }
501
502
503 void freefastshade()
504 {
505         while (fastlamplist) {
506                 FastLamp *fl= fastlamplist;
507                 fastlamplist= fl->next;
508                 
509                 MEM_freeN(fl);
510         }
511 }
512
513
514 static void fastshade(float *co, float *nor, float *orco, Material *ma, char *col1, char *col2, char *vertcol)
515 {
516         ShadeInput shi;
517         FastLamp *fl;
518         float i, t, inp, is, soft,  lv[3], lampdist, ld;
519         float diff1[3], diff2[3];
520         float isr1=0, isg1=0, isb1=0, isr=0, isg=0, isb=0;
521         int a, back;
522
523         if(ma==0) return;
524         shi.mat= ma;
525         shi.matren= ma->ren;
526         shi.vlr= NULL;  // have to do this!
527         ma= shi.matren;
528         shi.osatex= 0;  // also prevents reading vlr
529         VECCOPY(shi.vn, nor);
530         
531         if(ma->mode & MA_VERTEXCOLP) {
532                 if(vertcol) {
533                         ma->r= vertcol[3]/255.0;
534                         ma->g= vertcol[2]/255.0;
535                         ma->b= vertcol[1]/255.0;
536                 }
537         }
538         
539         if(ma->texco) {
540                 VECCOPY(shi.lo, orco);
541                 
542                 if(ma->texco & TEXCO_GLOB) {
543                         VECCOPY(shi.gl, shi.lo);
544                 }
545                 if(ma->texco & TEXCO_WINDOW) {
546                         VECCOPY(shi.winco, shi.lo);
547                 }
548                 if(ma->texco & TEXCO_STICKY) {
549                         VECCOPY(shi.sticky, shi.lo);
550                 }
551                 if(ma->texco & TEXCO_UV) {
552                         VECCOPY(shi.uv, shi.lo);
553                 }
554                 if(ma->texco & TEXCO_OBJECT) {
555                         VECCOPY(shi.co, shi.lo);
556                 }
557                 if(ma->texco & TEXCO_NORM) {
558                         VECCOPY(shi.orn, shi.vn);
559                 }
560                 if(ma->texco & TEXCO_REFL) {
561                         
562                         inp= 2.0*(shi.vn[2]);
563                         shi.ref[0]= (inp*shi.vn[0]);
564                         shi.ref[1]= (inp*shi.vn[1]);
565                         shi.ref[2]= (-1.0+inp*shi.vn[2]);
566                 }
567
568                 if(ma->mode & MA_VERTEXCOLP) {
569                         shi.mat->r= ma->r;
570                         shi.mat->g= ma->g;
571                         shi.mat->b= ma->b;
572                 }
573                 do_material_tex(&shi);
574         }
575
576         if(ma->mode & MA_SHLESS) {
577                 if(vertcol && (ma->mode & (MA_VERTEXCOL+MA_VERTEXCOLP))== MA_VERTEXCOL ) {
578                         col1[3]= vertcol[3]*ma->r;
579                         col1[2]= vertcol[2]*ma->g;
580                         col1[1]= vertcol[1]*ma->b;
581                 }
582                 else {
583                         col1[3]= (255.0*ma->r);
584                         col1[2]= (255.0*ma->g);
585                         col1[1]= (255.0*ma->b);
586                 }
587                 if(col2) {
588                         col2[3]= col1[3];
589                         col2[2]= col1[2];
590                         col2[1]= col1[1];
591                 }
592                 return;
593         }
594
595         if( vertcol && (ma->mode & (MA_VERTEXCOL+MA_VERTEXCOLP))== MA_VERTEXCOL ) {
596                 diff1[0]= diff2[0]= ma->r*(ma->emit+vertcol[3]/255.0);
597                 diff1[1]= diff2[1]= ma->g*(ma->emit+vertcol[2]/255.0);
598                 diff1[2]= diff2[2]= ma->b*(ma->emit+vertcol[1]/255.0);
599         }
600         else {
601                 diff1[0]= diff2[0]= ma->r*ma->emit;
602                 diff1[1]= diff2[1]= ma->g*ma->emit;
603                 diff1[2]= diff2[2]= ma->b*ma->emit;
604         }
605         
606         shi.view[0]= 0.0;
607         shi.view[1]= 0.0;
608         shi.view[2]= 1.0;
609         
610         Normalise(shi.view);
611
612         for (fl= fastlamplist; fl; fl= fl->next) {
613                 /* if(fl->mode & LA_LAYER) if((fl->lay & ma->lay)==0) continue; */
614
615                 if(fl->type==LA_SUN || fl->type==LA_HEMI) {
616                         VECCOPY(lv, fl->vec);
617                         lampdist= 1.0;
618                 }
619                 else {
620                         lv[0]= fl->co[0] - co[0];
621                         lv[1]= fl->co[1] - co[1];
622                         lv[2]= fl->co[2] - co[2];
623                         ld= sqrt(lv[0]*lv[0]+lv[1]*lv[1]+lv[2]*lv[2]);
624                         lv[0]/=ld;
625                         lv[1]/=ld;
626                         lv[2]/=ld;
627
628                         if(fl->mode & LA_QUAD) {
629                                 t= 1.0;
630                                 if(fl->att1>0.0)
631                                         t= fl->dist/(fl->dist+fl->att1*ld);
632                                 if(fl->att2>0.0)
633                                         t*= fl->distkw/(fl->distkw+fl->att2*ld*ld);
634
635                                 lampdist= t;
636                         }
637                         else {
638                                 lampdist= (fl->dist/(fl->dist+ld));
639                         }
640
641                         if(fl->mode & LA_SPHERE) {
642                                 t= fl->dist - ld;
643                                 if(t<0.0) continue;
644                                 
645                                 t/= fl->dist;
646                                 lampdist*= (t);
647                         }
648                 }
649
650                 if(fl->type==LA_SPOT) {
651                         inp= lv[0]*fl->vec[0]+lv[1]*fl->vec[1]+lv[2]*fl->vec[2];
652                         if(inp<fl->spotsi) continue;
653                         else {
654                                 t= inp-fl->spotsi;
655                                 i= 1.0;
656                                 soft= 1.0;
657                                 if(t<fl->spotbl && fl->spotbl!=0.0) {
658                                         /* soft area */
659                                         i= t/fl->spotbl;
660                                         t= i*i;
661                                         soft= (3.0*t-2.0*t*i);
662                                         inp*= soft;
663                                 }
664
665                                 lampdist*=inp;
666                         }
667                 }
668
669                 if(fl->mode & LA_NO_DIFF) is= 0.0;
670                 else {
671                         is= nor[0]*lv[0]+ nor[1]*lv[1]+ nor[2]*lv[2];
672         
673                         if(ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(nor, lv, shi.view, ma->roughness);
674                         else if(ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(nor, lv, shi.view, ma->param[0], ma->param[1]);
675                 }
676                 
677                 back= 0;
678                 if(is<0.0) {
679                         back= 1;
680                         is= -is;
681                 }
682                 inp= is*lampdist*ma->ref;
683
684                 if(back==0) {
685                         add_to_diffuse(diff1, &shi, is, inp*fl->r, inp*fl->g, inp*fl->b);
686                         //diff1[0]+= inp*fl->r;
687                         //diff1[1]+= inp*fl->g;
688                         //diff1[2]+= inp*fl->b;
689                 } else if(col2) {
690                         add_to_diffuse(diff2, &shi, is, inp*fl->r, inp*fl->g, inp*fl->b);
691                         //diff2[0]+= inp*fl->r;
692                         //diff2[1]+= inp*fl->g;
693                         //diff2[2]+= inp*fl->b;
694                 }
695                 if(ma->spec && (fl->mode & LA_NO_SPEC)==0) {
696                         float specfac;
697                         
698                         if(ma->spec_shader==MA_SPEC_PHONG) 
699                                 specfac= Phong_Spec(nor, lv, shi.view, ma->har);
700                         else if(ma->spec_shader==MA_SPEC_COOKTORR) 
701                                 specfac= CookTorr_Spec(nor, lv, shi.view, ma->har);
702                         else if(ma->spec_shader==MA_SPEC_BLINN) 
703                                 specfac= Blinn_Spec(nor, lv, shi.view, ma->refrac, (float)ma->har);
704                         else 
705                                 specfac= Toon_Spec(nor, lv, shi.view, ma->param[2], ma->param[3]);
706                         
707                         if(specfac>0) {
708                                 t= specfac*ma->spec*lampdist;
709                                 if(back==0) {
710                                         if(ma->mode & MA_RAMP_SPEC) {
711                                                 float spec[3];
712                                                 do_specular_ramp(&shi, specfac, t, spec);
713                                                 isr+= t*(fl->r * spec[0]);
714                                                 isg+= t*(fl->g * spec[1]);
715                                                 isb+= t*(fl->b * spec[2]);
716                                         }
717                                         else {
718                                                 isr+= t*(fl->r * ma->specr);
719                                                 isg+= t*(fl->g * ma->specg);
720                                                 isb+= t*(fl->b * ma->specb);
721                                         }
722                                 }
723                                 else if(col2) {
724                                         if(ma->mode & MA_RAMP_SPEC) {
725                                                 float spec[3];
726                                                 do_specular_ramp(&shi, specfac, t, spec);
727                                                 isr1+= t*(fl->r * spec[0]);
728                                                 isg1+= t*(fl->g * spec[1]);
729                                                 isb1+= t*(fl->b * spec[2]);
730                                         }
731                                         else {
732                                                 isr1+= t*(fl->r * ma->specr);
733                                                 isg1+= t*(fl->g * ma->specg);
734                                                 isb1+= t*(fl->b * ma->specb);
735                                         }
736                                 }
737                         }
738                 }
739
740         }
741
742         if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(diff1, &shi);
743         if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(&isr, &isg, &isb, &shi);
744
745         a= 256*(diff1[0] + ma->ambr +isr);
746         if(a>255) col1[3]= 255; 
747         else col1[3]= a;
748         a= 256*(diff1[1] + ma->ambg +isg);
749         if(a>255) col1[2]= 255; 
750         else col1[2]= a;
751         a= 256*(diff1[2] + ma->ambb +isb);
752         if(a>255) col1[1]= 255; 
753         else col1[1]= a;
754
755         if(col2) {
756                 if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(diff2, &shi);
757                 if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(&isr1, &isg1, &isb1, &shi);
758                 
759                 a= 256*(diff2[0] + ma->ambr +isr1);
760                 if(a>255) col2[3]= 255; 
761                 else col2[3]= a;
762                 a= 256*(diff2[1] + ma->ambg +isg1);
763                 if(a>255) col2[2]= 255; 
764                 else col2[2]= a;
765                 a= 256*(diff2[2] + ma->ambb +isb1);
766                 if(a>255) col2[1]= 255; 
767                 else col2[1]= a;
768         }
769
770 }
771
772 void addnormalsDispList(Object *ob, ListBase *lb)
773 {
774         DispList *dl = NULL;
775         Mesh *me;
776         MVert *ve1, *ve2, *ve3, *ve4;
777         MFace *mface;
778         float *vdata, *ndata, nor[3];
779         float *v1, *v2, *v3, *v4;
780         float *n1, *n2, *n3, *n4;
781         int a, b, p1, p2, p3, p4;
782
783         
784         if(ob->type==OB_MESH) {
785                 
786                 me= get_mesh(ob);
787                 
788                 if (mesh_uses_displist(me)) {
789                         DispList *dl= find_displist(&me->disp, DL_MESH);
790                         
791                         if (dl && !dl->nors) {
792                                 DispListMesh *dlm= dl->mesh;
793                                 int i;
794                                 
795                                 dl->nors= MEM_mallocN(sizeof(*dl->nors)*3*dlm->totface, "meshnormals");
796                                 
797                                 for (i=0; i<dlm->totface; i++) {
798                                         MFace *mf= &dlm->mface[i];
799                                         float *no= &dl->nors[i*3];
800                                         
801                                         if (mf->v3) {
802                                                 if (mf->v4)
803                                                         CalcNormFloat4(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, dlm->mvert[mf->v4].co, no);
804                                                 else
805                                                         CalcNormFloat(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, no);
806                                         }
807                                 }
808                         }
809                 } else {
810                         if(me->totface==0) return;
811                         
812                         if(me->disp.first==0) {
813                                 dl= MEM_callocN(sizeof(DispList), "meshdisp");
814                                 dl->type= DL_NORS;
815                                 dl->parts= 1;
816                                 dl->nr= me->totface;
817                                 BLI_addtail(&me->disp, dl);
818                         }
819                         else return;
820                         
821                         if(dl->nors==0) {
822                                 dl->nors= MEM_mallocN(sizeof(float)*3*me->totface, "meshnormals");
823                                 n1= dl->nors;
824                                 mface= me->mface;
825                                 a= me->totface;
826                                 while(a--) {
827                                         if(mface->v3) {
828                                                 ve1= me->mvert+mface->v1;
829                                                 ve2= me->mvert+mface->v2;
830                                                 ve3= me->mvert+mface->v3;
831                                                 ve4= me->mvert+mface->v4;
832                                                 
833                                                 if(mface->v4) CalcNormFloat4(ve1->co, ve2->co, ve3->co, ve4->co, n1);
834                                                 else CalcNormFloat(ve1->co, ve2->co, ve3->co, n1);
835                                         }
836                                         n1+= 3;
837                                         mface++;
838                                 }
839                         }
840                 }
841
842                 return;
843         }
844
845         dl= lb->first;
846         
847         while(dl) {
848                 if(dl->type==DL_INDEX3) {
849                         if(dl->nors==0) {
850                                 dl->nors= MEM_callocN(sizeof(float)*3, "dlnors");
851                                 if(dl->verts[2]<0.0) dl->nors[2]= -1.0;
852                                 else dl->nors[2]= 1.0;
853                         }
854                 }
855                 else if(dl->type==DL_SURF) {
856                         if(dl->nors==0) {
857                                 dl->nors= MEM_callocN(sizeof(float)*3*dl->nr*dl->parts, "dlnors");
858                                 
859                                 vdata= dl->verts;
860                                 ndata= dl->nors;
861                                 
862                                 for(a=0; a<dl->parts; a++) {
863         
864                                         DL_SURFINDEX(dl->flag & DL_CYCL_U, dl->flag & DL_CYCL_V, dl->nr, dl->parts);
865         
866                                         v1= vdata+ 3*p1; 
867                                         n1= ndata+ 3*p1;
868                                         v2= vdata+ 3*p2; 
869                                         n2= ndata+ 3*p2;
870                                         v3= vdata+ 3*p3; 
871                                         n3= ndata+ 3*p3;
872                                         v4= vdata+ 3*p4; 
873                                         n4= ndata+ 3*p4;
874                                         
875                                         for(; b<dl->nr; b++) {
876         
877                                                 CalcNormFloat4(v1, v3, v4, v2, nor);
878         
879                                                 VecAddf(n1, n1, nor);
880                                                 VecAddf(n2, n2, nor);
881                                                 VecAddf(n3, n3, nor);
882                                                 VecAddf(n4, n4, nor);
883         
884                                                 v2= v1; v1+= 3;
885                                                 v4= v3; v3+= 3;
886                                                 n2= n1; n1+= 3;
887                                                 n4= n3; n3+= 3;
888                                         }
889                                 }
890                                 a= dl->parts*dl->nr;
891                                 v1= ndata;
892                                 while(a--) {
893                                         Normalise(v1);
894                                         v1+= 3;
895                                 }
896                         }
897                 }
898                 dl= dl->next;
899         }
900 }
901
902
903 void shadeDispList(Object *ob)
904 {
905         MFace *mface;
906         MVert *mvert;
907         DispList *dl, *dlob, *dldeform;
908         Material *ma = NULL;
909         Mesh *me;
910         Curve *cu;
911 /*      extern Material defmaterial;     *//* initrender.c, already in bad lev calls*/
912         float *orco=NULL, imat[3][3], tmat[4][4], mat[4][4], vec[3], xn, yn, zn;
913         float *fp, *nor, n1[3];
914         unsigned int *col1, *col2, *vertcol;
915         int a, lastmat= -1, need_orco = 0;
916
917         if(ob->flag & OB_FROMDUPLI) return;
918         initfastshade();
919
920         Mat4MulMat4(mat, ob->obmat, fviewmat);
921         
922         Mat4Invert(tmat, mat);
923         Mat3CpyMat4(imat, tmat);
924         if(ob->transflag & OB_NEG_SCALE) Mat3MulFloat((float *)imat, -1.0);
925         
926         /* we extract dl_verts, deform info */
927         dldeform= find_displist(&ob->disp, DL_VERTS);
928         if(dldeform) BLI_remlink(&ob->disp, dldeform);
929         
930         /* Metaballs have the standard displist in the Object */
931         if(ob->type!=OB_MBALL) freedisplist(&ob->disp);
932
933         need_orco= 0;
934         for(a=0; a<ob->totcol; a++) {
935                 ma= give_current_material(ob, a+1);
936                 if(ma) {
937                         init_render_material(ma);
938                         if(ma->ren->texco & TEXCO_ORCO) need_orco= 1;
939                 }
940         }
941
942         if(ob->type==OB_MESH) {
943                 
944                 me= ob->data;
945                 
946                 if (mesh_uses_displist(me)) {
947                         if (need_orco) {
948                                 make_orco_displist_mesh(ob, me->subdiv);
949                                 orco= me->orco;
950                         }
951
952                         dl= me->disp.first;
953                         while(dl) {
954                                 if(dl->type==DL_MESH && dl->mesh && dl->mesh->totvert) {
955                                         DispListMesh *dlm= dl->mesh;
956                                         float *vnors, *vn;
957                                         int i;
958                                         
959                                         dlob= MEM_callocN(sizeof(DispList), "displistshade");
960                                         BLI_addtail(&ob->disp, dlob);
961                                         dlob->type= DL_VERTCOL;
962                                 
963                                         dlob->col1= MEM_mallocN(sizeof(*dlob->col1)*dlm->totface*4, "col1");
964                                         if (me->flag & ME_TWOSIDED)
965                                                 dlob->col2= MEM_mallocN(sizeof(*dlob->col2)*dlm->totface*4, "col1");
966                                         
967                                         /* vertexnormals */
968                                         vn=vnors= MEM_mallocN(dlm->totvert*3*sizeof(float), "vnors disp");
969                                         mvert= dlm->mvert;
970                                         a= dlm->totvert;
971                                         while(a--) {
972                                                 
973                                                 xn= mvert->no[0]; 
974                                                 yn= mvert->no[1]; 
975                                                 zn= mvert->no[2];
976                                                 
977                                                 /* transpose ! */
978                                                 vn[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
979                                                 vn[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
980                                                 vn[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
981                                                 Normalise(vn);
982                                                 
983                                                 mvert++; vn+=3;
984                                         }               
985                         
986                                         for (i=0; i<dlm->totface; i++) {
987                                                 MFace *mf= &dlm->mface[i];
988
989                                                 if (mf->v3) {
990                                                         int j, vidx[4], nverts= mf->v4?4:3;
991                                                         unsigned int *col1base= &dlob->col1[i*4];
992                                                         unsigned int *col2base= dlob->col2?&dlob->col2[i*4]:NULL;
993                                                         MCol *mcolbase= dlm->mcol?&dlm->mcol[i*4]:NULL;
994                                                         float nor[3];
995                                                         
996                                                         ma= give_current_material(ob, mf->mat_nr+1);
997                                                         if(ma==0) ma= &defmaterial;
998                                                         
999                                                         vidx[0]= mf->v1;
1000                                                         vidx[1]= mf->v2;
1001                                                         vidx[2]= mf->v3;
1002                                                         vidx[3]= mf->v4;
1003
1004                                                         if (mf->v4)
1005                                                                 CalcNormFloat4(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, dlm->mvert[mf->v4].co, nor);
1006                                                         else
1007                                                                 CalcNormFloat(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, nor);
1008
1009                                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
1010                                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
1011                                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
1012                                                         Normalise(n1);
1013
1014                                                         for (j=0; j<nverts; j++) {
1015                                                                 MVert *mv= &dlm->mvert[vidx[j]];
1016                                                                 unsigned int *col1= &col1base[j];
1017                                                                 unsigned int *col2= col2base?&col2base[j]:NULL;
1018                                                                 MCol *mcol= mcolbase?&mcolbase[j]:NULL;
1019                                                                 
1020                                                                 VECCOPY(vec, mv->co);
1021                                                                 Mat4MulVecfl(mat, vec);
1022                                                                 if(mf->flag & ME_SMOOTH) vn= vnors+3*vidx[j];
1023                                                                 else vn= n1;
1024                                                         
1025                                                                 if (need_orco && orco)
1026                                                                         fastshade(vec, vn, &orco[vidx[j]*3], ma, (char *)col1, (char*)col2, (char*) mcol);
1027                                                                 else
1028                                                                         fastshade(vec, vn, mv->co, ma, (char *)col1, (char*)col2, (char*) mcol);
1029                                                         }
1030                                                 }
1031                                         }
1032                                         MEM_freeN(vnors);
1033                                 }
1034                                 dl= dl->next;
1035                         }
1036                         
1037                         if (need_orco && orco) {
1038                                 MEM_freeN(me->orco);
1039                                 me->orco= NULL;
1040                         }
1041                 }
1042                 else if(me->totvert>0) {
1043                         float *vnors, *vn;
1044                         
1045                         if(me->orco==0 && need_orco) {
1046                                 make_orco_mesh(me);
1047                         }
1048                         orco= me->orco;
1049                         /* ms= me->msticky; */
1050                         
1051                         dl= me->disp.first;
1052                         if(dl==0 || dl->nors==0) addnormalsDispList(ob, &me->disp);
1053                         dl= me->disp.first;
1054                         if(dl==0 || dl->nors==0) return;
1055                         nor= dl->nors;
1056                         
1057                         dl= MEM_callocN(sizeof(DispList), "displistshade");
1058                         BLI_addtail(&ob->disp, dl);
1059                         dl->type= DL_VERTCOL;
1060                         col1= dl->col1= MEM_mallocN(4*sizeof(int)*me->totface, "col1");
1061                         col2= 0;
1062                         if(me->tface) tface_to_mcol(me);
1063                         vertcol= (unsigned int *)me->mcol;
1064                         
1065                         if( me->flag & ME_TWOSIDED) {
1066                                 col2= dl->col2= MEM_mallocN(4*sizeof(int)*me->totface, "col2");
1067                         }
1068                         
1069                         /* vertexnormals */
1070                         vn=vnors= MEM_mallocN(me->totvert*3*sizeof(float), "vnors disp");
1071                         mvert= me->mvert;
1072                         a= me->totvert;
1073                         while(a--) {
1074                                 
1075                                 xn= mvert->no[0]; 
1076                                 yn= mvert->no[1]; 
1077                                 zn= mvert->no[2];
1078                                 
1079                                 /* transpose ! */
1080                                 vn[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
1081                                 vn[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
1082                                 vn[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
1083                                 Normalise(vn);
1084                                 
1085                                 mvert++; vn+=3;
1086                         }               
1087                         
1088                         mface= me->mface;
1089                         a= me->totface;
1090                         while(a--) {
1091                                 
1092                                 if(mface->v3) {
1093                                 
1094                                         /* transpose ! */
1095                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
1096                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
1097                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
1098                                         Normalise(n1);
1099                                         
1100                                         if(lastmat!=mface->mat_nr) {
1101                                                 ma= give_current_material(ob, mface->mat_nr+1);
1102                                                 if(ma==0) ma= &defmaterial;
1103                                                 lastmat= mface->mat_nr;
1104                                         }
1105                                         
1106                                         mvert= me->mvert+mface->v1;
1107                                         VECCOPY(vec, mvert->co);
1108                                         Mat4MulVecfl(mat, vec);
1109                                         if(mface->flag & ME_SMOOTH) vn= vnors+3*mface->v1;
1110                                         else vn= n1;
1111                                         
1112                                         if(orco)  fastshade(vec, vn, orco+3*mface->v1, ma, (char *)col1, (char *)col2, (char *)vertcol);
1113                                         else fastshade(vec, vn, mvert->co, ma, (char *)col1, (char *)col2, (char *)vertcol);
1114                                         col1++;
1115                                         if(vertcol) vertcol++; 
1116                                         if(col2) col2++;
1117                                         
1118                                         mvert= me->mvert+mface->v2;
1119                                         VECCOPY(vec, mvert->co);
1120                                         Mat4MulVecfl(mat, vec);
1121                                         if(mface->flag & ME_SMOOTH) vn= vnors+3*mface->v2;
1122                                         else vn= n1;
1123                                         
1124                                         if(orco)  fastshade(vec, vn, orco+3*mface->v2, ma, (char *)col1, (char *)col2, (char *)vertcol);
1125                                         else fastshade(vec, vn, mvert->co, ma, (char *)col1, (char *)col2, (char *)vertcol);
1126                                         col1++;
1127                                         if(vertcol) vertcol++; 
1128                                         if(col2) col2++;
1129                                         
1130                                         mvert= me->mvert+mface->v3;
1131                                         VECCOPY(vec, mvert->co);
1132                                         Mat4MulVecfl(mat, vec);
1133                                         if(mface->flag & ME_SMOOTH) vn= vnors+3*mface->v3;
1134                                         else vn= n1;
1135                                         
1136                                         if(orco)  fastshade(vec, vn, orco+3*mface->v3, ma, (char *)col1, (char *)col2, (char *)vertcol);
1137                                         else fastshade(vec, vn, mvert->co, ma, (char *)col1, (char *)col2, (char *)vertcol);
1138                                         col1++;
1139                                         if(vertcol) vertcol++; 
1140                                         if(col2) col2++;
1141                                         
1142                                         if(mface->v4) {
1143                                                 mvert= me->mvert+mface->v4;
1144                                                 VECCOPY(vec, mvert->co);
1145                                                 Mat4MulVecfl(mat, vec);
1146                                                 if(mface->flag & ME_SMOOTH) vn= vnors+3*mface->v4;
1147                                                 else vn= n1;
1148                                                 
1149                                                 if(orco)  fastshade(vec, vn, orco+3*mface->v4, ma, (char *)col1, (char *)col2, (char *)vertcol);
1150                                                 else fastshade(vec, vn, mvert->co, ma, (char *)col1, (char *)col2, (char *)vertcol);
1151                                         }
1152                                         col1++;
1153                                         if(vertcol) vertcol++; 
1154                                         if(col2) col2++;
1155                                                 
1156                                 }
1157                                 else {
1158                                         col1+=4;
1159                                         if(vertcol) vertcol+=4; 
1160                                         if(col2) col2+=4;
1161                                 }
1162         
1163                                 nor+= 3;
1164                                 mface++;
1165                         }
1166                         
1167                         MEM_freeN(vnors);
1168                         
1169                         if(me->orco) {
1170                                 MEM_freeN(me->orco);
1171                                 me->orco= 0;
1172                         }
1173                         if(me->tface) {
1174                                 MEM_freeN(me->mcol);
1175                                 me->mcol= 0;
1176                         }
1177                 }
1178         }
1179         else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) {
1180         
1181                 /* now we need the normals */
1182                 cu= ob->data;
1183                 dl= cu->disp.first;
1184                 
1185                 while(dl) {
1186                         dlob= MEM_callocN(sizeof(DispList), "displistshade");
1187                         BLI_addtail(&ob->disp, dlob);
1188                         dlob->type= DL_VERTCOL;
1189                         dlob->parts= dl->parts;
1190                         dlob->nr= dl->nr;
1191                         
1192                         if(dl->type==DL_INDEX3) {
1193                                 col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
1194                         }
1195                         else {
1196                                 col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->parts*dl->nr, "col1");
1197                         }
1198                         
1199                 
1200                         ma= give_current_material(ob, dl->col+1);
1201                         if(ma==0) ma= &defmaterial;
1202
1203                         if(dl->type==DL_INDEX3) {
1204                                 if(dl->nors) {
1205                                         /* there's just one normal */
1206                                         n1[0]= imat[0][0]*dl->nors[0]+imat[0][1]*dl->nors[1]+imat[0][2]*dl->nors[2];
1207                                         n1[1]= imat[1][0]*dl->nors[0]+imat[1][1]*dl->nors[1]+imat[1][2]*dl->nors[2];
1208                                         n1[2]= imat[2][0]*dl->nors[0]+imat[2][1]*dl->nors[1]+imat[2][2]*dl->nors[2];
1209                                         Normalise(n1);
1210                                         
1211                                         fp= dl->verts;
1212                                         
1213                                         a= dl->nr;              
1214                                         while(a--) {
1215                                                 VECCOPY(vec, fp);
1216                                                 Mat4MulVecfl(mat, vec);
1217                                                 
1218                                                 fastshade(vec, n1, fp, ma, (char *)col1, 0, 0);
1219                                                 
1220                                                 fp+= 3; col1++;
1221                                         }
1222                                 }
1223                         }
1224                         else if(dl->type==DL_SURF) {
1225                                 if(dl->nors) {
1226                                         a= dl->nr*dl->parts;
1227                                         fp= dl->verts;
1228                                         nor= dl->nors;
1229                                         
1230                                         while(a--) {
1231                                                 VECCOPY(vec, fp);
1232                                                 Mat4MulVecfl(mat, vec);
1233                                                 
1234                                                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
1235                                                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
1236                                                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
1237                                                 Normalise(n1);
1238                         
1239                                                 fastshade(vec, n1, fp, ma, (char *)col1, 0, 0);
1240                                                 
1241                                                 fp+= 3; nor+= 3; col1++;
1242                                         }
1243                                 }
1244                         }
1245                         dl= dl->next;
1246                 }
1247         }
1248         else if(ob->type==OB_MBALL) {
1249                 /* there are normals already */
1250                 dl= ob->disp.first;
1251                 
1252                 while(dl) {
1253                         
1254                         if(dl->type==DL_INDEX4) {
1255                                 if(dl->nors) {
1256                                         
1257                                         if(dl->col1) MEM_freeN(dl->col1);
1258                                         col1= dl->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
1259                         
1260                                         ma= give_current_material(ob, dl->col+1);
1261                                         if(ma==0) ma= &defmaterial;
1262         
1263                                         fp= dl->verts;
1264                                         nor= dl->nors;
1265                                         
1266                                         a= dl->nr;              
1267                                         while(a--) {
1268                                                 VECCOPY(vec, fp);
1269                                                 Mat4MulVecfl(mat, vec);
1270                                                 
1271                                                 /* transpose ! */
1272                                                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
1273                                                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
1274                                                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
1275                                                 Normalise(n1);
1276                                         
1277                                                 fastshade(vec, n1, fp, ma, (char *)col1, 0, 0);
1278                                                 
1279                                                 fp+= 3; col1++; nor+= 3;
1280                                         }
1281                                 }
1282                         }
1283                         dl= dl->next;
1284                 }
1285         }
1286         
1287         for(a=0; a<ob->totcol; a++) {
1288                 ma= give_current_material(ob, a+1);
1289                 if(ma) end_render_material(ma);
1290         }
1291
1292         /* this one was temporally removed */
1293         if(dldeform) BLI_addtail(&ob->disp, dldeform);
1294 }
1295
1296 void reshadeall_displist(void)
1297 {
1298         DispList *dldeform;
1299         Base *base;
1300         Object *ob;
1301         
1302         freefastshade();
1303         
1304         base= G.scene->base.first;
1305         while(base) {
1306                 if(base->lay & G.scene->lay) {
1307                         
1308                         ob= base->object;
1309                         
1310                         /* we extract dl_verts, deform info */
1311                         dldeform= find_displist(&ob->disp, DL_VERTS);
1312                         if(dldeform) BLI_remlink(&ob->disp, dldeform);
1313                         
1314                         /* Metaballs have standard displist at the Object */
1315                         if(ob->type==OB_MBALL) shadeDispList(ob);
1316                         else freedisplist(&ob->disp);
1317                         
1318                         if(dldeform) BLI_addtail(&ob->disp, dldeform);
1319                 }
1320                 base= base->next;
1321         }
1322 }
1323
1324 void count_displist(ListBase *lb, int *totvert, int *totface)
1325 {
1326         DispList *dl;
1327         
1328         dl= lb->first;
1329         while(dl) {
1330                 
1331                 switch(dl->type) {
1332                 case DL_SURF:
1333                         *totvert+= dl->nr*dl->parts;
1334                         *totface+= (dl->nr-1)*(dl->parts-1);
1335                         break;
1336                 case DL_INDEX3:
1337                 case DL_INDEX4:
1338                         *totvert+= dl->nr;
1339                         *totface+= dl->parts;
1340                         break;
1341                 case DL_POLY:
1342                 case DL_SEGM:
1343                         *totvert+= dl->nr*dl->parts;
1344                 }
1345                 
1346                 dl= dl->next;
1347         }
1348 }
1349
1350 static void curve_to_displist(ListBase *nubase, ListBase *dispbase)
1351 {
1352         Nurb *nu;
1353         DispList *dl;
1354         BezTriple *bezt, *prevbezt;
1355         BPoint *bp;
1356         float *data, *v1, *v2;
1357         int a, len;
1358         
1359         nu= nubase->first;
1360         
1361         while(nu) {
1362                 if(nu->hide==0) {
1363                         if((nu->type & 7)==CU_BEZIER) {
1364                                 
1365                                 /* count */
1366                                 len= 0;
1367                                 a= nu->pntsu-1;
1368                                 if(nu->flagu & 1) a++;
1369
1370                                 prevbezt= nu->bezt;
1371                                 bezt= prevbezt+1;
1372                                 while(a--) {
1373                                         if(a==0 && (nu->flagu & 1)) bezt= nu->bezt;
1374                                         
1375                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) len++;
1376                                         else len+= nu->resolu;
1377                                         
1378                                         if(a==0 && (nu->flagu & 1)==0) len++;
1379                                         
1380                                         prevbezt= bezt;
1381                                         bezt++;
1382                                 }
1383                                 
1384                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbez");
1385                                 /* len+1 because of 'maakbez' function */
1386                                 dl->verts= MEM_callocN( (len+1)*3*sizeof(float), "dlverts");
1387                                 BLI_addtail(dispbase, dl);
1388                                 dl->parts= 1;
1389                                 dl->nr= len;
1390                                 dl->col= nu->mat_nr;
1391
1392                                 data= dl->verts;
1393
1394                                 if(nu->flagu & 1) {
1395                                         dl->type= DL_POLY;
1396                                         a= nu->pntsu;
1397                                 }
1398                                 else {
1399                                         dl->type= DL_SEGM;
1400                                         a= nu->pntsu-1;
1401                                 }
1402                                 
1403                                 prevbezt= nu->bezt;
1404                                 bezt= prevbezt+1;
1405                                 
1406                                 while(a--) {
1407                                         if(a==0 && dl->type== DL_POLY) bezt= nu->bezt;
1408                                         
1409                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
1410                                                 VECCOPY(data, prevbezt->vec[1]);
1411                                                 data+= 3;
1412                                         }
1413                                         else {
1414                                                 v1= prevbezt->vec[1];
1415                                                 v2= bezt->vec[0];
1416                                                 maakbez(v1[0], v1[3], v2[0], v2[3], data, nu->resolu);
1417                                                 maakbez(v1[1], v1[4], v2[1], v2[4], data+1, nu->resolu);
1418                                                 if((nu->type & 8)==0)
1419                                                         maakbez(v1[2], v1[5], v2[2], v2[5], data+2, nu->resolu);
1420                                                 data+= 3*nu->resolu;
1421                                         }
1422                                         
1423                                         if(a==0 && dl->type==DL_SEGM) {
1424                                                 VECCOPY(data, bezt->vec[1]);
1425                                         }
1426                                         
1427                                         prevbezt= bezt;
1428                                         bezt++;
1429                                 }
1430                         }
1431                         else if((nu->type & 7)==CU_NURBS) {
1432                                 len= nu->pntsu*nu->resolu;
1433                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1434                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1435                                 BLI_addtail(dispbase, dl);
1436                                 dl->parts= 1;
1437                                 dl->nr= len;
1438                                 dl->col= nu->mat_nr;
1439
1440                                 data= dl->verts;
1441                                 if(nu->flagu & 1) dl->type= DL_POLY;
1442                                 else dl->type= DL_SEGM;
1443                                 makeNurbcurve(nu, data, 3);
1444                         }
1445                         else if((nu->type & 7)==CU_POLY) {
1446                                 len= nu->pntsu;
1447                                 dl= MEM_callocN(sizeof(DispList), "makeDispListpoly");
1448                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1449                                 BLI_addtail(dispbase, dl);
1450                                 dl->parts= 1;
1451                                 dl->nr= len;
1452                                 dl->col= nu->mat_nr;
1453
1454                                 data= dl->verts;
1455                                 if(nu->flagu & 1) dl->type= DL_POLY;
1456                                 else dl->type= DL_SEGM;
1457                                 
1458                                 a= len;
1459                                 bp= nu->bp;
1460                                 while(a--) {
1461                                         VECCOPY(data, bp->vec);
1462                                         bp++;
1463                                         data+= 3;
1464                                 }
1465                         }
1466                 }
1467                 nu= nu->next;
1468         }
1469 }
1470
1471
1472 static void filldisplist(ListBase *dispbase, ListBase *to)
1473 {
1474         EditVert *eve, *v1, *vlast;
1475         EditFace *evl;
1476         DispList *dlnew=0, *dl;
1477         float *f1;
1478         int colnr=0, cont=1, tot, a, *index;
1479         long totvert;
1480         
1481         if(dispbase==0) return;
1482         if(dispbase->first==0) return;
1483
1484         /* tijd= clock(); */
1485         /* bit-wise and comes after == .... so this doesn't work...  */
1486 /*      if(G.f & G_PLAYANIM == 0) waitcursor(1); */
1487         if( !(G.f & G_PLAYANIM) ) waitcursor(1);
1488
1489         while(cont) {
1490                 cont= 0;
1491                 totvert=0;
1492                 
1493                 dl= dispbase->first;
1494                 while(dl) {
1495         
1496                         if(dl->type==DL_POLY) {
1497                                 if(colnr<dl->col) cont= 1;
1498                                 else if(colnr==dl->col) {
1499                         
1500                                         colnr= dl->col;
1501                 
1502                                         /* make editverts and edges */
1503                                         f1= dl->verts;
1504                                         a= dl->nr;
1505                                         eve= v1= 0;
1506                                         
1507                                         while(a--) {
1508                                                 vlast= eve;
1509                                                 
1510                                                 eve= BLI_addfillvert(f1);
1511                                                 totvert++;
1512                                                 
1513                                                 if(vlast==0) v1= eve;
1514                                                 else {
1515                                                         BLI_addfilledge(vlast, eve);
1516                                                 }
1517                                                 f1+=3;
1518                                         }
1519                                 
1520                                         if(eve!=0 && v1!=0) {
1521                                                 BLI_addfilledge(eve, v1);
1522                                         }
1523                                 }
1524                         }
1525                         dl= dl->next;
1526                 }
1527                 
1528                 /* to make edgefill work 
1529                 G.obedit can be 0 on file load */
1530                 if (G.obedit) {
1531                         BLI_setScanFillObjectRef(G.obedit);
1532                         BLI_setScanFillColourRef(&G.obedit->actcol);
1533                 }
1534
1535                 if(totvert && BLI_edgefill(0)!=0) {
1536
1537                         /* count faces  */
1538                         tot= 0;
1539                         evl= fillfacebase.first;
1540                         while(evl) {
1541                                 tot++;
1542                                 evl= evl->next;
1543                         }
1544
1545                         if(tot) {
1546                                 dlnew= MEM_callocN(sizeof(DispList), "filldisplist");
1547                                 dlnew->type= DL_INDEX3;
1548                                 dlnew->col= colnr;
1549                                 dlnew->nr= totvert;
1550                                 dlnew->parts= tot;
1551
1552                                 dlnew->index= MEM_mallocN(tot*3*sizeof(int), "dlindex");
1553                                 dlnew->verts= MEM_mallocN(totvert*3*sizeof(float), "dlverts");
1554                                 
1555                                 /* vert data */
1556                                 f1= dlnew->verts;
1557                                 totvert= 0;
1558                                 eve= fillvertbase.first;
1559                                 while(eve) {
1560                                         VECCOPY(f1, eve->co);
1561                                         f1+= 3;
1562         
1563                                         /* index number */
1564                                         eve->vn= (EditVert *)totvert;
1565                                         totvert++;
1566                                         
1567                                         eve= eve->next;
1568                                 }
1569                                 
1570                                 /* index data */
1571                                 evl= fillfacebase.first;
1572                                 index= dlnew->index;
1573                                 while(evl) {
1574                                         index[0]= (long)evl->v1->vn;
1575                                         index[1]= (long)evl->v2->vn;
1576                                         index[2]= (long)evl->v3->vn;
1577                                         
1578                                         index+= 3;
1579                                         evl= evl->next;
1580                                 }
1581                         }
1582
1583                         BLI_addhead(to, dlnew);
1584                         
1585                 }
1586                 BLI_end_edgefill();
1587
1588                 colnr++;
1589         }
1590         
1591         /* do not free polys, needed for wireframe display */
1592         
1593         /* same as above ... */
1594 /*      if(G.f & G_PLAYANIM == 0) waitcursor(0); */
1595         if( !(G.f & G_PLAYANIM) ) waitcursor(0);
1596         /* printf("time: %d\n",(clock()-tijd)/1000); */
1597
1598 }
1599
1600 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
1601 {
1602         ListBase front, back;
1603         DispList *dl, *dlnew;
1604         float *fp, *fp1;
1605         int a, dpoly;
1606         
1607         front.first= front.last= back.first= back.last= 0;
1608         if(cu->flag & CU_3D) return;
1609         if( (cu->flag & (CU_FRONT+CU_BACK))==0 ) return;
1610         
1611         dl= dispbase->first;
1612         while(dl) {
1613                 if(dl->type==DL_SURF) {
1614                         if( (dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U)==0 ) {
1615                                 if( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE) ) {
1616                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1617                                         BLI_addtail(&front, dlnew);
1618                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1619                                         dlnew->nr= dl->parts;
1620                                         dlnew->parts= 1;
1621                                         dlnew->type= DL_POLY;
1622                                         dlnew->col= dl->col;
1623                                         
1624                                         fp= dl->verts;
1625                                         dpoly= 3*dl->nr;
1626                                         
1627                                         a= dl->parts;
1628                                         while(a--) {
1629                                                 VECCOPY(fp1, fp);
1630                                                 fp1+= 3;
1631                                                 fp+= dpoly;
1632                                         }
1633                                 }
1634                                 if( (cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE) ) {
1635                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1636                                         BLI_addtail(&back, dlnew);
1637                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1638                                         dlnew->nr= dl->parts;
1639                                         dlnew->parts= 1;
1640                                         dlnew->type= DL_POLY;
1641                                         dlnew->col= dl->col;
1642                                         
1643                                         fp= dl->verts+3*(dl->nr-1);
1644                                         dpoly= 3*dl->nr;
1645                                         
1646                                         a= dl->parts;
1647                                         while(a--) {
1648                                                 VECCOPY(fp1, fp);
1649                                                 fp1+= 3;
1650                                                 fp+= dpoly;
1651                                         }
1652                                 }
1653                         }
1654                 }
1655                 dl= dl->next;
1656         }
1657
1658         filldisplist(&front, dispbase);
1659         filldisplist(&back, dispbase);
1660         
1661         freedisplist(&front);
1662         freedisplist(&back);
1663
1664         filldisplist(dispbase, dispbase);
1665         
1666 }
1667
1668 void curve_to_filledpoly(Curve *cu, ListBase *nurb, ListBase *dispbase)
1669 {
1670         DispList *dl;
1671         Nurb *nu;
1672                 
1673         dl= dispbase->first;
1674         
1675         if(cu->flag & CU_3D) return;
1676         
1677         nu= nurb->first;
1678         while(nu) {
1679                 if(nu->flagu & CU_CYCLIC) break;
1680                 nu= nu->next;
1681         }
1682         if(nu==0) return;
1683
1684         if(dl->type==DL_SURF) bevels_to_filledpoly(cu, dispbase);
1685         else {
1686                 if(cu->flag & CU_FRONT) filldisplist(dispbase, dispbase);
1687         }
1688 }
1689
1690
1691 static int dl_onlyzero= 0;
1692
1693 void set_displist_onlyzero(int val)
1694 {
1695         dl_onlyzero= val;
1696 }
1697
1698 /* taper rules:
1699   - only 1 curve
1700   - first point left, last point right
1701   - based on subdivided points in original curve, not on points in taper curve (still)
1702 */
1703 float calc_taper(Object *taperobj, int cur, int tot)
1704 {
1705         Curve *cu;
1706         DispList *dl;
1707         
1708         if(taperobj==NULL) return 1.0;
1709         
1710         cu= taperobj->data;
1711         dl= cu->disp.first;
1712         if(dl==NULL) {
1713                 makeDispList(taperobj);
1714                 dl= cu->disp.first;
1715         }
1716         if(dl) {
1717                 float fac= ((float)cur)/(float)(tot-1);
1718                 float minx, dx, *fp;
1719                 int a;
1720                 
1721                 /* horizontal size */
1722                 minx= dl->verts[0];
1723                 dx= dl->verts[3*(dl->nr-1)] - minx;
1724                 if(dx>0.0) {
1725                 
1726                         fp= dl->verts;
1727                         for(a=0; a<dl->nr; a++, fp+=3) {
1728                                 if( (fp[0]-minx)/dx >= fac) {
1729                                         /* interpolate with prev */
1730                                         if(a>0) {
1731                                                 float fac1= (fp[-3]-minx)/dx;
1732                                                 float fac2= (fp[0]-minx)/dx;
1733                                                 if(fac1!=fac2)
1734                                                         return fp[1]*(fac1-fac)/(fac1-fac2) + fp[-2]*(fac-fac2)/(fac1-fac2);
1735                                         }
1736                                         return fp[1];
1737                                 }
1738                         }
1739                         return fp[-2];  // last y coord
1740                 }
1741         }
1742         
1743         return 1.0;
1744 }
1745
1746 void makeDispList(Object *ob)
1747 {
1748         EditMesh *em = G.editMesh;
1749         Mesh *me;
1750         Nurb *nu;
1751         Curve *cu;
1752         BPoint *bp;
1753         ListBase dlbev, *dispbase;
1754         DispList *dl, *dlb;
1755         BevList *bl;
1756         BevPoint *bevp;
1757         float *data, *fp1, widfac, vec[3];
1758         int len, a, b, draw=0;
1759
1760         if(ob==NULL) return;
1761         if(ob->flag & OB_FROMDUPLI) return;
1762
1763         freedisplist(&(ob->disp));
1764         
1765         if(ob->type==OB_MESH) {
1766                 me= ob->data;
1767                 freedisplist(&(me->disp));
1768
1769                 tex_space_mesh(ob->data);
1770                 
1771                 if(ob!=G.obedit) {
1772                         mesh_modifier(ob, 's');
1773                 }
1774                 
1775                 if (mesh_uses_displist(me)) {  /* subsurf */
1776                         DispListMesh *dlm;
1777
1778                         if (ob==G.obedit) {
1779                                 dlm= subsurf_make_dispListMesh_from_editmesh(em, me->subdiv, me->flag, me->subsurftype);
1780                         } else {
1781                                 DispList *dlVerts= find_displist(&ob->disp, DL_VERTS);
1782                                 // dl->verts should not be needed anymore, recode it in modifier (ton)
1783                                 dlm= subsurf_make_dispListMesh_from_mesh(me, dlVerts?dlVerts->verts:NULL, 
1784                                                                                                         me->subdiv, me->flag);
1785                         }
1786
1787                         dl= MEM_callocN(sizeof(*dl), "dl");
1788                         dl->type= DL_MESH;
1789                         dl->mesh= dlm;
1790
1791                         free_displist_by_type(&me->disp, DL_MESH);
1792                         BLI_addtail(&me->disp, dl);
1793                 }
1794                 
1795                 if(ob!=G.obedit) mesh_modifier(ob, 'e');
1796                 
1797         }
1798         else if(ob->type==OB_MBALL) {
1799                 ob= find_basis_mball(ob);
1800
1801                 metaball_polygonize(ob);
1802                 tex_space_mball(ob);
1803
1804                 object_deform(ob);
1805         }
1806         else if(ob->type==OB_SURF) {
1807                 
1808                 draw= ob->dt;
1809                 cu= ob->data;
1810                 dispbase= &(cu->disp);
1811                 if(dl_onlyzero && dispbase->first) return;
1812                 freedisplist(dispbase);
1813                 
1814                 if(ob==G.obedit) nu= editNurb.first;
1815                 else {
1816                         curve_modifier(ob, 's');
1817                         nu= cu->nurb.first;
1818                 }
1819                 
1820                 while(nu) {
1821                         if(nu->hide==0) {
1822                                 if(nu->pntsv==1) {
1823                                         if(draw==0) len= nu->pntsu;
1824                                         else len= nu->pntsu*nu->resolu;
1825                                         
1826                                         dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1827                                         dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1828                                         
1829                                         BLI_addtail(dispbase, dl);
1830                                         dl->parts= 1;
1831                                         dl->nr= len;
1832                                         dl->col= nu->mat_nr;
1833                                         dl->rt= nu->flag;
1834                                         
1835                                         data= dl->verts;
1836                                         if(nu->flagu & 1) dl->type= DL_POLY;
1837                                         else dl->type= DL_SEGM;
1838                                         
1839                                         if(draw==0) {
1840                                                 bp= nu->bp;
1841                                                 while(len--) {
1842                                                         VECCOPY(data, bp->vec);
1843                                                         bp++;
1844                                                         data+= 3;
1845                                                 }
1846                                         }
1847                                         else makeNurbcurve(nu, data, 3);
1848                                 }
1849                                 else {
1850                                         if(draw==0 && ob==G.obedit) ;
1851                                         else {
1852                                                 if(draw==0) len= nu->pntsu*nu->pntsv;
1853                                                 else len= nu->resolu*nu->resolv;
1854                                                 
1855                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1856                                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1857                                                 BLI_addtail(dispbase, dl);
1858         
1859                                                 if(draw==0) {
1860                                                         dl->parts= nu->pntsv;
1861                                                         dl->nr= nu->pntsu;
1862                                                         if(nu->flagu & 1) dl->flag|= DL_CYCL_U;
1863                                                         if(nu->flagv & 1) dl->flag|= DL_CYCL_V;
1864                                                 }
1865                                                 else {
1866                                                         dl->parts= nu->resolu;  /* in reverse, because makeNurbfaces works that way */
1867                                                         dl->nr= nu->resolv;
1868                                                         if(nu->flagv & 1) dl->flag|= DL_CYCL_U; /* reverse too! */
1869                                                         if(nu->flagu & 1) dl->flag|= DL_CYCL_V;
1870                                                 }
1871                                                 dl->col= nu->mat_nr;
1872                                                 dl->rt= nu->flag;
1873                                                 
1874                                                 data= dl->verts;
1875                                                 dl->type= DL_SURF;
1876                                                 
1877                                                 if(draw==0) {
1878                                                         bp= nu->bp;
1879                                                         while(len--) {
1880                                                                 VECCOPY(data, bp->vec);
1881                                                                 bp++;
1882                                                                 data+= 3;
1883                                                         }
1884                                                 }
1885                                                 else makeNurbfaces(nu, data);
1886                                         }
1887                                 }
1888                         }
1889                         nu= nu->next;
1890                 }
1891                 
1892                 tex_space_curve(cu);
1893
1894                 if(ob!=G.obedit) curve_modifier(ob, 'e');
1895                 if(ob!=G.obedit) object_deform(ob);
1896         }
1897         else if ELEM(ob->type, OB_CURVE, OB_FONT) {
1898                 
1899                 draw= ob->dt;
1900                 cu= ob->data;
1901                 dispbase= &(cu->disp);
1902                 if(dl_onlyzero && dispbase->first) return;
1903                 freedisplist(dispbase);
1904                 
1905                 if(cu->path) free_path(cu->path);
1906                 cu->path= 0;
1907                 
1908                 BLI_freelistN(&(cu->bev));
1909                 
1910                 if(ob!=G.obedit) curve_modifier(ob, 's');
1911                 
1912                 if(ob==G.obedit) {
1913                         if(ob->type==OB_CURVE) curve_to_displist(&editNurb, dispbase);
1914                         else curve_to_displist(&cu->nurb, dispbase);
1915                         if(cu->flag & CU_PATH) makeBevelList(ob);
1916                 }
1917                 else if(cu->ext1==0.0 && cu->ext2==0.0 && cu->bevobj==NULL && cu->width==1.0) {
1918                         curve_to_displist(&cu->nurb, dispbase);
1919                         if(cu->flag & CU_PATH) makeBevelList(ob);
1920                 }
1921                 else {
1922                         
1923                         makeBevelList(ob);
1924
1925                         dlbev.first= dlbev.last= NULL;
1926                         if(cu->ext1!=0.0 || cu->ext2!=0.0 || cu->bevobj) {
1927                                 if(ob->dt!=0) makebevelcurve(ob, &dlbev);
1928                         }
1929
1930                         /* work with bevellist */
1931                         widfac= cu->width-1.0;
1932                         bl= cu->bev.first;
1933                         nu= cu->nurb.first;
1934                         while(bl) {
1935                                 if(dlbev.first==0) {
1936                                         dl= MEM_callocN(sizeof(DispList), "makeDispListbev");
1937                                         dl->verts= MEM_callocN(3*sizeof(float)*bl->nr, "dlverts");
1938                                         BLI_addtail(dispbase, dl);
1939                                         
1940                                         if(bl->poly!= -1) dl->type= DL_POLY;
1941                                         else dl->type= DL_SEGM;
1942                                         
1943                                         if(dl->type==DL_SEGM) dl->flag = (DL_FRONT_CURVE|DL_BACK_CURVE);
1944                                         
1945                                         dl->parts= 1;
1946                                         dl->nr= bl->nr;
1947                                         dl->col= nu->mat_nr;
1948
1949                                         a= dl->nr;
1950                                         bevp= (BevPoint *)(bl+1);
1951                                         data= dl->verts;
1952                                         while(a--) {
1953                                                 data[0]= bevp->x+widfac*bevp->sina;
1954                                                 data[1]= bevp->y+widfac*bevp->cosa;
1955                                                 data[2]= bevp->z;
1956                                                 bevp++;
1957                                                 data+=3;
1958                                         }
1959                                 }
1960                                 else {
1961                                         /* for each part of the bevel use a separate displblock */
1962                                         dlb= dlbev.first;
1963                                         while(dlb) {
1964                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1965                                                 dl->verts= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1966                                                 BLI_addtail(dispbase, dl);
1967                                                 /* dl->type= dlb->type; */
1968
1969                                                 dl->type= DL_SURF;
1970                                                 
1971                                                 dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1972                                                 if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1973                                                 if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1974                                                 
1975                                                 dl->parts= bl->nr;
1976                                                 dl->nr= dlb->nr;
1977                                                 dl->col= nu->mat_nr;
1978                                                 dl->rt= nu->flag;
1979
1980                                                 data= dl->verts;
1981                                                 bevp= (BevPoint *)(bl+1);
1982                                                 for(a=0; a<bl->nr; a++) {       /* for each point of poly make a bevel piece */
1983                                                         float fac;
1984                                                         
1985                                                         /* returns 1.0 if no taper, of course */
1986                                                         fac= calc_taper(cu->taperobj, a, bl->nr);
1987                                                         
1988                                                         /* rotate bevel piece and write in data */
1989                                                         fp1= dlb->verts;
1990                                                         b= dlb->nr;
1991
1992                                                         while(b--) {
1993                                                                 
1994                                                                 if(cu->flag & CU_3D) {
1995                                                                 
1996                                                                         vec[0]= fp1[1]+widfac;
1997                                                                         vec[1]= fp1[2];
1998                                                                         vec[2]= 0.0;
1999                                                                         
2000                                                                         Mat3MulVecfl(bevp->mat, vec);
2001                                                                         
2002                                                                         data[0]= bevp->x+ fac*vec[0];
2003                                                                         data[1]= bevp->y+ fac*vec[1];
2004                                                                         data[2]= bevp->z+ fac*vec[2];
2005                                                                 }
2006                                                                 else {
2007                                                                         data[0]= bevp->x+ fac*(fp1[1]+widfac)*bevp->sina;
2008                                                                         data[1]= bevp->y+ fac*(fp1[1]+widfac)*bevp->cosa;
2009                                                                         data[2]= bevp->z+ fac*fp1[2];
2010                                                                 }
2011
2012                                                                 data+=3;
2013                                                                 fp1+=3;
2014                                                         }
2015
2016                                                         bevp++;
2017                                                 }
2018
2019                                                 dlb= dlb->next;
2020                                         }
2021                                 }
2022
2023                                 bl= bl->next;
2024                                 nu= nu->next;
2025                         }
2026
2027                         if(cu->ext1!=0.0 || cu->ext2!=0.0 || cu->bevobj) {
2028                                 freedisplist(&dlbev);
2029                         }
2030                 }
2031
2032                 if(ob!=G.obedit) curve_modifier(ob, 'e');
2033                 if(ob!=G.obedit) object_deform(ob);
2034
2035                 tex_space_curve(cu);
2036
2037         }
2038         
2039         boundbox_displist(ob);
2040 }
2041
2042
2043 /*******************************/
2044 /*****       OUTLINE       *****/
2045 /*******************************/
2046
2047 typedef struct Sample{
2048         short x, y;
2049 } Sample;
2050
2051 typedef struct Segment{
2052         /* coordinates */
2053         struct Segment * next, * prev;
2054         float co[2];
2055 } Segment;
2056
2057
2058
2059 static int dflt_in_out(struct ImBuf * ibuf, int x, int y)
2060 {
2061         unsigned char * rect;
2062         
2063         if (ibuf == 0) return (0);
2064         if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y || ibuf->rect == 0) return (-1);
2065         
2066         rect = (unsigned char *) (ibuf->rect + (y * ibuf->x) + x);
2067         if (rect[0] > 0x81) return (1);
2068         return(0);
2069 }
2070
2071
2072 static Sample * outline(struct ImBuf * ibuf,
2073                                  int (*in_or_out)(struct ImBuf *, int, int))
2074 {
2075         static int dirs[8][2] = {
2076                 {-1,  0}, {-1,  1},     {0,  1}, {1,  1}, 
2077                 {1,  0}, {1, -1}, {0, -1}, {-1, -1}
2078         };
2079         
2080         int dir, x, y, in, i;
2081         int count, sampcount;
2082         int startx = 0, starty = 0;
2083         Sample * samp, * oldsamp;
2084         
2085         /* input:
2086          * 1 - image 
2087          * 2 - pointer to function that defines which pixel 'in' or 'out' is
2088          */
2089         
2090         if (ibuf == 0) return (0);
2091         if (ibuf->rect == 0) return (0);
2092         
2093         if (in_or_out == 0) in_or_out = dflt_in_out;
2094         in = in_or_out(ibuf, 0, 0);
2095         
2096         /* search for first transition, and continue from there */      
2097         for (y = 0; y < ibuf->y; y++) {
2098                 for (x = 0; x < ibuf->x; x++) {
2099                         if (in_or_out(ibuf, x, y) != in) {
2100                                 /* found first 'other' point !! */
2101                                 
2102                                 if (x != startx) dir = 0;
2103                                 else dir = 6;
2104                                 
2105                                 startx = x; starty = y;
2106                                 count = 1;
2107                                 sampcount = 2000;
2108                                 samp = MEM_mallocN(sampcount * sizeof(Sample), "wire_samples");
2109                                 
2110                                 do{
2111                                         samp[count].x = x; samp[count].y = y;
2112                                         count++;
2113                                         
2114                                         if (count >= sampcount) {
2115                                                 oldsamp = samp;
2116                                                 samp = MEM_mallocN(2 * sampcount * sizeof(Sample), "wire_samples");
2117                                                 memcpy(samp, oldsamp, sampcount * sizeof(Sample));
2118                                                 sampcount *= 2;
2119                                                 MEM_freeN(oldsamp);
2120                                         }
2121                                         
2122                                         i = 0;
2123                                         while(in_or_out(ibuf, x + dirs[dir][0], y + dirs[dir][1]) == in) {
2124                                                 dir = (dir + 1) & 0x7;
2125                                                 if (i++ == 9) break;
2126                                         }
2127                                         
2128                                         if (i >= 8) {
2129                                                 /* this has to be a loose point */
2130                                                 break;
2131                                         }
2132                                         
2133                                         x += dirs[dir][0];
2134                                         y += dirs[dir][1];
2135                                         dir = (dir - 3) & 0x7;
2136                                 } while(x != startx || y != starty);
2137                                 
2138                                 if (i >= 8) {
2139                                         /* patch for loose points */
2140                                         MEM_freeN(samp);
2141                                 } else {
2142                                         count = count - 1;
2143                                         samp[0].x = count >> 16;
2144                                         samp[0].y = count;
2145                                         return(samp);
2146                                 }
2147                         }
2148                 }
2149         }
2150         /* printf("no transition \n"); */
2151         return(0);
2152 }
2153
2154
2155
2156 /*******************************/
2157 /*****      WIREFRAME      *****/
2158 /*******************************/
2159
2160
2161 static float DistToLine2D(short *v1, short *v2, short *v3)   /* using Hesse formula :NO LINE PIECE! */
2162 {
2163         float a[2],deler;
2164
2165         a[0] = v2[1]-v3[1];
2166         a[1] = v3[0]-v2[0];
2167         deler = sqrt(a[0]*a[0]+a[1]*a[1]);
2168         if(deler == 0.0) return 0;
2169
2170         return fabs((v1[0]-v2[0])*a[0]+(v1[1]-v2[1])*a[1])/deler;
2171
2172 }
2173
2174 static float ComputeMaxShpError(Sample *samp, int first, int last, int *splitPoint)
2175     /* samp:  Array of digitized points */
2176     /* first, last:  Indices defining region    */
2177     /* splitpoint:  Point of maximum error      */
2178 {
2179     int         i;
2180     float       maxDist;                                /*  Maximum error               */
2181     float       dist;                                   /*  Current error               */
2182  
2183     *splitPoint = (last - first + 1) / 2;
2184     maxDist = 0.0;
2185         
2186     for (i = first + 1; i < last; i++) {                                
2187                 dist = DistToLine2D((short *)(samp+i), (short *)(samp+first), (short *)(samp+last));
2188
2189                 if (dist >= maxDist) {
2190                 maxDist = dist;
2191                 *splitPoint = i;
2192                 }
2193     }
2194
2195     return (maxDist);
2196 }
2197
2198
2199 static void FitPoly(Sample *samp, int first, int last, float shperr, ListBase *seglist)
2200     /* Samp: Array of digitized points */
2201     /* first,last: Indices of first and last pts in region */
2202     /* spherr: User-defined error squared          */
2203 {
2204     Segment     * seg;                          /* Control points segment*/
2205     float       maxError;                       /*  Maximum fitting error        */
2206     int         splitPoint;                     /*  Point to split point set at  */
2207     int         nPts;                           /*  Number of points in subset  */
2208         
2209     nPts = last - first + 1;
2210
2211     /*  Use heuristic if region only has two points in it */
2212
2213         seg = MEM_mallocN(sizeof(Segment), "wure_segment");
2214
2215         seg->co[0] = samp[first].x;
2216         seg->co[1] = samp[first].y;
2217         
2218     if (nPts == 2) {
2219                 BLI_addtail(seglist, seg);
2220                 return;
2221     }
2222
2223         maxError = ComputeMaxShpError(samp, first, last, &splitPoint);
2224         if (maxError < shperr) {
2225                 BLI_addtail(seglist, seg);
2226                 return;
2227         }
2228         
2229     /* Fitting failed -- split at max error point and fit recursively */
2230         
2231     FitPoly(samp, first, splitPoint, shperr, seglist);
2232     FitPoly(samp, splitPoint, last, shperr, seglist);
2233         
2234         MEM_freeN(seg);
2235 }
2236
2237
2238 static void ibuf2wire(ListBase * wireframe, struct ImBuf * ibuf)
2239 {
2240         int count;
2241         Sample * samp;
2242         
2243         /* first make a list of samples */
2244         
2245         samp = outline(ibuf, 0);
2246         if (samp == 0) return;
2247         
2248         count = (samp[0].x << 16) + samp[0].y;
2249         if (count) FitPoly(samp, 1, count, 1.0, wireframe); /* was 3.0. Frank */
2250
2251         MEM_freeN(samp);
2252 }
2253
2254
2255
2256 void imagestodisplist(void)
2257 {
2258         Base *base;
2259         Object *ob;
2260         Material *ma;
2261         Tex *tex;
2262         Mesh *me;
2263         ListBase _wireframe, *wireframe;
2264         DispList *dl;
2265         Segment *seg;
2266         float *data, xfac, yfac, xsi, ysi, vec[3];
2267         int tot;
2268         
2269         _wireframe.first= 0;
2270         _wireframe.last= 0;
2271         wireframe = &_wireframe;
2272         
2273         init_render_textures();
2274         
2275         base= G.scene->base.first;
2276         while(base) {
2277                 if(( (base->flag & SELECT) && (base->lay & G.scene->lay) ) ) {
2278                         if( base->object->type==OB_MESH) {
2279                                 ob= base->object;
2280                                 me= ob->data;
2281                                 
2282                                 ma= give_current_material(ob, 1);
2283         
2284                                 if(ma && ma->mtex[0] && ma->mtex[0]->tex) {
2285                                         tex= ma->mtex[0]->tex;
2286                                         
2287                                         /* this takes care of correct loading of new imbufs */
2288                                         externtex(ma->mtex[0], vec);
2289                                         
2290                                         if(tex->type==TEX_IMAGE && tex->ima && tex->ima->ibuf) {                                
2291                                                 
2292                                                 ob->dtx |= OB_DRAWIMAGE;
2293                                                 
2294                                                 ibuf2wire(wireframe, tex->ima->ibuf);
2295
2296                                                 tot= 0;
2297                                                 seg = wireframe->first;
2298                                                 while (seg) {
2299                                                         tot++;
2300                                                         seg = seg->next;
2301                                                 }
2302         
2303                                                 if(tot) {
2304                                                         freedisplist(&(ob->disp));
2305
2306                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListimage");
2307                                                         dl->verts= MEM_callocN(3*sizeof(float)*tot, "dlverts");
2308                                                         
2309                                                         BLI_addtail(&(ob->disp), dl);
2310                                                         dl->type= DL_POLY;
2311                                                         dl->parts= 1;
2312                                                         dl->nr= tot;
2313                                                         
2314                                                         xsi= 0.5*(tex->ima->ibuf->x);
2315                                                         ysi= 0.5*(tex->ima->ibuf->y);
2316                                                         xfac= me->size[0]/xsi;
2317                                                         yfac= me->size[1]/ysi;
2318                                                                                                 
2319                                                         data= dl->verts;
2320                                                         seg = wireframe->first;
2321                                                         while (seg) {
2322                                                                 data[0]= xfac*(seg->co[0]-xsi);
2323                                                                 data[1]= yfac*(seg->co[1]-ysi);
2324                                                                 data+= 3;
2325                                                                 seg = seg->next;
2326                                                         }
2327                                                         BLI_freelistN(wireframe);
2328                                                 }
2329                                         }
2330                                 }
2331                         }
2332                 }
2333                 base= base->next;
2334         }
2335         
2336         end_render_textures();
2337         
2338         allqueue(REDRAWVIEW3D, 0);
2339 }
2340
2341 /* on frame change */
2342 /* new method: only frees displists, and relies on 
2343    drawobject.c & convertscene.c to build it when needed
2344 */
2345 void test_all_displists(void)
2346 {
2347         Base *base;
2348         Object *ob;
2349         unsigned int lay;
2350         int makedisp, freedisp;
2351         
2352         /* background */        
2353         lay= G.scene->lay;
2354         
2355         base= G.scene->base.first;
2356         while(base) {
2357                 if(base->lay & lay) {
2358                         ob= base->object;
2359                         makedisp= freedisp= 0;
2360                         
2361                         if(ob->type==OB_MBALL && (ob->ipo || ob->parent)) {
2362                                 // find metaball object holding the displist
2363                                 // WARNING: if more metaballs have IPO's the displist
2364                                 // is recalculated to often... do we free the displist
2365                                 // and rely on the drawobject.c to build it again when needed
2366
2367                                 if(ob->disp.first == NULL) {
2368                                         ob= find_basis_mball(ob);
2369                                 }
2370                                 makedisp= 1;
2371                         }
2372                         else if(ob->parent) {
2373                                 
2374                                 if (ob->parent->type == OB_LATTICE)
2375                                         freedisp= 1;
2376                                 else if ((ob->parent->type==OB_ARMATURE) && (ob->partype == PARSKEL))
2377                                         makedisp= 1;
2378                                 else if ((ob->parent->type==OB_CURVE) && (ob->partype == PARSKEL))
2379                                         freedisp= 1;
2380                         }
2381
2382                         if(ob->hooks.first) {
2383                                 ObHook *hook;
2384                                 for(hook= ob->hooks.first; hook; hook= hook->next) {
2385                                         if(hook->parent) 
2386                                                 freedisp= 1;
2387                                         break;
2388                                 }
2389                         }
2390                         
2391                         if(ob->softflag) freedisplist_object(ob);
2392                         /* warn, ob pointer changed in case of OB_MALL */
2393
2394                         if ELEM(ob->type, OB_CURVE, OB_SURF) {
2395                                 if(ob!=G.obedit) {
2396                                         Curve *cu= ob->data;
2397                                         
2398                                         if(cu->key ) makedisp= 1;
2399                                         if(cu->bevobj) {
2400                                                 Curve *cu1= cu->bevobj->data;
2401                                                 if(cu1->key ) freedisp= 1;
2402                                         }
2403                                         if(cu->taperobj) {
2404                                                 Curve *cu1= cu->taperobj->data;
2405                                                 if(cu1->key ) freedisp= 1;
2406                                         }
2407                                 }
2408                         }
2409                         else if(ob->type==OB_FONT) {
2410                                 Curve *cu= ob->data;
2411                                 if(cu->textoncurve) {
2412                                         if( ((Curve *)cu->textoncurve->data)->key ) {
2413                                                 text_to_curve(ob, 0);
2414                                                 freedisp= 1;
2415                                         }
2416                                 }
2417                         }
2418                         else if(ob->type==OB_MESH) {
2419                                 if(ob->effect.first) {
2420                                         Effect *eff= ob->effect.first;
2421                                         while(eff) {
2422                                                 if(eff->type==EFF_WAVE) {
2423                                                         freedisp= 1;
2424                                                         break;
2425                                                 }
2426                                                 eff= eff->next;
2427                                         }
2428                                 }
2429                                 if(ob!=G.obedit) {
2430                                         if(( ((Mesh *)(ob->data))->key )) 
2431                                                 freedisp= 1;
2432                                 }
2433                         }
2434                         if(freedisp) freedisplist_object(ob);
2435                         if(makedisp) makeDispList(ob);
2436                 }
2437                 if(base->next==0 && G.scene->set && base==G.scene->base.last) base= G.scene->set->base.first;
2438                 else base= base->next;
2439         }
2440 }
2441
2442
2443 void boundbox_displist(Object *ob)
2444 {
2445         BoundBox *bb=0;
2446         float min[3], max[3];
2447         DispList *dl;
2448         float *vert;
2449         int a, tot=0;
2450         
2451         INIT_MINMAX(min, max);
2452
2453         if(ob->type==OB_MESH) {
2454                 Mesh *me= ob->data;
2455
2456                 dl= find_displist(&ob->disp, DL_VERTS);
2457                 if(!dl) return;
2458
2459                 if(me->bb==0) me->bb= MEM_callocN(sizeof(BoundBox), "boundbox");        
2460                 bb= me->bb;
2461
2462                 vert= dl->verts;
2463                 for(a=0; a<dl->nr; a++, vert+=3) {
2464                         DO_MINMAX(vert, min, max);
2465                 }
2466         }
2467         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2468                 Curve *cu= ob->data;
2469
2470                 if(cu->bb==0) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");        
2471                 bb= cu->bb;
2472                 
2473                 dl= cu->disp.first;
2474
2475                 while (dl) {
2476                         if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
2477                         else tot= dl->nr*dl->parts;
2478                         
2479                         vert= dl->verts;
2480                         for(a=0; a<tot; a++, vert+=3) {
2481                                 DO_MINMAX(vert, min, max);
2482                         }
2483
2484                         dl= dl->next;
2485                 }
2486         }
2487         
2488         if(bb) {
2489                 bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
2490                 bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
2491                 
2492                 bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
2493                 bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
2494         
2495                 bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
2496                 bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
2497         }
2498 }
2499