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