- added Mesh->derived and Mesh->decimated DerivedMesh pointers
[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 #include "MEM_guardedalloc.h"
45
46 #include "IMB_imbuf_types.h"
47
48 #include "DNA_texture_types.h"
49 #include "DNA_meta_types.h"
50 #include "DNA_curve_types.h"
51 #include "DNA_effect_types.h"
52 #include "DNA_listBase.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_mesh_types.h"
56 #include "DNA_meshdata_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_image_types.h"
59 #include "DNA_material_types.h"
60 #include "DNA_view3d_types.h"
61 #include "DNA_lattice_types.h"
62
63 #include "BLI_blenlib.h"
64 #include "BLI_arithb.h"
65 #include "BLI_editVert.h"
66
67 #include "BKE_bad_level_calls.h"
68 #include "BKE_utildefines.h"
69 #include "BKE_global.h"
70 #include "BKE_displist.h"
71 #include "BKE_deform.h"
72 #include "BKE_DerivedMesh.h"
73 #include "BKE_object.h"
74 #include "BKE_world.h"
75 #include "BKE_mesh.h"
76 #include "BKE_effect.h"
77 #include "BKE_mball.h"
78 #include "BKE_material.h"
79 #include "BKE_curve.h"
80 #include "BKE_anim.h"
81 #include "BKE_screen.h"
82 #include "BKE_texture.h"
83 #include "BKE_library.h"
84 #include "BKE_font.h"
85 #include "BKE_lattice.h"
86 #include "BKE_scene.h"
87 #include "BKE_subsurf.h"
88
89 #include "nla.h" /* For __NLA: Please do not remove yet */
90 #include "render.h"
91
92
93 /***/
94
95 typedef struct _FastLamp FastLamp;
96 struct _FastLamp {
97         FastLamp *next;
98         
99         short type, mode, lay, rt;
100         float co[3];
101         float vec[3];
102         float dist, distkw, att1, att2, spotsi, spotbl, r, g, b;
103 };
104
105 /***/
106
107 static FastLamp *fastlamplist= NULL;
108 static float fviewmat[4][4];
109
110 /* only called from subsurf.c when levels is zero */
111 DispListMesh *displistmesh_from_editmesh(EditMesh *em) 
112 {
113         DispListMesh *dlm= MEM_callocN(sizeof(*dlm),"dlm");
114         EditVert *eve, *evePrev;
115         EditEdge *eed;
116         EditFace *evl;
117         MFace *mfNew;
118         int i;
119
120         dlm->flag= 0;
121         dlm->totvert= BLI_countlist(&em->verts);
122         dlm->totface= BLI_countlist(&em->edges)+BLI_countlist(&em->faces);
123         dlm->mvert= MEM_callocN(sizeof(*dlm->mface)*dlm->totvert, "dlm->mvert");
124         dlm->mcol= NULL;
125         dlm->tface= NULL;
126         dlm->mface= MEM_mallocN(sizeof(*dlm->mface)*dlm->totface, "dlm->mface");
127
128         i=0;
129         for (eve= em->verts.first; eve; eve= eve->next, i++) {
130                 MVert *mvNew= &dlm->mvert[i];
131                 VECCOPY(mvNew->co, eve->co);
132                 eve->ssco= eve->co;             /* no subsurf coordinate, but we add a save pointer in it */
133                 eve->prev= (void*) i;   /* hack to fetch indices */
134         }
135
136         mfNew= dlm->mface;
137         for (evl= em->faces.first; evl; evl= evl->next) {
138                 // we skip hidden faces
139                 if(evl->v1->h || evl->v2->h || evl->v3->h || (evl->v4 && evl->v4->h)) dlm->totface--;
140                 else {
141                         mfNew->v1= (int) evl->v1->prev;
142                         mfNew->v2= (int) evl->v2->prev;
143                         mfNew->v3= (int) evl->v3->prev;
144                         mfNew->v4= evl->v4?(int) evl->v4->prev:0;
145                         mfNew->flag= evl->flag;
146                         mfNew->mat_nr= evl->mat_nr;
147                         mfNew->puno= 0;
148                         mfNew->edcode= 0;
149         
150                         if (evl->v4 && !mfNew->v4) {
151                                 mfNew->v1^= mfNew->v3^= mfNew->v1^= mfNew->v3;
152                                 mfNew->v2^= mfNew->v4^= mfNew->v2^= mfNew->v4;
153                         }
154                         mfNew++;
155                 }
156         }
157         for (eed= em->edges.first; eed; eed= eed->next, mfNew++) {
158
159                 mfNew->v1= (int) eed->v1->prev;
160                 mfNew->v2= (int) eed->v2->prev;
161                 mfNew->v3= 0;
162                 mfNew->v4= 0;
163                 mfNew->flag= 0;
164                 mfNew->mat_nr= 0;
165                 mfNew->puno= 0;
166                 mfNew->edcode= 0;
167         }
168
169                 /* restore prev links */
170         for (evePrev=NULL, eve= em->verts.first; eve; evePrev=eve, eve= eve->next)
171                 eve->prev= evePrev;
172
173         displistmesh_calc_normals(dlm);
174
175         return dlm;
176 }
177 DispListMesh *displistmesh_from_mesh(Mesh *me, float *extverts) 
178 {
179         DispListMesh *dlm= MEM_callocN(sizeof(*dlm),"dlm");
180         int i;
181         dlm->flag= 0;
182         dlm->totvert= me->totvert;
183         dlm->totface= me->totface;
184         dlm->mvert= MEM_dupallocN(me->mvert);
185         dlm->mcol= me->mcol?MEM_dupallocN(me->mcol):NULL;
186         dlm->tface= me->tface?MEM_dupallocN(me->tface):NULL;
187         dlm->mface= MEM_mallocN(sizeof(*dlm->mface)*dlm->totface, "dlm->mface");
188
189         if (extverts) {
190                 for (i=0; i<dlm->totvert; i++) {
191                         VECCOPY(dlm->mvert[i].co, &extverts[i*3]);
192                 }
193         }
194         for (i=0; i<dlm->totface; i++) {
195                 MFace *mfOld= &((MFace*) me->mface)[i];
196                 MFace *mfNew= &dlm->mface[i];
197
198                 mfNew->v1= mfOld->v1;
199                 mfNew->v2= mfOld->v2;
200                 mfNew->v3= mfOld->v3;
201                 mfNew->v4= mfOld->v4;
202                 mfNew->flag= mfOld->flag;
203                 mfNew->mat_nr= mfOld->mat_nr;
204                 mfNew->puno= 0;
205                 mfNew->edcode= 0;
206         }
207
208         displistmesh_calc_normals(dlm);
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         if (dlm->nors) MEM_freeN(dlm->nors);
224         MEM_freeN(dlm);
225 }
226
227 DispListMesh *displistmesh_copy(DispListMesh *odlm) 
228 {
229         DispListMesh *ndlm= MEM_dupallocN(odlm);
230         ndlm->editedge= NULL;
231         ndlm->editface= NULL;
232         ndlm->mvert= MEM_dupallocN(odlm->mvert);
233         ndlm->medge= MEM_dupallocN(odlm->medge);
234         ndlm->mface= MEM_dupallocN(odlm->mface);
235         if (odlm->nors) ndlm->nors = MEM_dupallocN(odlm->nors);
236         if (odlm->mcol) ndlm->mcol= MEM_dupallocN(odlm->mcol);
237         if (odlm->tface) ndlm->tface= MEM_dupallocN(odlm->tface);
238         
239         return ndlm;
240 }
241
242 void displistmesh_calc_normals(DispListMesh *dlm) 
243 {
244         MVert *mverts= dlm->mvert;
245         MFace *mfaces= dlm->mface;
246         float (*tnorms)[3]= MEM_callocN(dlm->totvert*sizeof(*tnorms), "tnorms");
247         int i;
248         
249         if (dlm->nors) {
250                 MEM_freeN(dlm->nors);
251         }
252
253         dlm->nors= MEM_mallocN(sizeof(*dlm->nors)*3*dlm->totface, "meshnormals");
254
255         for (i=0; i<dlm->totface; i++) {
256                 MFace *mf= &mfaces[i];
257                 float *f_no= &dlm->nors[i*3];
258
259                 if (!mf->v3)
260                         continue;
261                         
262                 if (mf->v4)
263                         CalcNormFloat4(mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co, f_no);
264                 else
265                         CalcNormFloat(mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, f_no);
266                 
267                 VecAddf(tnorms[mf->v1], tnorms[mf->v1], f_no);
268                 VecAddf(tnorms[mf->v2], tnorms[mf->v2], f_no);
269                 VecAddf(tnorms[mf->v3], tnorms[mf->v3], f_no);
270                 if (mf->v4)
271                         VecAddf(tnorms[mf->v4], tnorms[mf->v4], f_no);
272         }
273         for (i=0; i<dlm->totvert; i++) {
274                 MVert *mv= &mverts[i];
275                 float *no= tnorms[i];
276                 
277                 Normalise(no);
278                 mv->no[0]= (short)(no[0]*32767.0);
279                 mv->no[1]= (short)(no[1]*32767.0);
280                 mv->no[2]= (short)(no[2]*32767.0);
281         }
282         
283         MEM_freeN(tnorms);
284 }
285
286 void displistmesh_to_mesh(DispListMesh *dlm, Mesh *me) 
287 {
288         MFace *mfaces;
289         int i;
290         
291         if (dlm->totvert>MESH_MAX_VERTS) {
292                 error("Too many vertices");
293         } else {
294                 me->totface= dlm->totface;
295                 me->totvert= dlm->totvert;
296
297                 me->mvert= MEM_dupallocN(dlm->mvert);
298                 me->mface= mfaces= MEM_mallocN(sizeof(*mfaces)*me->totface, "me->mface");
299                 me->tface= MEM_dupallocN(dlm->tface);
300                 me->mcol= MEM_dupallocN(dlm->mcol);
301
302                 if(dlm->medge) {
303                         me->totedge= dlm->totedge;
304                         me->medge= MEM_dupallocN(dlm->medge);
305                 }
306
307                 for (i=0; i<me->totface; i++) {
308                         MFace *mf= &mfaces[i];
309                         MFace *oldmf= &dlm->mface[i];
310                 
311                         mf->v1= oldmf->v1;
312                         mf->v2= oldmf->v2;
313                         mf->v3= oldmf->v3;
314                         mf->v4= oldmf->v4;
315                         mf->flag= oldmf->flag;
316                         mf->mat_nr= oldmf->mat_nr;
317                         mf->puno= 0;
318                         mf->edcode= ME_V1V2|ME_V2V3|ME_V3V4|ME_V4V1;
319                 }
320         }
321 }
322
323 void free_disp_elem(DispList *dl)
324 {
325         if(dl) {
326                 if(dl->verts) MEM_freeN(dl->verts);
327                 if(dl->nors) MEM_freeN(dl->nors);
328                 if(dl->index) MEM_freeN(dl->index);
329                 if(dl->col1) MEM_freeN(dl->col1);
330                 if(dl->col2) MEM_freeN(dl->col2);
331                 MEM_freeN(dl);
332         }
333 }
334
335 void freedisplist(ListBase *lb)
336 {
337         DispList *dl;
338
339         dl= lb->first;
340         while(dl) {
341                 BLI_remlink(lb, dl);
342                 free_disp_elem(dl);
343                 dl= lb->first;
344         }
345 }
346
347 static void freedisplist_object(Object *ob)
348 {
349         freedisplist(&ob->disp);
350
351         if(ob->type==OB_MESH) {
352                 Mesh *me= ob->data;
353                 freedisplist(&me->disp);
354         }
355         else if(ob->type==OB_CURVE || ob->type==OB_SURF || ob->type==OB_FONT) {
356                 Curve *cu= ob->data;
357                 freedisplist(&cu->disp);
358         }
359 }
360
361 void free_displist_by_type(ListBase *lb, int type) 
362 {
363         DispList *dl;
364         
365         for (dl= lb->first; dl; ) {
366                 DispList *next= dl->next;
367                 
368                 if (dl->type==type) {
369                         BLI_remlink(lb, dl);
370                         free_disp_elem(dl);
371                 }
372                 
373                 dl= next;
374         }       
375 }
376
377 DispList *find_displist_create(ListBase *lb, int type)
378 {
379         DispList *dl;
380         
381         dl= lb->first;
382         while(dl) {
383                 if(dl->type==type) return dl;
384                 dl= dl->next;
385         }
386
387         dl= MEM_callocN(sizeof(DispList), "find_disp");
388         dl->type= type;
389         BLI_addtail(lb, dl);
390
391         return dl;
392 }
393
394 DispList *find_displist(ListBase *lb, int type)
395 {
396         DispList *dl;
397         
398         dl= lb->first;
399         while(dl) {
400                 if(dl->type==type) return dl;
401                 dl= dl->next;
402         }
403
404         return 0;
405 }
406
407 int displist_has_faces(ListBase *lb)
408 {
409         DispList *dl;
410         
411         dl= lb->first;
412         while(dl) {
413                 if ELEM5(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF, DL_TRIA, DL_POLY)
414                         return 1;
415                 dl= dl->next;
416         }
417         return 0;
418 }
419
420 void copy_displist(ListBase *lbn, ListBase *lb)
421 {
422         DispList *dln, *dl;
423         
424         lbn->first= lbn->last= 0;
425         
426         dl= lb->first;
427         while(dl) {
428                 
429                 dln= MEM_dupallocN(dl);
430                 BLI_addtail(lbn, dln);
431                 dln->verts= MEM_dupallocN(dl->verts);
432                 dln->nors= MEM_dupallocN(dl->nors);
433                 dln->index= MEM_dupallocN(dl->index);
434                 dln->col1= MEM_dupallocN(dl->col1);
435                 dln->col2= MEM_dupallocN(dl->col2);
436                 
437                 dl= dl->next;
438         }
439 }
440
441 static void initfastshade(void)
442 {
443         Base *base;
444         Object *ob;
445         Lamp *la;
446         FastLamp *fl;
447         float mat[4][4];
448
449         init_render_world();
450         
451         if(fastlamplist) return;
452         if(G.scene->camera==0) G.scene->camera= scene_find_camera(G.scene);
453         if(G.scene->camera==0) return;
454
455         /* copied from 'roteerscene' (does that function still exist? (ton) */
456         where_is_object(G.scene->camera);
457         Mat4CpyMat4(R.viewinv, G.scene->camera->obmat);
458         Mat4Ortho(R.viewinv);
459         Mat4Invert(fviewmat, R.viewinv);
460
461         /* initrendertexture(); */
462
463         base= G.scene->base.first;
464         while(base) {
465                 ob= base->object;
466                 if( ob->type==OB_LAMP && (base->lay & G.scene->lay)) {
467                         
468                         Mat4MulMat4(mat, ob->obmat, fviewmat);
469                         
470                         la= ob->data;
471                         fl= MEM_mallocN(sizeof(FastLamp), "initfastshade2");
472
473                         fl->next= fastlamplist;
474                         fastlamplist= fl;
475
476                         fl->type= la->type;
477                         fl->mode= la->mode;
478                         fl->lay= base->lay;
479                         
480                         fl->vec[0]= mat[2][0];
481                         fl->vec[1]= mat[2][1];
482                         fl->vec[2]= mat[2][2];
483                         Normalise(fl->vec);
484                         
485                         fl->co[0]= mat[3][0];
486                         fl->co[1]= mat[3][1];
487                         fl->co[2]= mat[3][2];
488
489                         fl->dist= la->dist;
490                         fl->distkw= fl->dist*fl->dist;
491                         fl->att1= la->att1;
492                         fl->att2= la->att2;
493
494                         fl->spotsi= (float)cos( M_PI*la->spotsize/360.0 );
495                         fl->spotbl= (1.0f-fl->spotsi)*la->spotblend;
496         
497                         fl->r= la->energy*la->r;
498                         fl->g= la->energy*la->g;
499                         fl->b= la->energy*la->b;
500                 }
501                 
502                 if(base->next==0 && G.scene->set && base==G.scene->base.last) base= G.scene->set->base.first;
503                 else base= base->next;
504         }
505 }
506
507
508 void freefastshade()
509 {
510         while (fastlamplist) {
511                 FastLamp *fl= fastlamplist;
512                 fastlamplist= fl->next;
513                 
514                 MEM_freeN(fl);
515         }
516 }
517
518
519 static void fastshade(float *co, float *nor, float *orco, Material *ma, char *col1, char *col2, char *vertcol)
520 {
521         ShadeInput shi;
522         FastLamp *fl;
523         float i, t, inp, is, soft,  lv[3], lampdist, ld;
524         float diff1[3], diff2[3];
525         float isr1=0, isg1=0, isb1=0, isr=0, isg=0, isb=0;
526         int a, back;
527
528         if(ma==0) return;
529         
530         shi.mat= ma;
531         shi.vlr= NULL;  // have to do this!
532         
533         // copy all relevant material vars, note, keep this synced with render_types.h
534         memcpy(&shi.r, &shi.mat->r, 23*sizeof(float));
535         // set special cases:
536         shi.har= shi.mat->har;
537         
538         shi.osatex= 0;  // also prevents reading vlr
539         
540         VECCOPY(shi.vn, nor);
541         
542         if(ma->mode & MA_VERTEXCOLP) {
543                 if(vertcol) {
544                         shi.r= vertcol[3]/255.0;
545                         shi.g= vertcol[2]/255.0;
546                         shi.b= vertcol[1]/255.0;
547                 }
548         }
549         
550         if(ma->texco) {
551                 VECCOPY(shi.lo, orco);
552                 
553                 if(ma->texco & TEXCO_GLOB) {
554                         VECCOPY(shi.gl, shi.lo);
555                 }
556                 if(ma->texco & TEXCO_WINDOW) {
557                         VECCOPY(shi.winco, shi.lo);
558                 }
559                 if(ma->texco & TEXCO_STICKY) {
560                         VECCOPY(shi.sticky, shi.lo);
561                 }
562                 if(ma->texco & TEXCO_UV) {
563                         VECCOPY(shi.uv, shi.lo);
564                 }
565                 if(ma->texco & TEXCO_OBJECT) {
566                         VECCOPY(shi.co, shi.lo);
567                 }
568                 if(ma->texco & TEXCO_NORM) {
569                         VECCOPY(shi.orn, shi.vn);
570                 }
571                 if(ma->texco & TEXCO_REFL) {
572                         
573                         inp= 2.0*(shi.vn[2]);
574                         shi.ref[0]= (inp*shi.vn[0]);
575                         shi.ref[1]= (inp*shi.vn[1]);
576                         shi.ref[2]= (-1.0+inp*shi.vn[2]);
577                 }
578
579                 do_material_tex(&shi);
580         }
581
582         if(ma->mode & MA_SHLESS) {
583                 if(vertcol && (ma->mode & (MA_VERTEXCOL+MA_VERTEXCOLP))== MA_VERTEXCOL ) {
584                         float fac;
585                         fac= vertcol[3]*shi.r;
586                         col1[3]= fac>=1.0?255:(char)fac;
587                         fac= vertcol[2]*shi.g;
588                         col1[2]= fac>=1.0?255:(char)fac;
589                         fac= vertcol[2]*shi.b;
590                         col1[1]= fac>=1.0?255:(char)fac;
591                 }
592                 else {
593                         int fac;
594                         fac= (int) (255.0*shi.r);
595                         col1[3]= fac>255?255:(char)fac;
596                         fac= (int) (255.0*shi.g);
597                         col1[2]= fac>255?255:(char)fac;
598                         fac= (int) (255.0*shi.b);
599                         col1[1]= fac>255?255:(char)fac;
600                 }
601                 if(col2) {
602                         col2[3]= col1[3];
603                         col2[2]= col1[2];
604                         col2[1]= col1[1];
605                 }
606                 return;
607         }
608
609         if( vertcol && (ma->mode & (MA_VERTEXCOL+MA_VERTEXCOLP))== MA_VERTEXCOL ) {
610                 diff1[0]= diff2[0]= shi.r*(shi.emit+vertcol[3]/255.0);
611                 diff1[1]= diff2[1]= shi.g*(shi.emit+vertcol[2]/255.0);
612                 diff1[2]= diff2[2]= shi.b*(shi.emit+vertcol[1]/255.0);
613         }
614         else {
615                 diff1[0]= diff2[0]= shi.r*shi.emit;
616                 diff1[1]= diff2[1]= shi.g*shi.emit;
617                 diff1[2]= diff2[2]= shi.b*shi.emit;
618         }
619         
620         shi.view[0]= 0.0;
621         shi.view[1]= 0.0;
622         shi.view[2]= 1.0;
623         
624         Normalise(shi.view);
625
626         for (fl= fastlamplist; fl; fl= fl->next) {
627                 /* if(fl->mode & LA_LAYER) if((fl->lay & ma->lay)==0) continue; */
628
629                 if(fl->type==LA_SUN || fl->type==LA_HEMI) {
630                         VECCOPY(lv, fl->vec);
631                         lampdist= 1.0;
632                 }
633                 else {
634                         lv[0]= fl->co[0] - co[0];
635                         lv[1]= fl->co[1] - co[1];
636                         lv[2]= fl->co[2] - co[2];
637                         ld= sqrt(lv[0]*lv[0]+lv[1]*lv[1]+lv[2]*lv[2]);
638                         lv[0]/=ld;
639                         lv[1]/=ld;
640                         lv[2]/=ld;
641
642                         if(fl->mode & LA_QUAD) {
643                                 t= 1.0;
644                                 if(fl->att1>0.0)
645                                         t= fl->dist/(fl->dist+fl->att1*ld);
646                                 if(fl->att2>0.0)
647                                         t*= fl->distkw/(fl->distkw+fl->att2*ld*ld);
648
649                                 lampdist= t;
650                         }
651                         else {
652                                 lampdist= (fl->dist/(fl->dist+ld));
653                         }
654
655                         if(fl->mode & LA_SPHERE) {
656                                 t= fl->dist - ld;
657                                 if(t<0.0) continue;
658                                 
659                                 t/= fl->dist;
660                                 lampdist*= (t);
661                         }
662                 }
663
664                 if(fl->type==LA_SPOT) {
665                         inp= lv[0]*fl->vec[0]+lv[1]*fl->vec[1]+lv[2]*fl->vec[2];
666                         if(inp<fl->spotsi) continue;
667                         else {
668                                 t= inp-fl->spotsi;
669                                 i= 1.0;
670                                 soft= 1.0;
671                                 if(t<fl->spotbl && fl->spotbl!=0.0) {
672                                         /* soft area */
673                                         i= t/fl->spotbl;
674                                         t= i*i;
675                                         soft= (3.0*t-2.0*t*i);
676                                         inp*= soft;
677                                 }
678
679                                 lampdist*=inp;
680                         }
681                 }
682
683                 if(fl->mode & LA_NO_DIFF) is= 0.0;
684                 else {
685                         is= nor[0]*lv[0]+ nor[1]*lv[1]+ nor[2]*lv[2];
686         
687                         if(ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(nor, lv, shi.view, ma->roughness);
688                         else if(ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(nor, lv, shi.view, ma->param[0], ma->param[1]);
689                 }
690                 
691                 back= 0;
692                 if(is<0.0) {
693                         back= 1;
694                         is= -is;
695                 }
696                 inp= is*lampdist*shi.refl;
697
698                 if(back==0) {
699                         add_to_diffuse(diff1, &shi, is, inp*fl->r, inp*fl->g, inp*fl->b);
700                         //diff1[0]+= inp*fl->r;
701                         //diff1[1]+= inp*fl->g;
702                         //diff1[2]+= inp*fl->b;
703                 } else if(col2) {
704                         add_to_diffuse(diff2, &shi, is, inp*fl->r, inp*fl->g, inp*fl->b);
705                         //diff2[0]+= inp*fl->r;
706                         //diff2[1]+= inp*fl->g;
707                         //diff2[2]+= inp*fl->b;
708                 }
709                 if(shi.spec!=0.0 && (fl->mode & LA_NO_SPEC)==0) {
710                         float specfac;
711                         
712                         if(ma->spec_shader==MA_SPEC_PHONG) 
713                                 specfac= Phong_Spec(nor, lv, shi.view, shi.har);
714                         else if(ma->spec_shader==MA_SPEC_COOKTORR) 
715                                 specfac= CookTorr_Spec(nor, lv, shi.view, shi.har);
716                         else if(ma->spec_shader==MA_SPEC_BLINN) 
717                                 specfac= Blinn_Spec(nor, lv, shi.view, ma->refrac, (float)shi.har);
718                         else 
719                                 specfac= Toon_Spec(nor, lv, shi.view, ma->param[2], ma->param[3]);
720                         
721                         if(specfac>0) {
722                                 t= specfac*shi.spec*lampdist;
723                                 if(back==0) {
724                                         if(ma->mode & MA_RAMP_SPEC) {
725                                                 float spec[3];
726                                                 do_specular_ramp(&shi, specfac, t, spec);
727                                                 isr+= t*(fl->r * spec[0]);
728                                                 isg+= t*(fl->g * spec[1]);
729                                                 isb+= t*(fl->b * spec[2]);
730                                         }
731                                         else {
732                                                 isr+= t*(fl->r * shi.specr);
733                                                 isg+= t*(fl->g * shi.specg);
734                                                 isb+= t*(fl->b * shi.specb);
735                                         }
736                                 }
737                                 else if(col2) {
738                                         if(ma->mode & MA_RAMP_SPEC) {
739                                                 float spec[3];
740                                                 do_specular_ramp(&shi, specfac, t, spec);
741                                                 isr1+= t*(fl->r * spec[0]);
742                                                 isg1+= t*(fl->g * spec[1]);
743                                                 isb1+= t*(fl->b * spec[2]);
744                                         }
745                                         else {
746                                                 isr1+= t*(fl->r * shi.specr);
747                                                 isg1+= t*(fl->g * shi.specg);
748                                                 isb1+= t*(fl->b * shi.specb);
749                                         }
750                                 }
751                         }
752                 }
753
754         }
755
756         if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(diff1, &shi);
757         if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(&isr, &isg, &isb, &shi);
758
759         a= 256*(diff1[0] + shi.ambr +isr);
760         if(a>255) col1[3]= 255; 
761         else col1[3]= a;
762         a= 256*(diff1[1] + shi.ambg +isg);
763         if(a>255) col1[2]= 255; 
764         else col1[2]= a;
765         a= 256*(diff1[2] + shi.ambb +isb);
766         if(a>255) col1[1]= 255; 
767         else col1[1]= a;
768
769         if(col2) {
770                 if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(diff2, &shi);
771                 if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(&isr1, &isg1, &isb1, &shi);
772                 
773                 a= 256*(diff2[0] + shi.ambr +isr1);
774                 if(a>255) col2[3]= 255; 
775                 else col2[3]= a;
776                 a= 256*(diff2[1] + shi.ambg +isg1);
777                 if(a>255) col2[2]= 255; 
778                 else col2[2]= a;
779                 a= 256*(diff2[2] + shi.ambb +isb1);
780                 if(a>255) col2[1]= 255; 
781                 else col2[1]= a;
782         }
783
784 }
785
786 void addnormalsDispList(Object *ob, ListBase *lb)
787 {
788         DispList *dl = NULL;
789         Mesh *me;
790         MVert *ve1, *ve2, *ve3, *ve4;
791         MFace *mface;
792         float *vdata, *ndata, nor[3];
793         float *v1, *v2, *v3, *v4;
794         float *n1, *n2, *n3, *n4;
795         int a, b, p1, p2, p3, p4;
796
797         
798         if(ob->type==OB_MESH) {
799                 
800                 me= get_mesh(ob);
801                 
802                 if(me->totface==0) return;
803                 
804                 if(me->disp.first==0) {
805                         dl= MEM_callocN(sizeof(DispList), "meshdisp");
806                         dl->type= DL_NORS;
807                         dl->parts= 1;
808                         dl->nr= me->totface;
809                         BLI_addtail(&me->disp, dl);
810                 }
811                 else return;
812                 
813                 if(dl->nors==0) {
814                         dl->nors= MEM_mallocN(sizeof(float)*3*me->totface, "meshnormals");
815                         n1= dl->nors;
816                         mface= me->mface;
817                         a= me->totface;
818                         while(a--) {
819                                 if(mface->v3) {
820                                         ve1= me->mvert+mface->v1;
821                                         ve2= me->mvert+mface->v2;
822                                         ve3= me->mvert+mface->v3;
823                                         ve4= me->mvert+mface->v4;
824                                         
825                                         if(mface->v4) CalcNormFloat4(ve1->co, ve2->co, ve3->co, ve4->co, n1);
826                                         else CalcNormFloat(ve1->co, ve2->co, ve3->co, n1);
827                                 }
828                                 n1+= 3;
829                                 mface++;
830                         }
831                 }
832
833                 return;
834         }
835
836         dl= lb->first;
837         
838         while(dl) {
839                 if(dl->type==DL_INDEX3) {
840                         if(dl->nors==0) {
841                                 dl->nors= MEM_callocN(sizeof(float)*3, "dlnors");
842                                 if(dl->verts[2]<0.0) dl->nors[2]= -1.0;
843                                 else dl->nors[2]= 1.0;
844                         }
845                 }
846                 else if(dl->type==DL_SURF) {
847                         if(dl->nors==0) {
848                                 dl->nors= MEM_callocN(sizeof(float)*3*dl->nr*dl->parts, "dlnors");
849                                 
850                                 vdata= dl->verts;
851                                 ndata= dl->nors;
852                                 
853                                 for(a=0; a<dl->parts; a++) {
854         
855                                         DL_SURFINDEX(dl->flag & DL_CYCL_U, dl->flag & DL_CYCL_V, dl->nr, dl->parts);
856         
857                                         v1= vdata+ 3*p1; 
858                                         n1= ndata+ 3*p1;
859                                         v2= vdata+ 3*p2; 
860                                         n2= ndata+ 3*p2;
861                                         v3= vdata+ 3*p3; 
862                                         n3= ndata+ 3*p3;
863                                         v4= vdata+ 3*p4; 
864                                         n4= ndata+ 3*p4;
865                                         
866                                         for(; b<dl->nr; b++) {
867         
868                                                 CalcNormFloat4(v1, v3, v4, v2, nor);
869         
870                                                 VecAddf(n1, n1, nor);
871                                                 VecAddf(n2, n2, nor);
872                                                 VecAddf(n3, n3, nor);
873                                                 VecAddf(n4, n4, nor);
874         
875                                                 v2= v1; v1+= 3;
876                                                 v4= v3; v3+= 3;
877                                                 n2= n1; n1+= 3;
878                                                 n4= n3; n3+= 3;
879                                         }
880                                 }
881                                 a= dl->parts*dl->nr;
882                                 v1= ndata;
883                                 while(a--) {
884                                         Normalise(v1);
885                                         v1+= 3;
886                                 }
887                         }
888                 }
889                 dl= dl->next;
890         }
891 }
892
893
894 void shadeDispList(Object *ob)
895 {
896         MFace *mface;
897         MVert *mvert;
898         DispList *dl, *dlob, *dldeform;
899         Material *ma = NULL;
900         Mesh *me;
901         Curve *cu;
902 /*      extern Material defmaterial;     *//* initrender.c, already in bad lev calls*/
903         float *orco=NULL, imat[3][3], tmat[4][4], mat[4][4], vec[3], xn, yn, zn;
904         float *fp, *nor, n1[3];
905         unsigned int *col1, *col2, *vertcol;
906         int a, lastmat= -1, need_orco = 0;
907
908         if(ob->flag & OB_FROMDUPLI) return;
909         initfastshade();
910
911         Mat4MulMat4(mat, ob->obmat, fviewmat);
912         
913         Mat4Invert(tmat, mat);
914         Mat3CpyMat4(imat, tmat);
915         if(ob->transflag & OB_NEG_SCALE) Mat3MulFloat((float *)imat, -1.0);
916         
917         /* we extract dl_verts, deform info */
918         dldeform= find_displist(&ob->disp, DL_VERTS);
919         if(dldeform) BLI_remlink(&ob->disp, dldeform);
920         
921         /* Metaballs have the standard displist in the Object */
922         if(ob->type!=OB_MBALL) freedisplist(&ob->disp);
923
924         need_orco= 0;
925         for(a=0; a<ob->totcol; a++) {
926                 ma= give_current_material(ob, a+1);
927                 if(ma) {
928                         init_render_material(ma);
929                         if(ma->texco & TEXCO_ORCO) need_orco= 1;
930                 }
931         }
932
933         if(ob->type==OB_MESH) {
934                 
935                 me= ob->data;
936                 
937                 if (mesh_uses_displist(me)) {
938                         DerivedMesh *dm= mesh_get_derived(ob);
939                         DispListMesh *dlm;
940
941                         if (need_orco) {
942                                 make_orco_displist_mesh(ob, me->subdiv);
943                                 orco= me->orco;
944                         }
945
946                         dlm= dm->convertToDispListMesh(dm);
947
948                         if (dlm && dlm->totvert) {
949                                 float *vnors, *vn;
950                                 int i;
951                                 
952                                 dlob= MEM_callocN(sizeof(DispList), "displistshade");
953                                 BLI_addtail(&ob->disp, dlob);
954                                 dlob->type= DL_VERTCOL;
955                         
956                                 dlob->col1= MEM_mallocN(sizeof(*dlob->col1)*dlm->totface*4, "col1");
957                                 if (me->flag & ME_TWOSIDED)
958                                         dlob->col2= MEM_mallocN(sizeof(*dlob->col2)*dlm->totface*4, "col1");
959                                 
960                                 /* vertexnormals */
961                                 vn=vnors= MEM_mallocN(dlm->totvert*3*sizeof(float), "vnors disp");
962                                 mvert= dlm->mvert;
963                                 a= dlm->totvert;
964                                 while(a--) {
965                                         
966                                         xn= mvert->no[0]; 
967                                         yn= mvert->no[1]; 
968                                         zn= mvert->no[2];
969                                         
970                                         /* transpose ! */
971                                         vn[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
972                                         vn[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
973                                         vn[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
974                                         Normalise(vn);
975                                         
976                                         mvert++; vn+=3;
977                                 }               
978                 
979                                 for (i=0; i<dlm->totface; i++) {
980                                         MFace *mf= &dlm->mface[i];
981
982                                         if (mf->v3) {
983                                                 int j, vidx[4], nverts= mf->v4?4:3;
984                                                 unsigned int *col1base= &dlob->col1[i*4];
985                                                 unsigned int *col2base= dlob->col2?&dlob->col2[i*4]:NULL;
986                                                 unsigned int *mcolbase;
987                                                 float nor[3];
988                                                 
989                                                 if (dlm->tface) {
990                                                         mcolbase = dlm->tface[i].col;
991                                                 } else if (dlm->mcol) {
992                                                         mcolbase = (unsigned int*) &dlm->mcol[i*4];
993                                                 } else {
994                                                         mcolbase = NULL;
995                                                 }
996
997                                                 ma= give_current_material(ob, mf->mat_nr+1);
998                                                 if(ma==0) ma= &defmaterial;
999                                                 
1000                                                 vidx[0]= mf->v1;
1001                                                 vidx[1]= mf->v2;
1002                                                 vidx[2]= mf->v3;
1003                                                 vidx[3]= mf->v4;
1004
1005                                                 if (mf->v4)
1006                                                         CalcNormFloat4(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, dlm->mvert[mf->v4].co, nor);
1007                                                 else
1008                                                         CalcNormFloat(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, nor);
1009
1010                                                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
1011                                                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
1012                                                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
1013                                                 Normalise(n1);
1014
1015                                                 for (j=0; j<nverts; j++) {
1016                                                         MVert *mv= &dlm->mvert[vidx[j]];
1017                                                         unsigned int *col1= &col1base[j];
1018                                                         unsigned int *col2= col2base?&col2base[j]:NULL;
1019                                                         unsigned int *mcol= mcolbase?&mcolbase[j]:NULL;
1020                                                         
1021                                                         VECCOPY(vec, mv->co);
1022                                                         Mat4MulVecfl(mat, vec);
1023                                                         if(mf->flag & ME_SMOOTH) vn= vnors+3*vidx[j];
1024                                                         else vn= n1;
1025                                                 
1026                                                         if (need_orco && orco)
1027                                                                 fastshade(vec, vn, &orco[vidx[j]*3], ma, (char *)col1, (char*)col2, (char*) mcol);
1028                                                         else
1029                                                                 fastshade(vec, vn, mv->co, ma, (char *)col1, (char*)col2, (char*) mcol);
1030                                                 }
1031                                         }
1032                                 }
1033                                 MEM_freeN(vnors);
1034                         }
1035                         displistmesh_free(dlm);
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                 if (me->derived) {
1769                         me->derived->release(me->derived);
1770                         me->derived= NULL;
1771                 }
1772
1773                 tex_space_mesh(ob->data);
1774                 
1775                 if (ob!=G.obedit) mesh_modifier(ob, 's');
1776
1777                 if (mesh_uses_displist(me)) {  /* subsurf */
1778                         if (ob==G.obedit) {
1779                                 me->derived= subsurf_make_derived_from_editmesh(em, me->subdiv, me->flag, me->subsurftype);
1780                         } else {
1781                                 me->derived= subsurf_make_derived_from_mesh(me, me->subdiv, me->flag);
1782                         }
1783                 }
1784
1785                 if (ob!=G.obedit) mesh_modifier(ob, 'e');
1786         }
1787         else if(ob->type==OB_MBALL) {
1788                 ob= find_basis_mball(ob);
1789
1790                 metaball_polygonize(ob);
1791                 tex_space_mball(ob);
1792
1793                 object_deform(ob);
1794         }
1795         else if(ob->type==OB_SURF) {
1796                 
1797                 draw= ob->dt;
1798                 cu= ob->data;
1799                 dispbase= &(cu->disp);
1800                 if(dl_onlyzero && dispbase->first) return;
1801                 freedisplist(dispbase);
1802                 
1803                 if(ob==G.obedit) nu= editNurb.first;
1804                 else {
1805                         curve_modifier(ob, 's');
1806                         nu= cu->nurb.first;
1807                 }
1808                 
1809                 while(nu) {
1810                         if(nu->hide==0) {
1811                                 if(nu->pntsv==1) {
1812                                         if(draw==0) len= nu->pntsu;
1813                                         else len= nu->pntsu*nu->resolu;
1814                                         
1815                                         dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1816                                         dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1817                                         
1818                                         BLI_addtail(dispbase, dl);
1819                                         dl->parts= 1;
1820                                         dl->nr= len;
1821                                         dl->col= nu->mat_nr;
1822                                         dl->rt= nu->flag;
1823                                         
1824                                         data= dl->verts;
1825                                         if(nu->flagu & 1) dl->type= DL_POLY;
1826                                         else dl->type= DL_SEGM;
1827                                         
1828                                         if(draw==0) {
1829                                                 bp= nu->bp;
1830                                                 while(len--) {
1831                                                         VECCOPY(data, bp->vec);
1832                                                         bp++;
1833                                                         data+= 3;
1834                                                 }
1835                                         }
1836                                         else makeNurbcurve(nu, data, 3);
1837                                 }
1838                                 else {
1839                                         if(draw==0 && ob==G.obedit) ;
1840                                         else {
1841                                                 if(draw==0) len= nu->pntsu*nu->pntsv;
1842                                                 else len= nu->resolu*nu->resolv;
1843                                                 
1844                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1845                                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1846                                                 BLI_addtail(dispbase, dl);
1847         
1848                                                 if(draw==0) {
1849                                                         dl->parts= nu->pntsv;
1850                                                         dl->nr= nu->pntsu;
1851                                                         if(nu->flagu & 1) dl->flag|= DL_CYCL_U;
1852                                                         if(nu->flagv & 1) dl->flag|= DL_CYCL_V;
1853                                                 }
1854                                                 else {
1855                                                         dl->parts= nu->resolu;  /* in reverse, because makeNurbfaces works that way */
1856                                                         dl->nr= nu->resolv;
1857                                                         if(nu->flagv & 1) dl->flag|= DL_CYCL_U; /* reverse too! */
1858                                                         if(nu->flagu & 1) dl->flag|= DL_CYCL_V;
1859                                                 }
1860                                                 dl->col= nu->mat_nr;
1861                                                 dl->rt= nu->flag;
1862                                                 
1863                                                 data= dl->verts;
1864                                                 dl->type= DL_SURF;
1865                                                 
1866                                                 if(draw==0) {
1867                                                         bp= nu->bp;
1868                                                         while(len--) {
1869                                                                 VECCOPY(data, bp->vec);
1870                                                                 bp++;
1871                                                                 data+= 3;
1872                                                         }
1873                                                 }
1874                                                 else makeNurbfaces(nu, data);
1875                                         }
1876                                 }
1877                         }
1878                         nu= nu->next;
1879                 }
1880                 
1881                 tex_space_curve(cu);
1882
1883                 if(ob!=G.obedit) curve_modifier(ob, 'e');
1884                 if(ob!=G.obedit) object_deform(ob);
1885         }
1886         else if ELEM(ob->type, OB_CURVE, OB_FONT) {
1887                 
1888                 draw= ob->dt;
1889                 cu= ob->data;
1890                 dispbase= &(cu->disp);
1891                 if(dl_onlyzero && dispbase->first) return;
1892                 freedisplist(dispbase);
1893                 
1894                 if(cu->path) free_path(cu->path);
1895                 cu->path= 0;
1896                 
1897                 BLI_freelistN(&(cu->bev));
1898                 
1899                 if(ob!=G.obedit) curve_modifier(ob, 's');
1900                 
1901                 if(ob==G.obedit) {
1902                         if(ob->type==OB_CURVE) curve_to_displist(&editNurb, dispbase);
1903                         else curve_to_displist(&cu->nurb, dispbase);
1904                         if(cu->flag & CU_PATH) makeBevelList(ob);
1905                 }
1906                 else if(cu->ext1==0.0 && cu->ext2==0.0 && cu->bevobj==NULL && cu->width==1.0) {
1907                         curve_to_displist(&cu->nurb, dispbase);
1908                         if(cu->flag & CU_PATH) makeBevelList(ob);
1909                 }
1910                 else {
1911                         
1912                         makeBevelList(ob);
1913
1914                         dlbev.first= dlbev.last= NULL;
1915                         if(cu->ext1!=0.0 || cu->ext2!=0.0 || cu->bevobj) {
1916                                 if(ob->dt!=0) makebevelcurve(ob, &dlbev);
1917                         }
1918
1919                         /* work with bevellist */
1920                         widfac= cu->width-1.0;
1921                         bl= cu->bev.first;
1922                         nu= cu->nurb.first;
1923                         while(bl) {
1924                                 if(dlbev.first==0) {
1925                                         dl= MEM_callocN(sizeof(DispList), "makeDispListbev");
1926                                         dl->verts= MEM_callocN(3*sizeof(float)*bl->nr, "dlverts");
1927                                         BLI_addtail(dispbase, dl);
1928                                         
1929                                         if(bl->poly!= -1) dl->type= DL_POLY;
1930                                         else dl->type= DL_SEGM;
1931                                         
1932                                         if(dl->type==DL_SEGM) dl->flag = (DL_FRONT_CURVE|DL_BACK_CURVE);
1933                                         
1934                                         dl->parts= 1;
1935                                         dl->nr= bl->nr;
1936                                         dl->col= nu->mat_nr;
1937
1938                                         a= dl->nr;
1939                                         bevp= (BevPoint *)(bl+1);
1940                                         data= dl->verts;
1941                                         while(a--) {
1942                                                 data[0]= bevp->x+widfac*bevp->sina;
1943                                                 data[1]= bevp->y+widfac*bevp->cosa;
1944                                                 data[2]= bevp->z;
1945                                                 bevp++;
1946                                                 data+=3;
1947                                         }
1948                                 }
1949                                 else {
1950                                         /* for each part of the bevel use a separate displblock */
1951                                         dlb= dlbev.first;
1952                                         while(dlb) {
1953                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1954                                                 dl->verts= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1955                                                 BLI_addtail(dispbase, dl);
1956                                                 /* dl->type= dlb->type; */
1957
1958                                                 dl->type= DL_SURF;
1959                                                 
1960                                                 dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1961                                                 if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1962                                                 if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1963                                                 
1964                                                 dl->parts= bl->nr;
1965                                                 dl->nr= dlb->nr;
1966                                                 dl->col= nu->mat_nr;
1967                                                 dl->rt= nu->flag;
1968
1969                                                 data= dl->verts;
1970                                                 bevp= (BevPoint *)(bl+1);
1971                                                 for(a=0; a<bl->nr; a++) {       /* for each point of poly make a bevel piece */
1972                                                         float fac;
1973                                                         
1974                                                         /* returns 1.0 if no taper, of course */
1975                                                         fac= calc_taper(cu->taperobj, a, bl->nr);
1976                                                         
1977                                                         /* rotate bevel piece and write in data */
1978                                                         fp1= dlb->verts;
1979                                                         b= dlb->nr;
1980
1981                                                         while(b--) {
1982                                                                 
1983                                                                 if(cu->flag & CU_3D) {
1984                                                                 
1985                                                                         vec[0]= fp1[1]+widfac;
1986                                                                         vec[1]= fp1[2];
1987                                                                         vec[2]= 0.0;
1988                                                                         
1989                                                                         Mat3MulVecfl(bevp->mat, vec);
1990                                                                         
1991                                                                         data[0]= bevp->x+ fac*vec[0];
1992                                                                         data[1]= bevp->y+ fac*vec[1];
1993                                                                         data[2]= bevp->z+ fac*vec[2];
1994                                                                 }
1995                                                                 else {
1996                                                                         data[0]= bevp->x+ fac*(fp1[1]+widfac)*bevp->sina;
1997                                                                         data[1]= bevp->y+ fac*(fp1[1]+widfac)*bevp->cosa;
1998                                                                         data[2]= bevp->z+ fac*fp1[2];
1999                                                                 }
2000
2001                                                                 data+=3;
2002                                                                 fp1+=3;
2003                                                         }
2004
2005                                                         bevp++;
2006                                                 }
2007
2008                                                 dlb= dlb->next;
2009                                         }
2010                                 }
2011
2012                                 bl= bl->next;
2013                                 nu= nu->next;
2014                         }
2015
2016                         if(cu->ext1!=0.0 || cu->ext2!=0.0 || cu->bevobj) {
2017                                 freedisplist(&dlbev);
2018                         }
2019                 }
2020
2021                 if(ob!=G.obedit) curve_modifier(ob, 'e');
2022                 if(ob!=G.obedit) object_deform(ob);
2023
2024                 tex_space_curve(cu);
2025
2026         }
2027         
2028         boundbox_displist(ob);
2029 }
2030
2031
2032 /*******************************/
2033 /*****       OUTLINE       *****/
2034 /*******************************/
2035
2036 typedef struct Sample{
2037         short x, y;
2038 } Sample;
2039
2040 typedef struct Segment{
2041         /* coordinates */
2042         struct Segment * next, * prev;
2043         float co[2];
2044 } Segment;
2045
2046
2047
2048 static int dflt_in_out(struct ImBuf * ibuf, int x, int y)
2049 {
2050         unsigned char * rect;
2051         
2052         if (ibuf == 0) return (0);
2053         if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y || ibuf->rect == 0) return (-1);
2054         
2055         rect = (unsigned char *) (ibuf->rect + (y * ibuf->x) + x);
2056         if (rect[0] > 0x81) return (1);
2057         return(0);
2058 }
2059
2060
2061 static Sample * outline(struct ImBuf * ibuf,
2062                                  int (*in_or_out)(struct ImBuf *, int, int))
2063 {
2064         static int dirs[8][2] = {
2065                 {-1,  0}, {-1,  1},     {0,  1}, {1,  1}, 
2066                 {1,  0}, {1, -1}, {0, -1}, {-1, -1}
2067         };
2068         
2069         int dir, x, y, in, i;
2070         int count, sampcount;
2071         int startx = 0, starty = 0;
2072         Sample * samp, * oldsamp;
2073         
2074         /* input:
2075          * 1 - image 
2076          * 2 - pointer to function that defines which pixel 'in' or 'out' is
2077          */
2078         
2079         if (ibuf == 0) return (0);
2080         if (ibuf->rect == 0) return (0);
2081         
2082         if (in_or_out == 0) in_or_out = dflt_in_out;
2083         in = in_or_out(ibuf, 0, 0);
2084         
2085         /* search for first transition, and continue from there */      
2086         for (y = 0; y < ibuf->y; y++) {
2087                 for (x = 0; x < ibuf->x; x++) {
2088                         if (in_or_out(ibuf, x, y) != in) {
2089                                 /* found first 'other' point !! */
2090                                 
2091                                 if (x != startx) dir = 0;
2092                                 else dir = 6;
2093                                 
2094                                 startx = x; starty = y;
2095                                 count = 1;
2096                                 sampcount = 2000;
2097                                 samp = MEM_mallocN(sampcount * sizeof(Sample), "wire_samples");
2098                                 
2099                                 do{
2100                                         samp[count].x = x; samp[count].y = y;
2101                                         count++;
2102                                         
2103                                         if (count >= sampcount) {
2104                                                 oldsamp = samp;
2105                                                 samp = MEM_mallocN(2 * sampcount * sizeof(Sample), "wire_samples");
2106                                                 memcpy(samp, oldsamp, sampcount * sizeof(Sample));
2107                                                 sampcount *= 2;
2108                                                 MEM_freeN(oldsamp);
2109                                         }
2110                                         
2111                                         i = 0;
2112                                         while(in_or_out(ibuf, x + dirs[dir][0], y + dirs[dir][1]) == in) {
2113                                                 dir = (dir + 1) & 0x7;
2114                                                 if (i++ == 9) break;
2115                                         }
2116                                         
2117                                         if (i >= 8) {
2118                                                 /* this has to be a loose point */
2119                                                 break;
2120                                         }
2121                                         
2122                                         x += dirs[dir][0];
2123                                         y += dirs[dir][1];
2124                                         dir = (dir - 3) & 0x7;
2125                                 } while(x != startx || y != starty);
2126                                 
2127                                 if (i >= 8) {
2128                                         /* patch for loose points */
2129                                         MEM_freeN(samp);
2130                                 } else {
2131                                         count = count - 1;
2132                                         samp[0].x = count >> 16;
2133                                         samp[0].y = count;
2134                                         return(samp);
2135                                 }
2136                         }
2137                 }
2138         }
2139         /* printf("no transition \n"); */
2140         return(0);
2141 }
2142
2143
2144
2145 /*******************************/
2146 /*****      WIREFRAME      *****/
2147 /*******************************/
2148
2149
2150 static float DistToLine2D(short *v1, short *v2, short *v3)   /* using Hesse formula :NO LINE PIECE! */
2151 {
2152         float a[2],deler;
2153
2154         a[0] = v2[1]-v3[1];
2155         a[1] = v3[0]-v2[0];
2156         deler = sqrt(a[0]*a[0]+a[1]*a[1]);
2157         if(deler == 0.0) return 0;
2158
2159         return fabs((v1[0]-v2[0])*a[0]+(v1[1]-v2[1])*a[1])/deler;
2160
2161 }
2162
2163 static float ComputeMaxShpError(Sample *samp, int first, int last, int *splitPoint)
2164     /* samp:  Array of digitized points */
2165     /* first, last:  Indices defining region    */
2166     /* splitpoint:  Point of maximum error      */
2167 {
2168     int         i;
2169     float       maxDist;                                /*  Maximum error               */
2170     float       dist;                                   /*  Current error               */
2171  
2172     *splitPoint = (last - first + 1) / 2;
2173     maxDist = 0.0;
2174         
2175     for (i = first + 1; i < last; i++) {                                
2176                 dist = DistToLine2D((short *)(samp+i), (short *)(samp+first), (short *)(samp+last));
2177
2178                 if (dist >= maxDist) {
2179                 maxDist = dist;
2180                 *splitPoint = i;
2181                 }
2182     }
2183
2184     return (maxDist);
2185 }
2186
2187
2188 static void FitPoly(Sample *samp, int first, int last, float shperr, ListBase *seglist)
2189     /* Samp: Array of digitized points */
2190     /* first,last: Indices of first and last pts in region */
2191     /* spherr: User-defined error squared          */
2192 {
2193     Segment     * seg;                          /* Control points segment*/
2194     float       maxError;                       /*  Maximum fitting error        */
2195     int         splitPoint;                     /*  Point to split point set at  */
2196     int         nPts;                           /*  Number of points in subset  */
2197         
2198     nPts = last - first + 1;
2199
2200     /*  Use heuristic if region only has two points in it */
2201
2202         seg = MEM_mallocN(sizeof(Segment), "wure_segment");
2203
2204         seg->co[0] = samp[first].x;
2205         seg->co[1] = samp[first].y;
2206         
2207     if (nPts == 2) {
2208                 BLI_addtail(seglist, seg);
2209                 return;
2210     }
2211
2212         maxError = ComputeMaxShpError(samp, first, last, &splitPoint);
2213         if (maxError < shperr) {
2214                 BLI_addtail(seglist, seg);
2215                 return;
2216         }
2217         
2218     /* Fitting failed -- split at max error point and fit recursively */
2219         
2220     FitPoly(samp, first, splitPoint, shperr, seglist);
2221     FitPoly(samp, splitPoint, last, shperr, seglist);
2222         
2223         MEM_freeN(seg);
2224 }
2225
2226
2227 static void ibuf2wire(ListBase * wireframe, struct ImBuf * ibuf)
2228 {
2229         int count;
2230         Sample * samp;
2231         
2232         /* first make a list of samples */
2233         
2234         samp = outline(ibuf, 0);
2235         if (samp == 0) return;
2236         
2237         count = (samp[0].x << 16) + samp[0].y;
2238         if (count) FitPoly(samp, 1, count, 1.0, wireframe); /* was 3.0. Frank */
2239
2240         MEM_freeN(samp);
2241 }
2242
2243
2244
2245 void imagestodisplist(void)
2246 {
2247         Base *base;
2248         Object *ob;
2249         Material *ma;
2250         Tex *tex;
2251         Mesh *me;
2252         ListBase _wireframe, *wireframe;
2253         DispList *dl;
2254         Segment *seg;
2255         float *data, xfac, yfac, xsi, ysi, vec[3], dum;
2256         int tot;
2257         
2258         _wireframe.first= 0;
2259         _wireframe.last= 0;
2260         wireframe = &_wireframe;
2261         
2262         init_render_textures();
2263         
2264         base= G.scene->base.first;
2265         while(base) {
2266                 if(( (base->flag & SELECT) && (base->lay & G.scene->lay) ) ) {
2267                         if( base->object->type==OB_MESH) {
2268                                 ob= base->object;
2269                                 me= ob->data;
2270                                 
2271                                 ma= give_current_material(ob, 1);
2272         
2273                                 if(ma && ma->mtex[0] && ma->mtex[0]->tex) {
2274                                         tex= ma->mtex[0]->tex;
2275                                         
2276                                         /* this takes care of correct loading of new imbufs */
2277                                         externtex(ma->mtex[0], vec, &dum, &dum, &dum, &dum, &dum);
2278                                         
2279                                         if(tex->type==TEX_IMAGE && tex->ima && tex->ima->ibuf) {                                
2280                                                 
2281                                                 ob->dtx |= OB_DRAWIMAGE;
2282                                                 
2283                                                 ibuf2wire(wireframe, tex->ima->ibuf);
2284
2285                                                 tot= 0;
2286                                                 seg = wireframe->first;
2287                                                 while (seg) {
2288                                                         tot++;
2289                                                         seg = seg->next;
2290                                                 }
2291         
2292                                                 if(tot) {
2293                                                         freedisplist(&(ob->disp));
2294
2295                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListimage");
2296                                                         dl->verts= MEM_callocN(3*sizeof(float)*tot, "dlverts");
2297                                                         
2298                                                         BLI_addtail(&(ob->disp), dl);
2299                                                         dl->type= DL_POLY;
2300                                                         dl->parts= 1;
2301                                                         dl->nr= tot;
2302                                                         
2303                                                         xsi= 0.5*(tex->ima->ibuf->x);
2304                                                         ysi= 0.5*(tex->ima->ibuf->y);
2305                                                         xfac= me->size[0]/xsi;
2306                                                         yfac= me->size[1]/ysi;
2307                                                                                                 
2308                                                         data= dl->verts;
2309                                                         seg = wireframe->first;
2310                                                         while (seg) {
2311                                                                 data[0]= xfac*(seg->co[0]-xsi);
2312                                                                 data[1]= yfac*(seg->co[1]-ysi);
2313                                                                 data+= 3;
2314                                                                 seg = seg->next;
2315                                                         }
2316                                                         BLI_freelistN(wireframe);
2317                                                 }
2318                                         }
2319                                 }
2320                         }
2321                 }
2322                 base= base->next;
2323         }
2324         
2325         end_render_textures();
2326         
2327         allqueue(REDRAWVIEW3D, 0);
2328 }
2329
2330 /* on frame change */
2331 /* new method: only frees displists, and relies on 
2332    drawobject.c & convertscene.c to build it when needed
2333 */
2334 void test_all_displists(void)
2335 {
2336         Base *base;
2337         Object *ob;
2338         unsigned int lay;
2339         int makedisp, freedisp;
2340         
2341         /* background */        
2342         lay= G.scene->lay;
2343         
2344         /* clear flags, we use them because parent->parents are evaluated too */
2345         base= G.scene->base.first;
2346         while(base) {
2347                 if(base->lay & lay) {
2348                         base->object->flag &= ~(BA_DISP_UPDATE|BA_WHERE_UPDATE);
2349                 }
2350                 if(base->next==0 && G.scene->set && base==G.scene->base.last) base= G.scene->set->base.first;
2351                 else base= base->next;
2352         }
2353         
2354         base= G.scene->base.first;
2355         while(base) {
2356                 if(base->lay & lay) {
2357                         ob= base->object;
2358                         makedisp= freedisp= 0;
2359                         
2360                         if(ob->type==OB_MBALL && (ob->ipo || ob->parent)) {
2361                                 // find metaball object holding the displist
2362                                 // WARNING: if more metaballs have IPO's the displist
2363                                 // is recalculated to often... do we free the displist
2364                                 // and rely on the drawobject.c to build it again when needed
2365
2366                                 if(ob->disp.first == NULL) {
2367                                         ob= find_basis_mball(ob);
2368                                 }
2369                                 makedisp= 1;
2370                         }
2371                         else if(ob->parent) {
2372                                 
2373                                 if (ob->parent->type == OB_LATTICE)
2374                                         freedisp= 1;
2375                                 else if ((ob->parent->type==OB_ARMATURE) && (ob->partype == PARSKEL))
2376                                         makedisp= 1;
2377                                 else if ((ob->parent->type==OB_CURVE) && (ob->partype == PARSKEL))
2378                                         freedisp= 1;
2379                                 else if(ob->partype==PARVERT1 || ob->partype==PARVERT3) {
2380                                         if(ob->parent->parent) 
2381                                                 ob->parent->flag |= BA_DISP_UPDATE;
2382                                         else if(ob->parent->effect.first)       // stupid test for wave
2383                                                 ob->parent->flag |= BA_DISP_UPDATE;
2384                                 }
2385                         }
2386
2387                         if(ob->hooks.first) {
2388                                 ObHook *hook;
2389                                 for(hook= ob->hooks.first; hook; hook= hook->next) {
2390                                         if(hook->parent) 
2391                                                 freedisp= 1;
2392                                         break;
2393                                 }
2394                         }
2395                         
2396                         if(ob->softflag & 0x01) freedisplist_object(ob);
2397                         /* warn, ob pointer changed in case of OB_MALL */
2398
2399                         if ELEM(ob->type, OB_CURVE, OB_SURF) {
2400                                 if(ob!=G.obedit) {
2401                                         Curve *cu= ob->data;
2402                                         
2403                                         if(cu->key ) makedisp= 1;
2404                                         if(cu->bevobj) {
2405                                                 Curve *cu1= cu->bevobj->data;
2406                                                 if(cu1->key ) freedisp= 1;
2407                                         }
2408                                         if(cu->taperobj) {
2409                                                 Curve *cu1= cu->taperobj->data;
2410                                                 if(cu1->key ) freedisp= 1;
2411                                         }
2412                                 }
2413                         }
2414                         else if(ob->type==OB_FONT) {
2415                                 Curve *cu= ob->data;
2416                                 if(cu->textoncurve) {
2417                                         if( ((Curve *)cu->textoncurve->data)->key ) {
2418                                                 text_to_curve(ob, 0);
2419                                                 freedisp= 1;
2420                                         }
2421                                 }
2422                         }
2423                         else if(ob->type==OB_MESH) {
2424                                 if(ob->effect.first) {
2425                                         Effect *eff= ob->effect.first;
2426                                         while(eff) {
2427                                                 if(eff->type==EFF_WAVE) {
2428                                                         freedisp= 1;
2429                                                         break;
2430                                                 }
2431                                                 eff= eff->next;
2432                                         }
2433                                 }
2434                                 if(ob!=G.obedit) {
2435                                         if(( ((Mesh *)(ob->data))->key )) 
2436                                                 freedisp= 1;
2437                                 }
2438                         }
2439                         if(freedisp) ob->flag |= BA_WHERE_UPDATE;
2440                         if(makedisp) ob->flag |= BA_DISP_UPDATE;
2441                 }
2442                 if(base->next==0 && G.scene->set && base==G.scene->base.last) base= G.scene->set->base.first;
2443                 else base= base->next;
2444         }
2445         
2446         /* going over the flags to free or make displists */
2447         base= G.scene->base.first;
2448         while(base) {
2449                 if(base->lay & lay) {
2450                         ob= base->object;
2451                         if(ob->flag & BA_DISP_UPDATE) makeDispList(ob);
2452                         else if(ob->flag & BA_WHERE_UPDATE) freedisplist_object(ob);
2453                 }
2454                 if(base->next==0 && G.scene->set && base==G.scene->base.last) base= G.scene->set->base.first;
2455                 else base= base->next;
2456         }
2457         
2458 }
2459
2460
2461 void boundbox_displist(Object *ob)
2462 {
2463         BoundBox *bb=0;
2464         float min[3], max[3];
2465         DispList *dl;
2466         float *vert;
2467         int a, tot=0;
2468         
2469         INIT_MINMAX(min, max);
2470
2471         if(ob->type==OB_MESH) {
2472                 Mesh *me= ob->data;
2473
2474                 dl= find_displist(&ob->disp, DL_VERTS);
2475                 if(!dl) return;
2476
2477                 if(me->bb==0) me->bb= MEM_callocN(sizeof(BoundBox), "boundbox");        
2478                 bb= me->bb;
2479
2480                 vert= dl->verts;
2481                 for(a=0; a<dl->nr; a++, vert+=3) {
2482                         DO_MINMAX(vert, min, max);
2483                 }
2484         }
2485         else if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2486                 Curve *cu= ob->data;
2487
2488                 if(cu->bb==0) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");        
2489                 bb= cu->bb;
2490                 
2491                 dl= cu->disp.first;
2492
2493                 while (dl) {
2494                         if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
2495                         else tot= dl->nr*dl->parts;
2496                         
2497                         vert= dl->verts;
2498                         for(a=0; a<tot; a++, vert+=3) {
2499                                 DO_MINMAX(vert, min, max);
2500                         }
2501
2502                         dl= dl->next;
2503                 }
2504         }
2505         
2506         if(bb) {
2507                 bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= min[0];
2508                 bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= max[0];
2509                 
2510                 bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= min[1];
2511                 bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= max[1];
2512         
2513                 bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= min[2];
2514                 bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= max[2];
2515         }
2516 }
2517