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