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