fix [#29980] Fluid only displays wireframe
[blender.git] / source / blender / blenkernel / intern / displist.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/displist.c
29  *  \ingroup bke
30  */
31
32
33 #include <math.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_curve_types.h"
40 #include "DNA_meshdata_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_material_types.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_math.h"
47 #include "BLI_editVert.h"
48 #include "BLI_scanfill.h"
49 #include "BLI_utildefines.h"
50
51 #include "BKE_global.h"
52 #include "BKE_displist.h"
53 #include "BKE_cdderivedmesh.h"
54 #include "BKE_object.h"
55 #include "BKE_mball.h"
56 #include "BKE_material.h"
57 #include "BKE_curve.h"
58 #include "BKE_key.h"
59 #include "BKE_anim.h"
60 #include "BKE_font.h"
61 #include "BKE_lattice.h"
62 #include "BKE_modifier.h"
63
64 #include "BLO_sys_types.h" // for intptr_t support
65
66 extern Material defmaterial;    /* material.c */
67
68 static void boundbox_displist(Object *ob);
69
70 void free_disp_elem(DispList *dl)
71 {
72         if(dl) {
73                 if(dl->verts) MEM_freeN(dl->verts);
74                 if(dl->nors) MEM_freeN(dl->nors);
75                 if(dl->index) MEM_freeN(dl->index);
76                 if(dl->col1) MEM_freeN(dl->col1);
77                 if(dl->col2) MEM_freeN(dl->col2);
78                 if(dl->bevelSplitFlag) MEM_freeN(dl->bevelSplitFlag);
79                 MEM_freeN(dl);
80         }
81 }
82
83 void freedisplist(ListBase *lb)
84 {
85         DispList *dl;
86
87         dl= lb->first;
88         while(dl) {
89                 BLI_remlink(lb, dl);
90                 free_disp_elem(dl);
91                 dl= lb->first;
92         }
93 }
94
95 DispList *find_displist_create(ListBase *lb, int type)
96 {
97         DispList *dl;
98         
99         dl= lb->first;
100         while(dl) {
101                 if(dl->type==type) return dl;
102                 dl= dl->next;
103         }
104
105         dl= MEM_callocN(sizeof(DispList), "find_disp");
106         dl->type= type;
107         BLI_addtail(lb, dl);
108
109         return dl;
110 }
111
112 DispList *find_displist(ListBase *lb, int type)
113 {
114         DispList *dl;
115         
116         dl= lb->first;
117         while(dl) {
118                 if(dl->type==type) return dl;
119                 dl= dl->next;
120         }
121
122         return NULL;
123 }
124
125 int displist_has_faces(ListBase *lb)
126 {
127         DispList *dl;
128         for(dl= lb->first; dl; dl= dl->next) {
129                 if ELEM3(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)
130                         return 1;
131         }
132         return 0;
133 }
134
135 void copy_displist(ListBase *lbn, ListBase *lb)
136 {
137         DispList *dln, *dl;
138         
139         freedisplist(lbn);
140         
141         dl= lb->first;
142         while(dl) {
143                 
144                 dln= MEM_dupallocN(dl);
145                 BLI_addtail(lbn, dln);
146                 dln->verts= MEM_dupallocN(dl->verts);
147                 dln->nors= MEM_dupallocN(dl->nors);
148                 dln->index= MEM_dupallocN(dl->index);
149                 dln->col1= MEM_dupallocN(dl->col1);
150                 dln->col2= MEM_dupallocN(dl->col2);
151
152                 if(dl->bevelSplitFlag)
153                         dln->bevelSplitFlag= MEM_dupallocN(dl->bevelSplitFlag);
154
155                 dl= dl->next;
156         }
157 }
158
159 void addnormalsDispList(ListBase *lb)
160 {
161         DispList *dl = NULL;
162         float *vdata, *ndata, nor[3];
163         float *v1, *v2, *v3, *v4;
164         float *n1, *n2, *n3, *n4;
165         int a, b, p1, p2, p3, p4;
166
167
168         dl= lb->first;
169         
170         while(dl) {
171                 if(dl->type==DL_INDEX3) {
172                         if(dl->nors==NULL) {
173                                 dl->nors= MEM_callocN(sizeof(float)*3, "dlnors");
174                                 if(dl->verts[2] < 0.0f) dl->nors[2]= -1.0f;
175                                 else dl->nors[2]= 1.0f;
176                         }
177                 }
178                 else if(dl->type==DL_SURF) {
179                         if(dl->nors==NULL) {
180                                 dl->nors= MEM_callocN(sizeof(float)*3*dl->nr*dl->parts, "dlnors");
181                                 
182                                 vdata= dl->verts;
183                                 ndata= dl->nors;
184                                 
185                                 for(a=0; a<dl->parts; a++) {
186                                         
187                                         if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
188                                                 break;
189         
190                                         v1= vdata+ 3*p1; 
191                                         n1= ndata+ 3*p1;
192                                         v2= vdata+ 3*p2; 
193                                         n2= ndata+ 3*p2;
194                                         v3= vdata+ 3*p3; 
195                                         n3= ndata+ 3*p3;
196                                         v4= vdata+ 3*p4; 
197                                         n4= ndata+ 3*p4;
198                                         
199                                         for(; b<dl->nr; b++) {
200         
201                                                 normal_quad_v3( nor,v1, v3, v4, v2);
202         
203                                                 add_v3_v3(n1, nor);
204                                                 add_v3_v3(n2, nor);
205                                                 add_v3_v3(n3, nor);
206                                                 add_v3_v3(n4, nor);
207         
208                                                 v2= v1; v1+= 3;
209                                                 v4= v3; v3+= 3;
210                                                 n2= n1; n1+= 3;
211                                                 n4= n3; n3+= 3;
212                                         }
213                                 }
214                                 a= dl->parts*dl->nr;
215                                 v1= ndata;
216                                 while(a--) {
217                                         normalize_v3(v1);
218                                         v1+= 3;
219                                 }
220                         }
221                 }
222                 dl= dl->next;
223         }
224 }
225
226 void count_displist(ListBase *lb, int *totvert, int *totface)
227 {
228         DispList *dl;
229         
230         dl= lb->first;
231         while(dl) {
232                 
233                 switch(dl->type) {
234                         case DL_SURF:
235                                 *totvert+= dl->nr*dl->parts;
236                                 *totface+= (dl->nr-1)*(dl->parts-1);
237                                 break;
238                         case DL_INDEX3:
239                         case DL_INDEX4:
240                                 *totvert+= dl->nr;
241                                 *totface+= dl->parts;
242                                 break;
243                         case DL_POLY:
244                         case DL_SEGM:
245                                 *totvert+= dl->nr*dl->parts;
246                 }
247                 
248                 dl= dl->next;
249         }
250 }
251
252 int surfindex_displist(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4)
253 {
254         if((dl->flag & DL_CYCL_V)==0 && a==(dl->parts)-1) {
255                 return 0;
256         }
257         
258         if(dl->flag & DL_CYCL_U) {
259                 (*p1)= dl->nr*a;
260                 (*p2)= (*p1)+ dl->nr-1;
261                 (*p3)= (*p1)+ dl->nr;
262                 (*p4)= (*p2)+ dl->nr;
263                 (*b)= 0;
264         } else {
265                 (*p2)= dl->nr*a;
266                 (*p1)= (*p2)+1;
267                 (*p4)= (*p2)+ dl->nr;
268                 (*p3)= (*p1)+ dl->nr;
269                 (*b)= 1;
270         }
271         
272         if( (dl->flag & DL_CYCL_V) && a==dl->parts-1) {
273                 (*p3)-= dl->nr*dl->parts;
274                 (*p4)-= dl->nr*dl->parts;
275         }
276         
277         return 1;
278 }
279
280 /* ****************** make displists ********************* */
281
282 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase, int forRender)
283 {
284         Nurb *nu;
285         DispList *dl;
286         BezTriple *bezt, *prevbezt;
287         BPoint *bp;
288         float *data;
289         int a, len, resolu;
290         
291         nu= nubase->first;
292         while(nu) {
293                 if(nu->hide==0) {
294                         
295                         if(forRender && cu->resolu_ren!=0)
296                                 resolu= cu->resolu_ren;
297                         else
298                                 resolu= nu->resolu;
299                         
300                         if(!check_valid_nurb_u(nu));
301                         else if(nu->type == CU_BEZIER) {
302                                 
303                                 /* count */
304                                 len= 0;
305                                 a= nu->pntsu-1;
306                                 if(nu->flagu & CU_NURB_CYCLIC) a++;
307
308                                 prevbezt= nu->bezt;
309                                 bezt= prevbezt+1;
310                                 while(a--) {
311                                         if(a==0 && (nu->flagu & CU_NURB_CYCLIC)) bezt= nu->bezt;
312                                         
313                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) len++;
314                                         else len+= resolu;
315                                         
316                                         if(a==0 && (nu->flagu & CU_NURB_CYCLIC)==0) len++;
317                                         
318                                         prevbezt= bezt;
319                                         bezt++;
320                                 }
321                                 
322                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbez");
323                                 /* len+1 because of 'forward_diff_bezier' function */
324                                 dl->verts= MEM_callocN( (len+1)*3*sizeof(float), "dlverts");
325                                 BLI_addtail(dispbase, dl);
326                                 dl->parts= 1;
327                                 dl->nr= len;
328                                 dl->col= nu->mat_nr;
329                                 dl->charidx= nu->charidx;
330
331                                 data= dl->verts;
332
333                                 if(nu->flagu & CU_NURB_CYCLIC) {
334                                         dl->type= DL_POLY;
335                                         a= nu->pntsu;
336                                 }
337                                 else {
338                                         dl->type= DL_SEGM;
339                                         a= nu->pntsu-1;
340                                 }
341                                 
342                                 prevbezt= nu->bezt;
343                                 bezt= prevbezt+1;
344                                 
345                                 while(a--) {
346                                         if(a==0 && dl->type== DL_POLY) bezt= nu->bezt;
347                                         
348                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
349                                                 copy_v3_v3(data, prevbezt->vec[1]);
350                                                 data+= 3;
351                                         }
352                                         else {
353                                                 int j;
354                                                 for(j=0; j<3; j++) {
355                                                         forward_diff_bezier(    prevbezt->vec[1][j],
356                                                                                                         prevbezt->vec[2][j],
357                                                                                                         bezt->vec[0][j],
358                                                                                                         bezt->vec[1][j],
359                                                                                                         data+j, resolu, 3*sizeof(float));
360                                                 }
361                                                 
362                                                 data+= 3*resolu;
363                                         }
364                                         
365                                         if(a==0 && dl->type==DL_SEGM) {
366                                                 copy_v3_v3(data, bezt->vec[1]);
367                                         }
368                                         
369                                         prevbezt= bezt;
370                                         bezt++;
371                                 }
372                         }
373                         else if(nu->type == CU_NURBS) {
374                                 len= (resolu*SEGMENTSU(nu));
375                                 
376                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
377                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
378                                 BLI_addtail(dispbase, dl);
379                                 dl->parts= 1;
380                                 
381                                 dl->nr= len;
382                                 dl->col= nu->mat_nr;
383                                 dl->charidx = nu->charidx;
384
385                                 data= dl->verts;
386                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
387                                 else dl->type= DL_SEGM;
388                                 makeNurbcurve(nu, data, NULL, NULL, NULL, resolu, 3*sizeof(float));
389                         }
390                         else if(nu->type == CU_POLY) {
391                                 len= nu->pntsu;
392                                 dl= MEM_callocN(sizeof(DispList), "makeDispListpoly");
393                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
394                                 BLI_addtail(dispbase, dl);
395                                 dl->parts= 1;
396                                 dl->nr= len;
397                                 dl->col= nu->mat_nr;
398                                 dl->charidx = nu->charidx;
399
400                                 data= dl->verts;
401                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
402                                 else dl->type= DL_SEGM;
403                                 
404                                 a= len;
405                                 bp= nu->bp;
406                                 while(a--) {
407                                         copy_v3_v3(data, bp->vec);
408                                         bp++;
409                                         data+= 3;
410                                 }
411                         }
412                 }
413                 nu= nu->next;
414         }
415 }
416
417
418 void filldisplist(ListBase *dispbase, ListBase *to, int flipnormal)
419 {
420         EditVert *eve, *v1, *vlast;
421         EditFace *efa;
422         DispList *dlnew=NULL, *dl;
423         float *f1;
424         int colnr=0, charidx=0, cont=1, tot, a, *index, nextcol= 0;
425         intptr_t totvert;
426         
427         if(dispbase==NULL) return;
428         if(dispbase->first==NULL) return;
429
430         while(cont) {
431                 cont= 0;
432                 totvert= 0;
433                 nextcol= 0;
434                 
435                 BLI_begin_edgefill();
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                                         copy_v3_v3(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                                                 copy_v3_v3(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                                                 copy_v3_v3(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         int useCache = !forRender;
839
840         if(forRender) required_mode = eModifierMode_Render;
841         else required_mode = eModifierMode_Realtime;
842
843         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
844         
845         if(editmode) required_mode |= eModifierMode_Editmode;
846
847         if (preTesselatePoint) {
848                 md = preTesselatePoint->next;
849         }
850
851         if (derivedFinal && *derivedFinal) {
852                 (*derivedFinal)->release (*derivedFinal);
853         }
854
855         for (; md; md=md->next) {
856                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
857
858                 md->scene= scene;
859
860                 if ((md->mode & required_mode) != required_mode) continue;
861                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
862
863                 if (mti->type == eModifierTypeType_OnlyDeform ||
864                                 (mti->type == eModifierTypeType_DeformOrConstruct && !dm)) {
865                         if (dm) {
866                                 if (!vertCos) {
867                                         totvert = dm->getNumVerts(dm);
868                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
869                                         dm->getVertCos(dm, vertCos);
870                                 }
871
872                                 mti->deformVerts(md, ob, dm, vertCos, totvert, forRender, editmode);
873                         } else {
874                                 if (!vertCos) {
875                                         vertCos= displist_get_allverts(dispbase, &totvert);
876                                 }
877
878                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, forRender, editmode);
879                         }
880                 } else {
881                         if (!derivedFinal) {
882                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh */
883                                 /* is totally unnecessary, so we could stop modifiers applying */
884                                 /* when we found constructive modifier but derived mesh is unwanted result */
885                                 break;
886                         }
887
888                         if (dm) {
889                                 if (vertCos) {
890                                         DerivedMesh *tdm = CDDM_copy(dm);
891                                         dm->release(dm);
892                                         dm = tdm;
893
894                                         CDDM_apply_vert_coords(dm, vertCos);
895                                         CDDM_calc_normals_mapping(dm);
896                                 }
897                         } else {
898                                 if (vertCos) {
899                                         displist_apply_allverts(dispbase, vertCos);
900                                 }
901
902                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
903                                         curve_to_filledpoly(cu, nurb, dispbase);
904                                 }
905
906                                 dm= CDDM_from_curve_customDB(ob, dispbase);
907
908                                 CDDM_calc_normals_mapping(dm);
909                         }
910
911                         if (vertCos) {
912                                 /* Vertex coordinates were applied to necessary data, could free it */
913                                 MEM_freeN(vertCos);
914                                 vertCos= NULL;
915                         }
916
917                         ndm = mti->applyModifier(md, ob, dm, forRender, useCache);
918
919                         if (ndm) {
920                                 /* Modifier returned a new derived mesh */
921
922                                 if (dm && dm != ndm) /* Modifier  */
923                                         dm->release (dm);
924                                 dm = ndm;
925                         }
926                 }
927         }
928
929         if (vertCos) {
930                 if (dm) {
931                         DerivedMesh *tdm = CDDM_copy(dm);
932                         dm->release(dm);
933                         dm = tdm;
934
935                         CDDM_apply_vert_coords(dm, vertCos);
936                         CDDM_calc_normals_mapping(dm);
937                         MEM_freeN(vertCos);
938                 } else {
939                         displist_apply_allverts(dispbase, vertCos);
940                         MEM_freeN(vertCos);
941                         vertCos= NULL;
942                 }
943         }
944
945         if (derivedFinal) {
946                 (*derivedFinal) = dm;
947         }
948
949         if (deformedVerts) {
950                 curve_applyVertexCos(ob->data, nurb, originalVerts);
951                 MEM_freeN(originalVerts);
952                 MEM_freeN(deformedVerts);
953         }
954 }
955
956 static void displist_surf_indices(DispList *dl)
957 {
958         int a, b, p1, p2, p3, p4;
959         int *index;
960         
961         dl->totindex= 0;
962         
963         index=dl->index= MEM_mallocN( 4*sizeof(int)*(dl->parts+1)*(dl->nr+1), "index array nurbs");
964         
965         for(a=0; a<dl->parts; a++) {
966                 
967                 if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
968                         break;
969                 
970                 for(; b<dl->nr; b++, index+=4) {        
971                         index[0]= p1;
972                         index[1]= p2;
973                         index[2]= p4;
974                         index[3]= p3;
975                         
976                         dl->totindex++;
977                         
978                         p2= p1; p1++;
979                         p4= p3; p3++;
980
981                 }
982         }
983         
984 }
985
986 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
987 {
988         DerivedMesh *dm;
989         ListBase disp= {NULL, NULL};
990
991         /* OrcoDM should be created from underformed disp lists */
992         makeDispListCurveTypes_forOrco(scene, ob, &disp);
993         dm= CDDM_from_curve_customDB(ob, &disp);
994
995         freedisplist(&disp);
996
997         return dm;
998 }
999
1000 static void add_orco_dm(Scene *scene, Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1001 {
1002         float (*orco)[3], (*layerorco)[3];
1003         int totvert, a;
1004         Curve *cu= ob->data;
1005
1006         totvert= dm->getNumVerts(dm);
1007
1008         if(orcodm) {
1009                 orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
1010
1011                 if(orcodm->getNumVerts(orcodm) == totvert)
1012                         orcodm->getVertCos(orcodm, orco);
1013                 else
1014                         dm->getVertCos(dm, orco);
1015         }
1016         else {
1017                 orco= (float(*)[3])make_orco_curve(scene, ob);
1018         }
1019
1020         for(a=0; a<totvert; a++) {
1021                 float *co = orco[a];
1022                 co[0] = (co[0]-cu->loc[0])/cu->size[0];
1023                 co[1] = (co[1]-cu->loc[1])/cu->size[1];
1024                 co[2] = (co[2]-cu->loc[2])/cu->size[2];
1025         }
1026
1027         if((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1028                 memcpy(layerorco, orco, sizeof(float)*totvert);
1029                 MEM_freeN(orco);
1030         }
1031         else
1032                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1033 }
1034
1035 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1036 {
1037         /* this function represents logic of mesh's orcodm calculation */
1038         /* for displist-based objects */
1039
1040         ModifierData *md = modifiers_getVirtualModifierList(ob);
1041         ModifierData *preTesselatePoint;
1042         Curve *cu= ob->data;
1043         int required_mode;
1044         int editmode = (!forRender && cu->editnurb);
1045         DerivedMesh *ndm, *orcodm= NULL;
1046
1047         if(forRender) required_mode = eModifierMode_Render;
1048         else required_mode = eModifierMode_Realtime;
1049
1050         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1051
1052         if(editmode) required_mode |= eModifierMode_Editmode;
1053
1054         if (preTesselatePoint) {
1055                 md = preTesselatePoint->next;
1056         }
1057
1058         for (; md; md=md->next) {
1059                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1060
1061                 md->scene= scene;
1062
1063                 if ((md->mode & required_mode) != required_mode) continue;
1064                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1065                 if (mti->type!=eModifierTypeType_Constructive) continue;
1066
1067                 if(!orcodm)
1068                         orcodm= create_orco_dm(scene, ob);
1069
1070                 ndm = mti->applyModifier(md, ob, orcodm, forRender, 0);
1071
1072                 if(ndm) {
1073                         /* if the modifier returned a new dm, release the old one */
1074                         if(orcodm && orcodm != ndm) {
1075                                 orcodm->release(orcodm);
1076                         }
1077                         orcodm = ndm;
1078                 }
1079         }
1080
1081         /* add an orco layer if needed */
1082         add_orco_dm(scene, ob, derivedFinal, orcodm);
1083
1084         if(orcodm)
1085                 orcodm->release(orcodm);
1086 }
1087
1088 void makeDispListSurf(Scene *scene, Object *ob, ListBase *dispbase,
1089         DerivedMesh **derivedFinal, int forRender, int forOrco)
1090 {
1091         ListBase *nubase;
1092         Nurb *nu;
1093         Curve *cu = ob->data;
1094         DispList *dl;
1095         float *data;
1096         int len;
1097         int numVerts;
1098         float (*originalVerts)[3];
1099         float (*deformedVerts)[3];
1100
1101         if(!forRender && cu->editnurb)
1102                 nubase= curve_editnurbs(cu);
1103         else
1104                 nubase= &cu->nurb;
1105
1106         if(!forOrco)
1107                 curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1108
1109         for (nu=nubase->first; nu; nu=nu->next) {
1110                 if(forRender || nu->hide==0) {
1111                         int resolu= nu->resolu, resolv= nu->resolv;
1112
1113                         if(forRender){
1114                                 if(cu->resolu_ren) resolu= cu->resolu_ren;
1115                                 if(cu->resolv_ren) resolv= cu->resolv_ren;
1116                         }
1117
1118                         if(nu->pntsv==1) {
1119                                 len= SEGMENTSU(nu)*resolu;
1120
1121                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1122                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1123
1124                                 BLI_addtail(dispbase, dl);
1125                                 dl->parts= 1;
1126                                 dl->nr= len;
1127                                 dl->col= nu->mat_nr;
1128                                 dl->charidx= nu->charidx;
1129
1130                                 /* dl->rt will be used as flag for render face and */
1131                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1132                                 dl->rt= nu->flag & ~CU_2D;
1133
1134                                 data= dl->verts;
1135                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
1136                                 else dl->type= DL_SEGM;
1137
1138                                 makeNurbcurve(nu, data, NULL, NULL, NULL, resolu, 3*sizeof(float));
1139                         }
1140                         else {
1141                                 len= (nu->pntsu*resolu) * (nu->pntsv*resolv);
1142                                 
1143                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1144                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1145                                 BLI_addtail(dispbase, dl);
1146
1147                                 dl->col= nu->mat_nr;
1148                                 dl->charidx= nu->charidx;
1149
1150                                 /* dl->rt will be used as flag for render face and */
1151                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1152                                 dl->rt= nu->flag & ~CU_2D;
1153
1154                                 data= dl->verts;
1155                                 dl->type= DL_SURF;
1156
1157                                 dl->parts= (nu->pntsu*resolu);  /* in reverse, because makeNurbfaces works that way */
1158                                 dl->nr= (nu->pntsv*resolv);
1159                                 if(nu->flagv & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_U;    /* reverse too! */
1160                                 if(nu->flagu & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_V;
1161
1162                                 makeNurbfaces(nu, data, 0, resolu, resolv);
1163                                 
1164                                 /* gl array drawing: using indices */
1165                                 displist_surf_indices(dl);
1166                         }
1167                 }
1168         }
1169
1170         /* make copy of 'undeformed" displist for texture space calculation
1171            actually, it's not totally undeformed -- pre-tesselation modifiers are
1172            already applied, thats how it worked for years, so keep for compatibility (sergey) */
1173         copy_displist(&cu->disp, dispbase);
1174
1175         if (!forRender) {
1176                 tex_space_curve(cu);
1177         }
1178
1179         if(!forOrco)
1180                 curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal,
1181                         forRender, originalVerts, deformedVerts);
1182 }
1183
1184 static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1185         DerivedMesh **derivedFinal, int forRender, int forOrco)
1186 {
1187         Curve *cu = ob->data;
1188
1189         /* we do allow duplis... this is only displist on curve level */
1190         if(!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1191
1192         if(ob->type==OB_SURF) {
1193                 makeDispListSurf(scene, ob, dispbase, derivedFinal, forRender, forOrco);
1194         }
1195         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1196                 ListBase dlbev;
1197                 ListBase *nubase;
1198                 float (*originalVerts)[3];
1199                 float (*deformedVerts)[3];
1200                 int numVerts;
1201
1202                 nubase= BKE_curve_nurbs(cu);
1203
1204                 BLI_freelistN(&(cu->bev));
1205
1206                 if(cu->path) free_path(cu->path);
1207                 cu->path= NULL;
1208
1209                 if(ob->type==OB_FONT) BKE_text_to_curve(G.main, scene, ob, 0);
1210
1211                 if(!forOrco) curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1212
1213                 makeBevelList(ob);
1214
1215                 /* If curve has no bevel will return nothing */
1216                 makebevelcurve(scene, ob, &dlbev, forRender);
1217
1218                 /* no bevel or extrude, and no width correction? */
1219                 if (!dlbev.first && cu->width==1.0f) {
1220                         curve_to_displist(cu, nubase, dispbase, forRender);
1221                 } else {
1222                         float widfac= cu->width - 1.0f;
1223                         BevList *bl= cu->bev.first;
1224                         Nurb *nu= nubase->first;
1225
1226                         for (; bl && nu; bl=bl->next,nu=nu->next) {
1227                                 DispList *dl;
1228                                 float *fp1, *data;
1229                                 BevPoint *bevp;
1230                                 int a,b;
1231
1232                                 if (bl->nr) { /* blank bevel lists can happen */
1233
1234                                         /* exception handling; curve without bevel or extrude, with width correction */
1235                                         if(dlbev.first==NULL) {
1236                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev");
1237                                                 dl->verts= MEM_callocN(3*sizeof(float)*bl->nr, "dlverts");
1238                                                 BLI_addtail(dispbase, dl);
1239
1240                                                 if(bl->poly!= -1) dl->type= DL_POLY;
1241                                                 else dl->type= DL_SEGM;
1242
1243                                                 if(dl->type==DL_SEGM) dl->flag = (DL_FRONT_CURVE|DL_BACK_CURVE);
1244
1245                                                 dl->parts= 1;
1246                                                 dl->nr= bl->nr;
1247                                                 dl->col= nu->mat_nr;
1248                                                 dl->charidx= nu->charidx;
1249
1250                                                 /* dl->rt will be used as flag for render face and */
1251                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1252                                                 dl->rt= nu->flag & ~CU_2D;
1253
1254                                                 a= dl->nr;
1255                                                 bevp= (BevPoint *)(bl+1);
1256                                                 data= dl->verts;
1257                                                 while(a--) {
1258                                                         data[0]= bevp->vec[0]+widfac*bevp->sina;
1259                                                         data[1]= bevp->vec[1]+widfac*bevp->cosa;
1260                                                         data[2]= bevp->vec[2];
1261                                                         bevp++;
1262                                                         data+=3;
1263                                                 }
1264                                         }
1265                                         else {
1266                                                 DispList *dlb;
1267
1268                                                 for (dlb=dlbev.first; dlb; dlb=dlb->next) {
1269         
1270                                                         /* for each part of the bevel use a separate displblock */
1271                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1272                                                         dl->verts= data= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1273                                                         BLI_addtail(dispbase, dl);
1274         
1275                                                         dl->type= DL_SURF;
1276                                                         
1277                                                         dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1278                                                         if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1279                                                         if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1280                                                         
1281                                                         dl->parts= bl->nr;
1282                                                         dl->nr= dlb->nr;
1283                                                         dl->col= nu->mat_nr;
1284                                                         dl->charidx= nu->charidx;
1285
1286                                                         /* dl->rt will be used as flag for render face and */
1287                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1288                                                         dl->rt= nu->flag & ~CU_2D;
1289
1290                                                         dl->bevelSplitFlag= MEM_callocN(sizeof(*dl->col2)*((bl->nr+0x1F)>>5), "bevelSplitFlag");
1291         
1292                                                         /* for each point of poly make a bevel piece */
1293                                                         bevp= (BevPoint *)(bl+1);
1294                                                         for(a=0; a<bl->nr; a++,bevp++) {
1295                                                                 float fac=1.0;
1296                                                                 if (cu->taperobj==NULL) {
1297                                                                         if ( (cu->bevobj!=NULL) || !((cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) )
1298                                                                                 fac = bevp->radius;
1299                                                                 } else {
1300                                                                         fac = calc_taper(scene, cu->taperobj, a, bl->nr);
1301                                                                 }
1302
1303                                                                 if (bevp->split_tag) {
1304                                                                         dl->bevelSplitFlag[a>>5] |= 1<<(a&0x1F);
1305                                                                 }
1306         
1307                                                                         /* rotate bevel piece and write in data */
1308                                                                 fp1= dlb->verts;
1309                                                                 for (b=0; b<dlb->nr; b++,fp1+=3,data+=3) {
1310                                                                         if(cu->flag & CU_3D) {
1311                                                                                 float vec[3];
1312         
1313                                                                                 vec[0]= fp1[1]+widfac;
1314                                                                                 vec[1]= fp1[2];
1315                                                                                 vec[2]= 0.0;
1316
1317                                                                                 mul_qt_v3(bevp->quat, vec);
1318
1319                                                                                 data[0]= bevp->vec[0] + fac*vec[0];
1320                                                                                 data[1]= bevp->vec[1] + fac*vec[1];
1321                                                                                 data[2]= bevp->vec[2] + fac*vec[2];
1322                                                                         }
1323                                                                         else {
1324                                                                                 data[0]= bevp->vec[0] + fac*(widfac+fp1[1])*bevp->sina;
1325                                                                                 data[1]= bevp->vec[1] + fac*(widfac+fp1[1])*bevp->cosa;
1326                                                                                 data[2]= bevp->vec[2] + fac*fp1[2];
1327                                                                         }
1328                                                                 }
1329                                                         }
1330                                                         
1331                                                         /* gl array drawing: using indices */
1332                                                         displist_surf_indices(dl);
1333                                                 }
1334                                         }
1335                                 }
1336
1337                         }
1338                         freedisplist(&dlbev);
1339                 }
1340
1341                 if (!(cu->flag & CU_DEFORM_FILL)) {
1342                         curve_to_filledpoly(cu, nubase, dispbase);
1343                 }
1344
1345                 if(cu->flag & CU_PATH) calc_curvepath(ob);
1346
1347                 /* make copy of 'undeformed" displist for texture space calculation
1348                    actually, it's not totally undeformed -- pre-tesselation modifiers are
1349                    already applied, thats how it worked for years, so keep for compatibility (sergey) */
1350                 copy_displist(&cu->disp, dispbase);
1351
1352                 if (!forRender) {
1353                         tex_space_curve(cu);
1354                 }
1355
1356                 if(!forOrco) curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal, forRender, originalVerts, deformedVerts);
1357
1358                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1359                         curve_to_filledpoly(cu, nubase, dispbase);
1360                 }
1361         }
1362 }
1363
1364 void makeDispListCurveTypes(Scene *scene, Object *ob, int forOrco)
1365 {
1366         Curve *cu= ob->data;
1367         ListBase *dispbase;
1368
1369         /* The same check for duplis as in do_makeDispListCurveTypes.
1370            Happens when curve used for constraint/bevel was converted to mesh.
1371            check there is still needed for render displist and orco displists. */
1372         if(!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1373
1374         freedisplist(&(ob->disp));
1375         dispbase= &(ob->disp);
1376         freedisplist(dispbase);
1377
1378         /* free displist used for textspace */
1379         freedisplist(&cu->disp);
1380
1381         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco);
1382
1383         if (ob->derivedFinal) {
1384                 DM_set_object_boundbox (ob, ob->derivedFinal);
1385         } else {
1386                 boundbox_displist (ob);
1387
1388                 /* if there is no derivedMesh, object's boundbox is unneeded */
1389                 if (ob->bb) {
1390                         MEM_freeN(ob->bb);
1391                         ob->bb= NULL;
1392                 }
1393         }
1394 }
1395
1396 void makeDispListCurveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1397         DerivedMesh **derivedFinal, int forOrco)
1398 {
1399         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco);
1400 }
1401
1402 void makeDispListCurveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1403 {
1404         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1);
1405 }
1406
1407 /* add Orco layer to the displist object which has got derived mesh and return orco */
1408 float *makeOrcoDispList(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1409 {
1410         float *orco;
1411
1412         if (derivedFinal == NULL)
1413                 derivedFinal= ob->derivedFinal;
1414
1415         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1416                 curve_calc_orcodm(scene, ob, derivedFinal, forRender);
1417         }
1418
1419         orco= derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1420
1421         if(orco) {
1422                 orco= MEM_dupallocN(orco);
1423         }
1424
1425         return orco;
1426 }
1427
1428 /* this is confusing, there's also min_max_object, appplying the obmat... */
1429 static void boundbox_displist(Object *ob)
1430 {
1431         BoundBox *bb=NULL;
1432         float min[3], max[3];
1433         DispList *dl;
1434         float *vert;
1435         int a, tot=0;
1436         
1437         INIT_MINMAX(min, max);
1438
1439         if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1440                 Curve *cu= ob->data;
1441                 int doit= 0;
1442
1443                 if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
1444                 bb= cu->bb;
1445                 
1446                 dl= ob->disp.first;
1447
1448                 while (dl) {
1449                         if(dl->type==DL_INDEX3) tot= dl->nr;
1450                         else tot= dl->nr*dl->parts;
1451                         
1452                         vert= dl->verts;
1453                         for(a=0; a<tot; a++, vert+=3) {
1454                                 doit= 1;
1455                                 DO_MINMAX(vert, min, max);
1456                         }
1457
1458                         dl= dl->next;
1459                 }
1460                 
1461                 if(!doit) {
1462                         /* there's no geometry in displist, use zero-sized boundbox */
1463                         zero_v3(min);
1464                         zero_v3(max);
1465                 }
1466                 
1467         }
1468         
1469         if(bb) {
1470                 boundbox_set_from_min_max(bb, min, max);
1471         }
1472 }
1473