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