Patch #34204: [Render Animation] Fails with "Error: Specified sample_fmt is not suppo...
[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_scanfill.h"
48 #include "BLI_utildefines.h"
49
50 #include "BKE_global.h"
51 #include "BKE_displist.h"
52 #include "BKE_cdderivedmesh.h"
53 #include "BKE_object.h"
54 #include "BKE_mball.h"
55 #include "BKE_material.h"
56 #include "BKE_curve.h"
57 #include "BKE_key.h"
58 #include "BKE_anim.h"
59 #include "BKE_font.h"
60 #include "BKE_lattice.h"
61 #include "BKE_modifier.h"
62
63 #include "BLO_sys_types.h" // for intptr_t support
64
65 static void boundbox_displist(Object *ob);
66 static void boundbox_dispbase(BoundBox *bb, ListBase *dispbase);
67
68 void BKE_displist_elem_free(DispList *dl)
69 {
70         if (dl) {
71                 if (dl->verts) MEM_freeN(dl->verts);
72                 if (dl->nors) MEM_freeN(dl->nors);
73                 if (dl->index) MEM_freeN(dl->index);
74                 if (dl->col1) MEM_freeN(dl->col1);
75                 if (dl->col2) MEM_freeN(dl->col2);
76                 if (dl->bevelSplitFlag) MEM_freeN(dl->bevelSplitFlag);
77                 MEM_freeN(dl);
78         }
79 }
80
81 void BKE_displist_free(ListBase *lb)
82 {
83         DispList *dl;
84
85         dl = lb->first;
86         while (dl) {
87                 BLI_remlink(lb, dl);
88                 BKE_displist_elem_free(dl);
89                 dl = lb->first;
90         }
91 }
92
93 DispList *BKE_displist_find_or_create(ListBase *lb, int type)
94 {
95         DispList *dl;
96
97         dl = lb->first;
98         while (dl) {
99                 if (dl->type == type)
100                         return dl;
101                 dl = dl->next;
102         }
103
104         dl = MEM_callocN(sizeof(DispList), "find_disp");
105         dl->type = type;
106         BLI_addtail(lb, dl);
107
108         return dl;
109 }
110
111 DispList *BKE_displist_find(ListBase *lb, int type)
112 {
113         DispList *dl;
114
115         dl = lb->first;
116         while (dl) {
117                 if (dl->type == type)
118                         return dl;
119                 dl = dl->next;
120         }
121
122         return NULL;
123 }
124
125 int BKE_displist_has_faces(ListBase *lb)
126 {
127         DispList *dl;
128
129         for (dl = lb->first; dl; dl = dl->next) {
130                 if (ELEM3(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)) {
131                         return TRUE;
132                 }
133         }
134
135         return FALSE;
136 }
137
138 void BKE_displist_copy(ListBase *lbn, ListBase *lb)
139 {
140         DispList *dln, *dl;
141
142         BKE_displist_free(lbn);
143
144         dl = lb->first;
145         while (dl) {
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 BKE_displist_normals_add(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         dl = lb->first;
170
171         while (dl) {
172                 if (dl->type == DL_INDEX3) {
173                         if (dl->nors == NULL) {
174                                 dl->nors = MEM_callocN(sizeof(float) * 3, "dlnors");
175
176                                 if (dl->verts[2] < 0.0f)
177                                         dl->nors[2] = -1.0f;
178                                 else
179                                         dl->nors[2] = 1.0f;
180                         }
181                 }
182                 else if (dl->type == DL_SURF) {
183                         if (dl->nors == NULL) {
184                                 dl->nors = MEM_callocN(sizeof(float) * 3 * dl->nr * dl->parts, "dlnors");
185
186                                 vdata = dl->verts;
187                                 ndata = dl->nors;
188
189                                 for (a = 0; a < dl->parts; a++) {
190
191                                         if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
192                                                 break;
193
194                                         v1 = vdata + 3 * p1;
195                                         n1 = ndata + 3 * p1;
196                                         v2 = vdata + 3 * p2;
197                                         n2 = ndata + 3 * p2;
198                                         v3 = vdata + 3 * p3;
199                                         n3 = ndata + 3 * p3;
200                                         v4 = vdata + 3 * p4;
201                                         n4 = ndata + 3 * p4;
202
203                                         for (; b < dl->nr; b++) {
204                                                 normal_quad_v3(nor, v1, v3, v4, v2);
205
206                                                 add_v3_v3(n1, nor);
207                                                 add_v3_v3(n2, nor);
208                                                 add_v3_v3(n3, nor);
209                                                 add_v3_v3(n4, nor);
210
211                                                 v2 = v1; v1 += 3;
212                                                 v4 = v3; v3 += 3;
213                                                 n2 = n1; n1 += 3;
214                                                 n4 = n3; n3 += 3;
215                                         }
216                                 }
217                                 a = dl->parts * dl->nr;
218                                 v1 = ndata;
219                                 while (a--) {
220                                         normalize_v3(v1);
221                                         v1 += 3;
222                                 }
223                         }
224                 }
225                 dl = dl->next;
226         }
227 }
228
229 void BKE_displist_count(ListBase *lb, int *totvert, int *totface, int *tottri)
230 {
231         DispList *dl;
232
233         for (dl = lb->first; dl; dl = dl->next) {
234                 int vert_tot = 0;
235                 int face_tot = 0;
236                 int tri_tot = 0;
237
238                 switch (dl->type) {
239                         case DL_SURF:
240                         {
241                                 vert_tot = dl->nr * dl->parts;
242                                 face_tot = (dl->nr - 1) * (dl->parts - 1);
243                                 tri_tot  = face_tot * 2;
244                                 break;
245                         }
246                         case DL_INDEX3:
247                         {
248                                 vert_tot = dl->nr;
249                                 face_tot = dl->parts;
250                                 tri_tot  = face_tot;
251                                 break;
252                         }
253                         case DL_INDEX4:
254                         {
255                                 vert_tot = dl->nr;
256                                 face_tot = dl->parts;
257                                 tri_tot  = face_tot * 2;
258                                 break;
259                         }
260                         case DL_POLY:
261                         case DL_SEGM:
262                         {
263                                 vert_tot = dl->nr * dl->parts;
264                                 break;
265                         }
266                 }
267
268                 *totvert += vert_tot;
269                 *totface += face_tot;
270                 *tottri  += tri_tot;
271         }
272 }
273
274 int BKE_displist_surfindex_get(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4)
275 {
276         if ((dl->flag & DL_CYCL_V) == 0 && a == (dl->parts) - 1) {
277                 return 0;
278         }
279
280         if (dl->flag & DL_CYCL_U) {
281                 (*p1) = dl->nr * a;
282                 (*p2) = (*p1) + dl->nr - 1;
283                 (*p3) = (*p1) + dl->nr;
284                 (*p4) = (*p2) + dl->nr;
285                 (*b) = 0;
286         }
287         else {
288                 (*p2) = dl->nr * a;
289                 (*p1) = (*p2) + 1;
290                 (*p4) = (*p2) + dl->nr;
291                 (*p3) = (*p1) + dl->nr;
292                 (*b) = 1;
293         }
294
295         if ((dl->flag & DL_CYCL_V) && a == dl->parts - 1) {
296                 (*p3) -= dl->nr * dl->parts;
297                 (*p4) -= dl->nr * dl->parts;
298         }
299
300         return 1;
301 }
302
303 /* ****************** make displists ********************* */
304
305 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase, int forRender)
306 {
307         Nurb *nu;
308         DispList *dl;
309         BezTriple *bezt, *prevbezt;
310         BPoint *bp;
311         float *data;
312         int a, len, resolu;
313         const int editmode = (!forRender && (cu->editnurb || cu->editfont));
314
315         nu = nubase->first;
316         while (nu) {
317                 if (nu->hide == 0 || editmode == 0) {
318                         if (forRender && cu->resolu_ren != 0)
319                                 resolu = cu->resolu_ren;
320                         else
321                                 resolu = nu->resolu;
322
323                         if (!BKE_nurb_check_valid_u(nu)) {
324                                 /* pass */
325                         }
326                         else if (nu->type == CU_BEZIER) {
327                                 /* count */
328                                 len = 0;
329                                 a = nu->pntsu - 1;
330                                 if (nu->flagu & CU_NURB_CYCLIC) a++;
331
332                                 prevbezt = nu->bezt;
333                                 bezt = prevbezt + 1;
334                                 while (a--) {
335                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC))
336                                                 bezt = nu->bezt;
337
338                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT)
339                                                 len++;
340                                         else
341                                                 len += resolu;
342
343                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC) == 0)
344                                                 len++;
345
346                                         prevbezt = bezt;
347                                         bezt++;
348                                 }
349
350                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbez");
351                                 /* len+1 because of 'forward_diff_bezier' function */
352                                 dl->verts = MEM_callocN((len + 1) * 3 * sizeof(float), "dlverts");
353                                 BLI_addtail(dispbase, dl);
354                                 dl->parts = 1;
355                                 dl->nr = len;
356                                 dl->col = nu->mat_nr;
357                                 dl->charidx = nu->charidx;
358
359                                 data = dl->verts;
360
361                                 if (nu->flagu & CU_NURB_CYCLIC) {
362                                         dl->type = DL_POLY;
363                                         a = nu->pntsu;
364                                 }
365                                 else {
366                                         dl->type = DL_SEGM;
367                                         a = nu->pntsu - 1;
368                                 }
369
370                                 prevbezt = nu->bezt;
371                                 bezt = prevbezt + 1;
372
373                                 while (a--) {
374                                         if (a == 0 && dl->type == DL_POLY)
375                                                 bezt = nu->bezt;
376
377                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
378                                                 copy_v3_v3(data, prevbezt->vec[1]);
379                                                 data += 3;
380                                         }
381                                         else {
382                                                 int j;
383                                                 for (j = 0; j < 3; j++) {
384                                                         BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],
385                                                                                       prevbezt->vec[2][j],
386                                                                                       bezt->vec[0][j],
387                                                                                       bezt->vec[1][j],
388                                                                                       data + j, resolu, 3 * sizeof(float));
389                                                 }
390
391                                                 data += 3 * resolu;
392                                         }
393
394                                         if (a == 0 && dl->type == DL_SEGM) {
395                                                 copy_v3_v3(data, bezt->vec[1]);
396                                         }
397
398                                         prevbezt = bezt;
399                                         bezt++;
400                                 }
401                         }
402                         else if (nu->type == CU_NURBS) {
403                                 len = (resolu * SEGMENTSU(nu));
404
405                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
406                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
407                                 BLI_addtail(dispbase, dl);
408                                 dl->parts = 1;
409
410                                 dl->nr = len;
411                                 dl->col = nu->mat_nr;
412                                 dl->charidx = nu->charidx;
413
414                                 data = dl->verts;
415                                 if (nu->flagu & CU_NURB_CYCLIC)
416                                         dl->type = DL_POLY;
417                                 else dl->type = DL_SEGM;
418                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
419                         }
420                         else if (nu->type == CU_POLY) {
421                                 len = nu->pntsu;
422                                 dl = MEM_callocN(sizeof(DispList), "makeDispListpoly");
423                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
424                                 BLI_addtail(dispbase, dl);
425                                 dl->parts = 1;
426                                 dl->nr = len;
427                                 dl->col = nu->mat_nr;
428                                 dl->charidx = nu->charidx;
429
430                                 data = dl->verts;
431                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
432                                 else dl->type = DL_SEGM;
433
434                                 a = len;
435                                 bp = nu->bp;
436                                 while (a--) {
437                                         copy_v3_v3(data, bp->vec);
438                                         bp++;
439                                         data += 3;
440                                 }
441                         }
442                 }
443                 nu = nu->next;
444         }
445 }
446
447
448 void BKE_displist_fill(ListBase *dispbase, ListBase *to, int flipnormal)
449 {
450         ScanFillContext sf_ctx;
451         ScanFillVert *sf_vert, *sf_vert_new, *sf_vert_last;
452         ScanFillFace *sf_tri;
453         DispList *dlnew = NULL, *dl;
454         float *f1;
455         int colnr = 0, charidx = 0, cont = 1, tot, a, *index, nextcol = 0;
456         intptr_t totvert;
457
458         if (dispbase == NULL)
459                 return;
460         if (dispbase->first == NULL)
461                 return;
462
463         while (cont) {
464                 cont = 0;
465                 totvert = 0;
466                 nextcol = 0;
467
468                 BLI_scanfill_begin(&sf_ctx);
469
470                 dl = dispbase->first;
471                 while (dl) {
472                         if (dl->type == DL_POLY) {
473                                 if (charidx < dl->charidx)
474                                         cont = 1;
475                                 else if (charidx == dl->charidx) { /* character with needed index */
476                                         if (colnr == dl->col) {
477                                                 /* make editverts and edges */
478                                                 f1 = dl->verts;
479                                                 a = dl->nr;
480                                                 sf_vert = sf_vert_new = NULL;
481
482                                                 while (a--) {
483                                                         sf_vert_last = sf_vert;
484
485                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, f1);
486                                                         totvert++;
487
488                                                         if (sf_vert_last == NULL)
489                                                                 sf_vert_new = sf_vert;
490                                                         else {
491                                                                 BLI_scanfill_edge_add(&sf_ctx, sf_vert_last, sf_vert);
492                                                         }
493                                                         f1 += 3;
494                                                 }
495
496                                                 if (sf_vert != NULL && sf_vert_new != NULL) {
497                                                         BLI_scanfill_edge_add(&sf_ctx, sf_vert, sf_vert_new);
498                                                 }
499                                         }
500                                         else if (colnr < dl->col) {
501                                                 /* got poly with next material at current char */
502                                                 cont = 1;
503                                                 nextcol = 1;
504                                         }
505                                 }
506                         }
507                         dl = dl->next;
508                 }
509
510                 /* XXX (obedit && obedit->actcol) ? (obedit->actcol-1) : 0)) { */
511                 if (totvert && (tot = BLI_scanfill_calc(&sf_ctx, BLI_SCANFILL_CALC_REMOVE_DOUBLES | BLI_SCANFILL_CALC_HOLES))) {
512                         if (tot) {
513                                 dlnew = MEM_callocN(sizeof(DispList), "filldisplist");
514                                 dlnew->type = DL_INDEX3;
515                                 dlnew->col = colnr;
516                                 dlnew->nr = totvert;
517                                 dlnew->parts = tot;
518
519                                 dlnew->index = MEM_mallocN(tot * 3 * sizeof(int), "dlindex");
520                                 dlnew->verts = MEM_mallocN(totvert * 3 * sizeof(float), "dlverts");
521
522                                 /* vert data */
523                                 f1 = dlnew->verts;
524                                 totvert = 0;
525
526                                 for (sf_vert = sf_ctx.fillvertbase.first; sf_vert; sf_vert = sf_vert->next) {
527                                         copy_v3_v3(f1, sf_vert->co);
528                                         f1 += 3;
529
530                                         /* index number */
531                                         sf_vert->tmp.l = totvert;
532                                         totvert++;
533                                 }
534
535                                 /* index data */
536                                 sf_tri = sf_ctx.fillfacebase.first;
537                                 index = dlnew->index;
538                                 while (sf_tri) {
539                                         index[0] = (intptr_t)sf_tri->v1->tmp.l;
540                                         index[1] = (intptr_t)sf_tri->v2->tmp.l;
541                                         index[2] = (intptr_t)sf_tri->v3->tmp.l;
542
543                                         if (flipnormal)
544                                                 SWAP(int, index[0], index[2]);
545
546                                         index += 3;
547                                         sf_tri = sf_tri->next;
548                                 }
549                         }
550
551                         BLI_addhead(to, dlnew);
552                 }
553                 BLI_scanfill_end(&sf_ctx);
554
555                 if (nextcol) {
556                         /* stay at current char but fill polys with next material */
557                         colnr++;
558                 }
559                 else {
560                         /* switch to next char and start filling from first material */
561                         charidx++;
562                         colnr = 0;
563                 }
564         }
565
566         /* do not free polys, needed for wireframe display */
567 }
568
569 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
570 {
571         ListBase front, back;
572         DispList *dl, *dlnew;
573         float *fp, *fp1;
574         int a, dpoly;
575
576         front.first = front.last = back.first = back.last = NULL;
577
578         dl = dispbase->first;
579         while (dl) {
580                 if (dl->type == DL_SURF) {
581                         if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U) == 0) {
582                                 if ( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE)) {
583                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
584                                         BLI_addtail(&front, dlnew);
585                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
586                                         dlnew->nr = dl->parts;
587                                         dlnew->parts = 1;
588                                         dlnew->type = DL_POLY;
589                                         dlnew->col = dl->col;
590                                         dlnew->charidx = dl->charidx;
591
592                                         fp = dl->verts;
593                                         dpoly = 3 * dl->nr;
594
595                                         a = dl->parts;
596                                         while (a--) {
597                                                 copy_v3_v3(fp1, fp);
598                                                 fp1 += 3;
599                                                 fp += dpoly;
600                                         }
601                                 }
602                                 if ((cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE)) {
603                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
604                                         BLI_addtail(&back, dlnew);
605                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
606                                         dlnew->nr = dl->parts;
607                                         dlnew->parts = 1;
608                                         dlnew->type = DL_POLY;
609                                         dlnew->col = dl->col;
610                                         dlnew->charidx = dl->charidx;
611
612                                         fp = dl->verts + 3 * (dl->nr - 1);
613                                         dpoly = 3 * dl->nr;
614
615                                         a = dl->parts;
616                                         while (a--) {
617                                                 copy_v3_v3(fp1, fp);
618                                                 fp1 += 3;
619                                                 fp += dpoly;
620                                         }
621                                 }
622                         }
623                 }
624                 dl = dl->next;
625         }
626
627         BKE_displist_fill(&front, dispbase, 1);
628         BKE_displist_fill(&back, dispbase, 0);
629
630         BKE_displist_free(&front);
631         BKE_displist_free(&back);
632
633         BKE_displist_fill(dispbase, dispbase, 0);
634 }
635
636 static void curve_to_filledpoly(Curve *cu, ListBase *UNUSED(nurb), ListBase *dispbase)
637 {
638         if (cu->flag & CU_3D)
639                 return;
640
641         if (dispbase->first && ((DispList *) dispbase->first)->type == DL_SURF) {
642                 bevels_to_filledpoly(cu, dispbase);
643         }
644         else {
645                 BKE_displist_fill(dispbase, dispbase, 0);
646         }
647 }
648
649 /* taper rules:
650  * - only 1 curve
651  * - first point left, last point right
652  * - based on subdivided points in original curve, not on points in taper curve (still)
653  */
654 static float displist_calc_taper(Scene *scene, Object *taperobj, float fac)
655 {
656         DispList *dl;
657
658         if (taperobj == NULL || taperobj->type != OB_CURVE)
659                 return 1.0;
660
661         dl = taperobj->disp.first;
662         if (dl == NULL) {
663                 BKE_displist_make_curveTypes(scene, taperobj, 0);
664                 dl = taperobj->disp.first;
665         }
666         if (dl) {
667                 float minx, dx, *fp;
668                 int a;
669
670                 /* horizontal size */
671                 minx = dl->verts[0];
672                 dx = dl->verts[3 * (dl->nr - 1)] - minx;
673                 if (dx > 0.0f) {
674                         fp = dl->verts;
675                         for (a = 0; a < dl->nr; a++, fp += 3) {
676                                 if ((fp[0] - minx) / dx >= fac) {
677                                         /* interpolate with prev */
678                                         if (a > 0) {
679                                                 float fac1 = (fp[-3] - minx) / dx;
680                                                 float fac2 = (fp[0] - minx) / dx;
681                                                 if (fac1 != fac2)
682                                                         return fp[1] * (fac1 - fac) / (fac1 - fac2) + fp[-2] * (fac - fac2) / (fac1 - fac2);
683                                         }
684                                         return fp[1];
685                                 }
686                         }
687                         return fp[-2];  // last y coord
688                 }
689         }
690
691         return 1.0;
692 }
693
694 float BKE_displist_calc_taper(Scene *scene, Object *taperobj, int cur, int tot)
695 {
696         float fac = ((float)cur) / (float)(tot - 1);
697
698         return displist_calc_taper(scene, taperobj, fac);
699 }
700
701 void BKE_displist_make_mball(Scene *scene, Object *ob)
702 {
703         if (!ob || ob->type != OB_MBALL)
704                 return;
705
706         /* XXX: mball stuff uses plenty of global variables
707          *      while this is unchanged updating during render is unsafe
708          */
709         if (G.is_rendering)
710                 return;
711
712         BKE_displist_free(&(ob->disp));
713
714         if (ob->type == OB_MBALL) {
715                 if (ob == BKE_mball_basis_find(scene, ob)) {
716                         BKE_mball_polygonize(scene, ob, &ob->disp);
717                         BKE_mball_texspace_calc(ob);
718
719                         object_deform_mball(ob, &ob->disp);
720                 }
721         }
722
723         boundbox_displist(ob);
724 }
725
726 void BKE_displist_make_mball_forRender(Scene *scene, Object *ob, ListBase *dispbase)
727 {
728         BKE_mball_polygonize(scene, ob, dispbase);
729         BKE_mball_texspace_calc(ob);
730
731         object_deform_mball(ob, dispbase);
732 }
733
734 static ModifierData *curve_get_tessellate_point(Scene *scene, Object *ob, int forRender, int editmode)
735 {
736         ModifierData *md = modifiers_getVirtualModifierList(ob);
737         ModifierData *pretessellatePoint;
738         int required_mode;
739
740         if (forRender)
741                 required_mode = eModifierMode_Render;
742         else
743                 required_mode = eModifierMode_Realtime;
744
745         if (editmode)
746                 required_mode |= eModifierMode_Editmode;
747
748         pretessellatePoint = NULL;
749         for (; md; md = md->next) {
750                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
751
752                 if (!modifier_isEnabled(scene, md, required_mode))
753                         continue;
754                 if (mti->type == eModifierTypeType_Constructive)
755                         return pretessellatePoint;
756
757                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
758                         pretessellatePoint = md;
759
760                         /* this modifiers are moving point of tessellation automatically
761                          * (some of them even can't be applied on tessellated curve), set flag
762                          * for information button in modifier's header
763                          */
764                         md->mode |= eModifierMode_ApplyOnSpline;
765                 }
766                 else if (md->mode & eModifierMode_ApplyOnSpline) {
767                         pretessellatePoint = md;
768                 }
769         }
770
771         return pretessellatePoint;
772 }
773
774 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, int forRender, float (**originalVerts_r)[3],
775                                      float (**deformedVerts_r)[3], int *numVerts_r)
776 {
777         ModifierData *md = modifiers_getVirtualModifierList(ob);
778         ModifierData *pretessellatePoint;
779         Curve *cu = ob->data;
780         ListBase *nurb = BKE_curve_nurbs_get(cu);
781         int numVerts = 0;
782         const int editmode = (!forRender && (cu->editnurb || cu->editfont));
783         ModifierApplyFlag app_flag = 0;
784         float (*originalVerts)[3] = NULL;
785         float (*deformedVerts)[3] = NULL;
786         float *keyVerts = NULL;
787         int required_mode;
788
789         if (editmode)
790                 app_flag |= MOD_APPLY_USECACHE;
791         if (forRender) {
792                 app_flag |= MOD_APPLY_RENDER;
793                 required_mode = eModifierMode_Render;
794         }
795         else
796                 required_mode = eModifierMode_Realtime;
797
798         pretessellatePoint = curve_get_tessellate_point(scene, ob, forRender, editmode);
799
800         if (editmode)
801                 required_mode |= eModifierMode_Editmode;
802
803         if (cu->editnurb == NULL) {
804                 keyVerts = BKE_key_evaluate_object(scene, ob, &numVerts);
805
806                 if (keyVerts) {
807                         /* split coords from key data, the latter also includes
808                          * tilts, which is passed through in the modifier stack.
809                          * this is also the reason curves do not use a virtual
810                          * shape key modifier yet. */
811                         deformedVerts = BKE_curve_keyVertexCos_get(cu, nurb, keyVerts);
812                         originalVerts = MEM_dupallocN(deformedVerts);
813                         BLI_assert(BKE_nurbList_verts_count(nurb) == numVerts);
814                 }
815         }
816
817         if (pretessellatePoint) {
818                 for (; md; md = md->next) {
819                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
820
821                         md->scene = scene;
822
823                         if (!modifier_isEnabled(scene, md, required_mode))
824                                 continue;
825                         if (mti->type != eModifierTypeType_OnlyDeform)
826                                 continue;
827
828                         if (!deformedVerts) {
829                                 deformedVerts = BKE_curve_vertexCos_get(cu, nurb, &numVerts);
830                                 originalVerts = MEM_dupallocN(deformedVerts);
831                         }
832
833                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, app_flag);
834
835                         if (md == pretessellatePoint)
836                                 break;
837                 }
838         }
839
840         if (deformedVerts)
841                 BK_curve_vertexCos_apply(cu, nurb, deformedVerts);
842         if (keyVerts) /* these are not passed through modifier stack */
843                 BKE_curve_keyVertexTilts_apply(cu, nurb, keyVerts);
844
845         if (keyVerts)
846                 MEM_freeN(keyVerts);
847
848         *originalVerts_r = originalVerts;
849         *deformedVerts_r = deformedVerts;
850         *numVerts_r = numVerts;
851 }
852
853 static float (*displist_get_allverts(ListBase * dispbase, int *totvert))[3]
854 {
855         DispList *dl;
856         float (*allverts)[3], *fp;
857
858         *totvert = 0;
859
860         for (dl = dispbase->first; dl; dl = dl->next)
861                 *totvert += (dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr;
862
863         allverts = MEM_mallocN((*totvert) * sizeof(float) * 3, "displist_get_allverts allverts");
864         fp = (float *)allverts;
865         for (dl = dispbase->first; dl; dl = dl->next) {
866                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
867                 memcpy(fp, dl->verts, sizeof(float) * offs);
868                 fp += offs;
869         }
870
871         return allverts;
872 }
873
874 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
875 {
876         DispList *dl;
877         float *fp;
878
879         fp = (float *)allverts;
880         for (dl = dispbase->first; dl; dl = dl->next) {
881                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
882                 memcpy(dl->verts, fp, sizeof(float) * offs);
883                 fp += offs;
884         }
885 }
886
887 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispbase, DerivedMesh **derivedFinal,
888                                       int forRender, float (*originalVerts)[3], float (*deformedVerts)[3])
889 {
890         ModifierData *md = modifiers_getVirtualModifierList(ob);
891         ModifierData *pretessellatePoint;
892         Curve *cu = ob->data;
893         ListBase *nurb = BKE_curve_nurbs_get(cu);
894         int required_mode = 0, totvert = 0;
895         int editmode = (!forRender && (cu->editnurb || cu->editfont));
896         DerivedMesh *dm = NULL, *ndm;
897         float (*vertCos)[3] = NULL;
898         int useCache = !forRender;
899         ModifierApplyFlag app_flag = 0;
900
901         if (forRender) {
902                 app_flag |= MOD_APPLY_RENDER;
903                 required_mode = eModifierMode_Render;
904         }
905         else
906                 required_mode = eModifierMode_Realtime;
907
908         pretessellatePoint = curve_get_tessellate_point(scene, ob, forRender, editmode);
909
910         if (editmode)
911                 required_mode |= eModifierMode_Editmode;
912
913         if (pretessellatePoint) {
914                 md = pretessellatePoint->next;
915         }
916
917         if (derivedFinal && *derivedFinal) {
918                 (*derivedFinal)->release(*derivedFinal);
919         }
920
921         for (; md; md = md->next) {
922                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
923                 ModifierApplyFlag appf = app_flag;
924
925                 md->scene = scene;
926
927                 if (!modifier_isEnabled(scene, md, required_mode))
928                         continue;
929
930                 if (mti->type == eModifierTypeType_OnlyDeform ||
931                     (mti->type == eModifierTypeType_DeformOrConstruct && !dm))
932                 {
933                         if (editmode)
934                                 appf |= MOD_APPLY_USECACHE;
935                         if (dm) {
936                                 if (!vertCos) {
937                                         totvert = dm->getNumVerts(dm);
938                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
939                                         dm->getVertCos(dm, vertCos);
940                                 }
941
942                                 mti->deformVerts(md, ob, dm, vertCos, totvert, appf);
943                         }
944                         else {
945                                 if (!vertCos) {
946                                         vertCos = displist_get_allverts(dispbase, &totvert);
947                                 }
948
949                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, appf);
950                         }
951                 }
952                 else {
953                         if (!derivedFinal) {
954                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh
955                                  * is totally unnecessary, so we could stop modifiers applying
956                                  * when we found constructive modifier but derived mesh is unwanted result
957                                  */
958                                 break;
959                         }
960
961                         if (dm) {
962                                 if (vertCos) {
963                                         DerivedMesh *tdm = CDDM_copy(dm);
964                                         dm->release(dm);
965                                         dm = tdm;
966
967                                         CDDM_apply_vert_coords(dm, vertCos);
968                                         CDDM_calc_normals_mapping(dm);
969                                 }
970                         }
971                         else {
972                                 if (vertCos) {
973                                         displist_apply_allverts(dispbase, vertCos);
974                                 }
975
976                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
977                                         curve_to_filledpoly(cu, nurb, dispbase);
978                                 }
979
980                                 dm = CDDM_from_curve_displist(ob, dispbase);
981
982                                 CDDM_calc_normals_mapping(dm);
983                         }
984
985                         if (vertCos) {
986                                 /* Vertex coordinates were applied to necessary data, could free it */
987                                 MEM_freeN(vertCos);
988                                 vertCos = NULL;
989                         }
990
991                         if (useCache)
992                                 appf |= MOD_APPLY_USECACHE;
993                         ndm = mti->applyModifier(md, ob, dm, appf);
994
995                         if (ndm) {
996                                 /* Modifier returned a new derived mesh */
997
998                                 if (dm && dm != ndm) /* Modifier  */
999                                         dm->release(dm);
1000                                 dm = ndm;
1001                         }
1002                 }
1003         }
1004
1005         if (vertCos) {
1006                 if (dm) {
1007                         DerivedMesh *tdm = CDDM_copy(dm);
1008                         dm->release(dm);
1009                         dm = tdm;
1010
1011                         CDDM_apply_vert_coords(dm, vertCos);
1012                         CDDM_calc_normals_mapping(dm);
1013                         MEM_freeN(vertCos);
1014                 }
1015                 else {
1016                         displist_apply_allverts(dispbase, vertCos);
1017                         MEM_freeN(vertCos);
1018                         vertCos = NULL;
1019                 }
1020         }
1021
1022         if (derivedFinal) {
1023                 if (dm)
1024                         DM_ensure_tessface(dm);  /* needed for drawing */
1025                 (*derivedFinal) = dm;
1026         }
1027
1028         if (deformedVerts) {
1029                 BK_curve_vertexCos_apply(ob->data, nurb, originalVerts);
1030                 MEM_freeN(originalVerts);
1031                 MEM_freeN(deformedVerts);
1032         }
1033 }
1034
1035 static void displist_surf_indices(DispList *dl)
1036 {
1037         int a, b, p1, p2, p3, p4;
1038         int *index;
1039
1040         dl->totindex = 0;
1041
1042         index = dl->index = MEM_mallocN(4 * sizeof(int) * (dl->parts + 1) * (dl->nr + 1), "index array nurbs");
1043
1044         for (a = 0; a < dl->parts; a++) {
1045
1046                 if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
1047                         break;
1048
1049                 for (; b < dl->nr; b++, index += 4) {
1050                         index[0] = p1;
1051                         index[1] = p2;
1052                         index[2] = p4;
1053                         index[3] = p3;
1054
1055                         dl->totindex++;
1056
1057                         p2 = p1; p1++;
1058                         p4 = p3; p3++;
1059                 }
1060         }
1061 }
1062
1063 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
1064 {
1065         DerivedMesh *dm;
1066         ListBase disp = {NULL, NULL};
1067
1068         /* OrcoDM should be created from underformed disp lists */
1069         BKE_displist_make_curveTypes_forOrco(scene, ob, &disp);
1070         dm = CDDM_from_curve_displist(ob, &disp);
1071
1072         BKE_displist_free(&disp);
1073
1074         return dm;
1075 }
1076
1077 static void add_orco_dm(Scene *scene, Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1078 {
1079         float (*orco)[3], (*layerorco)[3];
1080         int totvert, a;
1081         Curve *cu = ob->data;
1082
1083         totvert = dm->getNumVerts(dm);
1084
1085         if (orcodm) {
1086                 orco = MEM_callocN(sizeof(float) * 3 * totvert, "dm orco");
1087
1088                 if (orcodm->getNumVerts(orcodm) == totvert)
1089                         orcodm->getVertCos(orcodm, orco);
1090                 else
1091                         dm->getVertCos(dm, orco);
1092         }
1093         else {
1094                 orco = (float(*)[3])BKE_curve_make_orco(scene, ob);
1095         }
1096
1097         for (a = 0; a < totvert; a++) {
1098                 float *co = orco[a];
1099                 co[0] = (co[0] - cu->loc[0]) / cu->size[0];
1100                 co[1] = (co[1] - cu->loc[1]) / cu->size[1];
1101                 co[2] = (co[2] - cu->loc[2]) / cu->size[2];
1102         }
1103
1104         if ((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1105                 memcpy(layerorco, orco, sizeof(float) * totvert);
1106                 MEM_freeN(orco);
1107         }
1108         else
1109                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1110 }
1111
1112 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1113 {
1114         /* this function represents logic of mesh's orcodm calculation
1115          * for displist-based objects
1116          */
1117
1118         ModifierData *md = modifiers_getVirtualModifierList(ob);
1119         ModifierData *pretessellatePoint;
1120         Curve *cu = ob->data;
1121         int required_mode;
1122         int editmode = (!forRender && (cu->editnurb || cu->editfont));
1123         DerivedMesh *ndm, *orcodm = NULL;
1124         const ModifierApplyFlag app_flag = forRender ? MOD_APPLY_RENDER : 0;
1125
1126         if (forRender)
1127                 required_mode = eModifierMode_Render;
1128         else
1129                 required_mode = eModifierMode_Realtime;
1130
1131         pretessellatePoint = curve_get_tessellate_point(scene, ob, forRender, editmode);
1132
1133         if (editmode)
1134                 required_mode |= eModifierMode_Editmode;
1135
1136         if (pretessellatePoint) {
1137                 md = pretessellatePoint->next;
1138         }
1139
1140         for (; md; md = md->next) {
1141                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1142
1143                 md->scene = scene;
1144
1145                 if (!modifier_isEnabled(scene, md, required_mode))
1146                         continue;
1147                 if (mti->type != eModifierTypeType_Constructive)
1148                         continue;
1149
1150                 if (!orcodm)
1151                         orcodm = create_orco_dm(scene, ob);
1152
1153                 ndm = mti->applyModifier(md, ob, orcodm, app_flag);
1154
1155                 if (ndm) {
1156                         /* if the modifier returned a new dm, release the old one */
1157                         if (orcodm && orcodm != ndm) {
1158                                 orcodm->release(orcodm);
1159                         }
1160                         orcodm = ndm;
1161                 }
1162         }
1163
1164         /* add an orco layer if needed */
1165         add_orco_dm(scene, ob, derivedFinal, orcodm);
1166
1167         if (orcodm)
1168                 orcodm->release(orcodm);
1169 }
1170
1171 void BKE_displist_make_surf(Scene *scene, Object *ob, ListBase *dispbase,
1172                             DerivedMesh **derivedFinal, int forRender, int forOrco)
1173 {
1174         ListBase *nubase;
1175         Nurb *nu;
1176         Curve *cu = ob->data;
1177         DispList *dl;
1178         float *data;
1179         int len;
1180         int numVerts;
1181         float (*originalVerts)[3];
1182         float (*deformedVerts)[3];
1183
1184         if (!forRender && cu->editnurb)
1185                 nubase = BKE_curve_editNurbs_get(cu);
1186         else
1187                 nubase = &cu->nurb;
1188
1189         if (!forOrco)
1190                 curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1191
1192         for (nu = nubase->first; nu; nu = nu->next) {
1193                 if (forRender || nu->hide == 0) {
1194                         int resolu = nu->resolu, resolv = nu->resolv;
1195
1196                         if (forRender) {
1197                                 if (cu->resolu_ren)
1198                                         resolu = cu->resolu_ren;
1199                                 if (cu->resolv_ren)
1200                                         resolv = cu->resolv_ren;
1201                         }
1202
1203                         if (nu->pntsv == 1) {
1204                                 len = SEGMENTSU(nu) * resolu;
1205
1206                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1207                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
1208
1209                                 BLI_addtail(dispbase, dl);
1210                                 dl->parts = 1;
1211                                 dl->nr = len;
1212                                 dl->col = nu->mat_nr;
1213                                 dl->charidx = nu->charidx;
1214
1215                                 /* dl->rt will be used as flag for render face and */
1216                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1217                                 dl->rt = nu->flag & ~CU_2D;
1218
1219                                 data = dl->verts;
1220                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
1221                                 else dl->type = DL_SEGM;
1222
1223                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
1224                         }
1225                         else {
1226                                 len = (nu->pntsu * resolu) * (nu->pntsv * resolv);
1227
1228                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1229                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
1230                                 BLI_addtail(dispbase, dl);
1231
1232                                 dl->col = nu->mat_nr;
1233                                 dl->charidx = nu->charidx;
1234
1235                                 /* dl->rt will be used as flag for render face and */
1236                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1237                                 dl->rt = nu->flag & ~CU_2D;
1238
1239                                 data = dl->verts;
1240                                 dl->type = DL_SURF;
1241
1242                                 dl->parts = (nu->pntsu * resolu);  /* in reverse, because makeNurbfaces works that way */
1243                                 dl->nr = (nu->pntsv * resolv);
1244                                 if (nu->flagv & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_U;  /* reverse too! */
1245                                 if (nu->flagu & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_V;
1246
1247                                 BKE_nurb_makeFaces(nu, data, 0, resolu, resolv);
1248
1249                                 /* gl array drawing: using indices */
1250                                 displist_surf_indices(dl);
1251                         }
1252                 }
1253         }
1254
1255         /* make copy of 'undeformed" displist for texture space calculation
1256          * actually, it's not totally undeformed -- pre-tessellation modifiers are
1257          * already applied, thats how it worked for years, so keep for compatibility (sergey) */
1258         BKE_displist_copy(&cu->disp, dispbase);
1259
1260         if (!forRender) {
1261                 BKE_curve_texspace_calc(cu);
1262         }
1263
1264         if (!forOrco) {
1265                 curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal,
1266                                           forRender, originalVerts, deformedVerts);
1267         }
1268 }
1269
1270 static void rotateBevelPiece(Curve *cu, BevPoint *bevp, BevPoint *nbevp, DispList *dlb, float bev_blend, float widfac, float fac, float **data_r)
1271 {
1272         float *fp, *data = *data_r;
1273         int b;
1274
1275         fp = dlb->verts;
1276         for (b = 0; b < dlb->nr; b++, fp += 3, data += 3) {
1277                 if (cu->flag & CU_3D) {
1278                         float vec[3], quat[4];
1279
1280                         vec[0] = fp[1] + widfac;
1281                         vec[1] = fp[2];
1282                         vec[2] = 0.0;
1283
1284                         if (nbevp == NULL) {
1285                                 copy_v3_v3(data, bevp->vec);
1286                                 copy_qt_qt(quat, bevp->quat);
1287                         }
1288                         else {
1289                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1290                                 interp_qt_qtqt(quat, bevp->quat, nbevp->quat, bev_blend);
1291                         }
1292
1293                         mul_qt_v3(quat, vec);
1294
1295                         data[0] += fac * vec[0];
1296                         data[1] += fac * vec[1];
1297                         data[2] += fac * vec[2];
1298                 }
1299                 else {
1300                         float sina, cosa;
1301
1302                         if (nbevp == NULL) {
1303                                 copy_v3_v3(data, bevp->vec);
1304                                 sina = bevp->sina;
1305                                 cosa = bevp->cosa;
1306                         }
1307                         else {
1308                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1309
1310                                 /* perhaps we need to interpolate angles instead. but the thing is
1311                                  * cosa and sina are not actually sine and cosine
1312                                  */
1313                                 sina = nbevp->sina * bev_blend + bevp->sina * (1.0f - bev_blend);
1314                                 cosa = nbevp->cosa * bev_blend + bevp->cosa * (1.0f - bev_blend);
1315                         }
1316
1317                         data[0] += fac * (widfac + fp[1]) * sina;
1318                         data[1] += fac * (widfac + fp[1]) * cosa;
1319                         data[2] += fac * fp[2];
1320                 }
1321         }
1322
1323         *data_r = data;
1324 }
1325
1326 static void fillBevelCap(Nurb *nu, DispList *dlb, float *prev_fp, ListBase *dispbase)
1327 {
1328         DispList *dl;
1329         float *data;
1330         int b;
1331
1332         dl = MEM_callocN(sizeof(DispList), "makeDispListbev2");
1333         dl->verts = data = MEM_callocN(3 * sizeof(float) * dlb->nr, "dlverts");
1334
1335         dl->type = DL_POLY;
1336
1337         dl->parts = 1;
1338         dl->nr = dlb->nr;
1339         dl->col = nu->mat_nr;
1340         dl->charidx = nu->charidx;
1341
1342         /* dl->rt will be used as flag for render face and */
1343         /* CU_2D conflicts with R_NOPUNOFLIP */
1344         dl->rt = nu->flag & ~CU_2D;
1345
1346         for (b = 0; b < dlb->nr; b++, prev_fp += 3, data += 3)
1347                 copy_v3_v3(data, prev_fp);
1348
1349         BLI_addtail(dispbase, dl);
1350 }
1351
1352 static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1353                                       DerivedMesh **derivedFinal, int forRender, int forOrco)
1354 {
1355         Curve *cu = ob->data;
1356
1357         /* we do allow duplis... this is only displist on curve level */
1358         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1359
1360         if (ob->type == OB_SURF) {
1361                 BKE_displist_make_surf(scene, ob, dispbase, derivedFinal, forRender, forOrco);
1362         }
1363         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1364                 ListBase dlbev;
1365                 ListBase *nubase;
1366                 float (*originalVerts)[3];
1367                 float (*deformedVerts)[3];
1368                 int numVerts;
1369
1370                 nubase = BKE_curve_nurbs_get(cu);
1371
1372                 BLI_freelistN(&(cu->bev));
1373
1374                 if (cu->path) free_path(cu->path);
1375                 cu->path = NULL;
1376
1377                 if (ob->type == OB_FONT)
1378                         BKE_vfont_to_curve(G.main, scene, ob, 0);
1379
1380                 if (!forOrco)
1381                         curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1382
1383                 BKE_curve_bevelList_make(ob);
1384
1385                 /* If curve has no bevel will return nothing */
1386                 BKE_curve_bevel_make(scene, ob, &dlbev, forRender);
1387
1388                 /* no bevel or extrude, and no width correction? */
1389                 if (!dlbev.first && cu->width == 1.0f) {
1390                         curve_to_displist(cu, nubase, dispbase, forRender);
1391                 }
1392                 else {
1393                         float widfac = cu->width - 1.0f;
1394                         BevList *bl = cu->bev.first;
1395                         Nurb *nu = nubase->first;
1396
1397                         for (; bl && nu; bl = bl->next, nu = nu->next) {
1398                                 DispList *dl;
1399                                 float *data;
1400                                 BevPoint *bevp;
1401                                 int a;
1402
1403                                 if (bl->nr) { /* blank bevel lists can happen */
1404
1405                                         /* exception handling; curve without bevel or extrude, with width correction */
1406                                         if (dlbev.first == NULL) {
1407                                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbev");
1408                                                 dl->verts = MEM_callocN(3 * sizeof(float) * bl->nr, "dlverts");
1409                                                 BLI_addtail(dispbase, dl);
1410
1411                                                 if (bl->poly != -1) dl->type = DL_POLY;
1412                                                 else dl->type = DL_SEGM;
1413
1414                                                 if (dl->type == DL_SEGM) dl->flag = (DL_FRONT_CURVE | DL_BACK_CURVE);
1415
1416                                                 dl->parts = 1;
1417                                                 dl->nr = bl->nr;
1418                                                 dl->col = nu->mat_nr;
1419                                                 dl->charidx = nu->charidx;
1420
1421                                                 /* dl->rt will be used as flag for render face and */
1422                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1423                                                 dl->rt = nu->flag & ~CU_2D;
1424
1425                                                 a = dl->nr;
1426                                                 bevp = (BevPoint *)(bl + 1);
1427                                                 data = dl->verts;
1428                                                 while (a--) {
1429                                                         data[0] = bevp->vec[0] + widfac * bevp->sina;
1430                                                         data[1] = bevp->vec[1] + widfac * bevp->cosa;
1431                                                         data[2] = bevp->vec[2];
1432                                                         bevp++;
1433                                                         data += 3;
1434                                                 }
1435                                         }
1436                                         else {
1437                                                 DispList *dlb;
1438                                                 ListBase bottom_capbase = {NULL, NULL};
1439                                                 ListBase top_capbase = {NULL, NULL};
1440
1441                                                 for (dlb = dlbev.first; dlb; dlb = dlb->next) {
1442                                                         const float bevfac1 = min_ff(cu->bevfac1, cu->bevfac2);
1443                                                         const float bevfac2 = max_ff(cu->bevfac1, cu->bevfac2);
1444                                                         float firstblend = 0.0f, lastblend = 0.0f;
1445                                                         int i, start, steps;
1446
1447                                                         if (bevfac2 - bevfac1 == 0.0f)
1448                                                                 continue;
1449
1450                                                         start = (int)(bevfac1 * (bl->nr - 1));
1451                                                         steps = 2 + (int)((bevfac2) * (bl->nr - 1)) - start;
1452                                                         firstblend = 1.0f - (bevfac1 * (bl->nr - 1) - (int)(bevfac1 * (bl->nr - 1)));
1453                                                         lastblend  =         bevfac2 * (bl->nr - 1) - (int)(bevfac2 * (bl->nr - 1));
1454
1455                                                         if (start + steps > bl->nr) {
1456                                                                 steps = bl->nr - start;
1457                                                                 lastblend = 1.0f;
1458                                                         }
1459
1460                                                         /* for each part of the bevel use a separate displblock */
1461                                                         dl = MEM_callocN(sizeof(DispList), "makeDispListbev1");
1462                                                         dl->verts = data = MEM_callocN(3 * sizeof(float) * dlb->nr * steps, "dlverts");
1463                                                         BLI_addtail(dispbase, dl);
1464
1465                                                         dl->type = DL_SURF;
1466
1467                                                         dl->flag = dlb->flag & (DL_FRONT_CURVE | DL_BACK_CURVE);
1468                                                         if (dlb->type == DL_POLY) dl->flag |= DL_CYCL_U;
1469                                                         if (bl->poly >= 0) dl->flag |= DL_CYCL_V;
1470
1471                                                         dl->parts = steps;
1472                                                         dl->nr = dlb->nr;
1473                                                         dl->col = nu->mat_nr;
1474                                                         dl->charidx = nu->charidx;
1475
1476                                                         /* dl->rt will be used as flag for render face and */
1477                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1478                                                         dl->rt = nu->flag & ~CU_2D;
1479
1480                                                         dl->bevelSplitFlag = MEM_callocN(sizeof(*dl->col2) * ((steps + 0x1F) >> 5),
1481                                                                                          "bevelSplitFlag");
1482
1483                                                         /* for each point of poly make a bevel piece */
1484                                                         bevp = (BevPoint *)(bl + 1) + start;
1485                                                         for (i = start, a = 0; a < steps; i++, bevp++, a++) {
1486                                                                 float fac = 1.0;
1487                                                                 float *cur_data = data;
1488
1489                                                                 if (cu->taperobj == NULL) {
1490                                                                         fac = bevp->radius;
1491                                                                 }
1492                                                                 else {
1493                                                                         float len, taper_fac;
1494
1495                                                                         if (cu->flag & CU_MAP_TAPER) {
1496                                                                                 len = (steps - 3) + firstblend + lastblend;
1497
1498                                                                                 if (a == 0)
1499                                                                                         taper_fac = 0.0f;
1500                                                                                 else if (a == steps - 1)
1501                                                                                         taper_fac = 1.0f;
1502                                                                                 else
1503                                                                                         taper_fac = ((float) a - (1.0f - firstblend)) / len;
1504                                                                         }
1505                                                                         else {
1506                                                                                 len = bl->nr - 1;
1507                                                                                 taper_fac = (float) i / len;
1508
1509                                                                                 if (a == 0)
1510                                                                                         taper_fac += (1.0f - firstblend) / len;
1511                                                                                 else if (a == steps - 1)
1512                                                                                         taper_fac -= (1.0f - lastblend) / len;
1513                                                                         }
1514
1515                                                                         fac = displist_calc_taper(scene, cu->taperobj, taper_fac);
1516                                                                 }
1517
1518                                                                 if (bevp->split_tag) {
1519                                                                         dl->bevelSplitFlag[a >> 5] |= 1 << (a & 0x1F);
1520                                                                 }
1521
1522                                                                 /* rotate bevel piece and write in data */
1523                                                                 if (a == 0)
1524                                                                         rotateBevelPiece(cu, bevp, bevp + 1, dlb, 1.0f - firstblend, widfac, fac, &data);
1525                                                                 else if (a == steps - 1)
1526                                                                         rotateBevelPiece(cu, bevp, bevp - 1, dlb, 1.0f - lastblend, widfac, fac, &data);
1527                                                                 else
1528                                                                         rotateBevelPiece(cu, bevp, NULL, dlb, 0.0f, widfac, fac, &data);
1529
1530                                                                 if (cu->bevobj && (cu->flag & CU_FILL_CAPS)) {
1531                                                                         if (a == 1)
1532                                                                                 fillBevelCap(nu, dlb, cur_data - 3 * dlb->nr, &bottom_capbase);
1533                                                                         if (a == steps - 1)
1534                                                                                 fillBevelCap(nu, dlb, cur_data, &top_capbase);
1535                                                                 }
1536                                                         }
1537
1538                                                         /* gl array drawing: using indices */
1539                                                         displist_surf_indices(dl);
1540                                                 }
1541
1542                                                 if (bottom_capbase.first) {
1543                                                         BKE_displist_fill(&bottom_capbase, dispbase, 1);
1544                                                         BKE_displist_fill(&top_capbase, dispbase, 0);
1545                                                         BKE_displist_free(&bottom_capbase);
1546                                                         BKE_displist_free(&top_capbase);
1547                                                 }
1548                                         }
1549                                 }
1550
1551                         }
1552                         BKE_displist_free(&dlbev);
1553                 }
1554
1555                 if (!(cu->flag & CU_DEFORM_FILL)) {
1556                         curve_to_filledpoly(cu, nubase, dispbase);
1557                 }
1558
1559                 if ((cu->flag & CU_PATH) && !forOrco)
1560                         calc_curvepath(ob);
1561
1562                 /* make copy of 'undeformed" displist for texture space calculation
1563                  * actually, it's not totally undeformed -- pre-tessellation modifiers are
1564                  * already applied, thats how it worked for years, so keep for compatibility (sergey) */
1565                 BKE_displist_copy(&cu->disp, dispbase);
1566
1567                 if (!forRender) {
1568                         BKE_curve_texspace_calc(cu);
1569                 }
1570
1571                 if (!forOrco)
1572                         curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal, forRender, originalVerts, deformedVerts);
1573
1574                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1575                         curve_to_filledpoly(cu, nubase, dispbase);
1576                 }
1577         }
1578 }
1579
1580 void BKE_displist_make_curveTypes(Scene *scene, Object *ob, int forOrco)
1581 {
1582         Curve *cu = ob->data;
1583         ListBase *dispbase;
1584
1585         /* The same check for duplis as in do_makeDispListCurveTypes.
1586          * Happens when curve used for constraint/bevel was converted to mesh.
1587          * check there is still needed for render displist and orco displists. */
1588         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT))
1589                 return;
1590
1591         BKE_displist_free(&(ob->disp));
1592         dispbase = &(ob->disp);
1593         BKE_displist_free(dispbase);
1594
1595         /* free displist used for textspace */
1596         BKE_displist_free(&cu->disp);
1597
1598         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco);
1599
1600         if (ob->derivedFinal) {
1601                 DM_set_object_boundbox(ob, ob->derivedFinal);
1602
1603                 /* always keep curve's  BB in sync with non-deformed displist */
1604                 if (cu->bb == NULL)
1605                         cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1606
1607                 boundbox_dispbase(cu->bb, &cu->disp);
1608         }
1609         else {
1610                 boundbox_displist(ob);
1611         }
1612 }
1613
1614 void BKE_displist_make_curveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1615                                       DerivedMesh **derivedFinal, int forOrco)
1616 {
1617         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco);
1618 }
1619
1620 void BKE_displist_make_curveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1621 {
1622         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1);
1623 }
1624
1625 /* add Orco layer to the displist object which has got derived mesh and return orco */
1626 float *BKE_displist_make_orco(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1627 {
1628         float *orco;
1629
1630         if (derivedFinal == NULL)
1631                 derivedFinal = ob->derivedFinal;
1632
1633         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1634                 curve_calc_orcodm(scene, ob, derivedFinal, forRender);
1635         }
1636
1637         orco = derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1638
1639         if (orco) {
1640                 orco = MEM_dupallocN(orco);
1641         }
1642
1643         return orco;
1644 }
1645
1646 static void boundbox_dispbase(BoundBox *bb, ListBase *dispbase)
1647 {
1648         float min[3], max[3];
1649         DispList *dl;
1650         float *vert;
1651         int a, tot = 0;
1652         int doit = 0;
1653
1654         INIT_MINMAX(min, max);
1655
1656         for (dl = dispbase->first; dl; dl = dl->next) {
1657                 tot = (dl->type == DL_INDEX3) ? dl->nr : dl->nr * dl->parts;
1658                 vert = dl->verts;
1659                 for (a = 0; a < tot; a++, vert += 3) {
1660                         minmax_v3v3_v3(min, max, vert);
1661                 }
1662                 doit |= (tot != 0);
1663         }
1664
1665         if (!doit) {
1666                 /* there's no geometry in displist, use zero-sized boundbox */
1667                 zero_v3(min);
1668                 zero_v3(max);
1669         }
1670
1671         BKE_boundbox_init_from_minmax(bb, min, max);
1672 }
1673
1674 /* this is confusing, there's also min_max_object, appplying the obmat... */
1675 static void boundbox_displist(Object *ob)
1676 {
1677         if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1678                 Curve *cu = ob->data;
1679
1680                 /* calculate curve's BB based on non-deformed displist */
1681                 if (cu->bb == NULL)
1682                         cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1683
1684                 boundbox_dispbase(cu->bb, &cu->disp);
1685
1686                 /* object's BB is calculated from final displist */
1687                 if (ob->bb == NULL)
1688                         ob->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1689
1690                 boundbox_dispbase(ob->bb, &ob->disp);
1691         }
1692 }