33cb2c0fb233df837a019400f756cd45e1908815
[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 static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1368                                       DerivedMesh **r_dm_final,
1369                                       const bool for_render, const bool for_orco, const bool use_render_resolution)
1370 {
1371         Curve *cu = ob->data;
1372
1373         /* we do allow duplis... this is only displist on curve level */
1374         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1375
1376         if (ob->type == OB_SURF) {
1377                 BKE_displist_make_surf(scene, ob, dispbase, r_dm_final, for_render, for_orco, use_render_resolution);
1378         }
1379         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1380                 ListBase dlbev;
1381                 ListBase nubase = {NULL, NULL};
1382
1383                 BLI_freelistN(&(ob->curve_cache->bev));
1384
1385                 /* We only re-evlauate path if evaluation is not happening for orco.
1386                  * If the calculation happens for orco, we should never free data which
1387                  * was needed before and only not needed for orco calculation.
1388                  */
1389                 if (!for_orco) {
1390                         if (ob->curve_cache->path) free_path(ob->curve_cache->path);
1391                         ob->curve_cache->path = NULL;
1392                 }
1393
1394                 if (ob->type == OB_FONT) {
1395                         BKE_vfont_to_curve_nubase(G.main, ob, FO_EDIT, &nubase);
1396                 }
1397                 else {
1398                         BKE_nurbList_duplicate(&nubase, BKE_curve_nurbs_get(cu));
1399                 }
1400
1401                 if (!for_orco)
1402                         curve_calc_modifiers_pre(scene, ob, &nubase, for_render, use_render_resolution);
1403
1404                 BKE_curve_bevelList_make(ob, &nubase, for_render != FALSE);
1405
1406                 /* If curve has no bevel will return nothing */
1407                 BKE_curve_bevel_make(scene, ob, &dlbev, for_render, use_render_resolution);
1408
1409                 /* no bevel or extrude, and no width correction? */
1410                 if (!dlbev.first && cu->width == 1.0f) {
1411                         curve_to_displist(cu, &nubase, dispbase, for_render, use_render_resolution);
1412                 }
1413                 else {
1414                         float widfac = cu->width - 1.0f;
1415                         BevList *bl = ob->curve_cache->bev.first;
1416                         Nurb *nu = nubase.first;
1417
1418                         for (; bl && nu; bl = bl->next, nu = nu->next) {
1419                                 DispList *dl;
1420                                 float *data;
1421                                 BevPoint *bevp;
1422                                 int a;
1423
1424                                 if (bl->nr) { /* blank bevel lists can happen */
1425
1426                                         /* exception handling; curve without bevel or extrude, with width correction */
1427                                         if (BLI_listbase_is_empty(&dlbev)) {
1428                                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbev");
1429                                                 dl->verts = MEM_callocN(3 * sizeof(float) * bl->nr, "dlverts");
1430                                                 BLI_addtail(dispbase, dl);
1431
1432                                                 if (bl->poly != -1) dl->type = DL_POLY;
1433                                                 else dl->type = DL_SEGM;
1434
1435                                                 if (dl->type == DL_SEGM) dl->flag = (DL_FRONT_CURVE | DL_BACK_CURVE);
1436
1437                                                 dl->parts = 1;
1438                                                 dl->nr = bl->nr;
1439                                                 dl->col = nu->mat_nr;
1440                                                 dl->charidx = nu->charidx;
1441
1442                                                 /* dl->rt will be used as flag for render face and */
1443                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1444                                                 dl->rt = nu->flag & ~CU_2D;
1445
1446                                                 a = dl->nr;
1447                                                 bevp = (BevPoint *)(bl + 1);
1448                                                 data = dl->verts;
1449                                                 while (a--) {
1450                                                         data[0] = bevp->vec[0] + widfac * bevp->sina;
1451                                                         data[1] = bevp->vec[1] + widfac * bevp->cosa;
1452                                                         data[2] = bevp->vec[2];
1453                                                         bevp++;
1454                                                         data += 3;
1455                                                 }
1456                                         }
1457                                         else {
1458                                                 DispList *dlb;
1459                                                 ListBase bottom_capbase = {NULL, NULL};
1460                                                 ListBase top_capbase = {NULL, NULL};
1461                                                 float bottom_no[3] = {0.0f};
1462                                                 float top_no[3] = {0.0f};
1463
1464                                                 for (dlb = dlbev.first; dlb; dlb = dlb->next) {
1465                                                         const float bevfac1 = min_ff(cu->bevfac1, cu->bevfac2);
1466                                                         const float bevfac2 = max_ff(cu->bevfac1, cu->bevfac2);
1467                                                         float firstblend = 0.0f, lastblend = 0.0f;
1468                                                         int i, start, steps;
1469
1470                                                         if (bevfac2 - bevfac1 == 0.0f)
1471                                                                 continue;
1472
1473                                                         start = (int)(bevfac1 * (bl->nr - 1));
1474                                                         steps = 2 + (int)((bevfac2) * (bl->nr - 1)) - start;
1475                                                         firstblend = 1.0f - (bevfac1 * (bl->nr - 1) - (int)(bevfac1 * (bl->nr - 1)));
1476                                                         lastblend  =         bevfac2 * (bl->nr - 1) - (int)(bevfac2 * (bl->nr - 1));
1477
1478                                                         if (start + steps > bl->nr) {
1479                                                                 steps = bl->nr - start;
1480                                                                 lastblend = 1.0f;
1481                                                         }
1482
1483                                                         /* for each part of the bevel use a separate displblock */
1484                                                         dl = MEM_callocN(sizeof(DispList), "makeDispListbev1");
1485                                                         dl->verts = data = MEM_callocN(3 * sizeof(float) * dlb->nr * steps, "dlverts");
1486                                                         BLI_addtail(dispbase, dl);
1487
1488                                                         dl->type = DL_SURF;
1489
1490                                                         dl->flag = dlb->flag & (DL_FRONT_CURVE | DL_BACK_CURVE);
1491                                                         if (dlb->type == DL_POLY) dl->flag |= DL_CYCL_U;
1492                                                         if (bl->poly >= 0) dl->flag |= DL_CYCL_V;
1493
1494                                                         dl->parts = steps;
1495                                                         dl->nr = dlb->nr;
1496                                                         dl->col = nu->mat_nr;
1497                                                         dl->charidx = nu->charidx;
1498
1499                                                         /* dl->rt will be used as flag for render face and */
1500                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1501                                                         dl->rt = nu->flag & ~CU_2D;
1502
1503                                                         dl->bevelSplitFlag = MEM_callocN(sizeof(*dl->col2) * ((steps + 0x1F) >> 5),
1504                                                                                          "bevelSplitFlag");
1505
1506                                                         /* for each point of poly make a bevel piece */
1507                                                         bevp = (BevPoint *)(bl + 1) + start;
1508                                                         for (i = start, a = 0; a < steps; i++, bevp++, a++) {
1509                                                                 float fac = 1.0;
1510                                                                 float *cur_data = data;
1511
1512                                                                 if (cu->taperobj == NULL) {
1513                                                                         fac = bevp->radius;
1514                                                                 }
1515                                                                 else {
1516                                                                         float len, taper_fac;
1517
1518                                                                         if (cu->flag & CU_MAP_TAPER) {
1519                                                                                 len = (steps - 3) + firstblend + lastblend;
1520
1521                                                                                 if (a == 0)
1522                                                                                         taper_fac = 0.0f;
1523                                                                                 else if (a == steps - 1)
1524                                                                                         taper_fac = 1.0f;
1525                                                                                 else
1526                                                                                         taper_fac = ((float) a - (1.0f - firstblend)) / len;
1527                                                                         }
1528                                                                         else {
1529                                                                                 len = bl->nr - 1;
1530                                                                                 taper_fac = (float) i / len;
1531
1532                                                                                 if (a == 0)
1533                                                                                         taper_fac += (1.0f - firstblend) / len;
1534                                                                                 else if (a == steps - 1)
1535                                                                                         taper_fac -= (1.0f - lastblend) / len;
1536                                                                         }
1537
1538                                                                         fac = displist_calc_taper(scene, cu->taperobj, taper_fac);
1539                                                                 }
1540
1541                                                                 if (bevp->split_tag) {
1542                                                                         dl->bevelSplitFlag[a >> 5] |= 1 << (a & 0x1F);
1543                                                                 }
1544
1545                                                                 /* rotate bevel piece and write in data */
1546                                                                 if (a == 0)
1547                                                                         rotateBevelPiece(cu, bevp, bevp + 1, dlb, 1.0f - firstblend, widfac, fac, &data);
1548                                                                 else if (a == steps - 1)
1549                                                                         rotateBevelPiece(cu, bevp, bevp - 1, dlb, 1.0f - lastblend, widfac, fac, &data);
1550                                                                 else
1551                                                                         rotateBevelPiece(cu, bevp, NULL, dlb, 0.0f, widfac, fac, &data);
1552
1553                                                                 if (cu->bevobj && (cu->flag & CU_FILL_CAPS) && !(nu->flagu & CU_NURB_CYCLIC)) {
1554                                                                         if (a == 1) {
1555                                                                                 fillBevelCap(nu, dlb, cur_data - 3 * dlb->nr, &bottom_capbase);
1556                                                                                 negate_v3_v3(bottom_no, bevp->dir);
1557                                                                         }
1558                                                                         if (a == steps - 1) {
1559                                                                                 fillBevelCap(nu, dlb, cur_data, &top_capbase);
1560                                                                                 copy_v3_v3(top_no, bevp->dir);
1561                                                                         }
1562                                                                 }
1563                                                         }
1564
1565                                                         /* gl array drawing: using indices */
1566                                                         displist_surf_indices(dl);
1567                                                 }
1568
1569                                                 if (bottom_capbase.first) {
1570                                                         BKE_displist_fill(&bottom_capbase, dispbase, bottom_no, false);
1571                                                         BKE_displist_fill(&top_capbase, dispbase, top_no, false);
1572                                                         BKE_displist_free(&bottom_capbase);
1573                                                         BKE_displist_free(&top_capbase);
1574                                                 }
1575                                         }
1576                                 }
1577
1578                         }
1579                         BKE_displist_free(&dlbev);
1580                 }
1581
1582                 if (!(cu->flag & CU_DEFORM_FILL)) {
1583                         curve_to_filledpoly(cu, &nubase, dispbase);
1584                 }
1585
1586                 if (!for_orco) {
1587                         if ((cu->flag & CU_PATH) ||
1588                             DAG_get_eval_flags_for_object(scene, ob) & DAG_EVAL_NEED_CURVE_PATH)
1589                         {
1590                                 calc_curvepath(ob, &nubase);
1591                         }
1592                 }
1593
1594                 if (!for_orco)
1595                         curve_calc_modifiers_post(scene, ob, &nubase, dispbase, r_dm_final, for_render, use_render_resolution);
1596
1597                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1598                         curve_to_filledpoly(cu, &nubase, dispbase);
1599                 }
1600
1601                 BKE_nurbList_free(&nubase);
1602         }
1603 }
1604
1605 void BKE_displist_make_curveTypes(Scene *scene, Object *ob, bool for_orco)
1606 {
1607         ListBase *dispbase;
1608
1609         /* The same check for duplis as in do_makeDispListCurveTypes.
1610          * Happens when curve used for constraint/bevel was converted to mesh.
1611          * check there is still needed for render displist and orco displists. */
1612         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT))
1613                 return;
1614
1615         BKE_object_free_derived_caches(ob);
1616
1617         if (!ob->curve_cache) {
1618                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for curve types");
1619         }
1620
1621         dispbase = &(ob->curve_cache->disp);
1622
1623         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, for_orco, 0);
1624
1625         boundbox_displist_object(ob);
1626 }
1627
1628 void BKE_displist_make_curveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1629                                       DerivedMesh **r_dm_final, const bool for_orco, const bool use_render_resolution)
1630 {
1631         if (ob->curve_cache == NULL) {
1632                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for MBall");
1633         }
1634
1635         do_makeDispListCurveTypes(scene, ob, dispbase, r_dm_final, true, for_orco, use_render_resolution);
1636 }
1637
1638 void BKE_displist_make_curveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1639 {
1640         if (ob->curve_cache == NULL) {
1641                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for MBall");
1642         }
1643
1644         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1, 1);
1645 }
1646
1647 /* add Orco layer to the displist object which has got derived mesh and return orco */
1648 float *BKE_displist_make_orco(Scene *scene, Object *ob, DerivedMesh *dm_final,
1649                               const bool for_render,
1650                               const bool use_render_resolution)
1651 {
1652         float *orco;
1653
1654         if (dm_final == NULL)
1655                 dm_final = ob->derivedFinal;
1656
1657         if (!dm_final->getVertDataArray(dm_final, CD_ORCO)) {
1658                 curve_calc_orcodm(scene, ob, dm_final, for_render, use_render_resolution);
1659         }
1660
1661         orco = dm_final->getVertDataArray(dm_final, CD_ORCO);
1662
1663         if (orco) {
1664                 orco = MEM_dupallocN(orco);
1665         }
1666
1667         return orco;
1668 }
1669
1670 void BKE_displist_minmax(ListBase *dispbase, float min[3], float max[3])
1671 {
1672         DispList *dl;
1673         float *vert;
1674         int a, tot = 0;
1675         int doit = 0;
1676
1677         for (dl = dispbase->first; dl; dl = dl->next) {
1678                 tot = (dl->type == DL_INDEX3) ? dl->nr : dl->nr * dl->parts;
1679                 vert = dl->verts;
1680                 for (a = 0; a < tot; a++, vert += 3) {
1681                         minmax_v3v3_v3(min, max, vert);
1682                 }
1683                 doit |= (tot != 0);
1684         }
1685
1686         if (!doit) {
1687                 /* there's no geometry in displist, use zero-sized boundbox */
1688                 zero_v3(min);
1689                 zero_v3(max);
1690         }
1691 }
1692
1693 /* this is confusing, there's also min_max_object, appplying the obmat... */
1694 static void boundbox_displist_object(Object *ob)
1695 {
1696         if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1697                 /* Curver's BB is already calculated as a part of modifier stack,
1698                  * here we only calculate object BB based on final display list.
1699                  */
1700
1701                 /* object's BB is calculated from final displist */
1702                 if (ob->bb == NULL)
1703                         ob->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1704
1705                 if (ob->derivedFinal) {
1706                         DM_set_object_boundbox(ob, ob->derivedFinal);
1707                 }
1708                 else {
1709                         float min[3], max[3];
1710
1711                         INIT_MINMAX(min, max);
1712                         BKE_displist_minmax(&ob->curve_cache->disp, min, max);
1713                         BKE_boundbox_init_from_minmax(ob->bb, min, max);
1714                 }
1715         }
1716 }