Depsgraph: remove EvaluationContext, pass Depsgraph instead.
[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         pretessellatePoint = curve_get_tessellate_point(scene, ob, use_render_resolution, editmode);
831
832         if (editmode)
833                 required_mode |= eModifierMode_Editmode;
834
835         if (!editmode) {
836                 keyVerts = BKE_key_evaluate_object(ob, &numVerts);
837
838                 if (keyVerts) {
839                         /* split coords from key data, the latter also includes
840                          * tilts, which is passed through in the modifier stack.
841                          * this is also the reason curves do not use a virtual
842                          * shape key modifier yet. */
843                         deformedVerts = BKE_curve_nurbs_keyVertexCos_get(nurb, keyVerts);
844                         BLI_assert(BKE_nurbList_verts_count(nurb) == numVerts);
845                 }
846         }
847
848         if (pretessellatePoint) {
849                 for (; md; md = md->next) {
850                         const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
851
852                         md->scene = scene;
853
854                         if (!modifier_isEnabled(scene, md, required_mode))
855                                 continue;
856                         if (mti->type != eModifierTypeType_OnlyDeform)
857                                 continue;
858
859                         if (!deformedVerts) {
860                                 deformedVerts = BKE_curve_nurbs_vertexCos_get(nurb, &numVerts);
861                         }
862
863                         mti->deformVerts(md, depsgraph, ob, NULL, deformedVerts, numVerts, app_flag);
864
865                         if (md == pretessellatePoint)
866                                 break;
867                 }
868         }
869
870         if (deformedVerts) {
871                 BK_curve_nurbs_vertexCos_apply(nurb, deformedVerts);
872                 MEM_freeN(deformedVerts);
873         }
874         if (keyVerts) /* these are not passed through modifier stack */
875                 BKE_curve_nurbs_keyVertexTilts_apply(nurb, keyVerts);
876
877         if (keyVerts)
878                 MEM_freeN(keyVerts);
879 }
880
881 static float (*displist_get_allverts(ListBase *dispbase, int *totvert))[3]
882 {
883         DispList *dl;
884         float (*allverts)[3], *fp;
885
886         *totvert = 0;
887
888         for (dl = dispbase->first; dl; dl = dl->next)
889                 *totvert += (dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr;
890
891         allverts = MEM_mallocN((*totvert) * sizeof(float) * 3, "displist_get_allverts allverts");
892         fp = (float *)allverts;
893         for (dl = dispbase->first; dl; dl = dl->next) {
894                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
895                 memcpy(fp, dl->verts, sizeof(float) * offs);
896                 fp += offs;
897         }
898
899         return allverts;
900 }
901
902 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
903 {
904         DispList *dl;
905         const float *fp;
906
907         fp = (float *)allverts;
908         for (dl = dispbase->first; dl; dl = dl->next) {
909                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
910                 memcpy(dl->verts, fp, sizeof(float) * offs);
911                 fp += offs;
912         }
913 }
914
915 static void curve_calc_modifiers_post(
916         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *nurb,
917         ListBase *dispbase, DerivedMesh **r_dm_final,
918         const bool for_render, const bool use_render_resolution)
919 {
920         VirtualModifierData virtualModifierData;
921         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
922         ModifierData *pretessellatePoint;
923         Curve *cu = ob->data;
924         int required_mode = 0, totvert = 0;
925         const bool editmode = (!for_render && (cu->editnurb || cu->editfont));
926         DerivedMesh *dm = NULL, *ndm;
927         float (*vertCos)[3] = NULL;
928         int useCache = !for_render;
929         ModifierApplyFlag app_flag = 0;
930
931         if (use_render_resolution) {
932                 app_flag |= MOD_APPLY_RENDER;
933                 required_mode = eModifierMode_Render;
934         }
935         else
936                 required_mode = eModifierMode_Realtime;
937
938         pretessellatePoint = curve_get_tessellate_point(scene, ob, use_render_resolution, editmode);
939
940         if (editmode)
941                 required_mode |= eModifierMode_Editmode;
942
943         if (pretessellatePoint) {
944                 md = pretessellatePoint->next;
945         }
946
947         if (r_dm_final && *r_dm_final) {
948                 (*r_dm_final)->release(*r_dm_final);
949         }
950
951         for (; md; md = md->next) {
952                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
953                 ModifierApplyFlag appf = app_flag;
954
955                 md->scene = scene;
956
957                 if (!modifier_isEnabled(scene, md, required_mode))
958                         continue;
959
960                 if (mti->type == eModifierTypeType_OnlyDeform ||
961                     (mti->type == eModifierTypeType_DeformOrConstruct && !dm))
962                 {
963                         if (editmode)
964                                 appf |= MOD_APPLY_USECACHE;
965                         if (dm) {
966                                 if (!vertCos) {
967                                         totvert = dm->getNumVerts(dm);
968                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
969                                         dm->getVertCos(dm, vertCos);
970                                 }
971
972                                 mti->deformVerts(md, depsgraph, ob, dm, vertCos, totvert, appf);
973                         }
974                         else {
975                                 if (!vertCos) {
976                                         vertCos = displist_get_allverts(dispbase, &totvert);
977                                 }
978
979                                 mti->deformVerts(md, depsgraph, ob, NULL, vertCos, totvert, appf);
980                         }
981                 }
982                 else {
983                         if (!r_dm_final) {
984                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh
985                                  * is totally unnecessary, so we could stop modifiers applying
986                                  * when we found constructive modifier but derived mesh is unwanted result
987                                  */
988                                 break;
989                         }
990
991                         if (dm) {
992                                 if (vertCos) {
993                                         DerivedMesh *tdm = CDDM_copy(dm);
994                                         dm->release(dm);
995                                         dm = tdm;
996
997                                         CDDM_apply_vert_coords(dm, vertCos);
998                                 }
999                         }
1000                         else {
1001                                 if (vertCos) {
1002                                         displist_apply_allverts(dispbase, vertCos);
1003                                 }
1004
1005                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
1006                                         curve_to_filledpoly(cu, nurb, dispbase);
1007                                 }
1008
1009                                 dm = CDDM_from_curve_displist(ob, dispbase);
1010                         }
1011
1012                         if (vertCos) {
1013                                 /* Vertex coordinates were applied to necessary data, could free it */
1014                                 MEM_freeN(vertCos);
1015                                 vertCos = NULL;
1016                         }
1017
1018                         if (useCache)
1019                                 appf |= MOD_APPLY_USECACHE;
1020
1021                         ndm = modwrap_applyModifier(md, depsgraph, ob, dm, appf);
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         pretessellatePoint = curve_get_tessellate_point(scene, ob, use_render_resolution, editmode);
1166
1167         if (editmode)
1168                 required_mode |= eModifierMode_Editmode;
1169
1170         if (pretessellatePoint) {
1171                 md = pretessellatePoint->next;
1172         }
1173
1174         /* If modifiers are disabled, we wouldn't be here because
1175          * this function is only called if there're enabled constructive
1176          * modifiers applied on the curve.
1177          *
1178          * This means we can create ORCO DM in advance and assume it's
1179          * never NULL.
1180          */
1181         orcodm = create_orco_dm(depsgraph, scene, ob);
1182
1183         for (; md; md = md->next) {
1184                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1185
1186                 md->scene = scene;
1187
1188                 if (!modifier_isEnabled(scene, md, required_mode))
1189                         continue;
1190                 if (mti->type != eModifierTypeType_Constructive)
1191                         continue;
1192
1193                 ndm = modwrap_applyModifier(md, depsgraph, ob, orcodm, app_flag);
1194
1195                 if (ndm) {
1196                         /* if the modifier returned a new dm, release the old one */
1197                         if (orcodm && orcodm != ndm) {
1198                                 orcodm->release(orcodm);
1199                         }
1200                         orcodm = ndm;
1201                 }
1202         }
1203
1204         /* add an orco layer if needed */
1205         add_orco_dm(ob, dm_final, orcodm);
1206
1207         orcodm->release(orcodm);
1208 }
1209
1210 void BKE_displist_make_surf(
1211         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase,
1212         DerivedMesh **r_dm_final,
1213         const bool for_render, const bool for_orco, const bool use_render_resolution)
1214 {
1215         ListBase nubase = {NULL, NULL};
1216         Nurb *nu;
1217         Curve *cu = ob->data;
1218         DispList *dl;
1219         float *data;
1220         int len;
1221
1222         if (!for_render && cu->editnurb) {
1223                 BKE_nurbList_duplicate(&nubase, BKE_curve_editNurbs_get(cu));
1224         }
1225         else {
1226                 BKE_nurbList_duplicate(&nubase, &cu->nurb);
1227         }
1228
1229         if (!for_orco)
1230                 curve_calc_modifiers_pre(depsgraph, scene, ob, &nubase, for_render, use_render_resolution);
1231
1232         for (nu = nubase.first; nu; nu = nu->next) {
1233                 if ((for_render || nu->hide == 0) && BKE_nurb_check_valid_uv(nu)) {
1234                         int resolu = nu->resolu, resolv = nu->resolv;
1235
1236                         if (use_render_resolution) {
1237                                 if (cu->resolu_ren)
1238                                         resolu = cu->resolu_ren;
1239                                 if (cu->resolv_ren)
1240                                         resolv = cu->resolv_ren;
1241                         }
1242
1243                         if (nu->pntsv == 1) {
1244                                 len = SEGMENTSU(nu) * resolu;
1245
1246                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1247                                 dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
1248
1249                                 BLI_addtail(dispbase, dl);
1250                                 dl->parts = 1;
1251                                 dl->nr = len;
1252                                 dl->col = nu->mat_nr;
1253                                 dl->charidx = nu->charidx;
1254
1255                                 /* dl->rt will be used as flag for render face and */
1256                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1257                                 dl->rt = nu->flag & ~CU_2D;
1258
1259                                 data = dl->verts;
1260                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
1261                                 else dl->type = DL_SEGM;
1262
1263                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
1264                         }
1265                         else {
1266                                 len = (nu->pntsu * resolu) * (nu->pntsv * resolv);
1267
1268                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1269                                 dl->verts = MEM_mallocN(len * sizeof(float[3]), "dlverts");
1270                                 BLI_addtail(dispbase, dl);
1271
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                                 dl->type = DL_SURF;
1281
1282                                 dl->parts = (nu->pntsu * resolu);  /* in reverse, because makeNurbfaces works that way */
1283                                 dl->nr = (nu->pntsv * resolv);
1284                                 if (nu->flagv & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_U;  /* reverse too! */
1285                                 if (nu->flagu & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_V;
1286
1287                                 BKE_nurb_makeFaces(nu, data, 0, resolu, resolv);
1288
1289                                 /* gl array drawing: using indices */
1290                                 displist_surf_indices(dl);
1291                         }
1292                 }
1293         }
1294
1295         if (!for_orco) {
1296                 BKE_nurbList_duplicate(&ob->curve_cache->deformed_nurbs, &nubase);
1297                 curve_calc_modifiers_post(depsgraph, scene, ob, &nubase, dispbase, r_dm_final,
1298                                           for_render, use_render_resolution);
1299         }
1300
1301         BKE_nurbList_free(&nubase);
1302 }
1303
1304 static void rotateBevelPiece(Curve *cu, BevPoint *bevp, BevPoint *nbevp, DispList *dlb, float bev_blend, float widfac, float fac, float **r_data)
1305 {
1306         float *fp, *data = *r_data;
1307         int b;
1308
1309         fp = dlb->verts;
1310         for (b = 0; b < dlb->nr; b++, fp += 3, data += 3) {
1311                 if (cu->flag & CU_3D) {
1312                         float vec[3], quat[4];
1313
1314                         vec[0] = fp[1] + widfac;
1315                         vec[1] = fp[2];
1316                         vec[2] = 0.0;
1317
1318                         if (nbevp == NULL) {
1319                                 copy_v3_v3(data, bevp->vec);
1320                                 copy_qt_qt(quat, bevp->quat);
1321                         }
1322                         else {
1323                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1324                                 interp_qt_qtqt(quat, bevp->quat, nbevp->quat, bev_blend);
1325                         }
1326
1327                         mul_qt_v3(quat, vec);
1328
1329                         data[0] += fac * vec[0];
1330                         data[1] += fac * vec[1];
1331                         data[2] += fac * vec[2];
1332                 }
1333                 else {
1334                         float sina, cosa;
1335
1336                         if (nbevp == NULL) {
1337                                 copy_v3_v3(data, bevp->vec);
1338                                 sina = bevp->sina;
1339                                 cosa = bevp->cosa;
1340                         }
1341                         else {
1342                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1343
1344                                 /* perhaps we need to interpolate angles instead. but the thing is
1345                                  * cosa and sina are not actually sine and cosine
1346                                  */
1347                                 sina = nbevp->sina * bev_blend + bevp->sina * (1.0f - bev_blend);
1348                                 cosa = nbevp->cosa * bev_blend + bevp->cosa * (1.0f - bev_blend);
1349                         }
1350
1351                         data[0] += fac * (widfac + fp[1]) * sina;
1352                         data[1] += fac * (widfac + fp[1]) * cosa;
1353                         data[2] += fac * fp[2];
1354                 }
1355         }
1356
1357         *r_data = data;
1358 }
1359
1360 static void fillBevelCap(Nurb *nu, DispList *dlb, float *prev_fp, ListBase *dispbase)
1361 {
1362         DispList *dl;
1363
1364         dl = MEM_callocN(sizeof(DispList), "makeDispListbev2");
1365         dl->verts = MEM_mallocN(sizeof(float[3]) * dlb->nr, "dlverts");
1366         memcpy(dl->verts, prev_fp, 3 * sizeof(float) * dlb->nr);
1367
1368         dl->type = DL_POLY;
1369
1370         dl->parts = 1;
1371         dl->nr = dlb->nr;
1372         dl->col = nu->mat_nr;
1373         dl->charidx = nu->charidx;
1374
1375         /* dl->rt will be used as flag for render face and */
1376         /* CU_2D conflicts with R_NOPUNOFLIP */
1377         dl->rt = nu->flag & ~CU_2D;
1378
1379         BLI_addtail(dispbase, dl);
1380 }
1381
1382 static void calc_bevfac_segment_mapping(BevList *bl, float bevfac, float spline_length, int *r_bev, float *r_blend)
1383 {
1384         float normlen, normsum = 0.0f;
1385         float *seglen = bl->seglen;
1386         int *segbevcount = bl->segbevcount;
1387         int bevcount = 0, nr = bl->nr;
1388
1389         float bev_fl = bevfac * (bl->nr - 1);
1390         *r_bev = (int)bev_fl;
1391
1392         while (bevcount < nr - 1) {
1393                 normlen = *seglen / spline_length;
1394                 if (normsum + normlen > bevfac) {
1395                         bev_fl = bevcount + (bevfac - normsum) / normlen * *segbevcount;
1396                         *r_bev = (int) bev_fl;
1397                         *r_blend = bev_fl - *r_bev;
1398                         break;
1399                 }
1400                 normsum += normlen;
1401                 bevcount += *segbevcount;
1402                 segbevcount++;
1403                 seglen++;
1404         }
1405 }
1406
1407 static void calc_bevfac_spline_mapping(BevList *bl, float bevfac,
1408                                        float spline_length,
1409                                        int *r_bev, float *r_blend)
1410 {
1411         const float len_target = bevfac * spline_length;
1412         BevPoint *bevp = bl->bevpoints;
1413         float len_next = 0.0f, len = 0.0f;
1414         int i = 0, nr = bl->nr;
1415
1416         while (nr--) {
1417                 bevp++;
1418                 len_next = len + bevp->offset;
1419                 if (len_next > len_target) {
1420                         break;
1421                 }
1422                 len = len_next;
1423                 i++;
1424         }
1425
1426         *r_bev = i;
1427         *r_blend = (len_target - len) / bevp->offset;
1428 }
1429
1430 static void calc_bevfac_mapping_default(BevList *bl,
1431                                         int *r_start, float *r_firstblend,
1432                                         int *r_steps, float *r_lastblend)
1433 {
1434         *r_start = 0;
1435         *r_steps = bl->nr;
1436         *r_firstblend = 1.0f;
1437         *r_lastblend = 1.0f;
1438 }
1439
1440 static void calc_bevfac_mapping(Curve *cu, BevList *bl, Nurb *nu,
1441         int *r_start, float *r_firstblend, int *r_steps, float *r_lastblend)
1442 {
1443         float tmpf, total_length = 0.0f;
1444         int end = 0, i;
1445
1446         if ((BKE_nurb_check_valid_u(nu) == false) ||
1447             /* not essential, but skips unnecessary calculation */
1448             (min_ff(cu->bevfac1, cu->bevfac2) == 0.0f &&
1449              max_ff(cu->bevfac1, cu->bevfac2) == 1.0f))
1450         {
1451                 calc_bevfac_mapping_default(bl, r_start, r_firstblend, r_steps, r_lastblend);
1452                 return;
1453         }
1454
1455         if (ELEM(cu->bevfac1_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE) ||
1456             ELEM(cu->bevfac2_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE))
1457         {
1458                 for (i = 0; i < SEGMENTSU(nu); i++) {
1459                         total_length += bl->seglen[i];
1460                 }
1461         }
1462
1463         switch (cu->bevfac1_mapping) {
1464                 case CU_BEVFAC_MAP_RESOLU:
1465                 {
1466                         const float start_fl = cu->bevfac1 * (bl->nr - 1);
1467                         *r_start = (int)start_fl;
1468                         *r_firstblend = 1.0f - (start_fl - (*r_start));
1469                         break;
1470                 }
1471                 case CU_BEVFAC_MAP_SEGMENT:
1472                 {
1473                         calc_bevfac_segment_mapping(bl, cu->bevfac1, total_length, r_start, r_firstblend);
1474                         *r_firstblend = 1.0f - *r_firstblend;
1475                         break;
1476                 }
1477                 case CU_BEVFAC_MAP_SPLINE:
1478                 {
1479                         calc_bevfac_spline_mapping(bl, cu->bevfac1, total_length, r_start, r_firstblend);
1480                         *r_firstblend = 1.0f - *r_firstblend;
1481                         break;
1482                 }
1483         }
1484
1485         switch (cu->bevfac2_mapping) {
1486                 case CU_BEVFAC_MAP_RESOLU:
1487                 {
1488                         const float end_fl = cu->bevfac2 * (bl->nr - 1);
1489                         end = (int)end_fl;
1490
1491                         *r_steps = 2 + end - *r_start;
1492                         *r_lastblend = end_fl - end;
1493                         break;
1494                 }
1495                 case CU_BEVFAC_MAP_SEGMENT:
1496                 {
1497                         calc_bevfac_segment_mapping(bl, cu->bevfac2, total_length, &end, r_lastblend);
1498                         *r_steps = end - *r_start + 2;
1499                         break;
1500                 }
1501                 case CU_BEVFAC_MAP_SPLINE:
1502                 {
1503                         calc_bevfac_spline_mapping(bl, cu->bevfac2, total_length, &end, r_lastblend);
1504                         *r_steps = end - *r_start + 2;
1505                         break;
1506                 }
1507         }
1508
1509         if (end < *r_start || (end == *r_start && *r_lastblend < 1.0f - *r_firstblend )) {
1510                 SWAP(int, *r_start, end);
1511                 tmpf = *r_lastblend;
1512                 *r_lastblend = 1.0f - *r_firstblend;
1513                 *r_firstblend = 1.0f - tmpf;
1514                 *r_steps = end - *r_start + 2;
1515         }
1516
1517         if (*r_start + *r_steps > bl->nr) {
1518                 *r_steps = bl->nr - *r_start;
1519                 *r_lastblend = 1.0f;
1520         }
1521 }
1522
1523 static void do_makeDispListCurveTypes(
1524         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase,
1525         DerivedMesh **r_dm_final,
1526         const bool for_render, const bool for_orco, const bool use_render_resolution)
1527 {
1528         Curve *cu = ob->data;
1529
1530         /* we do allow duplis... this is only displist on curve level */
1531         if (!ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1532
1533         if (ob->type == OB_SURF) {
1534                 BKE_displist_make_surf(depsgraph, scene, ob, dispbase, r_dm_final, for_render, for_orco, use_render_resolution);
1535         }
1536         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1537                 ListBase dlbev;
1538                 ListBase nubase = {NULL, NULL};
1539
1540                 BKE_curve_bevelList_free(&ob->curve_cache->bev);
1541
1542                 /* We only re-evaluate path if evaluation is not happening for orco.
1543                  * If the calculation happens for orco, we should never free data which
1544                  * was needed before and only not needed for orco calculation.
1545                  */
1546                 if (!for_orco) {
1547                         if (ob->curve_cache->path) free_path(ob->curve_cache->path);
1548                         ob->curve_cache->path = NULL;
1549                 }
1550
1551                 if (ob->type == OB_FONT) {
1552                         BKE_vfont_to_curve_nubase(G.main, ob, FO_EDIT, &nubase);
1553                 }
1554                 else {
1555                         BKE_nurbList_duplicate(&nubase, BKE_curve_nurbs_get(cu));
1556                 }
1557
1558                 if (!for_orco)
1559                         curve_calc_modifiers_pre(depsgraph, scene, ob, &nubase, for_render, use_render_resolution);
1560
1561                 BKE_curve_bevelList_make(ob, &nubase, for_render != false);
1562
1563                 /* If curve has no bevel will return nothing */
1564                 BKE_curve_bevel_make(depsgraph, scene, ob, &dlbev, for_render, use_render_resolution);
1565
1566                 /* no bevel or extrude, and no width correction? */
1567                 if (!dlbev.first && cu->width == 1.0f) {
1568                         curve_to_displist(cu, &nubase, dispbase, for_render, use_render_resolution);
1569                 }
1570                 else {
1571                         float widfac = cu->width - 1.0f;
1572                         BevList *bl = ob->curve_cache->bev.first;
1573                         Nurb *nu = nubase.first;
1574
1575                         for (; bl && nu; bl = bl->next, nu = nu->next) {
1576                                 DispList *dl;
1577                                 float *data;
1578                                 int a;
1579
1580                                 if (bl->nr) { /* blank bevel lists can happen */
1581
1582                                         /* exception handling; curve without bevel or extrude, with width correction */
1583                                         if (BLI_listbase_is_empty(&dlbev)) {
1584                                                 BevPoint *bevp;
1585                                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbev");
1586                                                 dl->verts = MEM_mallocN(sizeof(float[3]) * bl->nr, "dlverts");
1587                                                 BLI_addtail(dispbase, dl);
1588
1589                                                 if (bl->poly != -1) dl->type = DL_POLY;
1590                                                 else dl->type = DL_SEGM;
1591
1592                                                 if (dl->type == DL_SEGM) dl->flag = (DL_FRONT_CURVE | DL_BACK_CURVE);
1593
1594                                                 dl->parts = 1;
1595                                                 dl->nr = bl->nr;
1596                                                 dl->col = nu->mat_nr;
1597                                                 dl->charidx = nu->charidx;
1598
1599                                                 /* dl->rt will be used as flag for render face and */
1600                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1601                                                 dl->rt = nu->flag & ~CU_2D;
1602
1603                                                 a = dl->nr;
1604                                                 bevp = bl->bevpoints;
1605                                                 data = dl->verts;
1606                                                 while (a--) {
1607                                                         data[0] = bevp->vec[0] + widfac * bevp->sina;
1608                                                         data[1] = bevp->vec[1] + widfac * bevp->cosa;
1609                                                         data[2] = bevp->vec[2];
1610                                                         bevp++;
1611                                                         data += 3;
1612                                                 }
1613                                         }
1614                                         else {
1615                                                 DispList *dlb;
1616                                                 ListBase bottom_capbase = {NULL, NULL};
1617                                                 ListBase top_capbase = {NULL, NULL};
1618                                                 float bottom_no[3] = {0.0f};
1619                                                 float top_no[3] = {0.0f};
1620                                                 float firstblend = 0.0f, lastblend = 0.0f;
1621                                                 int i, start, steps = 0;
1622
1623                                                 if (nu->flagu & CU_NURB_CYCLIC) {
1624                                                         calc_bevfac_mapping_default(bl,
1625                                                                                     &start, &firstblend, &steps, &lastblend);
1626                                                 }
1627                                                 else {
1628                                                         if (fabsf(cu->bevfac2 - cu->bevfac1) < FLT_EPSILON) {
1629                                                                 continue;
1630                                                         }
1631
1632                                                         calc_bevfac_mapping(cu, bl, nu, &start, &firstblend, &steps, &lastblend);
1633                                                 }
1634
1635                                                 for (dlb = dlbev.first; dlb; dlb = dlb->next) {
1636                                                         BevPoint *bevp_first, *bevp_last;
1637                                                         BevPoint *bevp;
1638
1639                                                         /* for each part of the bevel use a separate displblock */
1640                                                         dl = MEM_callocN(sizeof(DispList), "makeDispListbev1");
1641                                                         dl->verts = data = MEM_mallocN(sizeof(float[3]) * dlb->nr * steps, "dlverts");
1642                                                         BLI_addtail(dispbase, dl);
1643
1644                                                         dl->type = DL_SURF;
1645
1646                                                         dl->flag = dlb->flag & (DL_FRONT_CURVE | DL_BACK_CURVE);
1647                                                         if (dlb->type == DL_POLY) {
1648                                                                 dl->flag |= DL_CYCL_U;
1649                                                         }
1650                                                         if ((bl->poly >= 0) && (steps > 2)) {
1651                                                                 dl->flag |= DL_CYCL_V;
1652                                                         }
1653
1654                                                         dl->parts = steps;
1655                                                         dl->nr = dlb->nr;
1656                                                         dl->col = nu->mat_nr;
1657                                                         dl->charidx = nu->charidx;
1658
1659                                                         /* dl->rt will be used as flag for render face and */
1660                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1661                                                         dl->rt = nu->flag & ~CU_2D;
1662
1663                                                         dl->bevel_split = BLI_BITMAP_NEW(steps, "bevel_split");
1664
1665                                                         /* for each point of poly make a bevel piece */
1666                                                         bevp_first =  bl->bevpoints;
1667                                                         bevp_last  = &bl->bevpoints[bl->nr - 1];
1668                                                         bevp       = &bl->bevpoints[start];
1669                                                         for (i = start, a = 0; a < steps; i++, bevp++, a++) {
1670                                                                 float fac = 1.0;
1671                                                                 float *cur_data = data;
1672
1673                                                                 if (cu->taperobj == NULL) {
1674                                                                         fac = bevp->radius;
1675                                                                 }
1676                                                                 else {
1677                                                                         float len, taper_fac;
1678
1679                                                                         if (cu->flag & CU_MAP_TAPER) {
1680                                                                                 len = (steps - 3) + firstblend + lastblend;
1681
1682                                                                                 if (a == 0)
1683                                                                                         taper_fac = 0.0f;
1684                                                                                 else if (a == steps - 1)
1685                                                                                         taper_fac = 1.0f;
1686                                                                                 else
1687                                                                                         taper_fac = ((float) a - (1.0f - firstblend)) / len;
1688                                                                         }
1689                                                                         else {
1690                                                                                 len = bl->nr - 1;
1691                                                                                 taper_fac = (float) i / len;
1692
1693                                                                                 if (a == 0)
1694                                                                                         taper_fac += (1.0f - firstblend) / len;
1695                                                                                 else if (a == steps - 1)
1696                                                                                         taper_fac -= (1.0f - lastblend) / len;
1697                                                                         }
1698
1699                                                                         fac = displist_calc_taper(depsgraph, scene, cu->taperobj, taper_fac);
1700                                                                 }
1701
1702                                                                 if (bevp->split_tag) {
1703                                                                         BLI_BITMAP_ENABLE(dl->bevel_split, a);
1704                                                                 }
1705
1706                                                                 /* rotate bevel piece and write in data */
1707                                                                 if ((a == 0) && (bevp != bevp_last)) {
1708                                                                         rotateBevelPiece(cu, bevp, bevp + 1, dlb, 1.0f - firstblend, widfac, fac, &data);
1709                                                                 }
1710                                                                 else if ((a == steps - 1) && (bevp != bevp_first) ) {
1711                                                                         rotateBevelPiece(cu, bevp, bevp - 1, dlb, 1.0f - lastblend, widfac, fac, &data);
1712                                                                 }
1713                                                                 else {
1714                                                                         rotateBevelPiece(cu, bevp, NULL, dlb, 0.0f, widfac, fac, &data);
1715                                                                 }
1716
1717                                                                 if (cu->bevobj && (cu->flag & CU_FILL_CAPS) && !(nu->flagu & CU_NURB_CYCLIC)) {
1718                                                                         if (a == 1) {
1719                                                                                 fillBevelCap(nu, dlb, cur_data - 3 * dlb->nr, &bottom_capbase);
1720                                                                                 negate_v3_v3(bottom_no, bevp->dir);
1721                                                                         }
1722                                                                         if (a == steps - 1) {
1723                                                                                 fillBevelCap(nu, dlb, cur_data, &top_capbase);
1724                                                                                 copy_v3_v3(top_no, bevp->dir);
1725                                                                         }
1726                                                                 }
1727                                                         }
1728
1729                                                         /* gl array drawing: using indices */
1730                                                         displist_surf_indices(dl);
1731                                                 }
1732
1733                                                 if (bottom_capbase.first) {
1734                                                         BKE_displist_fill(&bottom_capbase, dispbase, bottom_no, false);
1735                                                         BKE_displist_fill(&top_capbase, dispbase, top_no, false);
1736                                                         BKE_displist_free(&bottom_capbase);
1737                                                         BKE_displist_free(&top_capbase);
1738                                                 }
1739                                         }
1740                                 }
1741
1742                         }
1743                         BKE_displist_free(&dlbev);
1744                 }
1745
1746                 if (!(cu->flag & CU_DEFORM_FILL)) {
1747                         curve_to_filledpoly(cu, &nubase, dispbase);
1748                 }
1749
1750                 if (!for_orco) {
1751                         if ((cu->flag & CU_PATH) ||
1752                             DEG_get_eval_flags_for_id(depsgraph, &ob->id) & DAG_EVAL_NEED_CURVE_PATH)
1753                         {
1754                                 calc_curvepath(ob, &nubase);
1755                         }
1756                 }
1757
1758                 if (!for_orco) {
1759                         BKE_nurbList_duplicate(&ob->curve_cache->deformed_nurbs, &nubase);
1760                         curve_calc_modifiers_post(depsgraph, scene, ob, &nubase, dispbase, r_dm_final, for_render, use_render_resolution);
1761                 }
1762
1763                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1764                         curve_to_filledpoly(cu, &nubase, dispbase);
1765                 }
1766
1767                 BKE_nurbList_free(&nubase);
1768         }
1769 }
1770
1771 void BKE_displist_make_curveTypes(Depsgraph *depsgraph, Scene *scene, Object *ob, const bool for_orco)
1772 {
1773         ListBase *dispbase;
1774
1775         /* The same check for duplis as in do_makeDispListCurveTypes.
1776          * Happens when curve used for constraint/bevel was converted to mesh.
1777          * check there is still needed for render displist and orco displists. */
1778         if (!ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT))
1779                 return;
1780
1781         BKE_object_free_derived_caches(ob);
1782
1783         if (!ob->curve_cache) {
1784                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for curve types");
1785         }
1786
1787         dispbase = &(ob->curve_cache->disp);
1788
1789         do_makeDispListCurveTypes(depsgraph, scene, ob, dispbase, &ob->derivedFinal, 0, for_orco, 0);
1790
1791         boundbox_displist_object(ob);
1792 }
1793
1794 void BKE_displist_make_curveTypes_forRender(
1795         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase,
1796         DerivedMesh **r_dm_final, const bool for_orco,
1797         const bool use_render_resolution)
1798 {
1799         if (ob->curve_cache == NULL) {
1800                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for Curve");
1801         }
1802
1803         do_makeDispListCurveTypes(depsgraph, scene, ob, dispbase, r_dm_final, true, for_orco, use_render_resolution);
1804 }
1805
1806 void BKE_displist_make_curveTypes_forOrco(
1807         Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *dispbase)
1808 {
1809         if (ob->curve_cache == NULL) {
1810                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for Curve");
1811         }
1812
1813         do_makeDispListCurveTypes(depsgraph, scene, ob, dispbase, NULL, 1, 1, 1);
1814 }
1815
1816 /* add Orco layer to the displist object which has got derived mesh and return orco */
1817 float *BKE_displist_make_orco(
1818         Depsgraph *depsgraph, Scene *scene, Object *ob, DerivedMesh *dm_final,
1819         const bool for_render,
1820         const bool use_render_resolution)
1821 {
1822         float *orco;
1823
1824         if (dm_final == NULL)
1825                 dm_final = ob->derivedFinal;
1826
1827         if (!dm_final->getVertDataArray(dm_final, CD_ORCO)) {
1828                 curve_calc_orcodm(depsgraph, scene, ob, dm_final, for_render, use_render_resolution);
1829         }
1830
1831         orco = dm_final->getVertDataArray(dm_final, CD_ORCO);
1832
1833         if (orco) {
1834                 orco = MEM_dupallocN(orco);
1835         }
1836
1837         return orco;
1838 }
1839
1840 void BKE_displist_minmax(ListBase *dispbase, float min[3], float max[3])
1841 {
1842         DispList *dl;
1843         const float *vert;
1844         int a, tot = 0;
1845         int doit = 0;
1846
1847         for (dl = dispbase->first; dl; dl = dl->next) {
1848                 tot = (dl->type == DL_INDEX3) ? dl->nr : dl->nr * dl->parts;
1849                 vert = dl->verts;
1850                 for (a = 0; a < tot; a++, vert += 3) {
1851                         minmax_v3v3_v3(min, max, vert);
1852                 }
1853                 doit |= (tot != 0);
1854         }
1855
1856         if (!doit) {
1857                 /* there's no geometry in displist, use zero-sized boundbox */
1858                 zero_v3(min);
1859                 zero_v3(max);
1860         }
1861 }
1862
1863 /* this is confusing, there's also min_max_object, appplying the obmat... */
1864 static void boundbox_displist_object(Object *ob)
1865 {
1866         if (ELEM(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1867                 /* Curve's BB is already calculated as a part of modifier stack,
1868                  * here we only calculate object BB based on final display list.
1869                  */
1870
1871                 /* object's BB is calculated from final displist */
1872                 if (ob->bb == NULL)
1873                         ob->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1874
1875                 if (ob->derivedFinal) {
1876                         DM_set_object_boundbox(ob, ob->derivedFinal);
1877                 }
1878                 else {
1879                         float min[3], max[3];
1880
1881                         INIT_MINMAX(min, max);
1882                         BKE_displist_minmax(&ob->curve_cache->disp, min, max);
1883                         BKE_boundbox_init_from_minmax(ob->bb, min, max);
1884
1885                         ob->bb->flag &= ~BOUNDBOX_DIRTY;
1886                 }
1887         }
1888 }