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