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