1abb738608e2275fe7009fdb9a73e7bd79b250c2
[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_memarena.h"
47 #include "BLI_math.h"
48 #include "BLI_scanfill.h"
49 #include "BLI_utildefines.h"
50
51 #include "BKE_global.h"
52 #include "BKE_displist.h"
53 #include "BKE_cdderivedmesh.h"
54 #include "BKE_object.h"
55 #include "BKE_mball.h"
56 #include "BKE_material.h"
57 #include "BKE_curve.h"
58 #include "BKE_key.h"
59 #include "BKE_anim.h"
60 #include "BKE_font.h"
61 #include "BKE_lattice.h"
62 #include "BKE_modifier.h"
63
64 #include "BLI_sys_types.h" // for intptr_t support
65
66 static void boundbox_displist_object(Object *ob);
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         while ((dl = BLI_pophead(lb))) {
86                 BKE_displist_elem_free(dl);
87         }
88 }
89
90 DispList *BKE_displist_find_or_create(ListBase *lb, int type)
91 {
92         DispList *dl;
93
94         dl = lb->first;
95         while (dl) {
96                 if (dl->type == type)
97                         return dl;
98                 dl = dl->next;
99         }
100
101         dl = MEM_callocN(sizeof(DispList), "find_disp");
102         dl->type = type;
103         BLI_addtail(lb, dl);
104
105         return dl;
106 }
107
108 DispList *BKE_displist_find(ListBase *lb, int type)
109 {
110         DispList *dl;
111
112         dl = lb->first;
113         while (dl) {
114                 if (dl->type == type)
115                         return dl;
116                 dl = dl->next;
117         }
118
119         return NULL;
120 }
121
122 bool BKE_displist_has_faces(ListBase *lb)
123 {
124         DispList *dl;
125
126         for (dl = lb->first; dl; dl = dl->next) {
127                 if (ELEM3(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)) {
128                         return true;
129                 }
130         }
131
132         return false;
133 }
134
135 void BKE_displist_copy(ListBase *lbn, ListBase *lb)
136 {
137         DispList *dln, *dl;
138
139         BKE_displist_free(lbn);
140
141         dl = lb->first;
142         while (dl) {
143                 dln = MEM_dupallocN(dl);
144                 BLI_addtail(lbn, dln);
145                 dln->verts = MEM_dupallocN(dl->verts);
146                 dln->nors = MEM_dupallocN(dl->nors);
147                 dln->index = MEM_dupallocN(dl->index);
148                 dln->col1 = MEM_dupallocN(dl->col1);
149                 dln->col2 = MEM_dupallocN(dl->col2);
150
151                 if (dl->bevelSplitFlag)
152                         dln->bevelSplitFlag = MEM_dupallocN(dl->bevelSplitFlag);
153
154                 dl = dl->next;
155         }
156 }
157
158 void BKE_displist_normals_add(ListBase *lb)
159 {
160         DispList *dl = NULL;
161         float *vdata, *ndata, nor[3];
162         float *v1, *v2, *v3, *v4;
163         float *n1, *n2, *n3, *n4;
164         int a, b, p1, p2, p3, p4;
165
166         dl = lb->first;
167
168         while (dl) {
169                 if (dl->type == DL_INDEX3) {
170                         if (dl->nors == NULL) {
171                                 dl->nors = MEM_callocN(sizeof(float) * 3, "dlnors");
172
173                                 if (dl->verts[2] < 0.0f)
174                                         dl->nors[2] = -1.0f;
175                                 else
176                                         dl->nors[2] = 1.0f;
177                         }
178                 }
179                 else if (dl->type == DL_SURF) {
180                         if (dl->nors == NULL) {
181                                 dl->nors = MEM_callocN(sizeof(float) * 3 * dl->nr * dl->parts, "dlnors");
182
183                                 vdata = dl->verts;
184                                 ndata = dl->nors;
185
186                                 for (a = 0; a < dl->parts; a++) {
187
188                                         if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
189                                                 break;
190
191                                         v1 = vdata + 3 * p1;
192                                         n1 = ndata + 3 * p1;
193                                         v2 = vdata + 3 * p2;
194                                         n2 = ndata + 3 * p2;
195                                         v3 = vdata + 3 * p3;
196                                         n3 = ndata + 3 * p3;
197                                         v4 = vdata + 3 * p4;
198                                         n4 = ndata + 3 * p4;
199
200                                         for (; b < dl->nr; b++) {
201                                                 normal_quad_v3(nor, v1, v3, v4, v2);
202
203                                                 add_v3_v3(n1, nor);
204                                                 add_v3_v3(n2, nor);
205                                                 add_v3_v3(n3, nor);
206                                                 add_v3_v3(n4, nor);
207
208                                                 v2 = v1; v1 += 3;
209                                                 v4 = v3; v3 += 3;
210                                                 n2 = n1; n1 += 3;
211                                                 n4 = n3; n3 += 3;
212                                         }
213                                 }
214                                 a = dl->parts * dl->nr;
215                                 v1 = ndata;
216                                 while (a--) {
217                                         normalize_v3(v1);
218                                         v1 += 3;
219                                 }
220                         }
221                 }
222                 dl = dl->next;
223         }
224 }
225
226 void BKE_displist_count(ListBase *lb, int *totvert, int *totface, int *tottri)
227 {
228         DispList *dl;
229
230         for (dl = lb->first; dl; dl = dl->next) {
231                 int vert_tot = 0;
232                 int face_tot = 0;
233                 int tri_tot = 0;
234
235                 switch (dl->type) {
236                         case DL_SURF:
237                         {
238                                 vert_tot = dl->nr * dl->parts;
239                                 face_tot = (dl->nr - 1) * (dl->parts - 1);
240                                 tri_tot  = face_tot * 2;
241                                 break;
242                         }
243                         case DL_INDEX3:
244                         {
245                                 vert_tot = dl->nr;
246                                 face_tot = dl->parts;
247                                 tri_tot  = face_tot;
248                                 break;
249                         }
250                         case DL_INDEX4:
251                         {
252                                 vert_tot = dl->nr;
253                                 face_tot = dl->parts;
254                                 tri_tot  = face_tot * 2;
255                                 break;
256                         }
257                         case DL_POLY:
258                         case DL_SEGM:
259                         {
260                                 vert_tot = dl->nr * dl->parts;
261                                 break;
262                         }
263                 }
264
265                 *totvert += vert_tot;
266                 *totface += face_tot;
267                 *tottri  += tri_tot;
268         }
269 }
270
271 bool BKE_displist_surfindex_get(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4)
272 {
273         if ((dl->flag & DL_CYCL_V) == 0 && a == (dl->parts) - 1) {
274                 return false;
275         }
276
277         if (dl->flag & DL_CYCL_U) {
278                 (*p1) = dl->nr * a;
279                 (*p2) = (*p1) + dl->nr - 1;
280                 (*p3) = (*p1) + dl->nr;
281                 (*p4) = (*p2) + dl->nr;
282                 (*b) = 0;
283         }
284         else {
285                 (*p2) = dl->nr * a;
286                 (*p1) = (*p2) + 1;
287                 (*p4) = (*p2) + dl->nr;
288                 (*p3) = (*p1) + dl->nr;
289                 (*b) = 1;
290         }
291
292         if ((dl->flag & DL_CYCL_V) && a == dl->parts - 1) {
293                 (*p3) -= dl->nr * dl->parts;
294                 (*p4) -= dl->nr * dl->parts;
295         }
296
297         return true;
298 }
299
300 /* ****************** make displists ********************* */
301
302 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase, int forRender, int renderResolution)
303 {
304         Nurb *nu;
305         DispList *dl;
306         BezTriple *bezt, *prevbezt;
307         BPoint *bp;
308         float *data;
309         int a, len, resolu;
310         const int editmode = (!forRender && (cu->editnurb || cu->editfont));
311
312         nu = nubase->first;
313         while (nu) {
314                 if (nu->hide == 0 || editmode == 0) {
315                         if (renderResolution && cu->resolu_ren != 0)
316                                 resolu = cu->resolu_ren;
317                         else
318                                 resolu = nu->resolu;
319
320                         if (!BKE_nurb_check_valid_u(nu)) {
321                                 /* pass */
322                         }
323                         else if (nu->type == CU_BEZIER) {
324                                 /* count */
325                                 len = 0;
326                                 a = nu->pntsu - 1;
327                                 if (nu->flagu & CU_NURB_CYCLIC) a++;
328
329                                 prevbezt = nu->bezt;
330                                 bezt = prevbezt + 1;
331                                 while (a--) {
332                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC))
333                                                 bezt = nu->bezt;
334
335                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT)
336                                                 len++;
337                                         else
338                                                 len += resolu;
339
340                                         if (a == 0 && (nu->flagu & CU_NURB_CYCLIC) == 0)
341                                                 len++;
342
343                                         prevbezt = bezt;
344                                         bezt++;
345                                 }
346
347                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbez");
348                                 /* len+1 because of 'forward_diff_bezier' function */
349                                 dl->verts = MEM_callocN((len + 1) * 3 * sizeof(float), "dlverts");
350                                 BLI_addtail(dispbase, dl);
351                                 dl->parts = 1;
352                                 dl->nr = len;
353                                 dl->col = nu->mat_nr;
354                                 dl->charidx = nu->charidx;
355
356                                 data = dl->verts;
357
358                                 if (nu->flagu & CU_NURB_CYCLIC) {
359                                         dl->type = DL_POLY;
360                                         a = nu->pntsu;
361                                 }
362                                 else {
363                                         dl->type = DL_SEGM;
364                                         a = nu->pntsu - 1;
365                                 }
366
367                                 prevbezt = nu->bezt;
368                                 bezt = prevbezt + 1;
369
370                                 while (a--) {
371                                         if (a == 0 && dl->type == DL_POLY)
372                                                 bezt = nu->bezt;
373
374                                         if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
375                                                 copy_v3_v3(data, prevbezt->vec[1]);
376                                                 data += 3;
377                                         }
378                                         else {
379                                                 int j;
380                                                 for (j = 0; j < 3; j++) {
381                                                         BKE_curve_forward_diff_bezier(prevbezt->vec[1][j],
382                                                                                       prevbezt->vec[2][j],
383                                                                                       bezt->vec[0][j],
384                                                                                       bezt->vec[1][j],
385                                                                                       data + j, resolu, 3 * sizeof(float));
386                                                 }
387
388                                                 data += 3 * resolu;
389                                         }
390
391                                         if (a == 0 && dl->type == DL_SEGM) {
392                                                 copy_v3_v3(data, bezt->vec[1]);
393                                         }
394
395                                         prevbezt = bezt;
396                                         bezt++;
397                                 }
398                         }
399                         else if (nu->type == CU_NURBS) {
400                                 len = (resolu * SEGMENTSU(nu));
401
402                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
403                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
404                                 BLI_addtail(dispbase, dl);
405                                 dl->parts = 1;
406
407                                 dl->nr = len;
408                                 dl->col = nu->mat_nr;
409                                 dl->charidx = nu->charidx;
410
411                                 data = dl->verts;
412                                 if (nu->flagu & CU_NURB_CYCLIC)
413                                         dl->type = DL_POLY;
414                                 else dl->type = DL_SEGM;
415                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
416                         }
417                         else if (nu->type == CU_POLY) {
418                                 len = nu->pntsu;
419                                 dl = MEM_callocN(sizeof(DispList), "makeDispListpoly");
420                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
421                                 BLI_addtail(dispbase, dl);
422                                 dl->parts = 1;
423                                 dl->nr = len;
424                                 dl->col = nu->mat_nr;
425                                 dl->charidx = nu->charidx;
426
427                                 data = dl->verts;
428                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
429                                 else dl->type = DL_SEGM;
430
431                                 a = len;
432                                 bp = nu->bp;
433                                 while (a--) {
434                                         copy_v3_v3(data, bp->vec);
435                                         bp++;
436                                         data += 3;
437                                 }
438                         }
439                 }
440                 nu = nu->next;
441         }
442 }
443
444 /**
445  * \param normal_proj  Optional normal thats used to project the scanfill verts into 2d coords.
446  * Pass this along if known since it saves time calculating the normal.
447  * \param flipnormal  Flip the normal (same as passing \a normal_proj negated)
448  */
449 void BKE_displist_fill(ListBase *dispbase, ListBase *to, const float normal_proj[3], const bool flipnormal)
450 {
451         ScanFillContext sf_ctx;
452         ScanFillVert *sf_vert, *sf_vert_new, *sf_vert_last;
453         ScanFillFace *sf_tri;
454         MemArena *sf_arena;
455         DispList *dlnew = NULL, *dl;
456         float *f1;
457         int colnr = 0, charidx = 0, cont = 1, tot, a, *index, nextcol = 0;
458         intptr_t totvert;
459
460         if (dispbase == NULL)
461                 return;
462         if (dispbase->first == NULL)
463                 return;
464
465         sf_arena = BLI_memarena_new(BLI_SCANFILL_ARENA_SIZE, __func__);
466
467         while (cont) {
468                 cont = 0;
469                 totvert = 0;
470                 nextcol = 0;
471
472                 BLI_scanfill_begin_arena(&sf_ctx, sf_arena);
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_arena(&sf_ctx, sf_arena);
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         BLI_memarena_free(sf_arena);
574
575         /* do not free polys, needed for wireframe display */
576 }
577
578 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
579 {
580         const float z_up[3] = {0.0f, 0.0f, 1.0f};
581         ListBase front, back;
582         DispList *dl, *dlnew;
583         float *fp, *fp1;
584         int a, dpoly;
585
586         front.first = front.last = back.first = back.last = NULL;
587
588         dl = dispbase->first;
589         while (dl) {
590                 if (dl->type == DL_SURF) {
591                         if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U) == 0) {
592                                 if ( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE)) {
593                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
594                                         BLI_addtail(&front, dlnew);
595                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
596                                         dlnew->nr = dl->parts;
597                                         dlnew->parts = 1;
598                                         dlnew->type = DL_POLY;
599                                         dlnew->col = dl->col;
600                                         dlnew->charidx = dl->charidx;
601
602                                         fp = dl->verts;
603                                         dpoly = 3 * dl->nr;
604
605                                         a = dl->parts;
606                                         while (a--) {
607                                                 copy_v3_v3(fp1, fp);
608                                                 fp1 += 3;
609                                                 fp += dpoly;
610                                         }
611                                 }
612                                 if ((cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE)) {
613                                         dlnew = MEM_callocN(sizeof(DispList), "filldisp");
614                                         BLI_addtail(&back, dlnew);
615                                         dlnew->verts = fp1 = MEM_mallocN(sizeof(float) * 3 * dl->parts, "filldisp1");
616                                         dlnew->nr = dl->parts;
617                                         dlnew->parts = 1;
618                                         dlnew->type = DL_POLY;
619                                         dlnew->col = dl->col;
620                                         dlnew->charidx = dl->charidx;
621
622                                         fp = dl->verts + 3 * (dl->nr - 1);
623                                         dpoly = 3 * dl->nr;
624
625                                         a = dl->parts;
626                                         while (a--) {
627                                                 copy_v3_v3(fp1, fp);
628                                                 fp1 += 3;
629                                                 fp += dpoly;
630                                         }
631                                 }
632                         }
633                 }
634                 dl = dl->next;
635         }
636
637         BKE_displist_fill(&front, dispbase, z_up, true);
638         BKE_displist_fill(&back, dispbase, z_up, false);
639
640         BKE_displist_free(&front);
641         BKE_displist_free(&back);
642
643         BKE_displist_fill(dispbase, dispbase, z_up, false);
644 }
645
646 static void curve_to_filledpoly(Curve *cu, ListBase *UNUSED(nurb), ListBase *dispbase)
647 {
648         if (!CU_DO_2DFILL(cu))
649                 return;
650
651         if (dispbase->first && ((DispList *) dispbase->first)->type == DL_SURF) {
652                 bevels_to_filledpoly(cu, dispbase);
653         }
654         else {
655                 /* TODO, investigate passing zup instead of NULL */
656                 BKE_displist_fill(dispbase, dispbase, NULL, false);
657         }
658 }
659
660 /* taper rules:
661  * - only 1 curve
662  * - first point left, last point right
663  * - based on subdivided points in original curve, not on points in taper curve (still)
664  */
665 static float displist_calc_taper(Scene *scene, Object *taperobj, float fac)
666 {
667         DispList *dl;
668
669         if (taperobj == NULL || taperobj->type != OB_CURVE)
670                 return 1.0;
671
672         dl = taperobj->curve_cache ? taperobj->curve_cache->disp.first : NULL;
673         if (dl == NULL) {
674                 BKE_displist_make_curveTypes(scene, taperobj, 0);
675                 dl = taperobj->curve_cache->disp.first;
676         }
677         if (dl) {
678                 float minx, dx, *fp;
679                 int a;
680
681                 /* horizontal size */
682                 minx = dl->verts[0];
683                 dx = dl->verts[3 * (dl->nr - 1)] - minx;
684                 if (dx > 0.0f) {
685                         fp = dl->verts;
686                         for (a = 0; a < dl->nr; a++, fp += 3) {
687                                 if ((fp[0] - minx) / dx >= fac) {
688                                         /* interpolate with prev */
689                                         if (a > 0) {
690                                                 float fac1 = (fp[-3] - minx) / dx;
691                                                 float fac2 = (fp[0] - minx) / dx;
692                                                 if (fac1 != fac2)
693                                                         return fp[1] * (fac1 - fac) / (fac1 - fac2) + fp[-2] * (fac - fac2) / (fac1 - fac2);
694                                         }
695                                         return fp[1];
696                                 }
697                         }
698                         return fp[-2];  // last y coord
699                 }
700         }
701
702         return 1.0;
703 }
704
705 float BKE_displist_calc_taper(Scene *scene, Object *taperobj, int cur, int tot)
706 {
707         float fac = ((float)cur) / (float)(tot - 1);
708
709         return displist_calc_taper(scene, taperobj, fac);
710 }
711
712 void BKE_displist_make_mball(Scene *scene, Object *ob)
713 {
714         if (!ob || ob->type != OB_MBALL)
715                 return;
716
717         if (ob->curve_cache) {
718                 BKE_displist_free(&(ob->curve_cache->disp));
719         }
720         else {
721                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for MBall");
722         }
723
724         if (ob->type == OB_MBALL) {
725                 if (ob == BKE_mball_basis_find(scene, ob)) {
726                         BKE_mball_polygonize(scene, ob, &ob->curve_cache->disp, false);
727                         BKE_mball_texspace_calc(ob);
728
729                         object_deform_mball(ob, &ob->curve_cache->disp);
730                 }
731
732                 boundbox_displist_object(ob);
733         }
734 }
735
736 void BKE_displist_make_mball_forRender(Scene *scene, Object *ob, ListBase *dispbase)
737 {
738         BKE_mball_polygonize(scene, ob, dispbase, true);
739         BKE_mball_texspace_calc(ob);
740
741         object_deform_mball(ob, dispbase);
742 }
743
744 static ModifierData *curve_get_tessellate_point(Scene *scene, Object *ob, int renderResolution, int editmode)
745 {
746         VirtualModifierData virtualModifierData;
747         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
748         ModifierData *pretessellatePoint;
749         int required_mode;
750
751         if (renderResolution)
752                 required_mode = eModifierMode_Render;
753         else
754                 required_mode = eModifierMode_Realtime;
755
756         if (editmode)
757                 required_mode |= eModifierMode_Editmode;
758
759         pretessellatePoint = NULL;
760         for (; md; md = md->next) {
761                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
762
763                 if (!modifier_isEnabled(scene, md, required_mode))
764                         continue;
765                 if (mti->type == eModifierTypeType_Constructive)
766                         return pretessellatePoint;
767
768                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
769                         pretessellatePoint = md;
770
771                         /* this modifiers are moving point of tessellation automatically
772                          * (some of them even can't be applied on tessellated curve), set flag
773                          * for information button in modifier's header
774                          */
775                         md->mode |= eModifierMode_ApplyOnSpline;
776                 }
777                 else if (md->mode & eModifierMode_ApplyOnSpline) {
778                         pretessellatePoint = md;
779                 }
780         }
781
782         return pretessellatePoint;
783 }
784
785 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, ListBase *nurb,
786                                      int forRender, int renderResolution)
787 {
788         VirtualModifierData virtualModifierData;
789         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
790         ModifierData *pretessellatePoint;
791         Curve *cu = ob->data;
792         int numVerts = 0;
793         const int editmode = (!forRender && (cu->editnurb || cu->editfont));
794         ModifierApplyFlag app_flag = 0;
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_nurbs_keyVertexCos_get(nurb, keyVerts);
822                         BLI_assert(BKE_nurbList_verts_count(nurb) == numVerts);
823                 }
824         }
825
826         if (pretessellatePoint) {
827                 for (; md; md = md->next) {
828                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
829
830                         md->scene = scene;
831
832                         if (!modifier_isEnabled(scene, md, required_mode))
833                                 continue;
834                         if (mti->type != eModifierTypeType_OnlyDeform)
835                                 continue;
836
837                         if (!deformedVerts) {
838                                 deformedVerts = BKE_curve_nurbs_vertexCos_get(nurb, &numVerts);
839                         }
840
841                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, app_flag);
842
843                         if (md == pretessellatePoint)
844                                 break;
845                 }
846         }
847
848         if (deformedVerts) {
849                 BK_curve_nurbs_vertexCos_apply(nurb, deformedVerts);
850                 MEM_freeN(deformedVerts);
851         }
852         if (keyVerts) /* these are not passed through modifier stack */
853                 BKE_curve_nurbs_keyVertexTilts_apply(nurb, keyVerts);
854
855         if (keyVerts)
856                 MEM_freeN(keyVerts);
857 }
858
859 static float (*displist_get_allverts(ListBase *dispbase, int *totvert))[3]
860 {
861         DispList *dl;
862         float (*allverts)[3], *fp;
863
864         *totvert = 0;
865
866         for (dl = dispbase->first; dl; dl = dl->next)
867                 *totvert += (dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr;
868
869         allverts = MEM_mallocN((*totvert) * sizeof(float) * 3, "displist_get_allverts allverts");
870         fp = (float *)allverts;
871         for (dl = dispbase->first; dl; dl = dl->next) {
872                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
873                 memcpy(fp, dl->verts, sizeof(float) * offs);
874                 fp += offs;
875         }
876
877         return allverts;
878 }
879
880 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
881 {
882         DispList *dl;
883         float *fp;
884
885         fp = (float *)allverts;
886         for (dl = dispbase->first; dl; dl = dl->next) {
887                 int offs = 3 * ((dl->type == DL_INDEX3) ? dl->nr : dl->parts * dl->nr);
888                 memcpy(dl->verts, fp, sizeof(float) * offs);
889                 fp += offs;
890         }
891 }
892
893 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *nurb,
894                                       ListBase *dispbase, DerivedMesh **derivedFinal,
895                                       int forRender, int renderResolution)
896 {
897         VirtualModifierData virtualModifierData;
898         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
899         ModifierData *pretessellatePoint;
900         Curve *cu = ob->data;
901         int required_mode = 0, totvert = 0;
902         int editmode = (!forRender && (cu->editnurb || cu->editfont));
903         DerivedMesh *dm = NULL, *ndm;
904         float (*vertCos)[3] = NULL;
905         int useCache = !forRender;
906         ModifierApplyFlag app_flag = 0;
907
908         if (renderResolution) {
909                 app_flag |= MOD_APPLY_RENDER;
910                 required_mode = eModifierMode_Render;
911         }
912         else
913                 required_mode = eModifierMode_Realtime;
914
915         pretessellatePoint = curve_get_tessellate_point(scene, ob, renderResolution, editmode);
916
917         if (editmode)
918                 required_mode |= eModifierMode_Editmode;
919
920         if (pretessellatePoint) {
921                 md = pretessellatePoint->next;
922         }
923
924         if (derivedFinal && *derivedFinal) {
925                 (*derivedFinal)->release(*derivedFinal);
926         }
927
928         for (; md; md = md->next) {
929                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
930                 ModifierApplyFlag appf = app_flag;
931
932                 md->scene = scene;
933
934                 if (!modifier_isEnabled(scene, md, required_mode))
935                         continue;
936
937                 if (mti->type == eModifierTypeType_OnlyDeform ||
938                     (mti->type == eModifierTypeType_DeformOrConstruct && !dm))
939                 {
940                         if (editmode)
941                                 appf |= MOD_APPLY_USECACHE;
942                         if (dm) {
943                                 if (!vertCos) {
944                                         totvert = dm->getNumVerts(dm);
945                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
946                                         dm->getVertCos(dm, vertCos);
947                                 }
948
949                                 mti->deformVerts(md, ob, dm, vertCos, totvert, appf);
950                         }
951                         else {
952                                 if (!vertCos) {
953                                         vertCos = displist_get_allverts(dispbase, &totvert);
954                                 }
955
956                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, appf);
957                         }
958                 }
959                 else {
960                         if (!derivedFinal) {
961                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh
962                                  * is totally unnecessary, so we could stop modifiers applying
963                                  * when we found constructive modifier but derived mesh is unwanted result
964                                  */
965                                 break;
966                         }
967
968                         if (dm) {
969                                 if (vertCos) {
970                                         DerivedMesh *tdm = CDDM_copy(dm);
971                                         dm->release(dm);
972                                         dm = tdm;
973
974                                         CDDM_apply_vert_coords(dm, vertCos);
975                                 }
976                         }
977                         else {
978                                 if (vertCos) {
979                                         displist_apply_allverts(dispbase, vertCos);
980                                 }
981
982                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
983                                         curve_to_filledpoly(cu, nurb, dispbase);
984                                 }
985
986                                 dm = CDDM_from_curve_displist(ob, dispbase);
987                         }
988
989                         if (vertCos) {
990                                 /* Vertex coordinates were applied to necessary data, could free it */
991                                 MEM_freeN(vertCos);
992                                 vertCos = NULL;
993                         }
994
995                         if (useCache)
996                                 appf |= MOD_APPLY_USECACHE;
997
998                         ndm = modwrap_applyModifier(md, ob, dm, appf);
999
1000                         if (ndm) {
1001                                 /* Modifier returned a new derived mesh */
1002
1003                                 if (dm && dm != ndm) /* Modifier  */
1004                                         dm->release(dm);
1005                                 dm = ndm;
1006                         }
1007                 }
1008         }
1009
1010         if (vertCos) {
1011                 if (dm) {
1012                         DerivedMesh *tdm = CDDM_copy(dm);
1013                         dm->release(dm);
1014                         dm = tdm;
1015
1016                         CDDM_apply_vert_coords(dm, vertCos);
1017                         CDDM_calc_normals_mapping(dm);
1018                         MEM_freeN(vertCos);
1019                 }
1020                 else {
1021                         displist_apply_allverts(dispbase, vertCos);
1022                         MEM_freeN(vertCos);
1023                         vertCos = NULL;
1024                 }
1025         }
1026
1027         if (derivedFinal) {
1028                 if (dm) {
1029                         /* see: mesh_calc_modifiers */
1030                         if (dm->getNumTessFaces(dm) == 0) {
1031                                 dm->recalcTessellation(dm);
1032                         }
1033                         /* Even if tessellation is not needed, some modifiers might have modified CD layers
1034                          * (like mloopcol or mloopuv), hence we have to update those. */
1035                         else if (dm->dirty & DM_DIRTY_TESS_CDLAYERS) {
1036                                 DM_update_tessface_data(dm);
1037                         }
1038
1039                         if (dm->type == DM_TYPE_CDDM) {
1040                                 CDDM_calc_normals_mapping_ex(dm, (dm->dirty & DM_DIRTY_NORMALS) ? false : true);
1041                         }
1042                 }
1043                 (*derivedFinal) = dm;
1044         }
1045 }
1046
1047 static void displist_surf_indices(DispList *dl)
1048 {
1049         int a, b, p1, p2, p3, p4;
1050         int *index;
1051
1052         dl->totindex = 0;
1053
1054         index = dl->index = MEM_mallocN(4 * sizeof(int) * (dl->parts + 1) * (dl->nr + 1), "index array nurbs");
1055
1056         for (a = 0; a < dl->parts; a++) {
1057
1058                 if (BKE_displist_surfindex_get(dl, a, &b, &p1, &p2, &p3, &p4) == 0)
1059                         break;
1060
1061                 for (; b < dl->nr; b++, index += 4) {
1062                         index[0] = p1;
1063                         index[1] = p2;
1064                         index[2] = p4;
1065                         index[3] = p3;
1066
1067                         dl->totindex++;
1068
1069                         p2 = p1; p1++;
1070                         p4 = p3; p3++;
1071                 }
1072         }
1073 }
1074
1075 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
1076 {
1077         DerivedMesh *dm;
1078         ListBase disp = {NULL, NULL};
1079
1080         /* OrcoDM should be created from underformed disp lists */
1081         BKE_displist_make_curveTypes_forOrco(scene, ob, &disp);
1082         dm = CDDM_from_curve_displist(ob, &disp);
1083
1084         BKE_displist_free(&disp);
1085
1086         return dm;
1087 }
1088
1089 static void add_orco_dm(Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1090 {
1091         float (*orco)[3], (*layerorco)[3];
1092         int totvert, a;
1093         Curve *cu = ob->data;
1094
1095         totvert = dm->getNumVerts(dm);
1096
1097         orco = MEM_callocN(sizeof(float) * 3 * totvert, "dm orco");
1098
1099         if (orcodm->getNumVerts(orcodm) == totvert)
1100                 orcodm->getVertCos(orcodm, orco);
1101         else
1102                 dm->getVertCos(dm, orco);
1103
1104         for (a = 0; a < totvert; a++) {
1105                 float *co = orco[a];
1106                 co[0] = (co[0] - cu->loc[0]) / cu->size[0];
1107                 co[1] = (co[1] - cu->loc[1]) / cu->size[1];
1108                 co[2] = (co[2] - cu->loc[2]) / cu->size[2];
1109         }
1110
1111         if ((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1112                 memcpy(layerorco, orco, sizeof(float) * totvert);
1113                 MEM_freeN(orco);
1114         }
1115         else
1116                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1117 }
1118
1119 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender, int renderResolution)
1120 {
1121         /* this function represents logic of mesh's orcodm calculation
1122          * for displist-based objects
1123          */
1124         VirtualModifierData virtualModifierData;
1125         ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
1126         ModifierData *pretessellatePoint;
1127         Curve *cu = ob->data;
1128         int required_mode;
1129         int editmode = (!forRender && (cu->editnurb || cu->editfont));
1130         DerivedMesh *ndm, *orcodm = NULL;
1131         ModifierApplyFlag app_flag = MOD_APPLY_ORCO;
1132
1133         if (renderResolution) {
1134                 app_flag |= MOD_APPLY_RENDER;
1135                 required_mode = eModifierMode_Render;
1136         }
1137         else
1138                 required_mode = eModifierMode_Realtime;
1139
1140         pretessellatePoint = curve_get_tessellate_point(scene, ob, renderResolution, editmode);
1141
1142         if (editmode)
1143                 required_mode |= eModifierMode_Editmode;
1144
1145         if (pretessellatePoint) {
1146                 md = pretessellatePoint->next;
1147         }
1148
1149         /* If modifiers are disabled, we wouldn't be here because
1150          * this function is only called if there're enabled constructive
1151          * modifiers applied on the curve.
1152          *
1153          * This means we can create ORCO DM in advance and assume it's
1154          * never NULL.
1155          */
1156         orcodm = create_orco_dm(scene, ob);
1157
1158         for (; md; md = md->next) {
1159                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1160
1161                 md->scene = scene;
1162
1163                 if (!modifier_isEnabled(scene, md, required_mode))
1164                         continue;
1165                 if (mti->type != eModifierTypeType_Constructive)
1166                         continue;
1167
1168                 ndm = modwrap_applyModifier(md, ob, orcodm, app_flag);
1169
1170                 if (ndm) {
1171                         /* if the modifier returned a new dm, release the old one */
1172                         if (orcodm && orcodm != ndm) {
1173                                 orcodm->release(orcodm);
1174                         }
1175                         orcodm = ndm;
1176                 }
1177         }
1178
1179         /* add an orco layer if needed */
1180         add_orco_dm(ob, derivedFinal, orcodm);
1181
1182         orcodm->release(orcodm);
1183 }
1184
1185 void BKE_displist_make_surf(Scene *scene, Object *ob, ListBase *dispbase,
1186                             DerivedMesh **derivedFinal, int forRender, int forOrco, int renderResolution)
1187 {
1188         ListBase nubase = {NULL, NULL};
1189         Nurb *nu;
1190         Curve *cu = ob->data;
1191         DispList *dl;
1192         float *data;
1193         int len;
1194
1195         if (!forRender && cu->editnurb) {
1196                 BKE_nurbList_duplicate(&nubase, BKE_curve_editNurbs_get(cu));
1197         }
1198         else {
1199                 BKE_nurbList_duplicate(&nubase, &cu->nurb);
1200         }
1201
1202         if (!forOrco)
1203                 curve_calc_modifiers_pre(scene, ob, &nubase, forRender, renderResolution);
1204
1205         for (nu = nubase.first; nu; nu = nu->next) {
1206                 if (forRender || nu->hide == 0) {
1207                         int resolu = nu->resolu, resolv = nu->resolv;
1208
1209                         if (renderResolution) {
1210                                 if (cu->resolu_ren)
1211                                         resolu = cu->resolu_ren;
1212                                 if (cu->resolv_ren)
1213                                         resolv = cu->resolv_ren;
1214                         }
1215
1216                         if (nu->pntsv == 1) {
1217                                 len = SEGMENTSU(nu) * resolu;
1218
1219                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1220                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
1221
1222                                 BLI_addtail(dispbase, dl);
1223                                 dl->parts = 1;
1224                                 dl->nr = len;
1225                                 dl->col = nu->mat_nr;
1226                                 dl->charidx = nu->charidx;
1227
1228                                 /* dl->rt will be used as flag for render face and */
1229                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1230                                 dl->rt = nu->flag & ~CU_2D;
1231
1232                                 data = dl->verts;
1233                                 if (nu->flagu & CU_NURB_CYCLIC) dl->type = DL_POLY;
1234                                 else dl->type = DL_SEGM;
1235
1236                                 BKE_nurb_makeCurve(nu, data, NULL, NULL, NULL, resolu, 3 * sizeof(float));
1237                         }
1238                         else {
1239                                 len = (nu->pntsu * resolu) * (nu->pntsv * resolv);
1240
1241                                 dl = MEM_callocN(sizeof(DispList), "makeDispListsurf");
1242                                 dl->verts = MEM_callocN(len * 3 * sizeof(float), "dlverts");
1243                                 BLI_addtail(dispbase, dl);
1244
1245                                 dl->col = nu->mat_nr;
1246                                 dl->charidx = nu->charidx;
1247
1248                                 /* dl->rt will be used as flag for render face and */
1249                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1250                                 dl->rt = nu->flag & ~CU_2D;
1251
1252                                 data = dl->verts;
1253                                 dl->type = DL_SURF;
1254
1255                                 dl->parts = (nu->pntsu * resolu);  /* in reverse, because makeNurbfaces works that way */
1256                                 dl->nr = (nu->pntsv * resolv);
1257                                 if (nu->flagv & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_U;  /* reverse too! */
1258                                 if (nu->flagu & CU_NURB_CYCLIC) dl->flag |= DL_CYCL_V;
1259
1260                                 BKE_nurb_makeFaces(nu, data, 0, resolu, resolv);
1261
1262                                 /* gl array drawing: using indices */
1263                                 displist_surf_indices(dl);
1264                         }
1265                 }
1266         }
1267
1268         if (!forOrco) {
1269                 curve_calc_modifiers_post(scene, ob, &nubase, dispbase, derivedFinal,
1270                                           forRender, renderResolution);
1271         }
1272
1273         BKE_nurbList_free(&nubase);
1274 }
1275
1276 static void rotateBevelPiece(Curve *cu, BevPoint *bevp, BevPoint *nbevp, DispList *dlb, float bev_blend, float widfac, float fac, float **data_r)
1277 {
1278         float *fp, *data = *data_r;
1279         int b;
1280
1281         fp = dlb->verts;
1282         for (b = 0; b < dlb->nr; b++, fp += 3, data += 3) {
1283                 if (cu->flag & CU_3D) {
1284                         float vec[3], quat[4];
1285
1286                         vec[0] = fp[1] + widfac;
1287                         vec[1] = fp[2];
1288                         vec[2] = 0.0;
1289
1290                         if (nbevp == NULL) {
1291                                 copy_v3_v3(data, bevp->vec);
1292                                 copy_qt_qt(quat, bevp->quat);
1293                         }
1294                         else {
1295                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1296                                 interp_qt_qtqt(quat, bevp->quat, nbevp->quat, bev_blend);
1297                         }
1298
1299                         mul_qt_v3(quat, vec);
1300
1301                         data[0] += fac * vec[0];
1302                         data[1] += fac * vec[1];
1303                         data[2] += fac * vec[2];
1304                 }
1305                 else {
1306                         float sina, cosa;
1307
1308                         if (nbevp == NULL) {
1309                                 copy_v3_v3(data, bevp->vec);
1310                                 sina = bevp->sina;
1311                                 cosa = bevp->cosa;
1312                         }
1313                         else {
1314                                 interp_v3_v3v3(data, bevp->vec, nbevp->vec, bev_blend);
1315
1316                                 /* perhaps we need to interpolate angles instead. but the thing is
1317                                  * cosa and sina are not actually sine and cosine
1318                                  */
1319                                 sina = nbevp->sina * bev_blend + bevp->sina * (1.0f - bev_blend);
1320                                 cosa = nbevp->cosa * bev_blend + bevp->cosa * (1.0f - bev_blend);
1321                         }
1322
1323                         data[0] += fac * (widfac + fp[1]) * sina;
1324                         data[1] += fac * (widfac + fp[1]) * cosa;
1325                         data[2] += fac * fp[2];
1326                 }
1327         }
1328
1329         *data_r = data;
1330 }
1331
1332 static void fillBevelCap(Nurb *nu, DispList *dlb, float *prev_fp, ListBase *dispbase)
1333 {
1334         DispList *dl;
1335
1336         dl = MEM_callocN(sizeof(DispList), "makeDispListbev2");
1337         dl->verts = MEM_mallocN(3 * sizeof(float) * dlb->nr, "dlverts");
1338         memcpy(dl->verts, prev_fp, 3 * sizeof(float) * dlb->nr);
1339
1340         dl->type = DL_POLY;
1341
1342         dl->parts = 1;
1343         dl->nr = dlb->nr;
1344         dl->col = nu->mat_nr;
1345         dl->charidx = nu->charidx;
1346
1347         /* dl->rt will be used as flag for render face and */
1348         /* CU_2D conflicts with R_NOPUNOFLIP */
1349         dl->rt = nu->flag & ~CU_2D;
1350
1351         BLI_addtail(dispbase, dl);
1352 }
1353
1354 static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1355                                       DerivedMesh **derivedFinal, int forRender, int forOrco, int renderResolution)
1356 {
1357         Curve *cu = ob->data;
1358
1359         /* we do allow duplis... this is only displist on curve level */
1360         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1361
1362         if (ob->type == OB_SURF) {
1363                 BKE_displist_make_surf(scene, ob, dispbase, derivedFinal, forRender, forOrco, renderResolution);
1364         }
1365         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1366                 ListBase dlbev;
1367                 ListBase nubase = {NULL, NULL};
1368
1369                 BLI_freelistN(&(ob->curve_cache->bev));
1370
1371                 if (ob->curve_cache->path) free_path(ob->curve_cache->path);
1372                 ob->curve_cache->path = NULL;
1373
1374                 if (ob->type == OB_FONT)
1375                         BKE_vfont_to_curve(G.main, scene, ob, 0);
1376
1377                 BKE_nurbList_duplicate(&nubase, BKE_curve_nurbs_get(cu));
1378
1379                 if (!forOrco)
1380                         curve_calc_modifiers_pre(scene, ob, &nubase, forRender, renderResolution);
1381
1382                 BKE_curve_bevelList_make(ob, &nubase, forRender != FALSE);
1383
1384                 /* If curve has no bevel will return nothing */
1385                 BKE_curve_bevel_make(scene, ob, &dlbev, forRender, renderResolution);
1386
1387                 /* no bevel or extrude, and no width correction? */
1388                 if (!dlbev.first && cu->width == 1.0f) {
1389                         curve_to_displist(cu, &nubase, dispbase, forRender, renderResolution);
1390                 }
1391                 else {
1392                         float widfac = cu->width - 1.0f;
1393                         BevList *bl = ob->curve_cache->bev.first;
1394                         Nurb *nu = nubase.first;
1395
1396                         for (; bl && nu; bl = bl->next, nu = nu->next) {
1397                                 DispList *dl;
1398                                 float *data;
1399                                 BevPoint *bevp;
1400                                 int a;
1401
1402                                 if (bl->nr) { /* blank bevel lists can happen */
1403
1404                                         /* exception handling; curve without bevel or extrude, with width correction */
1405                                         if (dlbev.first == NULL) {
1406                                                 dl = MEM_callocN(sizeof(DispList), "makeDispListbev");
1407                                                 dl->verts = MEM_callocN(3 * sizeof(float) * bl->nr, "dlverts");
1408                                                 BLI_addtail(dispbase, dl);
1409
1410                                                 if (bl->poly != -1) dl->type = DL_POLY;
1411                                                 else dl->type = DL_SEGM;
1412
1413                                                 if (dl->type == DL_SEGM) dl->flag = (DL_FRONT_CURVE | DL_BACK_CURVE);
1414
1415                                                 dl->parts = 1;
1416                                                 dl->nr = bl->nr;
1417                                                 dl->col = nu->mat_nr;
1418                                                 dl->charidx = nu->charidx;
1419
1420                                                 /* dl->rt will be used as flag for render face and */
1421                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1422                                                 dl->rt = nu->flag & ~CU_2D;
1423
1424                                                 a = dl->nr;
1425                                                 bevp = (BevPoint *)(bl + 1);
1426                                                 data = dl->verts;
1427                                                 while (a--) {
1428                                                         data[0] = bevp->vec[0] + widfac * bevp->sina;
1429                                                         data[1] = bevp->vec[1] + widfac * bevp->cosa;
1430                                                         data[2] = bevp->vec[2];
1431                                                         bevp++;
1432                                                         data += 3;
1433                                                 }
1434                                         }
1435                                         else {
1436                                                 DispList *dlb;
1437                                                 ListBase bottom_capbase = {NULL, NULL};
1438                                                 ListBase top_capbase = {NULL, NULL};
1439                                                 float bottom_no[3] = {0.0f};
1440                                                 float top_no[3] = {0.0f};
1441
1442                                                 for (dlb = dlbev.first; dlb; dlb = dlb->next) {
1443                                                         const float bevfac1 = min_ff(cu->bevfac1, cu->bevfac2);
1444                                                         const float bevfac2 = max_ff(cu->bevfac1, cu->bevfac2);
1445                                                         float firstblend = 0.0f, lastblend = 0.0f;
1446                                                         int i, start, steps;
1447
1448                                                         if (bevfac2 - bevfac1 == 0.0f)
1449                                                                 continue;
1450
1451                                                         start = (int)(bevfac1 * (bl->nr - 1));
1452                                                         steps = 2 + (int)((bevfac2) * (bl->nr - 1)) - start;
1453                                                         firstblend = 1.0f - (bevfac1 * (bl->nr - 1) - (int)(bevfac1 * (bl->nr - 1)));
1454                                                         lastblend  =         bevfac2 * (bl->nr - 1) - (int)(bevfac2 * (bl->nr - 1));
1455
1456                                                         if (start + steps > bl->nr) {
1457                                                                 steps = bl->nr - start;
1458                                                                 lastblend = 1.0f;
1459                                                         }
1460
1461                                                         /* for each part of the bevel use a separate displblock */
1462                                                         dl = MEM_callocN(sizeof(DispList), "makeDispListbev1");
1463                                                         dl->verts = data = MEM_callocN(3 * sizeof(float) * dlb->nr * steps, "dlverts");
1464                                                         BLI_addtail(dispbase, dl);
1465
1466                                                         dl->type = DL_SURF;
1467
1468                                                         dl->flag = dlb->flag & (DL_FRONT_CURVE | DL_BACK_CURVE);
1469                                                         if (dlb->type == DL_POLY) dl->flag |= DL_CYCL_U;
1470                                                         if (bl->poly >= 0) dl->flag |= DL_CYCL_V;
1471
1472                                                         dl->parts = steps;
1473                                                         dl->nr = dlb->nr;
1474                                                         dl->col = nu->mat_nr;
1475                                                         dl->charidx = nu->charidx;
1476
1477                                                         /* dl->rt will be used as flag for render face and */
1478                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1479                                                         dl->rt = nu->flag & ~CU_2D;
1480
1481                                                         dl->bevelSplitFlag = MEM_callocN(sizeof(*dl->col2) * ((steps + 0x1F) >> 5),
1482                                                                                          "bevelSplitFlag");
1483
1484                                                         /* for each point of poly make a bevel piece */
1485                                                         bevp = (BevPoint *)(bl + 1) + start;
1486                                                         for (i = start, a = 0; a < steps; i++, bevp++, a++) {
1487                                                                 float fac = 1.0;
1488                                                                 float *cur_data = data;
1489
1490                                                                 if (cu->taperobj == NULL) {
1491                                                                         fac = bevp->radius;
1492                                                                 }
1493                                                                 else {
1494                                                                         float len, taper_fac;
1495
1496                                                                         if (cu->flag & CU_MAP_TAPER) {
1497                                                                                 len = (steps - 3) + firstblend + lastblend;
1498
1499                                                                                 if (a == 0)
1500                                                                                         taper_fac = 0.0f;
1501                                                                                 else if (a == steps - 1)
1502                                                                                         taper_fac = 1.0f;
1503                                                                                 else
1504                                                                                         taper_fac = ((float) a - (1.0f - firstblend)) / len;
1505                                                                         }
1506                                                                         else {
1507                                                                                 len = bl->nr - 1;
1508                                                                                 taper_fac = (float) i / len;
1509
1510                                                                                 if (a == 0)
1511                                                                                         taper_fac += (1.0f - firstblend) / len;
1512                                                                                 else if (a == steps - 1)
1513                                                                                         taper_fac -= (1.0f - lastblend) / len;
1514                                                                         }
1515
1516                                                                         fac = displist_calc_taper(scene, cu->taperobj, taper_fac);
1517                                                                 }
1518
1519                                                                 if (bevp->split_tag) {
1520                                                                         dl->bevelSplitFlag[a >> 5] |= 1 << (a & 0x1F);
1521                                                                 }
1522
1523                                                                 /* rotate bevel piece and write in data */
1524                                                                 if (a == 0)
1525                                                                         rotateBevelPiece(cu, bevp, bevp + 1, dlb, 1.0f - firstblend, widfac, fac, &data);
1526                                                                 else if (a == steps - 1)
1527                                                                         rotateBevelPiece(cu, bevp, bevp - 1, dlb, 1.0f - lastblend, widfac, fac, &data);
1528                                                                 else
1529                                                                         rotateBevelPiece(cu, bevp, NULL, dlb, 0.0f, widfac, fac, &data);
1530
1531                                                                 if (cu->bevobj && (cu->flag & CU_FILL_CAPS) && !(nu->flagu & CU_NURB_CYCLIC)) {
1532                                                                         if (a == 1) {
1533                                                                                 fillBevelCap(nu, dlb, cur_data - 3 * dlb->nr, &bottom_capbase);
1534                                                                                 negate_v3_v3(bottom_no, bevp->dir);
1535                                                                         }
1536                                                                         if (a == steps - 1) {
1537                                                                                 fillBevelCap(nu, dlb, cur_data, &top_capbase);
1538                                                                                 copy_v3_v3(top_no, bevp->dir);
1539                                                                         }
1540                                                                 }
1541                                                         }
1542
1543                                                         /* gl array drawing: using indices */
1544                                                         displist_surf_indices(dl);
1545                                                 }
1546
1547                                                 if (bottom_capbase.first) {
1548                                                         BKE_displist_fill(&bottom_capbase, dispbase, bottom_no, false);
1549                                                         BKE_displist_fill(&top_capbase, dispbase, top_no, false);
1550                                                         BKE_displist_free(&bottom_capbase);
1551                                                         BKE_displist_free(&top_capbase);
1552                                                 }
1553                                         }
1554                                 }
1555
1556                         }
1557                         BKE_displist_free(&dlbev);
1558                 }
1559
1560                 if (!(cu->flag & CU_DEFORM_FILL)) {
1561                         curve_to_filledpoly(cu, &nubase, dispbase);
1562                 }
1563
1564                 if ((cu->flag & CU_PATH) && !forOrco)
1565                         calc_curvepath(ob, &nubase);
1566
1567                 if (!forOrco)
1568                         curve_calc_modifiers_post(scene, ob, &nubase, dispbase, derivedFinal, forRender, renderResolution);
1569
1570                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1571                         curve_to_filledpoly(cu, &nubase, dispbase);
1572                 }
1573
1574                 BKE_nurbList_free(&nubase);
1575         }
1576 }
1577
1578 void BKE_displist_make_curveTypes(Scene *scene, Object *ob, int forOrco)
1579 {
1580         ListBase *dispbase;
1581
1582         /* The same check for duplis as in do_makeDispListCurveTypes.
1583          * Happens when curve used for constraint/bevel was converted to mesh.
1584          * check there is still needed for render displist and orco displists. */
1585         if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT))
1586                 return;
1587
1588         BKE_object_free_derived_caches(ob);
1589
1590         if (!ob->curve_cache) {
1591                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for curve types");
1592         }
1593
1594         dispbase = &(ob->curve_cache->disp);
1595
1596         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco, 0);
1597
1598         boundbox_displist_object(ob);
1599 }
1600
1601 void BKE_displist_make_curveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1602                                       DerivedMesh **derivedFinal, int forOrco, int renderResolution)
1603 {
1604         if (ob->curve_cache == NULL) {
1605                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for MBall");
1606         }
1607
1608         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco, renderResolution);
1609 }
1610
1611 void BKE_displist_make_curveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1612 {
1613         if (ob->curve_cache == NULL) {
1614                 ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for MBall");
1615         }
1616
1617         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1, 1);
1618 }
1619
1620 /* add Orco layer to the displist object which has got derived mesh and return orco */
1621 float *BKE_displist_make_orco(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender, int renderResolution)
1622 {
1623         float *orco;
1624
1625         if (derivedFinal == NULL)
1626                 derivedFinal = ob->derivedFinal;
1627
1628         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1629                 curve_calc_orcodm(scene, ob, derivedFinal, forRender, renderResolution);
1630         }
1631
1632         orco = derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1633
1634         if (orco) {
1635                 orco = MEM_dupallocN(orco);
1636         }
1637
1638         return orco;
1639 }
1640
1641 void BKE_displist_minmax(ListBase *dispbase, float min[3], float max[3])
1642 {
1643         DispList *dl;
1644         float *vert;
1645         int a, tot = 0;
1646         int doit = 0;
1647
1648         for (dl = dispbase->first; dl; dl = dl->next) {
1649                 tot = (dl->type == DL_INDEX3) ? dl->nr : dl->nr * dl->parts;
1650                 vert = dl->verts;
1651                 for (a = 0; a < tot; a++, vert += 3) {
1652                         minmax_v3v3_v3(min, max, vert);
1653                 }
1654                 doit |= (tot != 0);
1655         }
1656
1657         if (!doit) {
1658                 /* there's no geometry in displist, use zero-sized boundbox */
1659                 zero_v3(min);
1660                 zero_v3(max);
1661         }
1662 }
1663
1664 /* this is confusing, there's also min_max_object, appplying the obmat... */
1665 static void boundbox_displist_object(Object *ob)
1666 {
1667         if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1668                 /* Curver's BB is already calculated as a part of modifier stack,
1669                  * here we only calculate object BB based on final display list.
1670                  */
1671
1672                 /* object's BB is calculated from final displist */
1673                 if (ob->bb == NULL)
1674                         ob->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
1675
1676                 if (ob->derivedFinal) {
1677                         DM_set_object_boundbox(ob, ob->derivedFinal);
1678                 }
1679                 else {
1680                         float min[3], max[3];
1681
1682                         INIT_MINMAX(min, max);
1683                         BKE_displist_minmax(&ob->curve_cache->disp, min, max);
1684                         BKE_boundbox_init_from_minmax(ob->bb, min, max);
1685                 }
1686         }
1687 }