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