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