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