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