remove redundant assignments & unused vars.
[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                 if (!modifier_isEnabled(scene, md, required_mode)) continue;
1224
1225                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
1226                         preTesselatePoint  = md;
1227                 }
1228         }
1229
1230         return preTesselatePoint;
1231 }
1232
1233 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, int forRender, float (**originalVerts_r)[3], float (**deformedVerts_r)[3], int *numVerts_r)
1234 {
1235         ModifierData *md = modifiers_getVirtualModifierList(ob);
1236         ModifierData *preTesselatePoint;
1237         Curve *cu= ob->data;
1238         ListBase *nurb= BKE_curve_nurbs(cu);
1239         int numVerts = 0;
1240         int editmode = (!forRender && cu->editnurb);
1241         float (*originalVerts)[3] = NULL;
1242         float (*deformedVerts)[3] = NULL;
1243         float *keyVerts= NULL;
1244         int required_mode;
1245
1246         if(forRender) required_mode = eModifierMode_Render;
1247         else required_mode = eModifierMode_Realtime;
1248
1249         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1250         
1251         if(editmode) required_mode |= eModifierMode_Editmode;
1252
1253         if(cu->editnurb==NULL) {
1254                 keyVerts= do_ob_key(scene, ob);
1255
1256                 if(keyVerts) {
1257                         /* split coords from key data, the latter also includes
1258                            tilts, which is passed through in the modifier stack.
1259                            this is also the reason curves do not use a virtual
1260                            shape key modifier yet. */
1261                         deformedVerts= curve_getKeyVertexCos(cu, nurb, keyVerts);
1262                         originalVerts= MEM_dupallocN(deformedVerts);
1263                 }
1264         }
1265         
1266         if (preTesselatePoint) {
1267                 for (; md; md=md->next) {
1268                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1269
1270                         md->scene= scene;
1271                         
1272                         if ((md->mode & required_mode) != required_mode) continue;
1273                         if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1274                         if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1275
1276                         if (!deformedVerts) {
1277                                 deformedVerts = curve_getVertexCos(cu, nurb, &numVerts);
1278                                 originalVerts = MEM_dupallocN(deformedVerts);
1279                         }
1280
1281                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, forRender, editmode);
1282
1283                         if (md==preTesselatePoint)
1284                                 break;
1285                 }
1286         }
1287
1288         if (deformedVerts)
1289                 curve_applyVertexCos(cu, nurb, deformedVerts);
1290         if (keyVerts) /* these are not passed through modifier stack */
1291                 curve_applyKeyVertexTilts(cu, nurb, keyVerts);
1292
1293         if(keyVerts)
1294                 MEM_freeN(keyVerts);
1295
1296         *originalVerts_r = originalVerts;
1297         *deformedVerts_r = deformedVerts;
1298         *numVerts_r = numVerts;
1299 }
1300
1301 static float (*displist_get_allverts (ListBase *dispbase, int *totvert))[3]
1302 {
1303         DispList *dl;
1304         float (*allverts)[3], *fp;
1305
1306         *totvert= 0;
1307
1308         for (dl=dispbase->first; dl; dl=dl->next)
1309                 *totvert+= (dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr;
1310
1311         allverts= MEM_mallocN((*totvert)*sizeof(float)*3, "displist_get_allverts allverts");
1312         fp= (float*)allverts;
1313         for (dl=dispbase->first; dl; dl=dl->next) {
1314                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1315                 memcpy(fp, dl->verts, sizeof(float) * offs);
1316                 fp+= offs;
1317         }
1318
1319         return allverts;
1320 }
1321
1322 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
1323 {
1324         DispList *dl;
1325         float *fp;
1326
1327         fp= (float*)allverts;
1328         for (dl=dispbase->first; dl; dl=dl->next) {
1329                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1330                 memcpy(dl->verts, fp, sizeof(float) * offs);
1331                 fp+= offs;
1332         }
1333 }
1334
1335 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispbase,
1336         DerivedMesh **derivedFinal, int forRender, float (*originalVerts)[3], float (*deformedVerts)[3])
1337 {
1338         ModifierData *md = modifiers_getVirtualModifierList(ob);
1339         ModifierData *preTesselatePoint;
1340         Curve *cu= ob->data;
1341         ListBase *nurb= BKE_curve_nurbs(cu);
1342         int required_mode = 0, totvert = 0;
1343         int editmode = (!forRender && cu->editnurb);
1344         DerivedMesh *dm= NULL, *ndm;
1345         float (*vertCos)[3] = NULL;
1346
1347         if(forRender) required_mode = eModifierMode_Render;
1348         else required_mode = eModifierMode_Realtime;
1349
1350         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1351         
1352         if(editmode) required_mode |= eModifierMode_Editmode;
1353
1354         if (preTesselatePoint) {
1355                 md = preTesselatePoint->next;
1356         }
1357
1358         if (derivedFinal && *derivedFinal) {
1359                 (*derivedFinal)->release (*derivedFinal);
1360         }
1361
1362         for (; md; md=md->next) {
1363                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1364
1365                 md->scene= scene;
1366
1367                 if ((md->mode & required_mode) != required_mode) continue;
1368                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1369
1370                 if (mti->type == eModifierTypeType_OnlyDeform ||
1371                                 (mti->type == eModifierTypeType_DeformOrConstruct && !dm)) {
1372                         if (dm) {
1373                                 if (!vertCos) {
1374                                         totvert = dm->getNumVerts(dm);
1375                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
1376                                         dm->getVertCos(dm, vertCos);
1377                                 }
1378
1379                                 mti->deformVerts(md, ob, dm, vertCos, totvert, forRender, editmode);
1380                         } else {
1381                                 if (!vertCos) {
1382                                         vertCos= displist_get_allverts(dispbase, &totvert);
1383                                 }
1384
1385                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, forRender, editmode);
1386                         }
1387                 } else {
1388                         if (!derivedFinal) {
1389                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh */
1390                                 /* is totally unnecessary, so we could stop modifiers applying */
1391                                 /* when we found constructive modifier but derived mesh is unwanted result */
1392                                 break;
1393                         }
1394
1395                         if (dm) {
1396                                 if (vertCos) {
1397                                         DerivedMesh *tdm = CDDM_copy(dm);
1398                                         dm->release(dm);
1399                                         dm = tdm;
1400
1401                                         CDDM_apply_vert_coords(dm, vertCos);
1402                                         CDDM_calc_normals(dm);
1403                                 }
1404                         } else {
1405                                 if (vertCos) {
1406                                         displist_apply_allverts(dispbase, vertCos);
1407                                 }
1408
1409                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
1410                                         curve_to_filledpoly(cu, nurb, dispbase);
1411                                 }
1412
1413                                 dm= CDDM_from_curve_customDB(ob, dispbase);
1414
1415                                 CDDM_calc_normals(dm);
1416                         }
1417
1418                         if (vertCos) {
1419                                 /* Vertex coordinates were applied to necessary data, could free it */
1420                                 MEM_freeN(vertCos);
1421                                 vertCos= NULL;
1422                         }
1423
1424                         ndm = mti->applyModifier(md, ob, dm, forRender, editmode);
1425
1426                         if (ndm) {
1427                                 /* Modifier returned a new derived mesh */
1428
1429                                 if (dm && dm != ndm) /* Modifier  */
1430                                         dm->release (dm);
1431                                 dm = ndm;
1432                         }
1433                 }
1434         }
1435
1436         if (vertCos) {
1437                 if (dm) {
1438                         DerivedMesh *tdm = CDDM_copy(dm);
1439                         dm->release(dm);
1440                         dm = tdm;
1441
1442                         CDDM_apply_vert_coords(dm, vertCos);
1443                         CDDM_calc_normals(dm);
1444                         MEM_freeN(vertCos);
1445                 } else {
1446                         displist_apply_allverts(dispbase, vertCos);
1447                         MEM_freeN(vertCos);
1448                         vertCos= NULL;
1449                 }
1450         }
1451
1452         if (derivedFinal) {
1453                 (*derivedFinal) = dm;
1454         }
1455
1456         if (deformedVerts) {
1457                 curve_applyVertexCos(ob->data, nurb, originalVerts);
1458                 MEM_freeN(originalVerts);
1459                 MEM_freeN(deformedVerts);
1460         }
1461 }
1462
1463 static void displist_surf_indices(DispList *dl)
1464 {
1465         int a, b, p1, p2, p3, p4;
1466         int *index;
1467         
1468         dl->totindex= 0;
1469         
1470         index=dl->index= MEM_mallocN( 4*sizeof(int)*(dl->parts+1)*(dl->nr+1), "index array nurbs");
1471         
1472         for(a=0; a<dl->parts; a++) {
1473                 
1474                 if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
1475                         break;
1476                 
1477                 for(; b<dl->nr; b++, index+=4) {        
1478                         index[0]= p1;
1479                         index[1]= p2;
1480                         index[2]= p4;
1481                         index[3]= p3;
1482                         
1483                         dl->totindex++;
1484                         
1485                         p2= p1; p1++;
1486                         p4= p3; p3++;
1487
1488                 }
1489         }
1490         
1491 }
1492
1493 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
1494 {
1495         DerivedMesh *dm;
1496         ListBase disp= {NULL, NULL};
1497
1498         /* OrcoDM should be created from underformed disp lists */
1499         makeDispListCurveTypes_forOrco(scene, ob, &disp);
1500         dm= CDDM_from_curve_customDB(ob, &disp);
1501
1502         freedisplist(&disp);
1503
1504         return dm;
1505 }
1506
1507 static void add_orco_dm(Scene *scene, Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1508 {
1509         float (*orco)[3], (*layerorco)[3];
1510         int totvert, a;
1511         Curve *cu= ob->data;
1512
1513         totvert= dm->getNumVerts(dm);
1514
1515         if(orcodm) {
1516                 orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
1517
1518                 if(orcodm->getNumVerts(orcodm) == totvert)
1519                         orcodm->getVertCos(orcodm, orco);
1520                 else
1521                         dm->getVertCos(dm, orco);
1522         }
1523         else {
1524                 orco= (float(*)[3])make_orco_curve(scene, ob);
1525         }
1526
1527         for(a=0; a<totvert; a++) {
1528                 float *co = orco[a];
1529                 co[0] = (co[0]-cu->loc[0])/cu->size[0];
1530                 co[1] = (co[1]-cu->loc[1])/cu->size[1];
1531                 co[2] = (co[2]-cu->loc[2])/cu->size[2];
1532         }
1533
1534         if((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1535                 memcpy(layerorco, orco, sizeof(float)*totvert);
1536                 MEM_freeN(orco);
1537         }
1538         else
1539                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1540 }
1541
1542 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1543 {
1544         /* this function represents logic of mesh's orcodm calculation */
1545         /* for displist-based objects */
1546
1547         ModifierData *md = modifiers_getVirtualModifierList(ob);
1548         ModifierData *preTesselatePoint;
1549         Curve *cu= ob->data;
1550         int required_mode;
1551         int editmode = (!forRender && cu->editnurb);
1552         DerivedMesh *ndm, *orcodm= NULL;
1553
1554         if(forRender) required_mode = eModifierMode_Render;
1555         else required_mode = eModifierMode_Realtime;
1556
1557         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1558
1559         if(editmode) required_mode |= eModifierMode_Editmode;
1560
1561         if (preTesselatePoint) {
1562                 md = preTesselatePoint->next;
1563         }
1564
1565         for (; md; md=md->next) {
1566                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1567
1568                 md->scene= scene;
1569
1570                 if ((md->mode & required_mode) != required_mode) continue;
1571                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1572                 if (mti->type!=eModifierTypeType_Constructive) continue;
1573
1574                 if(!orcodm)
1575                         orcodm= create_orco_dm(scene, ob);
1576
1577                 ndm = mti->applyModifier(md, ob, orcodm, forRender, 0);
1578
1579                 if(ndm) {
1580                         /* if the modifier returned a new dm, release the old one */
1581                         if(orcodm && orcodm != ndm) {
1582                                 orcodm->release(orcodm);
1583                         }
1584                         orcodm = ndm;
1585                 }
1586         }
1587
1588         /* add an orco layer if needed */
1589         add_orco_dm(scene, ob, derivedFinal, orcodm);
1590
1591         if(orcodm)
1592                 orcodm->release(orcodm);
1593 }
1594
1595 void makeDispListSurf(Scene *scene, Object *ob, ListBase *dispbase,
1596         DerivedMesh **derivedFinal, int forRender, int forOrco)
1597 {
1598         ListBase *nubase;
1599         Nurb *nu;
1600         Curve *cu = ob->data;
1601         DispList *dl;
1602         float *data;
1603         int len;
1604         int numVerts;
1605         float (*originalVerts)[3];
1606         float (*deformedVerts)[3];
1607
1608         if(!forRender && cu->editnurb)
1609                 nubase= ED_curve_editnurbs(cu);
1610         else
1611                 nubase= &cu->nurb;
1612
1613         if(!forOrco)
1614                 curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1615
1616         for (nu=nubase->first; nu; nu=nu->next) {
1617                 if(forRender || nu->hide==0) {
1618                         int resolu= nu->resolu, resolv= nu->resolv;
1619
1620                         if(forRender){
1621                                 if(cu->resolu_ren) resolu= cu->resolu_ren;
1622                                 if(cu->resolv_ren) resolv= cu->resolv_ren;
1623                         }
1624
1625                         if(nu->pntsv==1) {
1626                                 len= SEGMENTSU(nu)*resolu;
1627
1628                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1629                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1630
1631                                 BLI_addtail(dispbase, dl);
1632                                 dl->parts= 1;
1633                                 dl->nr= len;
1634                                 dl->col= nu->mat_nr;
1635                                 dl->charidx= nu->charidx;
1636
1637                                 /* dl->rt will be used as flag for render face and */
1638                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1639                                 dl->rt= nu->flag & ~CU_2D;
1640
1641                                 data= dl->verts;
1642                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
1643                                 else dl->type= DL_SEGM;
1644
1645                                 makeNurbcurve(nu, data, NULL, NULL, NULL, resolu, 3*sizeof(float));
1646                         }
1647                         else {
1648                                 len= (nu->pntsu*resolu) * (nu->pntsv*resolv);
1649                                 
1650                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1651                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1652                                 BLI_addtail(dispbase, dl);
1653
1654                                 dl->col= nu->mat_nr;
1655                                 dl->charidx= nu->charidx;
1656
1657                                 /* dl->rt will be used as flag for render face and */
1658                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1659                                 dl->rt= nu->flag & ~CU_2D;
1660
1661                                 data= dl->verts;
1662                                 dl->type= DL_SURF;
1663
1664                                 dl->parts= (nu->pntsu*resolu);  /* in reverse, because makeNurbfaces works that way */
1665                                 dl->nr= (nu->pntsv*resolv);
1666                                 if(nu->flagv & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_U;    /* reverse too! */
1667                                 if(nu->flagu & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_V;
1668
1669                                 makeNurbfaces(nu, data, 0, resolu, resolv);
1670                                 
1671                                 /* gl array drawing: using indices */
1672                                 displist_surf_indices(dl);
1673                         }
1674                 }
1675         }
1676
1677         /* make copy of 'undeformed" displist for texture space calculation
1678            actually, it's not totally undeformed -- pre-tesselation modifiers are
1679            already applied, thats how it worked for years, so keep for compatibility (sergey) */
1680         copy_displist(&cu->disp, dispbase);
1681
1682         if (!forRender) {
1683                 tex_space_curve(cu);
1684         }
1685
1686         if(!forOrco)
1687                 curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal,
1688                         forRender, originalVerts, deformedVerts);
1689 }
1690
1691 static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1692         DerivedMesh **derivedFinal, int forRender, int forOrco)
1693 {
1694         Curve *cu = ob->data;
1695
1696         /* we do allow duplis... this is only displist on curve level */
1697         if(!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1698
1699         if(ob->type==OB_SURF) {
1700                 makeDispListSurf(scene, ob, dispbase, derivedFinal, forRender, forOrco);
1701         }
1702         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1703                 ListBase dlbev;
1704                 ListBase *nubase;
1705                 float (*originalVerts)[3];
1706                 float (*deformedVerts)[3];
1707                 int numVerts;
1708
1709                 nubase= BKE_curve_nurbs(cu);
1710
1711                 BLI_freelistN(&(cu->bev));
1712
1713                 if(cu->path) free_path(cu->path);
1714                 cu->path= NULL;
1715
1716                 if(ob->type==OB_FONT) BKE_text_to_curve(scene, ob, 0);
1717
1718                 if(!forOrco) curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1719
1720                 makeBevelList(ob);
1721
1722                 /* If curve has no bevel will return nothing */
1723                 makebevelcurve(scene, ob, &dlbev, forRender);
1724
1725                 /* no bevel or extrude, and no width correction? */
1726                 if (!dlbev.first && cu->width==1.0f) {
1727                         curve_to_displist(cu, nubase, dispbase, forRender);
1728                 } else {
1729                         float widfac= cu->width-1.0;
1730                         BevList *bl= cu->bev.first;
1731                         Nurb *nu= nubase->first;
1732
1733                         for (; bl && nu; bl=bl->next,nu=nu->next) {
1734                                 DispList *dl;
1735                                 float *fp1, *data;
1736                                 BevPoint *bevp;
1737                                 int a,b;
1738
1739                                 if (bl->nr) { /* blank bevel lists can happen */
1740
1741                                         /* exception handling; curve without bevel or extrude, with width correction */
1742                                         if(dlbev.first==NULL) {
1743                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev");
1744                                                 dl->verts= MEM_callocN(3*sizeof(float)*bl->nr, "dlverts");
1745                                                 BLI_addtail(dispbase, dl);
1746
1747                                                 if(bl->poly!= -1) dl->type= DL_POLY;
1748                                                 else dl->type= DL_SEGM;
1749
1750                                                 if(dl->type==DL_SEGM) dl->flag = (DL_FRONT_CURVE|DL_BACK_CURVE);
1751
1752                                                 dl->parts= 1;
1753                                                 dl->nr= bl->nr;
1754                                                 dl->col= nu->mat_nr;
1755                                                 dl->charidx= nu->charidx;
1756
1757                                                 /* dl->rt will be used as flag for render face and */
1758                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1759                                                 dl->rt= nu->flag & ~CU_2D;
1760
1761                                                 a= dl->nr;
1762                                                 bevp= (BevPoint *)(bl+1);
1763                                                 data= dl->verts;
1764                                                 while(a--) {
1765                                                         data[0]= bevp->vec[0]+widfac*bevp->sina;
1766                                                         data[1]= bevp->vec[1]+widfac*bevp->cosa;
1767                                                         data[2]= bevp->vec[2];
1768                                                         bevp++;
1769                                                         data+=3;
1770                                                 }
1771                                         }
1772                                         else {
1773                                                 DispList *dlb;
1774
1775                                                 for (dlb=dlbev.first; dlb; dlb=dlb->next) {
1776         
1777                                                         /* for each part of the bevel use a separate displblock */
1778                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1779                                                         dl->verts= data= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1780                                                         BLI_addtail(dispbase, dl);
1781         
1782                                                         dl->type= DL_SURF;
1783                                                         
1784                                                         dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1785                                                         if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1786                                                         if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1787                                                         
1788                                                         dl->parts= bl->nr;
1789                                                         dl->nr= dlb->nr;
1790                                                         dl->col= nu->mat_nr;
1791                                                         dl->charidx= nu->charidx;
1792
1793                                                         /* dl->rt will be used as flag for render face and */
1794                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1795                                                         dl->rt= nu->flag & ~CU_2D;
1796
1797                                                         dl->bevelSplitFlag= MEM_callocN(sizeof(*dl->col2)*((bl->nr+0x1F)>>5), "bevelSplitFlag");
1798         
1799                                                         /* for each point of poly make a bevel piece */
1800                                                         bevp= (BevPoint *)(bl+1);
1801                                                         for(a=0; a<bl->nr; a++,bevp++) {
1802                                                                 float fac=1.0;
1803                                                                 if (cu->taperobj==NULL) {
1804                                                                         if ( (cu->bevobj!=NULL) || !((cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) )
1805                                                                                 fac = bevp->radius;
1806                                                                 } else {
1807                                                                         fac = calc_taper(scene, cu->taperobj, a, bl->nr);
1808                                                                 }
1809
1810                                                                 if (bevp->split_tag) {
1811                                                                         dl->bevelSplitFlag[a>>5] |= 1<<(a&0x1F);
1812                                                                 }
1813         
1814                                                                         /* rotate bevel piece and write in data */
1815                                                                 fp1= dlb->verts;
1816                                                                 for (b=0; b<dlb->nr; b++,fp1+=3,data+=3) {
1817                                                                         if(cu->flag & CU_3D) {
1818                                                                                 float vec[3];
1819         
1820                                                                                 vec[0]= fp1[1]+widfac;
1821                                                                                 vec[1]= fp1[2];
1822                                                                                 vec[2]= 0.0;
1823
1824                                                                                 mul_qt_v3(bevp->quat, vec);
1825
1826                                                                                 data[0]= bevp->vec[0] + fac*vec[0];
1827                                                                                 data[1]= bevp->vec[1] + fac*vec[1];
1828                                                                                 data[2]= bevp->vec[2] + fac*vec[2];
1829                                                                         }
1830                                                                         else {
1831                                                                                 data[0]= bevp->vec[0] + fac*(widfac+fp1[1])*bevp->sina;
1832                                                                                 data[1]= bevp->vec[1] + fac*(widfac+fp1[1])*bevp->cosa;
1833                                                                                 data[2]= bevp->vec[2] + fac*fp1[2];
1834                                                                         }
1835                                                                 }
1836                                                         }
1837                                                         
1838                                                         /* gl array drawing: using indices */
1839                                                         displist_surf_indices(dl);
1840                                                 }
1841                                         }
1842                                 }
1843
1844                         }
1845                         freedisplist(&dlbev);
1846                 }
1847
1848                 if (!(cu->flag & CU_DEFORM_FILL)) {
1849                         curve_to_filledpoly(cu, nubase, dispbase);
1850                 }
1851
1852                 if(cu->flag & CU_PATH) calc_curvepath(ob);
1853
1854                 /* make copy of 'undeformed" displist for texture space calculation
1855                    actually, it's not totally undeformed -- pre-tesselation modifiers are
1856                    already applied, thats how it worked for years, so keep for compatibility (sergey) */
1857                 copy_displist(&cu->disp, dispbase);
1858
1859                  if (!forRender) {
1860                          tex_space_curve(cu);
1861                  }
1862
1863                 if(!forOrco) curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal, forRender, originalVerts, deformedVerts);
1864
1865                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1866                         curve_to_filledpoly(cu, nubase, dispbase);
1867                 }
1868         }
1869 }
1870
1871 void makeDispListCurveTypes(Scene *scene, Object *ob, int forOrco)
1872 {
1873         Curve *cu= ob->data;
1874         ListBase *dispbase;
1875
1876         freedisplist(&(ob->disp));
1877         dispbase= &(ob->disp);
1878         freedisplist(dispbase);
1879
1880         /* free displist used for textspace */
1881         freedisplist(&cu->disp);
1882
1883         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco);
1884
1885         if (ob->derivedFinal) {
1886                 DM_set_object_boundbox (ob, ob->derivedFinal);
1887         } else {
1888                 boundbox_displist (ob);
1889
1890                 /* if there is no derivedMesh, object's boundbox is unneeded */
1891                 if (ob->bb) {
1892                         MEM_freeN(ob->bb);
1893                         ob->bb= NULL;
1894                 }
1895         }
1896 }
1897
1898 void makeDispListCurveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1899         DerivedMesh **derivedFinal, int forOrco)
1900 {
1901         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco);
1902 }
1903
1904 void makeDispListCurveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1905 {
1906         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1);
1907 }
1908
1909 /* add Orco layer to the displist object which has got derived mesh and return orco */
1910 float *makeOrcoDispList(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender) {
1911         float *orco;
1912
1913         if (derivedFinal == NULL)
1914                 derivedFinal= ob->derivedFinal;
1915
1916         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1917                 curve_calc_orcodm(scene, ob, derivedFinal, forRender);
1918         }
1919
1920         orco= derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1921
1922         if(orco) {
1923                 orco= MEM_dupallocN(orco);
1924         }
1925
1926         return orco;
1927 }
1928
1929 void imagestodisplist(void)
1930 {
1931         /* removed */
1932 }
1933
1934 /* this is confusing, there's also min_max_object, appplying the obmat... */
1935 static void boundbox_displist(Object *ob)
1936 {
1937         BoundBox *bb=0;
1938         float min[3], max[3];
1939         DispList *dl;
1940         float *vert;
1941         int a, tot=0;
1942         
1943         INIT_MINMAX(min, max);
1944
1945         if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1946                 Curve *cu= ob->data;
1947                 int doit= 0;
1948
1949                 if(cu->bb==0) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
1950                 bb= cu->bb;
1951                 
1952                 dl= ob->disp.first;
1953
1954                 while (dl) {
1955                         if(dl->type==DL_INDEX3) tot= dl->nr;
1956                         else tot= dl->nr*dl->parts;
1957                         
1958                         vert= dl->verts;
1959                         for(a=0; a<tot; a++, vert+=3) {
1960                                 doit= 1;
1961                                 DO_MINMAX(vert, min, max);
1962                         }
1963
1964                         dl= dl->next;
1965                 }
1966                 
1967                 if(!doit) {
1968                         min[0] = min[1] = min[2] = -1.0f;
1969                         max[0] = max[1] = max[2] = 1.0f;
1970                 }
1971                 
1972         }
1973         
1974         if(bb) {
1975                 boundbox_set_from_min_max(bb, min, max);
1976         }
1977 }
1978