Todo item #19816: Wave modifier does not affect Curve Paths
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "DNA_curve_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_material_types.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_editVert.h"
47 #include "BLI_scanfill.h"
48 #include "BLI_utildefines.h"
49
50 #include "BKE_global.h"
51 #include "BKE_displist.h"
52 #include "BKE_cdderivedmesh.h"
53 #include "BKE_object.h"
54 #include "BKE_mball.h"
55 #include "BKE_material.h"
56 #include "BKE_curve.h"
57 #include "BKE_key.h"
58 #include "BKE_anim.h"
59 #include "BKE_font.h"
60 #include "BKE_lattice.h"
61 #include "BKE_modifier.h"
62
63 #include "RE_pipeline.h"
64 #include "RE_shader_ext.h"
65
66 #include "BLO_sys_types.h" // for intptr_t support
67
68 #include "ED_curve.h" /* for BKE_curve_nurbs */
69
70 static void boundbox_displist(Object *ob);
71
72 void free_disp_elem(DispList *dl)
73 {
74         if(dl) {
75                 if(dl->verts) MEM_freeN(dl->verts);
76                 if(dl->nors) MEM_freeN(dl->nors);
77                 if(dl->index) MEM_freeN(dl->index);
78                 if(dl->col1) MEM_freeN(dl->col1);
79                 if(dl->col2) MEM_freeN(dl->col2);
80                 if(dl->bevelSplitFlag) MEM_freeN(dl->bevelSplitFlag);
81                 MEM_freeN(dl);
82         }
83 }
84
85 void freedisplist(ListBase *lb)
86 {
87         DispList *dl;
88
89         dl= lb->first;
90         while(dl) {
91                 BLI_remlink(lb, dl);
92                 free_disp_elem(dl);
93                 dl= lb->first;
94         }
95 }
96
97 DispList *find_displist_create(ListBase *lb, int type)
98 {
99         DispList *dl;
100         
101         dl= lb->first;
102         while(dl) {
103                 if(dl->type==type) return dl;
104                 dl= dl->next;
105         }
106
107         dl= MEM_callocN(sizeof(DispList), "find_disp");
108         dl->type= type;
109         BLI_addtail(lb, dl);
110
111         return dl;
112 }
113
114 DispList *find_displist(ListBase *lb, int type)
115 {
116         DispList *dl;
117         
118         dl= lb->first;
119         while(dl) {
120                 if(dl->type==type) return dl;
121                 dl= dl->next;
122         }
123
124         return 0;
125 }
126
127 int displist_has_faces(ListBase *lb)
128 {
129         DispList *dl;
130         for(dl= lb->first; dl; dl= dl->next) {
131                 if ELEM3(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)
132                         return 1;
133         }
134         return 0;
135 }
136
137 void copy_displist(ListBase *lbn, ListBase *lb)
138 {
139         DispList *dln, *dl;
140         
141         freedisplist(lbn);
142         
143         dl= lb->first;
144         while(dl) {
145                 
146                 dln= MEM_dupallocN(dl);
147                 BLI_addtail(lbn, dln);
148                 dln->verts= MEM_dupallocN(dl->verts);
149                 dln->nors= MEM_dupallocN(dl->nors);
150                 dln->index= MEM_dupallocN(dl->index);
151                 dln->col1= MEM_dupallocN(dl->col1);
152                 dln->col2= MEM_dupallocN(dl->col2);
153
154                 if(dl->bevelSplitFlag)
155                         dln->bevelSplitFlag= MEM_dupallocN(dl->bevelSplitFlag);
156
157                 dl= dl->next;
158         }
159 }
160
161 void addnormalsDispList(ListBase *lb)
162 {
163         DispList *dl = NULL;
164         float *vdata, *ndata, nor[3];
165         float *v1, *v2, *v3, *v4;
166         float *n1, *n2, *n3, *n4;
167         int a, b, p1, p2, p3, p4;
168
169
170         dl= lb->first;
171         
172         while(dl) {
173                 if(dl->type==DL_INDEX3) {
174                         if(dl->nors==NULL) {
175                                 dl->nors= MEM_callocN(sizeof(float)*3, "dlnors");
176                                 if(dl->verts[2]<0.0) dl->nors[2]= -1.0;
177                                 else dl->nors[2]= 1.0;
178                         }
179                 }
180                 else if(dl->type==DL_SURF) {
181                         if(dl->nors==NULL) {
182                                 dl->nors= MEM_callocN(sizeof(float)*3*dl->nr*dl->parts, "dlnors");
183                                 
184                                 vdata= dl->verts;
185                                 ndata= dl->nors;
186                                 
187                                 for(a=0; a<dl->parts; a++) {
188                                         
189                                         if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
190                                                 break;
191         
192                                         v1= vdata+ 3*p1; 
193                                         n1= ndata+ 3*p1;
194                                         v2= vdata+ 3*p2; 
195                                         n2= ndata+ 3*p2;
196                                         v3= vdata+ 3*p3; 
197                                         n3= ndata+ 3*p3;
198                                         v4= vdata+ 3*p4; 
199                                         n4= ndata+ 3*p4;
200                                         
201                                         for(; b<dl->nr; b++) {
202         
203                                                 normal_quad_v3( nor,v1, v3, v4, v2);
204         
205                                                 add_v3_v3(n1, nor);
206                                                 add_v3_v3(n2, nor);
207                                                 add_v3_v3(n3, nor);
208                                                 add_v3_v3(n4, nor);
209         
210                                                 v2= v1; v1+= 3;
211                                                 v4= v3; v3+= 3;
212                                                 n2= n1; n1+= 3;
213                                                 n4= n3; n3+= 3;
214                                         }
215                                 }
216                                 a= dl->parts*dl->nr;
217                                 v1= ndata;
218                                 while(a--) {
219                                         normalize_v3(v1);
220                                         v1+= 3;
221                                 }
222                         }
223                 }
224                 dl= dl->next;
225         }
226 }
227
228 void count_displist(ListBase *lb, int *totvert, int *totface)
229 {
230         DispList *dl;
231         
232         dl= lb->first;
233         while(dl) {
234                 
235                 switch(dl->type) {
236                         case DL_SURF:
237                                 *totvert+= dl->nr*dl->parts;
238                                 *totface+= (dl->nr-1)*(dl->parts-1);
239                                 break;
240                         case DL_INDEX3:
241                         case DL_INDEX4:
242                                 *totvert+= dl->nr;
243                                 *totface+= dl->parts;
244                                 break;
245                         case DL_POLY:
246                         case DL_SEGM:
247                                 *totvert+= dl->nr*dl->parts;
248                 }
249                 
250                 dl= dl->next;
251         }
252 }
253
254 int surfindex_displist(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4)
255 {
256         if((dl->flag & DL_CYCL_V)==0 && a==(dl->parts)-1) {
257                 return 0;
258         }
259         
260         if(dl->flag & DL_CYCL_U) {
261                 (*p1)= dl->nr*a;
262                 (*p2)= (*p1)+ dl->nr-1;
263                 (*p3)= (*p1)+ dl->nr;
264                 (*p4)= (*p2)+ dl->nr;
265                 (*b)= 0;
266         } else {
267                 (*p2)= dl->nr*a;
268                 (*p1)= (*p2)+1;
269                 (*p4)= (*p2)+ dl->nr;
270                 (*p3)= (*p1)+ dl->nr;
271                 (*b)= 1;
272         }
273         
274         if( (dl->flag & DL_CYCL_V) && a==dl->parts-1) {                     \
275                 (*p3)-= dl->nr*dl->parts;                                   \
276                 (*p4)-= dl->nr*dl->parts;                                   \
277         }
278         
279         return 1;
280 }
281
282 /* ***************************** shade displist. note colors now are in rgb(a) order ******************** */
283
284 /* create default shade input... save cpu cycles with ugly global */
285 /* XXXX bad code warning: local ShadeInput initialize... */
286 static ShadeInput shi;
287 static void init_fastshade_shadeinput(Render *re)
288 {
289         memset(&shi, 0, sizeof(ShadeInput));
290         shi.lay= RE_GetScene(re)->lay;
291         shi.view[2]= -1.0f;
292         shi.passflag= SCE_PASS_COMBINED;
293         shi.combinedflag= -1;
294 }
295
296 static Render *fastshade_get_render(Scene *UNUSED(scene))
297 {
298         // XXX 2.5: this crashes combined with previewrender
299         // due to global R so disabled for now
300 #if 0
301         /* XXX ugly global still, but we can't do preview while rendering */
302         if(G.rendering==0) {
303                 
304                 Render *re= RE_GetRender("_Shade View_");
305                 if(re==NULL) {
306                         re= RE_NewRender("_Shade View_");
307                 
308                         RE_Database_Baking(re, scene, 0, 0);    /* 0= no faces */
309                 }
310                 return re;
311         }
312 #endif
313         
314         return NULL;
315 }
316
317 /* called on file reading */
318 void fastshade_free_render(void)
319 {
320         Render *re= RE_GetRender("_Shade View_");
321         
322         if(re) {
323                 RE_Database_Free(re);
324                 RE_FreeRender(re);
325         }
326 }
327
328 static int fastshade_customdata_layer_num(int n, int active)
329 {   
330         /* make the active layer the first */
331         if (n == active) return 0;
332         else if (n < active) return n+1;
333         else return n;
334 }
335
336 static void fastshade_customdata(CustomData *fdata, int a, int j, Material *ma)
337 {
338         CustomDataLayer *layer;
339         MTFace *mtface;
340         int index, n, needuv= ma->texco & TEXCO_UV;
341         char *vertcol;
342
343         shi.totuv= 0;
344         shi.totcol= 0;
345
346         for(index=0; index<fdata->totlayer; index++) {
347                 layer= &fdata->layers[index];
348                 
349                 if(needuv && layer->type == CD_MTFACE && shi.totuv < MAX_MTFACE) {
350                         n= fastshade_customdata_layer_num(shi.totuv, layer->active_rnd);
351                         mtface= &((MTFace*)layer->data)[a];
352
353                         shi.uv[shi.totuv].uv[0]= 2.0f*mtface->uv[j][0]-1.0f;
354                         shi.uv[shi.totuv].uv[1]= 2.0f*mtface->uv[j][1]-1.0f;
355                         shi.uv[shi.totuv].uv[2]= 1.0f;
356
357                         shi.uv[shi.totuv].name= layer->name;
358                         shi.totuv++;
359                 }
360                 else if(layer->type == CD_MCOL && shi.totcol < MAX_MCOL) {
361                         n= fastshade_customdata_layer_num(shi.totcol, layer->active_rnd);
362                         vertcol= (char*)&((MCol*)layer->data)[a*4 + j];
363
364                         shi.col[shi.totcol].col[0]= ((float)vertcol[3])/255.0f;
365                         shi.col[shi.totcol].col[1]= ((float)vertcol[2])/255.0f;
366                         shi.col[shi.totcol].col[2]= ((float)vertcol[1])/255.0f;
367
368                         shi.col[shi.totcol].name= layer->name;
369                         shi.totcol++;
370                 }
371         }
372
373         if(needuv && shi.totuv == 0)
374                 VECCOPY(shi.uv[0].uv, shi.lo);
375
376         if(shi.totcol)
377                 VECCOPY(shi.vcol, shi.col[0].col);
378 }
379
380 static void fastshade(float *co, float *nor, float *orco, Material *ma, char *col1, char *col2)
381 {
382         ShadeResult shr;
383         int a;
384         
385         VECCOPY(shi.co, co);
386         shi.vn[0]= -nor[0];
387         shi.vn[1]= -nor[1];
388         shi.vn[2]= -nor[2];
389         VECCOPY(shi.vno, shi.vn);
390         VECCOPY(shi.facenor, shi.vn);
391         
392         if(ma->texco) {
393                 VECCOPY(shi.lo, orco);
394                 
395                 if(ma->texco & TEXCO_GLOB) {
396                         VECCOPY(shi.gl, shi.lo);
397                 }
398                 if(ma->texco & TEXCO_WINDOW) {
399                         VECCOPY(shi.winco, shi.lo);
400                 }
401                 if(ma->texco & TEXCO_STICKY) {
402                         VECCOPY(shi.sticky, shi.lo);
403                 }
404                 if(ma->texco & TEXCO_OBJECT) {
405                         VECCOPY(shi.co, shi.lo);
406                 }
407                 if(ma->texco & TEXCO_NORM) {
408                         VECCOPY(shi.orn, shi.vn);
409                 }
410                 if(ma->texco & TEXCO_REFL) {
411                         float inp= 2.0*(shi.vn[2]);
412                         shi.ref[0]= (inp*shi.vn[0]);
413                         shi.ref[1]= (inp*shi.vn[1]);
414                         shi.ref[2]= (-1.0+inp*shi.vn[2]);
415                 }
416         }
417         
418         shi.mat= ma;    /* set each time... node shaders change it */
419         RE_shade_external(NULL, &shi, &shr);
420         
421         a= 256.0f*(shr.combined[0]);
422         col1[0]= CLAMPIS(a, 0, 255);
423         a= 256.0f*(shr.combined[1]);
424         col1[1]= CLAMPIS(a, 0, 255);
425         a= 256.0f*(shr.combined[2]);
426         col1[2]= CLAMPIS(a, 0, 255);
427         
428         if(col2) {
429                 shi.vn[0]= -shi.vn[0];
430                 shi.vn[1]= -shi.vn[1];
431                 shi.vn[2]= -shi.vn[2];
432                 
433                 shi.mat= ma;    /* set each time... node shaders change it */
434                 RE_shade_external(NULL, &shi, &shr);
435                 
436                 a= 256.0f*(shr.combined[0]);
437                 col2[0]= CLAMPIS(a, 0, 255);
438                 a= 256.0f*(shr.combined[1]);
439                 col2[1]= CLAMPIS(a, 0, 255);
440                 a= 256.0f*(shr.combined[2]);
441                 col2[2]= CLAMPIS(a, 0, 255);
442         }
443 }
444
445 static void init_fastshade_for_ob(Render *re, Object *ob, int *need_orco_r, float mat[4][4], float imat[3][3])
446 {
447         float tmat[4][4];
448         float amb[3]= {0.0f, 0.0f, 0.0f};
449         int a;
450         
451         /* initialize globals in render */
452         RE_shade_external(re, NULL, NULL);
453
454         /* initialize global here */
455         init_fastshade_shadeinput(re);
456         
457         RE_DataBase_GetView(re, tmat);
458         mul_m4_m4m4(mat, ob->obmat, tmat);
459         
460         invert_m4_m4(tmat, mat);
461         copy_m3_m4(imat, tmat);
462         if(ob->transflag & OB_NEG_SCALE) mul_m3_fl(imat, -1.0);
463         
464         if (need_orco_r) *need_orco_r= 0;
465         for(a=0; a<ob->totcol; a++) {
466                 Material *ma= give_current_material(ob, a+1);
467                 if(ma) {
468                         init_render_material(ma, 0, amb);
469
470                         if(ma->texco & TEXCO_ORCO) {
471                                 if (need_orco_r) *need_orco_r= 1;
472                         }
473                 }
474         }
475 }
476
477 static void end_fastshade_for_ob(Object *ob)
478 {
479         int a;
480         
481         for(a=0; a<ob->totcol; a++) {
482                 Material *ma= give_current_material(ob, a+1);
483                 if(ma)
484                         end_render_material(ma);
485         }
486 }
487
488
489 static void mesh_create_shadedColors(Render *re, Object *ob, int onlyForMesh, unsigned int **col1_r, unsigned int **col2_r)
490 {
491         Mesh *me= ob->data;
492         DerivedMesh *dm;
493         MVert *mvert;
494         MFace *mface;
495         unsigned int *col1, *col2;
496         float *orco, *vnors, *nors, imat[3][3], mat[4][4], vec[3];
497         int a, i, need_orco, totface, totvert;
498         CustomDataMask dataMask = CD_MASK_BAREMESH | CD_MASK_MCOL
499                                                           | CD_MASK_MTFACE | CD_MASK_NORMAL;
500
501
502         init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
503
504         if(need_orco)
505                 dataMask |= CD_MASK_ORCO;
506
507         if (onlyForMesh)
508                 dm = mesh_get_derived_deform(RE_GetScene(re), ob, dataMask);
509         else
510                 dm = mesh_get_derived_final(RE_GetScene(re), ob, dataMask);
511         
512         mvert = dm->getVertArray(dm);
513         mface = dm->getFaceArray(dm);
514         nors = dm->getFaceDataArray(dm, CD_NORMAL);
515         totvert = dm->getNumVerts(dm);
516         totface = dm->getNumFaces(dm);
517         orco= dm->getVertDataArray(dm, CD_ORCO);
518
519         if (onlyForMesh) {
520                 col1 = *col1_r;
521                 col2 = NULL;
522         } else {
523                 *col1_r = col1 = MEM_mallocN(sizeof(*col1)*totface*4, "col1");
524
525                 if (col2_r && (me->flag & ME_TWOSIDED))
526                         col2 = MEM_mallocN(sizeof(*col2)*totface*4, "col2");
527                 else
528                         col2 = NULL;
529                 
530                 if (col2_r) *col2_r = col2;
531         }
532
533                 /* vertexnormals */
534         vnors= MEM_mallocN(totvert*3*sizeof(float), "vnors disp");
535         for (a=0; a<totvert; a++) {
536                 MVert *mv = &mvert[a];
537                 float *vn= &vnors[a*3];
538                 float xn= mv->no[0]; 
539                 float yn= mv->no[1]; 
540                 float zn= mv->no[2];
541                 
542                         /* transpose ! */
543                 vn[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
544                 vn[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
545                 vn[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
546                 normalize_v3(vn);
547         }               
548
549         for (i=0; i<totface; i++) {
550                 extern Material defmaterial;    /* material.c */
551                 MFace *mf= &mface[i];
552                 Material *ma= give_current_material(ob, mf->mat_nr+1);
553                 int j, vidx[4], nverts= mf->v4?4:3;
554                 unsigned char *col1base= (unsigned char*) &col1[i*4];
555                 unsigned char *col2base= (unsigned char*) (col2?&col2[i*4]:NULL);
556                 float nor[3], n1[3];
557                 
558                 if(ma==NULL) ma= &defmaterial;
559                 
560                 vidx[0]= mf->v1;
561                 vidx[1]= mf->v2;
562                 vidx[2]= mf->v3;
563                 vidx[3]= mf->v4;
564
565                 if (nors) {
566                         VECCOPY(nor, &nors[i*3]);
567                 } else {
568                         if (mf->v4)
569                                 normal_quad_v3( nor,mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co, mvert[mf->v4].co);
570                         else
571                                 normal_tri_v3( nor,mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co);
572                 }
573
574                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
575                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
576                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
577                 normalize_v3(n1);
578
579                 for (j=0; j<nverts; j++) {
580                         MVert *mv= &mvert[vidx[j]];
581                         char *col1= (char*)&col1base[j*4];
582                         char *col2= (char*)(col2base?&col2base[j*4]:NULL);
583                         float *vn = (mf->flag & ME_SMOOTH)?&vnors[3*vidx[j]]:n1;
584
585                         mul_v3_m4v3(vec, mat, mv->co);
586
587                         vec[0]+= 0.001*vn[0];
588                         vec[1]+= 0.001*vn[1];
589                         vec[2]+= 0.001*vn[2];
590
591                         fastshade_customdata(&dm->faceData, i, j, ma);
592                         fastshade(vec, vn, orco?&orco[vidx[j]*3]:mv->co, ma, col1, col2);
593                 }
594         } 
595         MEM_freeN(vnors);
596
597         dm->release(dm);
598
599         end_fastshade_for_ob(ob);
600 }
601
602 void shadeMeshMCol(Scene *scene, Object *ob, Mesh *me)
603 {
604         Render *re= fastshade_get_render(scene);
605         int a;
606         char *cp;
607         unsigned int *mcol= (unsigned int*)me->mcol;
608         
609         if(re) {
610                 mesh_create_shadedColors(re, ob, 1, &mcol, NULL);
611                 me->mcol= (MCol*)mcol;
612
613                 /* swap bytes */
614                 for(cp= (char *)me->mcol, a= 4*me->totface; a>0; a--, cp+=4) {
615                         SWAP(char, cp[0], cp[3]);
616                         SWAP(char, cp[1], cp[2]);
617                 }
618         }
619 }
620
621 /* has base pointer, to check for layer */
622 /* called from drawobject.c */
623 void shadeDispList(Scene *scene, Base *base)
624 {
625         Object *ob= base->object;
626         DispList *dl, *dlob;
627         Material *ma = NULL;
628         Render *re;
629         float imat[3][3], mat[4][4], vec[3];
630         float *fp, *nor, n1[3];
631         unsigned int *col1;
632         int a, need_orco;
633         
634         re= fastshade_get_render(scene);
635         if(re==NULL)
636                 return;
637         
638         dl = find_displist(&ob->disp, DL_VERTCOL);
639         if (dl) {
640                 BLI_remlink(&ob->disp, dl);
641                 free_disp_elem(dl);
642         }
643
644         if(ob->type==OB_MESH) {
645                 dl= MEM_callocN(sizeof(DispList), "displistshade");
646                 dl->type= DL_VERTCOL;
647
648                 mesh_create_shadedColors(re, ob, 0, &dl->col1, &dl->col2);
649
650                 /* add dl to ob->disp after mesh_create_shadedColors, because it
651                    might indirectly free ob->disp */
652                 BLI_addtail(&ob->disp, dl);
653         }
654         else {
655
656                 init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
657                 
658                 if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
659                 
660                         /* now we need the normals */
661                         dl= ob->disp.first;
662                         
663                         while(dl) {
664                                 extern Material defmaterial;    /* material.c */
665                                 
666                                 dlob= MEM_callocN(sizeof(DispList), "displistshade");
667                                 BLI_addtail(&ob->disp, dlob);
668                                 dlob->type= DL_VERTCOL;
669                                 dlob->parts= dl->parts;
670                                 dlob->nr= dl->nr;
671                                 
672                                 if(dl->type==DL_INDEX3) {
673                                         col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
674                                 }
675                                 else {
676                                         col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->parts*dl->nr, "col1");
677                                 }
678                                 
679                         
680                                 ma= give_current_material(ob, dl->col+1);
681                                 if(ma==NULL) ma= &defmaterial;
682                                 
683                                 if(dl->type==DL_INDEX3) {
684                                         if(dl->nors) {
685                                                 /* there's just one normal */
686                                                 n1[0]= imat[0][0]*dl->nors[0]+imat[0][1]*dl->nors[1]+imat[0][2]*dl->nors[2];
687                                                 n1[1]= imat[1][0]*dl->nors[0]+imat[1][1]*dl->nors[1]+imat[1][2]*dl->nors[2];
688                                                 n1[2]= imat[2][0]*dl->nors[0]+imat[2][1]*dl->nors[1]+imat[2][2]*dl->nors[2];
689                                                 normalize_v3(n1);
690                                                 
691                                                 fp= dl->verts;
692                                                 
693                                                 a= dl->nr;              
694                                                 while(a--) {
695                                                         mul_v3_m4v3(vec, mat, fp);
696                                                         
697                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
698                                                         
699                                                         fp+= 3; col1++;
700                                                 }
701                                         }
702                                 }
703                                 else if(dl->type==DL_SURF) {
704                                         if(dl->nors) {
705                                                 a= dl->nr*dl->parts;
706                                                 fp= dl->verts;
707                                                 nor= dl->nors;
708                                                 
709                                                 while(a--) {
710                                                         mul_v3_m4v3(vec, mat, fp);
711                                                         
712                                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
713                                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
714                                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
715                                                         normalize_v3(n1);
716                                 
717                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
718                                                         
719                                                         fp+= 3; nor+= 3; col1++;
720                                                 }
721                                         }
722                                 }
723                                 dl= dl->next;
724                         }
725                 }
726                 else if(ob->type==OB_MBALL) {
727                         /* there are normals already */
728                         dl= ob->disp.first;
729                         
730                         while(dl) {
731                                 
732                                 if(dl->type==DL_INDEX4) {
733                                         if(dl->nors) {
734                                                 extern Material defmaterial;    /* material.c */
735                                                 
736                                                 if(dl->col1) MEM_freeN(dl->col1);
737                                                 col1= dl->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
738                                 
739                                                 ma= give_current_material(ob, dl->col+1);
740                                                 if(ma==NULL) ma= &defmaterial;
741                                                 
742                                                 fp= dl->verts;
743                                                 nor= dl->nors;
744                                                 
745                                                 a= dl->nr;              
746                                                 while(a--) {
747                                                         mul_v3_m4v3(vec, mat, fp);
748                                                         
749                                                         /* transpose ! */
750                                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
751                                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
752                                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
753                                                         normalize_v3(n1);
754                                                 
755                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
756                                                         
757                                                         fp+= 3; col1++; nor+= 3;
758                                                 }
759                                         }
760                                 }
761                                 dl= dl->next;
762                         }
763                 }
764                 
765                 end_fastshade_for_ob(ob);
766         }
767 }
768
769 /* frees render and shade part of displists */
770 /* note: dont do a shade again, until a redraw happens */
771 void reshadeall_displist(Scene *scene)
772 {
773         Base *base;
774         Object *ob;
775         
776         fastshade_free_render();
777         
778         for(base= scene->base.first; base; base= base->next) {
779                 ob= base->object;
780
781                 if(ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
782                         freedisplist(&ob->disp);
783
784                 if(base->lay & scene->lay) {
785                         /* Metaballs have standard displist at the Object */
786                         if(ob->type==OB_MBALL) shadeDispList(scene, base);
787                 }
788         }
789 }
790
791 /* ****************** make displists ********************* */
792
793 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase, int forRender)
794 {
795         Nurb *nu;
796         DispList *dl;
797         BezTriple *bezt, *prevbezt;
798         BPoint *bp;
799         float *data;
800         int a, len, resolu;
801         
802         nu= nubase->first;
803         while(nu) {
804                 if(nu->hide==0) {
805                         
806                         if(forRender && cu->resolu_ren!=0)
807                                 resolu= cu->resolu_ren;
808                         else
809                                 resolu= nu->resolu;
810                         
811                         if(!check_valid_nurb_u(nu));
812                         else if(nu->type == CU_BEZIER) {
813                                 
814                                 /* count */
815                                 len= 0;
816                                 a= nu->pntsu-1;
817                                 if(nu->flagu & CU_NURB_CYCLIC) a++;
818
819                                 prevbezt= nu->bezt;
820                                 bezt= prevbezt+1;
821                                 while(a--) {
822                                         if(a==0 && (nu->flagu & CU_NURB_CYCLIC)) bezt= nu->bezt;
823                                         
824                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) len++;
825                                         else len+= resolu;
826                                         
827                                         if(a==0 && (nu->flagu & CU_NURB_CYCLIC)==0) len++;
828                                         
829                                         prevbezt= bezt;
830                                         bezt++;
831                                 }
832                                 
833                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbez");
834                                 /* len+1 because of 'forward_diff_bezier' function */
835                                 dl->verts= MEM_callocN( (len+1)*3*sizeof(float), "dlverts");
836                                 BLI_addtail(dispbase, dl);
837                                 dl->parts= 1;
838                                 dl->nr= len;
839                                 dl->col= nu->mat_nr;
840                                 dl->charidx= nu->charidx;
841
842                                 data= dl->verts;
843
844                                 if(nu->flagu & CU_NURB_CYCLIC) {
845                                         dl->type= DL_POLY;
846                                         a= nu->pntsu;
847                                 }
848                                 else {
849                                         dl->type= DL_SEGM;
850                                         a= nu->pntsu-1;
851                                 }
852                                 
853                                 prevbezt= nu->bezt;
854                                 bezt= prevbezt+1;
855                                 
856                                 while(a--) {
857                                         if(a==0 && dl->type== DL_POLY) bezt= nu->bezt;
858                                         
859                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
860                                                 VECCOPY(data, prevbezt->vec[1]);
861                                                 data+= 3;
862                                         }
863                                         else {
864                                                 int j;
865                                                 for(j=0; j<3; j++) {
866                                                         forward_diff_bezier(    prevbezt->vec[1][j],
867                                                                                                         prevbezt->vec[2][j],
868                                                                                                         bezt->vec[0][j],
869                                                                                                         bezt->vec[1][j],
870                                                                                                         data+j, resolu, 3*sizeof(float));
871                                                 }
872                                                 
873                                                 data+= 3*resolu;
874                                         }
875                                         
876                                         if(a==0 && dl->type==DL_SEGM) {
877                                                 VECCOPY(data, bezt->vec[1]);
878                                         }
879                                         
880                                         prevbezt= bezt;
881                                         bezt++;
882                                 }
883                         }
884                         else if(nu->type == CU_NURBS) {
885                                 len= (resolu*SEGMENTSU(nu));
886                                 
887                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
888                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
889                                 BLI_addtail(dispbase, dl);
890                                 dl->parts= 1;
891                                 
892                                 dl->nr= len;
893                                 dl->col= nu->mat_nr;
894                                 dl->charidx = nu->charidx;
895
896                                 data= dl->verts;
897                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
898                                 else dl->type= DL_SEGM;
899                                 makeNurbcurve(nu, data, NULL, NULL, NULL, resolu, 3*sizeof(float));
900                         }
901                         else if(nu->type == CU_POLY) {
902                                 len= nu->pntsu;
903                                 dl= MEM_callocN(sizeof(DispList), "makeDispListpoly");
904                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
905                                 BLI_addtail(dispbase, dl);
906                                 dl->parts= 1;
907                                 dl->nr= len;
908                                 dl->col= nu->mat_nr;
909                                 dl->charidx = nu->charidx;
910
911                                 data= dl->verts;
912                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
913                                 else dl->type= DL_SEGM;
914                                 
915                                 a= len;
916                                 bp= nu->bp;
917                                 while(a--) {
918                                         VECCOPY(data, bp->vec);
919                                         bp++;
920                                         data+= 3;
921                                 }
922                         }
923                 }
924                 nu= nu->next;
925         }
926 }
927
928
929 void filldisplist(ListBase *dispbase, ListBase *to, int flipnormal)
930 {
931         EditVert *eve, *v1, *vlast;
932         EditFace *efa;
933         DispList *dlnew=0, *dl;
934         float *f1;
935         int colnr=0, charidx=0, cont=1, tot, a, *index, nextcol= 0;
936         intptr_t totvert;
937         
938         if(dispbase==0) return;
939         if(dispbase->first==0) return;
940
941         while(cont) {
942                 cont= 0;
943                 totvert= 0;
944                 nextcol= 0;
945                 
946                 dl= dispbase->first;
947                 while(dl) {
948         
949                         if(dl->type==DL_POLY) {
950                                 if(charidx<dl->charidx) cont= 1;
951                                 else if(charidx==dl->charidx) { /* character with needed index */
952                                         if(colnr==dl->col) {
953                                                 /* make editverts and edges */
954                                                 f1= dl->verts;
955                                                 a= dl->nr;
956                                                 eve= v1= 0;
957                                                 
958                                                 while(a--) {
959                                                         vlast= eve;
960
961                                                         eve= BLI_addfillvert(f1);
962                                                         totvert++;
963
964                                                         if(vlast==0) v1= eve;
965                                                         else {
966                                                                 BLI_addfilledge(vlast, eve);
967                                                         }
968                                                         f1+=3;
969                                                 }
970
971                                                 if(eve!=0 && v1!=0) {
972                                                         BLI_addfilledge(eve, v1);
973                                                 }
974                                         } else if (colnr<dl->col) {
975                                                 /* got poly with next material at current char */
976                                                 cont= 1;
977                                                 nextcol= 1;
978                                         }
979                                 }
980                         }
981                         dl= dl->next;
982                 }
983                 
984                 if(totvert && BLI_edgefill(0)) { // XXX (obedit && obedit->actcol)?(obedit->actcol-1):0)) {
985
986                         /* count faces  */
987                         tot= 0;
988                         efa= fillfacebase.first;
989                         while(efa) {
990                                 tot++;
991                                 efa= efa->next;
992                         }
993
994                         if(tot) {
995                                 dlnew= MEM_callocN(sizeof(DispList), "filldisplist");
996                                 dlnew->type= DL_INDEX3;
997                                 dlnew->col= colnr;
998                                 dlnew->nr= totvert;
999                                 dlnew->parts= tot;
1000
1001                                 dlnew->index= MEM_mallocN(tot*3*sizeof(int), "dlindex");
1002                                 dlnew->verts= MEM_mallocN(totvert*3*sizeof(float), "dlverts");
1003                                 
1004                                 /* vert data */
1005                                 f1= dlnew->verts;
1006                                 totvert= 0;
1007                                 eve= fillvertbase.first;
1008                                 while(eve) {
1009                                         VECCOPY(f1, eve->co);
1010                                         f1+= 3;
1011         
1012                                         /* index number */
1013                                         eve->tmp.l = totvert;
1014                                         totvert++;
1015                                         
1016                                         eve= eve->next;
1017                                 }
1018                                 
1019                                 /* index data */
1020                                 efa= fillfacebase.first;
1021                                 index= dlnew->index;
1022                                 while(efa) {
1023                                         index[0]= (intptr_t)efa->v1->tmp.l;
1024                                         index[1]= (intptr_t)efa->v2->tmp.l;
1025                                         index[2]= (intptr_t)efa->v3->tmp.l;
1026
1027                                         if(flipnormal)
1028                                                 SWAP(int, index[0], index[2]);
1029                                         
1030                                         index+= 3;
1031                                         efa= efa->next;
1032                                 }
1033                         }
1034
1035                         BLI_addhead(to, dlnew);
1036                         
1037                 }
1038                 BLI_end_edgefill();
1039
1040                 if(nextcol) {
1041                         /* stay at current char but fill polys with next material */
1042                         colnr++;
1043                 } else {
1044                         /* switch to next char and start filling from first material */
1045                         charidx++;
1046                         colnr= 0;
1047                 }
1048         }
1049         
1050         /* do not free polys, needed for wireframe display */
1051         
1052 }
1053
1054 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
1055 {
1056         ListBase front, back;
1057         DispList *dl, *dlnew;
1058         float *fp, *fp1;
1059         int a, dpoly;
1060         
1061         front.first= front.last= back.first= back.last= 0;
1062         
1063         dl= dispbase->first;
1064         while(dl) {
1065                 if(dl->type==DL_SURF) {
1066                         if( (dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U)==0 ) {
1067                                 if( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE) ) {
1068                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1069                                         BLI_addtail(&front, dlnew);
1070                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1071                                         dlnew->nr= dl->parts;
1072                                         dlnew->parts= 1;
1073                                         dlnew->type= DL_POLY;
1074                                         dlnew->col= dl->col;
1075                                         dlnew->charidx = dl->charidx;
1076                                         
1077                                         fp= dl->verts;
1078                                         dpoly= 3*dl->nr;
1079                                         
1080                                         a= dl->parts;
1081                                         while(a--) {
1082                                                 VECCOPY(fp1, fp);
1083                                                 fp1+= 3;
1084                                                 fp+= dpoly;
1085                                         }
1086                                 }
1087                                 if( (cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE) ) {
1088                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1089                                         BLI_addtail(&back, dlnew);
1090                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1091                                         dlnew->nr= dl->parts;
1092                                         dlnew->parts= 1;
1093                                         dlnew->type= DL_POLY;
1094                                         dlnew->col= dl->col;
1095                                         dlnew->charidx= dl->charidx;
1096                                         
1097                                         fp= dl->verts+3*(dl->nr-1);
1098                                         dpoly= 3*dl->nr;
1099                                         
1100                                         a= dl->parts;
1101                                         while(a--) {
1102                                                 VECCOPY(fp1, fp);
1103                                                 fp1+= 3;
1104                                                 fp+= dpoly;
1105                                         }
1106                                 }
1107                         }
1108                 }
1109                 dl= dl->next;
1110         }
1111
1112         filldisplist(&front, dispbase, 1);
1113         filldisplist(&back, dispbase, 0);
1114         
1115         freedisplist(&front);
1116         freedisplist(&back);
1117
1118         filldisplist(dispbase, dispbase, 0);
1119         
1120 }
1121
1122 static void curve_to_filledpoly(Curve *cu, ListBase *UNUSED(nurb), ListBase *dispbase)
1123 {
1124         if(cu->flag & CU_3D) return;
1125
1126         if(dispbase->first && ((DispList*) dispbase->first)->type==DL_SURF) {
1127                 bevels_to_filledpoly(cu, dispbase);
1128         }
1129         else {
1130                 filldisplist(dispbase, dispbase, 0);
1131         }
1132 }
1133
1134 /* taper rules:
1135   - only 1 curve
1136   - first point left, last point right
1137   - based on subdivided points in original curve, not on points in taper curve (still)
1138 */
1139 float calc_taper(Scene *scene, Object *taperobj, int cur, int tot)
1140 {
1141         DispList *dl;
1142         
1143         if(taperobj==NULL || taperobj->type!=OB_CURVE) return 1.0;
1144         
1145         dl= taperobj->disp.first;
1146         if(dl==NULL) {
1147                 makeDispListCurveTypes(scene, taperobj, 0);
1148                 dl= taperobj->disp.first;
1149         }
1150         if(dl) {
1151                 float fac= ((float)cur)/(float)(tot-1);
1152                 float minx, dx, *fp;
1153                 int a;
1154                 
1155                 /* horizontal size */
1156                 minx= dl->verts[0];
1157                 dx= dl->verts[3*(dl->nr-1)] - minx;
1158                 if(dx>0.0) {
1159                 
1160                         fp= dl->verts;
1161                         for(a=0; a<dl->nr; a++, fp+=3) {
1162                                 if( (fp[0]-minx)/dx >= fac) {
1163                                         /* interpolate with prev */
1164                                         if(a>0) {
1165                                                 float fac1= (fp[-3]-minx)/dx;
1166                                                 float fac2= (fp[0]-minx)/dx;
1167                                                 if(fac1!=fac2)
1168                                                         return fp[1]*(fac1-fac)/(fac1-fac2) + fp[-2]*(fac-fac2)/(fac1-fac2);
1169                                         }
1170                                         return fp[1];
1171                                 }
1172                         }
1173                         return fp[-2];  // last y coord
1174                 }
1175         }
1176         
1177         return 1.0;
1178 }
1179
1180 void makeDispListMBall(Scene *scene, Object *ob)
1181 {
1182         if(!ob || ob->type!=OB_MBALL) return;
1183
1184         // XXX: mball stuff uses plenty of global variables
1185         //      while this is unchanged updating during render is unsafe
1186         if(G.rendering) return;
1187
1188         freedisplist(&(ob->disp));
1189
1190         if(ob->type==OB_MBALL) {
1191                 if(ob==find_basis_mball(scene, ob)) {
1192                         metaball_polygonize(scene, ob, &ob->disp);
1193                         tex_space_mball(ob);
1194
1195                         object_deform_mball(ob, &ob->disp);
1196                 }
1197         }
1198         
1199         boundbox_displist(ob);
1200 }
1201
1202 void makeDispListMBall_forRender(Scene *scene, Object *ob, ListBase *dispbase)
1203 {
1204         metaball_polygonize(scene, ob, dispbase);
1205         tex_space_mball(ob);
1206         
1207         object_deform_mball(ob, dispbase);
1208 }
1209
1210 static ModifierData *curve_get_tesselate_point(Scene *scene, Object *ob, int forRender, int editmode)
1211 {
1212         ModifierData *md = modifiers_getVirtualModifierList(ob);
1213         ModifierData *preTesselatePoint;
1214         int required_mode;
1215
1216         if(forRender) required_mode = eModifierMode_Render;
1217         else required_mode = eModifierMode_Realtime;
1218
1219         if(editmode) required_mode |= eModifierMode_Editmode;
1220
1221         preTesselatePoint = NULL;
1222         for (; md; md=md->next) {
1223                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1224
1225                 if (!modifier_isEnabled(scene, md, required_mode)) continue;
1226                 if (mti->type == eModifierTypeType_Constructive) return preTesselatePoint;
1227
1228                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
1229                         preTesselatePoint = md;
1230
1231                         /* this modifiers are moving point of tesselation automatically
1232                            (some of them even can't be applied on tesselated curve), set flag
1233                            for incformation button in modifier's header */
1234                         md->mode |= eModifierMode_ApplyOnSpline;
1235                 } else if(md->mode&eModifierMode_ApplyOnSpline) {
1236                         preTesselatePoint = md;
1237                 }
1238         }
1239
1240         return preTesselatePoint;
1241 }
1242
1243 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, int forRender, float (**originalVerts_r)[3], float (**deformedVerts_r)[3], int *numVerts_r)
1244 {
1245         ModifierData *md = modifiers_getVirtualModifierList(ob);
1246         ModifierData *preTesselatePoint;
1247         Curve *cu= ob->data;
1248         ListBase *nurb= BKE_curve_nurbs(cu);
1249         int numVerts = 0;
1250         int editmode = (!forRender && cu->editnurb);
1251         float (*originalVerts)[3] = NULL;
1252         float (*deformedVerts)[3] = NULL;
1253         float *keyVerts= NULL;
1254         int required_mode;
1255
1256         if(forRender) required_mode = eModifierMode_Render;
1257         else required_mode = eModifierMode_Realtime;
1258
1259         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1260         
1261         if(editmode) required_mode |= eModifierMode_Editmode;
1262
1263         if(cu->editnurb==NULL) {
1264                 keyVerts= do_ob_key(scene, ob);
1265
1266                 if(keyVerts) {
1267                         /* split coords from key data, the latter also includes
1268                            tilts, which is passed through in the modifier stack.
1269                            this is also the reason curves do not use a virtual
1270                            shape key modifier yet. */
1271                         deformedVerts= curve_getKeyVertexCos(cu, nurb, keyVerts);
1272                         originalVerts= MEM_dupallocN(deformedVerts);
1273                 }
1274         }
1275         
1276         if (preTesselatePoint) {
1277                 for (; md; md=md->next) {
1278                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1279
1280                         md->scene= scene;
1281                         
1282                         if ((md->mode & required_mode) != required_mode) continue;
1283                         if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1284                         if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1285
1286                         if (!deformedVerts) {
1287                                 deformedVerts = curve_getVertexCos(cu, nurb, &numVerts);
1288                                 originalVerts = MEM_dupallocN(deformedVerts);
1289                         }
1290
1291                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, forRender, editmode);
1292
1293                         if (md==preTesselatePoint)
1294                                 break;
1295                 }
1296         }
1297
1298         if (deformedVerts)
1299                 curve_applyVertexCos(cu, nurb, deformedVerts);
1300         if (keyVerts) /* these are not passed through modifier stack */
1301                 curve_applyKeyVertexTilts(cu, nurb, keyVerts);
1302
1303         if(keyVerts)
1304                 MEM_freeN(keyVerts);
1305
1306         *originalVerts_r = originalVerts;
1307         *deformedVerts_r = deformedVerts;
1308         *numVerts_r = numVerts;
1309 }
1310
1311 static float (*displist_get_allverts (ListBase *dispbase, int *totvert))[3]
1312 {
1313         DispList *dl;
1314         float (*allverts)[3], *fp;
1315
1316         *totvert= 0;
1317
1318         for (dl=dispbase->first; dl; dl=dl->next)
1319                 *totvert+= (dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr;
1320
1321         allverts= MEM_mallocN((*totvert)*sizeof(float)*3, "displist_get_allverts allverts");
1322         fp= (float*)allverts;
1323         for (dl=dispbase->first; dl; dl=dl->next) {
1324                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1325                 memcpy(fp, dl->verts, sizeof(float) * offs);
1326                 fp+= offs;
1327         }
1328
1329         return allverts;
1330 }
1331
1332 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
1333 {
1334         DispList *dl;
1335         float *fp;
1336
1337         fp= (float*)allverts;
1338         for (dl=dispbase->first; dl; dl=dl->next) {
1339                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1340                 memcpy(dl->verts, fp, sizeof(float) * offs);
1341                 fp+= offs;
1342         }
1343 }
1344
1345 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispbase,
1346         DerivedMesh **derivedFinal, int forRender, float (*originalVerts)[3], float (*deformedVerts)[3])
1347 {
1348         ModifierData *md = modifiers_getVirtualModifierList(ob);
1349         ModifierData *preTesselatePoint;
1350         Curve *cu= ob->data;
1351         ListBase *nurb= BKE_curve_nurbs(cu);
1352         int required_mode = 0, totvert = 0;
1353         int editmode = (!forRender && cu->editnurb);
1354         DerivedMesh *dm= NULL, *ndm;
1355         float (*vertCos)[3] = NULL;
1356
1357         if(forRender) required_mode = eModifierMode_Render;
1358         else required_mode = eModifierMode_Realtime;
1359
1360         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1361         
1362         if(editmode) required_mode |= eModifierMode_Editmode;
1363
1364         if (preTesselatePoint) {
1365                 md = preTesselatePoint->next;
1366         }
1367
1368         if (derivedFinal && *derivedFinal) {
1369                 (*derivedFinal)->release (*derivedFinal);
1370         }
1371
1372         for (; md; md=md->next) {
1373                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1374
1375                 md->scene= scene;
1376
1377                 if ((md->mode & required_mode) != required_mode) continue;
1378                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1379
1380                 if (mti->type == eModifierTypeType_OnlyDeform ||
1381                                 (mti->type == eModifierTypeType_DeformOrConstruct && !dm)) {
1382                         if (dm) {
1383                                 if (!vertCos) {
1384                                         totvert = dm->getNumVerts(dm);
1385                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
1386                                         dm->getVertCos(dm, vertCos);
1387                                 }
1388
1389                                 mti->deformVerts(md, ob, dm, vertCos, totvert, forRender, editmode);
1390                         } else {
1391                                 if (!vertCos) {
1392                                         vertCos= displist_get_allverts(dispbase, &totvert);
1393                                 }
1394
1395                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, forRender, editmode);
1396                         }
1397                 } else {
1398                         if (!derivedFinal) {
1399                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh */
1400                                 /* is totally unnecessary, so we could stop modifiers applying */
1401                                 /* when we found constructive modifier but derived mesh is unwanted result */
1402                                 break;
1403                         }
1404
1405                         if (dm) {
1406                                 if (vertCos) {
1407                                         DerivedMesh *tdm = CDDM_copy(dm);
1408                                         dm->release(dm);
1409                                         dm = tdm;
1410
1411                                         CDDM_apply_vert_coords(dm, vertCos);
1412                                         CDDM_calc_normals(dm);
1413                                 }
1414                         } else {
1415                                 if (vertCos) {
1416                                         displist_apply_allverts(dispbase, vertCos);
1417                                 }
1418
1419                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
1420                                         curve_to_filledpoly(cu, nurb, dispbase);
1421                                 }
1422
1423                                 dm= CDDM_from_curve_customDB(ob, dispbase);
1424
1425                                 CDDM_calc_normals(dm);
1426                         }
1427
1428                         if (vertCos) {
1429                                 /* Vertex coordinates were applied to necessary data, could free it */
1430                                 MEM_freeN(vertCos);
1431                                 vertCos= NULL;
1432                         }
1433
1434                         ndm = mti->applyModifier(md, ob, dm, forRender, editmode);
1435
1436                         if (ndm) {
1437                                 /* Modifier returned a new derived mesh */
1438
1439                                 if (dm && dm != ndm) /* Modifier  */
1440                                         dm->release (dm);
1441                                 dm = ndm;
1442                         }
1443                 }
1444         }
1445
1446         if (vertCos) {
1447                 if (dm) {
1448                         DerivedMesh *tdm = CDDM_copy(dm);
1449                         dm->release(dm);
1450                         dm = tdm;
1451
1452                         CDDM_apply_vert_coords(dm, vertCos);
1453                         CDDM_calc_normals(dm);
1454                         MEM_freeN(vertCos);
1455                 } else {
1456                         displist_apply_allverts(dispbase, vertCos);
1457                         MEM_freeN(vertCos);
1458                         vertCos= NULL;
1459                 }
1460         }
1461
1462         if (derivedFinal) {
1463                 (*derivedFinal) = dm;
1464         }
1465
1466         if (deformedVerts) {
1467                 curve_applyVertexCos(ob->data, nurb, originalVerts);
1468                 MEM_freeN(originalVerts);
1469                 MEM_freeN(deformedVerts);
1470         }
1471 }
1472
1473 static void displist_surf_indices(DispList *dl)
1474 {
1475         int a, b, p1, p2, p3, p4;
1476         int *index;
1477         
1478         dl->totindex= 0;
1479         
1480         index=dl->index= MEM_mallocN( 4*sizeof(int)*(dl->parts+1)*(dl->nr+1), "index array nurbs");
1481         
1482         for(a=0; a<dl->parts; a++) {
1483                 
1484                 if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
1485                         break;
1486                 
1487                 for(; b<dl->nr; b++, index+=4) {        
1488                         index[0]= p1;
1489                         index[1]= p2;
1490                         index[2]= p4;
1491                         index[3]= p3;
1492                         
1493                         dl->totindex++;
1494                         
1495                         p2= p1; p1++;
1496                         p4= p3; p3++;
1497
1498                 }
1499         }
1500         
1501 }
1502
1503 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
1504 {
1505         DerivedMesh *dm;
1506         ListBase disp= {NULL, NULL};
1507
1508         /* OrcoDM should be created from underformed disp lists */
1509         makeDispListCurveTypes_forOrco(scene, ob, &disp);
1510         dm= CDDM_from_curve_customDB(ob, &disp);
1511
1512         freedisplist(&disp);
1513
1514         return dm;
1515 }
1516
1517 static void add_orco_dm(Scene *scene, Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1518 {
1519         float (*orco)[3], (*layerorco)[3];
1520         int totvert, a;
1521         Curve *cu= ob->data;
1522
1523         totvert= dm->getNumVerts(dm);
1524
1525         if(orcodm) {
1526                 orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
1527
1528                 if(orcodm->getNumVerts(orcodm) == totvert)
1529                         orcodm->getVertCos(orcodm, orco);
1530                 else
1531                         dm->getVertCos(dm, orco);
1532         }
1533         else {
1534                 orco= (float(*)[3])make_orco_curve(scene, ob);
1535         }
1536
1537         for(a=0; a<totvert; a++) {
1538                 float *co = orco[a];
1539                 co[0] = (co[0]-cu->loc[0])/cu->size[0];
1540                 co[1] = (co[1]-cu->loc[1])/cu->size[1];
1541                 co[2] = (co[2]-cu->loc[2])/cu->size[2];
1542         }
1543
1544         if((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1545                 memcpy(layerorco, orco, sizeof(float)*totvert);
1546                 MEM_freeN(orco);
1547         }
1548         else
1549                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1550 }
1551
1552 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1553 {
1554         /* this function represents logic of mesh's orcodm calculation */
1555         /* for displist-based objects */
1556
1557         ModifierData *md = modifiers_getVirtualModifierList(ob);
1558         ModifierData *preTesselatePoint;
1559         Curve *cu= ob->data;
1560         int required_mode;
1561         int editmode = (!forRender && cu->editnurb);
1562         DerivedMesh *ndm, *orcodm= NULL;
1563
1564         if(forRender) required_mode = eModifierMode_Render;
1565         else required_mode = eModifierMode_Realtime;
1566
1567         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1568
1569         if(editmode) required_mode |= eModifierMode_Editmode;
1570
1571         if (preTesselatePoint) {
1572                 md = preTesselatePoint->next;
1573         }
1574
1575         for (; md; md=md->next) {
1576                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1577
1578                 md->scene= scene;
1579
1580                 if ((md->mode & required_mode) != required_mode) continue;
1581                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1582                 if (mti->type!=eModifierTypeType_Constructive) continue;
1583
1584                 if(!orcodm)
1585                         orcodm= create_orco_dm(scene, ob);
1586
1587                 ndm = mti->applyModifier(md, ob, orcodm, forRender, 0);
1588
1589                 if(ndm) {
1590                         /* if the modifier returned a new dm, release the old one */
1591                         if(orcodm && orcodm != ndm) {
1592                                 orcodm->release(orcodm);
1593                         }
1594                         orcodm = ndm;
1595                 }
1596         }
1597
1598         /* add an orco layer if needed */
1599         add_orco_dm(scene, ob, derivedFinal, orcodm);
1600
1601         if(orcodm)
1602                 orcodm->release(orcodm);
1603 }
1604
1605 void makeDispListSurf(Scene *scene, Object *ob, ListBase *dispbase,
1606         DerivedMesh **derivedFinal, int forRender, int forOrco)
1607 {
1608         ListBase *nubase;
1609         Nurb *nu;
1610         Curve *cu = ob->data;
1611         DispList *dl;
1612         float *data;
1613         int len;
1614         int numVerts;
1615         float (*originalVerts)[3];
1616         float (*deformedVerts)[3];
1617
1618         if(!forRender && cu->editnurb)
1619                 nubase= ED_curve_editnurbs(cu);
1620         else
1621                 nubase= &cu->nurb;
1622
1623         if(!forOrco)
1624                 curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1625
1626         for (nu=nubase->first; nu; nu=nu->next) {
1627                 if(forRender || nu->hide==0) {
1628                         int resolu= nu->resolu, resolv= nu->resolv;
1629
1630                         if(forRender){
1631                                 if(cu->resolu_ren) resolu= cu->resolu_ren;
1632                                 if(cu->resolv_ren) resolv= cu->resolv_ren;
1633                         }
1634
1635                         if(nu->pntsv==1) {
1636                                 len= SEGMENTSU(nu)*resolu;
1637
1638                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1639                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1640
1641                                 BLI_addtail(dispbase, dl);
1642                                 dl->parts= 1;
1643                                 dl->nr= len;
1644                                 dl->col= nu->mat_nr;
1645                                 dl->charidx= nu->charidx;
1646
1647                                 /* dl->rt will be used as flag for render face and */
1648                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1649                                 dl->rt= nu->flag & ~CU_2D;
1650
1651                                 data= dl->verts;
1652                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
1653                                 else dl->type= DL_SEGM;
1654
1655                                 makeNurbcurve(nu, data, NULL, NULL, NULL, resolu, 3*sizeof(float));
1656                         }
1657                         else {
1658                                 len= (nu->pntsu*resolu) * (nu->pntsv*resolv);
1659                                 
1660                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1661                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1662                                 BLI_addtail(dispbase, dl);
1663
1664                                 dl->col= nu->mat_nr;
1665                                 dl->charidx= nu->charidx;
1666
1667                                 /* dl->rt will be used as flag for render face and */
1668                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1669                                 dl->rt= nu->flag & ~CU_2D;
1670
1671                                 data= dl->verts;
1672                                 dl->type= DL_SURF;
1673
1674                                 dl->parts= (nu->pntsu*resolu);  /* in reverse, because makeNurbfaces works that way */
1675                                 dl->nr= (nu->pntsv*resolv);
1676                                 if(nu->flagv & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_U;    /* reverse too! */
1677                                 if(nu->flagu & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_V;
1678
1679                                 makeNurbfaces(nu, data, 0, resolu, resolv);
1680                                 
1681                                 /* gl array drawing: using indices */
1682                                 displist_surf_indices(dl);
1683                         }
1684                 }
1685         }
1686
1687         /* make copy of 'undeformed" displist for texture space calculation
1688            actually, it's not totally undeformed -- pre-tesselation modifiers are
1689            already applied, thats how it worked for years, so keep for compatibility (sergey) */
1690         copy_displist(&cu->disp, dispbase);
1691
1692         if (!forRender) {
1693                 tex_space_curve(cu);
1694         }
1695
1696         if(!forOrco)
1697                 curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal,
1698                         forRender, originalVerts, deformedVerts);
1699 }
1700
1701 static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1702         DerivedMesh **derivedFinal, int forRender, int forOrco)
1703 {
1704         Curve *cu = ob->data;
1705
1706         /* we do allow duplis... this is only displist on curve level */
1707         if(!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1708
1709         if(ob->type==OB_SURF) {
1710                 makeDispListSurf(scene, ob, dispbase, derivedFinal, forRender, forOrco);
1711         }
1712         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1713                 ListBase dlbev;
1714                 ListBase *nubase;
1715                 float (*originalVerts)[3];
1716                 float (*deformedVerts)[3];
1717                 int numVerts;
1718
1719                 nubase= BKE_curve_nurbs(cu);
1720
1721                 BLI_freelistN(&(cu->bev));
1722
1723                 if(cu->path) free_path(cu->path);
1724                 cu->path= NULL;
1725
1726                 if(ob->type==OB_FONT) BKE_text_to_curve(scene, ob, 0);
1727
1728                 if(!forOrco) curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1729
1730                 makeBevelList(ob);
1731
1732                 /* If curve has no bevel will return nothing */
1733                 makebevelcurve(scene, ob, &dlbev, forRender);
1734
1735                 /* no bevel or extrude, and no width correction? */
1736                 if (!dlbev.first && cu->width==1.0f) {
1737                         curve_to_displist(cu, nubase, dispbase, forRender);
1738                 } else {
1739                         float widfac= cu->width-1.0;
1740                         BevList *bl= cu->bev.first;
1741                         Nurb *nu= nubase->first;
1742
1743                         for (; bl && nu; bl=bl->next,nu=nu->next) {
1744                                 DispList *dl;
1745                                 float *fp1, *data;
1746                                 BevPoint *bevp;
1747                                 int a,b;
1748
1749                                 if (bl->nr) { /* blank bevel lists can happen */
1750
1751                                         /* exception handling; curve without bevel or extrude, with width correction */
1752                                         if(dlbev.first==NULL) {
1753                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev");
1754                                                 dl->verts= MEM_callocN(3*sizeof(float)*bl->nr, "dlverts");
1755                                                 BLI_addtail(dispbase, dl);
1756
1757                                                 if(bl->poly!= -1) dl->type= DL_POLY;
1758                                                 else dl->type= DL_SEGM;
1759
1760                                                 if(dl->type==DL_SEGM) dl->flag = (DL_FRONT_CURVE|DL_BACK_CURVE);
1761
1762                                                 dl->parts= 1;
1763                                                 dl->nr= bl->nr;
1764                                                 dl->col= nu->mat_nr;
1765                                                 dl->charidx= nu->charidx;
1766
1767                                                 /* dl->rt will be used as flag for render face and */
1768                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1769                                                 dl->rt= nu->flag & ~CU_2D;
1770
1771                                                 a= dl->nr;
1772                                                 bevp= (BevPoint *)(bl+1);
1773                                                 data= dl->verts;
1774                                                 while(a--) {
1775                                                         data[0]= bevp->vec[0]+widfac*bevp->sina;
1776                                                         data[1]= bevp->vec[1]+widfac*bevp->cosa;
1777                                                         data[2]= bevp->vec[2];
1778                                                         bevp++;
1779                                                         data+=3;
1780                                                 }
1781                                         }
1782                                         else {
1783                                                 DispList *dlb;
1784
1785                                                 for (dlb=dlbev.first; dlb; dlb=dlb->next) {
1786         
1787                                                         /* for each part of the bevel use a separate displblock */
1788                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1789                                                         dl->verts= data= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1790                                                         BLI_addtail(dispbase, dl);
1791         
1792                                                         dl->type= DL_SURF;
1793                                                         
1794                                                         dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1795                                                         if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1796                                                         if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1797                                                         
1798                                                         dl->parts= bl->nr;
1799                                                         dl->nr= dlb->nr;
1800                                                         dl->col= nu->mat_nr;
1801                                                         dl->charidx= nu->charidx;
1802
1803                                                         /* dl->rt will be used as flag for render face and */
1804                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1805                                                         dl->rt= nu->flag & ~CU_2D;
1806
1807                                                         dl->bevelSplitFlag= MEM_callocN(sizeof(*dl->col2)*((bl->nr+0x1F)>>5), "bevelSplitFlag");
1808         
1809                                                         /* for each point of poly make a bevel piece */
1810                                                         bevp= (BevPoint *)(bl+1);
1811                                                         for(a=0; a<bl->nr; a++,bevp++) {
1812                                                                 float fac=1.0;
1813                                                                 if (cu->taperobj==NULL) {
1814                                                                         if ( (cu->bevobj!=NULL) || !((cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) )
1815                                                                                 fac = bevp->radius;
1816                                                                 } else {
1817                                                                         fac = calc_taper(scene, cu->taperobj, a, bl->nr);
1818                                                                 }
1819
1820                                                                 if (bevp->split_tag) {
1821                                                                         dl->bevelSplitFlag[a>>5] |= 1<<(a&0x1F);
1822                                                                 }
1823         
1824                                                                         /* rotate bevel piece and write in data */
1825                                                                 fp1= dlb->verts;
1826                                                                 for (b=0; b<dlb->nr; b++,fp1+=3,data+=3) {
1827                                                                         if(cu->flag & CU_3D) {
1828                                                                                 float vec[3];
1829         
1830                                                                                 vec[0]= fp1[1]+widfac;
1831                                                                                 vec[1]= fp1[2];
1832                                                                                 vec[2]= 0.0;
1833
1834                                                                                 mul_qt_v3(bevp->quat, vec);
1835
1836                                                                                 data[0]= bevp->vec[0] + fac*vec[0];
1837                                                                                 data[1]= bevp->vec[1] + fac*vec[1];
1838                                                                                 data[2]= bevp->vec[2] + fac*vec[2];
1839                                                                         }
1840                                                                         else {
1841                                                                                 data[0]= bevp->vec[0] + fac*(widfac+fp1[1])*bevp->sina;
1842                                                                                 data[1]= bevp->vec[1] + fac*(widfac+fp1[1])*bevp->cosa;
1843                                                                                 data[2]= bevp->vec[2] + fac*fp1[2];
1844                                                                         }
1845                                                                 }
1846                                                         }
1847                                                         
1848                                                         /* gl array drawing: using indices */
1849                                                         displist_surf_indices(dl);
1850                                                 }
1851                                         }
1852                                 }
1853
1854                         }
1855                         freedisplist(&dlbev);
1856                 }
1857
1858                 if (!(cu->flag & CU_DEFORM_FILL)) {
1859                         curve_to_filledpoly(cu, nubase, dispbase);
1860                 }
1861
1862                 if(cu->flag & CU_PATH) calc_curvepath(ob);
1863
1864                 /* make copy of 'undeformed" displist for texture space calculation
1865                    actually, it's not totally undeformed -- pre-tesselation modifiers are
1866                    already applied, thats how it worked for years, so keep for compatibility (sergey) */
1867                 copy_displist(&cu->disp, dispbase);
1868
1869                  if (!forRender) {
1870                          tex_space_curve(cu);
1871                  }
1872
1873                 if(!forOrco) curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal, forRender, originalVerts, deformedVerts);
1874
1875                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1876                         curve_to_filledpoly(cu, nubase, dispbase);
1877                 }
1878         }
1879 }
1880
1881 void makeDispListCurveTypes(Scene *scene, Object *ob, int forOrco)
1882 {
1883         Curve *cu= ob->data;
1884         ListBase *dispbase;
1885
1886         freedisplist(&(ob->disp));
1887         dispbase= &(ob->disp);
1888         freedisplist(dispbase);
1889
1890         /* free displist used for textspace */
1891         freedisplist(&cu->disp);
1892
1893         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco);
1894
1895         if (ob->derivedFinal) {
1896                 DM_set_object_boundbox (ob, ob->derivedFinal);
1897         } else {
1898                 boundbox_displist (ob);
1899
1900                 /* if there is no derivedMesh, object's boundbox is unneeded */
1901                 if (ob->bb) {
1902                         MEM_freeN(ob->bb);
1903                         ob->bb= NULL;
1904                 }
1905         }
1906 }
1907
1908 void makeDispListCurveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1909         DerivedMesh **derivedFinal, int forOrco)
1910 {
1911         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco);
1912 }
1913
1914 void makeDispListCurveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1915 {
1916         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1);
1917 }
1918
1919 /* add Orco layer to the displist object which has got derived mesh and return orco */
1920 float *makeOrcoDispList(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender) {
1921         float *orco;
1922
1923         if (derivedFinal == NULL)
1924                 derivedFinal= ob->derivedFinal;
1925
1926         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1927                 curve_calc_orcodm(scene, ob, derivedFinal, forRender);
1928         }
1929
1930         orco= derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1931
1932         if(orco) {
1933                 orco= MEM_dupallocN(orco);
1934         }
1935
1936         return orco;
1937 }
1938
1939 void imagestodisplist(void)
1940 {
1941         /* removed */
1942 }
1943
1944 /* this is confusing, there's also min_max_object, appplying the obmat... */
1945 static void boundbox_displist(Object *ob)
1946 {
1947         BoundBox *bb=0;
1948         float min[3], max[3];
1949         DispList *dl;
1950         float *vert;
1951         int a, tot=0;
1952         
1953         INIT_MINMAX(min, max);
1954
1955         if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1956                 Curve *cu= ob->data;
1957                 int doit= 0;
1958
1959                 if(cu->bb==0) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
1960                 bb= cu->bb;
1961                 
1962                 dl= ob->disp.first;
1963
1964                 while (dl) {
1965                         if(dl->type==DL_INDEX3) tot= dl->nr;
1966                         else tot= dl->nr*dl->parts;
1967                         
1968                         vert= dl->verts;
1969                         for(a=0; a<tot; a++, vert+=3) {
1970                                 doit= 1;
1971                                 DO_MINMAX(vert, min, max);
1972                         }
1973
1974                         dl= dl->next;
1975                 }
1976                 
1977                 if(!doit) {
1978                         min[0] = min[1] = min[2] = -1.0f;
1979                         max[0] = max[1] = max[2] = 1.0f;
1980                 }
1981                 
1982         }
1983         
1984         if(bb) {
1985                 boundbox_set_from_min_max(bb, min, max);
1986         }
1987 }
1988