Remove unneeded include, was a residual from some WIP patch in the past
[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(Object *ob);
66
67 void BKE_displist_elem_free(DispList *dl)
68 {
69         if (dl) {
70                 if (dl->verts) MEM_freeN(dl->verts);
71                 if (dl->nors) MEM_freeN(dl->nors);
72                 if (dl->index) MEM_freeN(dl->index);
73                 if (dl->col1) MEM_freeN(dl->col1);
74                 if (dl->col2) MEM_freeN(dl->col2);
75                 if (dl->bevelSplitFlag) MEM_freeN(dl->bevelSplitFlag);
76                 MEM_freeN(dl);
77         }
78 }
79
80 void BKE_displist_free(ListBase *lb)
81 {
82         DispList *dl;
83
84         dl = lb->first;
85         while (dl) {
86                 BLI_remlink(lb, dl);
87                 BKE_displist_elem_free(dl);
88                 dl = lb->first;
89         }
90 }
91
92 DispList *BKE_displist_find_or_create(ListBase *lb, int type)
93 {
94         DispList *dl;
95
96         dl = lb->first;
97         while (dl) {
98                 if (dl->type == type)
99                         return dl;
100                 dl = dl->next;
101         }
102
103         dl = MEM_callocN(sizeof(DispList), "find_disp");
104         dl->type = type;
105         BLI_addtail(lb, dl);
106
107         return dl;
108 }
109
110 DispList *BKE_displist_find(ListBase *lb, int type)
111 {
112         DispList *dl;
113
114         dl = lb->first;
115         while (dl) {
116                 if (dl->type == type)
117                         return dl;
118                 dl = dl->next;
119         }
120
121         return NULL;
122 }
123
124 bool BKE_displist_has_faces(ListBase *lb)
125 {
126         DispList *dl;
127
128         for (dl = lb->first; dl; dl = dl->next) {
129                 if (ELEM3(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)) {
130                         return true;
131                 }
132         }
133
134         return false;
135 }
136
137 void BKE_displist_copy(ListBase *lbn, ListBase *lb)
138 {
139         DispList *dln, *dl;
140
141         BKE_displist_free(lbn);
142
143         dl = lb->first;
144         while (dl) {
145                 dln = MEM_dupallocN(dl);
146                 BLI_addtail(lbn, dln);
147                 dln->verts = MEM_dupallocN(dl->verts);
148                 dln->nors = MEM_dupallocN(dl->nors);
149                 dln->index = MEM_dupallocN(dl->index);
150                 dln->col1 = MEM_dupallocN(dl->col1);
151                 dln->col2 = MEM_dupallocN(dl->col2);
152
153                 if (dl->bevelSplitFlag)
154                         dln->bevelSplitFlag = MEM_dupallocN(dl->bevelSplitFlag);
155
156                 dl = dl->next;
157         }
158 }
159
160 void BKE_displist_normals_add(ListBase *lb)
161 {
162         DispList *dl = NULL;
163         float *vdata, *ndata, nor[3];
164         float *v1, *v2, *v3, *v4;
165         float *n1, *n2, *n3, *n4;
166         int a, b, p1, p2, p3, p4;
167
168         dl = lb->first;
169
170         while (dl) {
171                 if (dl->type == DL_INDEX3) {
172                         if (dl->nors == NULL) {
173                                 dl->nors = MEM_callocN(sizeof(float) * 3, "dlnors");
174
175                                 if (dl->verts[2] < 0.0f)
176                                         dl->nors[2] = -1.0f;
177                                 else
178                                         dl->nors[2] = 1.0f;
179                         }
180                 }
181                 else if (dl->type == DL_SURF) {
182                         if (dl->nors == NULL) {
183                                 dl->nors = MEM_callocN(sizeof(float) * 3 * dl->nr * dl->parts, "dlnors");
184
185                                 vdata = dl->verts;
186                                 ndata = dl->nors;
187
188                                 for (a = 0; a < dl->parts; a++) {
189
190                                         if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
191                                                 break;
192
193                                         v1 = vdata + 3 * p1;
194                                         n1 = ndata + 3 * p1;
195                                         v2 = vdata + 3 * p2;
196                                         n2 = ndata + 3 * p2;
197                                         v3 = vdata + 3 * p3;
198                                         n3 = ndata + 3 * p3;
199                                         v4 = vdata + 3 * p4;
200                                         n4 = ndata + 3 * p4;
201
202                                         for (; b < dl->nr; b++) {
203                                                 normal_quad_v3(nor, v1, v3, v4, v2);
204
205                                                 add_v3_v3(n1, nor);
206                                                 add_v3_v3(n2, nor);
207                                                 add_v3_v3(n3, nor);
208                                                 add_v3_v3(n4, nor);
209
210                                                 v2 = v1; v1 += 3;
211                                                 v4 = v3; v3 += 3;
212                                                 n2 = n1; n1 += 3;
213                                                 n4 = n3; n3 += 3;
214                                         }
215                                 }
216                                 a = dl->parts * dl->nr;
217                                 v1 = ndata;
218                                 while (a--) {
219                                         normalize_v3(v1);
220                                         v1 += 3;
221                                 }
222                         }
223                 }
224                 dl = dl->next;
225         }
226 }
227
228 void BKE_displist_count(ListBase *lb, int *totvert, int *totface, int *tottri)
229 {
230         DispList *dl;
231
232         for (dl = lb->first; dl; dl = dl->next) {
233                 int vert_tot = 0;
234                 int face_tot = 0;
235                 int tri_tot = 0;
236
237                 switch (dl->type) {
238                         case DL_SURF:
239                         {
240                                 vert_tot = dl->nr * dl->parts;
241                                 face_tot = (dl->nr - 1) * (dl->parts - 1);
242                                 tri_tot  = face_tot * 2;
243                                 break;
244                         }
245                         case DL_INDEX3:
246                         {
247                                 vert_tot = dl->nr;
248                                 face_tot = dl->parts;
249                                 tri_tot  = face_tot;
250                                 break;
251                         }
252                         case DL_INDEX4:
253                         {
254                                 vert_tot = dl->nr;
255                                 face_tot = dl->parts;
256                                 tri_tot  = face_tot * 2;
257                                 break;
258                         }
259                         case DL_POLY:
260                         case DL_SEGM:
261                         {
262                                 vert_tot = dl->nr * dl->parts;
263                                 break;
264                         }
265                 }
266
267                 *totvert += vert_tot;
268                 *totface += face_tot;
269                 *tottri  += tri_tot;
270         }
271 }
272
273 bool BKE_displist_surfindex_get(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4)
274 {
275         if ((dl->flag & DL_CYCL_V) == 0 && a == (dl->parts) - 1) {
276                 return false;
277         }
278
279         if (dl->flag & DL_CYCL_U) {
280                 (*p1) = dl->nr * a;
281                 (*p2) = (*p1) + dl->nr - 1;
282                 (*p3) = (*p1) + dl->nr;
283                 (*p4) = (*p2) + dl->nr;
284                 (*b) = 0;
285         }
286         else {
287                 (*p2) = dl->nr * a;
288                 (*p1) = (*p2) + 1;
289                 (*p4) = (*p2) + dl->nr;
290                 (*p3) = (*p1) + dl->nr;
291                 (*b) = 1;
292         }
293
294         if ((dl->flag & DL_CYCL_V) && a == dl->parts - 1) {
295                 (*p3) -= dl->nr * dl->parts;
296                 (*p4) -= dl->nr * dl->parts;
297         }
298
299         return true;
300 }
301
302 /* ****************** make displists ********************* */
303
304 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase, int forRender, int renderResolution)
305 {
306         Nurb *nu;
307         DispList *dl;
308         BezTriple *bezt, *prevbezt;
309         BPoint *bp;
310         float *data;
311         int a, len, resolu;
312         const int editmode = (!forRender && (cu->editnurb || cu->editfont));
313
314         nu = nubase->first;
315         while (nu) {
316                 if (nu->hide == 0 || editmode == 0) {
317                         if (renderResolution && cu->resolu_ren != 0)
318                                 resolu = cu->resolu_ren;
319                         else
320                                 resolu = nu->resolu;
321
322                         if (!BKE_nurb_check_valid_u(nu)) {
323                                 /* pass */
324                         }
325                         else if (nu->type == CU_BEZIER) {
326                                 /* count */
327                                 len = 0;
328                                 a = nu->pntsu - 1;
329                                 if (nu->flagu & CU_NURB_CYCLIC) a++;
330
331                                 prevbezt = nu->bezt;
332                                 bezt = prevbezt + 1;
333                                 while (a--) {
334                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC))
335                                                 bezt = nu->bezt;
336
337                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT)
338                                                 len++;
339                                         else
340                                                 len += resolu;
341
342                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC) == 0)
343                                                 len++;
344
345                                         prevbezt = bezt;
346                                         bezt++;
347                                 }
348
349                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbez");
350                                 /* len+1 because of 'forward_diff_bezier' function */
351                                 dl->verts = MEM_callocN((len + 1) * 3 * sizeof(float), "dlverts");
352                                 BLI_addtail(dispbase, dl);
353                                 dl->parts = 1;
354                                 dl->nr = len;
355                                 dl->col = nu->mat_nr;
356                                 dl->charidx = nu->charidx;
357
358                                 data = dl->verts;
359
360                                 if (nu->flagu & CU_NURB_CYCLIC) {
361                                         dl->type = DL_POLY;
362                                         a = nu->pntsu;
363                                 }
364                                 else {
365                                         dl->type = DL_SEGM;
366                                         a = nu->pntsu - 1;
367                                 }
368
369                                 prevbezt = nu->bezt;
370                                 bezt = prevbezt + 1;
371
372                                 while (a--) {
373                                         if (a == 0 && dl->type == DL_POLY)
374                                                 bezt = nu->bezt;
375
376                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
377                                                 copy_v3_v3(data, prevbezt->vec[1]);
378                                                 data += 3;
379                                         }
380                                         else {
381                                                 int j;
382                                                 for (j = 0; j < 3; j++) {
383                                                         BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],
384                                                                                       prevbezt->vec[2][j],
385                                                                                       bezt->vec[0][j],
386                                                                                       bezt->vec[1][j],
387                                                                                       data + j, resolu, 3 * sizeof(float));
388                                                 }
389
390                                                 data += 3 * resolu;
391                                         }
392
393                                         if (a == 0 && dl->type == DL_SEGM) {
394                                                 copy_v3_v3(data, bezt->vec[1]);
395                                         }
396
397                                         prevbezt = bezt;
398                                         bezt++;
399                                 }
400                         }
401                         else if (nu->type == CU_NURBS) {
402                                 len = (resolu * SEGMENTSU(nu));
403
404                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
405                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
406                                 BLI_addtail(dispbase, dl);
407                                 dl->parts = 1;
408
409                                 dl->nr = len;
410                                 dl->col = nu->mat_nr;
411                                 dl->charidx = nu->charidx;
412
413                                 data = dl->verts;
414                                 if (nu->flagu & CU_NURB_CYCLIC)
415                                         dl->type = DL_POLY;
416                                 else dl->type = DL_SEGM;
417                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
418                         }
419                         else if (nu->type == CU_POLY) {
420                                 len = nu->pntsu;
421                                 dl = MEM_callocN(sizeof(DispList), "makeDispListpoly");
422                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
423                                 BLI_addtail(dispbase, dl);
424                                 dl->parts = 1;
425                                 dl->nr = len;
426                                 dl->col = nu->mat_nr;
427                                 dl->charidx = nu->charidx;
428
429                                 data = dl->verts;
430                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
431                                 else dl->type = DL_SEGM;
432
433                                 a = len;
434                                 bp = nu->bp;
435                                 while (a--) {
436                                         copy_v3_v3(data, bp->vec);
437                                         bp++;
438                                         data += 3;
439                                 }
440                         }
441                 }
442                 nu = nu->next;
443         }
444 }
445
446 /**
447  * \param normal_proj  Optional normal thats used to project the scanfill verts into 2d coords.
448  * Pass this along if known since it saves time calculating the normal.
449  * \param flipnormal  Flip the normal (same as passing \a normal_proj negated)
450  */
451 void BKE_displist_fill(ListBase *dispbase, ListBase *to, const float normal_proj[3], const bool flipnormal)
452 {
453         ScanFillContext sf_ctx;
454         ScanFillVert *sf_vert, *sf_vert_new, *sf_vert_last;
455         ScanFillFace *sf_tri;
456         DispList *dlnew = NULL, *dl;
457         float *f1;
458         int colnr = 0, charidx = 0, cont = 1, tot, a, *index, nextcol = 0;
459         intptr_t totvert;
460
461         if (dispbase == NULL)
462                 return;
463         if (dispbase->first == NULL)
464                 return;
465
466         while (cont) {
467                 cont = 0;
468                 totvert = 0;
469                 nextcol = 0;
470
471                 BLI_scanfill_begin(&sf_ctx);
472
473                 dl = dispbase->first;
474                 while (dl) {
475                         if (dl->type == DL_POLY) {
476                                 if (charidx < dl->charidx)
477                                         cont = 1;
478                                 else if (charidx == dl->charidx) { /* character with needed index */
479                                         if (colnr == dl->col) {
480                                                 /* make editverts and edges */
481                                                 f1 = dl->verts;
482                                                 a = dl->nr;
483                                                 sf_vert = sf_vert_new = NULL;
484
485                                                 while (a--) {
486                                                         sf_vert_last = sf_vert;
487
488                                                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, f1);
489                                                         totvert++;
490
491                                                         if (sf_vert_last == NULL)
492                                                                 sf_vert_new = sf_vert;
493                                                         else {
494                                                                 BLI_scanfill_edge_add(&sf_ctx, sf_vert_last, sf_vert);
495                                                         }
496                                                         f1 += 3;
497                                                 }
498
499                                                 if (sf_vert != NULL && sf_vert_new != NULL) {
500                                                         BLI_scanfill_edge_add(&sf_ctx, sf_vert, sf_vert_new);
501                                                 }
502                                         }
503                                         else if (colnr < dl->col) {
504                                                 /* got poly with next material at current char */
505                                                 cont = 1;
506                                                 nextcol = 1;
507                                         }
508                                 }
509                         }
510                         dl = dl->next;
511                 }
512
513                 /* XXX (obedit && obedit->actcol) ? (obedit->actcol-1) : 0)) { */
514                 if (totvert && (tot = BLI_scanfill_calc_ex(&sf_ctx,
515                                                            BLI_SCANFILL_CALC_REMOVE_DOUBLES | BLI_SCANFILL_CALC_HOLES,
516                                                            normal_proj)))
517                 {
518                         if (tot) {
519                                 dlnew = MEM_callocN(sizeof(DispList), "filldisplist");
520                                 dlnew->type = DL_INDEX3;
521                                 dlnew->col = colnr;
522                                 dlnew->nr = totvert;
523                                 dlnew->parts = tot;
524
525                                 dlnew->index = MEM_mallocN(tot * 3 * sizeof(int), "dlindex");
526                                 dlnew->verts = MEM_mallocN(totvert * 3 * sizeof(float), "dlverts");
527
528                                 /* vert data */
529                                 f1 = dlnew->verts;
530                                 totvert = 0;
531
532                                 for (sf_vert = sf_ctx.fillvertbase.first; sf_vert; sf_vert = sf_vert->next) {
533                                         copy_v3_v3(f1, sf_vert->co);
534                                         f1 += 3;
535
536                                         /* index number */
537                                         sf_vert->tmp.l = totvert;
538                                         totvert++;
539                                 }
540
541                                 /* index data */
542                                 sf_tri = sf_ctx.fillfacebase.first;
543                                 index = dlnew->index;
544                                 while (sf_tri) {
545                                         index[0] = (intptr_t)sf_tri->v1->tmp.l;
546                                         index[1] = (intptr_t)sf_tri->v2->tmp.l;
547                                         index[2] = (intptr_t)sf_tri->v3->tmp.l;
548
549                                         if (flipnormal)
550                                                 SWAP(int, index[0], index[2]);
551
552                                         index += 3;
553                                         sf_tri = sf_tri->next;
554                                 }
555                         }
556
557                         BLI_addhead(to, dlnew);
558                 }
559                 BLI_scanfill_end(&sf_ctx);
560
561                 if (nextcol) {
562                         /* stay at current char but fill polys with next material */
563                         colnr++;
564                 }
565                 else {
566                         /* switch to next char and start filling from first material */
567                         charidx++;
568                         colnr = 0;
569                 }
570         }
571
572         /* do not free polys, needed for wireframe display */
573 }
574
575 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
576 {
577         const float z_up[3] = {0.0f, 0.0f, 1.0f};
578         ListBase front, back;
579         DispList *dl, *dlnew;
580         float *fp, *fp1;
581         int a, dpoly;
582
583         front.first = front.last = back.first = back.last = NULL;
584
585         dl = dispbase->first;
586         while (dl) {
587                 if (dl->type == DL_SURF) {
588                         if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U) == 0) {
589                                 if ( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE)) {
590                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
591                                         BLI_addtail(&front, dlnew);
592                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
593                                         dlnew->nr = dl->parts;
594                                         dlnew->parts = 1;
595                                         dlnew->type = DL_POLY;
596                                         dlnew->col = dl->col;
597                                         dlnew->charidx = dl->charidx;
598
599                                         fp = dl->verts;
600                                         dpoly = 3 * dl->nr;
601
602                                         a = dl->parts;
603                                         while (a--) {
604                                                 copy_v3_v3(fp1, fp);
605                                                 fp1 += 3;
606                                                 fp += dpoly;
607                                         }
608                                 }
609                                 if ((cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE)) {
610                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
611                                         BLI_addtail(&back, dlnew);
612                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
613                                         dlnew->nr = dl->parts;
614                                         dlnew->parts = 1;
615                                         dlnew->type = DL_POLY;
616                                         dlnew->col = dl->col;
617                                         dlnew->charidx = dl->charidx;
618
619                                         fp = dl->verts + 3 * (dl->nr - 1);
620                                         dpoly = 3 * dl->nr;
621
622                                         a = dl->parts;
623                                         while (a--) {
624                                                 copy_v3_v3(fp1, fp);
625                                                 fp1 += 3;
626                                                 fp += dpoly;
627                                         }
628                                 }
629                         }
630                 }
631                 dl = dl->next;
632         }
633
634         BKE_displist_fill(&front, dispbase, z_up, true);
635         BKE_displist_fill(&back, dispbase, z_up, false);
636
637         BKE_displist_free(&front);
638         BKE_displist_free(&back);
639
640         BKE_displist_fill(dispbase, dispbase, z_up, false);
641 }
642
643 static void curve_to_filledpoly(Curve *cu, ListBase *UNUSED(nurb), ListBase *dispbase)
644 {
645         if (cu->flag & CU_3D)
646                 return;
647
648         if (dispbase->first && ((DispList *) dispbase->first)->type == DL_SURF) {
649                 bevels_to_filledpoly(cu, dispbase);
650         }
651         else {
652                 /* TODO, investigate passing zup instead of NULL */
653                 BKE_displist_fill(dispbase, dispbase, NULL, false);
654         }
655 }
656
657 /* taper rules:
658  * - only 1 curve
659  * - first point left, last point right
660  * - based on subdivided points in original curve, not on points in taper curve (still)
661  */
662 static float displist_calc_taper(Scene *scene, Object *taperobj, float fac)
663 {
664         DispList *dl;
665
666         if (taperobj == NULL || taperobj->type != OB_CURVE)
667                 return 1.0;
668
669         dl = taperobj->curve_cache ? taperobj->curve_cache->disp.first : NULL;
670         if (dl == NULL) {
671                 BKE_displist_make_curveTypes(scene, taperobj, 0);
672                 dl = taperobj->curve_cache->disp.first;
673         }
674         if (dl) {
675                 float minx, dx, *fp;
676                 int a;
677
678                 /* horizontal size */
679                 minx = dl->verts[0];
680                 dx = dl->verts[3 * (dl->nr - 1)] - minx;
681                 if (dx > 0.0f) {
682                         fp = dl->verts;
683                         for (a = 0; a < dl->nr; a++, fp += 3) {
684                                 if ((fp[0] - minx) / dx >= fac) {
685                                         /* interpolate with prev */
686                                         if (a > 0) {
687                                                 float fac1 = (fp[-3] - minx) / dx;
688                                                 float fac2 = (fp[0] - minx) / dx;
689                                                 if (fac1 != fac2)
690                                                         return fp[1] * (fac1 - fac) / (fac1 - fac2) + fp[-2] * (fac - fac2) / (fac1 - fac2);
691                                         }
692                                         return fp[1];
693                                 }
694                         }
695                         return fp[-2];  // last y coord
696                 }
697         }
698
699         return 1.0;
700 }
701
702 float BKE_displist_calc_taper(Scene *scene, Object *taperobj, int cur, int tot)
703 {
704         float fac = ((float)cur) / (float)(tot - 1);
705
706         return displist_calc_taper(scene, taperobj, fac);
707 }
708
709 void BKE_displist_make_mball(Scene *scene, Object *ob)
710 {
711         if (!ob || ob->type != OB_MBALL)
712                 return;
713
714         if (ob->curve_cache) {
715                 BKE_displist_free(&(ob->curve_cache->disp));
716         }
717         else {
718                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for MBall");
719         }
720
721         if (ob->type == OB_MBALL) {
722                 if (ob == BKE_mball_basis_find(scene, ob)) {
723                         BKE_mball_polygonize(scene, ob, &ob->curve_cache->disp, false);
724                         BKE_mball_texspace_calc(ob);
725
726                         object_deform_mball(ob, &ob->curve_cache->disp);
727                 }
728
729                 boundbox_displist_object(ob);
730         }
731 }
732
733 void BKE_displist_make_mball_forRender(Scene *scene, Object *ob, ListBase *dispbase)
734 {
735         BKE_mball_polygonize(scene, ob, dispbase, true);
736         BKE_mball_texspace_calc(ob);
737
738         object_deform_mball(ob, dispbase);
739 }
740
741 static ModifierData *curve_get_tessellate_point(Scene *scene, Object *ob, int renderResolution, int editmode)
742 {
743         VirtualModifierData virtualModifierData;
744         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
745         ModifierData *pretessellatePoint;
746         int required_mode;
747
748         if (renderResolution)
749                 required_mode = eModifierMode_Render;
750         else
751                 required_mode = eModifierMode_Realtime;
752
753         if (editmode)
754                 required_mode |= eModifierMode_Editmode;
755
756         pretessellatePoint = NULL;
757         for (; md; md = md->next) {
758                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
759
760                 if (!modifier_isEnabled(scene, md, required_mode))
761                         continue;
762                 if (mti->type == eModifierTypeType_Constructive)
763                         return pretessellatePoint;
764
765                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
766                         pretessellatePoint = md;
767
768                         /* this modifiers are moving point of tessellation automatically
769                          * (some of them even can't be applied on tessellated curve), set flag
770                          * for information button in modifier's header
771                          */
772                         md->mode |= eModifierMode_ApplyOnSpline;
773                 }
774                 else if (md->mode & eModifierMode_ApplyOnSpline) {
775                         pretessellatePoint = md;
776                 }
777         }
778
779         return pretessellatePoint;
780 }
781
782 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, ListBase *nurb,
783                                      int forRender, int renderResolution)
784 {
785         VirtualModifierData virtualModifierData;
786         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
787         ModifierData *pretessellatePoint;
788         Curve *cu = ob->data;
789         int numVerts = 0;
790         const int editmode = (!forRender && (cu->editnurb || cu->editfont));
791         ModifierApplyFlag app_flag = 0;
792         float (*deformedVerts)[3] = NULL;
793         float *keyVerts = NULL;
794         int required_mode;
795
796         if (editmode)
797                 app_flag |= MOD_APPLY_USECACHE;
798         if (renderResolution) {
799                 app_flag |= MOD_APPLY_RENDER;
800                 required_mode = eModifierMode_Render;
801         }
802         else
803                 required_mode = eModifierMode_Realtime;
804
805         pretessellatePoint = curve_get_tessellate_point(scene, ob, renderResolution, editmode);
806
807         if (editmode)
808                 required_mode |= eModifierMode_Editmode;
809
810         if (cu->editnurb == NULL) {
811                 keyVerts = BKE_key_evaluate_object(scene, ob, &numVerts);
812
813                 if (keyVerts) {
814                         /* split coords from key data, the latter also includes
815                          * tilts, which is passed through in the modifier stack.
816                          * this is also the reason curves do not use a virtual
817                          * shape key modifier yet. */
818                         deformedVerts = BKE_curve_nurbs_keyVertexCos_get(nurb, keyVerts);
819                         BLI_assert(BKE_nurbList_verts_count(nurb) == numVerts);
820                 }
821         }
822
823         if (pretessellatePoint) {
824                 for (; md; md = md->next) {
825                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
826
827                         md->scene = scene;
828
829                         if (!modifier_isEnabled(scene, md, required_mode))
830                                 continue;
831                         if (mti->type != eModifierTypeType_OnlyDeform)
832                                 continue;
833
834                         if (!deformedVerts) {
835                                 deformedVerts = BKE_curve_nurbs_vertexCos_get(nurb, &numVerts);
836                         }
837
838                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, app_flag);
839
840                         if (md == pretessellatePoint)
841                                 break;
842                 }
843         }
844
845         if (deformedVerts) {
846                 BK_curve_nurbs_vertexCos_apply(nurb, deformedVerts);
847                 MEM_freeN(deformedVerts);
848         }
849         if (keyVerts) /* these are not passed through modifier stack */
850                 BKE_curve_nurbs_keyVertexTilts_apply(nurb, keyVerts);
851
852         if (keyVerts)
853                 MEM_freeN(keyVerts);
854 }
855
856 static float (*displist_get_allverts(ListBase *dispbase, int *totvert))[3]
857 {
858         DispList *dl;
859         float (*allverts)[3], *fp;
860
861         *totvert = 0;
862
863         for (dl = dispbase->first; dl; dl = dl->next)
864                 *totvert += (dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr;
865
866         allverts = MEM_mallocN((*totvert) * sizeof(float) * 3, "displist_get_allverts allverts");
867         fp = (float *)allverts;
868         for (dl = dispbase->first; dl; dl = dl->next) {
869                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
870                 memcpy(fp, dl->verts, sizeof(float) * offs);
871                 fp += offs;
872         }
873
874         return allverts;
875 }
876
877 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
878 {
879         DispList *dl;
880         float *fp;
881
882         fp = (float *)allverts;
883         for (dl = dispbase->first; dl; dl = dl->next) {
884                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
885                 memcpy(dl->verts, fp, sizeof(float) * offs);
886                 fp += offs;
887         }
888 }
889
890 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *nurb,
891                                       ListBase *dispbase, DerivedMesh **derivedFinal,
892                                       int forRender, int renderResolution)
893 {
894         VirtualModifierData virtualModifierData;
895         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
896         ModifierData *pretessellatePoint;
897         Curve *cu = ob->data;
898         int required_mode = 0, totvert = 0;
899         int editmode = (!forRender && (cu->editnurb || cu->editfont));
900         DerivedMesh *dm = NULL, *ndm;
901         float (*vertCos)[3] = NULL;
902         int useCache = !forRender;
903         ModifierApplyFlag app_flag = 0;
904
905         if (renderResolution) {
906                 app_flag |= MOD_APPLY_RENDER;
907                 required_mode = eModifierMode_Render;
908         }
909         else
910                 required_mode = eModifierMode_Realtime;
911
912         pretessellatePoint = curve_get_tessellate_point(scene, ob, renderResolution, editmode);
913
914         if (editmode)
915                 required_mode |= eModifierMode_Editmode;
916
917         if (pretessellatePoint) {
918                 md = pretessellatePoint->next;
919         }
920
921         if (derivedFinal && *derivedFinal) {
922                 (*derivedFinal)->release(*derivedFinal);
923         }
924
925         for (; md; md = md->next) {
926                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
927                 ModifierApplyFlag appf = app_flag;
928
929                 md->scene = scene;
930
931                 if (!modifier_isEnabled(scene, md, required_mode))
932                         continue;
933
934                 if (mti->type == eModifierTypeType_OnlyDeform ||
935                     (mti->type == eModifierTypeType_DeformOrConstruct && !dm))
936                 {
937                         if (editmode)
938                                 appf |= MOD_APPLY_USECACHE;
939                         if (dm) {
940                                 if (!vertCos) {
941                                         totvert = dm->getNumVerts(dm);
942                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
943                                         dm->getVertCos(dm, vertCos);
944                                 }
945
946                                 mti->deformVerts(md, ob, dm, vertCos, totvert, appf);
947                         }
948                         else {
949                                 if (!vertCos) {
950                                         vertCos = displist_get_allverts(dispbase, &totvert);
951                                 }
952
953                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, appf);
954                         }
955                 }
956                 else {
957                         if (!derivedFinal) {
958                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh
959                                  * is totally unnecessary, so we could stop modifiers applying
960                                  * when we found constructive modifier but derived mesh is unwanted result
961                                  */
962                                 break;
963                         }
964
965                         if (dm) {
966                                 if (vertCos) {
967                                         DerivedMesh *tdm = CDDM_copy(dm);
968                                         dm->release(dm);
969                                         dm = tdm;
970
971                                         CDDM_apply_vert_coords(dm, vertCos);
972                                 }
973                         }
974                         else {
975                                 if (vertCos) {
976                                         displist_apply_allverts(dispbase, vertCos);
977                                 }
978
979                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
980                                         curve_to_filledpoly(cu, nurb, dispbase);
981                                 }
982
983                                 dm = CDDM_from_curve_displist(ob, dispbase);
984                         }
985
986                         if (vertCos) {
987                                 /* Vertex coordinates were applied to necessary data, could free it */
988                                 MEM_freeN(vertCos);
989                                 vertCos = NULL;
990                         }
991
992                         if (useCache)
993                                 appf |= MOD_APPLY_USECACHE;
994
995                         ndm = modwrap_applyModifier(md, ob, dm, appf);
996
997                         if (ndm) {
998                                 /* Modifier returned a new derived mesh */
999
1000                                 if (dm && dm != ndm) /* Modifier  */
1001                                         dm->release(dm);
1002                                 dm = ndm;
1003                         }
1004                 }
1005         }
1006
1007         if (vertCos) {
1008                 if (dm) {
1009                         DerivedMesh *tdm = CDDM_copy(dm);
1010                         dm->release(dm);
1011                         dm = tdm;
1012
1013                         CDDM_apply_vert_coords(dm, vertCos);
1014                         CDDM_calc_normals_mapping(dm);
1015                         MEM_freeN(vertCos);
1016                 }
1017                 else {
1018                         displist_apply_allverts(dispbase, vertCos);
1019                         MEM_freeN(vertCos);
1020                         vertCos = NULL;
1021                 }
1022         }
1023
1024         if (derivedFinal) {
1025                 if (dm) {
1026                         /* see: mesh_calc_modifiers */
1027                         if (dm->getNumTessFaces(dm) == 0) {
1028                                 dm->recalcTessellation(dm);
1029                         }
1030                         /* Even if tessellation is not needed, some modifiers might have modified CD layers
1031                          * (like mloopcol or mloopuv), hence we have to update those. */
1032                         else if (dm->dirty & DM_DIRTY_TESS_CDLAYERS) {
1033                                 DM_update_tessface_data(dm);
1034                         }
1035
1036                         if (dm->type == DM_TYPE_CDDM) {
1037                                 CDDM_calc_normals_mapping_ex(dm, (dm->dirty & DM_DIRTY_NORMALS) ? false : true);
1038                         }
1039                 }
1040                 (*derivedFinal) = dm;
1041         }
1042 }
1043
1044 static void displist_surf_indices(DispList *dl)
1045 {
1046         int a, b, p1, p2, p3, p4;
1047         int *index;
1048
1049         dl->totindex = 0;
1050
1051         index = dl->index = MEM_mallocN(4 * sizeof(int) * (dl->parts + 1) * (dl->nr + 1), "index array nurbs");
1052
1053         for (a = 0; a < dl->parts; a++) {
1054
1055                 if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
1056                         break;
1057
1058                 for (; b < dl->nr; b++, index += 4) {
1059                         index[0] = p1;
1060                         index[1] = p2;
1061                         index[2] = p4;
1062                         index[3] = p3;
1063
1064                         dl->totindex++;
1065
1066                         p2 = p1; p1++;
1067                         p4 = p3; p3++;
1068                 }
1069         }
1070 }
1071
1072 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
1073 {
1074         DerivedMesh *dm;
1075         ListBase disp = {NULL, NULL};
1076
1077         /* OrcoDM should be created from underformed disp lists */
1078         BKE_displist_make_curveTypes_forOrco(scene, ob, &disp);
1079         dm = CDDM_from_curve_displist(ob, &disp);
1080
1081         BKE_displist_free(&disp);
1082
1083         return dm;
1084 }
1085
1086 static void add_orco_dm(Scene *scene, Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1087 {
1088         float (*orco)[3], (*layerorco)[3];
1089         int totvert, a;
1090         Curve *cu = ob->data;
1091
1092         totvert = dm->getNumVerts(dm);
1093
1094         if (orcodm) {
1095                 orco = MEM_callocN(sizeof(float) * 3 * totvert, "dm orco");
1096
1097                 if (orcodm->getNumVerts(orcodm) == totvert)
1098                         orcodm->getVertCos(orcodm, orco);
1099                 else
1100                         dm->getVertCos(dm, orco);
1101         }
1102         else {
1103                 int totvert_curve;
1104                 orco = (float(*)[3])BKE_curve_make_orco(scene, ob, &totvert_curve);
1105                 if (totvert != totvert_curve) {
1106                         MEM_freeN(orco);
1107                         orco = MEM_callocN(sizeof(float) * 3 * totvert, "dm orco");
1108                         dm->getVertCos(dm, orco);
1109                 }
1110         }
1111
1112         for (a = 0; a < totvert; a++) {
1113                 float *co = orco[a];
1114                 co[0] = (co[0] - cu->loc[0]) / cu->size[0];
1115                 co[1] = (co[1] - cu->loc[1]) / cu->size[1];
1116                 co[2] = (co[2] - cu->loc[2]) / cu->size[2];
1117         }
1118
1119         if ((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1120                 memcpy(layerorco, orco, sizeof(float) * totvert);
1121                 MEM_freeN(orco);
1122         }
1123         else
1124                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1125 }
1126
1127 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender, int renderResolution)
1128 {
1129         /* this function represents logic of mesh's orcodm calculation
1130          * for displist-based objects
1131          */
1132         VirtualModifierData virtualModifierData;
1133         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
1134         ModifierData *pretessellatePoint;
1135         Curve *cu = ob->data;
1136         int required_mode;
1137         int editmode = (!forRender && (cu->editnurb || cu->editfont));
1138         DerivedMesh *ndm, *orcodm = NULL;
1139         ModifierApplyFlag app_flag = MOD_APPLY_ORCO;
1140
1141         if (renderResolution) {
1142                 app_flag |= MOD_APPLY_RENDER;
1143                 required_mode = eModifierMode_Render;
1144         }
1145         else
1146                 required_mode = eModifierMode_Realtime;
1147
1148         pretessellatePoint = curve_get_tessellate_point(scene, ob, renderResolution, editmode);
1149
1150         if (editmode)
1151                 required_mode |= eModifierMode_Editmode;
1152
1153         if (pretessellatePoint) {
1154                 md = pretessellatePoint->next;
1155         }
1156
1157         for (; md; md = md->next) {
1158                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1159
1160                 md->scene = scene;
1161
1162                 if (!modifier_isEnabled(scene, md, required_mode))
1163                         continue;
1164                 if (mti->type != eModifierTypeType_Constructive)
1165                         continue;
1166
1167                 if (!orcodm)
1168                         orcodm = create_orco_dm(scene, ob);
1169
1170                 ndm = modwrap_applyModifier(md, ob, orcodm, app_flag);
1171
1172                 if (ndm) {
1173                         /* if the modifier returned a new dm, release the old one */
1174                         if (orcodm && orcodm != ndm) {
1175                                 orcodm->release(orcodm);
1176                         }
1177                         orcodm = ndm;
1178                 }
1179         }
1180
1181         /* add an orco layer if needed */
1182         add_orco_dm(scene, ob, derivedFinal, orcodm);
1183
1184         if (orcodm)
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                 BKE_nurbList_duplicate(&nubase, BKE_curve_nurbs_get(cu));
1373
1374                 BLI_freelistN(&(ob->curve_cache->bev));
1375
1376                 if (ob->curve_cache->path) free_path(ob->curve_cache->path);
1377                 ob->curve_cache->path = NULL;
1378
1379                 if (ob->type == OB_FONT)
1380                         BKE_vfont_to_curve(G.main, scene, ob, 0);
1381
1382                 if (!forOrco)
1383                         curve_calc_modifiers_pre(scene, ob, &nubase, forRender, renderResolution);
1384
1385                 BKE_curve_bevelList_make(ob, &nubase, forRender != FALSE);
1386
1387                 /* If curve has no bevel will return nothing */
1388                 BKE_curve_bevel_make(scene, ob, &dlbev, forRender, renderResolution);
1389
1390                 /* no bevel or extrude, and no width correction? */
1391                 if (!dlbev.first && cu->width == 1.0f) {
1392                         curve_to_displist(cu, &nubase, dispbase, forRender, renderResolution);
1393                 }
1394                 else {
1395                         float widfac = cu->width - 1.0f;
1396                         BevList *bl = ob->curve_cache->bev.first;
1397                         Nurb *nu = nubase.first;
1398
1399                         for (; bl && nu; bl = bl->next, nu = nu->next) {
1400                                 DispList *dl;
1401                                 float *data;
1402                                 BevPoint *bevp;
1403                                 int a;
1404
1405                                 if (bl->nr) { /* blank bevel lists can happen */
1406
1407                                         /* exception handling; curve without bevel or extrude, with width correction */
1408                                         if (dlbev.first == NULL) {
1409                                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbev");
1410                                                 dl->verts = MEM_callocN(3 * sizeof(float) * bl->nr, "dlverts");
1411                                                 BLI_addtail(dispbase, dl);
1412
1413                                                 if (bl->poly != -1) dl->type = DL_POLY;
1414                                                 else dl->type = DL_SEGM;
1415
1416                                                 if (dl->type == DL_SEGM) dl->flag = (DL_FRONT_CURVE | DL_BACK_CURVE);
1417
1418                                                 dl->parts = 1;
1419                                                 dl->nr = bl->nr;
1420                                                 dl->col = nu->mat_nr;
1421                                                 dl->charidx = nu->charidx;
1422
1423                                                 /* dl->rt will be used as flag for render face and */
1424                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1425                                                 dl->rt = nu->flag & ~CU_2D;
1426
1427                                                 a = dl->nr;
1428                                                 bevp = (BevPoint *)(bl + 1);
1429                                                 data = dl->verts;
1430                                                 while (a--) {
1431                                                         data[0] = bevp->vec[0] + widfac * bevp->sina;
1432                                                         data[1] = bevp->vec[1] + widfac * bevp->cosa;
1433                                                         data[2] = bevp->vec[2];
1434                                                         bevp++;
1435                                                         data += 3;
1436                                                 }
1437                                         }
1438                                         else {
1439                                                 DispList *dlb;
1440                                                 ListBase bottom_capbase = {NULL, NULL};
1441                                                 ListBase top_capbase = {NULL, NULL};
1442                                                 float bottom_no[3] = {0.0f};
1443                                                 float top_no[3] = {0.0f};
1444
1445                                                 for (dlb = dlbev.first; dlb; dlb = dlb->next) {
1446                                                         const float bevfac1 = min_ff(cu->bevfac1, cu->bevfac2);
1447                                                         const float bevfac2 = max_ff(cu->bevfac1, cu->bevfac2);
1448                                                         float firstblend = 0.0f, lastblend = 0.0f;
1449                                                         int i, start, steps;
1450
1451                                                         if (bevfac2 - bevfac1 == 0.0f)
1452                                                                 continue;
1453
1454                                                         start = (int)(bevfac1 * (bl->nr - 1));
1455                                                         steps = 2 + (int)((bevfac2) * (bl->nr - 1)) - start;
1456                                                         firstblend = 1.0f - (bevfac1 * (bl->nr - 1) - (int)(bevfac1 * (bl->nr - 1)));
1457                                                         lastblend  =         bevfac2 * (bl->nr - 1) - (int)(bevfac2 * (bl->nr - 1));
1458
1459                                                         if (start + steps > bl->nr) {
1460                                                                 steps = bl->nr - start;
1461                                                                 lastblend = 1.0f;
1462                                                         }
1463
1464                                                         /* for each part of the bevel use a separate displblock */
1465                                                         dl = MEM_callocN(sizeof(DispList), "makeDispListbev1");
1466                                                         dl->verts = data = MEM_callocN(3 * sizeof(float) * dlb->nr * steps, "dlverts");
1467                                                         BLI_addtail(dispbase, dl);
1468
1469                                                         dl->type = DL_SURF;
1470
1471                                                         dl->flag = dlb->flag & (DL_FRONT_CURVE | DL_BACK_CURVE);
1472                                                         if (dlb->type == DL_POLY) dl->flag |= DL_CYCL_U;
1473                                                         if (bl->poly >= 0) dl->flag |= DL_CYCL_V;
1474
1475                                                         dl->parts = steps;
1476                                                         dl->nr = dlb->nr;
1477                                                         dl->col = nu->mat_nr;
1478                                                         dl->charidx = nu->charidx;
1479
1480                                                         /* dl->rt will be used as flag for render face and */
1481                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1482                                                         dl->rt = nu->flag & ~CU_2D;
1483
1484                                                         dl->bevelSplitFlag = MEM_callocN(sizeof(*dl->col2) * ((steps + 0x1F) >> 5),
1485                                                                                          "bevelSplitFlag");
1486
1487                                                         /* for each point of poly make a bevel piece */
1488                                                         bevp = (BevPoint *)(bl + 1) + start;
1489                                                         for (i = start, a = 0; a < steps; i++, bevp++, a++) {
1490                                                                 float fac = 1.0;
1491                                                                 float *cur_data = data;
1492
1493                                                                 if (cu->taperobj == NULL) {
1494                                                                         fac = bevp->radius;
1495                                                                 }
1496                                                                 else {
1497                                                                         float len, taper_fac;
1498
1499                                                                         if (cu->flag & CU_MAP_TAPER) {
1500                                                                                 len = (steps - 3) + firstblend + lastblend;
1501
1502                                                                                 if (a == 0)
1503                                                                                         taper_fac = 0.0f;
1504                                                                                 else if (a == steps - 1)
1505                                                                                         taper_fac = 1.0f;
1506                                                                                 else
1507                                                                                         taper_fac = ((float) a - (1.0f - firstblend)) / len;
1508                                                                         }
1509                                                                         else {
1510                                                                                 len = bl->nr - 1;
1511                                                                                 taper_fac = (float) i / len;
1512
1513                                                                                 if (a == 0)
1514                                                                                         taper_fac += (1.0f - firstblend) / len;
1515                                                                                 else if (a == steps - 1)
1516                                                                                         taper_fac -= (1.0f - lastblend) / len;
1517                                                                         }
1518
1519                                                                         fac = displist_calc_taper(scene, cu->taperobj, taper_fac);
1520                                                                 }
1521
1522                                                                 if (bevp->split_tag) {
1523                                                                         dl->bevelSplitFlag[a >> 5] |= 1 << (a & 0x1F);
1524                                                                 }
1525
1526                                                                 /* rotate bevel piece and write in data */
1527                                                                 if (a == 0)
1528                                                                         rotateBevelPiece(cu, bevp, bevp + 1, dlb, 1.0f - firstblend, widfac, fac, &data);
1529                                                                 else if (a == steps - 1)
1530                                                                         rotateBevelPiece(cu, bevp, bevp - 1, dlb, 1.0f - lastblend, widfac, fac, &data);
1531                                                                 else
1532                                                                         rotateBevelPiece(cu, bevp, NULL, dlb, 0.0f, widfac, fac, &data);
1533
1534                                                                 if (cu->bevobj && (cu->flag & CU_FILL_CAPS) && !(nu->flagu & CU_NURB_CYCLIC)) {
1535                                                                         if (a == 1) {
1536                                                                                 fillBevelCap(nu, dlb, cur_data - 3 * dlb->nr, &bottom_capbase);
1537                                                                                 negate_v3_v3(bottom_no, bevp->dir);
1538                                                                         }
1539                                                                         if (a == steps - 1) {
1540                                                                                 fillBevelCap(nu, dlb, cur_data, &top_capbase);
1541                                                                                 copy_v3_v3(top_no, bevp->dir);
1542                                                                         }
1543                                                                 }
1544                                                         }
1545
1546                                                         /* gl array drawing: using indices */
1547                                                         displist_surf_indices(dl);
1548                                                 }
1549
1550                                                 if (bottom_capbase.first) {
1551                                                         BKE_displist_fill(&bottom_capbase, dispbase, bottom_no, false);
1552                                                         BKE_displist_fill(&top_capbase, dispbase, top_no, false);
1553                                                         BKE_displist_free(&bottom_capbase);
1554                                                         BKE_displist_free(&top_capbase);
1555                                                 }
1556                                         }
1557                                 }
1558
1559                         }
1560                         BKE_displist_free(&dlbev);
1561                 }
1562
1563                 if (!(cu->flag & CU_DEFORM_FILL)) {
1564                         curve_to_filledpoly(cu, &nubase, dispbase);
1565                 }
1566
1567                 if ((cu->flag & CU_PATH) && !forOrco)
1568                         calc_curvepath(ob, &nubase);
1569
1570                 if (!forOrco)
1571                         curve_calc_modifiers_post(scene, ob, &nubase, dispbase, derivedFinal, forRender, renderResolution);
1572
1573                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1574                         curve_to_filledpoly(cu, &nubase, dispbase);
1575                 }
1576
1577                 BKE_nurbList_free(&nubase);
1578         }
1579 }
1580
1581 void BKE_displist_make_curveTypes(Scene *scene, Object *ob, int forOrco)
1582 {
1583         ListBase *dispbase;
1584
1585         /* The same check for duplis as in do_makeDispListCurveTypes.
1586          * Happens when curve used for constraint/bevel was converted to mesh.
1587          * check there is still needed for render displist and orco displists. */
1588         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT))
1589                 return;
1590
1591         BKE_object_free_derived_caches(ob);
1592
1593         if (!ob->curve_cache) {
1594                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for curve types");
1595         }
1596
1597         dispbase = &(ob->curve_cache->disp);
1598
1599         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco, 0);
1600
1601         boundbox_displist_object(ob);
1602 }
1603
1604 void BKE_displist_make_curveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1605                                       DerivedMesh **derivedFinal, int forOrco, int renderResolution)
1606 {
1607         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco, renderResolution);
1608 }
1609
1610 void BKE_displist_make_curveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1611 {
1612         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1, 1);
1613 }
1614
1615 /* add Orco layer to the displist object which has got derived mesh and return orco */
1616 float *BKE_displist_make_orco(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender, int renderResolution)
1617 {
1618         float *orco;
1619
1620         if (derivedFinal == NULL)
1621                 derivedFinal = ob->derivedFinal;
1622
1623         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1624                 curve_calc_orcodm(scene, ob, derivedFinal, forRender, renderResolution);
1625         }
1626
1627         orco = derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1628
1629         if (orco) {
1630                 orco = MEM_dupallocN(orco);
1631         }
1632
1633         return orco;
1634 }
1635
1636 void BKE_displist_minmax(ListBase *dispbase, float min[3], float max[3])
1637 {
1638         DispList *dl;
1639         float *vert;
1640         int a, tot = 0;
1641         int doit = 0;
1642
1643         for (dl = dispbase->first; dl; dl = dl->next) {
1644                 tot = (dl->type == DL_INDEX3) ? dl->nr : dl->nr * dl->parts;
1645                 vert = dl->verts;
1646                 for (a = 0; a < tot; a++, vert += 3) {
1647                         minmax_v3v3_v3(min, max, vert);
1648                 }
1649                 doit |= (tot != 0);
1650         }
1651
1652         if (!doit) {
1653                 /* there's no geometry in displist, use zero-sized boundbox */
1654                 zero_v3(min);
1655                 zero_v3(max);
1656         }
1657 }
1658
1659 /* this is confusing, there's also min_max_object, appplying the obmat... */
1660 static void boundbox_displist_object(Object *ob)
1661 {
1662         if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1663                 /* Curver's BB is already calculated as a part of modifier stack,
1664                  * here we only calculate object BB based on final display list.
1665                  */
1666
1667                 /* object's BB is calculated from final displist */
1668                 if (ob->bb == NULL)
1669                         ob->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1670
1671                 if (ob->derivedFinal) {
1672                         DM_set_object_boundbox(ob, ob->derivedFinal);
1673                 }
1674                 else {
1675                         float min[3], max[3];
1676
1677                         INIT_MINMAX(min, max);
1678                         BKE_displist_minmax(&ob->curve_cache->disp, min, max);
1679                         BKE_boundbox_init_from_minmax(ob->bb, min, max);
1680                 }
1681         }
1682 }