svn merge -r 22138:22364 https://svn.blender.org/svnroot/bf-blender/branches/blender...
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "IMB_imbuf_types.h"
39
40 #include "DNA_texture_types.h"
41 #include "DNA_meta_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_effect_types.h"
44 #include "DNA_listBase.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_object_force.h"
48 #include "DNA_mesh_types.h"
49 #include "DNA_meshdata_types.h"
50 #include "DNA_modifier_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_image_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_view3d_types.h"
55 #include "DNA_lattice_types.h"
56 #include "DNA_key_types.h"
57
58 #include "BLI_blenlib.h"
59 #include "BLI_arithb.h"
60 #include "BLI_editVert.h"
61 #include "BLI_edgehash.h"
62
63 #include "BKE_utildefines.h"
64 #include "BKE_global.h"
65 #include "BKE_displist.h"
66 #include "BKE_deform.h"
67 #include "BKE_DerivedMesh.h"
68 #include "BKE_object.h"
69 #include "BKE_world.h"
70 #include "BKE_mesh.h"
71 #include "BKE_effect.h"
72 #include "BKE_mball.h"
73 #include "BKE_material.h"
74 #include "BKE_curve.h"
75 #include "BKE_key.h"
76 #include "BKE_anim.h"
77 #include "BKE_screen.h"
78 #include "BKE_texture.h"
79 #include "BKE_library.h"
80 #include "BKE_font.h"
81 #include "BKE_lattice.h"
82 #include "BKE_scene.h"
83 #include "BKE_subsurf.h"
84 #include "BKE_modifier.h"
85 #include "BKE_customdata.h"
86
87 #include "RE_pipeline.h"
88 #include "RE_shader_ext.h"
89
90 #include "BLO_sys_types.h" // for intptr_t support
91
92
93 static void boundbox_displist(Object *ob);
94
95 void free_disp_elem(DispList *dl)
96 {
97         if(dl) {
98                 if(dl->verts) MEM_freeN(dl->verts);
99                 if(dl->nors) MEM_freeN(dl->nors);
100                 if(dl->index) MEM_freeN(dl->index);
101                 if(dl->col1) MEM_freeN(dl->col1);
102                 if(dl->col2) MEM_freeN(dl->col2);
103                 if(dl->bevelSplitFlag) MEM_freeN(dl->bevelSplitFlag);
104                 MEM_freeN(dl);
105         }
106 }
107
108 void freedisplist(ListBase *lb)
109 {
110         DispList *dl;
111
112         dl= lb->first;
113         while(dl) {
114                 BLI_remlink(lb, dl);
115                 free_disp_elem(dl);
116                 dl= lb->first;
117         }
118 }
119
120 DispList *find_displist_create(ListBase *lb, int type)
121 {
122         DispList *dl;
123         
124         dl= lb->first;
125         while(dl) {
126                 if(dl->type==type) return dl;
127                 dl= dl->next;
128         }
129
130         dl= MEM_callocN(sizeof(DispList), "find_disp");
131         dl->type= type;
132         BLI_addtail(lb, dl);
133
134         return dl;
135 }
136
137 DispList *find_displist(ListBase *lb, int type)
138 {
139         DispList *dl;
140         
141         dl= lb->first;
142         while(dl) {
143                 if(dl->type==type) return dl;
144                 dl= dl->next;
145         }
146
147         return 0;
148 }
149
150 int displist_has_faces(ListBase *lb)
151 {
152         DispList *dl;
153         for(dl= lb->first; dl; dl= dl->next) {
154                 if ELEM3(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)
155                         return 1;
156         }
157         return 0;
158 }
159
160 void copy_displist(ListBase *lbn, ListBase *lb)
161 {
162         DispList *dln, *dl;
163         
164         lbn->first= lbn->last= 0;
165         
166         dl= lb->first;
167         while(dl) {
168                 
169                 dln= MEM_dupallocN(dl);
170                 BLI_addtail(lbn, dln);
171                 dln->verts= MEM_dupallocN(dl->verts);
172                 dln->nors= MEM_dupallocN(dl->nors);
173                 dln->index= MEM_dupallocN(dl->index);
174                 dln->col1= MEM_dupallocN(dl->col1);
175                 dln->col2= MEM_dupallocN(dl->col2);
176                 
177                 dl= dl->next;
178         }
179 }
180
181 void addnormalsDispList(Object *ob, ListBase *lb)
182 {
183         DispList *dl = NULL;
184         float *vdata, *ndata, nor[3];
185         float *v1, *v2, *v3, *v4;
186         float *n1, *n2, *n3, *n4;
187         int a, b, p1, p2, p3, p4;
188
189
190         dl= lb->first;
191         
192         while(dl) {
193                 if(dl->type==DL_INDEX3) {
194                         if(dl->nors==NULL) {
195                                 dl->nors= MEM_callocN(sizeof(float)*3, "dlnors");
196                                 if(dl->verts[2]<0.0) dl->nors[2]= -1.0;
197                                 else dl->nors[2]= 1.0;
198                         }
199                 }
200                 else if(dl->type==DL_SURF) {
201                         if(dl->nors==NULL) {
202                                 dl->nors= MEM_callocN(sizeof(float)*3*dl->nr*dl->parts, "dlnors");
203                                 
204                                 vdata= dl->verts;
205                                 ndata= dl->nors;
206                                 
207                                 for(a=0; a<dl->parts; a++) {
208                                         
209                                         if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
210                                                 break;
211         
212                                         v1= vdata+ 3*p1; 
213                                         n1= ndata+ 3*p1;
214                                         v2= vdata+ 3*p2; 
215                                         n2= ndata+ 3*p2;
216                                         v3= vdata+ 3*p3; 
217                                         n3= ndata+ 3*p3;
218                                         v4= vdata+ 3*p4; 
219                                         n4= ndata+ 3*p4;
220                                         
221                                         for(; b<dl->nr; b++) {
222         
223                                                 CalcNormFloat4(v1, v3, v4, v2, nor);
224         
225                                                 VecAddf(n1, n1, nor);
226                                                 VecAddf(n2, n2, nor);
227                                                 VecAddf(n3, n3, nor);
228                                                 VecAddf(n4, n4, nor);
229         
230                                                 v2= v1; v1+= 3;
231                                                 v4= v3; v3+= 3;
232                                                 n2= n1; n1+= 3;
233                                                 n4= n3; n3+= 3;
234                                         }
235                                 }
236                                 a= dl->parts*dl->nr;
237                                 v1= ndata;
238                                 while(a--) {
239                                         Normalize(v1);
240                                         v1+= 3;
241                                 }
242                         }
243                 }
244                 dl= dl->next;
245         }
246 }
247
248 void count_displist(ListBase *lb, int *totvert, int *totface)
249 {
250         DispList *dl;
251         
252         dl= lb->first;
253         while(dl) {
254                 
255                 switch(dl->type) {
256                         case DL_SURF:
257                                 *totvert+= dl->nr*dl->parts;
258                                 *totface+= (dl->nr-1)*(dl->parts-1);
259                                 break;
260                         case DL_INDEX3:
261                         case DL_INDEX4:
262                                 *totvert+= dl->nr;
263                                 *totface+= dl->parts;
264                                 break;
265                         case DL_POLY:
266                         case DL_SEGM:
267                                 *totvert+= dl->nr*dl->parts;
268                 }
269                 
270                 dl= dl->next;
271         }
272 }
273
274 int surfindex_displist(DispList *dl, int a, int *b, int *p1, int *p2, int *p3, int *p4)
275 {
276         if((dl->flag & DL_CYCL_V)==0 && a==(dl->parts)-1) {
277                 return 0;
278         }
279         
280         if(dl->flag & DL_CYCL_U) {
281                 (*p1)= dl->nr*a;
282                 (*p2)= (*p1)+ dl->nr-1;
283                 (*p3)= (*p1)+ dl->nr;
284                 (*p4)= (*p2)+ dl->nr;
285                 (*b)= 0;
286         } 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 1;
300 }
301
302 /* ***************************** shade displist. note colors now are in rgb(a) order ******************** */
303
304 /* create default shade input... save cpu cycles with ugly global */
305 /* XXXX bad code warning: local ShadeInput initialize... */
306 static ShadeInput shi;
307 static void init_fastshade_shadeinput(Render *re)
308 {
309         memset(&shi, 0, sizeof(ShadeInput));
310         shi.lay= RE_GetScene(re)->lay;
311         shi.view[2]= -1.0f;
312         shi.passflag= SCE_PASS_COMBINED;
313         shi.combinedflag= -1;
314 }
315
316 static Render *fastshade_get_render(Scene *scene)
317 {
318         // XXX 2.5: this crashes combined with previewrender
319         // due to global R so disabled for now
320 #if 0
321         /* XXX ugly global still, but we can't do preview while rendering */
322         if(G.rendering==0) {
323                 
324                 Render *re= RE_GetRender("_Shade View_");
325                 if(re==NULL) {
326                         re= RE_NewRender("_Shade View_");
327                 
328                         RE_Database_Baking(re, scene, 0, 0);    /* 0= no faces */
329                 }
330                 return re;
331         }
332 #endif
333         
334         return NULL;
335 }
336
337 /* called on file reading */
338 void fastshade_free_render(void)
339 {
340         Render *re= RE_GetRender("_Shade View_");
341         
342         if(re) {
343                 RE_Database_Free(re);
344                 RE_FreeRender(re);
345         }
346 }
347
348 static int fastshade_customdata_layer_num(int n, int active)
349 {   
350         /* make the active layer the first */
351         if (n == active) return 0;
352         else if (n < active) return n+1;
353         else return n;
354 }
355
356 static void fastshade_customdata(CustomData *fdata, int a, int j, Material *ma)
357 {
358         CustomDataLayer *layer;
359         MTFace *mtface;
360         int index, n, needuv= ma->texco & TEXCO_UV;
361         char *vertcol;
362
363         shi.totuv= 0;
364         shi.totcol= 0;
365
366         for(index=0; index<fdata->totlayer; index++) {
367                 layer= &fdata->layers[index];
368                 
369                 if(needuv && layer->type == CD_MTFACE && shi.totuv < MAX_MTFACE) {
370                         n= fastshade_customdata_layer_num(shi.totuv, layer->active_rnd);
371                         mtface= &((MTFace*)layer->data)[a];
372
373                         shi.uv[shi.totuv].uv[0]= 2.0f*mtface->uv[j][0]-1.0f;
374                         shi.uv[shi.totuv].uv[1]= 2.0f*mtface->uv[j][1]-1.0f;
375                         shi.uv[shi.totuv].uv[2]= 1.0f;
376
377                         shi.uv[shi.totuv].name= layer->name;
378                         shi.totuv++;
379                 }
380                 else if(layer->type == CD_MCOL && shi.totcol < MAX_MCOL) {
381                         n= fastshade_customdata_layer_num(shi.totcol, layer->active_rnd);
382                         vertcol= (char*)&((MCol*)layer->data)[a*4 + j];
383
384                         shi.col[shi.totcol].col[0]= ((float)vertcol[3])/255.0f;
385                         shi.col[shi.totcol].col[1]= ((float)vertcol[2])/255.0f;
386                         shi.col[shi.totcol].col[2]= ((float)vertcol[1])/255.0f;
387
388                         shi.col[shi.totcol].name= layer->name;
389                         shi.totcol++;
390                 }
391         }
392
393         if(needuv && shi.totuv == 0)
394                 VECCOPY(shi.uv[0].uv, shi.lo);
395
396         if(shi.totcol)
397                 VECCOPY(shi.vcol, shi.col[0].col);
398 }
399
400 static void fastshade(float *co, float *nor, float *orco, Material *ma, char *col1, char *col2)
401 {
402         ShadeResult shr;
403         int a;
404         
405         VECCOPY(shi.co, co);
406         shi.vn[0]= -nor[0];
407         shi.vn[1]= -nor[1];
408         shi.vn[2]= -nor[2];
409         VECCOPY(shi.vno, shi.vn);
410         VECCOPY(shi.facenor, shi.vn);
411         
412         if(ma->texco) {
413                 VECCOPY(shi.lo, orco);
414                 
415                 if(ma->texco & TEXCO_GLOB) {
416                         VECCOPY(shi.gl, shi.lo);
417                 }
418                 if(ma->texco & TEXCO_WINDOW) {
419                         VECCOPY(shi.winco, shi.lo);
420                 }
421                 if(ma->texco & TEXCO_STICKY) {
422                         VECCOPY(shi.sticky, shi.lo);
423                 }
424                 if(ma->texco & TEXCO_OBJECT) {
425                         VECCOPY(shi.co, shi.lo);
426                 }
427                 if(ma->texco & TEXCO_NORM) {
428                         VECCOPY(shi.orn, shi.vn);
429                 }
430                 if(ma->texco & TEXCO_REFL) {
431                         float inp= 2.0*(shi.vn[2]);
432                         shi.ref[0]= (inp*shi.vn[0]);
433                         shi.ref[1]= (inp*shi.vn[1]);
434                         shi.ref[2]= (-1.0+inp*shi.vn[2]);
435                 }
436         }
437         
438         shi.mat= ma;    /* set each time... node shaders change it */
439         RE_shade_external(NULL, &shi, &shr);
440         
441         a= 256.0f*(shr.combined[0]);
442         col1[0]= CLAMPIS(a, 0, 255);
443         a= 256.0f*(shr.combined[1]);
444         col1[1]= CLAMPIS(a, 0, 255);
445         a= 256.0f*(shr.combined[2]);
446         col1[2]= CLAMPIS(a, 0, 255);
447         
448         if(col2) {
449                 shi.vn[0]= -shi.vn[0];
450                 shi.vn[1]= -shi.vn[1];
451                 shi.vn[2]= -shi.vn[2];
452                 
453                 shi.mat= ma;    /* set each time... node shaders change it */
454                 RE_shade_external(NULL, &shi, &shr);
455                 
456                 a= 256.0f*(shr.combined[0]);
457                 col2[0]= CLAMPIS(a, 0, 255);
458                 a= 256.0f*(shr.combined[1]);
459                 col2[1]= CLAMPIS(a, 0, 255);
460                 a= 256.0f*(shr.combined[2]);
461                 col2[2]= CLAMPIS(a, 0, 255);
462         }
463 }
464
465 static void init_fastshade_for_ob(Render *re, Object *ob, int *need_orco_r, float mat[4][4], float imat[3][3])
466 {
467         float tmat[4][4];
468         float amb[3]= {0.0f, 0.0f, 0.0f};
469         int a;
470         
471         /* initialize globals in render */
472         RE_shade_external(re, NULL, NULL);
473
474         /* initialize global here */
475         init_fastshade_shadeinput(re);
476         
477         RE_DataBase_GetView(re, tmat);
478         Mat4MulMat4(mat, ob->obmat, tmat);
479         
480         Mat4Invert(tmat, mat);
481         Mat3CpyMat4(imat, tmat);
482         if(ob->transflag & OB_NEG_SCALE) Mat3MulFloat((float *)imat, -1.0);
483         
484         if (need_orco_r) *need_orco_r= 0;
485         for(a=0; a<ob->totcol; a++) {
486                 Material *ma= give_current_material(ob, a+1);
487                 if(ma) {
488                         init_render_material(ma, 0, amb);
489
490                         if(ma->texco & TEXCO_ORCO) {
491                                 if (need_orco_r) *need_orco_r= 1;
492                         }
493                 }
494         }
495 }
496
497 static void end_fastshade_for_ob(Object *ob)
498 {
499         int a;
500         
501         for(a=0; a<ob->totcol; a++) {
502                 Material *ma= give_current_material(ob, a+1);
503                 if(ma)
504                         end_render_material(ma);
505         }
506 }
507
508
509 static void mesh_create_shadedColors(Render *re, Object *ob, int onlyForMesh, unsigned int **col1_r, unsigned int **col2_r)
510 {
511         Mesh *me= ob->data;
512         DerivedMesh *dm;
513         MVert *mvert;
514         MFace *mface;
515         unsigned int *col1, *col2;
516         float *orco, *vnors, *nors, imat[3][3], mat[4][4], vec[3];
517         int a, i, need_orco, totface, totvert;
518         CustomDataMask dataMask = CD_MASK_BAREMESH | CD_MASK_MCOL
519                                   | CD_MASK_MTFACE | CD_MASK_NORMAL;
520
521
522         init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
523
524         if(need_orco)
525                 dataMask |= CD_MASK_ORCO;
526
527         if (onlyForMesh)
528                 dm = mesh_get_derived_deform(RE_GetScene(re), ob, dataMask);
529         else
530                 dm = mesh_get_derived_final(RE_GetScene(re), ob, dataMask);
531         
532         mvert = dm->getVertArray(dm);
533         mface = dm->getFaceArray(dm);
534         nors = dm->getFaceDataArray(dm, CD_NORMAL);
535         totvert = dm->getNumVerts(dm);
536         totface = dm->getNumFaces(dm);
537         orco= dm->getVertDataArray(dm, CD_ORCO);
538
539         if (onlyForMesh) {
540                 col1 = *col1_r;
541                 col2 = NULL;
542         } else {
543                 *col1_r = col1 = MEM_mallocN(sizeof(*col1)*totface*4, "col1");
544
545                 if (col2_r && (me->flag & ME_TWOSIDED))
546                         col2 = MEM_mallocN(sizeof(*col2)*totface*4, "col2");
547                 else
548                         col2 = NULL;
549                 
550                 if (col2_r) *col2_r = col2;
551         }
552
553                 /* vertexnormals */
554         vnors= MEM_mallocN(totvert*3*sizeof(float), "vnors disp");
555         for (a=0; a<totvert; a++) {
556                 MVert *mv = &mvert[a];
557                 float *vn= &vnors[a*3];
558                 float xn= mv->no[0]; 
559                 float yn= mv->no[1]; 
560                 float zn= mv->no[2];
561                 
562                         /* transpose ! */
563                 vn[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
564                 vn[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
565                 vn[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
566                 Normalize(vn);
567         }               
568
569         for (i=0; i<totface; i++) {
570                 extern Material defmaterial;    /* material.c */
571                 MFace *mf= &mface[i];
572                 Material *ma= give_current_material(ob, mf->mat_nr+1);
573                 int j, vidx[4], nverts= mf->v4?4:3;
574                 unsigned char *col1base= (unsigned char*) &col1[i*4];
575                 unsigned char *col2base= (unsigned char*) (col2?&col2[i*4]:NULL);
576                 float nor[3], n1[3];
577                 
578                 if(ma==NULL) ma= &defmaterial;
579                 
580                 vidx[0]= mf->v1;
581                 vidx[1]= mf->v2;
582                 vidx[2]= mf->v3;
583                 vidx[3]= mf->v4;
584
585                 if (nors) {
586                         VECCOPY(nor, &nors[i*3]);
587                 } else {
588                         if (mf->v4)
589                                 CalcNormFloat4(mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co, mvert[mf->v4].co, nor);
590                         else
591                                 CalcNormFloat(mvert[mf->v1].co, mvert[mf->v2].co, mvert[mf->v3].co, nor);
592                 }
593
594                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
595                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
596                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
597                 Normalize(n1);
598
599                 for (j=0; j<nverts; j++) {
600                         MVert *mv= &mvert[vidx[j]];
601                         char *col1= (char*)&col1base[j*4];
602                         char *col2= (char*)(col2base?&col2base[j*4]:NULL);
603                         float *vn = (mf->flag & ME_SMOOTH)?&vnors[3*vidx[j]]:n1;
604                         
605                         VECCOPY(vec, mv->co);
606                         Mat4MulVecfl(mat, vec);
607                         vec[0]+= 0.001*vn[0];
608                         vec[1]+= 0.001*vn[1];
609                         vec[2]+= 0.001*vn[2];
610
611                         fastshade_customdata(&dm->faceData, i, j, ma);
612                         fastshade(vec, vn, orco?&orco[vidx[j]*3]:mv->co, ma, col1, col2);
613                 }
614         } 
615         MEM_freeN(vnors);
616
617         dm->release(dm);
618
619         end_fastshade_for_ob(ob);
620 }
621
622 void shadeMeshMCol(Scene *scene, Object *ob, Mesh *me)
623 {
624         Render *re= fastshade_get_render(scene);
625         int a;
626         char *cp;
627         unsigned int *mcol= (unsigned int*)me->mcol;
628         
629         if(re) {
630                 mesh_create_shadedColors(re, ob, 1, &mcol, NULL);
631                 me->mcol= (MCol*)mcol;
632
633                 /* swap bytes */
634                 for(cp= (char *)me->mcol, a= 4*me->totface; a>0; a--, cp+=4) {
635                         SWAP(char, cp[0], cp[3]);
636                         SWAP(char, cp[1], cp[2]);
637                 }
638         }
639 }
640
641 /* has base pointer, to check for layer */
642 /* called from drawobject.c */
643 void shadeDispList(Scene *scene, Base *base)
644 {
645         Object *ob= base->object;
646         DispList *dl, *dlob;
647         Material *ma = NULL;
648         Curve *cu;
649         Render *re;
650         float imat[3][3], mat[4][4], vec[3];
651         float *fp, *nor, n1[3];
652         unsigned int *col1;
653         int a, need_orco;
654         
655         re= fastshade_get_render(scene);
656         if(re==NULL)
657                 return;
658         
659         dl = find_displist(&ob->disp, DL_VERTCOL);
660         if (dl) {
661                 BLI_remlink(&ob->disp, dl);
662                 free_disp_elem(dl);
663         }
664
665         if(ob->type==OB_MESH) {
666                 dl= MEM_callocN(sizeof(DispList), "displistshade");
667                 dl->type= DL_VERTCOL;
668
669                 mesh_create_shadedColors(re, ob, 0, &dl->col1, &dl->col2);
670
671                 /* add dl to ob->disp after mesh_create_shadedColors, because it
672                    might indirectly free ob->disp */
673                 BLI_addtail(&ob->disp, dl);
674         }
675         else {
676
677                 init_fastshade_for_ob(re, ob, &need_orco, mat, imat);
678                 
679                 if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
680                 
681                         /* now we need the normals */
682                         cu= ob->data;
683                         dl= cu->disp.first;
684                         
685                         while(dl) {
686                                 extern Material defmaterial;    /* material.c */
687                                 
688                                 dlob= MEM_callocN(sizeof(DispList), "displistshade");
689                                 BLI_addtail(&ob->disp, dlob);
690                                 dlob->type= DL_VERTCOL;
691                                 dlob->parts= dl->parts;
692                                 dlob->nr= dl->nr;
693                                 
694                                 if(dl->type==DL_INDEX3) {
695                                         col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
696                                 }
697                                 else {
698                                         col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->parts*dl->nr, "col1");
699                                 }
700                                 
701                         
702                                 ma= give_current_material(ob, dl->col+1);
703                                 if(ma==NULL) ma= &defmaterial;
704                                 
705                                 if(dl->type==DL_INDEX3) {
706                                         if(dl->nors) {
707                                                 /* there's just one normal */
708                                                 n1[0]= imat[0][0]*dl->nors[0]+imat[0][1]*dl->nors[1]+imat[0][2]*dl->nors[2];
709                                                 n1[1]= imat[1][0]*dl->nors[0]+imat[1][1]*dl->nors[1]+imat[1][2]*dl->nors[2];
710                                                 n1[2]= imat[2][0]*dl->nors[0]+imat[2][1]*dl->nors[1]+imat[2][2]*dl->nors[2];
711                                                 Normalize(n1);
712                                                 
713                                                 fp= dl->verts;
714                                                 
715                                                 a= dl->nr;              
716                                                 while(a--) {
717                                                         VECCOPY(vec, fp);
718                                                         Mat4MulVecfl(mat, vec);
719                                                         
720                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
721                                                         
722                                                         fp+= 3; col1++;
723                                                 }
724                                         }
725                                 }
726                                 else if(dl->type==DL_SURF) {
727                                         if(dl->nors) {
728                                                 a= dl->nr*dl->parts;
729                                                 fp= dl->verts;
730                                                 nor= dl->nors;
731                                                 
732                                                 while(a--) {
733                                                         VECCOPY(vec, fp);
734                                                         Mat4MulVecfl(mat, vec);
735                                                         
736                                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
737                                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
738                                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
739                                                         Normalize(n1);
740                                 
741                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
742                                                         
743                                                         fp+= 3; nor+= 3; col1++;
744                                                 }
745                                         }
746                                 }
747                                 dl= dl->next;
748                         }
749                 }
750                 else if(ob->type==OB_MBALL) {
751                         /* there are normals already */
752                         dl= ob->disp.first;
753                         
754                         while(dl) {
755                                 
756                                 if(dl->type==DL_INDEX4) {
757                                         if(dl->nors) {
758                                                 extern Material defmaterial;    /* material.c */
759                                                 
760                                                 if(dl->col1) MEM_freeN(dl->col1);
761                                                 col1= dl->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
762                                 
763                                                 ma= give_current_material(ob, dl->col+1);
764                                                 if(ma==NULL) ma= &defmaterial;
765                                                 
766                                                 fp= dl->verts;
767                                                 nor= dl->nors;
768                                                 
769                                                 a= dl->nr;              
770                                                 while(a--) {
771                                                         VECCOPY(vec, fp);
772                                                         Mat4MulVecfl(mat, vec);
773                                                         
774                                                         /* transpose ! */
775                                                         n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
776                                                         n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
777                                                         n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
778                                                         Normalize(n1);
779                                                 
780                                                         fastshade(vec, n1, fp, ma, (char *)col1, NULL);
781                                                         
782                                                         fp+= 3; col1++; nor+= 3;
783                                                 }
784                                         }
785                                 }
786                                 dl= dl->next;
787                         }
788                 }
789                 
790                 end_fastshade_for_ob(ob);
791         }
792 }
793
794 /* frees render and shade part of displists */
795 /* note: dont do a shade again, until a redraw happens */
796 void reshadeall_displist(Scene *scene)
797 {
798         Base *base;
799         Object *ob;
800         
801         fastshade_free_render();
802         
803         for(base= scene->base.first; base; base= base->next) {
804                 ob= base->object;
805
806                 if(ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
807                         freedisplist(&ob->disp);
808
809                 if(base->lay & scene->lay) {
810                         /* Metaballs have standard displist at the Object */
811                         if(ob->type==OB_MBALL) shadeDispList(scene, base);
812                 }
813         }
814 }
815
816 /* ****************** make displists ********************* */
817
818 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase)
819 {
820         Nurb *nu;
821         DispList *dl;
822         BezTriple *bezt, *prevbezt;
823         BPoint *bp;
824         float *data, *v1, *v2;
825         int a, len, resolu;
826         
827         nu= nubase->first;
828         while(nu) {
829                 if(nu->hide==0) {
830                         
831                         if(G.rendering && cu->resolu_ren!=0) 
832                                 resolu= cu->resolu_ren;
833                         else
834                                 resolu= nu->resolu;
835                         
836                         if(!check_valid_nurb_u(nu));
837                         else if((nu->type & 7)==CU_BEZIER) {
838                                 
839                                 /* count */
840                                 len= 0;
841                                 a= nu->pntsu-1;
842                                 if(nu->flagu & CU_CYCLIC) a++;
843
844                                 prevbezt= nu->bezt;
845                                 bezt= prevbezt+1;
846                                 while(a--) {
847                                         if(a==0 && (nu->flagu & CU_CYCLIC)) bezt= nu->bezt;
848                                         
849                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) len++;
850                                         else len+= resolu;
851                                         
852                                         if(a==0 && (nu->flagu & CU_CYCLIC)==0) len++;
853                                         
854                                         prevbezt= bezt;
855                                         bezt++;
856                                 }
857                                 
858                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbez");
859                                 /* len+1 because of 'forward_diff_bezier' function */
860                                 dl->verts= MEM_callocN( (len+1)*3*sizeof(float), "dlverts");
861                                 BLI_addtail(dispbase, dl);
862                                 dl->parts= 1;
863                                 dl->nr= len;
864                                 dl->col= nu->mat_nr;
865                                 dl->charidx= nu->charidx;
866
867                                 data= dl->verts;
868
869                                 if(nu->flagu & CU_CYCLIC) {
870                                         dl->type= DL_POLY;
871                                         a= nu->pntsu;
872                                 }
873                                 else {
874                                         dl->type= DL_SEGM;
875                                         a= nu->pntsu-1;
876                                 }
877                                 
878                                 prevbezt= nu->bezt;
879                                 bezt= prevbezt+1;
880                                 
881                                 while(a--) {
882                                         if(a==0 && dl->type== DL_POLY) bezt= nu->bezt;
883                                         
884                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
885                                                 VECCOPY(data, prevbezt->vec[1]);
886                                                 data+= 3;
887                                         }
888                                         else {
889                                                 v1= prevbezt->vec[1];
890                                                 v2= bezt->vec[0];
891                                                 forward_diff_bezier(v1[0], v1[3], v2[0], v2[3], data, resolu, 3);
892                                                 forward_diff_bezier(v1[1], v1[4], v2[1], v2[4], data+1, resolu, 3);
893                                                 forward_diff_bezier(v1[2], v1[5], v2[2], v2[5], data+2, resolu, 3);
894                                                 data+= 3*resolu;
895                                         }
896                                         
897                                         if(a==0 && dl->type==DL_SEGM) {
898                                                 VECCOPY(data, bezt->vec[1]);
899                                         }
900                                         
901                                         prevbezt= bezt;
902                                         bezt++;
903                                 }
904                         }
905                         else if((nu->type & 7)==CU_NURBS) {
906                                 len= (resolu*SEGMENTSU(nu));
907                                 
908                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
909                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
910                                 BLI_addtail(dispbase, dl);
911                                 dl->parts= 1;
912                                 
913                                 dl->nr= len;
914                                 dl->col= nu->mat_nr;
915                                 dl->charidx = nu->charidx;
916
917                                 data= dl->verts;
918                                 if(nu->flagu & CU_CYCLIC) dl->type= DL_POLY;
919                                 else dl->type= DL_SEGM;
920                                 makeNurbcurve(nu, data, NULL, NULL, resolu);
921                         }
922                         else if((nu->type & 7)==CU_POLY) {
923                                 len= nu->pntsu;
924                                 dl= MEM_callocN(sizeof(DispList), "makeDispListpoly");
925                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
926                                 BLI_addtail(dispbase, dl);
927                                 dl->parts= 1;
928                                 dl->nr= len;
929                                 dl->col= nu->mat_nr;
930                                 dl->charidx = nu->charidx;
931
932                                 data= dl->verts;
933                                 if(nu->flagu & CU_CYCLIC) dl->type= DL_POLY;
934                                 else dl->type= DL_SEGM;
935                                 
936                                 a= len;
937                                 bp= nu->bp;
938                                 while(a--) {
939                                         VECCOPY(data, bp->vec);
940                                         bp++;
941                                         data+= 3;
942                                 }
943                         }
944                 }
945                 nu= nu->next;
946         }
947 }
948
949
950 void filldisplist(ListBase *dispbase, ListBase *to)
951 {
952         EditVert *eve, *v1, *vlast;
953         EditFace *efa;
954         DispList *dlnew=0, *dl;
955         float *f1;
956         int colnr=0, charidx=0, cont=1, tot, a, *index;
957         intptr_t totvert;
958         
959         if(dispbase==0) return;
960         if(dispbase->first==0) return;
961
962         while(cont) {
963                 cont= 0;
964                 totvert=0;
965                 
966                 dl= dispbase->first;
967                 while(dl) {
968         
969                         if(dl->type==DL_POLY) {
970                                 if(charidx<dl->charidx) cont= 1;
971                                 else if(charidx==dl->charidx) {
972                         
973                                         colnr= dl->col;
974                                         charidx= dl->charidx;
975                 
976                                         /* make editverts and edges */
977                                         f1= dl->verts;
978                                         a= dl->nr;
979                                         eve= v1= 0;
980                                         
981                                         while(a--) {
982                                                 vlast= eve;
983                                                 
984                                                 eve= BLI_addfillvert(f1);
985                                                 totvert++;
986                                                 
987                                                 if(vlast==0) v1= eve;
988                                                 else {
989                                                         BLI_addfilledge(vlast, eve);
990                                                 }
991                                                 f1+=3;
992                                         }
993                                 
994                                         if(eve!=0 && v1!=0) {
995                                                 BLI_addfilledge(eve, v1);
996                                         }
997                                 }
998                         }
999                         dl= dl->next;
1000                 }
1001                 
1002                 if(totvert && BLI_edgefill(0, 0)) { // XXX (obedit && obedit->actcol)?(obedit->actcol-1):0)) {
1003
1004                         /* count faces  */
1005                         tot= 0;
1006                         efa= fillfacebase.first;
1007                         while(efa) {
1008                                 tot++;
1009                                 efa= efa->next;
1010                         }
1011
1012                         if(tot) {
1013                                 dlnew= MEM_callocN(sizeof(DispList), "filldisplist");
1014                                 dlnew->type= DL_INDEX3;
1015                                 dlnew->col= colnr;
1016                                 dlnew->nr= totvert;
1017                                 dlnew->parts= tot;
1018
1019                                 dlnew->index= MEM_mallocN(tot*3*sizeof(int), "dlindex");
1020                                 dlnew->verts= MEM_mallocN(totvert*3*sizeof(float), "dlverts");
1021                                 
1022                                 /* vert data */
1023                                 f1= dlnew->verts;
1024                                 totvert= 0;
1025                                 eve= fillvertbase.first;
1026                                 while(eve) {
1027                                         VECCOPY(f1, eve->co);
1028                                         f1+= 3;
1029         
1030                                         /* index number */
1031                                         eve->tmp.l = totvert;
1032                                         totvert++;
1033                                         
1034                                         eve= eve->next;
1035                                 }
1036                                 
1037                                 /* index data */
1038                                 efa= fillfacebase.first;
1039                                 index= dlnew->index;
1040                                 while(efa) {
1041                                         index[0]= (intptr_t)efa->v1->tmp.l;
1042                                         index[1]= (intptr_t)efa->v2->tmp.l;
1043                                         index[2]= (intptr_t)efa->v3->tmp.l;
1044                                         
1045                                         index+= 3;
1046                                         efa= efa->next;
1047                                 }
1048                         }
1049
1050                         BLI_addhead(to, dlnew);
1051                         
1052                 }
1053                 BLI_end_edgefill();
1054
1055                 charidx++;
1056         }
1057         
1058         /* do not free polys, needed for wireframe display */
1059         
1060 }
1061
1062 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
1063 {
1064         ListBase front, back;
1065         DispList *dl, *dlnew;
1066         float *fp, *fp1;
1067         int a, dpoly;
1068         
1069         front.first= front.last= back.first= back.last= 0;
1070         
1071         dl= dispbase->first;
1072         while(dl) {
1073                 if(dl->type==DL_SURF) {
1074                         if( (dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U)==0 ) {
1075                                 if( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE) ) {
1076                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1077                                         BLI_addtail(&front, 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;
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                                 if( (cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE) ) {
1096                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1097                                         BLI_addtail(&back, dlnew);
1098                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1099                                         dlnew->nr= dl->parts;
1100                                         dlnew->parts= 1;
1101                                         dlnew->type= DL_POLY;
1102                                         dlnew->col= dl->col;
1103                                         dlnew->charidx= dl->charidx;
1104                                         
1105                                         fp= dl->verts+3*(dl->nr-1);
1106                                         dpoly= 3*dl->nr;
1107                                         
1108                                         a= dl->parts;
1109                                         while(a--) {
1110                                                 VECCOPY(fp1, fp);
1111                                                 fp1+= 3;
1112                                                 fp+= dpoly;
1113                                         }
1114                                 }
1115                         }
1116                 }
1117                 dl= dl->next;
1118         }
1119
1120         filldisplist(&front, dispbase);
1121         filldisplist(&back, dispbase);
1122         
1123         freedisplist(&front);
1124         freedisplist(&back);
1125
1126         filldisplist(dispbase, dispbase);
1127         
1128 }
1129
1130 void curve_to_filledpoly(Curve *cu, ListBase *nurb, ListBase *dispbase)
1131 {
1132         if(cu->flag & CU_3D) return;
1133
1134         if(dispbase->first && ((DispList*) dispbase->first)->type==DL_SURF) {
1135                 bevels_to_filledpoly(cu, dispbase);
1136         }
1137         else {
1138                 filldisplist(dispbase, dispbase);
1139         }
1140 }
1141
1142 /* taper rules:
1143   - only 1 curve
1144   - first point left, last point right
1145   - based on subdivided points in original curve, not on points in taper curve (still)
1146 */
1147 float calc_taper(Scene *scene, Object *taperobj, int cur, int tot)
1148 {
1149         Curve *cu;
1150         DispList *dl;
1151         
1152         if(taperobj==NULL) return 1.0;
1153         
1154         cu= taperobj->data;
1155         dl= cu->disp.first;
1156         if(dl==NULL) {
1157                 makeDispListCurveTypes(scene, taperobj, 0);
1158                 dl= cu->disp.first;
1159         }
1160         if(dl) {
1161                 float fac= ((float)cur)/(float)(tot-1);
1162                 float minx, dx, *fp;
1163                 int a;
1164                 
1165                 /* horizontal size */
1166                 minx= dl->verts[0];
1167                 dx= dl->verts[3*(dl->nr-1)] - minx;
1168                 if(dx>0.0) {
1169                 
1170                         fp= dl->verts;
1171                         for(a=0; a<dl->nr; a++, fp+=3) {
1172                                 if( (fp[0]-minx)/dx >= fac) {
1173                                         /* interpolate with prev */
1174                                         if(a>0) {
1175                                                 float fac1= (fp[-3]-minx)/dx;
1176                                                 float fac2= (fp[0]-minx)/dx;
1177                                                 if(fac1!=fac2)
1178                                                         return fp[1]*(fac1-fac)/(fac1-fac2) + fp[-2]*(fac-fac2)/(fac1-fac2);
1179                                         }
1180                                         return fp[1];
1181                                 }
1182                         }
1183                         return fp[-2];  // last y coord
1184                 }
1185         }
1186         
1187         return 1.0;
1188 }
1189
1190 void makeDispListMBall(Scene *scene, Object *ob)
1191 {
1192         if(!ob || ob->type!=OB_MBALL) return;
1193
1194         freedisplist(&(ob->disp));
1195         
1196         if(ob->type==OB_MBALL) {
1197                 if(ob==find_basis_mball(scene, ob)) {
1198                         metaball_polygonize(scene, ob);
1199                         tex_space_mball(ob);
1200
1201                         object_deform_mball(ob);
1202                 }
1203         }
1204         
1205         boundbox_displist(ob);
1206 }
1207
1208 static ModifierData *curve_get_tesselate_point(Object *ob, int forRender, int editmode)
1209 {
1210         ModifierData *md = modifiers_getVirtualModifierList(ob);
1211         ModifierData *preTesselatePoint;
1212         int required_mode;
1213
1214         if(forRender) required_mode = eModifierMode_Render;
1215         else required_mode = eModifierMode_Realtime;
1216
1217         if(editmode) required_mode |= eModifierMode_Editmode;
1218
1219         preTesselatePoint = NULL;
1220         for (; md; md=md->next) {
1221                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1222
1223                 if ((md->mode & required_mode) != required_mode) continue;
1224                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1225
1226                 if (ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_MeshDeform)) {
1227                         preTesselatePoint  = md;
1228                 }
1229         }
1230
1231         return preTesselatePoint;
1232 }
1233
1234 static void curve_calc_modifiers_pre(Scene *scene, Object *ob, int forRender, float (**originalVerts_r)[3], float (**deformedVerts_r)[3], int *numVerts_r)
1235 {
1236         ModifierData *md = modifiers_getVirtualModifierList(ob);
1237         ModifierData *preTesselatePoint;
1238         Curve *cu= ob->data;
1239         ListBase *nurb= cu->editnurb?cu->editnurb:&cu->nurb;
1240         int numVerts = 0;
1241         int editmode = (!forRender && cu->editnurb);
1242         float (*originalVerts)[3] = NULL;
1243         float (*deformedVerts)[3] = NULL;
1244         int required_mode;
1245
1246         if(forRender) required_mode = eModifierMode_Render;
1247         else required_mode = eModifierMode_Realtime;
1248
1249         preTesselatePoint = curve_get_tesselate_point(ob, forRender, editmode);
1250         
1251         if(editmode) required_mode |= eModifierMode_Editmode;
1252
1253         if(cu->editnurb==NULL && do_ob_key(scene, ob)) {
1254                 deformedVerts = curve_getVertexCos(ob->data, nurb, &numVerts);
1255                 originalVerts = MEM_dupallocN(deformedVerts);
1256         }
1257         
1258         if (preTesselatePoint) {
1259                 for (; md; md=md->next) {
1260                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1261
1262                         md->scene= scene;
1263                         
1264                         if ((md->mode & required_mode) != required_mode) continue;
1265                         if (mti->isDisabled && mti->isDisabled(md)) continue;
1266                         if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1267
1268                         if (!deformedVerts) {
1269                                 deformedVerts = curve_getVertexCos(ob->data, nurb, &numVerts);
1270                                 originalVerts = MEM_dupallocN(deformedVerts);
1271                         }
1272                         
1273                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, forRender, editmode);
1274
1275                         if (md==preTesselatePoint)
1276                                 break;
1277                 }
1278         }
1279
1280         if (deformedVerts) {
1281                 curve_applyVertexCos(ob->data, nurb, deformedVerts);
1282         }
1283
1284         *originalVerts_r = originalVerts;
1285         *deformedVerts_r = deformedVerts;
1286         *numVerts_r = numVerts;
1287 }
1288
1289 static void curve_calc_modifiers_post(Scene *scene, Object *ob, ListBase *dispbase, int forRender, float (*originalVerts)[3], float (*deformedVerts)[3])
1290 {
1291         ModifierData *md = modifiers_getVirtualModifierList(ob);
1292         ModifierData *preTesselatePoint;
1293         Curve *cu= ob->data;
1294         ListBase *nurb= cu->editnurb?cu->editnurb:&cu->nurb;
1295         DispList *dl;
1296         int required_mode;
1297         int editmode = (!forRender && cu->editnurb);
1298
1299         if(forRender) required_mode = eModifierMode_Render;
1300         else required_mode = eModifierMode_Realtime;
1301
1302         preTesselatePoint = curve_get_tesselate_point(ob, forRender, editmode);
1303         
1304         if(editmode) required_mode |= eModifierMode_Editmode;
1305
1306         if (preTesselatePoint) {
1307                 md = preTesselatePoint->next;
1308         }
1309
1310         for (; md; md=md->next) {
1311                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1312                 
1313                 md->scene= scene;
1314                 
1315                 if ((md->mode & required_mode) != required_mode) continue;
1316                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1317                 if (mti->type!=eModifierTypeType_OnlyDeform && mti->type!=eModifierTypeType_DeformOrConstruct) continue;
1318
1319                 /* need to put all verts in 1 block for curve deform */
1320                 if(md->type==eModifierType_Curve) {
1321                         float *allverts, *fp;
1322                         int totvert= 0;
1323                         
1324                         for (dl=dispbase->first; dl; dl=dl->next)
1325                                 totvert+= (dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr;
1326                         
1327                         fp= allverts= MEM_mallocN(totvert*sizeof(float)*3, "temp vert");
1328                         for (dl=dispbase->first; dl; dl=dl->next) {
1329                                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1330                                 memcpy(fp, dl->verts, sizeof(float) * offs);
1331                                 fp+= offs;
1332                         }
1333                         
1334                         mti->deformVerts(md, ob, NULL, (float(*)[3]) allverts, totvert, forRender, editmode);
1335                         
1336                         fp= allverts;
1337                         for (dl=dispbase->first; dl; dl=dl->next) {
1338                                 int offs= 3 * ((dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1339                                 memcpy(dl->verts, fp, sizeof(float) * offs);
1340                                 fp+= offs;
1341                         }
1342                         MEM_freeN(allverts);
1343                 }
1344                 else {
1345                         for (dl=dispbase->first; dl; dl=dl->next) {
1346                                 mti->deformVerts(md, ob, NULL, (float(*)[3]) dl->verts, (dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr, forRender, editmode);
1347                         }
1348                 }
1349         }
1350
1351         if (deformedVerts) {
1352                 curve_applyVertexCos(ob->data, nurb, originalVerts);
1353                 MEM_freeN(originalVerts);
1354                 MEM_freeN(deformedVerts);
1355         }
1356 }
1357
1358 static void displist_surf_indices(DispList *dl)
1359 {
1360         int a, b, p1, p2, p3, p4;
1361         int *index;
1362         
1363         dl->totindex= 0;
1364         
1365         index=dl->index= MEM_mallocN( 4*sizeof(int)*(dl->parts+1)*(dl->nr+1), "index array nurbs");
1366         
1367         for(a=0; a<dl->parts; a++) {
1368                 
1369                 if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
1370                         break;
1371                 
1372                 for(; b<dl->nr; b++, index+=4) {        
1373                         index[0]= p1;
1374                         index[1]= p2;
1375                         index[2]= p4;
1376                         index[3]= p3;
1377                         
1378                         dl->totindex++;
1379                         
1380                         p2= p1; p1++;
1381                         p4= p3; p3++;
1382
1383                 }
1384         }
1385         
1386 }
1387
1388 void makeDispListSurf(Scene *scene, Object *ob, ListBase *dispbase, int forRender, int forOrco)
1389 {
1390         ListBase *nubase;
1391         Nurb *nu;
1392         Curve *cu = ob->data;
1393         DispList *dl;
1394         float *data;
1395         int len;
1396         int numVerts;
1397         float (*originalVerts)[3];
1398         float (*deformedVerts)[3];
1399                 
1400         if(!forRender && cu->editnurb)
1401                 nubase= cu->editnurb;
1402         else
1403                 nubase= &cu->nurb;
1404
1405         if(!forOrco)
1406                 curve_calc_modifiers_pre(scene, ob, forRender, &originalVerts, &deformedVerts, &numVerts);
1407
1408         for (nu=nubase->first; nu; nu=nu->next) {
1409                 if(forRender || nu->hide==0) {
1410                         if(nu->pntsv==1) {
1411                                 len= SEGMENTSU(nu)*nu->resolu;
1412                                 
1413                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1414                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1415                                 
1416                                 BLI_addtail(dispbase, dl);
1417                                 dl->parts= 1;
1418                                 dl->nr= len;
1419                                 dl->col= nu->mat_nr;
1420                                 dl->charidx= nu->charidx;
1421                                 dl->rt= nu->flag;
1422                                 
1423                                 data= dl->verts;
1424                                 if(nu->flagu & CU_CYCLIC) dl->type= DL_POLY;
1425                                 else dl->type= DL_SEGM;
1426                                 
1427                                 makeNurbcurve(nu, data, NULL, NULL, nu->resolu);
1428                         }
1429                         else {
1430                                 len= (nu->pntsu*nu->resolu) * (nu->pntsv*nu->resolv);
1431                                 
1432                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1433                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1434                                 BLI_addtail(dispbase, dl);
1435
1436                                 dl->col= nu->mat_nr;
1437                                 dl->charidx= nu->charidx;
1438                                 dl->rt= nu->flag;
1439                                 
1440                                 data= dl->verts;
1441                                 dl->type= DL_SURF;
1442                                 
1443                                 dl->parts= (nu->pntsu*nu->resolu);      /* in reverse, because makeNurbfaces works that way */
1444                                 dl->nr= (nu->pntsv*nu->resolv);
1445                                 if(nu->flagv & CU_CYCLIC) dl->flag|= DL_CYCL_U; /* reverse too! */
1446                                 if(nu->flagu & CU_CYCLIC) dl->flag|= DL_CYCL_V;
1447
1448                                 makeNurbfaces(nu, data, 0);
1449                                 
1450                                 /* gl array drawing: using indices */
1451                                 displist_surf_indices(dl);
1452                         }
1453                 }
1454         }
1455
1456         if (!forRender) {
1457                 tex_space_curve(cu);
1458         }
1459
1460         if(!forOrco)
1461                 curve_calc_modifiers_post(scene, ob, dispbase, forRender, originalVerts, deformedVerts);
1462 }
1463
1464 void makeDispListCurveTypes(Scene *scene, Object *ob, int forOrco)
1465 {
1466         Curve *cu = ob->data;
1467         ListBase *dispbase;
1468         
1469         /* we do allow duplis... this is only displist on curve level */
1470         if(!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1471
1472         freedisplist(&(ob->disp));
1473         dispbase= &(cu->disp);
1474         freedisplist(dispbase);
1475         
1476         if(ob->type==OB_SURF) {
1477                 makeDispListSurf(scene, ob, dispbase, 0, forOrco);
1478         }
1479         else if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
1480                 ListBase dlbev;
1481                 ListBase *nubase;
1482                 float (*originalVerts)[3];
1483                 float (*deformedVerts)[3];
1484                 int numVerts;
1485
1486                 if(cu->editnurb)
1487                         nubase= cu->editnurb;
1488                 else
1489                         nubase= &cu->nurb;
1490                 
1491                 BLI_freelistN(&(cu->bev));
1492                 
1493                 if(cu->path) free_path(cu->path);
1494                 cu->path= NULL;
1495                 
1496                 if(ob->type==OB_FONT) BKE_text_to_curve(scene, ob, 0);
1497                 
1498                 if(!forOrco) curve_calc_modifiers_pre(scene, ob, 0, &originalVerts, &deformedVerts, &numVerts);
1499
1500                 makeBevelList(ob);
1501
1502                 /* If curve has no bevel will return nothing */
1503                 makebevelcurve(scene, ob, &dlbev);
1504
1505                 /* no bevel or extrude, and no width correction? */
1506                 if (!dlbev.first && cu->width==1.0f) {
1507                         curve_to_displist(cu, nubase, dispbase);
1508                 } else {
1509                         float widfac= cu->width-1.0;
1510                         BevList *bl= cu->bev.first;
1511                         Nurb *nu= nubase->first;
1512
1513                         for (; bl && nu; bl=bl->next,nu=nu->next) {
1514                                 DispList *dl;
1515                                 float *fp1, *data;
1516                                 BevPoint *bevp;
1517                                 int a,b;
1518                                 
1519                                 if (bl->nr) { /* blank bevel lists can happen */
1520                                         
1521                                         /* exception handling; curve without bevel or extrude, with width correction */
1522                                         if(dlbev.first==NULL) {
1523                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev");
1524                                                 dl->verts= MEM_callocN(3*sizeof(float)*bl->nr, "dlverts");
1525                                                 BLI_addtail(dispbase, dl);
1526                                                 
1527                                                 if(bl->poly!= -1) dl->type= DL_POLY;
1528                                                 else dl->type= DL_SEGM;
1529                                                 
1530                                                 if(dl->type==DL_SEGM) dl->flag = (DL_FRONT_CURVE|DL_BACK_CURVE);
1531                                                 
1532                                                 dl->parts= 1;
1533                                                 dl->nr= bl->nr;
1534                                                 dl->col= nu->mat_nr;
1535                                                 dl->charidx= nu->charidx;
1536                                                 dl->rt= nu->flag;
1537                                                 
1538                                                 a= dl->nr;
1539                                                 bevp= (BevPoint *)(bl+1);
1540                                                 data= dl->verts;
1541                                                 while(a--) {
1542                                                         data[0]= bevp->x+widfac*bevp->sina;
1543                                                         data[1]= bevp->y+widfac*bevp->cosa;
1544                                                         data[2]= bevp->z;
1545                                                         bevp++;
1546                                                         data+=3;
1547                                                 }
1548                                         }
1549                                         else {
1550                                                 DispList *dlb;
1551                                                 
1552                                                 for (dlb=dlbev.first; dlb; dlb=dlb->next) {
1553         
1554                                                                 /* for each part of the bevel use a separate displblock */
1555                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1556                                                         dl->verts= data= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1557                                                         BLI_addtail(dispbase, dl);
1558         
1559                                                         dl->type= DL_SURF;
1560                                                         
1561                                                         dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1562                                                         if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1563                                                         if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1564                                                         
1565                                                         dl->parts= bl->nr;
1566                                                         dl->nr= dlb->nr;
1567                                                         dl->col= nu->mat_nr;
1568                                                         dl->charidx= nu->charidx;
1569                                                         dl->rt= nu->flag;
1570                                                         dl->bevelSplitFlag= MEM_callocN(sizeof(*dl->col2)*((bl->nr+0x1F)>>5), "col2");
1571                                                         bevp= (BevPoint *)(bl+1);
1572         
1573                                                                 /* for each point of poly make a bevel piece */
1574                                                         bevp= (BevPoint *)(bl+1);
1575                                                         for(a=0; a<bl->nr; a++,bevp++) {
1576                                                                 float fac=1.0;
1577                                                                 if (cu->taperobj==NULL) {
1578                                                                         if ( (cu->bevobj!=NULL) || !((cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) )
1579                                                                                 fac = bevp->radius;
1580                                                                 } else {
1581                                                                         fac = calc_taper(scene, cu->taperobj, a, bl->nr);
1582                                                                 }
1583                                                                 
1584                                                                 if (bevp->f1) {
1585                                                                         dl->bevelSplitFlag[a>>5] |= 1<<(a&0x1F);
1586                                                                 }
1587         
1588                                                                         /* rotate bevel piece and write in data */
1589                                                                 fp1= dlb->verts;
1590                                                                 for (b=0; b<dlb->nr; b++,fp1+=3,data+=3) {
1591                                                                         if(cu->flag & CU_3D) {
1592                                                                                 float vec[3];
1593         
1594                                                                                 vec[0]= fp1[1]+widfac;
1595                                                                                 vec[1]= fp1[2];
1596                                                                                 vec[2]= 0.0;
1597                                                                                 
1598                                                                                 Mat3MulVecfl(bevp->mat, vec);
1599                                                                                 
1600                                                                                 data[0]= bevp->x+ fac*vec[0];
1601                                                                                 data[1]= bevp->y+ fac*vec[1];
1602                                                                                 data[2]= bevp->z+ fac*vec[2];
1603                                                                         }
1604                                                                         else {
1605                                                                                 data[0]= bevp->x+ fac*(widfac+fp1[1])*bevp->sina;
1606                                                                                 data[1]= bevp->y+ fac*(widfac+fp1[1])*bevp->cosa;
1607                                                                                 data[2]= bevp->z+ fac*fp1[2];
1608                                                                         }
1609                                                                 }
1610                                                         }
1611                                                         
1612                                                         /* gl array drawing: using indices */
1613                                                         displist_surf_indices(dl);
1614                                                 }
1615                                         }
1616                                 }
1617
1618                         }
1619                         freedisplist(&dlbev);
1620                 }
1621
1622                 curve_to_filledpoly(cu, nubase, dispbase);
1623
1624                 if(cu->flag & CU_PATH) calc_curvepath(ob);
1625
1626                 if(!forOrco) curve_calc_modifiers_post(scene, ob, &cu->disp, 0, originalVerts, deformedVerts);
1627                 tex_space_curve(cu);
1628         }
1629         
1630         boundbox_displist(ob);
1631 }
1632
1633 void imagestodisplist(void)
1634 {
1635         /* removed */
1636 }
1637
1638 /* this is confusing, there's also min_max_object, appplying the obmat... */
1639 static void boundbox_displist(Object *ob)
1640 {
1641         BoundBox *bb=0;
1642         float min[3], max[3];
1643         DispList *dl;
1644         float *vert;
1645         int a, tot=0;
1646         
1647         INIT_MINMAX(min, max);
1648
1649         if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1650                 Curve *cu= ob->data;
1651                 int doit= 0;
1652
1653                 if(cu->bb==0) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");        
1654                 bb= cu->bb;
1655                 
1656                 dl= cu->disp.first;
1657
1658                 while (dl) {
1659                         if(dl->type==DL_INDEX3) tot= dl->nr;
1660                         else tot= dl->nr*dl->parts;
1661                         
1662                         vert= dl->verts;
1663                         for(a=0; a<tot; a++, vert+=3) {
1664                                 doit= 1;
1665                                 DO_MINMAX(vert, min, max);
1666                         }
1667
1668                         dl= dl->next;
1669                 }
1670                 
1671                 if(!doit) {
1672                         min[0] = min[1] = min[2] = -1.0f;
1673                         max[0] = max[1] = max[2] = 1.0f;
1674                 }
1675                 
1676         }
1677         
1678         if(bb) {
1679                 boundbox_set_from_min_max(bb, min, max);
1680         }
1681 }
1682