header cleanup (no functional changes)
[blender.git] / source / blender / blenkernel / intern / displist.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/blenkernel/intern/displist.c
31  *  \ingroup bke
32  */
33
34
35 #include <math.h>
36 #include <stdio.h>
37 #include <string.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_curve_types.h"
42 #include "DNA_meshdata_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_material_types.h"
46
47 #include "BLI_blenlib.h"
48 #include "BLI_math.h"
49 #include "BLI_editVert.h"
50 #include "BLI_scanfill.h"
51 #include "BLI_utildefines.h"
52
53 #include "BKE_global.h"
54 #include "BKE_displist.h"
55 #include "BKE_cdderivedmesh.h"
56 #include "BKE_object.h"
57 #include "BKE_mball.h"
58 #include "BKE_material.h"
59 #include "BKE_curve.h"
60 #include "BKE_key.h"
61 #include "BKE_anim.h"
62 #include "BKE_font.h"
63 #include "BKE_lattice.h"
64 #include "BKE_modifier.h"
65
66 #include "BLO_sys_types.h" // for intptr_t support
67
68 extern Material defmaterial;    /* material.c */
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 NULL;
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.0f) dl->nors[2]= -1.0f;
177                                 else dl->nors[2]= 1.0f;
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 /* ****************** make displists ********************* */
283
284 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase, int forRender)
285 {
286         Nurb *nu;
287         DispList *dl;
288         BezTriple *bezt, *prevbezt;
289         BPoint *bp;
290         float *data;
291         int a, len, resolu;
292         
293         nu= nubase->first;
294         while(nu) {
295                 if(nu->hide==0) {
296                         
297                         if(forRender && cu->resolu_ren!=0)
298                                 resolu= cu->resolu_ren;
299                         else
300                                 resolu= nu->resolu;
301                         
302                         if(!check_valid_nurb_u(nu));
303                         else if(nu->type == CU_BEZIER) {
304                                 
305                                 /* count */
306                                 len= 0;
307                                 a= nu->pntsu-1;
308                                 if(nu->flagu & CU_NURB_CYCLIC) a++;
309
310                                 prevbezt= nu->bezt;
311                                 bezt= prevbezt+1;
312                                 while(a--) {
313                                         if(a==0 && (nu->flagu & CU_NURB_CYCLIC)) bezt= nu->bezt;
314                                         
315                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) len++;
316                                         else len+= resolu;
317                                         
318                                         if(a==0 && (nu->flagu & CU_NURB_CYCLIC)==0) len++;
319                                         
320                                         prevbezt= bezt;
321                                         bezt++;
322                                 }
323                                 
324                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbez");
325                                 /* len+1 because of 'forward_diff_bezier' function */
326                                 dl->verts= MEM_callocN( (len+1)*3*sizeof(float), "dlverts");
327                                 BLI_addtail(dispbase, dl);
328                                 dl->parts= 1;
329                                 dl->nr= len;
330                                 dl->col= nu->mat_nr;
331                                 dl->charidx= nu->charidx;
332
333                                 data= dl->verts;
334
335                                 if(nu->flagu & CU_NURB_CYCLIC) {
336                                         dl->type= DL_POLY;
337                                         a= nu->pntsu;
338                                 }
339                                 else {
340                                         dl->type= DL_SEGM;
341                                         a= nu->pntsu-1;
342                                 }
343                                 
344                                 prevbezt= nu->bezt;
345                                 bezt= prevbezt+1;
346                                 
347                                 while(a--) {
348                                         if(a==0 && dl->type== DL_POLY) bezt= nu->bezt;
349                                         
350                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
351                                                 VECCOPY(data, prevbezt->vec[1]);
352                                                 data+= 3;
353                                         }
354                                         else {
355                                                 int j;
356                                                 for(j=0; j<3; j++) {
357                                                         forward_diff_bezier(    prevbezt->vec[1][j],
358                                                                                                         prevbezt->vec[2][j],
359                                                                                                         bezt->vec[0][j],
360                                                                                                         bezt->vec[1][j],
361                                                                                                         data+j, resolu, 3*sizeof(float));
362                                                 }
363                                                 
364                                                 data+= 3*resolu;
365                                         }
366                                         
367                                         if(a==0 && dl->type==DL_SEGM) {
368                                                 VECCOPY(data, bezt->vec[1]);
369                                         }
370                                         
371                                         prevbezt= bezt;
372                                         bezt++;
373                                 }
374                         }
375                         else if(nu->type == CU_NURBS) {
376                                 len= (resolu*SEGMENTSU(nu));
377                                 
378                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
379                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
380                                 BLI_addtail(dispbase, dl);
381                                 dl->parts= 1;
382                                 
383                                 dl->nr= len;
384                                 dl->col= nu->mat_nr;
385                                 dl->charidx = nu->charidx;
386
387                                 data= dl->verts;
388                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
389                                 else dl->type= DL_SEGM;
390                                 makeNurbcurve(nu, data, NULL, NULL, NULL, resolu, 3*sizeof(float));
391                         }
392                         else if(nu->type == CU_POLY) {
393                                 len= nu->pntsu;
394                                 dl= MEM_callocN(sizeof(DispList), "makeDispListpoly");
395                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
396                                 BLI_addtail(dispbase, dl);
397                                 dl->parts= 1;
398                                 dl->nr= len;
399                                 dl->col= nu->mat_nr;
400                                 dl->charidx = nu->charidx;
401
402                                 data= dl->verts;
403                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
404                                 else dl->type= DL_SEGM;
405                                 
406                                 a= len;
407                                 bp= nu->bp;
408                                 while(a--) {
409                                         VECCOPY(data, bp->vec);
410                                         bp++;
411                                         data+= 3;
412                                 }
413                         }
414                 }
415                 nu= nu->next;
416         }
417 }
418
419
420 void filldisplist(ListBase *dispbase, ListBase *to, int flipnormal)
421 {
422         EditVert *eve, *v1, *vlast;
423         EditFace *efa;
424         DispList *dlnew=NULL, *dl;
425         float *f1;
426         int colnr=0, charidx=0, cont=1, tot, a, *index, nextcol= 0;
427         intptr_t totvert;
428         
429         if(dispbase==NULL) return;
430         if(dispbase->first==NULL) return;
431
432         while(cont) {
433                 cont= 0;
434                 totvert= 0;
435                 nextcol= 0;
436                 
437                 dl= dispbase->first;
438                 while(dl) {
439         
440                         if(dl->type==DL_POLY) {
441                                 if(charidx<dl->charidx) cont= 1;
442                                 else if(charidx==dl->charidx) { /* character with needed index */
443                                         if(colnr==dl->col) {
444                                                 /* make editverts and edges */
445                                                 f1= dl->verts;
446                                                 a= dl->nr;
447                                                 eve= v1= NULL;
448                                                 
449                                                 while(a--) {
450                                                         vlast= eve;
451
452                                                         eve= BLI_addfillvert(f1);
453                                                         totvert++;
454
455                                                         if(vlast==NULL) v1= eve;
456                                                         else {
457                                                                 BLI_addfilledge(vlast, eve);
458                                                         }
459                                                         f1+=3;
460                                                 }
461
462                                                 if(eve!=NULL && v1!=NULL) {
463                                                         BLI_addfilledge(eve, v1);
464                                                 }
465                                         } else if (colnr<dl->col) {
466                                                 /* got poly with next material at current char */
467                                                 cont= 1;
468                                                 nextcol= 1;
469                                         }
470                                 }
471                         }
472                         dl= dl->next;
473                 }
474                 
475                 if(totvert && (tot= BLI_edgefill(0))) { // XXX (obedit && obedit->actcol)?(obedit->actcol-1):0)) {
476                         if(tot) {
477                                 dlnew= MEM_callocN(sizeof(DispList), "filldisplist");
478                                 dlnew->type= DL_INDEX3;
479                                 dlnew->col= colnr;
480                                 dlnew->nr= totvert;
481                                 dlnew->parts= tot;
482
483                                 dlnew->index= MEM_mallocN(tot*3*sizeof(int), "dlindex");
484                                 dlnew->verts= MEM_mallocN(totvert*3*sizeof(float), "dlverts");
485                                 
486                                 /* vert data */
487                                 f1= dlnew->verts;
488                                 totvert= 0;
489                                 eve= fillvertbase.first;
490                                 while(eve) {
491                                         VECCOPY(f1, eve->co);
492                                         f1+= 3;
493         
494                                         /* index number */
495                                         eve->tmp.l = totvert;
496                                         totvert++;
497                                         
498                                         eve= eve->next;
499                                 }
500                                 
501                                 /* index data */
502                                 efa= fillfacebase.first;
503                                 index= dlnew->index;
504                                 while(efa) {
505                                         index[0]= (intptr_t)efa->v1->tmp.l;
506                                         index[1]= (intptr_t)efa->v2->tmp.l;
507                                         index[2]= (intptr_t)efa->v3->tmp.l;
508
509                                         if(flipnormal)
510                                                 SWAP(int, index[0], index[2]);
511                                         
512                                         index+= 3;
513                                         efa= efa->next;
514                                 }
515                         }
516
517                         BLI_addhead(to, dlnew);
518                         
519                 }
520                 BLI_end_edgefill();
521
522                 if(nextcol) {
523                         /* stay at current char but fill polys with next material */
524                         colnr++;
525                 } else {
526                         /* switch to next char and start filling from first material */
527                         charidx++;
528                         colnr= 0;
529                 }
530         }
531         
532         /* do not free polys, needed for wireframe display */
533         
534 }
535
536 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
537 {
538         ListBase front, back;
539         DispList *dl, *dlnew;
540         float *fp, *fp1;
541         int a, dpoly;
542         
543         front.first= front.last= back.first= back.last= NULL;
544         
545         dl= dispbase->first;
546         while(dl) {
547                 if(dl->type==DL_SURF) {
548                         if( (dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U)==0 ) {
549                                 if( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE) ) {
550                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
551                                         BLI_addtail(&front, dlnew);
552                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
553                                         dlnew->nr= dl->parts;
554                                         dlnew->parts= 1;
555                                         dlnew->type= DL_POLY;
556                                         dlnew->col= dl->col;
557                                         dlnew->charidx = dl->charidx;
558                                         
559                                         fp= dl->verts;
560                                         dpoly= 3*dl->nr;
561                                         
562                                         a= dl->parts;
563                                         while(a--) {
564                                                 VECCOPY(fp1, fp);
565                                                 fp1+= 3;
566                                                 fp+= dpoly;
567                                         }
568                                 }
569                                 if( (cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE) ) {
570                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
571                                         BLI_addtail(&back, dlnew);
572                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
573                                         dlnew->nr= dl->parts;
574                                         dlnew->parts= 1;
575                                         dlnew->type= DL_POLY;
576                                         dlnew->col= dl->col;
577                                         dlnew->charidx= dl->charidx;
578                                         
579                                         fp= dl->verts+3*(dl->nr-1);
580                                         dpoly= 3*dl->nr;
581                                         
582                                         a= dl->parts;
583                                         while(a--) {
584                                                 VECCOPY(fp1, fp);
585                                                 fp1+= 3;
586                                                 fp+= dpoly;
587                                         }
588                                 }
589                         }
590                 }
591                 dl= dl->next;
592         }
593
594         filldisplist(&front, dispbase, 1);
595         filldisplist(&back, dispbase, 0);
596         
597         freedisplist(&front);
598         freedisplist(&back);
599
600         filldisplist(dispbase, dispbase, 0);
601         
602 }
603
604 static void curve_to_filledpoly(Curve *cu, ListBase *UNUSED(nurb), ListBase *dispbase)
605 {
606         if(cu->flag & CU_3D) return;
607
608         if(dispbase->first && ((DispList*) dispbase->first)->type==DL_SURF) {
609                 bevels_to_filledpoly(cu, dispbase);
610         }
611         else {
612                 filldisplist(dispbase, dispbase, 0);
613         }
614 }
615
616 /* taper rules:
617   - only 1 curve
618   - first point left, last point right
619   - based on subdivided points in original curve, not on points in taper curve (still)
620 */
621 float calc_taper(Scene *scene, Object *taperobj, int cur, int tot)
622 {
623         DispList *dl;
624         
625         if(taperobj==NULL || taperobj->type!=OB_CURVE) return 1.0;
626         
627         dl= taperobj->disp.first;
628         if(dl==NULL) {
629                 makeDispListCurveTypes(scene, taperobj, 0);
630                 dl= taperobj->disp.first;
631         }
632         if(dl) {
633                 float fac= ((float)cur)/(float)(tot-1);
634                 float minx, dx, *fp;
635                 int a;
636                 
637                 /* horizontal size */
638                 minx= dl->verts[0];
639                 dx= dl->verts[3*(dl->nr-1)] - minx;
640                 if(dx > 0.0f) {
641                 
642                         fp= dl->verts;
643                         for(a=0; a<dl->nr; a++, fp+=3) {
644                                 if( (fp[0]-minx)/dx >= fac) {
645                                         /* interpolate with prev */
646                                         if(a>0) {
647                                                 float fac1= (fp[-3]-minx)/dx;
648                                                 float fac2= (fp[0]-minx)/dx;
649                                                 if(fac1!=fac2)
650                                                         return fp[1]*(fac1-fac)/(fac1-fac2) + fp[-2]*(fac-fac2)/(fac1-fac2);
651                                         }
652                                         return fp[1];
653                                 }
654                         }
655                         return fp[-2];  // last y coord
656                 }
657         }
658         
659         return 1.0;
660 }
661
662 void makeDispListMBall(Scene *scene, Object *ob)
663 {
664         if(!ob || ob->type!=OB_MBALL) return;
665
666         // XXX: mball stuff uses plenty of global variables
667         //      while this is unchanged updating during render is unsafe
668         if(G.rendering) return;
669
670         freedisplist(&(ob->disp));
671
672         if(ob->type==OB_MBALL) {
673                 if(ob==find_basis_mball(scene, ob)) {
674                         metaball_polygonize(scene, ob, &ob->disp);
675                         tex_space_mball(ob);
676
677                         object_deform_mball(ob, &ob->disp);
678                 }
679         }
680         
681         boundbox_displist(ob);
682 }
683
684 void makeDispListMBall_forRender(Scene *scene, Object *ob, ListBase *dispbase)
685 {
686         metaball_polygonize(scene, ob, dispbase);
687         tex_space_mball(ob);
688         
689         object_deform_mball(ob, dispbase);
690 }
691
692 static ModifierData *curve_get_tesselate_point(Scene *scene, Object *ob, int forRender, int editmode)
693 {
694         ModifierData *md = modifiers_getVirtualModifierList(ob);
695         ModifierData *preTesselatePoint;
696         int required_mode;
697
698         if(forRender) required_mode = eModifierMode_Render;
699         else required_mode = eModifierMode_Realtime;
700
701         if(editmode) required_mode |= eModifierMode_Editmode;
702
703         preTesselatePoint = NULL;
704         for (; md; md=md->next) {
705                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
706
707                 if (!modifier_isEnabled(scene, md, required_mode)) continue;
708                 if (mti->type == eModifierTypeType_Constructive) return preTesselatePoint;
709
710                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
711                         preTesselatePoint = md;
712
713                         /* this modifiers are moving point of tesselation automatically
714                            (some of them even can't be applied on tesselated curve), set flag
715                            for incformation button in modifier's header */
716                         md->mode |= eModifierMode_ApplyOnSpline;
717                 } else if(md->mode&eModifierMode_ApplyOnSpline) {
718                         preTesselatePoint = md;
719                 }
720         }
721
722         return preTesselatePoint;
723 }
724
725 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, int forRender, float (**originalVerts_r)[3], float (**deformedVerts_r)[3], int *numVerts_r)
726 {
727         ModifierData *md = modifiers_getVirtualModifierList(ob);
728         ModifierData *preTesselatePoint;
729         Curve *cu= ob->data;
730         ListBase *nurb= BKE_curve_nurbs(cu);
731         int numVerts = 0;
732         int editmode = (!forRender && cu->editnurb);
733         float (*originalVerts)[3] = NULL;
734         float (*deformedVerts)[3] = NULL;
735         float *keyVerts= NULL;
736         int required_mode;
737
738         if(forRender) required_mode = eModifierMode_Render;
739         else required_mode = eModifierMode_Realtime;
740
741         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
742         
743         if(editmode) required_mode |= eModifierMode_Editmode;
744
745         if(cu->editnurb==NULL) {
746                 keyVerts= do_ob_key(scene, ob);
747
748                 if(keyVerts) {
749                         /* split coords from key data, the latter also includes
750                            tilts, which is passed through in the modifier stack.
751                            this is also the reason curves do not use a virtual
752                            shape key modifier yet. */
753                         deformedVerts= curve_getKeyVertexCos(cu, nurb, keyVerts);
754                         originalVerts= MEM_dupallocN(deformedVerts);
755                 }
756         }
757         
758         if (preTesselatePoint) {
759                 for (; md; md=md->next) {
760                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
761
762                         md->scene= scene;
763                         
764                         if ((md->mode & required_mode) != required_mode) continue;
765                         if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
766                         if (mti->type!=eModifierTypeType_OnlyDeform) continue;
767
768                         if (!deformedVerts) {
769                                 deformedVerts = curve_getVertexCos(cu, nurb, &numVerts);
770                                 originalVerts = MEM_dupallocN(deformedVerts);
771                         }
772
773                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, forRender, editmode);
774
775                         if (md==preTesselatePoint)
776                                 break;
777                 }
778         }
779
780         if (deformedVerts)
781                 curve_applyVertexCos(cu, nurb, deformedVerts);
782         if (keyVerts) /* these are not passed through modifier stack */
783                 curve_applyKeyVertexTilts(cu, nurb, keyVerts);
784
785         if(keyVerts)
786                 MEM_freeN(keyVerts);
787
788         *originalVerts_r = originalVerts;
789         *deformedVerts_r = deformedVerts;
790         *numVerts_r = numVerts;
791 }
792
793 static float (*displist_get_allverts (ListBase *dispbase, int *totvert))[3]
794 {
795         DispList *dl;
796         float (*allverts)[3], *fp;
797
798         *totvert= 0;
799
800         for (dl=dispbase->first; dl; dl=dl->next)
801                 *totvert+= (dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr;
802
803         allverts= MEM_mallocN((*totvert)*sizeof(float)*3, "displist_get_allverts allverts");
804         fp= (float*)allverts;
805         for (dl=dispbase->first; dl; dl=dl->next) {
806                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
807                 memcpy(fp, dl->verts, sizeof(float) * offs);
808                 fp+= offs;
809         }
810
811         return allverts;
812 }
813
814 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
815 {
816         DispList *dl;
817         float *fp;
818
819         fp= (float*)allverts;
820         for (dl=dispbase->first; dl; dl=dl->next) {
821                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
822                 memcpy(dl->verts, fp, sizeof(float) * offs);
823                 fp+= offs;
824         }
825 }
826
827 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispbase,
828         DerivedMesh **derivedFinal, int forRender, float (*originalVerts)[3], float (*deformedVerts)[3])
829 {
830         ModifierData *md = modifiers_getVirtualModifierList(ob);
831         ModifierData *preTesselatePoint;
832         Curve *cu= ob->data;
833         ListBase *nurb= BKE_curve_nurbs(cu);
834         int required_mode = 0, totvert = 0;
835         int editmode = (!forRender && cu->editnurb);
836         DerivedMesh *dm= NULL, *ndm;
837         float (*vertCos)[3] = NULL;
838
839         if(forRender) required_mode = eModifierMode_Render;
840         else required_mode = eModifierMode_Realtime;
841
842         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
843         
844         if(editmode) required_mode |= eModifierMode_Editmode;
845
846         if (preTesselatePoint) {
847                 md = preTesselatePoint->next;
848         }
849
850         if (derivedFinal && *derivedFinal) {
851                 (*derivedFinal)->release (*derivedFinal);
852         }
853
854         for (; md; md=md->next) {
855                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
856
857                 md->scene= scene;
858
859                 if ((md->mode & required_mode) != required_mode) continue;
860                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
861
862                 if (mti->type == eModifierTypeType_OnlyDeform ||
863                                 (mti->type == eModifierTypeType_DeformOrConstruct && !dm)) {
864                         if (dm) {
865                                 if (!vertCos) {
866                                         totvert = dm->getNumVerts(dm);
867                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
868                                         dm->getVertCos(dm, vertCos);
869                                 }
870
871                                 mti->deformVerts(md, ob, dm, vertCos, totvert, forRender, editmode);
872                         } else {
873                                 if (!vertCos) {
874                                         vertCos= displist_get_allverts(dispbase, &totvert);
875                                 }
876
877                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, forRender, editmode);
878                         }
879                 } else {
880                         if (!derivedFinal) {
881                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh */
882                                 /* is totally unnecessary, so we could stop modifiers applying */
883                                 /* when we found constructive modifier but derived mesh is unwanted result */
884                                 break;
885                         }
886
887                         if (dm) {
888                                 if (vertCos) {
889                                         DerivedMesh *tdm = CDDM_copy(dm);
890                                         dm->release(dm);
891                                         dm = tdm;
892
893                                         CDDM_apply_vert_coords(dm, vertCos);
894                                         CDDM_calc_normals(dm);
895                                 }
896                         } else {
897                                 if (vertCos) {
898                                         displist_apply_allverts(dispbase, vertCos);
899                                 }
900
901                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
902                                         curve_to_filledpoly(cu, nurb, dispbase);
903                                 }
904
905                                 dm= CDDM_from_curve_customDB(ob, dispbase);
906
907                                 CDDM_calc_normals(dm);
908                         }
909
910                         if (vertCos) {
911                                 /* Vertex coordinates were applied to necessary data, could free it */
912                                 MEM_freeN(vertCos);
913                                 vertCos= NULL;
914                         }
915
916                         ndm = mti->applyModifier(md, ob, dm, forRender, editmode);
917
918                         if (ndm) {
919                                 /* Modifier returned a new derived mesh */
920
921                                 if (dm && dm != ndm) /* Modifier  */
922                                         dm->release (dm);
923                                 dm = ndm;
924                         }
925                 }
926         }
927
928         if (vertCos) {
929                 if (dm) {
930                         DerivedMesh *tdm = CDDM_copy(dm);
931                         dm->release(dm);
932                         dm = tdm;
933
934                         CDDM_apply_vert_coords(dm, vertCos);
935                         CDDM_calc_normals(dm);
936                         MEM_freeN(vertCos);
937                 } else {
938                         displist_apply_allverts(dispbase, vertCos);
939                         MEM_freeN(vertCos);
940                         vertCos= NULL;
941                 }
942         }
943
944         if (derivedFinal) {
945                 (*derivedFinal) = dm;
946         }
947
948         if (deformedVerts) {
949                 curve_applyVertexCos(ob->data, nurb, originalVerts);
950                 MEM_freeN(originalVerts);
951                 MEM_freeN(deformedVerts);
952         }
953 }
954
955 static void displist_surf_indices(DispList *dl)
956 {
957         int a, b, p1, p2, p3, p4;
958         int *index;
959         
960         dl->totindex= 0;
961         
962         index=dl->index= MEM_mallocN( 4*sizeof(int)*(dl->parts+1)*(dl->nr+1), "index array nurbs");
963         
964         for(a=0; a<dl->parts; a++) {
965                 
966                 if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
967                         break;
968                 
969                 for(; b<dl->nr; b++, index+=4) {        
970                         index[0]= p1;
971                         index[1]= p2;
972                         index[2]= p4;
973                         index[3]= p3;
974                         
975                         dl->totindex++;
976                         
977                         p2= p1; p1++;
978                         p4= p3; p3++;
979
980                 }
981         }
982         
983 }
984
985 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
986 {
987         DerivedMesh *dm;
988         ListBase disp= {NULL, NULL};
989
990         /* OrcoDM should be created from underformed disp lists */
991         makeDispListCurveTypes_forOrco(scene, ob, &disp);
992         dm= CDDM_from_curve_customDB(ob, &disp);
993
994         freedisplist(&disp);
995
996         return dm;
997 }
998
999 static void add_orco_dm(Scene *scene, Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1000 {
1001         float (*orco)[3], (*layerorco)[3];
1002         int totvert, a;
1003         Curve *cu= ob->data;
1004
1005         totvert= dm->getNumVerts(dm);
1006
1007         if(orcodm) {
1008                 orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
1009
1010                 if(orcodm->getNumVerts(orcodm) == totvert)
1011                         orcodm->getVertCos(orcodm, orco);
1012                 else
1013                         dm->getVertCos(dm, orco);
1014         }
1015         else {
1016                 orco= (float(*)[3])make_orco_curve(scene, ob);
1017         }
1018
1019         for(a=0; a<totvert; a++) {
1020                 float *co = orco[a];
1021                 co[0] = (co[0]-cu->loc[0])/cu->size[0];
1022                 co[1] = (co[1]-cu->loc[1])/cu->size[1];
1023                 co[2] = (co[2]-cu->loc[2])/cu->size[2];
1024         }
1025
1026         if((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1027                 memcpy(layerorco, orco, sizeof(float)*totvert);
1028                 MEM_freeN(orco);
1029         }
1030         else
1031                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1032 }
1033
1034 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1035 {
1036         /* this function represents logic of mesh's orcodm calculation */
1037         /* for displist-based objects */
1038
1039         ModifierData *md = modifiers_getVirtualModifierList(ob);
1040         ModifierData *preTesselatePoint;
1041         Curve *cu= ob->data;
1042         int required_mode;
1043         int editmode = (!forRender && cu->editnurb);
1044         DerivedMesh *ndm, *orcodm= NULL;
1045
1046         if(forRender) required_mode = eModifierMode_Render;
1047         else required_mode = eModifierMode_Realtime;
1048
1049         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1050
1051         if(editmode) required_mode |= eModifierMode_Editmode;
1052
1053         if (preTesselatePoint) {
1054                 md = preTesselatePoint->next;
1055         }
1056
1057         for (; md; md=md->next) {
1058                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1059
1060                 md->scene= scene;
1061
1062                 if ((md->mode & required_mode) != required_mode) continue;
1063                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1064                 if (mti->type!=eModifierTypeType_Constructive) continue;
1065
1066                 if(!orcodm)
1067                         orcodm= create_orco_dm(scene, ob);
1068
1069                 ndm = mti->applyModifier(md, ob, orcodm, forRender, 0);
1070
1071                 if(ndm) {
1072                         /* if the modifier returned a new dm, release the old one */
1073                         if(orcodm && orcodm != ndm) {
1074                                 orcodm->release(orcodm);
1075                         }
1076                         orcodm = ndm;
1077                 }
1078         }
1079
1080         /* add an orco layer if needed */
1081         add_orco_dm(scene, ob, derivedFinal, orcodm);
1082
1083         if(orcodm)
1084                 orcodm->release(orcodm);
1085 }
1086
1087 void makeDispListSurf(Scene *scene, Object *ob, ListBase *dispbase,
1088         DerivedMesh **derivedFinal, int forRender, int forOrco)
1089 {
1090         ListBase *nubase;
1091         Nurb *nu;
1092         Curve *cu = ob->data;
1093         DispList *dl;
1094         float *data;
1095         int len;
1096         int numVerts;
1097         float (*originalVerts)[3];
1098         float (*deformedVerts)[3];
1099
1100         if(!forRender && cu->editnurb)
1101                 nubase= curve_editnurbs(cu);
1102         else
1103                 nubase= &cu->nurb;
1104
1105         if(!forOrco)
1106                 curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1107
1108         for (nu=nubase->first; nu; nu=nu->next) {
1109                 if(forRender || nu->hide==0) {
1110                         int resolu= nu->resolu, resolv= nu->resolv;
1111
1112                         if(forRender){
1113                                 if(cu->resolu_ren) resolu= cu->resolu_ren;
1114                                 if(cu->resolv_ren) resolv= cu->resolv_ren;
1115                         }
1116
1117                         if(nu->pntsv==1) {
1118                                 len= SEGMENTSU(nu)*resolu;
1119
1120                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1121                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1122
1123                                 BLI_addtail(dispbase, dl);
1124                                 dl->parts= 1;
1125                                 dl->nr= len;
1126                                 dl->col= nu->mat_nr;
1127                                 dl->charidx= nu->charidx;
1128
1129                                 /* dl->rt will be used as flag for render face and */
1130                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1131                                 dl->rt= nu->flag & ~CU_2D;
1132
1133                                 data= dl->verts;
1134                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
1135                                 else dl->type= DL_SEGM;
1136
1137                                 makeNurbcurve(nu, data, NULL, NULL, NULL, resolu, 3*sizeof(float));
1138                         }
1139                         else {
1140                                 len= (nu->pntsu*resolu) * (nu->pntsv*resolv);
1141                                 
1142                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1143                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1144                                 BLI_addtail(dispbase, dl);
1145
1146                                 dl->col= nu->mat_nr;
1147                                 dl->charidx= nu->charidx;
1148
1149                                 /* dl->rt will be used as flag for render face and */
1150                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1151                                 dl->rt= nu->flag & ~CU_2D;
1152
1153                                 data= dl->verts;
1154                                 dl->type= DL_SURF;
1155
1156                                 dl->parts= (nu->pntsu*resolu);  /* in reverse, because makeNurbfaces works that way */
1157                                 dl->nr= (nu->pntsv*resolv);
1158                                 if(nu->flagv & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_U;    /* reverse too! */
1159                                 if(nu->flagu & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_V;
1160
1161                                 makeNurbfaces(nu, data, 0, resolu, resolv);
1162                                 
1163                                 /* gl array drawing: using indices */
1164                                 displist_surf_indices(dl);
1165                         }
1166                 }
1167         }
1168
1169         /* make copy of 'undeformed" displist for texture space calculation
1170            actually, it's not totally undeformed -- pre-tesselation modifiers are
1171            already applied, thats how it worked for years, so keep for compatibility (sergey) */
1172         copy_displist(&cu->disp, dispbase);
1173
1174         if (!forRender) {
1175                 tex_space_curve(cu);
1176         }
1177
1178         if(!forOrco)
1179                 curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal,
1180                         forRender, originalVerts, deformedVerts);
1181 }
1182
1183 static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1184         DerivedMesh **derivedFinal, int forRender, int forOrco)
1185 {
1186         Curve *cu = ob->data;
1187
1188         /* we do allow duplis... this is only displist on curve level */
1189         if(!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1190
1191         if(ob->type==OB_SURF) {
1192                 makeDispListSurf(scene, ob, dispbase, derivedFinal, forRender, forOrco);
1193         }
1194         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1195                 ListBase dlbev;
1196                 ListBase *nubase;
1197                 float (*originalVerts)[3];
1198                 float (*deformedVerts)[3];
1199                 int numVerts;
1200
1201                 nubase= BKE_curve_nurbs(cu);
1202
1203                 BLI_freelistN(&(cu->bev));
1204
1205                 if(cu->path) free_path(cu->path);
1206                 cu->path= NULL;
1207
1208                 if(ob->type==OB_FONT) BKE_text_to_curve(G.main, scene, ob, 0);
1209
1210                 if(!forOrco) curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1211
1212                 makeBevelList(ob);
1213
1214                 /* If curve has no bevel will return nothing */
1215                 makebevelcurve(scene, ob, &dlbev, forRender);
1216
1217                 /* no bevel or extrude, and no width correction? */
1218                 if (!dlbev.first && cu->width==1.0f) {
1219                         curve_to_displist(cu, nubase, dispbase, forRender);
1220                 } else {
1221                         float widfac= cu->width - 1.0f;
1222                         BevList *bl= cu->bev.first;
1223                         Nurb *nu= nubase->first;
1224
1225                         for (; bl && nu; bl=bl->next,nu=nu->next) {
1226                                 DispList *dl;
1227                                 float *fp1, *data;
1228                                 BevPoint *bevp;
1229                                 int a,b;
1230
1231                                 if (bl->nr) { /* blank bevel lists can happen */
1232
1233                                         /* exception handling; curve without bevel or extrude, with width correction */
1234                                         if(dlbev.first==NULL) {
1235                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev");
1236                                                 dl->verts= MEM_callocN(3*sizeof(float)*bl->nr, "dlverts");
1237                                                 BLI_addtail(dispbase, dl);
1238
1239                                                 if(bl->poly!= -1) dl->type= DL_POLY;
1240                                                 else dl->type= DL_SEGM;
1241
1242                                                 if(dl->type==DL_SEGM) dl->flag = (DL_FRONT_CURVE|DL_BACK_CURVE);
1243
1244                                                 dl->parts= 1;
1245                                                 dl->nr= bl->nr;
1246                                                 dl->col= nu->mat_nr;
1247                                                 dl->charidx= nu->charidx;
1248
1249                                                 /* dl->rt will be used as flag for render face and */
1250                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1251                                                 dl->rt= nu->flag & ~CU_2D;
1252
1253                                                 a= dl->nr;
1254                                                 bevp= (BevPoint *)(bl+1);
1255                                                 data= dl->verts;
1256                                                 while(a--) {
1257                                                         data[0]= bevp->vec[0]+widfac*bevp->sina;
1258                                                         data[1]= bevp->vec[1]+widfac*bevp->cosa;
1259                                                         data[2]= bevp->vec[2];
1260                                                         bevp++;
1261                                                         data+=3;
1262                                                 }
1263                                         }
1264                                         else {
1265                                                 DispList *dlb;
1266
1267                                                 for (dlb=dlbev.first; dlb; dlb=dlb->next) {
1268         
1269                                                         /* for each part of the bevel use a separate displblock */
1270                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1271                                                         dl->verts= data= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1272                                                         BLI_addtail(dispbase, dl);
1273         
1274                                                         dl->type= DL_SURF;
1275                                                         
1276                                                         dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1277                                                         if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1278                                                         if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1279                                                         
1280                                                         dl->parts= bl->nr;
1281                                                         dl->nr= dlb->nr;
1282                                                         dl->col= nu->mat_nr;
1283                                                         dl->charidx= nu->charidx;
1284
1285                                                         /* dl->rt will be used as flag for render face and */
1286                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1287                                                         dl->rt= nu->flag & ~CU_2D;
1288
1289                                                         dl->bevelSplitFlag= MEM_callocN(sizeof(*dl->col2)*((bl->nr+0x1F)>>5), "bevelSplitFlag");
1290         
1291                                                         /* for each point of poly make a bevel piece */
1292                                                         bevp= (BevPoint *)(bl+1);
1293                                                         for(a=0; a<bl->nr; a++,bevp++) {
1294                                                                 float fac=1.0;
1295                                                                 if (cu->taperobj==NULL) {
1296                                                                         if ( (cu->bevobj!=NULL) || !((cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) )
1297                                                                                 fac = bevp->radius;
1298                                                                 } else {
1299                                                                         fac = calc_taper(scene, cu->taperobj, a, bl->nr);
1300                                                                 }
1301
1302                                                                 if (bevp->split_tag) {
1303                                                                         dl->bevelSplitFlag[a>>5] |= 1<<(a&0x1F);
1304                                                                 }
1305         
1306                                                                         /* rotate bevel piece and write in data */
1307                                                                 fp1= dlb->verts;
1308                                                                 for (b=0; b<dlb->nr; b++,fp1+=3,data+=3) {
1309                                                                         if(cu->flag & CU_3D) {
1310                                                                                 float vec[3];
1311         
1312                                                                                 vec[0]= fp1[1]+widfac;
1313                                                                                 vec[1]= fp1[2];
1314                                                                                 vec[2]= 0.0;
1315
1316                                                                                 mul_qt_v3(bevp->quat, vec);
1317
1318                                                                                 data[0]= bevp->vec[0] + fac*vec[0];
1319                                                                                 data[1]= bevp->vec[1] + fac*vec[1];
1320                                                                                 data[2]= bevp->vec[2] + fac*vec[2];
1321                                                                         }
1322                                                                         else {
1323                                                                                 data[0]= bevp->vec[0] + fac*(widfac+fp1[1])*bevp->sina;
1324                                                                                 data[1]= bevp->vec[1] + fac*(widfac+fp1[1])*bevp->cosa;
1325                                                                                 data[2]= bevp->vec[2] + fac*fp1[2];
1326                                                                         }
1327                                                                 }
1328                                                         }
1329                                                         
1330                                                         /* gl array drawing: using indices */
1331                                                         displist_surf_indices(dl);
1332                                                 }
1333                                         }
1334                                 }
1335
1336                         }
1337                         freedisplist(&dlbev);
1338                 }
1339
1340                 if (!(cu->flag & CU_DEFORM_FILL)) {
1341                         curve_to_filledpoly(cu, nubase, dispbase);
1342                 }
1343
1344                 if(cu->flag & CU_PATH) calc_curvepath(ob);
1345
1346                 /* make copy of 'undeformed" displist for texture space calculation
1347                    actually, it's not totally undeformed -- pre-tesselation modifiers are
1348                    already applied, thats how it worked for years, so keep for compatibility (sergey) */
1349                 copy_displist(&cu->disp, dispbase);
1350
1351                 if (!forRender) {
1352                         tex_space_curve(cu);
1353                 }
1354
1355                 if(!forOrco) curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal, forRender, originalVerts, deformedVerts);
1356
1357                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1358                         curve_to_filledpoly(cu, nubase, dispbase);
1359                 }
1360         }
1361 }
1362
1363 void makeDispListCurveTypes(Scene *scene, Object *ob, int forOrco)
1364 {
1365         Curve *cu= ob->data;
1366         ListBase *dispbase;
1367
1368         /* The same check for duplis as in do_makeDispListCurveTypes.
1369            Happens when curve used for constraint/bevel was converted to mesh.
1370            check there is still needed for render displist and orco displists. */
1371         if(!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1372
1373         freedisplist(&(ob->disp));
1374         dispbase= &(ob->disp);
1375         freedisplist(dispbase);
1376
1377         /* free displist used for textspace */
1378         freedisplist(&cu->disp);
1379
1380         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco);
1381
1382         if (ob->derivedFinal) {
1383                 DM_set_object_boundbox (ob, ob->derivedFinal);
1384         } else {
1385                 boundbox_displist (ob);
1386
1387                 /* if there is no derivedMesh, object's boundbox is unneeded */
1388                 if (ob->bb) {
1389                         MEM_freeN(ob->bb);
1390                         ob->bb= NULL;
1391                 }
1392         }
1393 }
1394
1395 void makeDispListCurveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1396         DerivedMesh **derivedFinal, int forOrco)
1397 {
1398         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco);
1399 }
1400
1401 void makeDispListCurveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1402 {
1403         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1);
1404 }
1405
1406 /* add Orco layer to the displist object which has got derived mesh and return orco */
1407 float *makeOrcoDispList(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1408 {
1409         float *orco;
1410
1411         if (derivedFinal == NULL)
1412                 derivedFinal= ob->derivedFinal;
1413
1414         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1415                 curve_calc_orcodm(scene, ob, derivedFinal, forRender);
1416         }
1417
1418         orco= derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1419
1420         if(orco) {
1421                 orco= MEM_dupallocN(orco);
1422         }
1423
1424         return orco;
1425 }
1426
1427 /* this is confusing, there's also min_max_object, appplying the obmat... */
1428 static void boundbox_displist(Object *ob)
1429 {
1430         BoundBox *bb=NULL;
1431         float min[3], max[3];
1432         DispList *dl;
1433         float *vert;
1434         int a, tot=0;
1435         
1436         INIT_MINMAX(min, max);
1437
1438         if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1439                 Curve *cu= ob->data;
1440                 int doit= 0;
1441
1442                 if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
1443                 bb= cu->bb;
1444                 
1445                 dl= ob->disp.first;
1446
1447                 while (dl) {
1448                         if(dl->type==DL_INDEX3) tot= dl->nr;
1449                         else tot= dl->nr*dl->parts;
1450                         
1451                         vert= dl->verts;
1452                         for(a=0; a<tot; a++, vert+=3) {
1453                                 doit= 1;
1454                                 DO_MINMAX(vert, min, max);
1455                         }
1456
1457                         dl= dl->next;
1458                 }
1459                 
1460                 if(!doit) {
1461                         /* there's no geometry in displist, use zero-sized boundbox */
1462                         zero_v3(min);
1463                         zero_v3(max);
1464                 }
1465                 
1466         }
1467         
1468         if(bb) {
1469                 boundbox_set_from_min_max(bb, min, max);
1470         }
1471 }
1472