fix [#35795] Edge split modifier do not show result in viewport with curves.
[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
45 #include "BLI_blenlib.h"
46 #include "BLI_math.h"
47 #include "BLI_scanfill.h"
48 #include "BLI_utildefines.h"
49
50 #include "BKE_global.h"
51 #include "BKE_displist.h"
52 #include "BKE_cdderivedmesh.h"
53 #include "BKE_object.h"
54 #include "BKE_mball.h"
55 #include "BKE_material.h"
56 #include "BKE_curve.h"
57 #include "BKE_key.h"
58 #include "BKE_anim.h"
59 #include "BKE_font.h"
60 #include "BKE_lattice.h"
61 #include "BKE_modifier.h"
62
63 #include "BLI_sys_types.h" // for intptr_t support
64
65 static void boundbox_displist(Object *ob);
66 static void boundbox_dispbase(BoundBox *bb, ListBase *dispbase);
67
68 void BKE_displist_elem_free(DispList *dl)
69 {
70         if (dl) {
71                 if (dl->verts) MEM_freeN(dl->verts);
72                 if (dl->nors) MEM_freeN(dl->nors);
73                 if (dl->index) MEM_freeN(dl->index);
74                 if (dl->col1) MEM_freeN(dl->col1);
75                 if (dl->col2) MEM_freeN(dl->col2);
76                 if (dl->bevelSplitFlag) MEM_freeN(dl->bevelSplitFlag);
77                 MEM_freeN(dl);
78         }
79 }
80
81 void BKE_displist_free(ListBase *lb)
82 {
83         DispList *dl;
84
85         dl = lb->first;
86         while (dl) {
87                 BLI_remlink(lb, dl);
88                 BKE_displist_elem_free(dl);
89                 dl = lb->first;
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         DispList *dlnew = NULL, *dl;
458         float *f1;
459         int colnr = 0, charidx = 0, cont = 1, tot, a, *index, nextcol = 0;
460         intptr_t totvert;
461
462         if (dispbase == NULL)
463                 return;
464         if (dispbase->first == NULL)
465                 return;
466
467         while (cont) {
468                 cont = 0;
469                 totvert = 0;
470                 nextcol = 0;
471
472                 BLI_scanfill_begin(&sf_ctx);
473
474                 dl = dispbase->first;
475                 while (dl) {
476                         if (dl->type == DL_POLY) {
477                                 if (charidx < dl->charidx)
478                                         cont = 1;
479                                 else if (charidx == dl->charidx) { /* character with needed index */
480                                         if (colnr == dl->col) {
481                                                 /* make editverts and edges */
482                                                 f1 = dl->verts;
483                                                 a = dl->nr;
484                                                 sf_vert = sf_vert_new = NULL;
485
486                                                 while (a--) {
487                                                         sf_vert_last = sf_vert;
488
489                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, f1);
490                                                         totvert++;
491
492                                                         if (sf_vert_last == NULL)
493                                                                 sf_vert_new = sf_vert;
494                                                         else {
495                                                                 BLI_scanfill_edge_add(&sf_ctx, sf_vert_last, sf_vert);
496                                                         }
497                                                         f1 += 3;
498                                                 }
499
500                                                 if (sf_vert != NULL && sf_vert_new != NULL) {
501                                                         BLI_scanfill_edge_add(&sf_ctx, sf_vert, sf_vert_new);
502                                                 }
503                                         }
504                                         else if (colnr < dl->col) {
505                                                 /* got poly with next material at current char */
506                                                 cont = 1;
507                                                 nextcol = 1;
508                                         }
509                                 }
510                         }
511                         dl = dl->next;
512                 }
513
514                 /* XXX (obedit && obedit->actcol) ? (obedit->actcol-1) : 0)) { */
515                 if (totvert && (tot = BLI_scanfill_calc_ex(&sf_ctx,
516                                                            BLI_SCANFILL_CALC_REMOVE_DOUBLES | BLI_SCANFILL_CALC_HOLES,
517                                                            normal_proj)))
518                 {
519                         if (tot) {
520                                 dlnew = MEM_callocN(sizeof(DispList), "filldisplist");
521                                 dlnew->type = DL_INDEX3;
522                                 dlnew->col = colnr;
523                                 dlnew->nr = totvert;
524                                 dlnew->parts = tot;
525
526                                 dlnew->index = MEM_mallocN(tot * 3 * sizeof(int), "dlindex");
527                                 dlnew->verts = MEM_mallocN(totvert * 3 * sizeof(float), "dlverts");
528
529                                 /* vert data */
530                                 f1 = dlnew->verts;
531                                 totvert = 0;
532
533                                 for (sf_vert = sf_ctx.fillvertbase.first; sf_vert; sf_vert = sf_vert->next) {
534                                         copy_v3_v3(f1, sf_vert->co);
535                                         f1 += 3;
536
537                                         /* index number */
538                                         sf_vert->tmp.l = totvert;
539                                         totvert++;
540                                 }
541
542                                 /* index data */
543                                 sf_tri = sf_ctx.fillfacebase.first;
544                                 index = dlnew->index;
545                                 while (sf_tri) {
546                                         index[0] = (intptr_t)sf_tri->v1->tmp.l;
547                                         index[1] = (intptr_t)sf_tri->v2->tmp.l;
548                                         index[2] = (intptr_t)sf_tri->v3->tmp.l;
549
550                                         if (flipnormal)
551                                                 SWAP(int, index[0], index[2]);
552
553                                         index += 3;
554                                         sf_tri = sf_tri->next;
555                                 }
556                         }
557
558                         BLI_addhead(to, dlnew);
559                 }
560                 BLI_scanfill_end(&sf_ctx);
561
562                 if (nextcol) {
563                         /* stay at current char but fill polys with next material */
564                         colnr++;
565                 }
566                 else {
567                         /* switch to next char and start filling from first material */
568                         charidx++;
569                         colnr = 0;
570                 }
571         }
572
573         /* do not free polys, needed for wireframe display */
574 }
575
576 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
577 {
578         const float z_up[3] = {0.0f, 0.0f, 1.0f};
579         ListBase front, back;
580         DispList *dl, *dlnew;
581         float *fp, *fp1;
582         int a, dpoly;
583
584         front.first = front.last = back.first = back.last = NULL;
585
586         dl = dispbase->first;
587         while (dl) {
588                 if (dl->type == DL_SURF) {
589                         if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U) == 0) {
590                                 if ( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE)) {
591                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
592                                         BLI_addtail(&front, dlnew);
593                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
594                                         dlnew->nr = dl->parts;
595                                         dlnew->parts = 1;
596                                         dlnew->type = DL_POLY;
597                                         dlnew->col = dl->col;
598                                         dlnew->charidx = dl->charidx;
599
600                                         fp = dl->verts;
601                                         dpoly = 3 * dl->nr;
602
603                                         a = dl->parts;
604                                         while (a--) {
605                                                 copy_v3_v3(fp1, fp);
606                                                 fp1 += 3;
607                                                 fp += dpoly;
608                                         }
609                                 }
610                                 if ((cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE)) {
611                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
612                                         BLI_addtail(&back, dlnew);
613                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
614                                         dlnew->nr = dl->parts;
615                                         dlnew->parts = 1;
616                                         dlnew->type = DL_POLY;
617                                         dlnew->col = dl->col;
618                                         dlnew->charidx = dl->charidx;
619
620                                         fp = dl->verts + 3 * (dl->nr - 1);
621                                         dpoly = 3 * dl->nr;
622
623                                         a = dl->parts;
624                                         while (a--) {
625                                                 copy_v3_v3(fp1, fp);
626                                                 fp1 += 3;
627                                                 fp += dpoly;
628                                         }
629                                 }
630                         }
631                 }
632                 dl = dl->next;
633         }
634
635         BKE_displist_fill(&front, dispbase, z_up, true);
636         BKE_displist_fill(&back, dispbase, z_up, false);
637
638         BKE_displist_free(&front);
639         BKE_displist_free(&back);
640
641         BKE_displist_fill(dispbase, dispbase, z_up, false);
642 }
643
644 static void curve_to_filledpoly(Curve *cu, ListBase *UNUSED(nurb), ListBase *dispbase)
645 {
646         if (cu->flag & CU_3D)
647                 return;
648
649         if (dispbase->first && ((DispList *) dispbase->first)->type == DL_SURF) {
650                 bevels_to_filledpoly(cu, dispbase);
651         }
652         else {
653                 /* TODO, investigate passing zup instead of NULL */
654                 BKE_displist_fill(dispbase, dispbase, NULL, false);
655         }
656 }
657
658 /* taper rules:
659  * - only 1 curve
660  * - first point left, last point right
661  * - based on subdivided points in original curve, not on points in taper curve (still)
662  */
663 static float displist_calc_taper(Scene *scene, Object *taperobj, float fac)
664 {
665         DispList *dl;
666
667         if (taperobj == NULL || taperobj->type != OB_CURVE)
668                 return 1.0;
669
670         dl = taperobj->disp.first;
671         if (dl == NULL) {
672                 BKE_displist_make_curveTypes(scene, taperobj, 0);
673                 dl = taperobj->disp.first;
674         }
675         if (dl) {
676                 float minx, dx, *fp;
677                 int a;
678
679                 /* horizontal size */
680                 minx = dl->verts[0];
681                 dx = dl->verts[3 * (dl->nr - 1)] - minx;
682                 if (dx > 0.0f) {
683                         fp = dl->verts;
684                         for (a = 0; a < dl->nr; a++, fp += 3) {
685                                 if ((fp[0] - minx) / dx >= fac) {
686                                         /* interpolate with prev */
687                                         if (a > 0) {
688                                                 float fac1 = (fp[-3] - minx) / dx;
689                                                 float fac2 = (fp[0] - minx) / dx;
690                                                 if (fac1 != fac2)
691                                                         return fp[1] * (fac1 - fac) / (fac1 - fac2) + fp[-2] * (fac - fac2) / (fac1 - fac2);
692                                         }
693                                         return fp[1];
694                                 }
695                         }
696                         return fp[-2];  // last y coord
697                 }
698         }
699
700         return 1.0;
701 }
702
703 float BKE_displist_calc_taper(Scene *scene, Object *taperobj, int cur, int tot)
704 {
705         float fac = ((float)cur) / (float)(tot - 1);
706
707         return displist_calc_taper(scene, taperobj, fac);
708 }
709
710 void BKE_displist_make_mball(Scene *scene, Object *ob)
711 {
712         if (!ob || ob->type != OB_MBALL)
713                 return;
714
715         /* XXX: mball stuff uses plenty of global variables
716          *      while this is unchanged updating during render is unsafe
717          */
718         if (G.is_rendering)
719                 return;
720
721         BKE_displist_free(&(ob->disp));
722
723         if (ob->type == OB_MBALL) {
724                 if (ob == BKE_mball_basis_find(scene, ob)) {
725                         BKE_mball_polygonize(scene, ob, &ob->disp);
726                         BKE_mball_texspace_calc(ob);
727
728                         object_deform_mball(ob, &ob->disp);
729                 }
730         }
731
732         boundbox_displist(ob);
733 }
734
735 void BKE_displist_make_mball_forRender(Scene *scene, Object *ob, ListBase *dispbase)
736 {
737         BKE_mball_polygonize(scene, ob, dispbase);
738         BKE_mball_texspace_calc(ob);
739
740         object_deform_mball(ob, dispbase);
741 }
742
743 static ModifierData *curve_get_tessellate_point(Scene *scene, Object *ob, int renderResolution, int editmode)
744 {
745         ModifierData *md = modifiers_getVirtualModifierList(ob);
746         ModifierData *pretessellatePoint;
747         int required_mode;
748
749         if (renderResolution)
750                 required_mode = eModifierMode_Render;
751         else
752                 required_mode = eModifierMode_Realtime;
753
754         if (editmode)
755                 required_mode |= eModifierMode_Editmode;
756
757         pretessellatePoint = NULL;
758         for (; md; md = md->next) {
759                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
760
761                 if (!modifier_isEnabled(scene, md, required_mode))
762                         continue;
763                 if (mti->type == eModifierTypeType_Constructive)
764                         return pretessellatePoint;
765
766                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
767                         pretessellatePoint = md;
768
769                         /* this modifiers are moving point of tessellation automatically
770                          * (some of them even can't be applied on tessellated curve), set flag
771                          * for information button in modifier's header
772                          */
773                         md->mode |= eModifierMode_ApplyOnSpline;
774                 }
775                 else if (md->mode & eModifierMode_ApplyOnSpline) {
776                         pretessellatePoint = md;
777                 }
778         }
779
780         return pretessellatePoint;
781 }
782
783 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, int forRender, int renderResolution,
784                                      float (**originalVerts_r)[3],
785                                      float (**deformedVerts_r)[3], int *numVerts_r)
786 {
787         ModifierData *md = modifiers_getVirtualModifierList(ob);
788         ModifierData *pretessellatePoint;
789         Curve *cu = ob->data;
790         ListBase *nurb = BKE_curve_nurbs_get(cu);
791         int numVerts = 0;
792         const int editmode = (!forRender && (cu->editnurb || cu->editfont));
793         ModifierApplyFlag app_flag = 0;
794         float (*originalVerts)[3] = NULL;
795         float (*deformedVerts)[3] = NULL;
796         float *keyVerts = NULL;
797         int required_mode;
798
799         if (editmode)
800                 app_flag |= MOD_APPLY_USECACHE;
801         if (renderResolution) {
802                 app_flag |= MOD_APPLY_RENDER;
803                 required_mode = eModifierMode_Render;
804         }
805         else
806                 required_mode = eModifierMode_Realtime;
807
808         pretessellatePoint = curve_get_tessellate_point(scene, ob, renderResolution, editmode);
809
810         if (editmode)
811                 required_mode |= eModifierMode_Editmode;
812
813         if (cu->editnurb == NULL) {
814                 keyVerts = BKE_key_evaluate_object(scene, ob, &numVerts);
815
816                 if (keyVerts) {
817                         /* split coords from key data, the latter also includes
818                          * tilts, which is passed through in the modifier stack.
819                          * this is also the reason curves do not use a virtual
820                          * shape key modifier yet. */
821                         deformedVerts = BKE_curve_keyVertexCos_get(cu, nurb, keyVerts);
822                         originalVerts = MEM_dupallocN(deformedVerts);
823                         BLI_assert(BKE_nurbList_verts_count(nurb) == numVerts);
824                 }
825         }
826
827         if (pretessellatePoint) {
828                 for (; md; md = md->next) {
829                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
830
831                         md->scene = scene;
832
833                         if (!modifier_isEnabled(scene, md, required_mode))
834                                 continue;
835                         if (mti->type != eModifierTypeType_OnlyDeform)
836                                 continue;
837
838                         if (!deformedVerts) {
839                                 deformedVerts = BKE_curve_vertexCos_get(cu, nurb, &numVerts);
840                                 originalVerts = MEM_dupallocN(deformedVerts);
841                         }
842
843                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, app_flag);
844
845                         if (md == pretessellatePoint)
846                                 break;
847                 }
848         }
849
850         if (deformedVerts)
851                 BK_curve_vertexCos_apply(cu, nurb, deformedVerts);
852         if (keyVerts) /* these are not passed through modifier stack */
853                 BKE_curve_keyVertexTilts_apply(cu, nurb, keyVerts);
854
855         if (keyVerts)
856                 MEM_freeN(keyVerts);
857
858         *originalVerts_r = originalVerts;
859         *deformedVerts_r = deformedVerts;
860         *numVerts_r = numVerts;
861 }
862
863 static float (*displist_get_allverts(ListBase *dispbase, int *totvert))[3]
864 {
865         DispList *dl;
866         float (*allverts)[3], *fp;
867
868         *totvert = 0;
869
870         for (dl = dispbase->first; dl; dl = dl->next)
871                 *totvert += (dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr;
872
873         allverts = MEM_mallocN((*totvert) * sizeof(float) * 3, "displist_get_allverts allverts");
874         fp = (float *)allverts;
875         for (dl = dispbase->first; dl; dl = dl->next) {
876                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
877                 memcpy(fp, dl->verts, sizeof(float) * offs);
878                 fp += offs;
879         }
880
881         return allverts;
882 }
883
884 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
885 {
886         DispList *dl;
887         float *fp;
888
889         fp = (float *)allverts;
890         for (dl = dispbase->first; dl; dl = dl->next) {
891                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
892                 memcpy(dl->verts, fp, sizeof(float) * offs);
893                 fp += offs;
894         }
895 }
896
897 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispbase, DerivedMesh **derivedFinal,
898                                       int forRender, int renderResolution,
899                                       float (*originalVerts)[3], float (*deformedVerts)[3])
900 {
901         ModifierData *md = modifiers_getVirtualModifierList(ob);
902         ModifierData *pretessellatePoint;
903         Curve *cu = ob->data;
904         ListBase *nurb = BKE_curve_nurbs_get(cu);
905         int required_mode = 0, totvert = 0;
906         int editmode = (!forRender && (cu->editnurb || cu->editfont));
907         DerivedMesh *dm = NULL, *ndm;
908         float (*vertCos)[3] = NULL;
909         int useCache = !forRender;
910         ModifierApplyFlag app_flag = 0;
911
912         if (renderResolution) {
913                 app_flag |= MOD_APPLY_RENDER;
914                 required_mode = eModifierMode_Render;
915         }
916         else
917                 required_mode = eModifierMode_Realtime;
918
919         pretessellatePoint = curve_get_tessellate_point(scene, ob, renderResolution, editmode);
920
921         if (editmode)
922                 required_mode |= eModifierMode_Editmode;
923
924         if (pretessellatePoint) {
925                 md = pretessellatePoint->next;
926         }
927
928         if (derivedFinal && *derivedFinal) {
929                 (*derivedFinal)->release(*derivedFinal);
930         }
931
932         for (; md; md = md->next) {
933                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
934                 ModifierApplyFlag appf = app_flag;
935
936                 md->scene = scene;
937
938                 if (!modifier_isEnabled(scene, md, required_mode))
939                         continue;
940
941                 if (mti->type == eModifierTypeType_OnlyDeform ||
942                     (mti->type == eModifierTypeType_DeformOrConstruct && !dm))
943                 {
944                         if (editmode)
945                                 appf |= MOD_APPLY_USECACHE;
946                         if (dm) {
947                                 if (!vertCos) {
948                                         totvert = dm->getNumVerts(dm);
949                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
950                                         dm->getVertCos(dm, vertCos);
951                                 }
952
953                                 mti->deformVerts(md, ob, dm, vertCos, totvert, appf);
954                         }
955                         else {
956                                 if (!vertCos) {
957                                         vertCos = displist_get_allverts(dispbase, &totvert);
958                                 }
959
960                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, appf);
961                         }
962                 }
963                 else {
964                         if (!derivedFinal) {
965                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh
966                                  * is totally unnecessary, so we could stop modifiers applying
967                                  * when we found constructive modifier but derived mesh is unwanted result
968                                  */
969                                 break;
970                         }
971
972                         if (dm) {
973                                 if (vertCos) {
974                                         DerivedMesh *tdm = CDDM_copy(dm);
975                                         dm->release(dm);
976                                         dm = tdm;
977
978                                         CDDM_apply_vert_coords(dm, vertCos);
979                                 }
980                         }
981                         else {
982                                 if (vertCos) {
983                                         displist_apply_allverts(dispbase, vertCos);
984                                 }
985
986                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
987                                         curve_to_filledpoly(cu, nurb, dispbase);
988                                 }
989
990                                 dm = CDDM_from_curve_displist(ob, dispbase);
991                         }
992
993                         if (vertCos) {
994                                 /* Vertex coordinates were applied to necessary data, could free it */
995                                 MEM_freeN(vertCos);
996                                 vertCos = NULL;
997                         }
998
999                         if (useCache)
1000                                 appf |= MOD_APPLY_USECACHE;
1001
1002                         ndm = modwrap_applyModifier(md, ob, dm, appf);
1003
1004                         if (ndm) {
1005                                 /* Modifier returned a new derived mesh */
1006
1007                                 if (dm && dm != ndm) /* Modifier  */
1008                                         dm->release(dm);
1009                                 dm = ndm;
1010                         }
1011                 }
1012         }
1013
1014         if (vertCos) {
1015                 if (dm) {
1016                         DerivedMesh *tdm = CDDM_copy(dm);
1017                         dm->release(dm);
1018                         dm = tdm;
1019
1020                         CDDM_apply_vert_coords(dm, vertCos);
1021                         CDDM_calc_normals_mapping(dm);
1022                         MEM_freeN(vertCos);
1023                 }
1024                 else {
1025                         displist_apply_allverts(dispbase, vertCos);
1026                         MEM_freeN(vertCos);
1027                         vertCos = NULL;
1028                 }
1029         }
1030
1031         if (derivedFinal) {
1032                 if (dm) {
1033                         /* see: mesh_calc_modifiers */
1034                         if (dm->getNumTessFaces(dm) == 0) {
1035                                 dm->recalcTessellation(dm);
1036                         }
1037                         /* Even if tessellation is not needed, some modifiers might have modified CD layers
1038                          * (like mloopcol or mloopuv), hence we have to update those. */
1039                         else if (dm->dirty & DM_DIRTY_TESS_CDLAYERS) {
1040                                 DM_update_tessface_data(dm);
1041                         }
1042
1043                         CDDM_calc_normals_mapping_ex(dm, (dm->dirty & DM_DIRTY_NORMALS) ? false : true);
1044                 }
1045                 (*derivedFinal) = dm;
1046         }
1047
1048         if (deformedVerts) {
1049                 BK_curve_vertexCos_apply(ob->data, nurb, originalVerts);
1050                 MEM_freeN(originalVerts);
1051                 MEM_freeN(deformedVerts);
1052         }
1053 }
1054
1055 static void displist_surf_indices(DispList *dl)
1056 {
1057         int a, b, p1, p2, p3, p4;
1058         int *index;
1059
1060         dl->totindex = 0;
1061
1062         index = dl->index = MEM_mallocN(4 * sizeof(int) * (dl->parts + 1) * (dl->nr + 1), "index array nurbs");
1063
1064         for (a = 0; a < dl->parts; a++) {
1065
1066                 if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
1067                         break;
1068
1069                 for (; b < dl->nr; b++, index += 4) {
1070                         index[0] = p1;
1071                         index[1] = p2;
1072                         index[2] = p4;
1073                         index[3] = p3;
1074
1075                         dl->totindex++;
1076
1077                         p2 = p1; p1++;
1078                         p4 = p3; p3++;
1079                 }
1080         }
1081 }
1082
1083 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
1084 {
1085         DerivedMesh *dm;
1086         ListBase disp = {NULL, NULL};
1087
1088         /* OrcoDM should be created from underformed disp lists */
1089         BKE_displist_make_curveTypes_forOrco(scene, ob, &disp);
1090         dm = CDDM_from_curve_displist(ob, &disp);
1091
1092         BKE_displist_free(&disp);
1093
1094         return dm;
1095 }
1096
1097 static void add_orco_dm(Scene *scene, Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1098 {
1099         float (*orco)[3], (*layerorco)[3];
1100         int totvert, a;
1101         Curve *cu = ob->data;
1102
1103         totvert = dm->getNumVerts(dm);
1104
1105         if (orcodm) {
1106                 orco = MEM_callocN(sizeof(float) * 3 * totvert, "dm orco");
1107
1108                 if (orcodm->getNumVerts(orcodm) == totvert)
1109                         orcodm->getVertCos(orcodm, orco);
1110                 else
1111                         dm->getVertCos(dm, orco);
1112         }
1113         else {
1114                 int totvert_curve;
1115                 orco = (float(*)[3])BKE_curve_make_orco(scene, ob, &totvert_curve);
1116                 if (totvert != totvert_curve) {
1117                         MEM_freeN(orco);
1118                         orco = MEM_callocN(sizeof(float) * 3 * totvert, "dm orco");
1119                         dm->getVertCos(dm, orco);
1120                 }
1121         }
1122
1123         for (a = 0; a < totvert; a++) {
1124                 float *co = orco[a];
1125                 co[0] = (co[0] - cu->loc[0]) / cu->size[0];
1126                 co[1] = (co[1] - cu->loc[1]) / cu->size[1];
1127                 co[2] = (co[2] - cu->loc[2]) / cu->size[2];
1128         }
1129
1130         if ((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1131                 memcpy(layerorco, orco, sizeof(float) * totvert);
1132                 MEM_freeN(orco);
1133         }
1134         else
1135                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1136 }
1137
1138 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender, int renderResolution)
1139 {
1140         /* this function represents logic of mesh's orcodm calculation
1141          * for displist-based objects
1142          */
1143
1144         ModifierData *md = modifiers_getVirtualModifierList(ob);
1145         ModifierData *pretessellatePoint;
1146         Curve *cu = ob->data;
1147         int required_mode;
1148         int editmode = (!forRender && (cu->editnurb || cu->editfont));
1149         DerivedMesh *ndm, *orcodm = NULL;
1150         ModifierApplyFlag app_flag = MOD_APPLY_ORCO;
1151
1152         if (renderResolution) {
1153                 app_flag |= MOD_APPLY_RENDER;
1154                 required_mode = eModifierMode_Render;
1155         }
1156         else
1157                 required_mode = eModifierMode_Realtime;
1158
1159         pretessellatePoint = curve_get_tessellate_point(scene, ob, renderResolution, editmode);
1160
1161         if (editmode)
1162                 required_mode |= eModifierMode_Editmode;
1163
1164         if (pretessellatePoint) {
1165                 md = pretessellatePoint->next;
1166         }
1167
1168         for (; md; md = md->next) {
1169                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1170
1171                 md->scene = scene;
1172
1173                 if (!modifier_isEnabled(scene, md, required_mode))
1174                         continue;
1175                 if (mti->type != eModifierTypeType_Constructive)
1176                         continue;
1177
1178                 if (!orcodm)
1179                         orcodm = create_orco_dm(scene, ob);
1180
1181                 ndm = mti->applyModifier(md, ob, orcodm, app_flag);
1182
1183                 if (ndm) {
1184                         /* if the modifier returned a new dm, release the old one */
1185                         if (orcodm && orcodm != ndm) {
1186                                 orcodm->release(orcodm);
1187                         }
1188                         orcodm = ndm;
1189                 }
1190         }
1191
1192         /* add an orco layer if needed */
1193         add_orco_dm(scene, ob, derivedFinal, orcodm);
1194
1195         if (orcodm)
1196                 orcodm->release(orcodm);
1197 }
1198
1199 void BKE_displist_make_surf(Scene *scene, Object *ob, ListBase *dispbase,
1200                             DerivedMesh **derivedFinal, int forRender, int forOrco, int renderResolution)
1201 {
1202         ListBase *nubase;
1203         Nurb *nu;
1204         Curve *cu = ob->data;
1205         DispList *dl;
1206         float *data;
1207         int len;
1208         int numVerts;
1209         float (*originalVerts)[3];
1210         float (*deformedVerts)[3];
1211
1212         if (!forRender && cu->editnurb)
1213                 nubase = BKE_curve_editNurbs_get(cu);
1214         else
1215                 nubase = &cu->nurb;
1216
1217         if (!forOrco)
1218                 curve_calc_modifiers_pre(scene, ob, forRender, renderResolution, &originalVerts, &deformedVerts, &numVerts);
1219
1220         for (nu = nubase->first; nu; nu = nu->next) {
1221                 if (forRender || nu->hide == 0) {
1222                         int resolu = nu->resolu, resolv = nu->resolv;
1223
1224                         if (renderResolution) {
1225                                 if (cu->resolu_ren)
1226                                         resolu = cu->resolu_ren;
1227                                 if (cu->resolv_ren)
1228                                         resolv = cu->resolv_ren;
1229                         }
1230
1231                         if (nu->pntsv == 1) {
1232                                 len = SEGMENTSU(nu) * resolu;
1233
1234                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1235                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
1236
1237                                 BLI_addtail(dispbase, dl);
1238                                 dl->parts = 1;
1239                                 dl->nr = len;
1240                                 dl->col = nu->mat_nr;
1241                                 dl->charidx = nu->charidx;
1242
1243                                 /* dl->rt will be used as flag for render face and */
1244                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1245                                 dl->rt = nu->flag & ~CU_2D;
1246
1247                                 data = dl->verts;
1248                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
1249                                 else dl->type = DL_SEGM;
1250
1251                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
1252                         }
1253                         else {
1254                                 len = (nu->pntsu * resolu) * (nu->pntsv * resolv);
1255
1256                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1257                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
1258                                 BLI_addtail(dispbase, dl);
1259
1260                                 dl->col = nu->mat_nr;
1261                                 dl->charidx = nu->charidx;
1262
1263                                 /* dl->rt will be used as flag for render face and */
1264                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1265                                 dl->rt = nu->flag & ~CU_2D;
1266
1267                                 data = dl->verts;
1268                                 dl->type = DL_SURF;
1269
1270                                 dl->parts = (nu->pntsu * resolu);  /* in reverse, because makeNurbfaces works that way */
1271                                 dl->nr = (nu->pntsv * resolv);
1272                                 if (nu->flagv & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_U;  /* reverse too! */
1273                                 if (nu->flagu & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_V;
1274
1275                                 BKE_nurb_makeFaces(nu, data, 0, resolu, resolv);
1276
1277                                 /* gl array drawing: using indices */
1278                                 displist_surf_indices(dl);
1279                         }
1280                 }
1281         }
1282
1283         /* make copy of 'undeformed" displist for texture space calculation
1284          * actually, it's not totally undeformed -- pre-tessellation modifiers are
1285          * already applied, thats how it worked for years, so keep for compatibility (sergey) */
1286         BKE_displist_copy(&cu->disp, dispbase);
1287
1288         if (!forRender) {
1289                 BKE_curve_texspace_calc(cu);
1290         }
1291
1292         if (!forOrco) {
1293                 curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal,
1294                                           forRender, renderResolution,
1295                                           originalVerts, deformedVerts);
1296         }
1297 }
1298
1299 static void rotateBevelPiece(Curve *cu, BevPoint *bevp, BevPoint *nbevp, DispList *dlb, float bev_blend, float widfac, float fac, float **data_r)
1300 {
1301         float *fp, *data = *data_r;
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         *data_r = 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(3 * sizeof(float) * 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 do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1378                                       DerivedMesh **derivedFinal, int forRender, int forOrco, int renderResolution)
1379 {
1380         Curve *cu = ob->data;
1381
1382         /* we do allow duplis... this is only displist on curve level */
1383         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1384
1385         if (ob->type == OB_SURF) {
1386                 BKE_displist_make_surf(scene, ob, dispbase, derivedFinal, forRender, forOrco, renderResolution);
1387         }
1388         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1389                 ListBase dlbev;
1390                 ListBase *nubase;
1391                 float (*originalVerts)[3];
1392                 float (*deformedVerts)[3];
1393                 int numVerts;
1394
1395                 nubase = BKE_curve_nurbs_get(cu);
1396
1397                 BLI_freelistN(&(cu->bev));
1398
1399                 if (cu->path) free_path(cu->path);
1400                 cu->path = NULL;
1401
1402                 if (ob->type == OB_FONT)
1403                         BKE_vfont_to_curve(G.main, scene, ob, 0);
1404
1405                 if (!forOrco)
1406                         curve_calc_modifiers_pre(scene, ob, forRender, renderResolution, &originalVerts, &deformedVerts, &numVerts);
1407
1408                 BKE_curve_bevelList_make(ob);
1409
1410                 /* If curve has no bevel will return nothing */
1411                 BKE_curve_bevel_make(scene, ob, &dlbev, forRender, renderResolution);
1412
1413                 /* no bevel or extrude, and no width correction? */
1414                 if (!dlbev.first && cu->width == 1.0f) {
1415                         curve_to_displist(cu, nubase, dispbase, forRender, renderResolution);
1416                 }
1417                 else {
1418                         float widfac = cu->width - 1.0f;
1419                         BevList *bl = cu->bev.first;
1420                         Nurb *nu = nubase->first;
1421
1422                         for (; bl && nu; bl = bl->next, nu = nu->next) {
1423                                 DispList *dl;
1424                                 float *data;
1425                                 BevPoint *bevp;
1426                                 int a;
1427
1428                                 if (bl->nr) { /* blank bevel lists can happen */
1429
1430                                         /* exception handling; curve without bevel or extrude, with width correction */
1431                                         if (dlbev.first == NULL) {
1432                                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbev");
1433                                                 dl->verts = MEM_callocN(3 * sizeof(float) * bl->nr, "dlverts");
1434                                                 BLI_addtail(dispbase, dl);
1435
1436                                                 if (bl->poly != -1) dl->type = DL_POLY;
1437                                                 else dl->type = DL_SEGM;
1438
1439                                                 if (dl->type == DL_SEGM) dl->flag = (DL_FRONT_CURVE | DL_BACK_CURVE);
1440
1441                                                 dl->parts = 1;
1442                                                 dl->nr = bl->nr;
1443                                                 dl->col = nu->mat_nr;
1444                                                 dl->charidx = nu->charidx;
1445
1446                                                 /* dl->rt will be used as flag for render face and */
1447                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1448                                                 dl->rt = nu->flag & ~CU_2D;
1449
1450                                                 a = dl->nr;
1451                                                 bevp = (BevPoint *)(bl + 1);
1452                                                 data = dl->verts;
1453                                                 while (a--) {
1454                                                         data[0] = bevp->vec[0] + widfac * bevp->sina;
1455                                                         data[1] = bevp->vec[1] + widfac * bevp->cosa;
1456                                                         data[2] = bevp->vec[2];
1457                                                         bevp++;
1458                                                         data += 3;
1459                                                 }
1460                                         }
1461                                         else {
1462                                                 DispList *dlb;
1463                                                 ListBase bottom_capbase = {NULL, NULL};
1464                                                 ListBase top_capbase = {NULL, NULL};
1465                                                 float bottom_no[3] = {0.0f};
1466                                                 float top_no[3] = {0.0f};
1467
1468                                                 for (dlb = dlbev.first; dlb; dlb = dlb->next) {
1469                                                         const float bevfac1 = min_ff(cu->bevfac1, cu->bevfac2);
1470                                                         const float bevfac2 = max_ff(cu->bevfac1, cu->bevfac2);
1471                                                         float firstblend = 0.0f, lastblend = 0.0f;
1472                                                         int i, start, steps;
1473
1474                                                         if (bevfac2 - bevfac1 == 0.0f)
1475                                                                 continue;
1476
1477                                                         start = (int)(bevfac1 * (bl->nr - 1));
1478                                                         steps = 2 + (int)((bevfac2) * (bl->nr - 1)) - start;
1479                                                         firstblend = 1.0f - (bevfac1 * (bl->nr - 1) - (int)(bevfac1 * (bl->nr - 1)));
1480                                                         lastblend  =         bevfac2 * (bl->nr - 1) - (int)(bevfac2 * (bl->nr - 1));
1481
1482                                                         if (start + steps > bl->nr) {
1483                                                                 steps = bl->nr - start;
1484                                                                 lastblend = 1.0f;
1485                                                         }
1486
1487                                                         /* for each part of the bevel use a separate displblock */
1488                                                         dl = MEM_callocN(sizeof(DispList), "makeDispListbev1");
1489                                                         dl->verts = data = MEM_callocN(3 * sizeof(float) * dlb->nr * steps, "dlverts");
1490                                                         BLI_addtail(dispbase, dl);
1491
1492                                                         dl->type = DL_SURF;
1493
1494                                                         dl->flag = dlb->flag & (DL_FRONT_CURVE | DL_BACK_CURVE);
1495                                                         if (dlb->type == DL_POLY) dl->flag |= DL_CYCL_U;
1496                                                         if (bl->poly >= 0) dl->flag |= DL_CYCL_V;
1497
1498                                                         dl->parts = steps;
1499                                                         dl->nr = dlb->nr;
1500                                                         dl->col = nu->mat_nr;
1501                                                         dl->charidx = nu->charidx;
1502
1503                                                         /* dl->rt will be used as flag for render face and */
1504                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1505                                                         dl->rt = nu->flag & ~CU_2D;
1506
1507                                                         dl->bevelSplitFlag = MEM_callocN(sizeof(*dl->col2) * ((steps + 0x1F) >> 5),
1508                                                                                          "bevelSplitFlag");
1509
1510                                                         /* for each point of poly make a bevel piece */
1511                                                         bevp = (BevPoint *)(bl + 1) + start;
1512                                                         for (i = start, a = 0; a < steps; i++, bevp++, a++) {
1513                                                                 float fac = 1.0;
1514                                                                 float *cur_data = data;
1515
1516                                                                 if (cu->taperobj == NULL) {
1517                                                                         fac = bevp->radius;
1518                                                                 }
1519                                                                 else {
1520                                                                         float len, taper_fac;
1521
1522                                                                         if (cu->flag & CU_MAP_TAPER) {
1523                                                                                 len = (steps - 3) + firstblend + lastblend;
1524
1525                                                                                 if (a == 0)
1526                                                                                         taper_fac = 0.0f;
1527                                                                                 else if (a == steps - 1)
1528                                                                                         taper_fac = 1.0f;
1529                                                                                 else
1530                                                                                         taper_fac = ((float) a - (1.0f - firstblend)) / len;
1531                                                                         }
1532                                                                         else {
1533                                                                                 len = bl->nr - 1;
1534                                                                                 taper_fac = (float) i / len;
1535
1536                                                                                 if (a == 0)
1537                                                                                         taper_fac += (1.0f - firstblend) / len;
1538                                                                                 else if (a == steps - 1)
1539                                                                                         taper_fac -= (1.0f - lastblend) / len;
1540                                                                         }
1541
1542                                                                         fac = displist_calc_taper(scene, cu->taperobj, taper_fac);
1543                                                                 }
1544
1545                                                                 if (bevp->split_tag) {
1546                                                                         dl->bevelSplitFlag[a >> 5] |= 1 << (a & 0x1F);
1547                                                                 }
1548
1549                                                                 /* rotate bevel piece and write in data */
1550                                                                 if (a == 0)
1551                                                                         rotateBevelPiece(cu, bevp, bevp + 1, dlb, 1.0f - firstblend, widfac, fac, &data);
1552                                                                 else if (a == steps - 1)
1553                                                                         rotateBevelPiece(cu, bevp, bevp - 1, dlb, 1.0f - lastblend, widfac, fac, &data);
1554                                                                 else
1555                                                                         rotateBevelPiece(cu, bevp, NULL, dlb, 0.0f, widfac, fac, &data);
1556
1557                                                                 if (cu->bevobj && (cu->flag & CU_FILL_CAPS) && !(nu->flagu & CU_NURB_CYCLIC)) {
1558                                                                         if (a == 1) {
1559                                                                                 fillBevelCap(nu, dlb, cur_data - 3 * dlb->nr, &bottom_capbase);
1560                                                                                 negate_v3_v3(bottom_no, bevp->dir);
1561                                                                         }
1562                                                                         if (a == steps - 1) {
1563                                                                                 fillBevelCap(nu, dlb, cur_data, &top_capbase);
1564                                                                                 copy_v3_v3(top_no, bevp->dir);
1565                                                                         }
1566                                                                 }
1567                                                         }
1568
1569                                                         /* gl array drawing: using indices */
1570                                                         displist_surf_indices(dl);
1571                                                 }
1572
1573                                                 if (bottom_capbase.first) {
1574                                                         BKE_displist_fill(&bottom_capbase, dispbase, bottom_no, false);
1575                                                         BKE_displist_fill(&top_capbase, dispbase, top_no, false);
1576                                                         BKE_displist_free(&bottom_capbase);
1577                                                         BKE_displist_free(&top_capbase);
1578                                                 }
1579                                         }
1580                                 }
1581
1582                         }
1583                         BKE_displist_free(&dlbev);
1584                 }
1585
1586                 if (!(cu->flag & CU_DEFORM_FILL)) {
1587                         curve_to_filledpoly(cu, nubase, dispbase);
1588                 }
1589
1590                 if ((cu->flag & CU_PATH) && !forOrco)
1591                         calc_curvepath(ob);
1592
1593                 /* make copy of 'undeformed" displist for texture space calculation
1594                  * actually, it's not totally undeformed -- pre-tessellation modifiers are
1595                  * already applied, thats how it worked for years, so keep for compatibility (sergey) */
1596                 BKE_displist_copy(&cu->disp, dispbase);
1597
1598                 if (!forRender) {
1599                         BKE_curve_texspace_calc(cu);
1600                 }
1601
1602                 if (!forOrco)
1603                         curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal, forRender, renderResolution, originalVerts, deformedVerts);
1604
1605                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1606                         curve_to_filledpoly(cu, nubase, dispbase);
1607                 }
1608         }
1609 }
1610
1611 void BKE_displist_make_curveTypes(Scene *scene, Object *ob, int forOrco)
1612 {
1613         Curve *cu = ob->data;
1614         ListBase *dispbase;
1615
1616         /* The same check for duplis as in do_makeDispListCurveTypes.
1617          * Happens when curve used for constraint/bevel was converted to mesh.
1618          * check there is still needed for render displist and orco displists. */
1619         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT))
1620                 return;
1621
1622         BKE_displist_free(&(ob->disp));
1623         dispbase = &(ob->disp);
1624         BKE_displist_free(dispbase);
1625
1626         /* free displist used for textspace */
1627         BKE_displist_free(&cu->disp);
1628
1629         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco, 0);
1630
1631         if (ob->derivedFinal) {
1632                 DM_set_object_boundbox(ob, ob->derivedFinal);
1633
1634                 /* always keep curve's  BB in sync with non-deformed displist */
1635                 if (cu->bb == NULL)
1636                         cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1637
1638                 boundbox_dispbase(cu->bb, &cu->disp);
1639         }
1640         else {
1641                 boundbox_displist(ob);
1642         }
1643 }
1644
1645 void BKE_displist_make_curveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1646                                       DerivedMesh **derivedFinal, int forOrco, int renderResolution)
1647 {
1648         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco, renderResolution);
1649 }
1650
1651 void BKE_displist_make_curveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1652 {
1653         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1, 1);
1654 }
1655
1656 /* add Orco layer to the displist object which has got derived mesh and return orco */
1657 float *BKE_displist_make_orco(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender, int renderResolution)
1658 {
1659         float *orco;
1660
1661         if (derivedFinal == NULL)
1662                 derivedFinal = ob->derivedFinal;
1663
1664         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1665                 curve_calc_orcodm(scene, ob, derivedFinal, forRender, renderResolution);
1666         }
1667
1668         orco = derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1669
1670         if (orco) {
1671                 orco = MEM_dupallocN(orco);
1672         }
1673
1674         return orco;
1675 }
1676
1677 static void boundbox_dispbase(BoundBox *bb, ListBase *dispbase)
1678 {
1679         float min[3], max[3];
1680         DispList *dl;
1681         float *vert;
1682         int a, tot = 0;
1683         int doit = 0;
1684
1685         INIT_MINMAX(min, max);
1686
1687         for (dl = dispbase->first; dl; dl = dl->next) {
1688                 tot = (dl->type == DL_INDEX3) ? dl->nr : dl->nr * dl->parts;
1689                 vert = dl->verts;
1690                 for (a = 0; a < tot; a++, vert += 3) {
1691                         minmax_v3v3_v3(min, max, vert);
1692                 }
1693                 doit |= (tot != 0);
1694         }
1695
1696         if (!doit) {
1697                 /* there's no geometry in displist, use zero-sized boundbox */
1698                 zero_v3(min);
1699                 zero_v3(max);
1700         }
1701
1702         BKE_boundbox_init_from_minmax(bb, min, max);
1703 }
1704
1705 /* this is confusing, there's also min_max_object, appplying the obmat... */
1706 static void boundbox_displist(Object *ob)
1707 {
1708         if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1709                 Curve *cu = ob->data;
1710
1711                 /* calculate curve's BB based on non-deformed displist */
1712                 if (cu->bb == NULL)
1713                         cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1714
1715                 boundbox_dispbase(cu->bb, &cu->disp);
1716
1717                 /* object's BB is calculated from final displist */
1718                 if (ob->bb == NULL)
1719                         ob->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1720
1721                 boundbox_dispbase(ob->bb, &ob->disp);
1722         }
1723 }