Merge from 2.5 r21160 through r21285
[blender.git] / source / blender / blenkernel / intern / displist.c
1 /*  displist.c
2  * 
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <math.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "IMB_imbuf_types.h"
39
40 #include "DNA_texture_types.h"
41 #include "DNA_meta_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_effect_types.h"
44 #include "DNA_listBase.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_object_force.h"
48 #include "DNA_mesh_types.h"
49 #include "DNA_meshdata_types.h"
50 #include "DNA_modifier_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_image_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_view3d_types.h"
55 #include "DNA_lattice_types.h"
56 #include "DNA_key_types.h"
57
58 #include "BLI_blenlib.h"
59 #include "BLI_arithb.h"
60 #include "BLI_editVert.h"
61 #include "BLI_edgehash.h"
62
63 #include "BKE_utildefines.h"
64 #include "BKE_global.h"
65 #include "BKE_displist.h"
66 #include "BKE_deform.h"
67 #include "BKE_DerivedMesh.h"
68 #include "BKE_object.h"
69 #include "BKE_world.h"
70 #include "BKE_mesh.h"
71 #include "BKE_effect.h"
72 #include "BKE_mball.h"
73 #include "BKE_material.h"
74 #include "BKE_curve.h"
75 #include "BKE_key.h"
76 #include "BKE_anim.h"
77 #include "BKE_screen.h"
78 #include "BKE_texture.h"
79 #include "BKE_library.h"
80 #include "BKE_font.h"
81 #include "BKE_lattice.h"
82 #include "BKE_scene.h"
83 #include "BKE_subsurf.h"
84 #include "BKE_modifier.h"
85 #include "BKE_customdata.h"
86
87 #include "RE_pipeline.h"
88 #include "RE_shader_ext.h"
89
90 #include "BLO_sys_types.h" // for intptr_t support
91
92
93 static void boundbox_displist(Object *ob);
94
95 void free_disp_elem(DispList *dl)
96 {
97         if(dl) {
98                 if(dl->verts) MEM_freeN(dl->verts);
99                 if(dl->nors) MEM_freeN(dl->nors);
100                 if(dl->index) MEM_freeN(dl->index);
101                 if(dl->col1) MEM_freeN(dl->col1);
102                 if(dl->col2) MEM_freeN(dl->col2);
103                 if(dl->bevelSplitFlag) MEM_freeN(dl->bevelSplitFlag);
104                 MEM_freeN(dl);
105         }
106 }
107
108 void freedisplist(ListBase *lb)
109 {
110         DispList *dl;
111
112         dl= lb->first;
113         while(dl) {
114                 BLI_remlink(lb, dl);
115                 free_disp_elem(dl);
116                 dl= lb->first;
117         }
118 }
119
120 DispList *find_displist_create(ListBase *lb, int type)
121 {
122         DispList *dl;
123         
124         dl= lb->first;
125         while(dl) {
126                 if(dl->type==type) return dl;
127                 dl= dl->next;
128         }
129
130         dl= MEM_callocN(sizeof(DispList), "find_disp");
131         dl->type= type;
132         BLI_addtail(lb, dl);
133
134         return dl;
135 }
136
137 DispList *find_displist(ListBase *lb, int type)
138 {
139         DispList *dl;
140         
141         dl= lb->first;
142         while(dl) {
143                 if(dl->type==type) return dl;
144                 dl= dl->next;
145         }
146
147         return 0;
148 }
149
150 int displist_has_faces(ListBase *lb)
151 {
152         DispList *dl;
153         for(dl= lb->first; dl; dl= dl->next) {
154                 if ELEM3(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)
155                         return 1;
156         }
157         return 0;
158 }
159
160 void copy_displist(ListBase *lbn, ListBase *lb)
161 {
162         DispList *dln, *dl;
163         
164         lbn->first= lbn->last= 0;
165         
166         dl= lb->first;
167         while(dl) {
168                 
169                 dln= MEM_dupallocN(dl);
170                 BLI_addtail(lbn, dln);
171                 dln->verts= MEM_dupallocN(dl->verts);
172                 dln->nors= MEM_dupallocN(dl->nors);
173                 dln->index= MEM_dupallocN(dl->index);
174                 dln->col1= MEM_dupallocN(dl->col1);
175                 dln->col2= MEM_dupallocN(dl->col2);
176                 
177                 dl= dl->next;
178         }
179 }
180
181 void addnormalsDispList(Object *ob, ListBase *lb)
182 {
183         DispList *dl = NULL;
184         float *vdata, *ndata, nor[3];
185         float *v1, *v2, *v3, *v4;
186         float *n1, *n2, *n3, *n4;
187         int a, b, p1, p2, p3, p4;
188
189
190         dl= lb->first;
191         
192         while(dl) {
193                 if(dl->type==DL_INDEX3) {
194                         if(dl->nors==NULL) {
195                                 dl->nors= MEM_callocN(sizeof(float)*3, "dlnors");
196                                 if(dl->verts[2]<0.0) dl->nors[2]= -1.0;
197                                 else dl->nors[2]= 1.0;
198                         }
199                 }
200                 else if(dl->type==DL_SURF) {
201                         if(dl->nors==NULL) {
202                                 dl->nors= MEM_callocN(sizeof(float)*3*dl->nr*dl->parts, "dlnors");
203                                 
204                                 vdata= dl->verts;
205                                 ndata= dl->nors;
206                                 
207                                 for(a=0; a<dl->parts; a++) {
208                                         
209                                         if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
210                                                 break;
211         
212                                         v1= vdata+ 3*p1; 
213                                         n1= ndata+ 3*p1;
214                                         v2= vdata+ 3*p2; 
215                                         n2= ndata+ 3*p2;
216                                         v3= vdata+ 3*p3; 
217                                         n3= ndata+ 3*p3;
218                                         v4= vdata+ 3*p4; 
219                                         n4= ndata+ 3*p4;
220                                         
221                                         for(; b<dl->nr; b++) {
222         
223                                                 CalcNormFloat4(v1, v3, v4, v2, nor);
224         
225                                                 VecAddf(n1, n1, nor);
226                                                 VecAddf(n2, n2, nor);
227                                                 VecAddf(n3, n3, nor);
228                                                 VecAddf(n4, n4, nor);
229         
230                                                 v2= v1; v1+= 3;
231                                                 v4= v3; v3+= 3;
232                                                 n2= n1; n1+= 3;
233                                                 n4= n3; n3+= 3;
234                                         }
235                                 }
236                                 a= dl->parts*dl->nr;
237                                 v1= ndata;
238                                 while(a--) {
239                                         Normalize(v1);
240                                         v1+= 3;
241                                 }
242                         }
243                 }
244                 dl= dl->next;
245         }
246 }
247
248 void count_displist(ListBase *lb, int *totvert, int *totface)
249 {
250         DispList *dl;
251         
252         dl= lb->first;
253         while(dl) {
254                 
255                 switch(dl->type) {
256                         case DL_SURF:
257                                 *totvert+= dl->nr*dl->parts;
258                                 *totface+= (dl->nr-1)*(dl->parts-1);
259                                 break;
260                         case DL_INDEX3:
261                         case DL_INDEX4:
262                                 *totvert+= dl->nr;
263                                 *totface+= dl->parts;
264                                 break;
265                         case DL_POLY:
266                         case DL_SEGM:
267                                 *totvert+= dl->nr*dl->parts;
268                 }
269                 
270                 dl= dl->next;
271         }
272 }
273
274 int surfindex_displist(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4)
275 {
276         if((dl->flag & DL_CYCL_V)==0 && a==(dl->parts)-1) {
277                 return 0;
278         }
279         
280         if(dl->flag & DL_CYCL_U) {
281                 (*p1)= dl->nr*a;
282                 (*p2)= (*p1)+ dl->nr-1;
283                 (*p3)= (*p1)+ dl->nr;
284                 (*p4)= (*p2)+ dl->nr;
285                 (*b)= 0;
286         } else {
287                 (*p2)= dl->nr*a;
288                 (*p1)= (*p2)+1;
289                 (*p4)= (*p2)+ dl->nr;
290                 (*p3)= (*p1)+ dl->nr;
291                 (*b)= 1;
292         }
293         
294         if( (dl->flag & DL_CYCL_V) && a==dl->parts-1) {                     \
295                 (*p3)-= dl->nr*dl->parts;                                   \
296                 (*p4)-= dl->nr*dl->parts;                                   \
297         }
298         
299         return 1;
300 }
301
302 /* ***************************** shade displist. note colors now are in rgb(a) order ******************** */
303
304 /* create default shade input... save cpu cycles with ugly global */
305 /* XXXX bad code warning: local ShadeInput initialize... */
306 static ShadeInput shi;
307 static void init_fastshade_shadeinput(Render *re)
308 {
309         memset(&shi, 0, sizeof(ShadeInput));
310         shi.lay= RE_GetScene(re)->lay;
311         shi.view[2]= -1.0f;
312         shi.passflag= SCE_PASS_COMBINED;
313         shi.combinedflag= -1;
314 }
315
316 static Render *fastshade_get_render(Scene *scene)
317 {
318         /* XXX ugly global still, but we can't do preview while rendering */
319         if(G.rendering==0) {
320                 
321                 Render *re= RE_GetRender("_Shade View_");
322                 if(re==NULL) {
323                         re= RE_NewRender("_Shade View_");
324                 
325                         RE_Database_Baking(re, scene, 0, 0);    /* 0= no faces */
326                 }
327                 return re;
328         }
329         
330         return NULL;
331 }
332
333 /* called on file reading */
334 void fastshade_free_render(void)
335 {
336         Render *re= RE_GetRender("_Shade View_");
337         
338         if(re) {
339                 RE_Database_Free(re);
340                 RE_FreeRender(re);
341         }
342 }
343
344 static int fastshade_customdata_layer_num(int n, int active)
345 {   
346         /* make the active layer the first */
347         if (n == active) return 0;
348         else if (n < active) return n+1;
349         else return n;
350 }
351
352 static void fastshade_customdata(CustomData *fdata, int a, int j, Material *ma)
353 {
354         CustomDataLayer *layer;
355         MTFace *mtface;
356         int index, n, needuv= ma->texco & TEXCO_UV;
357         char *vertcol;
358
359         shi.totuv= 0;
360         shi.totcol= 0;
361
362         for(index=0; index<fdata->totlayer; index++) {
363                 layer= &fdata->layers[index];
364                 
365                 if(needuv && layer->type == CD_MTFACE && shi.totuv < MAX_MTFACE) {
366                         n= fastshade_customdata_layer_num(shi.totuv, layer->active_rnd);
367                         mtface= &((MTFace*)layer->data)[a];
368
369                         shi.uv[shi.totuv].uv[0]= 2.0f*mtface->uv[j][0]-1.0f;
370                         shi.uv[shi.totuv].uv[1]= 2.0f*mtface->uv[j][1]-1.0f;
371                         shi.uv[shi.totuv].uv[2]= 1.0f;
372
373                         shi.uv[shi.totuv].name= layer->name;
374                         shi.totuv++;
375                 }
376                 else if(layer->type == CD_MCOL && shi.totcol < MAX_MCOL) {
377                         n= fastshade_customdata_layer_num(shi.totcol, layer->active_rnd);
378                         vertcol= (char*)&((MCol*)layer->data)[a*4 + j];
379
380                         shi.col[shi.totcol].col[0]= ((float)vertcol[3])/255.0f;
381                         shi.col[shi.totcol].col[1]= ((float)vertcol[2])/255.0f;
382                         shi.col[shi.totcol].col[2]= ((float)vertcol[1])/255.0f;
383
384                         shi.col[shi.totcol].name= layer->name;
385                         shi.totcol++;
386                 }
387         }
388
389         if(needuv && shi.totuv == 0)
390                 VECCOPY(shi.uv[0].uv, shi.lo);
391
392         if(shi.totcol)
393                 VECCOPY(shi.vcol, shi.col[0].col);
394 }
395
396 static void fastshade(float *co, float *nor, float *orco, Material *ma, char *col1, char *col2)
397 {
398         ShadeResult shr;
399         int a;
400         
401         VECCOPY(shi.co, co);
402         shi.vn[0]= -nor[0];
403         shi.vn[1]= -nor[1];
404         shi.vn[2]= -nor[2];
405         VECCOPY(shi.vno, shi.vn);
406         VECCOPY(shi.facenor, shi.vn);
407         
408         if(ma->texco) {
409                 VECCOPY(shi.lo, orco);
410                 
411                 if(ma->texco & TEXCO_GLOB) {
412                         VECCOPY(shi.gl, shi.lo);
413                 }
414                 if(ma->texco & TEXCO_WINDOW) {
415                         VECCOPY(shi.winco, shi.lo);
416                 }
417                 if(ma->texco & TEXCO_STICKY) {
418                         VECCOPY(shi.sticky, shi.lo);
419                 }
420                 if(ma->texco & TEXCO_OBJECT) {
421                         VECCOPY(shi.co, shi.lo);
422                 }
423                 if(ma->texco & TEXCO_NORM) {
424                         VECCOPY(shi.orn, shi.vn);
425                 }
426                 if(ma->texco & TEXCO_REFL) {
427                         float inp= 2.0*(shi.vn[2]);
428                         shi.ref[0]= (inp*shi.vn[0]);
429                         shi.ref[1]= (inp*shi.vn[1]);
430                         shi.ref[2]= (-1.0+inp*shi.vn[2]);
431                 }
432         }
433         
434         shi.mat= ma;    /* set each time... node shaders change it */
435         RE_shade_external(NULL, &shi, &shr);
436         
437         a= 256.0f*(shr.combined[0]);
438         col1[0]= CLAMPIS(a, 0, 255);
439         a= 256.0f*(shr.combined[1]);
440         col1[1]= CLAMPIS(a, 0, 255);
441         a= 256.0f*(shr.combined[2]);
442         col1[2]= CLAMPIS(a, 0, 255);
443         
444         if(col2) {
445                 shi.vn[0]= -shi.vn[0];
446                 shi.vn[1]= -shi.vn[1];
447                 shi.vn[2]= -shi.vn[2];
448                 
449                 shi.mat= ma;    /* set each time... node shaders change it */
450                 RE_shade_external(NULL, &shi, &shr);
451                 
452                 a= 256.0f*(shr.combined[0]);
453                 col2[0]= CLAMPIS(a, 0, 255);
454                 a= 256.0f*(shr.combined[1]);
455                 col2[1]= CLAMPIS(a, 0, 255);
456                 a= 256.0f*(shr.combined[2]);
457                 col2[2]= CLAMPIS(a, 0, 255);
458         }
459 }
460
461 static void init_fastshade_for_ob(Render *re, Object *ob, int *need_orco_r, float mat[4][4], float imat[3][3])
462 {
463         float tmat[4][4];
464         float amb[3]= {0.0f, 0.0f, 0.0f};
465         int a;
466         
467         /* initialize globals in render */
468         RE_shade_external(re, NULL, NULL);
469
470         /* initialize global here */
471         init_fastshade_shadeinput(re);
472         
473         RE_DataBase_GetView(re, tmat);
474         Mat4MulMat4(mat, ob->obmat, tmat);
475         
476         Mat4Invert(tmat, mat);
477         Mat3CpyMat4(imat, tmat);
478         if(ob->transflag & OB_NEG_SCALE) Mat3MulFloat((float *)imat, -1.0);
479         
480         if (need_orco_r) *need_orco_r= 0;
481         for(a=0; a<ob->totcol; a++) {
482                 Material *ma= give_current_material(ob, a+1);
483                 if(ma) {
484                         init_render_material(ma, 0, amb);
485
486                         if(ma->texco & TEXCO_ORCO) {
487                                 if (need_orco_r) *need_orco_r= 1;
488                         }
489                 }
490         }
491 }
492
493 static void end_fastshade_for_ob(Object *ob)
494 {
495         int a;
496         
497         for(a=0; a<ob->totcol; a++) {
498                 Material *ma= give_current_material(ob, a+1);
499                 if(ma)
500                         end_render_material(ma);
501         }
502 }
503
504
505 static void mesh_create_shadedColors(Render *re, Object *ob, int onlyForMesh, unsigned int **col1_r, unsigned int **col2_r)
506 {
507         Mesh *me= ob->data;
508         DerivedMesh *dm;
509         MVert *mvert;
510         MFace *mface;
511         unsigned int *col1, *col2;
512         float *orco, *vnors, *nors, imat[3][3], mat[4][4], vec[3];
513         int a, i, need_orco, totface, totvert;
514         CustomDataMask dataMask = CD_MASK_BAREMESH | CD_MASK_MCOL
515                                   | CD_MASK_MTFACE | CD_MASK_NORMAL;
516
517
518         init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
519
520         if(need_orco)
521                 dataMask |= CD_MASK_ORCO;
522
523         if (onlyForMesh)
524                 dm = mesh_get_derived_deform(RE_GetScene(re), ob, dataMask);
525         else
526                 dm = mesh_get_derived_final(RE_GetScene(re), ob, dataMask);
527         
528         mvert = dm->getVertArray(dm);
529         mface = dm->getFaceArray(dm);
530         nors = dm->getFaceDataArray(dm, CD_NORMAL);
531         totvert = dm->getNumVerts(dm);
532         totface = dm->getNumFaces(dm);
533         orco= dm->getVertDataArray(dm, CD_ORCO);
534
535         if (onlyForMesh) {
536                 col1 = *col1_r;
537                 col2 = NULL;
538         } else {
539                 *col1_r = col1 = MEM_mallocN(sizeof(*col1)*totface*4, "col1");
540
541                 if (col2_r && (me->flag & ME_TWOSIDED))
542                         col2 = MEM_mallocN(sizeof(*col2)*totface*4, "col2");
543                 else
544                         col2 = NULL;
545                 
546                 if (col2_r) *col2_r = col2;
547         }
548
549                 /* vertexnormals */
550         vnors= MEM_mallocN(totvert*3*sizeof(float), "vnors disp");
551         for (a=0; a<totvert; a++) {
552                 MVert *mv = &mvert[a];
553                 float *vn= &vnors[a*3];
554                 float xn= mv->no[0]; 
555                 float yn= mv->no[1]; 
556                 float zn= mv->no[2];
557                 
558                         /* transpose ! */
559                 vn[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
560                 vn[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
561                 vn[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
562                 Normalize(vn);
563         }               
564
565         for (i=0; i<totface; i++) {
566                 extern Material defmaterial;    /* material.c */
567                 MFace *mf= &mface[i];
568                 Material *ma= give_current_material(ob, mf->mat_nr+1);
569                 int j, vidx[4], nverts= mf->v4?4:3;
570                 unsigned char *col1base= (unsigned char*) &col1[i*4];
571                 unsigned char *col2base= (unsigned char*) (col2?&col2[i*4]:NULL);
572                 float nor[3], n1[3];
573                 
574                 if(ma==NULL) ma= &defmaterial;
575                 
576                 vidx[0]= mf->v1;
577                 vidx[1]= mf->v2;
578                 vidx[2]= mf->v3;
579                 vidx[3]= mf->v4;
580
581                 if (nors) {
582                         VECCOPY(nor, &nors[i*3]);
583                 } else {
584                         if (mf->v4)
585                                 CalcNormFloat4(mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co, mvert[mf->v4].co, nor);
586                         else
587                                 CalcNormFloat(mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co, nor);
588                 }
589
590                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
591                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
592                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
593                 Normalize(n1);
594
595                 for (j=0; j<nverts; j++) {
596                         MVert *mv= &mvert[vidx[j]];
597                         char *col1= (char*)&col1base[j*4];
598                         char *col2= (char*)(col2base?&col2base[j*4]:NULL);
599                         float *vn = (mf->flag & ME_SMOOTH)?&vnors[3*vidx[j]]:n1;
600                         
601                         VECCOPY(vec, mv->co);
602                         Mat4MulVecfl(mat, vec);
603                         vec[0]+= 0.001*vn[0];
604                         vec[1]+= 0.001*vn[1];
605                         vec[2]+= 0.001*vn[2];
606
607                         fastshade_customdata(&dm->faceData, i, j, ma);
608                         fastshade(vec, vn, orco?&orco[vidx[j]*3]:mv->co, ma, col1, col2);
609                 }
610         } 
611         MEM_freeN(vnors);
612
613         dm->release(dm);
614
615         end_fastshade_for_ob(ob);
616 }
617
618 void shadeMeshMCol(Scene *scene, Object *ob, Mesh *me)
619 {
620         Render *re= fastshade_get_render(scene);
621         int a;
622         char *cp;
623         unsigned int *mcol= (unsigned int*)me->mcol;
624         
625         if(re) {
626                 mesh_create_shadedColors(re, ob, 1, &mcol, NULL);
627                 me->mcol= (MCol*)mcol;
628
629                 /* swap bytes */
630                 for(cp= (char *)me->mcol, a= 4*me->totface; a>0; a--, cp+=4) {
631                         SWAP(char, cp[0], cp[3]);
632                         SWAP(char, cp[1], cp[2]);
633                 }
634         }
635 }
636
637 /* has base pointer, to check for layer */
638 /* called from drawobject.c */
639 void shadeDispList(Scene *scene, Base *base)
640 {
641         Object *ob= base->object;
642         DispList *dl, *dlob;
643         Material *ma = NULL;
644         Curve *cu;
645         Render *re;
646         float imat[3][3], mat[4][4], vec[3];
647         float *fp, *nor, n1[3];
648         unsigned int *col1;
649         int a, need_orco;
650         
651         re= fastshade_get_render(scene);
652         if(re==NULL)
653                 return;
654         
655         dl = find_displist(&ob->disp, DL_VERTCOL);
656         if (dl) {
657                 BLI_remlink(&ob->disp, dl);
658                 free_disp_elem(dl);
659         }
660
661         if(ob->type==OB_MESH) {
662                 dl= MEM_callocN(sizeof(DispList), "displistshade");
663                 dl->type= DL_VERTCOL;
664
665                 mesh_create_shadedColors(re, ob, 0, &dl->col1, &dl->col2);
666
667                 /* add dl to ob->disp after mesh_create_shadedColors, because it
668                    might indirectly free ob->disp */
669                 BLI_addtail(&ob->disp, dl);
670         }
671         else {
672
673                 init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
674                 
675                 if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
676                 
677                         /* now we need the normals */
678                         cu= ob->data;
679                         dl= cu->disp.first;
680                         
681                         while(dl) {
682                                 extern Material defmaterial;    /* material.c */
683                                 
684                                 dlob= MEM_callocN(sizeof(DispList), "displistshade");
685                                 BLI_addtail(&ob->disp, dlob);
686                                 dlob->type= DL_VERTCOL;
687                                 dlob->parts= dl->parts;
688                                 dlob->nr= dl->nr;
689                                 
690                                 if(dl->type==DL_INDEX3) {
691                                         col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
692                                 }
693                                 else {
694                                         col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->parts*dl->nr, "col1");
695                                 }
696                                 
697                         
698                                 ma= give_current_material(ob, dl->col+1);
699                                 if(ma==NULL) ma= &defmaterial;
700                                 
701                                 if(dl->type==DL_INDEX3) {
702                                         if(dl->nors) {
703                                                 /* there's just one normal */
704                                                 n1[0]= imat[0][0]*dl->nors[0]+imat[0][1]*dl->nors[1]+imat[0][2]*dl->nors[2];
705                                                 n1[1]= imat[1][0]*dl->nors[0]+imat[1][1]*dl->nors[1]+imat[1][2]*dl->nors[2];
706                                                 n1[2]= imat[2][0]*dl->nors[0]+imat[2][1]*dl->nors[1]+imat[2][2]*dl->nors[2];
707                                                 Normalize(n1);
708                                                 
709                                                 fp= dl->verts;
710                                                 
711                                                 a= dl->nr;              
712                                                 while(a--) {
713                                                         VECCOPY(vec, fp);
714                                                         Mat4MulVecfl(mat, vec);
715                                                         
716                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
717                                                         
718                                                         fp+= 3; col1++;
719                                                 }
720                                         }
721                                 }
722                                 else if(dl->type==DL_SURF) {
723                                         if(dl->nors) {
724                                                 a= dl->nr*dl->parts;
725                                                 fp= dl->verts;
726                                                 nor= dl->nors;
727                                                 
728                                                 while(a--) {
729                                                         VECCOPY(vec, fp);
730                                                         Mat4MulVecfl(mat, vec);
731                                                         
732                                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
733                                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
734                                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
735                                                         Normalize(n1);
736                                 
737                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
738                                                         
739                                                         fp+= 3; nor+= 3; col1++;
740                                                 }
741                                         }
742                                 }
743                                 dl= dl->next;
744                         }
745                 }
746                 else if(ob->type==OB_MBALL) {
747                         /* there are normals already */
748                         dl= ob->disp.first;
749                         
750                         while(dl) {
751                                 
752                                 if(dl->type==DL_INDEX4) {
753                                         if(dl->nors) {
754                                                 extern Material defmaterial;    /* material.c */
755                                                 
756                                                 if(dl->col1) MEM_freeN(dl->col1);
757                                                 col1= dl->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
758                                 
759                                                 ma= give_current_material(ob, dl->col+1);
760                                                 if(ma==NULL) ma= &defmaterial;
761                                                 
762                                                 fp= dl->verts;
763                                                 nor= dl->nors;
764                                                 
765                                                 a= dl->nr;              
766                                                 while(a--) {
767                                                         VECCOPY(vec, fp);
768                                                         Mat4MulVecfl(mat, vec);
769                                                         
770                                                         /* transpose ! */
771                                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
772                                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
773                                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
774                                                         Normalize(n1);
775                                                 
776                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
777                                                         
778                                                         fp+= 3; col1++; nor+= 3;
779                                                 }
780                                         }
781                                 }
782                                 dl= dl->next;
783                         }
784                 }
785                 
786                 end_fastshade_for_ob(ob);
787         }
788 }
789
790 /* frees render and shade part of displists */
791 /* note: dont do a shade again, until a redraw happens */
792 void reshadeall_displist(Scene *scene)
793 {
794         Base *base;
795         Object *ob;
796         
797         fastshade_free_render();
798         
799         for(base= scene->base.first; base; base= base->next) {
800                 ob= base->object;
801
802                 if(ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
803                         freedisplist(&ob->disp);
804
805                 if(base->lay & scene->lay) {
806                         /* Metaballs have standard displist at the Object */
807                         if(ob->type==OB_MBALL) shadeDispList(scene, base);
808                 }
809         }
810 }
811
812 /* ****************** make displists ********************* */
813
814 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase)
815 {
816         Nurb *nu;
817         DispList *dl;
818         BezTriple *bezt, *prevbezt;
819         BPoint *bp;
820         float *data, *v1, *v2;
821         int a, len, resolu;
822         
823         nu= nubase->first;
824         while(nu) {
825                 if(nu->hide==0) {
826                         
827                         if(G.rendering && cu->resolu_ren!=0) 
828                                 resolu= cu->resolu_ren;
829                         else
830                                 resolu= nu->resolu;
831                         
832                         if(!check_valid_nurb_u(nu));
833                         else if((nu->type & 7)==CU_BEZIER) {
834                                 
835                                 /* count */
836                                 len= 0;
837                                 a= nu->pntsu-1;
838                                 if(nu->flagu & CU_CYCLIC) a++;
839
840                                 prevbezt= nu->bezt;
841                                 bezt= prevbezt+1;
842                                 while(a--) {
843                                         if(a==0 && (nu->flagu & CU_CYCLIC)) bezt= nu->bezt;
844                                         
845                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) len++;
846                                         else len+= resolu;
847                                         
848                                         if(a==0 && (nu->flagu & CU_CYCLIC)==0) len++;
849                                         
850                                         prevbezt= bezt;
851                                         bezt++;
852                                 }
853                                 
854                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbez");
855                                 /* len+1 because of 'forward_diff_bezier' function */
856                                 dl->verts= MEM_callocN( (len+1)*3*sizeof(float), "dlverts");
857                                 BLI_addtail(dispbase, dl);
858                                 dl->parts= 1;
859                                 dl->nr= len;
860                                 dl->col= nu->mat_nr;
861                                 dl->charidx= nu->charidx;
862
863                                 data= dl->verts;
864
865                                 if(nu->flagu & CU_CYCLIC) {
866                                         dl->type= DL_POLY;
867                                         a= nu->pntsu;
868                                 }
869                                 else {
870                                         dl->type= DL_SEGM;
871                                         a= nu->pntsu-1;
872                                 }
873                                 
874                                 prevbezt= nu->bezt;
875                                 bezt= prevbezt+1;
876                                 
877                                 while(a--) {
878                                         if(a==0 && dl->type== DL_POLY) bezt= nu->bezt;
879                                         
880                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
881                                                 VECCOPY(data, prevbezt->vec[1]);
882                                                 data+= 3;
883                                         }
884                                         else {
885                                                 v1= prevbezt->vec[1];
886                                                 v2= bezt->vec[0];
887                                                 forward_diff_bezier(v1[0], v1[3], v2[0], v2[3], data, resolu, 3);
888                                                 forward_diff_bezier(v1[1], v1[4], v2[1], v2[4], data+1, resolu, 3);
889                                                 forward_diff_bezier(v1[2], v1[5], v2[2], v2[5], data+2, resolu, 3);
890                                                 data+= 3*resolu;
891                                         }
892                                         
893                                         if(a==0 && dl->type==DL_SEGM) {
894                                                 VECCOPY(data, bezt->vec[1]);
895                                         }
896                                         
897                                         prevbezt= bezt;
898                                         bezt++;
899                                 }
900                         }
901                         else if((nu->type & 7)==CU_NURBS) {
902                                 len= (resolu*SEGMENTSU(nu));
903                                 
904                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
905                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
906                                 BLI_addtail(dispbase, dl);
907                                 dl->parts= 1;
908                                 
909                                 dl->nr= len;
910                                 dl->col= nu->mat_nr;
911                                 dl->charidx = nu->charidx;
912
913                                 data= dl->verts;
914                                 if(nu->flagu & CU_CYCLIC) dl->type= DL_POLY;
915                                 else dl->type= DL_SEGM;
916                                 makeNurbcurve(nu, data, NULL, NULL, resolu);
917                         }
918                         else if((nu->type & 7)==CU_POLY) {
919                                 len= nu->pntsu;
920                                 dl= MEM_callocN(sizeof(DispList), "makeDispListpoly");
921                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
922                                 BLI_addtail(dispbase, dl);
923                                 dl->parts= 1;
924                                 dl->nr= len;
925                                 dl->col= nu->mat_nr;
926                                 dl->charidx = nu->charidx;
927
928                                 data= dl->verts;
929                                 if(nu->flagu & CU_CYCLIC) dl->type= DL_POLY;
930                                 else dl->type= DL_SEGM;
931                                 
932                                 a= len;
933                                 bp= nu->bp;
934                                 while(a--) {
935                                         VECCOPY(data, bp->vec);
936                                         bp++;
937                                         data+= 3;
938                                 }
939                         }
940                 }
941                 nu= nu->next;
942         }
943 }
944
945
946 void filldisplist(ListBase *dispbase, ListBase *to)
947 {
948         EditVert *eve, *v1, *vlast;
949         EditFace *efa;
950         DispList *dlnew=0, *dl;
951         float *f1;
952         int colnr=0, charidx=0, cont=1, tot, a, *index;
953         intptr_t totvert;
954         
955         if(dispbase==0) return;
956         if(dispbase->first==0) return;
957
958         while(cont) {
959                 cont= 0;
960                 totvert=0;
961                 
962                 dl= dispbase->first;
963                 while(dl) {
964         
965                         if(dl->type==DL_POLY) {
966                                 if(charidx<dl->charidx) cont= 1;
967                                 else if(charidx==dl->charidx) {
968                         
969                                         colnr= dl->col;
970                                         charidx= dl->charidx;
971                 
972                                         /* make editverts and edges */
973                                         f1= dl->verts;
974                                         a= dl->nr;
975                                         eve= v1= 0;
976                                         
977                                         while(a--) {
978                                                 vlast= eve;
979                                                 
980                                                 eve= BLI_addfillvert(f1);
981                                                 totvert++;
982                                                 
983                                                 if(vlast==0) v1= eve;
984                                                 else {
985                                                         BLI_addfilledge(vlast, eve);
986                                                 }
987                                                 f1+=3;
988                                         }
989                                 
990                                         if(eve!=0 && v1!=0) {
991                                                 BLI_addfilledge(eve, v1);
992                                         }
993                                 }
994                         }
995                         dl= dl->next;
996                 }
997                 
998                 if(totvert && BLI_edgefill(0, 0)) { // XXX (obedit && obedit->actcol)?(obedit->actcol-1):0)) {
999
1000                         /* count faces  */
1001                         tot= 0;
1002                         efa= fillfacebase.first;
1003                         while(efa) {
1004                                 tot++;
1005                                 efa= efa->next;
1006                         }
1007
1008                         if(tot) {
1009                                 dlnew= MEM_callocN(sizeof(DispList), "filldisplist");
1010                                 dlnew->type= DL_INDEX3;
1011                                 dlnew->col= colnr;
1012                                 dlnew->nr= totvert;
1013                                 dlnew->parts= tot;
1014
1015                                 dlnew->index= MEM_mallocN(tot*3*sizeof(int), "dlindex");
1016                                 dlnew->verts= MEM_mallocN(totvert*3*sizeof(float), "dlverts");
1017                                 
1018                                 /* vert data */
1019                                 f1= dlnew->verts;
1020                                 totvert= 0;
1021                                 eve= fillvertbase.first;
1022                                 while(eve) {
1023                                         VECCOPY(f1, eve->co);
1024                                         f1+= 3;
1025         
1026                                         /* index number */
1027                                         eve->tmp.l = totvert;
1028                                         totvert++;
1029                                         
1030                                         eve= eve->next;
1031                                 }
1032                                 
1033                                 /* index data */
1034                                 efa= fillfacebase.first;
1035                                 index= dlnew->index;
1036                                 while(efa) {
1037                                         index[0]= (intptr_t)efa->v1->tmp.l;
1038                                         index[1]= (intptr_t)efa->v2->tmp.l;
1039                                         index[2]= (intptr_t)efa->v3->tmp.l;
1040                                         
1041                                         index+= 3;
1042                                         efa= efa->next;
1043                                 }
1044                         }
1045
1046                         BLI_addhead(to, dlnew);
1047                         
1048                 }
1049                 BLI_end_edgefill();
1050
1051                 charidx++;
1052         }
1053         
1054         /* do not free polys, needed for wireframe display */
1055         
1056 }
1057
1058 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
1059 {
1060         ListBase front, back;
1061         DispList *dl, *dlnew;
1062         float *fp, *fp1;
1063         int a, dpoly;
1064         
1065         front.first= front.last= back.first= back.last= 0;
1066         
1067         dl= dispbase->first;
1068         while(dl) {
1069                 if(dl->type==DL_SURF) {
1070                         if( (dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U)==0 ) {
1071                                 if( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE) ) {
1072                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1073                                         BLI_addtail(&front, dlnew);
1074                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1075                                         dlnew->nr= dl->parts;
1076                                         dlnew->parts= 1;
1077                                         dlnew->type= DL_POLY;
1078                                         dlnew->col= dl->col;
1079                                         dlnew->charidx = dl->charidx;
1080                                         
1081                                         fp= dl->verts;
1082                                         dpoly= 3*dl->nr;
1083                                         
1084                                         a= dl->parts;
1085                                         while(a--) {
1086                                                 VECCOPY(fp1, fp);
1087                                                 fp1+= 3;
1088                                                 fp+= dpoly;
1089                                         }
1090                                 }
1091                                 if( (cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE) ) {
1092                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1093                                         BLI_addtail(&back, dlnew);
1094                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1095                                         dlnew->nr= dl->parts;
1096                                         dlnew->parts= 1;
1097                                         dlnew->type= DL_POLY;
1098                                         dlnew->col= dl->col;
1099                                         dlnew->charidx= dl->charidx;
1100                                         
1101                                         fp= dl->verts+3*(dl->nr-1);
1102                                         dpoly= 3*dl->nr;
1103                                         
1104                                         a= dl->parts;
1105                                         while(a--) {
1106                                                 VECCOPY(fp1, fp);
1107                                                 fp1+= 3;
1108                                                 fp+= dpoly;
1109                                         }
1110                                 }
1111                         }
1112                 }
1113                 dl= dl->next;
1114         }
1115
1116         filldisplist(&front, dispbase);
1117         filldisplist(&back, dispbase);
1118         
1119         freedisplist(&front);
1120         freedisplist(&back);
1121
1122         filldisplist(dispbase, dispbase);
1123         
1124 }
1125
1126 void curve_to_filledpoly(Curve *cu, ListBase *nurb, ListBase *dispbase)
1127 {
1128         if(cu->flag & CU_3D) return;
1129
1130         if(dispbase->first && ((DispList*) dispbase->first)->type==DL_SURF) {
1131                 bevels_to_filledpoly(cu, dispbase);
1132         }
1133         else {
1134                 filldisplist(dispbase, dispbase);
1135         }
1136 }
1137
1138 /* taper rules:
1139   - only 1 curve
1140   - first point left, last point right
1141   - based on subdivided points in original curve, not on points in taper curve (still)
1142 */
1143 float calc_taper(Scene *scene, Object *taperobj, int cur, int tot)
1144 {
1145         Curve *cu;
1146         DispList *dl;
1147         
1148         if(taperobj==NULL) return 1.0;
1149         
1150         cu= taperobj->data;
1151         dl= cu->disp.first;
1152         if(dl==NULL) {
1153                 makeDispListCurveTypes(scene, taperobj, 0);
1154                 dl= cu->disp.first;
1155         }
1156         if(dl) {
1157                 float fac= ((float)cur)/(float)(tot-1);
1158                 float minx, dx, *fp;
1159                 int a;
1160                 
1161                 /* horizontal size */
1162                 minx= dl->verts[0];
1163                 dx= dl->verts[3*(dl->nr-1)] - minx;
1164                 if(dx>0.0) {
1165                 
1166                         fp= dl->verts;
1167                         for(a=0; a<dl->nr; a++, fp+=3) {
1168                                 if( (fp[0]-minx)/dx >= fac) {
1169                                         /* interpolate with prev */
1170                                         if(a>0) {
1171                                                 float fac1= (fp[-3]-minx)/dx;
1172                                                 float fac2= (fp[0]-minx)/dx;
1173                                                 if(fac1!=fac2)
1174                                                         return fp[1]*(fac1-fac)/(fac1-fac2) + fp[-2]*(fac-fac2)/(fac1-fac2);
1175                                         }
1176                                         return fp[1];
1177                                 }
1178                         }
1179                         return fp[-2];  // last y coord
1180                 }
1181         }
1182         
1183         return 1.0;
1184 }
1185
1186 void makeDispListMBall(Scene *scene, Object *ob)
1187 {
1188         if(!ob || ob->type!=OB_MBALL) return;
1189
1190         freedisplist(&(ob->disp));
1191         
1192         if(ob->type==OB_MBALL) {
1193                 if(ob==find_basis_mball(scene, ob)) {
1194                         metaball_polygonize(scene, ob);
1195                         tex_space_mball(ob);
1196
1197                         object_deform_mball(ob);
1198                 }
1199         }
1200         
1201         boundbox_displist(ob);
1202 }
1203
1204 static ModifierData *curve_get_tesselate_point(Object *ob, int forRender, int editmode)
1205 {
1206         ModifierData *md = modifiers_getVirtualModifierList(ob);
1207         ModifierData *preTesselatePoint;
1208         int required_mode;
1209
1210         if(forRender) required_mode = eModifierMode_Render;
1211         else required_mode = eModifierMode_Realtime;
1212
1213         if(editmode) required_mode |= eModifierMode_Editmode;
1214
1215         preTesselatePoint = NULL;
1216         for (; md; md=md->next) {
1217                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1218
1219                 if ((md->mode & required_mode) != required_mode) continue;
1220                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1221
1222                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
1223                         preTesselatePoint  = md;
1224                 }
1225         }
1226
1227         return preTesselatePoint;
1228 }
1229
1230 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, int forRender, float (**originalVerts_r)[3], float (**deformedVerts_r)[3], int *numVerts_r)
1231 {
1232         ModifierData *md = modifiers_getVirtualModifierList(ob);
1233         ModifierData *preTesselatePoint;
1234         Curve *cu= ob->data;
1235         ListBase *nurb= cu->editnurb?cu->editnurb:&cu->nurb;
1236         int numVerts = 0;
1237         int editmode = (!forRender && cu->editnurb);
1238         float (*originalVerts)[3] = NULL;
1239         float (*deformedVerts)[3] = NULL;
1240         int required_mode;
1241
1242         if(forRender) required_mode = eModifierMode_Render;
1243         else required_mode = eModifierMode_Realtime;
1244
1245         preTesselatePoint = curve_get_tesselate_point(ob, forRender, editmode);
1246         
1247         if(editmode) required_mode |= eModifierMode_Editmode;
1248
1249         if(cu->editnurb==NULL && do_ob_key(scene, ob)) {
1250                 deformedVerts = curve_getVertexCos(ob->data, nurb, &numVerts);
1251                 originalVerts = MEM_dupallocN(deformedVerts);
1252         }
1253         
1254         if (preTesselatePoint) {
1255                 for (; md; md=md->next) {
1256                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1257
1258                         md->scene= scene;
1259                         
1260                         if ((md->mode & required_mode) != required_mode) continue;
1261                         if (mti->isDisabled && mti->isDisabled(md)) continue;
1262                         if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1263
1264                         if (!deformedVerts) {
1265                                 deformedVerts = curve_getVertexCos(ob->data, nurb, &numVerts);
1266                                 originalVerts = MEM_dupallocN(deformedVerts);
1267                         }
1268                         
1269                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, forRender, editmode);
1270
1271                         if (md==preTesselatePoint)
1272                                 break;
1273                 }
1274         }
1275
1276         if (deformedVerts) {
1277                 curve_applyVertexCos(ob->data, nurb, deformedVerts);
1278         }
1279
1280         *originalVerts_r = originalVerts;
1281         *deformedVerts_r = deformedVerts;
1282         *numVerts_r = numVerts;
1283 }
1284
1285 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispbase, int forRender, float (*originalVerts)[3], float (*deformedVerts)[3])
1286 {
1287         ModifierData *md = modifiers_getVirtualModifierList(ob);
1288         ModifierData *preTesselatePoint;
1289         Curve *cu= ob->data;
1290         ListBase *nurb= cu->editnurb?cu->editnurb:&cu->nurb;
1291         DispList *dl;
1292         int required_mode;
1293         int editmode = (!forRender && cu->editnurb);
1294
1295         if(forRender) required_mode = eModifierMode_Render;
1296         else required_mode = eModifierMode_Realtime;
1297
1298         preTesselatePoint = curve_get_tesselate_point(ob, forRender, editmode);
1299         
1300         if(editmode) required_mode |= eModifierMode_Editmode;
1301
1302         if (preTesselatePoint) {
1303                 md = preTesselatePoint->next;
1304         }
1305
1306         for (; md; md=md->next) {
1307                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1308                 
1309                 md->scene= scene;
1310                 
1311                 if ((md->mode & required_mode) != required_mode) continue;
1312                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1313                 if (mti->type!=eModifierTypeType_OnlyDeform && mti->type!=eModifierTypeType_DeformOrConstruct) continue;
1314
1315                 /* need to put all verts in 1 block for curve deform */
1316                 if(md->type==eModifierType_Curve) {
1317                         float *allverts, *fp;
1318                         int totvert= 0;
1319                         
1320                         for (dl=dispbase->first; dl; dl=dl->next)
1321                                 totvert+= (dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr;
1322                         
1323                         fp= allverts= MEM_mallocN(totvert*sizeof(float)*3, "temp vert");
1324                         for (dl=dispbase->first; dl; dl=dl->next) {
1325                                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1326                                 memcpy(fp, dl->verts, sizeof(float) * offs);
1327                                 fp+= offs;
1328                         }
1329                         
1330                         mti->deformVerts(md, ob, NULL, (float(*)[3]) allverts, totvert, forRender, editmode);
1331                         
1332                         fp= allverts;
1333                         for (dl=dispbase->first; dl; dl=dl->next) {
1334                                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1335                                 memcpy(dl->verts, fp, sizeof(float) * offs);
1336                                 fp+= offs;
1337                         }
1338                         MEM_freeN(allverts);
1339                 }
1340                 else {
1341                         for (dl=dispbase->first; dl; dl=dl->next) {
1342                                 mti->deformVerts(md, ob, NULL, (float(*)[3]) dl->verts, (dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr, forRender, editmode);
1343                         }
1344                 }
1345         }
1346
1347         if (deformedVerts) {
1348                 curve_applyVertexCos(ob->data, nurb, originalVerts);
1349                 MEM_freeN(originalVerts);
1350                 MEM_freeN(deformedVerts);
1351         }
1352 }
1353
1354 static void displist_surf_indices(DispList *dl)
1355 {
1356         int a, b, p1, p2, p3, p4;
1357         int *index;
1358         
1359         dl->totindex= 0;
1360         
1361         index=dl->index= MEM_mallocN( 4*sizeof(int)*(dl->parts+1)*(dl->nr+1), "index array nurbs");
1362         
1363         for(a=0; a<dl->parts; a++) {
1364                 
1365                 if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
1366                         break;
1367                 
1368                 for(; b<dl->nr; b++, index+=4) {        
1369                         index[0]= p1;
1370                         index[1]= p2;
1371                         index[2]= p4;
1372                         index[3]= p3;
1373                         
1374                         dl->totindex++;
1375                         
1376                         p2= p1; p1++;
1377                         p4= p3; p3++;
1378
1379                 }
1380         }
1381         
1382 }
1383
1384 void makeDispListSurf(Scene *scene, Object *ob, ListBase *dispbase, int forRender, int forOrco)
1385 {
1386         ListBase *nubase;
1387         Nurb *nu;
1388         Curve *cu = ob->data;
1389         DispList *dl;
1390         float *data;
1391         int len;
1392         int numVerts;
1393         float (*originalVerts)[3];
1394         float (*deformedVerts)[3];
1395                 
1396         if(!forRender && cu->editnurb)
1397                 nubase= cu->editnurb;
1398         else
1399                 nubase= &cu->nurb;
1400
1401         if(!forOrco)
1402                 curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1403
1404         for (nu=nubase->first; nu; nu=nu->next) {
1405                 if(forRender || nu->hide==0) {
1406                         if(nu->pntsv==1) {
1407                                 len= SEGMENTSU(nu)*nu->resolu;
1408                                 
1409                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1410                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1411                                 
1412                                 BLI_addtail(dispbase, dl);
1413                                 dl->parts= 1;
1414                                 dl->nr= len;
1415                                 dl->col= nu->mat_nr;
1416                                 dl->charidx= nu->charidx;
1417                                 dl->rt= nu->flag;
1418                                 
1419                                 data= dl->verts;
1420                                 if(nu->flagu & CU_CYCLIC) dl->type= DL_POLY;
1421                                 else dl->type= DL_SEGM;
1422                                 
1423                                 makeNurbcurve(nu, data, NULL, NULL, nu->resolu);
1424                         }
1425                         else {
1426                                 len= (nu->pntsu*nu->resolu) * (nu->pntsv*nu->resolv);
1427                                 
1428                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1429                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1430                                 BLI_addtail(dispbase, dl);
1431
1432                                 dl->col= nu->mat_nr;
1433                                 dl->charidx= nu->charidx;
1434                                 dl->rt= nu->flag;
1435                                 
1436                                 data= dl->verts;
1437                                 dl->type= DL_SURF;
1438                                 
1439                                 dl->parts= (nu->pntsu*nu->resolu);      /* in reverse, because makeNurbfaces works that way */
1440                                 dl->nr= (nu->pntsv*nu->resolv);
1441                                 if(nu->flagv & CU_CYCLIC) dl->flag|= DL_CYCL_U; /* reverse too! */
1442                                 if(nu->flagu & CU_CYCLIC) dl->flag|= DL_CYCL_V;
1443
1444                                 makeNurbfaces(nu, data, 0);
1445                                 
1446                                 /* gl array drawing: using indices */
1447                                 displist_surf_indices(dl);
1448                         }
1449                 }
1450         }
1451
1452         if (!forRender) {
1453                 tex_space_curve(cu);
1454         }
1455
1456         if(!forOrco)
1457                 curve_calc_modifiers_post(scene, ob, dispbase, forRender, originalVerts, deformedVerts);
1458 }
1459
1460 void makeDispListCurveTypes(Scene *scene, Object *ob, int forOrco)
1461 {
1462         Curve *cu = ob->data;
1463         ListBase *dispbase;
1464         
1465         /* we do allow duplis... this is only displist on curve level */
1466         if(!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1467
1468         freedisplist(&(ob->disp));
1469         dispbase= &(cu->disp);
1470         freedisplist(dispbase);
1471         
1472         if(ob->type==OB_SURF) {
1473                 makeDispListSurf(scene, ob, dispbase, 0, forOrco);
1474         }
1475         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1476                 ListBase dlbev;
1477                 ListBase *nubase;
1478                 float (*originalVerts)[3];
1479                 float (*deformedVerts)[3];
1480                 int numVerts;
1481
1482                 if(cu->editnurb)
1483                         nubase= cu->editnurb;
1484                 else
1485                         nubase= &cu->nurb;
1486                 
1487                 BLI_freelistN(&(cu->bev));
1488                 
1489                 if(cu->path) free_path(cu->path);
1490                 cu->path= NULL;
1491                 
1492                 if(ob->type==OB_FONT) BKE_text_to_curve(scene, ob, 0);
1493                 
1494                 if(!forOrco) curve_calc_modifiers_pre(scene, ob, 0, &originalVerts, &deformedVerts, &numVerts);
1495
1496                 makeBevelList(ob);
1497
1498                 /* If curve has no bevel will return nothing */
1499                 makebevelcurve(scene, ob, &dlbev);
1500
1501                 /* no bevel or extrude, and no width correction? */
1502                 if (!dlbev.first && cu->width==1.0f) {
1503                         curve_to_displist(cu, nubase, dispbase);
1504                 } else {
1505                         float widfac= cu->width-1.0;
1506                         BevList *bl= cu->bev.first;
1507                         Nurb *nu= nubase->first;
1508
1509                         for (; bl && nu; bl=bl->next,nu=nu->next) {
1510                                 DispList *dl;
1511                                 float *fp1, *data;
1512                                 BevPoint *bevp;
1513                                 int a,b;
1514                                 
1515                                 if (bl->nr) { /* blank bevel lists can happen */
1516                                         
1517                                         /* exception handling; curve without bevel or extrude, with width correction */
1518                                         if(dlbev.first==NULL) {
1519                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev");
1520                                                 dl->verts= MEM_callocN(3*sizeof(float)*bl->nr, "dlverts");
1521                                                 BLI_addtail(dispbase, dl);
1522                                                 
1523                                                 if(bl->poly!= -1) dl->type= DL_POLY;
1524                                                 else dl->type= DL_SEGM;
1525                                                 
1526                                                 if(dl->type==DL_SEGM) dl->flag = (DL_FRONT_CURVE|DL_BACK_CURVE);
1527                                                 
1528                                                 dl->parts= 1;
1529                                                 dl->nr= bl->nr;
1530                                                 dl->col= nu->mat_nr;
1531                                                 dl->charidx= nu->charidx;
1532                                                 dl->rt= nu->flag;
1533                                                 
1534                                                 a= dl->nr;
1535                                                 bevp= (BevPoint *)(bl+1);
1536                                                 data= dl->verts;
1537                                                 while(a--) {
1538                                                         data[0]= bevp->x+widfac*bevp->sina;
1539                                                         data[1]= bevp->y+widfac*bevp->cosa;
1540                                                         data[2]= bevp->z;
1541                                                         bevp++;
1542                                                         data+=3;
1543                                                 }
1544                                         }
1545                                         else {
1546                                                 DispList *dlb;
1547                                                 
1548                                                 for (dlb=dlbev.first; dlb; dlb=dlb->next) {
1549         
1550                                                                 /* for each part of the bevel use a separate displblock */
1551                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1552                                                         dl->verts= data= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1553                                                         BLI_addtail(dispbase, dl);
1554         
1555                                                         dl->type= DL_SURF;
1556                                                         
1557                                                         dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1558                                                         if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1559                                                         if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1560                                                         
1561                                                         dl->parts= bl->nr;
1562                                                         dl->nr= dlb->nr;
1563                                                         dl->col= nu->mat_nr;
1564                                                         dl->charidx= nu->charidx;
1565                                                         dl->rt= nu->flag;
1566                                                         dl->bevelSplitFlag= MEM_callocN(sizeof(*dl->col2)*((bl->nr+0x1F)>>5), "col2");
1567                                                         bevp= (BevPoint *)(bl+1);
1568         
1569                                                                 /* for each point of poly make a bevel piece */
1570                                                         bevp= (BevPoint *)(bl+1);
1571                                                         for(a=0; a<bl->nr; a++,bevp++) {
1572                                                                 float fac=1.0;
1573                                                                 if (cu->taperobj==NULL) {
1574                                                                         if ( (cu->bevobj!=NULL) || !((cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) )
1575                                                                                 fac = bevp->radius;
1576                                                                 } else {
1577                                                                         fac = calc_taper(scene, cu->taperobj, a, bl->nr);
1578                                                                 }
1579                                                                 
1580                                                                 if (bevp->f1) {
1581                                                                         dl->bevelSplitFlag[a>>5] |= 1<<(a&0x1F);
1582                                                                 }
1583         
1584                                                                         /* rotate bevel piece and write in data */
1585                                                                 fp1= dlb->verts;
1586                                                                 for (b=0; b<dlb->nr; b++,fp1+=3,data+=3) {
1587                                                                         if(cu->flag & CU_3D) {
1588                                                                                 float vec[3];
1589         
1590                                                                                 vec[0]= fp1[1]+widfac;
1591                                                                                 vec[1]= fp1[2];
1592                                                                                 vec[2]= 0.0;
1593                                                                                 
1594                                                                                 Mat3MulVecfl(bevp->mat, vec);
1595                                                                                 
1596                                                                                 data[0]= bevp->x+ fac*vec[0];
1597                                                                                 data[1]= bevp->y+ fac*vec[1];
1598                                                                                 data[2]= bevp->z+ fac*vec[2];
1599                                                                         }
1600                                                                         else {
1601                                                                                 data[0]= bevp->x+ fac*(widfac+fp1[1])*bevp->sina;
1602                                                                                 data[1]= bevp->y+ fac*(widfac+fp1[1])*bevp->cosa;
1603                                                                                 data[2]= bevp->z+ fac*fp1[2];
1604                                                                         }
1605                                                                 }
1606                                                         }
1607                                                         
1608                                                         /* gl array drawing: using indices */
1609                                                         displist_surf_indices(dl);
1610                                                 }
1611                                         }
1612                                 }
1613
1614                         }
1615                         freedisplist(&dlbev);
1616                 }
1617
1618                 curve_to_filledpoly(cu, nubase, dispbase);
1619
1620                 if(cu->flag & CU_PATH) calc_curvepath(ob);
1621
1622                 if(!forOrco) curve_calc_modifiers_post(scene, ob, &cu->disp, 0, originalVerts, deformedVerts);
1623                 tex_space_curve(cu);
1624         }
1625         
1626         boundbox_displist(ob);
1627 }
1628
1629 void imagestodisplist(void)
1630 {
1631         /* removed */
1632 }
1633
1634 /* this is confusing, there's also min_max_object, appplying the obmat... */
1635 static void boundbox_displist(Object *ob)
1636 {
1637         BoundBox *bb=0;
1638         float min[3], max[3];
1639         DispList *dl;
1640         float *vert;
1641         int a, tot=0;
1642         
1643         INIT_MINMAX(min, max);
1644
1645         if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1646                 Curve *cu= ob->data;
1647                 int doit= 0;
1648
1649                 if(cu->bb==0) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");        
1650                 bb= cu->bb;
1651                 
1652                 dl= cu->disp.first;
1653
1654                 while (dl) {
1655                         if(dl->type==DL_INDEX3) tot= dl->nr;
1656                         else tot= dl->nr*dl->parts;
1657                         
1658                         vert= dl->verts;
1659                         for(a=0; a<tot; a++, vert+=3) {
1660                                 doit= 1;
1661                                 DO_MINMAX(vert, min, max);
1662                         }
1663
1664                         dl= dl->next;
1665                 }
1666                 
1667                 if(!doit) {
1668                         min[0] = min[1] = min[2] = -1.0f;
1669                         max[0] = max[1] = max[2] = 1.0f;
1670                 }
1671                 
1672         }
1673         
1674         if(bb) {
1675                 boundbox_set_from_min_max(bb, min, max);
1676         }
1677 }
1678