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