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