10c9f30f5ee92196d13c8bc6f716bab12de9529e
[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                                 /* pass */
304                         }
305                         else if (nu->type == CU_BEZIER) {
306                                 /* count */
307                                 len = 0;
308                                 a = nu->pntsu - 1;
309                                 if (nu->flagu & CU_NURB_CYCLIC) a++;
310
311                                 prevbezt = nu->bezt;
312                                 bezt = prevbezt + 1;
313                                 while (a--) {
314                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC))
315                                                 bezt = nu->bezt;
316
317                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT)
318                                                 len++;
319                                         else
320                                                 len += resolu;
321
322                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC) == 0)
323                                                 len++;
324
325                                         prevbezt = bezt;
326                                         bezt++;
327                                 }
328
329                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbez");
330                                 /* len+1 because of 'forward_diff_bezier' function */
331                                 dl->verts = MEM_callocN((len + 1) * 3 * sizeof(float), "dlverts");
332                                 BLI_addtail(dispbase, dl);
333                                 dl->parts = 1;
334                                 dl->nr = len;
335                                 dl->col = nu->mat_nr;
336                                 dl->charidx = nu->charidx;
337
338                                 data = dl->verts;
339
340                                 if (nu->flagu & CU_NURB_CYCLIC) {
341                                         dl->type = DL_POLY;
342                                         a = nu->pntsu;
343                                 }
344                                 else {
345                                         dl->type = DL_SEGM;
346                                         a = nu->pntsu - 1;
347                                 }
348
349                                 prevbezt = nu->bezt;
350                                 bezt = prevbezt + 1;
351
352                                 while (a--) {
353                                         if (a == 0 && dl->type == DL_POLY)
354                                                 bezt = nu->bezt;
355
356                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
357                                                 copy_v3_v3(data, prevbezt->vec[1]);
358                                                 data += 3;
359                                         }
360                                         else {
361                                                 int j;
362                                                 for (j = 0; j < 3; j++) {
363                                                         BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],
364                                                                                       prevbezt->vec[2][j],
365                                                                                       bezt->vec[0][j],
366                                                                                       bezt->vec[1][j],
367                                                                                       data + j, resolu, 3 * sizeof(float));
368                                                 }
369
370                                                 data += 3 * resolu;
371                                         }
372
373                                         if (a == 0 && dl->type == DL_SEGM) {
374                                                 copy_v3_v3(data, bezt->vec[1]);
375                                         }
376
377                                         prevbezt = bezt;
378                                         bezt++;
379                                 }
380                         }
381                         else if (nu->type == CU_NURBS) {
382                                 len = (resolu * SEGMENTSU(nu));
383
384                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
385                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
386                                 BLI_addtail(dispbase, dl);
387                                 dl->parts = 1;
388
389                                 dl->nr = len;
390                                 dl->col = nu->mat_nr;
391                                 dl->charidx = nu->charidx;
392
393                                 data = dl->verts;
394                                 if (nu->flagu & CU_NURB_CYCLIC)
395                                         dl->type = DL_POLY;
396                                 else dl->type = DL_SEGM;
397                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
398                         }
399                         else if (nu->type == CU_POLY) {
400                                 len = nu->pntsu;
401                                 dl = MEM_callocN(sizeof(DispList), "makeDispListpoly");
402                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
403                                 BLI_addtail(dispbase, dl);
404                                 dl->parts = 1;
405                                 dl->nr = len;
406                                 dl->col = nu->mat_nr;
407                                 dl->charidx = nu->charidx;
408
409                                 data = dl->verts;
410                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
411                                 else dl->type = DL_SEGM;
412
413                                 a = len;
414                                 bp = nu->bp;
415                                 while (a--) {
416                                         copy_v3_v3(data, bp->vec);
417                                         bp++;
418                                         data += 3;
419                                 }
420                         }
421                 }
422                 nu = nu->next;
423         }
424 }
425
426
427 void BKE_displist_fill(ListBase *dispbase, ListBase *to, int flipnormal)
428 {
429         ScanFillContext sf_ctx;
430         ScanFillVert *sf_vert, *sf_vert_new, *sf_vert_last;
431         ScanFillFace *sf_tri;
432         DispList *dlnew = NULL, *dl;
433         float *f1;
434         int colnr = 0, charidx = 0, cont = 1, tot, a, *index, nextcol = 0;
435         intptr_t totvert;
436
437         if (dispbase == NULL)
438                 return;
439         if (dispbase->first == NULL)
440                 return;
441
442         while (cont) {
443                 cont = 0;
444                 totvert = 0;
445                 nextcol = 0;
446
447                 BLI_scanfill_begin(&sf_ctx);
448
449                 dl = dispbase->first;
450                 while (dl) {
451                         if (dl->type == DL_POLY) {
452                                 if (charidx < dl->charidx)
453                                         cont = 1;
454                                 else if (charidx == dl->charidx) { /* character with needed index */
455                                         if (colnr == dl->col) {
456                                                 /* make editverts and edges */
457                                                 f1 = dl->verts;
458                                                 a = dl->nr;
459                                                 sf_vert = sf_vert_new = NULL;
460
461                                                 while (a--) {
462                                                         sf_vert_last = sf_vert;
463
464                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, f1);
465                                                         totvert++;
466
467                                                         if (sf_vert_last == NULL)
468                                                                 sf_vert_new = sf_vert;
469                                                         else {
470                                                                 BLI_scanfill_edge_add(&sf_ctx, sf_vert_last, sf_vert);
471                                                         }
472                                                         f1 += 3;
473                                                 }
474
475                                                 if (sf_vert != NULL && sf_vert_new != NULL) {
476                                                         BLI_scanfill_edge_add(&sf_ctx, sf_vert, sf_vert_new);
477                                                 }
478                                         }
479                                         else if (colnr < dl->col) {
480                                                 /* got poly with next material at current char */
481                                                 cont = 1;
482                                                 nextcol = 1;
483                                         }
484                                 }
485                         }
486                         dl = dl->next;
487                 }
488
489                 /* XXX (obedit && obedit->actcol)?(obedit->actcol-1):0)) { */
490                 if (totvert && (tot = BLI_scanfill_calc(&sf_ctx, FALSE))) {
491                         if (tot) {
492                                 dlnew = MEM_callocN(sizeof(DispList), "filldisplist");
493                                 dlnew->type = DL_INDEX3;
494                                 dlnew->col = colnr;
495                                 dlnew->nr = totvert;
496                                 dlnew->parts = tot;
497
498                                 dlnew->index = MEM_mallocN(tot * 3 * sizeof(int), "dlindex");
499                                 dlnew->verts = MEM_mallocN(totvert * 3 * sizeof(float), "dlverts");
500
501                                 /* vert data */
502                                 f1 = dlnew->verts;
503                                 totvert = 0;
504
505                                 for (sf_vert = sf_ctx.fillvertbase.first; sf_vert; sf_vert = sf_vert->next) {
506                                         copy_v3_v3(f1, sf_vert->co);
507                                         f1 += 3;
508
509                                         /* index number */
510                                         sf_vert->tmp.l = totvert;
511                                         totvert++;
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.is_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 || cu->editfont));
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 (!modifier_isEnabled(scene, md, required_mode))
797                                 continue;
798                         if (mti->type != eModifierTypeType_OnlyDeform)
799                                 continue;
800
801                         if (!deformedVerts) {
802                                 deformedVerts = BKE_curve_vertexCos_get(cu, nurb, &numVerts);
803                                 originalVerts = MEM_dupallocN(deformedVerts);
804                         }
805
806                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, app_flag);
807
808                         if (md == pretessellatePoint)
809                                 break;
810                 }
811         }
812
813         if (deformedVerts)
814                 BK_curve_vertexCos_apply(cu, nurb, deformedVerts);
815         if (keyVerts) /* these are not passed through modifier stack */
816                 BKE_curve_keyVertexTilts_apply(cu, nurb, keyVerts);
817
818         if (keyVerts)
819                 MEM_freeN(keyVerts);
820
821         *originalVerts_r = originalVerts;
822         *deformedVerts_r = deformedVerts;
823         *numVerts_r = numVerts;
824 }
825
826 static float (*displist_get_allverts(ListBase * dispbase, int *totvert))[3]
827 {
828         DispList *dl;
829         float (*allverts)[3], *fp;
830
831         *totvert = 0;
832
833         for (dl = dispbase->first; dl; dl = dl->next)
834                 *totvert += (dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr;
835
836         allverts = MEM_mallocN((*totvert) * sizeof(float) * 3, "displist_get_allverts allverts");
837         fp = (float *)allverts;
838         for (dl = dispbase->first; dl; dl = dl->next) {
839                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
840                 memcpy(fp, dl->verts, sizeof(float) * offs);
841                 fp += offs;
842         }
843
844         return allverts;
845 }
846
847 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
848 {
849         DispList *dl;
850         float *fp;
851
852         fp = (float *)allverts;
853         for (dl = dispbase->first; dl; dl = dl->next) {
854                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
855                 memcpy(dl->verts, fp, sizeof(float) * offs);
856                 fp += offs;
857         }
858 }
859
860 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispbase, DerivedMesh **derivedFinal,
861                                       int forRender, float (*originalVerts)[3], float (*deformedVerts)[3])
862 {
863         ModifierData *md = modifiers_getVirtualModifierList(ob);
864         ModifierData *pretessellatePoint;
865         Curve *cu = ob->data;
866         ListBase *nurb = BKE_curve_nurbs_get(cu);
867         int required_mode = 0, totvert = 0;
868         int editmode = (!forRender && (cu->editnurb || cu->editfont));
869         DerivedMesh *dm = NULL, *ndm;
870         float (*vertCos)[3] = NULL;
871         int useCache = !forRender;
872         ModifierApplyFlag app_flag = 0;
873
874         if (forRender) {
875                 app_flag |= MOD_APPLY_RENDER;
876                 required_mode = eModifierMode_Render;
877         }
878         else
879                 required_mode = eModifierMode_Realtime;
880
881         pretessellatePoint = curve_get_tessellate_point(scene, ob, forRender, editmode);
882
883         if (editmode)
884                 required_mode |= eModifierMode_Editmode;
885
886         if (pretessellatePoint) {
887                 md = pretessellatePoint->next;
888         }
889
890         if (derivedFinal && *derivedFinal) {
891                 (*derivedFinal)->release(*derivedFinal);
892         }
893
894         for (; md; md = md->next) {
895                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
896                 ModifierApplyFlag appf = app_flag;
897
898                 md->scene = scene;
899
900                 if (!modifier_isEnabled(scene, md, required_mode))
901                         continue;
902
903                 if (mti->type == eModifierTypeType_OnlyDeform ||
904                     (mti->type == eModifierTypeType_DeformOrConstruct && !dm))
905                 {
906                         if (editmode)
907                                 appf |= MOD_APPLY_USECACHE;
908                         if (dm) {
909                                 if (!vertCos) {
910                                         totvert = dm->getNumVerts(dm);
911                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
912                                         dm->getVertCos(dm, vertCos);
913                                 }
914
915                                 mti->deformVerts(md, ob, dm, vertCos, totvert, appf);
916                         }
917                         else {
918                                 if (!vertCos) {
919                                         vertCos = displist_get_allverts(dispbase, &totvert);
920                                 }
921
922                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, appf);
923                         }
924                 }
925                 else {
926                         if (!derivedFinal) {
927                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh
928                                  * is totally unnecessary, so we could stop modifiers applying
929                                  * when we found constructive modifier but derived mesh is unwanted result
930                                  */
931                                 break;
932                         }
933
934                         if (dm) {
935                                 if (vertCos) {
936                                         DerivedMesh *tdm = CDDM_copy(dm);
937                                         dm->release(dm);
938                                         dm = tdm;
939
940                                         CDDM_apply_vert_coords(dm, vertCos);
941                                         CDDM_calc_normals_mapping(dm);
942                                 }
943                         }
944                         else {
945                                 if (vertCos) {
946                                         displist_apply_allverts(dispbase, vertCos);
947                                 }
948
949                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
950                                         curve_to_filledpoly(cu, nurb, dispbase);
951                                 }
952
953                                 dm = CDDM_from_curve_displist(ob, dispbase, NULL);
954
955                                 CDDM_calc_normals_mapping(dm);
956                         }
957
958                         if (vertCos) {
959                                 /* Vertex coordinates were applied to necessary data, could free it */
960                                 MEM_freeN(vertCos);
961                                 vertCos = NULL;
962                         }
963
964                         if (useCache)
965                                 appf |= MOD_APPLY_USECACHE;
966                         ndm = mti->applyModifier(md, ob, dm, appf);
967
968                         if (ndm) {
969                                 /* Modifier returned a new derived mesh */
970
971                                 if (dm && dm != ndm) /* Modifier  */
972                                         dm->release(dm);
973                                 dm = ndm;
974                         }
975                 }
976         }
977
978         if (vertCos) {
979                 if (dm) {
980                         DerivedMesh *tdm = CDDM_copy(dm);
981                         dm->release(dm);
982                         dm = tdm;
983
984                         CDDM_apply_vert_coords(dm, vertCos);
985                         CDDM_calc_normals_mapping(dm);
986                         MEM_freeN(vertCos);
987                 }
988                 else {
989                         displist_apply_allverts(dispbase, vertCos);
990                         MEM_freeN(vertCos);
991                         vertCos = NULL;
992                 }
993         }
994
995         if (derivedFinal) {
996                 if (dm)
997                         DM_ensure_tessface(dm);  /* needed for drawing */
998                 (*derivedFinal) = dm;
999         }
1000
1001         if (deformedVerts) {
1002                 BK_curve_vertexCos_apply(ob->data, nurb, originalVerts);
1003                 MEM_freeN(originalVerts);
1004                 MEM_freeN(deformedVerts);
1005         }
1006 }
1007
1008 static void displist_surf_indices(DispList *dl)
1009 {
1010         int a, b, p1, p2, p3, p4;
1011         int *index;
1012
1013         dl->totindex = 0;
1014
1015         index = dl->index = MEM_mallocN(4 * sizeof(int) * (dl->parts + 1) * (dl->nr + 1), "index array nurbs");
1016
1017         for (a = 0; a < dl->parts; a++) {
1018
1019                 if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
1020                         break;
1021
1022                 for (; b < dl->nr; b++, index += 4) {
1023                         index[0] = p1;
1024                         index[1] = p2;
1025                         index[2] = p4;
1026                         index[3] = p3;
1027
1028                         dl->totindex++;
1029
1030                         p2 = p1; p1++;
1031                         p4 = p3; p3++;
1032                 }
1033         }
1034 }
1035
1036 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
1037 {
1038         DerivedMesh *dm;
1039         ListBase disp = {NULL, NULL};
1040
1041         /* OrcoDM should be created from underformed disp lists */
1042         BKE_displist_make_curveTypes_forOrco(scene, ob, &disp);
1043         dm = CDDM_from_curve_displist(ob, &disp, NULL);
1044
1045         BKE_displist_free(&disp);
1046
1047         return dm;
1048 }
1049
1050 static void add_orco_dm(Scene *scene, Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1051 {
1052         float (*orco)[3], (*layerorco)[3];
1053         int totvert, a;
1054         Curve *cu = ob->data;
1055
1056         totvert = dm->getNumVerts(dm);
1057
1058         if (orcodm) {
1059                 orco = MEM_callocN(sizeof(float) * 3 * totvert, "dm orco");
1060
1061                 if (orcodm->getNumVerts(orcodm) == totvert)
1062                         orcodm->getVertCos(orcodm, orco);
1063                 else
1064                         dm->getVertCos(dm, orco);
1065         }
1066         else {
1067                 orco = (float(*)[3])BKE_curve_make_orco(scene, ob);
1068         }
1069
1070         for (a = 0; a < totvert; a++) {
1071                 float *co = orco[a];
1072                 co[0] = (co[0] - cu->loc[0]) / cu->size[0];
1073                 co[1] = (co[1] - cu->loc[1]) / cu->size[1];
1074                 co[2] = (co[2] - cu->loc[2]) / cu->size[2];
1075         }
1076
1077         if ((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1078                 memcpy(layerorco, orco, sizeof(float) * totvert);
1079                 MEM_freeN(orco);
1080         }
1081         else
1082                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1083 }
1084
1085 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1086 {
1087         /* this function represents logic of mesh's orcodm calculation
1088          * for displist-based objects
1089          */
1090
1091         ModifierData *md = modifiers_getVirtualModifierList(ob);
1092         ModifierData *pretessellatePoint;
1093         Curve *cu = ob->data;
1094         int required_mode;
1095         int editmode = (!forRender && (cu->editnurb || cu->editfont));
1096         DerivedMesh *ndm, *orcodm = NULL;
1097         const ModifierApplyFlag app_flag = forRender ? MOD_APPLY_RENDER : 0;
1098
1099         if (forRender)
1100                 required_mode = eModifierMode_Render;
1101         else
1102                 required_mode = eModifierMode_Realtime;
1103
1104         pretessellatePoint = curve_get_tessellate_point(scene, ob, forRender, editmode);
1105
1106         if (editmode)
1107                 required_mode |= eModifierMode_Editmode;
1108
1109         if (pretessellatePoint) {
1110                 md = pretessellatePoint->next;
1111         }
1112
1113         for (; md; md = md->next) {
1114                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1115
1116                 md->scene = scene;
1117
1118                 if (!modifier_isEnabled(scene, md, required_mode))
1119                         continue;
1120                 if (mti->type != eModifierTypeType_Constructive)
1121                         continue;
1122
1123                 if (!orcodm)
1124                         orcodm = create_orco_dm(scene, ob);
1125
1126                 ndm = mti->applyModifier(md, ob, orcodm, app_flag);
1127
1128                 if (ndm) {
1129                         /* if the modifier returned a new dm, release the old one */
1130                         if (orcodm && orcodm != ndm) {
1131                                 orcodm->release(orcodm);
1132                         }
1133                         orcodm = ndm;
1134                 }
1135         }
1136
1137         /* add an orco layer if needed */
1138         add_orco_dm(scene, ob, derivedFinal, orcodm);
1139
1140         if (orcodm)
1141                 orcodm->release(orcodm);
1142 }
1143
1144 void BKE_displist_make_surf(Scene *scene, Object *ob, ListBase *dispbase,
1145                             DerivedMesh **derivedFinal, int forRender, int forOrco)
1146 {
1147         ListBase *nubase;
1148         Nurb *nu;
1149         Curve *cu = ob->data;
1150         DispList *dl;
1151         float *data;
1152         int len;
1153         int numVerts;
1154         float (*originalVerts)[3];
1155         float (*deformedVerts)[3];
1156
1157         if (!forRender && cu->editnurb)
1158                 nubase = BKE_curve_editNurbs_get(cu);
1159         else
1160                 nubase = &cu->nurb;
1161
1162         if (!forOrco)
1163                 curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1164
1165         for (nu = nubase->first; nu; nu = nu->next) {
1166                 if (forRender || nu->hide == 0) {
1167                         int resolu = nu->resolu, resolv = nu->resolv;
1168
1169                         if (forRender) {
1170                                 if (cu->resolu_ren)
1171                                         resolu = cu->resolu_ren;
1172                                 if (cu->resolv_ren)
1173                                         resolv = cu->resolv_ren;
1174                         }
1175
1176                         if (nu->pntsv == 1) {
1177                                 len = SEGMENTSU(nu) * resolu;
1178
1179                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1180                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
1181
1182                                 BLI_addtail(dispbase, dl);
1183                                 dl->parts = 1;
1184                                 dl->nr = len;
1185                                 dl->col = nu->mat_nr;
1186                                 dl->charidx = nu->charidx;
1187
1188                                 /* dl->rt will be used as flag for render face and */
1189                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1190                                 dl->rt = nu->flag & ~CU_2D;
1191
1192                                 data = dl->verts;
1193                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
1194                                 else dl->type = DL_SEGM;
1195
1196                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
1197                         }
1198                         else {
1199                                 len = (nu->pntsu * resolu) * (nu->pntsv * resolv);
1200
1201                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1202                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
1203                                 BLI_addtail(dispbase, dl);
1204
1205                                 dl->col = nu->mat_nr;
1206                                 dl->charidx = nu->charidx;
1207
1208                                 /* dl->rt will be used as flag for render face and */
1209                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1210                                 dl->rt = nu->flag & ~CU_2D;
1211
1212                                 data = dl->verts;
1213                                 dl->type = DL_SURF;
1214
1215                                 dl->parts = (nu->pntsu * resolu);  /* in reverse, because makeNurbfaces works that way */
1216                                 dl->nr = (nu->pntsv * resolv);
1217                                 if (nu->flagv & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_U;  /* reverse too! */
1218                                 if (nu->flagu & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_V;
1219
1220                                 BKE_nurb_makeFaces(nu, data, 0, resolu, resolv);
1221
1222                                 /* gl array drawing: using indices */
1223                                 displist_surf_indices(dl);
1224                         }
1225                 }
1226         }
1227
1228         /* make copy of 'undeformed" displist for texture space calculation
1229          * actually, it's not totally undeformed -- pre-tessellation modifiers are
1230          * already applied, thats how it worked for years, so keep for compatibility (sergey) */
1231         BKE_displist_copy(&cu->disp, dispbase);
1232
1233         if (!forRender) {
1234                 BKE_curve_texspace_calc(cu);
1235         }
1236
1237         if (!forOrco) {
1238                 curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal,
1239                                           forRender, originalVerts, deformedVerts);
1240         }
1241 }
1242
1243 static void rotateBevelPiece(Curve *cu, BevPoint *bevp, DispList *dlb, float widfac, float fac, float **data_r)
1244 {
1245         float *fp, *data = *data_r;
1246         int b;
1247
1248         fp = dlb->verts;
1249         for (b = 0; b < dlb->nr; b++, fp += 3, data += 3) {
1250                 if (cu->flag & CU_3D) {
1251                         float vec[3];
1252
1253                         vec[0] = fp[1] + widfac;
1254                         vec[1] = fp[2];
1255                         vec[2] = 0.0;
1256
1257                         mul_qt_v3(bevp->quat, vec);
1258
1259                         data[0] = bevp->vec[0] + fac * vec[0];
1260                         data[1] = bevp->vec[1] + fac * vec[1];
1261                         data[2] = bevp->vec[2] + fac * vec[2];
1262                 }
1263                 else {
1264                         data[0] = bevp->vec[0] + fac * (widfac + fp[1]) * bevp->sina;
1265                         data[1] = bevp->vec[1] + fac * (widfac + fp[1]) * bevp->cosa;
1266                         data[2] = bevp->vec[2] + fac * fp[2];
1267                 }
1268         }
1269
1270         *data_r = data;
1271 }
1272
1273 static void fillBevelCap(Nurb *nu, DispList *dlb, float *prev_fp, ListBase *dispbase)
1274 {
1275         DispList *dl;
1276         float *data;
1277         int b;
1278
1279         dl = MEM_callocN(sizeof(DispList), "makeDispListbev2");
1280         dl->verts = data = MEM_callocN(3 * sizeof(float) * dlb->nr, "dlverts");
1281
1282         dl->type = DL_POLY;
1283
1284         dl->parts = 1;
1285         dl->nr = dlb->nr;
1286         dl->col = nu->mat_nr;
1287         dl->charidx = nu->charidx;
1288
1289         /* dl->rt will be used as flag for render face and */
1290         /* CU_2D conflicts with R_NOPUNOFLIP */
1291         dl->rt = nu->flag & ~CU_2D;
1292
1293         for (b = 0; b < dlb->nr; b++, prev_fp += 3, data += 3)
1294                 copy_v3_v3(data, prev_fp);
1295
1296         BLI_addtail(dispbase, dl);
1297 }
1298
1299 static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1300                                       DerivedMesh **derivedFinal, int forRender, int forOrco)
1301 {
1302         Curve *cu = ob->data;
1303
1304         /* we do allow duplis... this is only displist on curve level */
1305         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1306
1307         if (ob->type == OB_SURF) {
1308                 BKE_displist_make_surf(scene, ob, dispbase, derivedFinal, forRender, forOrco);
1309         }
1310         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1311                 ListBase dlbev;
1312                 ListBase *nubase;
1313                 float (*originalVerts)[3];
1314                 float (*deformedVerts)[3];
1315                 int numVerts;
1316
1317                 nubase = BKE_curve_nurbs_get(cu);
1318
1319                 BLI_freelistN(&(cu->bev));
1320
1321                 if (cu->path) free_path(cu->path);
1322                 cu->path = NULL;
1323
1324                 if (ob->type == OB_FONT)
1325                         BKE_vfont_to_curve(G.main, scene, ob, 0);
1326
1327                 if (!forOrco)
1328                         curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1329
1330                 BKE_curve_bevelList_make(ob);
1331
1332                 /* If curve has no bevel will return nothing */
1333                 BKE_curve_bevel_make(scene, ob, &dlbev, forRender);
1334
1335                 /* no bevel or extrude, and no width correction? */
1336                 if (!dlbev.first && cu->width == 1.0f) {
1337                         curve_to_displist(cu, nubase, dispbase, forRender);
1338                 }
1339                 else {
1340                         float widfac = cu->width - 1.0f;
1341                         BevList *bl = cu->bev.first;
1342                         Nurb *nu = nubase->first;
1343
1344                         for (; bl && nu; bl = bl->next, nu = nu->next) {
1345                                 DispList *dl;
1346                                 float *data;
1347                                 BevPoint *bevp;
1348                                 int a;
1349
1350                                 if (bl->nr) { /* blank bevel lists can happen */
1351
1352                                         /* exception handling; curve without bevel or extrude, with width correction */
1353                                         if (dlbev.first == NULL) {
1354                                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbev");
1355                                                 dl->verts = MEM_callocN(3 * sizeof(float) * bl->nr, "dlverts");
1356                                                 BLI_addtail(dispbase, dl);
1357
1358                                                 if (bl->poly != -1) dl->type = DL_POLY;
1359                                                 else dl->type = DL_SEGM;
1360
1361                                                 if (dl->type == DL_SEGM) dl->flag = (DL_FRONT_CURVE | DL_BACK_CURVE);
1362
1363                                                 dl->parts = 1;
1364                                                 dl->nr = bl->nr;
1365                                                 dl->col = nu->mat_nr;
1366                                                 dl->charidx = nu->charidx;
1367
1368                                                 /* dl->rt will be used as flag for render face and */
1369                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1370                                                 dl->rt = nu->flag & ~CU_2D;
1371
1372                                                 a = dl->nr;
1373                                                 bevp = (BevPoint *)(bl + 1);
1374                                                 data = dl->verts;
1375                                                 while (a--) {
1376                                                         data[0] = bevp->vec[0] + widfac * bevp->sina;
1377                                                         data[1] = bevp->vec[1] + widfac * bevp->cosa;
1378                                                         data[2] = bevp->vec[2];
1379                                                         bevp++;
1380                                                         data += 3;
1381                                                 }
1382                                         }
1383                                         else {
1384                                                 DispList *dlb;
1385                                                 ListBase bottom_capbase = {NULL, NULL};
1386                                                 ListBase top_capbase = {NULL, NULL};
1387
1388                                                 for (dlb = dlbev.first; dlb; dlb = dlb->next) {
1389                                                         const float bevfac1 = minf(cu->bevfac1, cu->bevfac2);
1390                                                         const float bevfac2 = maxf(cu->bevfac1, cu->bevfac2);
1391                                                         float firstblend = 0.0f, lastblend = 0.0f;
1392                                                         int i, start, steps;
1393
1394                                                         if (bevfac2 - bevfac1 == 0.0f)
1395                                                                 continue;
1396
1397                                                         start = (int)(bevfac1 * (bl->nr - 1));
1398                                                         steps = 2 + (int)((bevfac2) * (bl->nr - 1)) - start;
1399                                                         firstblend = 1.0f - (bevfac1 * (bl->nr - 1) - (int)(bevfac1 * (bl->nr - 1)));
1400                                                         lastblend  =         bevfac2 * (bl->nr - 1) - (int)(bevfac2 * (bl->nr - 1));
1401
1402                                                         if (steps > bl->nr) {
1403                                                                 steps = bl->nr;
1404                                                                 lastblend = 1.0f;
1405                                                         }
1406
1407                                                         /* for each part of the bevel use a separate displblock */
1408                                                         dl = MEM_callocN(sizeof(DispList), "makeDispListbev1");
1409                                                         dl->verts = data = MEM_callocN(3 * sizeof(float) * dlb->nr * steps, "dlverts");
1410                                                         BLI_addtail(dispbase, dl);
1411
1412                                                         dl->type = DL_SURF;
1413
1414                                                         dl->flag = dlb->flag & (DL_FRONT_CURVE | DL_BACK_CURVE);
1415                                                         if (dlb->type == DL_POLY) dl->flag |= DL_CYCL_U;
1416                                                         if (bl->poly >= 0) dl->flag |= DL_CYCL_V;
1417
1418                                                         dl->parts = steps;
1419                                                         dl->nr = dlb->nr;
1420                                                         dl->col = nu->mat_nr;
1421                                                         dl->charidx = nu->charidx;
1422
1423                                                         /* dl->rt will be used as flag for render face and */
1424                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1425                                                         dl->rt = nu->flag & ~CU_2D;
1426
1427                                                         dl->bevelSplitFlag = MEM_callocN(sizeof(*dl->col2) * ((steps + 0x1F) >> 5),
1428                                                                                          "bevelSplitFlag");
1429
1430                                                         /* for each point of poly make a bevel piece */
1431                                                         bevp = (BevPoint *)(bl + 1) + start;
1432                                                         for (i = start, a = 0; a < steps; i++, bevp++, a++) {
1433                                                                 float fac = 1.0;
1434                                                                 float *cur_data = data;
1435
1436                                                                 if (cu->taperobj == NULL) {
1437                                                                         if ( (cu->bevobj != NULL) || !((cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) )
1438                                                                                 fac = bevp->radius;
1439                                                                 }
1440                                                                 else {
1441                                                                         fac = BKE_displist_calc_taper(scene, cu->taperobj, i, bl->nr);
1442                                                                 }
1443
1444                                                                 if (bevp->split_tag) {
1445                                                                         dl->bevelSplitFlag[a >> 5] |= 1 << (a & 0x1F);
1446                                                                 }
1447
1448                                                                 /* rotate bevel piece and write in data */
1449                                                                 rotateBevelPiece(cu, bevp, dlb, widfac, fac, &data);
1450
1451                                                                 if (a == 1 || a == steps - 1) {
1452                                                                         float *cur_fp = cur_data, *prev_fp = cur_data - 3 * dlb->nr;
1453                                                                         int b;
1454
1455                                                                         for (b = 0; b < dlb->nr; b++, prev_fp += 3, cur_fp += 3) {
1456                                                                                 float cur[3], prev[3];
1457
1458                                                                                 copy_v3_v3(cur, cur_fp);
1459                                                                                 copy_v3_v3(prev, prev_fp);
1460
1461                                                                                 if (a == 1)
1462                                                                                         interp_v3_v3v3(prev, cur_fp, prev_fp, firstblend);
1463                                                                                 if (a == steps - 1)
1464                                                                                         interp_v3_v3v3(cur, prev_fp, cur_fp, lastblend);
1465
1466                                                                                 copy_v3_v3(cur_fp, cur);
1467                                                                                 copy_v3_v3(prev_fp, prev);
1468                                                                         }
1469                                                                 }
1470
1471                                                                 if (cu->bevobj && (cu->flag & CU_FILL_CAPS)) {
1472                                                                         if (a == 1)
1473                                                                                 fillBevelCap(nu, dlb, cur_data - 3 * dlb->nr, &bottom_capbase);
1474                                                                         if (a == steps - 1)
1475                                                                                 fillBevelCap(nu, dlb, cur_data, &top_capbase);
1476                                                                 }
1477                                                         }
1478
1479                                                         /* gl array drawing: using indices */
1480                                                         displist_surf_indices(dl);
1481                                                 }
1482
1483                                                 if (bottom_capbase.first) {
1484                                                         BKE_displist_fill(&bottom_capbase, dispbase, 1);
1485                                                         BKE_displist_fill(&top_capbase, dispbase, 0);
1486                                                         BKE_displist_free(&bottom_capbase);
1487                                                         BKE_displist_free(&top_capbase);
1488                                                 }
1489                                         }
1490                                 }
1491
1492                         }
1493                         BKE_displist_free(&dlbev);
1494                 }
1495
1496                 if (!(cu->flag & CU_DEFORM_FILL)) {
1497                         curve_to_filledpoly(cu, nubase, dispbase);
1498                 }
1499
1500                 if ((cu->flag & CU_PATH) && !forOrco)
1501                         calc_curvepath(ob);
1502
1503                 /* make copy of 'undeformed" displist for texture space calculation
1504                  * actually, it's not totally undeformed -- pre-tessellation modifiers are
1505                  * already applied, thats how it worked for years, so keep for compatibility (sergey) */
1506                 BKE_displist_copy(&cu->disp, dispbase);
1507
1508                 if (!forRender) {
1509                         BKE_curve_texspace_calc(cu);
1510                 }
1511
1512                 if (!forOrco)
1513                         curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal, forRender, originalVerts, deformedVerts);
1514
1515                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1516                         curve_to_filledpoly(cu, nubase, dispbase);
1517                 }
1518         }
1519 }
1520
1521 void BKE_displist_make_curveTypes(Scene *scene, Object *ob, int forOrco)
1522 {
1523         Curve *cu = ob->data;
1524         ListBase *dispbase;
1525
1526         /* The same check for duplis as in do_makeDispListCurveTypes.
1527          * Happens when curve used for constraint/bevel was converted to mesh.
1528          * check there is still needed for render displist and orco displists. */
1529         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT))
1530                 return;
1531
1532         BKE_displist_free(&(ob->disp));
1533         dispbase = &(ob->disp);
1534         BKE_displist_free(dispbase);
1535
1536         /* free displist used for textspace */
1537         BKE_displist_free(&cu->disp);
1538
1539         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco);
1540
1541         if (ob->derivedFinal) {
1542                 DM_set_object_boundbox(ob, ob->derivedFinal);
1543         }
1544         else {
1545                 boundbox_displist(ob);
1546
1547                 /* if there is no derivedMesh, object's boundbox is unneeded */
1548                 if (ob->bb) {
1549                         MEM_freeN(ob->bb);
1550                         ob->bb = NULL;
1551                 }
1552         }
1553 }
1554
1555 void BKE_displist_make_curveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1556                                       DerivedMesh **derivedFinal, int forOrco)
1557 {
1558         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco);
1559 }
1560
1561 void BKE_displist_make_curveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1562 {
1563         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1);
1564 }
1565
1566 /* add Orco layer to the displist object which has got derived mesh and return orco */
1567 float *BKE_displist_make_orco(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1568 {
1569         float *orco;
1570
1571         if (derivedFinal == NULL)
1572                 derivedFinal = ob->derivedFinal;
1573
1574         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1575                 curve_calc_orcodm(scene, ob, derivedFinal, forRender);
1576         }
1577
1578         orco = derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1579
1580         if (orco) {
1581                 orco = MEM_dupallocN(orco);
1582         }
1583
1584         return orco;
1585 }
1586
1587 /* this is confusing, there's also min_max_object, appplying the obmat... */
1588 static void boundbox_displist(Object *ob)
1589 {
1590         BoundBox *bb = NULL;
1591         float min[3], max[3];
1592         DispList *dl;
1593         float *vert;
1594         int a, tot = 0;
1595
1596         INIT_MINMAX(min, max);
1597
1598         if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1599                 Curve *cu = ob->data;
1600                 int doit = 0;
1601
1602                 if (cu->bb == NULL) cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1603                 bb = cu->bb;
1604
1605                 for (dl = ob->disp.first; dl; dl = dl->next) {
1606                         tot = (dl->type == DL_INDEX3) ? dl->nr : dl->nr * dl->parts;
1607                         vert = dl->verts;
1608                         for (a = 0; a < tot; a++, vert += 3) {
1609                                 minmax_v3v3_v3(min, max, vert);
1610                         }
1611                         doit = (tot != 0);
1612                 }
1613
1614                 if (!doit) {
1615                         /* there's no geometry in displist, use zero-sized boundbox */
1616                         zero_v3(min);
1617                         zero_v3(max);
1618                 }
1619
1620         }
1621
1622         if (bb) {
1623                 BKE_boundbox_init_from_minmax(bb, min, max);
1624         }
1625 }