merge with trunk at r31523
[blender.git] / source / blender / blenkernel / intern / displist.c
1 /*  displist.c
2  * 
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <math.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38
39 #include "DNA_curve_types.h"
40 #include "DNA_meshdata_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_material_types.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_math.h"
47 #include "BLI_editVert.h"
48
49 #include "BKE_global.h"
50 #include "BKE_displist.h"
51 #include "BKE_cdderivedmesh.h"
52 #include "BKE_object.h"
53 #include "BKE_mball.h"
54 #include "BKE_material.h"
55 #include "BKE_curve.h"
56 #include "BKE_key.h"
57 #include "BKE_anim.h"
58 #include "BKE_font.h"
59 #include "BKE_lattice.h"
60 #include "BKE_modifier.h"
61
62 #include "RE_pipeline.h"
63 #include "RE_shader_ext.h"
64
65 #include "BLO_sys_types.h" // for intptr_t support
66
67 #include "ED_curve.h" /* for BKE_curve_nurbs */
68
69 static void boundbox_displist(Object *ob);
70
71 void free_disp_elem(DispList *dl)
72 {
73         if(dl) {
74                 if(dl->verts) MEM_freeN(dl->verts);
75                 if(dl->nors) MEM_freeN(dl->nors);
76                 if(dl->index) MEM_freeN(dl->index);
77                 if(dl->col1) MEM_freeN(dl->col1);
78                 if(dl->col2) MEM_freeN(dl->col2);
79                 if(dl->bevelSplitFlag) MEM_freeN(dl->bevelSplitFlag);
80                 MEM_freeN(dl);
81         }
82 }
83
84 void freedisplist(ListBase *lb)
85 {
86         DispList *dl;
87
88         dl= lb->first;
89         while(dl) {
90                 BLI_remlink(lb, dl);
91                 free_disp_elem(dl);
92                 dl= lb->first;
93         }
94 }
95
96 DispList *find_displist_create(ListBase *lb, int type)
97 {
98         DispList *dl;
99         
100         dl= lb->first;
101         while(dl) {
102                 if(dl->type==type) return dl;
103                 dl= dl->next;
104         }
105
106         dl= MEM_callocN(sizeof(DispList), "find_disp");
107         dl->type= type;
108         BLI_addtail(lb, dl);
109
110         return dl;
111 }
112
113 DispList *find_displist(ListBase *lb, int type)
114 {
115         DispList *dl;
116         
117         dl= lb->first;
118         while(dl) {
119                 if(dl->type==type) return dl;
120                 dl= dl->next;
121         }
122
123         return 0;
124 }
125
126 int displist_has_faces(ListBase *lb)
127 {
128         DispList *dl;
129         for(dl= lb->first; dl; dl= dl->next) {
130                 if ELEM3(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)
131                         return 1;
132         }
133         return 0;
134 }
135
136 void copy_displist(ListBase *lbn, ListBase *lb)
137 {
138         DispList *dln, *dl;
139         
140         lbn->first= lbn->last= 0;
141         
142         dl= lb->first;
143         while(dl) {
144                 
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                 dl= dl->next;
154         }
155 }
156
157 void addnormalsDispList(ListBase *lb)
158 {
159         DispList *dl = NULL;
160         float *vdata, *ndata, nor[3];
161         float *v1, *v2, *v3, *v4;
162         float *n1, *n2, *n3, *n4;
163         int a, b, p1, p2, p3, p4;
164
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                                 if(dl->verts[2]<0.0) dl->nors[2]= -1.0;
173                                 else dl->nors[2]= 1.0;
174                         }
175                 }
176                 else if(dl->type==DL_SURF) {
177                         if(dl->nors==NULL) {
178                                 dl->nors= MEM_callocN(sizeof(float)*3*dl->nr*dl->parts, "dlnors");
179                                 
180                                 vdata= dl->verts;
181                                 ndata= dl->nors;
182                                 
183                                 for(a=0; a<dl->parts; a++) {
184                                         
185                                         if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
186                                                 break;
187         
188                                         v1= vdata+ 3*p1; 
189                                         n1= ndata+ 3*p1;
190                                         v2= vdata+ 3*p2; 
191                                         n2= ndata+ 3*p2;
192                                         v3= vdata+ 3*p3; 
193                                         n3= ndata+ 3*p3;
194                                         v4= vdata+ 3*p4; 
195                                         n4= ndata+ 3*p4;
196                                         
197                                         for(; b<dl->nr; b++) {
198         
199                                                 normal_quad_v3( nor,v1, v3, v4, v2);
200         
201                                                 add_v3_v3(n1, nor);
202                                                 add_v3_v3(n2, nor);
203                                                 add_v3_v3(n3, nor);
204                                                 add_v3_v3(n4, nor);
205         
206                                                 v2= v1; v1+= 3;
207                                                 v4= v3; v3+= 3;
208                                                 n2= n1; n1+= 3;
209                                                 n4= n3; n3+= 3;
210                                         }
211                                 }
212                                 a= dl->parts*dl->nr;
213                                 v1= ndata;
214                                 while(a--) {
215                                         normalize_v3(v1);
216                                         v1+= 3;
217                                 }
218                         }
219                 }
220                 dl= dl->next;
221         }
222 }
223
224 void count_displist(ListBase *lb, int *totvert, int *totface)
225 {
226         DispList *dl;
227         
228         dl= lb->first;
229         while(dl) {
230                 
231                 switch(dl->type) {
232                         case DL_SURF:
233                                 *totvert+= dl->nr*dl->parts;
234                                 *totface+= (dl->nr-1)*(dl->parts-1);
235                                 break;
236                         case DL_INDEX3:
237                         case DL_INDEX4:
238                                 *totvert+= dl->nr;
239                                 *totface+= dl->parts;
240                                 break;
241                         case DL_POLY:
242                         case DL_SEGM:
243                                 *totvert+= dl->nr*dl->parts;
244                 }
245                 
246                 dl= dl->next;
247         }
248 }
249
250 int surfindex_displist(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4)
251 {
252         if((dl->flag & DL_CYCL_V)==0 && a==(dl->parts)-1) {
253                 return 0;
254         }
255         
256         if(dl->flag & DL_CYCL_U) {
257                 (*p1)= dl->nr*a;
258                 (*p2)= (*p1)+ dl->nr-1;
259                 (*p3)= (*p1)+ dl->nr;
260                 (*p4)= (*p2)+ dl->nr;
261                 (*b)= 0;
262         } else {
263                 (*p2)= dl->nr*a;
264                 (*p1)= (*p2)+1;
265                 (*p4)= (*p2)+ dl->nr;
266                 (*p3)= (*p1)+ dl->nr;
267                 (*b)= 1;
268         }
269         
270         if( (dl->flag & DL_CYCL_V) && a==dl->parts-1) {                     \
271                 (*p3)-= dl->nr*dl->parts;                                   \
272                 (*p4)-= dl->nr*dl->parts;                                   \
273         }
274         
275         return 1;
276 }
277
278 /* ***************************** shade displist. note colors now are in rgb(a) order ******************** */
279
280 /* create default shade input... save cpu cycles with ugly global */
281 /* XXXX bad code warning: local ShadeInput initialize... */
282 static ShadeInput shi;
283 static void init_fastshade_shadeinput(Render *re)
284 {
285         memset(&shi, 0, sizeof(ShadeInput));
286         shi.lay= RE_GetScene(re)->lay;
287         shi.view[2]= -1.0f;
288         shi.passflag= SCE_PASS_COMBINED;
289         shi.combinedflag= -1;
290 }
291
292 static Render *fastshade_get_render(Scene *scene)
293 {
294         // XXX 2.5: this crashes combined with previewrender
295         // due to global R so disabled for now
296 #if 0
297         /* XXX ugly global still, but we can't do preview while rendering */
298         if(G.rendering==0) {
299                 
300                 Render *re= RE_GetRender("_Shade View_");
301                 if(re==NULL) {
302                         re= RE_NewRender("_Shade View_");
303                 
304                         RE_Database_Baking(re, scene, 0, 0);    /* 0= no faces */
305                 }
306                 return re;
307         }
308 #endif
309         
310         return NULL;
311 }
312
313 /* called on file reading */
314 void fastshade_free_render(void)
315 {
316         Render *re= RE_GetRender("_Shade View_");
317         
318         if(re) {
319                 RE_Database_Free(re);
320                 RE_FreeRender(re);
321         }
322 }
323
324 static int fastshade_customdata_layer_num(int n, int active)
325 {   
326         /* make the active layer the first */
327         if (n == active) return 0;
328         else if (n < active) return n+1;
329         else return n;
330 }
331
332 static void fastshade_customdata(CustomData *fdata, int a, int j, Material *ma)
333 {
334         CustomDataLayer *layer;
335         MTFace *mtface;
336         int index, n, needuv= ma->texco & TEXCO_UV;
337         char *vertcol;
338
339         shi.totuv= 0;
340         shi.totcol= 0;
341
342         for(index=0; index<fdata->totlayer; index++) {
343                 layer= &fdata->layers[index];
344                 
345                 if(needuv && layer->type == CD_MTFACE && shi.totuv < MAX_MTFACE) {
346                         n= fastshade_customdata_layer_num(shi.totuv, layer->active_rnd);
347                         mtface= &((MTFace*)layer->data)[a];
348
349                         shi.uv[shi.totuv].uv[0]= 2.0f*mtface->uv[j][0]-1.0f;
350                         shi.uv[shi.totuv].uv[1]= 2.0f*mtface->uv[j][1]-1.0f;
351                         shi.uv[shi.totuv].uv[2]= 1.0f;
352
353                         shi.uv[shi.totuv].name= layer->name;
354                         shi.totuv++;
355                 }
356                 else if(layer->type == CD_MCOL && shi.totcol < MAX_MCOL) {
357                         n= fastshade_customdata_layer_num(shi.totcol, layer->active_rnd);
358                         vertcol= (char*)&((MCol*)layer->data)[a*4 + j];
359
360                         shi.col[shi.totcol].col[0]= ((float)vertcol[3])/255.0f;
361                         shi.col[shi.totcol].col[1]= ((float)vertcol[2])/255.0f;
362                         shi.col[shi.totcol].col[2]= ((float)vertcol[1])/255.0f;
363
364                         shi.col[shi.totcol].name= layer->name;
365                         shi.totcol++;
366                 }
367         }
368
369         if(needuv && shi.totuv == 0)
370                 VECCOPY(shi.uv[0].uv, shi.lo);
371
372         if(shi.totcol)
373                 VECCOPY(shi.vcol, shi.col[0].col);
374 }
375
376 static void fastshade(float *co, float *nor, float *orco, Material *ma, char *col1, char *col2)
377 {
378         ShadeResult shr;
379         int a;
380         
381         VECCOPY(shi.co, co);
382         shi.vn[0]= -nor[0];
383         shi.vn[1]= -nor[1];
384         shi.vn[2]= -nor[2];
385         VECCOPY(shi.vno, shi.vn);
386         VECCOPY(shi.facenor, shi.vn);
387         
388         if(ma->texco) {
389                 VECCOPY(shi.lo, orco);
390                 
391                 if(ma->texco & TEXCO_GLOB) {
392                         VECCOPY(shi.gl, shi.lo);
393                 }
394                 if(ma->texco & TEXCO_WINDOW) {
395                         VECCOPY(shi.winco, shi.lo);
396                 }
397                 if(ma->texco & TEXCO_STICKY) {
398                         VECCOPY(shi.sticky, shi.lo);
399                 }
400                 if(ma->texco & TEXCO_OBJECT) {
401                         VECCOPY(shi.co, shi.lo);
402                 }
403                 if(ma->texco & TEXCO_NORM) {
404                         VECCOPY(shi.orn, shi.vn);
405                 }
406                 if(ma->texco & TEXCO_REFL) {
407                         float inp= 2.0*(shi.vn[2]);
408                         shi.ref[0]= (inp*shi.vn[0]);
409                         shi.ref[1]= (inp*shi.vn[1]);
410                         shi.ref[2]= (-1.0+inp*shi.vn[2]);
411                 }
412         }
413         
414         shi.mat= ma;    /* set each time... node shaders change it */
415         RE_shade_external(NULL, &shi, &shr);
416         
417         a= 256.0f*(shr.combined[0]);
418         col1[0]= CLAMPIS(a, 0, 255);
419         a= 256.0f*(shr.combined[1]);
420         col1[1]= CLAMPIS(a, 0, 255);
421         a= 256.0f*(shr.combined[2]);
422         col1[2]= CLAMPIS(a, 0, 255);
423         
424         if(col2) {
425                 shi.vn[0]= -shi.vn[0];
426                 shi.vn[1]= -shi.vn[1];
427                 shi.vn[2]= -shi.vn[2];
428                 
429                 shi.mat= ma;    /* set each time... node shaders change it */
430                 RE_shade_external(NULL, &shi, &shr);
431                 
432                 a= 256.0f*(shr.combined[0]);
433                 col2[0]= CLAMPIS(a, 0, 255);
434                 a= 256.0f*(shr.combined[1]);
435                 col2[1]= CLAMPIS(a, 0, 255);
436                 a= 256.0f*(shr.combined[2]);
437                 col2[2]= CLAMPIS(a, 0, 255);
438         }
439 }
440
441 static void init_fastshade_for_ob(Render *re, Object *ob, int *need_orco_r, float mat[4][4], float imat[3][3])
442 {
443         float tmat[4][4];
444         float amb[3]= {0.0f, 0.0f, 0.0f};
445         int a;
446         
447         /* initialize globals in render */
448         RE_shade_external(re, NULL, NULL);
449
450         /* initialize global here */
451         init_fastshade_shadeinput(re);
452         
453         RE_DataBase_GetView(re, tmat);
454         mul_m4_m4m4(mat, ob->obmat, tmat);
455         
456         invert_m4_m4(tmat, mat);
457         copy_m3_m4(imat, tmat);
458         if(ob->transflag & OB_NEG_SCALE) mul_m3_fl(imat, -1.0);
459         
460         if (need_orco_r) *need_orco_r= 0;
461         for(a=0; a<ob->totcol; a++) {
462                 Material *ma= give_current_material(ob, a+1);
463                 if(ma) {
464                         init_render_material(ma, 0, amb);
465
466                         if(ma->texco & TEXCO_ORCO) {
467                                 if (need_orco_r) *need_orco_r= 1;
468                         }
469                 }
470         }
471 }
472
473 static void end_fastshade_for_ob(Object *ob)
474 {
475         int a;
476         
477         for(a=0; a<ob->totcol; a++) {
478                 Material *ma= give_current_material(ob, a+1);
479                 if(ma)
480                         end_render_material(ma);
481         }
482 }
483
484
485 static void mesh_create_shadedColors(Render *re, Object *ob, int onlyForMesh, unsigned int **col1_r, unsigned int **col2_r)
486 {
487         Mesh *me= ob->data;
488         DerivedMesh *dm;
489         MVert *mvert;
490         MFace *mface;
491         unsigned int *col1, *col2;
492         float *orco, *vnors, *nors, imat[3][3], mat[4][4], vec[3];
493         int a, i, need_orco, totface, totvert;
494         CustomDataMask dataMask = CD_MASK_BAREMESH | CD_MASK_MCOL
495                                                           | CD_MASK_MTFACE | CD_MASK_NORMAL;
496
497
498         init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
499
500         if(need_orco)
501                 dataMask |= CD_MASK_ORCO;
502
503         if (onlyForMesh)
504                 dm = mesh_get_derived_deform(RE_GetScene(re), ob, dataMask);
505         else
506                 dm = mesh_get_derived_final(RE_GetScene(re), ob, dataMask);
507         
508         mvert = dm->getVertArray(dm);
509         mface = dm->getTessFaceArray(dm);
510         nors = dm->getTessFaceDataArray(dm, CD_NORMAL);
511         totvert = dm->getNumVerts(dm);
512         totface = dm->getNumTessFaces(dm);
513         orco= dm->getVertDataArray(dm, CD_ORCO);
514
515         if (onlyForMesh) {
516                 col1 = *col1_r;
517                 col2 = NULL;
518         } else {
519                 *col1_r = col1 = MEM_mallocN(sizeof(*col1)*totface*4, "col1");
520
521                 if (col2_r && (me->flag & ME_TWOSIDED))
522                         col2 = MEM_mallocN(sizeof(*col2)*totface*4, "col2");
523                 else
524                         col2 = NULL;
525                 
526                 if (col2_r) *col2_r = col2;
527         }
528
529                 /* vertexnormals */
530         vnors= MEM_mallocN(totvert*3*sizeof(float), "vnors disp");
531         for (a=0; a<totvert; a++) {
532                 MVert *mv = &mvert[a];
533                 float *vn= &vnors[a*3];
534                 float xn= mv->no[0]; 
535                 float yn= mv->no[1]; 
536                 float zn= mv->no[2];
537                 
538                         /* transpose ! */
539                 vn[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
540                 vn[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
541                 vn[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
542                 normalize_v3(vn);
543         }               
544
545         for (i=0; i<totface; i++) {
546                 extern Material defmaterial;    /* material.c */
547                 MFace *mf= &mface[i];
548                 Material *ma= give_current_material(ob, mf->mat_nr+1);
549                 int j, vidx[4], nverts= mf->v4?4:3;
550                 unsigned char *col1base= (unsigned char*) &col1[i*4];
551                 unsigned char *col2base= (unsigned char*) (col2?&col2[i*4]:NULL);
552                 float nor[3], n1[3];
553                 
554                 if(ma==NULL) ma= &defmaterial;
555                 
556                 vidx[0]= mf->v1;
557                 vidx[1]= mf->v2;
558                 vidx[2]= mf->v3;
559                 vidx[3]= mf->v4;
560
561                 if (nors) {
562                         VECCOPY(nor, &nors[i*3]);
563                 } else {
564                         if (mf->v4)
565                                 normal_quad_v3( nor,mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co, mvert[mf->v4].co);
566                         else
567                                 normal_tri_v3( nor,mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co);
568                 }
569
570                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
571                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
572                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
573                 normalize_v3(n1);
574
575                 for (j=0; j<nverts; j++) {
576                         MVert *mv= &mvert[vidx[j]];
577                         char *col1= (char*)&col1base[j*4];
578                         char *col2= (char*)(col2base?&col2base[j*4]:NULL);
579                         float *vn = (mf->flag & ME_SMOOTH)?&vnors[3*vidx[j]]:n1;
580
581                         mul_v3_m4v3(vec, mat, mv->co);
582
583                         vec[0]+= 0.001*vn[0];
584                         vec[1]+= 0.001*vn[1];
585                         vec[2]+= 0.001*vn[2];
586
587                         fastshade_customdata(&dm->faceData, i, j, ma);
588                         fastshade(vec, vn, orco?&orco[vidx[j]*3]:mv->co, ma, col1, col2);
589                 }
590         } 
591         MEM_freeN(vnors);
592
593         dm->release(dm);
594
595         end_fastshade_for_ob(ob);
596 }
597
598 void shadeMeshMCol(Scene *scene, Object *ob, Mesh *me)
599 {
600         Render *re= fastshade_get_render(scene);
601         int a;
602         char *cp;
603         unsigned int *mcol= (unsigned int*)me->mcol;
604         
605         if(re) {
606                 mesh_create_shadedColors(re, ob, 1, &mcol, NULL);
607                 me->mcol= (MCol*)mcol;
608
609                 /* swap bytes */
610                 for(cp= (char *)me->mcol, a= 4*me->totface; a>0; a--, cp+=4) {
611                         SWAP(char, cp[0], cp[3]);
612                         SWAP(char, cp[1], cp[2]);
613                 }
614         }
615 }
616
617 /* has base pointer, to check for layer */
618 /* called from drawobject.c */
619 void shadeDispList(Scene *scene, Base *base)
620 {
621         Object *ob= base->object;
622         DispList *dl, *dlob;
623         Material *ma = NULL;
624         Curve *cu;
625         Render *re;
626         float imat[3][3], mat[4][4], vec[3];
627         float *fp, *nor, n1[3];
628         unsigned int *col1;
629         int a, need_orco;
630         
631         re= fastshade_get_render(scene);
632         if(re==NULL)
633                 return;
634         
635         dl = find_displist(&ob->disp, DL_VERTCOL);
636         if (dl) {
637                 BLI_remlink(&ob->disp, dl);
638                 free_disp_elem(dl);
639         }
640
641         if(ob->type==OB_MESH) {
642                 dl= MEM_callocN(sizeof(DispList), "displistshade");
643                 dl->type= DL_VERTCOL;
644
645                 mesh_create_shadedColors(re, ob, 0, &dl->col1, &dl->col2);
646
647                 /* add dl to ob->disp after mesh_create_shadedColors, because it
648                    might indirectly free ob->disp */
649                 BLI_addtail(&ob->disp, dl);
650         }
651         else {
652
653                 init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
654                 
655                 if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
656                 
657                         /* now we need the normals */
658                         cu= ob->data;
659                         dl= cu->disp.first;
660                         
661                         while(dl) {
662                                 extern Material defmaterial;    /* material.c */
663                                 
664                                 dlob= MEM_callocN(sizeof(DispList), "displistshade");
665                                 BLI_addtail(&ob->disp, dlob);
666                                 dlob->type= DL_VERTCOL;
667                                 dlob->parts= dl->parts;
668                                 dlob->nr= dl->nr;
669                                 
670                                 if(dl->type==DL_INDEX3) {
671                                         col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
672                                 }
673                                 else {
674                                         col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->parts*dl->nr, "col1");
675                                 }
676                                 
677                         
678                                 ma= give_current_material(ob, dl->col+1);
679                                 if(ma==NULL) ma= &defmaterial;
680                                 
681                                 if(dl->type==DL_INDEX3) {
682                                         if(dl->nors) {
683                                                 /* there's just one normal */
684                                                 n1[0]= imat[0][0]*dl->nors[0]+imat[0][1]*dl->nors[1]+imat[0][2]*dl->nors[2];
685                                                 n1[1]= imat[1][0]*dl->nors[0]+imat[1][1]*dl->nors[1]+imat[1][2]*dl->nors[2];
686                                                 n1[2]= imat[2][0]*dl->nors[0]+imat[2][1]*dl->nors[1]+imat[2][2]*dl->nors[2];
687                                                 normalize_v3(n1);
688                                                 
689                                                 fp= dl->verts;
690                                                 
691                                                 a= dl->nr;              
692                                                 while(a--) {
693                                                         mul_v3_m4v3(vec, mat, fp);
694                                                         
695                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
696                                                         
697                                                         fp+= 3; col1++;
698                                                 }
699                                         }
700                                 }
701                                 else if(dl->type==DL_SURF) {
702                                         if(dl->nors) {
703                                                 a= dl->nr*dl->parts;
704                                                 fp= dl->verts;
705                                                 nor= dl->nors;
706                                                 
707                                                 while(a--) {
708                                                         mul_v3_m4v3(vec, mat, fp);
709                                                         
710                                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
711                                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
712                                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
713                                                         normalize_v3(n1);
714                                 
715                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
716                                                         
717                                                         fp+= 3; nor+= 3; col1++;
718                                                 }
719                                         }
720                                 }
721                                 dl= dl->next;
722                         }
723                 }
724                 else if(ob->type==OB_MBALL) {
725                         /* there are normals already */
726                         dl= ob->disp.first;
727                         
728                         while(dl) {
729                                 
730                                 if(dl->type==DL_INDEX4) {
731                                         if(dl->nors) {
732                                                 extern Material defmaterial;    /* material.c */
733                                                 
734                                                 if(dl->col1) MEM_freeN(dl->col1);
735                                                 col1= dl->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
736                                 
737                                                 ma= give_current_material(ob, dl->col+1);
738                                                 if(ma==NULL) ma= &defmaterial;
739                                                 
740                                                 fp= dl->verts;
741                                                 nor= dl->nors;
742                                                 
743                                                 a= dl->nr;              
744                                                 while(a--) {
745                                                         mul_v3_m4v3(vec, mat, fp);
746                                                         
747                                                         /* transpose ! */
748                                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
749                                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
750                                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
751                                                         normalize_v3(n1);
752                                                 
753                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
754                                                         
755                                                         fp+= 3; col1++; nor+= 3;
756                                                 }
757                                         }
758                                 }
759                                 dl= dl->next;
760                         }
761                 }
762                 
763                 end_fastshade_for_ob(ob);
764         }
765 }
766
767 /* frees render and shade part of displists */
768 /* note: dont do a shade again, until a redraw happens */
769 void reshadeall_displist(Scene *scene)
770 {
771         Base *base;
772         Object *ob;
773         
774         fastshade_free_render();
775         
776         for(base= scene->base.first; base; base= base->next) {
777                 ob= base->object;
778
779                 if(ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
780                         freedisplist(&ob->disp);
781
782                 if(base->lay & scene->lay) {
783                         /* Metaballs have standard displist at the Object */
784                         if(ob->type==OB_MBALL) shadeDispList(scene, base);
785                 }
786         }
787 }
788
789 /* ****************** make displists ********************* */
790
791 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase)
792 {
793         Nurb *nu;
794         DispList *dl;
795         BezTriple *bezt, *prevbezt;
796         BPoint *bp;
797         float *data;
798         int a, len, resolu;
799         
800         nu= nubase->first;
801         while(nu) {
802                 if(nu->hide==0) {
803                         
804                         if(G.rendering && cu->resolu_ren!=0) 
805                                 resolu= cu->resolu_ren;
806                         else
807                                 resolu= nu->resolu;
808                         
809                         if(!check_valid_nurb_u(nu));
810                         else if(nu->type == CU_BEZIER) {
811                                 
812                                 /* count */
813                                 len= 0;
814                                 a= nu->pntsu-1;
815                                 if(nu->flagu & CU_NURB_CYCLIC) a++;
816
817                                 prevbezt= nu->bezt;
818                                 bezt= prevbezt+1;
819                                 while(a--) {
820                                         if(a==0 && (nu->flagu & CU_NURB_CYCLIC)) bezt= nu->bezt;
821                                         
822                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) len++;
823                                         else len+= resolu;
824                                         
825                                         if(a==0 && (nu->flagu & CU_NURB_CYCLIC)==0) len++;
826                                         
827                                         prevbezt= bezt;
828                                         bezt++;
829                                 }
830                                 
831                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbez");
832                                 /* len+1 because of 'forward_diff_bezier' function */
833                                 dl->verts= MEM_callocN( (len+1)*3*sizeof(float), "dlverts");
834                                 BLI_addtail(dispbase, dl);
835                                 dl->parts= 1;
836                                 dl->nr= len;
837                                 dl->col= nu->mat_nr;
838                                 dl->charidx= nu->charidx;
839
840                                 data= dl->verts;
841
842                                 if(nu->flagu & CU_NURB_CYCLIC) {
843                                         dl->type= DL_POLY;
844                                         a= nu->pntsu;
845                                 }
846                                 else {
847                                         dl->type= DL_SEGM;
848                                         a= nu->pntsu-1;
849                                 }
850                                 
851                                 prevbezt= nu->bezt;
852                                 bezt= prevbezt+1;
853                                 
854                                 while(a--) {
855                                         if(a==0 && dl->type== DL_POLY) bezt= nu->bezt;
856                                         
857                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
858                                                 VECCOPY(data, prevbezt->vec[1]);
859                                                 data+= 3;
860                                         }
861                                         else {
862                                                 int j;
863                                                 for(j=0; j<3; j++) {
864                                                         forward_diff_bezier(    prevbezt->vec[1][j],
865                                                                                                         prevbezt->vec[2][j],
866                                                                                                         bezt->vec[0][j],
867                                                                                                         bezt->vec[1][j],
868                                                                                                         data+j, resolu, 3*sizeof(float));
869                                                 }
870                                                 
871                                                 data+= 3*resolu;
872                                         }
873                                         
874                                         if(a==0 && dl->type==DL_SEGM) {
875                                                 VECCOPY(data, bezt->vec[1]);
876                                         }
877                                         
878                                         prevbezt= bezt;
879                                         bezt++;
880                                 }
881                         }
882                         else if(nu->type == CU_NURBS) {
883                                 len= (resolu*SEGMENTSU(nu));
884                                 
885                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
886                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
887                                 BLI_addtail(dispbase, dl);
888                                 dl->parts= 1;
889                                 
890                                 dl->nr= len;
891                                 dl->col= nu->mat_nr;
892                                 dl->charidx = nu->charidx;
893
894                                 data= dl->verts;
895                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
896                                 else dl->type= DL_SEGM;
897                                 makeNurbcurve(nu, data, NULL, NULL, NULL, resolu, 3*sizeof(float));
898                         }
899                         else if(nu->type == CU_POLY) {
900                                 len= nu->pntsu;
901                                 dl= MEM_callocN(sizeof(DispList), "makeDispListpoly");
902                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
903                                 BLI_addtail(dispbase, dl);
904                                 dl->parts= 1;
905                                 dl->nr= len;
906                                 dl->col= nu->mat_nr;
907                                 dl->charidx = nu->charidx;
908
909                                 data= dl->verts;
910                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
911                                 else dl->type= DL_SEGM;
912                                 
913                                 a= len;
914                                 bp= nu->bp;
915                                 while(a--) {
916                                         VECCOPY(data, bp->vec);
917                                         bp++;
918                                         data+= 3;
919                                 }
920                         }
921                 }
922                 nu= nu->next;
923         }
924 }
925
926
927 void filldisplist(ListBase *dispbase, ListBase *to, int flipnormal)
928 {
929         EditVert *eve, *v1, *vlast;
930         EditFace *efa;
931         DispList *dlnew=0, *dl;
932         float *f1;
933         int colnr=0, charidx=0, cont=1, tot, a, *index;
934         intptr_t totvert;
935         
936         if(dispbase==0) return;
937         if(dispbase->first==0) return;
938
939         while(cont) {
940                 cont= 0;
941                 totvert=0;
942                 
943                 dl= dispbase->first;
944                 while(dl) {
945         
946                         if(dl->type==DL_POLY) {
947                                 if(charidx<dl->charidx) cont= 1;
948                                 else if(charidx==dl->charidx) {
949                         
950                                         colnr= dl->col;
951                                         charidx= dl->charidx;
952                 
953                                         /* make editverts and edges */
954                                         f1= dl->verts;
955                                         a= dl->nr;
956                                         eve= v1= 0;
957                                         
958                                         while(a--) {
959                                                 vlast= eve;
960                                                 
961                                                 eve= BLI_addfillvert(f1);
962                                                 totvert++;
963                                                 
964                                                 if(vlast==0) v1= eve;
965                                                 else {
966                                                         BLI_addfilledge(vlast, eve);
967                                                 }
968                                                 f1+=3;
969                                         }
970                                 
971                                         if(eve!=0 && v1!=0) {
972                                                 BLI_addfilledge(eve, v1);
973                                         }
974                                 }
975                         }
976                         dl= dl->next;
977                 }
978                 
979                 if(totvert && BLI_edgefill(0, 0)) { // XXX (obedit && obedit->actcol)?(obedit->actcol-1):0)) {
980
981                         /* count faces  */
982                         tot= 0;
983                         efa= fillfacebase.first;
984                         while(efa) {
985                                 tot++;
986                                 efa= efa->next;
987                         }
988
989                         if(tot) {
990                                 dlnew= MEM_callocN(sizeof(DispList), "filldisplist");
991                                 dlnew->type= DL_INDEX3;
992                                 dlnew->col= colnr;
993                                 dlnew->nr= totvert;
994                                 dlnew->parts= tot;
995
996                                 dlnew->index= MEM_mallocN(tot*3*sizeof(int), "dlindex");
997                                 dlnew->verts= MEM_mallocN(totvert*3*sizeof(float), "dlverts");
998                                 
999                                 /* vert data */
1000                                 f1= dlnew->verts;
1001                                 totvert= 0;
1002                                 eve= fillvertbase.first;
1003                                 while(eve) {
1004                                         VECCOPY(f1, eve->co);
1005                                         f1+= 3;
1006         
1007                                         /* index number */
1008                                         eve->tmp.l = totvert;
1009                                         totvert++;
1010                                         
1011                                         eve= eve->next;
1012                                 }
1013                                 
1014                                 /* index data */
1015                                 efa= fillfacebase.first;
1016                                 index= dlnew->index;
1017                                 while(efa) {
1018                                         index[0]= (intptr_t)efa->v1->tmp.l;
1019                                         index[1]= (intptr_t)efa->v2->tmp.l;
1020                                         index[2]= (intptr_t)efa->v3->tmp.l;
1021
1022                                         if(flipnormal)
1023                                                 SWAP(int, index[0], index[2]);
1024                                         
1025                                         index+= 3;
1026                                         efa= efa->next;
1027                                 }
1028                         }
1029
1030                         BLI_addhead(to, dlnew);
1031                         
1032                 }
1033                 BLI_end_edgefill();
1034
1035                 charidx++;
1036         }
1037         
1038         /* do not free polys, needed for wireframe display */
1039         
1040 }
1041
1042 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
1043 {
1044         ListBase front, back;
1045         DispList *dl, *dlnew;
1046         float *fp, *fp1;
1047         int a, dpoly;
1048         
1049         front.first= front.last= back.first= back.last= 0;
1050         
1051         dl= dispbase->first;
1052         while(dl) {
1053                 if(dl->type==DL_SURF) {
1054                         if( (dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U)==0 ) {
1055                                 if( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE) ) {
1056                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1057                                         BLI_addtail(&front, dlnew);
1058                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1059                                         dlnew->nr= dl->parts;
1060                                         dlnew->parts= 1;
1061                                         dlnew->type= DL_POLY;
1062                                         dlnew->col= dl->col;
1063                                         dlnew->charidx = dl->charidx;
1064                                         
1065                                         fp= dl->verts;
1066                                         dpoly= 3*dl->nr;
1067                                         
1068                                         a= dl->parts;
1069                                         while(a--) {
1070                                                 VECCOPY(fp1, fp);
1071                                                 fp1+= 3;
1072                                                 fp+= dpoly;
1073                                         }
1074                                 }
1075                                 if( (cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE) ) {
1076                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1077                                         BLI_addtail(&back, dlnew);
1078                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1079                                         dlnew->nr= dl->parts;
1080                                         dlnew->parts= 1;
1081                                         dlnew->type= DL_POLY;
1082                                         dlnew->col= dl->col;
1083                                         dlnew->charidx= dl->charidx;
1084                                         
1085                                         fp= dl->verts+3*(dl->nr-1);
1086                                         dpoly= 3*dl->nr;
1087                                         
1088                                         a= dl->parts;
1089                                         while(a--) {
1090                                                 VECCOPY(fp1, fp);
1091                                                 fp1+= 3;
1092                                                 fp+= dpoly;
1093                                         }
1094                                 }
1095                         }
1096                 }
1097                 dl= dl->next;
1098         }
1099
1100         filldisplist(&front, dispbase, 1);
1101         filldisplist(&back, dispbase, 0);
1102         
1103         freedisplist(&front);
1104         freedisplist(&back);
1105
1106         filldisplist(dispbase, dispbase, 0);
1107         
1108 }
1109
1110 static void curve_to_filledpoly(Curve *cu, ListBase *nurb, ListBase *dispbase)
1111 {
1112         if(cu->flag & CU_3D) return;
1113
1114         if(dispbase->first && ((DispList*) dispbase->first)->type==DL_SURF) {
1115                 bevels_to_filledpoly(cu, dispbase);
1116         }
1117         else {
1118                 filldisplist(dispbase, dispbase, 0);
1119         }
1120 }
1121
1122 /* taper rules:
1123   - only 1 curve
1124   - first point left, last point right
1125   - based on subdivided points in original curve, not on points in taper curve (still)
1126 */
1127 float calc_taper(Scene *scene, Object *taperobj, int cur, int tot)
1128 {
1129         Curve *cu;
1130         DispList *dl;
1131         
1132         if(taperobj==NULL || taperobj->type!=OB_CURVE) return 1.0;
1133         
1134         cu= taperobj->data;
1135         dl= cu->disp.first;
1136         if(dl==NULL) {
1137                 makeDispListCurveTypes(scene, taperobj, 0);
1138                 dl= cu->disp.first;
1139         }
1140         if(dl) {
1141                 float fac= ((float)cur)/(float)(tot-1);
1142                 float minx, dx, *fp;
1143                 int a;
1144                 
1145                 /* horizontal size */
1146                 minx= dl->verts[0];
1147                 dx= dl->verts[3*(dl->nr-1)] - minx;
1148                 if(dx>0.0) {
1149                 
1150                         fp= dl->verts;
1151                         for(a=0; a<dl->nr; a++, fp+=3) {
1152                                 if( (fp[0]-minx)/dx >= fac) {
1153                                         /* interpolate with prev */
1154                                         if(a>0) {
1155                                                 float fac1= (fp[-3]-minx)/dx;
1156                                                 float fac2= (fp[0]-minx)/dx;
1157                                                 if(fac1!=fac2)
1158                                                         return fp[1]*(fac1-fac)/(fac1-fac2) + fp[-2]*(fac-fac2)/(fac1-fac2);
1159                                         }
1160                                         return fp[1];
1161                                 }
1162                         }
1163                         return fp[-2];  // last y coord
1164                 }
1165         }
1166         
1167         return 1.0;
1168 }
1169
1170 void makeDispListMBall(Scene *scene, Object *ob)
1171 {
1172         if(!ob || ob->type!=OB_MBALL) return;
1173
1174         // XXX: mball stuff uses plenty of global variables
1175         //      while this is unchanged updating during render is unsafe
1176         if(G.rendering) return;
1177
1178         freedisplist(&(ob->disp));
1179
1180         if(ob->type==OB_MBALL) {
1181                 if(ob==find_basis_mball(scene, ob)) {
1182                         metaball_polygonize(scene, ob, &ob->disp);
1183                         tex_space_mball(ob);
1184
1185                         object_deform_mball(ob, &ob->disp);
1186                 }
1187         }
1188         
1189         boundbox_displist(ob);
1190 }
1191
1192 void makeDispListMBall_forRender(Scene *scene, Object *ob, ListBase *dispbase)
1193 {
1194         metaball_polygonize(scene, ob, dispbase);
1195         object_deform_mball(ob, dispbase);
1196 }
1197
1198 static ModifierData *curve_get_tesselate_point(Scene *scene, Object *ob, int forRender, int editmode)
1199 {
1200         ModifierData *md = modifiers_getVirtualModifierList(ob);
1201         ModifierData *preTesselatePoint;
1202         int required_mode;
1203
1204         if(forRender) required_mode = eModifierMode_Render;
1205         else required_mode = eModifierMode_Realtime;
1206
1207         if(editmode) required_mode |= eModifierMode_Editmode;
1208
1209         preTesselatePoint = NULL;
1210         for (; md; md=md->next) {
1211                 if (!modifier_isEnabled(scene, md, required_mode)) continue;
1212
1213                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
1214                         preTesselatePoint  = md;
1215                 }
1216         }
1217
1218         return preTesselatePoint;
1219 }
1220
1221 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, int forRender, float (**originalVerts_r)[3], float (**deformedVerts_r)[3], int *numVerts_r)
1222 {
1223         ModifierData *md = modifiers_getVirtualModifierList(ob);
1224         ModifierData *preTesselatePoint;
1225         Curve *cu= ob->data;
1226         ListBase *nurb= BKE_curve_nurbs(cu);
1227         int numVerts = 0;
1228         int editmode = (!forRender && cu->editnurb);
1229         float (*originalVerts)[3] = NULL;
1230         float (*deformedVerts)[3] = NULL;
1231         float *keyVerts= NULL;
1232         int required_mode;
1233
1234         if(forRender) required_mode = eModifierMode_Render;
1235         else required_mode = eModifierMode_Realtime;
1236
1237         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1238         
1239         if(editmode) required_mode |= eModifierMode_Editmode;
1240
1241         if(cu->editnurb==NULL) {
1242                 keyVerts= do_ob_key(scene, ob);
1243
1244                 if(keyVerts) {
1245                         /* split coords from key data, the latter also includes
1246                            tilts, which is passed through in the modifier stack.
1247                            this is also the reason curves do not use a virtual
1248                            shape key modifier yet. */
1249                         deformedVerts= curve_getKeyVertexCos(cu, nurb, keyVerts);
1250                         originalVerts= MEM_dupallocN(deformedVerts);
1251                 }
1252         }
1253         
1254         if (preTesselatePoint) {
1255                 for (; md; md=md->next) {
1256                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1257
1258                         md->scene= scene;
1259                         
1260                         if ((md->mode & required_mode) != required_mode) continue;
1261                         if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1262                         if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1263
1264                         if (!deformedVerts) {
1265                                 deformedVerts = curve_getVertexCos(cu, nurb, &numVerts);
1266                                 originalVerts = MEM_dupallocN(deformedVerts);
1267                         }
1268
1269                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, forRender, editmode);
1270
1271                         if (md==preTesselatePoint)
1272                                 break;
1273                 }
1274         }
1275
1276         if (deformedVerts)
1277                 curve_applyVertexCos(cu, nurb, deformedVerts);
1278         if (keyVerts) /* these are not passed through modifier stack */
1279                 curve_applyKeyVertexTilts(cu, nurb, keyVerts);
1280
1281         if(keyVerts)
1282                 MEM_freeN(keyVerts);
1283
1284         *originalVerts_r = originalVerts;
1285         *deformedVerts_r = deformedVerts;
1286         *numVerts_r = numVerts;
1287 }
1288
1289 static float (*displist_get_allverts (ListBase *dispbase, int *totvert))[3]
1290 {
1291         DispList *dl;
1292         float (*allverts)[3], *fp;
1293
1294         *totvert= 0;
1295
1296         for (dl=dispbase->first; dl; dl=dl->next)
1297                 *totvert+= (dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr;
1298
1299         allverts= MEM_mallocN((*totvert)*sizeof(float)*3, "displist_get_allverts allverts");
1300         fp= (float*)allverts;
1301         for (dl=dispbase->first; dl; dl=dl->next) {
1302                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1303                 memcpy(fp, dl->verts, sizeof(float) * offs);
1304                 fp+= offs;
1305         }
1306
1307         return allverts;
1308 }
1309
1310 static void displist_apply_allverts(ListBase *dispbase, float (*allverts)[3])
1311 {
1312         DispList *dl;
1313         float *fp;
1314
1315         fp= (float*)allverts;
1316         for (dl=dispbase->first; dl; dl=dl->next) {
1317                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1318                 memcpy(dl->verts, fp, sizeof(float) * offs);
1319                 fp+= offs;
1320         }
1321 }
1322
1323 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispbase,
1324         DerivedMesh **derivedFinal, int forRender, float (*originalVerts)[3], float (*deformedVerts)[3])
1325 {
1326         ModifierData *md = modifiers_getVirtualModifierList(ob);
1327         ModifierData *preTesselatePoint;
1328         Curve *cu= ob->data;
1329         ListBase *nurb= BKE_curve_nurbs(cu);
1330         int required_mode = 0, totvert = 0;
1331         int editmode = (!forRender && cu->editnurb);
1332         DerivedMesh *dm= NULL, *ndm;
1333         float (*vertCos)[3] = NULL;
1334
1335         if(forRender) required_mode = eModifierMode_Render;
1336         else required_mode = eModifierMode_Realtime;
1337
1338         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1339         
1340         if(editmode) required_mode |= eModifierMode_Editmode;
1341
1342         if (preTesselatePoint) {
1343                 md = preTesselatePoint->next;
1344         }
1345
1346         if (derivedFinal && *derivedFinal) {
1347                 (*derivedFinal)->release (*derivedFinal);
1348         }
1349
1350         for (; md; md=md->next) {
1351                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1352
1353                 md->scene= scene;
1354
1355                 if ((md->mode & required_mode) != required_mode) continue;
1356                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1357
1358                 if (mti->type == eModifierTypeType_OnlyDeform ||
1359                                 (mti->type == eModifierTypeType_DeformOrConstruct && !dm)) {
1360                         if (dm) {
1361                                 if (!vertCos) {
1362                                         totvert = dm->getNumVerts(dm);
1363                                         vertCos = MEM_mallocN(sizeof(*vertCos) * totvert, "dfmv");
1364                                         dm->getVertCos(dm, vertCos);
1365                                 }
1366
1367                                 mti->deformVerts(md, ob, dm, vertCos, totvert, forRender, editmode);
1368                         } else {
1369                                 if (!vertCos) {
1370                                         vertCos= displist_get_allverts(dispbase, &totvert);
1371                                 }
1372
1373                                 mti->deformVerts(md, ob, NULL, vertCos, totvert, forRender, editmode);
1374                         }
1375                 } else {
1376                         if (!derivedFinal) {
1377                                 /* makeDisplistCurveTypes could be used for beveling, where derived mesh */
1378                                 /* is totally unnecessary, so we could stop modifiers applying */
1379                                 /* when we found constructive modifier but derived mesh is unwanted result */
1380                                 break;
1381                         }
1382
1383                         if (dm) {
1384                                 if (vertCos) {
1385                                         DerivedMesh *tdm = CDDM_copy(dm, 0);
1386                                         dm->release(dm);
1387                                         dm = tdm;
1388
1389                                         CDDM_apply_vert_coords(dm, vertCos);
1390                                         CDDM_calc_normals(dm);
1391                                 }
1392                         } else {
1393                                 if (vertCos) {
1394                                         displist_apply_allverts(dispbase, vertCos);
1395                                 }
1396
1397                                 if (ELEM(ob->type, OB_CURVE, OB_FONT) && (cu->flag & CU_DEFORM_FILL)) {
1398                                         curve_to_filledpoly(cu, nurb, dispbase);
1399                                 }
1400
1401                                 dm= CDDM_from_curve_customDB(ob, dispbase);
1402
1403                                 CDDM_calc_normals(dm);
1404                         }
1405
1406                         if (vertCos) {
1407                                 /* Vertex coordinates were applied to necessary data, could free it */
1408                                 MEM_freeN(vertCos);
1409                                 vertCos= NULL;
1410                         }
1411
1412                         ndm = mti->applyModifier(md, ob, dm, forRender, editmode);
1413
1414                         if (ndm) {
1415                                 /* Modifier returned a new derived mesh */
1416
1417                                 if (dm && dm != ndm) /* Modifier  */
1418                                         dm->release (dm);
1419                                 dm = ndm;
1420                         }
1421                 }
1422         }
1423
1424         if (vertCos) {
1425                 if (dm) {
1426                         DerivedMesh *tdm = CDDM_copy(dm, 0);
1427                         dm->release(dm);
1428                         dm = tdm;
1429
1430                         CDDM_apply_vert_coords(dm, vertCos);
1431                         CDDM_calc_normals(dm);
1432                         MEM_freeN(vertCos);
1433                 } else {
1434                         displist_apply_allverts(dispbase, vertCos);
1435                         MEM_freeN(vertCos);
1436                         vertCos= NULL;
1437                 }
1438         }
1439
1440         if (derivedFinal) {
1441                 (*derivedFinal) = dm;
1442         }
1443
1444         if (deformedVerts) {
1445                 curve_applyVertexCos(ob->data, nurb, originalVerts);
1446                 MEM_freeN(originalVerts);
1447                 MEM_freeN(deformedVerts);
1448         }
1449 }
1450
1451 static void displist_surf_indices(DispList *dl)
1452 {
1453         int a, b, p1, p2, p3, p4;
1454         int *index;
1455         
1456         dl->totindex= 0;
1457         
1458         index=dl->index= MEM_mallocN( 4*sizeof(int)*(dl->parts+1)*(dl->nr+1), "index array nurbs");
1459         
1460         for(a=0; a<dl->parts; a++) {
1461                 
1462                 if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
1463                         break;
1464                 
1465                 for(; b<dl->nr; b++, index+=4) {        
1466                         index[0]= p1;
1467                         index[1]= p2;
1468                         index[2]= p4;
1469                         index[3]= p3;
1470                         
1471                         dl->totindex++;
1472                         
1473                         p2= p1; p1++;
1474                         p4= p3; p3++;
1475
1476                 }
1477         }
1478         
1479 }
1480
1481 static DerivedMesh *create_orco_dm(Scene *scene, Object *ob)
1482 {
1483         DerivedMesh *dm;
1484         ListBase disp= {NULL, NULL};
1485
1486         /* OrcoDM should be created from underformed disp lists */
1487         makeDispListCurveTypes_forOrco(scene, ob, &disp);
1488         dm= CDDM_from_curve_customDB(ob, &disp);
1489
1490         freedisplist(&disp);
1491
1492         return dm;
1493 }
1494
1495 static void add_orco_dm(Scene *scene, Object *ob, DerivedMesh *dm, DerivedMesh *orcodm)
1496 {
1497         float (*orco)[3], (*layerorco)[3];
1498         int totvert, a;
1499         Curve *cu= ob->data;
1500
1501         totvert= dm->getNumVerts(dm);
1502
1503         if(orcodm) {
1504                 orco= MEM_callocN(sizeof(float)*3*totvert, "dm orco");
1505
1506                 if(orcodm->getNumVerts(orcodm) == totvert)
1507                         orcodm->getVertCos(orcodm, orco);
1508                 else
1509                         dm->getVertCos(dm, orco);
1510         }
1511         else {
1512                 orco= (float(*)[3])make_orco_curve(scene, ob);
1513         }
1514
1515         for(a=0; a<totvert; a++) {
1516                 float *co = orco[a];
1517                 co[0] = (co[0]-cu->loc[0])/cu->size[0];
1518                 co[1] = (co[1]-cu->loc[1])/cu->size[1];
1519                 co[2] = (co[2]-cu->loc[2])/cu->size[2];
1520         }
1521
1522         if((layerorco = DM_get_vert_data_layer(dm, CD_ORCO))) {
1523                 memcpy(layerorco, orco, sizeof(float)*totvert);
1524                 MEM_freeN(orco);
1525         }
1526         else
1527                 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, orco);
1528 }
1529
1530 static void curve_calc_orcodm(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender)
1531 {
1532         /* this function represents logic of mesh's orcodm calculation */
1533         /* for displist-based objects */
1534
1535         ModifierData *md = modifiers_getVirtualModifierList(ob);
1536         ModifierData *preTesselatePoint;
1537         Curve *cu= ob->data;
1538         int required_mode;
1539         int editmode = (!forRender && cu->editnurb);
1540         DerivedMesh *ndm, *orcodm= NULL;
1541
1542         if(forRender) required_mode = eModifierMode_Render;
1543         else required_mode = eModifierMode_Realtime;
1544
1545         preTesselatePoint = curve_get_tesselate_point(scene, ob, forRender, editmode);
1546
1547         if(editmode) required_mode |= eModifierMode_Editmode;
1548
1549         if (preTesselatePoint) {
1550                 md = preTesselatePoint->next;
1551         }
1552
1553         for (; md; md=md->next) {
1554                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1555
1556                 md->scene= scene;
1557
1558                 if ((md->mode & required_mode) != required_mode) continue;
1559                 if (mti->isDisabled && mti->isDisabled(md, forRender)) continue;
1560                 if (mti->type!=eModifierTypeType_Constructive) continue;
1561
1562                 if(!orcodm)
1563                         orcodm= create_orco_dm(scene, ob);
1564
1565                 ndm = mti->applyModifier(md, ob, orcodm, forRender, 0);
1566
1567                 if(ndm) {
1568                         /* if the modifier returned a new dm, release the old one */
1569                         if(orcodm && orcodm != ndm) {
1570                                 orcodm->release(orcodm);
1571                         }
1572                         orcodm = ndm;
1573                 }
1574         }
1575
1576         /* add an orco layer if needed */
1577         add_orco_dm(scene, ob, derivedFinal, orcodm);
1578
1579         if(orcodm)
1580                 orcodm->release(orcodm);
1581 }
1582
1583 void makeDispListSurf(Scene *scene, Object *ob, ListBase *dispbase,
1584         DerivedMesh **derivedFinal, int forRender, int forOrco)
1585 {
1586         ListBase *nubase;
1587         Nurb *nu;
1588         Curve *cu = ob->data;
1589         DispList *dl;
1590         float *data;
1591         int len;
1592         int numVerts;
1593         float (*originalVerts)[3];
1594         float (*deformedVerts)[3];
1595
1596         if(!forRender && cu->editnurb)
1597                 nubase= ED_curve_editnurbs(cu);
1598         else
1599                 nubase= &cu->nurb;
1600
1601         if(!forOrco)
1602                 curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1603
1604         for (nu=nubase->first; nu; nu=nu->next) {
1605                 if(forRender || nu->hide==0) {
1606                         if(nu->pntsv==1) {
1607                                 len= SEGMENTSU(nu)*nu->resolu;
1608
1609                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1610                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1611
1612                                 BLI_addtail(dispbase, dl);
1613                                 dl->parts= 1;
1614                                 dl->nr= len;
1615                                 dl->col= nu->mat_nr;
1616                                 dl->charidx= nu->charidx;
1617
1618                                 /* dl->rt will be used as flag for render face and */
1619                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1620                                 dl->rt= nu->flag & ~CU_2D;
1621
1622                                 data= dl->verts;
1623                                 if(nu->flagu & CU_NURB_CYCLIC) dl->type= DL_POLY;
1624                                 else dl->type= DL_SEGM;
1625
1626                                 makeNurbcurve(nu, data, NULL, NULL, NULL, nu->resolu, 3*sizeof(float));
1627                         }
1628                         else {
1629                                 len= (nu->pntsu*nu->resolu) * (nu->pntsv*nu->resolv);
1630                                 
1631                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1632                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1633                                 BLI_addtail(dispbase, dl);
1634
1635                                 dl->col= nu->mat_nr;
1636                                 dl->charidx= nu->charidx;
1637
1638                                 /* dl->rt will be used as flag for render face and */
1639                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1640                                 dl->rt= nu->flag & ~CU_2D;
1641
1642                                 data= dl->verts;
1643                                 dl->type= DL_SURF;
1644
1645                                 dl->parts= (nu->pntsu*nu->resolu);      /* in reverse, because makeNurbfaces works that way */
1646                                 dl->nr= (nu->pntsv*nu->resolv);
1647                                 if(nu->flagv & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_U;    /* reverse too! */
1648                                 if(nu->flagu & CU_NURB_CYCLIC) dl->flag|= DL_CYCL_V;
1649
1650                                 makeNurbfaces(nu, data, 0);
1651                                 
1652                                 /* gl array drawing: using indices */
1653                                 displist_surf_indices(dl);
1654                         }
1655                 }
1656         }
1657
1658         if (!forRender) {
1659                 tex_space_curve(cu);
1660         }
1661
1662         if(!forOrco)
1663                 curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal,
1664                         forRender, originalVerts, deformedVerts);
1665 }
1666
1667 static void do_makeDispListCurveTypes(Scene *scene, Object *ob, ListBase *dispbase,
1668         DerivedMesh **derivedFinal, int forRender, int forOrco)
1669 {
1670         Curve *cu = ob->data;
1671
1672         /* we do allow duplis... this is only displist on curve level */
1673         if(!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1674
1675         if(ob->type==OB_SURF) {
1676                 makeDispListSurf(scene, ob, dispbase, derivedFinal, forRender, forOrco);
1677         }
1678         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1679                 ListBase dlbev;
1680                 ListBase *nubase;
1681                 float (*originalVerts)[3];
1682                 float (*deformedVerts)[3];
1683                 int numVerts;
1684
1685                 nubase= BKE_curve_nurbs(cu);
1686
1687                 BLI_freelistN(&(cu->bev));
1688
1689                 if(cu->path) free_path(cu->path);
1690                 cu->path= NULL;
1691
1692                 if(ob->type==OB_FONT) BKE_text_to_curve(scene, ob, 0);
1693
1694                 if(!forOrco) curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1695
1696                 makeBevelList(ob);
1697
1698                 /* If curve has no bevel will return nothing */
1699                 makebevelcurve(scene, ob, &dlbev, forRender);
1700
1701                 /* no bevel or extrude, and no width correction? */
1702                 if (!dlbev.first && cu->width==1.0f) {
1703                         curve_to_displist(cu, nubase, dispbase);
1704                 } else {
1705                         float widfac= cu->width-1.0;
1706                         BevList *bl= cu->bev.first;
1707                         Nurb *nu= nubase->first;
1708
1709                         for (; bl && nu; bl=bl->next,nu=nu->next) {
1710                                 DispList *dl;
1711                                 float *fp1, *data;
1712                                 BevPoint *bevp;
1713                                 int a,b;
1714
1715                                 if (bl->nr) { /* blank bevel lists can happen */
1716
1717                                         /* exception handling; curve without bevel or extrude, with width correction */
1718                                         if(dlbev.first==NULL) {
1719                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev");
1720                                                 dl->verts= MEM_callocN(3*sizeof(float)*bl->nr, "dlverts");
1721                                                 BLI_addtail(dispbase, dl);
1722
1723                                                 if(bl->poly!= -1) dl->type= DL_POLY;
1724                                                 else dl->type= DL_SEGM;
1725
1726                                                 if(dl->type==DL_SEGM) dl->flag = (DL_FRONT_CURVE|DL_BACK_CURVE);
1727
1728                                                 dl->parts= 1;
1729                                                 dl->nr= bl->nr;
1730                                                 dl->col= nu->mat_nr;
1731                                                 dl->charidx= nu->charidx;
1732
1733                                                 /* dl->rt will be used as flag for render face and */
1734                                                 /* CU_2D conflicts with R_NOPUNOFLIP */
1735                                                 dl->rt= nu->flag & ~CU_2D;
1736
1737                                                 a= dl->nr;
1738                                                 bevp= (BevPoint *)(bl+1);
1739                                                 data= dl->verts;
1740                                                 while(a--) {
1741                                                         data[0]= bevp->vec[0]+widfac*bevp->sina;
1742                                                         data[1]= bevp->vec[1]+widfac*bevp->cosa;
1743                                                         data[2]= bevp->vec[2];
1744                                                         bevp++;
1745                                                         data+=3;
1746                                                 }
1747                                         }
1748                                         else {
1749                                                 DispList *dlb;
1750
1751                                                 for (dlb=dlbev.first; dlb; dlb=dlb->next) {
1752         
1753                                                         /* for each part of the bevel use a separate displblock */
1754                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1755                                                         dl->verts= data= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1756                                                         BLI_addtail(dispbase, dl);
1757         
1758                                                         dl->type= DL_SURF;
1759                                                         
1760                                                         dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1761                                                         if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1762                                                         if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1763                                                         
1764                                                         dl->parts= bl->nr;
1765                                                         dl->nr= dlb->nr;
1766                                                         dl->col= nu->mat_nr;
1767                                                         dl->charidx= nu->charidx;
1768
1769                                                         /* dl->rt will be used as flag for render face and */
1770                                                         /* CU_2D conflicts with R_NOPUNOFLIP */
1771                                                         dl->rt= nu->flag & ~CU_2D;
1772
1773                                                         dl->bevelSplitFlag= MEM_callocN(sizeof(*dl->col2)*((bl->nr+0x1F)>>5), "col2");
1774                                                         bevp= (BevPoint *)(bl+1);
1775         
1776                                                         /* for each point of poly make a bevel piece */
1777                                                         bevp= (BevPoint *)(bl+1);
1778                                                         for(a=0; a<bl->nr; a++,bevp++) {
1779                                                                 float fac=1.0;
1780                                                                 if (cu->taperobj==NULL) {
1781                                                                         if ( (cu->bevobj!=NULL) || !((cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) )
1782                                                                                 fac = bevp->radius;
1783                                                                 } else {
1784                                                                         fac = calc_taper(scene, cu->taperobj, a, bl->nr);
1785                                                                 }
1786
1787                                                                 if (bevp->split_tag) {
1788                                                                         dl->bevelSplitFlag[a>>5] |= 1<<(a&0x1F);
1789                                                                 }
1790         
1791                                                                         /* rotate bevel piece and write in data */
1792                                                                 fp1= dlb->verts;
1793                                                                 for (b=0; b<dlb->nr; b++,fp1+=3,data+=3) {
1794                                                                         if(cu->flag & CU_3D) {
1795                                                                                 float vec[3];
1796         
1797                                                                                 vec[0]= fp1[1]+widfac;
1798                                                                                 vec[1]= fp1[2];
1799                                                                                 vec[2]= 0.0;
1800
1801                                                                                 mul_qt_v3(bevp->quat, vec);
1802
1803                                                                                 data[0]= bevp->vec[0] + fac*vec[0];
1804                                                                                 data[1]= bevp->vec[1] + fac*vec[1];
1805                                                                                 data[2]= bevp->vec[2] + fac*vec[2];
1806                                                                         }
1807                                                                         else {
1808                                                                                 data[0]= bevp->vec[0] + fac*(widfac+fp1[1])*bevp->sina;
1809                                                                                 data[1]= bevp->vec[1] + fac*(widfac+fp1[1])*bevp->cosa;
1810                                                                                 data[2]= bevp->vec[2] + fac*fp1[2];
1811                                                                         }
1812                                                                 }
1813                                                         }
1814                                                         
1815                                                         /* gl array drawing: using indices */
1816                                                         displist_surf_indices(dl);
1817                                                 }
1818                                         }
1819                                 }
1820
1821                         }
1822                         freedisplist(&dlbev);
1823                 }
1824
1825                 if (!(cu->flag & CU_DEFORM_FILL)) {
1826                         curve_to_filledpoly(cu, nubase, dispbase);
1827                 }
1828
1829                 if(cu->flag & CU_PATH) calc_curvepath(ob);
1830
1831                  if (!forRender) {
1832                          tex_space_curve(cu);
1833                  }
1834
1835                 if(!forOrco) curve_calc_modifiers_post(scene, ob, dispbase, derivedFinal, forRender, originalVerts, deformedVerts);
1836
1837                 if (cu->flag & CU_DEFORM_FILL && !ob->derivedFinal) {
1838                         curve_to_filledpoly(cu, nubase, dispbase);
1839                 }
1840         }
1841 }
1842
1843 void makeDispListCurveTypes(Scene *scene, Object *ob, int forOrco)
1844 {
1845         Curve *cu = ob->data;
1846         ListBase *dispbase;
1847
1848         freedisplist(&(ob->disp));
1849         dispbase= &(cu->disp);
1850         freedisplist(dispbase);
1851
1852         do_makeDispListCurveTypes(scene, ob, dispbase, &ob->derivedFinal, 0, forOrco);
1853
1854         if (ob->derivedFinal) {
1855                 DM_set_object_boundbox (ob, ob->derivedFinal);
1856         } else {
1857                 boundbox_displist (ob);
1858
1859                 /* if there is no derivedMesh, object's boundbox is unneeded */
1860                 if (ob->bb) {
1861                         MEM_freeN(ob->bb);
1862                         ob->bb= NULL;
1863                 }
1864         }
1865 }
1866
1867 void makeDispListCurveTypes_forRender(Scene *scene, Object *ob, ListBase *dispbase,
1868         DerivedMesh **derivedFinal, int forOrco)
1869 {
1870         do_makeDispListCurveTypes(scene, ob, dispbase, derivedFinal, 1, forOrco);
1871 }
1872
1873 void makeDispListCurveTypes_forOrco(struct Scene *scene, struct Object *ob, struct ListBase *dispbase)
1874 {
1875         do_makeDispListCurveTypes(scene, ob, dispbase, NULL, 1, 1);
1876 }
1877
1878 /* add Orco layer to the displist object which has got derived mesh and return orco */
1879 float *makeOrcoDispList(Scene *scene, Object *ob, DerivedMesh *derivedFinal, int forRender) {
1880         float *orco;
1881
1882         if (derivedFinal == NULL)
1883                 derivedFinal= ob->derivedFinal;
1884
1885         if (!derivedFinal->getVertDataArray(derivedFinal, CD_ORCO)) {
1886                 curve_calc_orcodm(scene, ob, derivedFinal, forRender);
1887         }
1888
1889         orco= derivedFinal->getVertDataArray(derivedFinal, CD_ORCO);
1890
1891         if(orco) {
1892                 orco= MEM_dupallocN(orco);
1893         }
1894
1895         return orco;
1896 }
1897
1898 void imagestodisplist(void)
1899 {
1900         /* removed */
1901 }
1902
1903 /* this is confusing, there's also min_max_object, appplying the obmat... */
1904 static void boundbox_displist(Object *ob)
1905 {
1906         BoundBox *bb=0;
1907         float min[3], max[3];
1908         DispList *dl;
1909         float *vert;
1910         int a, tot=0;
1911         
1912         INIT_MINMAX(min, max);
1913
1914         if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1915                 Curve *cu= ob->data;
1916                 int doit= 0;
1917
1918                 if(cu->bb==0) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
1919                 bb= cu->bb;
1920                 
1921                 dl= cu->disp.first;
1922
1923                 while (dl) {
1924                         if(dl->type==DL_INDEX3) tot= dl->nr;
1925                         else tot= dl->nr*dl->parts;
1926                         
1927                         vert= dl->verts;
1928                         for(a=0; a<tot; a++, vert+=3) {
1929                                 doit= 1;
1930                                 DO_MINMAX(vert, min, max);
1931                         }
1932
1933                         dl= dl->next;
1934                 }
1935                 
1936                 if(!doit) {
1937                         min[0] = min[1] = min[2] = -1.0f;
1938                         max[0] = max[1] = max[2] = 1.0f;
1939                 }
1940                 
1941         }
1942         
1943         if(bb) {
1944                 boundbox_set_from_min_max(bb, min, max);
1945         }
1946 }
1947