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