Saturday morning first cup of coffee hack (yeah, its a late
[blender.git] / source / blender / blenkernel / intern / displist.c
1
2 /*  displist.c
3  * 
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version. The Blender
13  * Foundation also sells licenses for use in proprietary software under
14  * the Blender License.  See http://www.blender.org/BL/ for information
15  * about this.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
27  * All rights reserved.
28  *
29  * The Original Code is: all of this file.
30  *
31  * Contributor(s): none yet.
32  *
33  * ***** END GPL/BL DUAL LICENSE BLOCK *****
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include <math.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #include "MEM_guardedalloc.h"
45
46 #include "IMB_imbuf_types.h"
47
48 #include "DNA_texture_types.h"
49 #include "DNA_meta_types.h"
50 #include "DNA_curve_types.h"
51 #include "DNA_effect_types.h"
52 #include "DNA_listBase.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_object_force.h"
56 #include "DNA_mesh_types.h"
57 #include "DNA_meshdata_types.h"
58 #include "DNA_modifier_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_image_types.h"
61 #include "DNA_material_types.h"
62 #include "DNA_view3d_types.h"
63 #include "DNA_lattice_types.h"
64 #include "DNA_key_types.h"
65
66 #include "BLI_blenlib.h"
67 #include "BLI_arithb.h"
68 #include "BLI_editVert.h"
69 #include "BLI_edgehash.h"
70
71 #include "BKE_bad_level_calls.h"
72 #include "BKE_utildefines.h"
73 #include "BKE_global.h"
74 #include "BKE_displist.h"
75 #include "BKE_deform.h"
76 #include "BKE_DerivedMesh.h"
77 #include "BKE_object.h"
78 #include "BKE_world.h"
79 #include "BKE_mesh.h"
80 #include "BKE_effect.h"
81 #include "BKE_mball.h"
82 #include "BKE_material.h"
83 #include "BKE_curve.h"
84 #include "BKE_key.h"
85 #include "BKE_anim.h"
86 #include "BKE_screen.h"
87 #include "BKE_texture.h"
88 #include "BKE_library.h"
89 #include "BKE_font.h"
90 #include "BKE_lattice.h"
91 #include "BKE_scene.h"
92 #include "BKE_subsurf.h"
93 #include "BKE_modifier.h"
94
95 #include "nla.h" /* For __NLA: Please do not remove yet */
96 #include "render.h"
97
98
99 /***/
100
101 typedef struct _FastLamp FastLamp;
102 struct _FastLamp {
103         FastLamp *next;
104         
105         short type, mode, lay, rt;
106         float co[3];
107         float vec[3];
108         float dist, distkw, att1, att2, spotsi, spotbl, r, g, b;
109 };
110
111 /***/
112
113 static void boundbox_displist(Object *ob);
114
115 static FastLamp *fastlamplist= NULL;
116 static float fviewmat[4][4];
117
118 void displistmesh_free(DispListMesh *dlm) 
119 {
120         // also check on mvert and mface, can be NULL after decimator (ton)
121         if (!dlm->dontFreeVerts && dlm->mvert) MEM_freeN(dlm->mvert);
122         if (!dlm->dontFreeNors && dlm->nors) MEM_freeN(dlm->nors);
123         if (!dlm->dontFreeOther) {
124                 if (dlm->medge) MEM_freeN(dlm->medge);
125                 if (dlm->mface) MEM_freeN(dlm->mface);
126                 if (dlm->mcol) MEM_freeN(dlm->mcol);
127                 if (dlm->tface) MEM_freeN(dlm->tface);
128         }
129         MEM_freeN(dlm);
130 }
131
132 DispListMesh *displistmesh_copy(DispListMesh *odlm) 
133 {
134         DispListMesh *ndlm= MEM_dupallocN(odlm);
135         ndlm->mvert= MEM_dupallocN(odlm->mvert);
136         if (odlm->medge) ndlm->medge= MEM_dupallocN(odlm->medge);
137         ndlm->mface= MEM_dupallocN(odlm->mface);
138         if (odlm->nors) ndlm->nors = MEM_dupallocN(odlm->nors);
139         if (odlm->mcol) ndlm->mcol= MEM_dupallocN(odlm->mcol);
140         if (odlm->tface) ndlm->tface= MEM_dupallocN(odlm->tface);
141
142         return ndlm;
143 }
144
145 DispListMesh *displistmesh_copyShared(DispListMesh *odlm) 
146 {
147         DispListMesh *ndlm= MEM_dupallocN(odlm);
148         ndlm->dontFreeNors = ndlm->dontFreeOther = ndlm->dontFreeVerts = 1;
149         
150         return ndlm;
151 }
152
153 void displistmesh_to_mesh(DispListMesh *dlm, Mesh *me) 
154 {
155                 /* We assume, rather courageously, that any
156                  * shared data came from the mesh itself and so
157                  * we can ignore the dlm->dontFreeOther flag.
158                  */
159
160         if (me->mvert && dlm->mvert!=me->mvert) MEM_freeN(me->mvert);
161         if (me->mface && dlm->mface!=me->mface) MEM_freeN(me->mface);
162         if (me->tface && dlm->tface!=me->tface) MEM_freeN(me->tface);
163         if (me->mcol && dlm->mcol!=me->mcol) MEM_freeN(me->mcol);
164         if (me->medge && dlm->medge!=me->medge) MEM_freeN(me->medge);
165
166         me->tface = NULL;
167         me->mcol = NULL;
168         me->medge = NULL;
169
170         if (dlm->totvert!=me->totvert) {
171                 if (me->msticky) MEM_freeN(me->msticky);
172                 me->msticky = NULL;
173
174                 if (me->dvert) free_dverts(me->dvert, me->totvert);
175                 me->dvert = NULL;
176
177                 if(me->key) me->key->id.us--;
178                 me->key = NULL;
179         }
180
181         me->totface= dlm->totface;
182         me->totvert= dlm->totvert;
183         me->totedge= 0;
184
185         me->mvert= dlm->mvert;
186         me->mface= dlm->mface;
187         if (dlm->tface)
188                 me->tface= dlm->tface;
189         if (dlm->mcol)
190                 me->mcol= dlm->mcol;
191
192         if(dlm->medge) {
193                 me->totedge= dlm->totedge;
194                 me->medge= dlm->medge;
195         }
196
197         if (dlm->nors && !dlm->dontFreeNors) MEM_freeN(dlm->nors);
198
199         MEM_freeN(dlm);
200 }
201
202 void free_disp_elem(DispList *dl)
203 {
204         if(dl) {
205                 if(dl->verts) MEM_freeN(dl->verts);
206                 if(dl->nors) MEM_freeN(dl->nors);
207                 if(dl->index) MEM_freeN(dl->index);
208                 if(dl->col1) MEM_freeN(dl->col1);
209                 if(dl->col2) MEM_freeN(dl->col2);
210                 if(dl->bevelSplitFlag) MEM_freeN(dl->bevelSplitFlag);
211                 MEM_freeN(dl);
212         }
213 }
214
215 void freedisplist(ListBase *lb)
216 {
217         DispList *dl;
218
219         dl= lb->first;
220         while(dl) {
221                 BLI_remlink(lb, dl);
222                 free_disp_elem(dl);
223                 dl= lb->first;
224         }
225 }
226
227 DispList *find_displist_create(ListBase *lb, int type)
228 {
229         DispList *dl;
230         
231         dl= lb->first;
232         while(dl) {
233                 if(dl->type==type) return dl;
234                 dl= dl->next;
235         }
236
237         dl= MEM_callocN(sizeof(DispList), "find_disp");
238         dl->type= type;
239         BLI_addtail(lb, dl);
240
241         return dl;
242 }
243
244 DispList *find_displist(ListBase *lb, int type)
245 {
246         DispList *dl;
247         
248         dl= lb->first;
249         while(dl) {
250                 if(dl->type==type) return dl;
251                 dl= dl->next;
252         }
253
254         return 0;
255 }
256
257 int displist_has_faces(ListBase *lb)
258 {
259         DispList *dl;
260         
261         dl= lb->first;
262         while(dl) {
263                 if ELEM4(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF, DL_POLY)
264                         return 1;
265                 dl= dl->next;
266         }
267         return 0;
268 }
269
270 void copy_displist(ListBase *lbn, ListBase *lb)
271 {
272         DispList *dln, *dl;
273         
274         lbn->first= lbn->last= 0;
275         
276         dl= lb->first;
277         while(dl) {
278                 
279                 dln= MEM_dupallocN(dl);
280                 BLI_addtail(lbn, dln);
281                 dln->verts= MEM_dupallocN(dl->verts);
282                 dln->nors= MEM_dupallocN(dl->nors);
283                 dln->index= MEM_dupallocN(dl->index);
284                 dln->col1= MEM_dupallocN(dl->col1);
285                 dln->col2= MEM_dupallocN(dl->col2);
286                 
287                 dl= dl->next;
288         }
289 }
290
291 static void initfastshade(void)
292 {
293         Base *base;
294         Object *ob;
295         Lamp *la;
296         FastLamp *fl;
297         float mat[4][4];
298
299         init_render_world();
300         
301         if(fastlamplist) return;
302         if(G.scene->camera==0) G.scene->camera= scene_find_camera(G.scene);
303         if(G.scene->camera==0) return;
304
305         /* copied from 'roteerscene' (does that function still exist? (ton) */
306         where_is_object(G.scene->camera);
307         Mat4CpyMat4(R.viewinv, G.scene->camera->obmat);
308         Mat4Ortho(R.viewinv);
309         Mat4Invert(fviewmat, R.viewinv);
310
311         /* initrendertexture(); */
312
313         base= G.scene->base.first;
314         while(base) {
315                 ob= base->object;
316                 if( ob->type==OB_LAMP && (base->lay & G.scene->lay)) {
317                         
318                         Mat4MulMat4(mat, ob->obmat, fviewmat);
319                         
320                         la= ob->data;
321                         fl= MEM_mallocN(sizeof(FastLamp), "initfastshade2");
322
323                         fl->next= fastlamplist;
324                         fastlamplist= fl;
325
326                         fl->type= la->type;
327                         fl->mode= la->mode;
328                         fl->lay= base->lay;
329                         
330                         fl->vec[0]= mat[2][0];
331                         fl->vec[1]= mat[2][1];
332                         fl->vec[2]= mat[2][2];
333                         Normalise(fl->vec);
334                         
335                         fl->co[0]= mat[3][0];
336                         fl->co[1]= mat[3][1];
337                         fl->co[2]= mat[3][2];
338
339                         fl->dist= la->dist;
340                         fl->distkw= fl->dist*fl->dist;
341                         fl->att1= la->att1;
342                         fl->att2= la->att2;
343
344                         fl->spotsi= (float)cos( M_PI*la->spotsize/360.0 );
345                         fl->spotbl= (1.0f-fl->spotsi)*la->spotblend;
346         
347                         fl->r= la->energy*la->r;
348                         fl->g= la->energy*la->g;
349                         fl->b= la->energy*la->b;
350                 }
351                 
352                 if(base->next==0 && G.scene->set && base==G.scene->base.last) base= G.scene->set->base.first;
353                 else base= base->next;
354         }
355 }
356
357
358 void freefastshade()
359 {
360         while (fastlamplist) {
361                 FastLamp *fl= fastlamplist;
362                 fastlamplist= fl->next;
363                 
364                 MEM_freeN(fl);
365         }
366 }
367
368
369 static void fastshade(float *co, float *nor, float *orco, Material *ma, char *col1, char *col2, char *vertcol)
370 {
371         ShadeInput shi;
372         FastLamp *fl;
373         float i, t, inp, is, soft,  lv[3], lampdist, ld;
374         float diff1[3], diff2[3];
375         float isr1=0, isg1=0, isb1=0, isr=0, isg=0, isb=0;
376         int a, back;
377
378         if(ma==0) return;
379         
380         shi.mat= ma;
381         shi.vlr= NULL;  // have to do this!
382         
383         // copy all relevant material vars, note, keep this synced with render_types.h
384         memcpy(&shi.r, &shi.mat->r, 23*sizeof(float));
385         // set special cases:
386         shi.har= shi.mat->har;
387         
388         shi.osatex= 0;  // also prevents reading vlr
389         
390         VECCOPY(shi.vn, nor);
391         
392         if(ma->mode & MA_VERTEXCOLP) {
393                 if(vertcol) {
394                         shi.r= vertcol[3]/255.0;
395                         shi.g= vertcol[2]/255.0;
396                         shi.b= vertcol[1]/255.0;
397                 }
398         }
399         
400         if(ma->texco) {
401                 VECCOPY(shi.lo, orco);
402                 
403                 if(ma->texco & TEXCO_GLOB) {
404                         VECCOPY(shi.gl, shi.lo);
405                 }
406                 if(ma->texco & TEXCO_WINDOW) {
407                         VECCOPY(shi.winco, shi.lo);
408                 }
409                 if(ma->texco & TEXCO_STICKY) {
410                         VECCOPY(shi.sticky, shi.lo);
411                 }
412                 if(ma->texco & TEXCO_UV) {
413                         VECCOPY(shi.uv, shi.lo);
414                 }
415                 if(ma->texco & TEXCO_OBJECT) {
416                         VECCOPY(shi.co, shi.lo);
417                 }
418                 if(ma->texco & TEXCO_NORM) {
419                         VECCOPY(shi.orn, shi.vn);
420                 }
421                 if(ma->texco & TEXCO_REFL) {
422                         
423                         inp= 2.0*(shi.vn[2]);
424                         shi.ref[0]= (inp*shi.vn[0]);
425                         shi.ref[1]= (inp*shi.vn[1]);
426                         shi.ref[2]= (-1.0+inp*shi.vn[2]);
427                 }
428
429                 do_material_tex(&shi);
430         }
431
432         if(ma->mode & MA_SHLESS) {
433                 if(vertcol && (ma->mode & (MA_VERTEXCOL+MA_VERTEXCOLP))== MA_VERTEXCOL ) {
434                         float fac;
435                         fac= vertcol[3]*shi.r;
436                         col1[3]= fac>=1.0?255:(char)fac;
437                         fac= vertcol[2]*shi.g;
438                         col1[2]= fac>=1.0?255:(char)fac;
439                         fac= vertcol[1]*shi.b;
440                         col1[1]= fac>=1.0?255:(char)fac;
441                 }
442                 else {
443                         int fac;
444                         fac= (int) (255.0*shi.r);
445                         col1[3]= fac>255?255:(char)fac;
446                         fac= (int) (255.0*shi.g);
447                         col1[2]= fac>255?255:(char)fac;
448                         fac= (int) (255.0*shi.b);
449                         col1[1]= fac>255?255:(char)fac;
450                 }
451                 if(col2) {
452                         col2[3]= col1[3];
453                         col2[2]= col1[2];
454                         col2[1]= col1[1];
455                 }
456                 return;
457         }
458
459         if( vertcol && (ma->mode & (MA_VERTEXCOL+MA_VERTEXCOLP))== MA_VERTEXCOL ) {
460                 diff1[0]= diff2[0]= shi.r*(shi.emit+vertcol[3]/255.0);
461                 diff1[1]= diff2[1]= shi.g*(shi.emit+vertcol[2]/255.0);
462                 diff1[2]= diff2[2]= shi.b*(shi.emit+vertcol[1]/255.0);
463         }
464         else {
465                 diff1[0]= diff2[0]= shi.r*shi.emit;
466                 diff1[1]= diff2[1]= shi.g*shi.emit;
467                 diff1[2]= diff2[2]= shi.b*shi.emit;
468         }
469         
470         shi.view[0]= 0.0;
471         shi.view[1]= 0.0;
472         shi.view[2]= 1.0;
473         
474         Normalise(shi.view);
475
476         for (fl= fastlamplist; fl; fl= fl->next) {
477                 /* if(fl->mode & LA_LAYER) if((fl->lay & ma->lay)==0) continue; */
478
479                 if(fl->type==LA_SUN || fl->type==LA_HEMI) {
480                         VECCOPY(lv, fl->vec);
481                         lampdist= 1.0;
482                 }
483                 else {
484                         lv[0]= fl->co[0] - co[0];
485                         lv[1]= fl->co[1] - co[1];
486                         lv[2]= fl->co[2] - co[2];
487                         ld= sqrt(lv[0]*lv[0]+lv[1]*lv[1]+lv[2]*lv[2]);
488                         lv[0]/=ld;
489                         lv[1]/=ld;
490                         lv[2]/=ld;
491
492                         if(fl->mode & LA_QUAD) {
493                                 t= 1.0;
494                                 if(fl->att1>0.0)
495                                         t= fl->dist/(fl->dist+fl->att1*ld);
496                                 if(fl->att2>0.0)
497                                         t*= fl->distkw/(fl->distkw+fl->att2*ld*ld);
498
499                                 lampdist= t;
500                         }
501                         else {
502                                 lampdist= (fl->dist/(fl->dist+ld));
503                         }
504
505                         if(fl->mode & LA_SPHERE) {
506                                 t= fl->dist - ld;
507                                 if(t<0.0) continue;
508                                 
509                                 t/= fl->dist;
510                                 lampdist*= (t);
511                         }
512                 }
513
514                 if(fl->type==LA_SPOT) {
515                         inp= lv[0]*fl->vec[0]+lv[1]*fl->vec[1]+lv[2]*fl->vec[2];
516                         if(inp<fl->spotsi) continue;
517                         else {
518                                 t= inp-fl->spotsi;
519                                 i= 1.0;
520                                 soft= 1.0;
521                                 if(t<fl->spotbl && fl->spotbl!=0.0) {
522                                         /* soft area */
523                                         i= t/fl->spotbl;
524                                         t= i*i;
525                                         soft= (3.0*t-2.0*t*i);
526                                         inp*= soft;
527                                 }
528
529                                 lampdist*=inp;
530                         }
531                 }
532
533                 if(fl->mode & LA_NO_DIFF) is= 0.0;
534                 else {
535                         is= nor[0]*lv[0]+ nor[1]*lv[1]+ nor[2]*lv[2];
536         
537                         if(ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(nor, lv, shi.view, ma->roughness);
538                         else if(ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(nor, lv, shi.view, ma->param[0], ma->param[1]);
539                         else if(ma->diff_shader==MA_DIFF_MINNAERT) is= Minnaert_Diff(is, nor, shi.view, ma->darkness);
540                 }
541                 
542                 back= 0;
543                 if(is<0.0) {
544                         back= 1;
545                         is= -is;
546                 }
547                 inp= is*lampdist*shi.refl;
548
549                 if(back==0) {
550                         add_to_diffuse(diff1, &shi, is, inp*fl->r, inp*fl->g, inp*fl->b);
551                         //diff1[0]+= inp*fl->r;
552                         //diff1[1]+= inp*fl->g;
553                         //diff1[2]+= inp*fl->b;
554                 } else if(col2) {
555                         add_to_diffuse(diff2, &shi, is, inp*fl->r, inp*fl->g, inp*fl->b);
556                         //diff2[0]+= inp*fl->r;
557                         //diff2[1]+= inp*fl->g;
558                         //diff2[2]+= inp*fl->b;
559                 }
560                 if(shi.spec!=0.0 && (fl->mode & LA_NO_SPEC)==0) {
561                         float specfac;
562                         
563                         if(ma->spec_shader==MA_SPEC_PHONG) 
564                                 specfac= Phong_Spec(nor, lv, shi.view, shi.har);
565                         else if(ma->spec_shader==MA_SPEC_COOKTORR) 
566                                 specfac= CookTorr_Spec(nor, lv, shi.view, shi.har);
567                         else if(ma->spec_shader==MA_SPEC_BLINN) 
568                                 specfac= Blinn_Spec(nor, lv, shi.view, ma->refrac, (float)shi.har);
569                         else if(ma->spec_shader==MA_SPEC_WARDISO)
570                                 specfac= WardIso_Spec(nor, lv, shi.view, ma->rms);
571                         else 
572                                 specfac= Toon_Spec(nor, lv, shi.view, ma->param[2], ma->param[3]);
573                         
574                         if(specfac>0) {
575                                 t= specfac*shi.spec*lampdist;
576                                 if(back==0) {
577                                         if(ma->mode & MA_RAMP_SPEC) {
578                                                 float spec[3];
579                                                 do_specular_ramp(&shi, specfac, t, spec);
580                                                 isr+= t*(fl->r * spec[0]);
581                                                 isg+= t*(fl->g * spec[1]);
582                                                 isb+= t*(fl->b * spec[2]);
583                                         }
584                                         else {
585                                                 isr+= t*(fl->r * shi.specr);
586                                                 isg+= t*(fl->g * shi.specg);
587                                                 isb+= t*(fl->b * shi.specb);
588                                         }
589                                 }
590                                 else if(col2) {
591                                         if(ma->mode & MA_RAMP_SPEC) {
592                                                 float spec[3];
593                                                 do_specular_ramp(&shi, specfac, t, spec);
594                                                 isr1+= t*(fl->r * spec[0]);
595                                                 isg1+= t*(fl->g * spec[1]);
596                                                 isb1+= t*(fl->b * spec[2]);
597                                         }
598                                         else {
599                                                 isr1+= t*(fl->r * shi.specr);
600                                                 isg1+= t*(fl->g * shi.specg);
601                                                 isb1+= t*(fl->b * shi.specb);
602                                         }
603                                 }
604                         }
605                 }
606
607         }
608
609         if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(diff1, &shi);
610         if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(&isr, &isg, &isb, &shi);
611
612         a= 256*(diff1[0] + shi.ambr +isr);
613         if(a>255) col1[3]= 255; 
614         else col1[3]= a;
615         a= 256*(diff1[1] + shi.ambg +isg);
616         if(a>255) col1[2]= 255; 
617         else col1[2]= a;
618         a= 256*(diff1[2] + shi.ambb +isb);
619         if(a>255) col1[1]= 255; 
620         else col1[1]= a;
621
622         if(col2) {
623                 if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(diff2, &shi);
624                 if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(&isr1, &isg1, &isb1, &shi);
625                 
626                 a= 256*(diff2[0] + shi.ambr +isr1);
627                 if(a>255) col2[3]= 255; 
628                 else col2[3]= a;
629                 a= 256*(diff2[1] + shi.ambg +isg1);
630                 if(a>255) col2[2]= 255; 
631                 else col2[2]= a;
632                 a= 256*(diff2[2] + shi.ambb +isb1);
633                 if(a>255) col2[1]= 255; 
634                 else col2[1]= a;
635         }
636
637 }
638
639 void addnormalsDispList(Object *ob, ListBase *lb)
640 {
641         DispList *dl = NULL;
642         float *vdata, *ndata, nor[3];
643         float *v1, *v2, *v3, *v4;
644         float *n1, *n2, *n3, *n4;
645         int a, b, p1, p2, p3, p4;
646
647
648         dl= lb->first;
649         
650         while(dl) {
651                 if(dl->type==DL_INDEX3) {
652                         if(dl->nors==0) {
653                                 dl->nors= MEM_callocN(sizeof(float)*3, "dlnors");
654                                 if(dl->verts[2]<0.0) dl->nors[2]= -1.0;
655                                 else dl->nors[2]= 1.0;
656                         }
657                 }
658                 else if(dl->type==DL_SURF) {
659                         if(dl->nors==0) {
660                                 dl->nors= MEM_callocN(sizeof(float)*3*dl->nr*dl->parts, "dlnors");
661                                 
662                                 vdata= dl->verts;
663                                 ndata= dl->nors;
664                                 
665                                 for(a=0; a<dl->parts; a++) {
666         
667                                         DL_SURFINDEX(dl->flag & DL_CYCL_U, dl->flag & DL_CYCL_V, dl->nr, dl->parts);
668         
669                                         v1= vdata+ 3*p1; 
670                                         n1= ndata+ 3*p1;
671                                         v2= vdata+ 3*p2; 
672                                         n2= ndata+ 3*p2;
673                                         v3= vdata+ 3*p3; 
674                                         n3= ndata+ 3*p3;
675                                         v4= vdata+ 3*p4; 
676                                         n4= ndata+ 3*p4;
677                                         
678                                         for(; b<dl->nr; b++) {
679         
680                                                 CalcNormFloat4(v1, v3, v4, v2, nor);
681         
682                                                 VecAddf(n1, n1, nor);
683                                                 VecAddf(n2, n2, nor);
684                                                 VecAddf(n3, n3, nor);
685                                                 VecAddf(n4, n4, nor);
686         
687                                                 v2= v1; v1+= 3;
688                                                 v4= v3; v3+= 3;
689                                                 n2= n1; n1+= 3;
690                                                 n4= n3; n3+= 3;
691                                         }
692                                 }
693                                 a= dl->parts*dl->nr;
694                                 v1= ndata;
695                                 while(a--) {
696                                         Normalise(v1);
697                                         v1+= 3;
698                                 }
699                         }
700                 }
701                 dl= dl->next;
702         }
703 }
704
705 static void init_fastshade_for_ob(Object *ob, int *need_orco_r, float mat[4][4], float imat[3][3])
706 {
707         float tmat[4][4];
708         int a;
709
710         initfastshade();
711
712         Mat4MulMat4(mat, ob->obmat, fviewmat);
713         
714         Mat4Invert(tmat, mat);
715         Mat3CpyMat4(imat, tmat);
716         if(ob->transflag & OB_NEG_SCALE) Mat3MulFloat((float *)imat, -1.0);
717
718         if (need_orco_r) *need_orco_r= 0;
719         for(a=0; a<ob->totcol; a++) {
720                 Material *ma= give_current_material(ob, a+1);
721                 if(ma) {
722                         init_render_material(ma);
723                         if(ma->texco & TEXCO_ORCO) {
724                                 if (need_orco_r) *need_orco_r= 1;
725                         }
726                 }
727         }
728 }
729 static void end_fastshade_for_ob(Object *ob)
730 {
731         int a;
732
733         for(a=0; a<ob->totcol; a++) {
734                 Material *ma= give_current_material(ob, a+1);
735                 if(ma) end_render_material(ma);
736         }
737 }
738
739 void mesh_create_shadedColors(Object *ob, int onlyForMesh, unsigned int **col1_r, unsigned int **col2_r)
740 {
741         Mesh *me= ob->data;
742         int dmNeedsFree;
743         DerivedMesh *dm;
744         DispListMesh *dlm;
745         unsigned int *col1, *col2;
746         float *orco, *vnors, imat[3][3], mat[4][4], vec[3];
747         int a, i, need_orco;
748
749         init_fastshade_for_ob(ob, &need_orco, mat, imat);
750
751         if (need_orco) {
752                 orco = mesh_create_orco(ob);
753         } else {
754                 orco = NULL;
755         }
756
757         if (onlyForMesh) {
758                 dm = mesh_get_derived_deform(ob, &dmNeedsFree);
759         } else {
760                 dm = mesh_get_derived_final(ob, &dmNeedsFree);
761         }
762         dlm= dm->convertToDispListMesh(dm, 1);
763
764         col1 = MEM_mallocN(sizeof(*col1)*dlm->totface*4, "col1");
765         if (col2_r && (me->flag & ME_TWOSIDED)) {
766                 col2 = MEM_mallocN(sizeof(*col2)*dlm->totface*4, "col1");
767         } else {
768                 col2 = NULL;
769         }
770         
771         *col1_r = col1;
772         if (col2_r) *col2_r = col2;
773
774                 /* vertexnormals */
775         vnors= MEM_mallocN(dlm->totvert*3*sizeof(float), "vnors disp");
776         for (a=0; a<dlm->totvert; a++) {
777                 MVert *mv = &dlm->mvert[a];
778                 float *vn= &vnors[a*3];
779                 float xn= mv->no[0]; 
780                 float yn= mv->no[1]; 
781                 float zn= mv->no[2];
782                 
783                         /* transpose ! */
784                 vn[0]= imat[0][0]*xn+imat[0][1]*yn+imat[0][2]*zn;
785                 vn[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
786                 vn[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
787                 Normalise(vn);
788         }               
789
790         for (i=0; i<dlm->totface; i++) {
791                 MFace *mf= &dlm->mface[i];
792                 int j, vidx[4], nverts= mf->v4?4:3;
793                 unsigned char *col1base= (unsigned char*) &col1[i*4];
794                 unsigned char *col2base= (unsigned char*) (col2?&col2[i*4]:NULL);
795                 unsigned char *mcolbase;
796                 Material *ma= give_current_material(ob, mf->mat_nr+1);
797                 float nor[3], n1[3];
798                 
799                 if(ma==0) ma= &defmaterial;
800                 
801                 if (dlm->tface) {
802                         mcolbase = (unsigned char*) dlm->tface[i].col;
803                 } else if (dlm->mcol) {
804                         mcolbase = (unsigned char*) &dlm->mcol[i*4];
805                 } else {
806                         mcolbase = NULL;
807                 }
808
809                 vidx[0]= mf->v1;
810                 vidx[1]= mf->v2;
811                 vidx[2]= mf->v3;
812                 vidx[3]= mf->v4;
813
814                 if (dlm->nors) {
815                         VECCOPY(nor, &dlm->nors[i*3]);
816                 } else {
817                         if (mf->v4)
818                                 CalcNormFloat4(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, dlm->mvert[mf->v4].co, nor);
819                         else
820                                 CalcNormFloat(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, nor);
821                 }
822
823                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
824                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
825                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
826                 Normalise(n1);
827
828                 for (j=0; j<nverts; j++) {
829                         MVert *mv= &dlm->mvert[vidx[j]];
830                         unsigned char *col1= &col1base[j*4];
831                         unsigned char *col2= col2base?&col2base[j*4]:NULL;
832                         unsigned char *mcol= mcolbase?&mcolbase[j*4]:NULL;
833                         float *vn = (mf->flag & ME_SMOOTH)?&vnors[3*vidx[j]]:n1;
834
835                         VECCOPY(vec, mv->co);
836                         Mat4MulVecfl(mat, vec);
837                         fastshade(vec, vn, orco?&orco[vidx[j]*3]:mv->co, ma, col1, col2, mcol);
838                 }
839         }
840         MEM_freeN(vnors);
841         displistmesh_free(dlm);
842
843         if (orco) {
844                 MEM_freeN(orco);
845         }
846
847         if (dmNeedsFree) dm->release(dm);
848
849         end_fastshade_for_ob(ob);
850 }
851
852 void shadeDispList(Object *ob)
853 {
854         DispList *dl, *dlob;
855         Material *ma = NULL;
856         Curve *cu;
857         float imat[3][3], mat[4][4], vec[3];
858         float *fp, *nor, n1[3];
859         unsigned int *col1;
860         int a;
861
862         if(ob->flag & OB_FROMDUPLI) return;
863
864         dl = find_displist(&ob->disp, DL_VERTCOL);
865         if (dl) {
866                 BLI_remlink(&ob->disp, dl);
867                 free_disp_elem(dl);
868         }
869
870         if(ob->type==OB_MESH) {
871                 dl= MEM_callocN(sizeof(DispList), "displistshade");
872                 BLI_addtail(&ob->disp, dl);
873                 dl->type= DL_VERTCOL;
874
875                 mesh_create_shadedColors(ob, 0, &dl->col1, &dl->col2);
876
877                 return;
878         }
879
880         init_fastshade_for_ob(ob, NULL, mat, imat);
881         
882         if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) {
883         
884                 /* now we need the normals */
885                 cu= ob->data;
886                 dl= cu->disp.first;
887                 
888                 while(dl) {
889                         dlob= MEM_callocN(sizeof(DispList), "displistshade");
890                         BLI_addtail(&ob->disp, dlob);
891                         dlob->type= DL_VERTCOL;
892                         dlob->parts= dl->parts;
893                         dlob->nr= dl->nr;
894                         
895                         if(dl->type==DL_INDEX3) {
896                                 col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
897                         }
898                         else {
899                                 col1= dlob->col1= MEM_mallocN(sizeof(int)*dl->parts*dl->nr, "col1");
900                         }
901                         
902                 
903                         ma= give_current_material(ob, dl->col+1);
904                         if(ma==0) ma= &defmaterial;
905
906                         if(dl->type==DL_INDEX3) {
907                                 if(dl->nors) {
908                                         /* there's just one normal */
909                                         n1[0]= imat[0][0]*dl->nors[0]+imat[0][1]*dl->nors[1]+imat[0][2]*dl->nors[2];
910                                         n1[1]= imat[1][0]*dl->nors[0]+imat[1][1]*dl->nors[1]+imat[1][2]*dl->nors[2];
911                                         n1[2]= imat[2][0]*dl->nors[0]+imat[2][1]*dl->nors[1]+imat[2][2]*dl->nors[2];
912                                         Normalise(n1);
913                                         
914                                         fp= dl->verts;
915                                         
916                                         a= dl->nr;              
917                                         while(a--) {
918                                                 VECCOPY(vec, fp);
919                                                 Mat4MulVecfl(mat, vec);
920                                                 
921                                                 fastshade(vec, n1, fp, ma, (char *)col1, 0, 0);
922                                                 
923                                                 fp+= 3; col1++;
924                                         }
925                                 }
926                         }
927                         else if(dl->type==DL_SURF) {
928                                 if(dl->nors) {
929                                         a= dl->nr*dl->parts;
930                                         fp= dl->verts;
931                                         nor= dl->nors;
932                                         
933                                         while(a--) {
934                                                 VECCOPY(vec, fp);
935                                                 Mat4MulVecfl(mat, vec);
936                                                 
937                                                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
938                                                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
939                                                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
940                                                 Normalise(n1);
941                         
942                                                 fastshade(vec, n1, fp, ma, (char *)col1, 0, 0);
943                                                 
944                                                 fp+= 3; nor+= 3; col1++;
945                                         }
946                                 }
947                         }
948                         dl= dl->next;
949                 }
950         }
951         else if(ob->type==OB_MBALL) {
952                 /* there are normals already */
953                 dl= ob->disp.first;
954                 
955                 while(dl) {
956                         
957                         if(dl->type==DL_INDEX4) {
958                                 if(dl->nors) {
959                                         
960                                         if(dl->col1) MEM_freeN(dl->col1);
961                                         col1= dl->col1= MEM_mallocN(sizeof(int)*dl->nr, "col1");
962                         
963                                         ma= give_current_material(ob, dl->col+1);
964                                         if(ma==0) ma= &defmaterial;
965         
966                                         fp= dl->verts;
967                                         nor= dl->nors;
968                                         
969                                         a= dl->nr;              
970                                         while(a--) {
971                                                 VECCOPY(vec, fp);
972                                                 Mat4MulVecfl(mat, vec);
973                                                 
974                                                 /* transpose ! */
975                                                 n1[0]= imat[0][0]*nor[0]+imat[0][1]*nor[1]+imat[0][2]*nor[2];
976                                                 n1[1]= imat[1][0]*nor[0]+imat[1][1]*nor[1]+imat[1][2]*nor[2];
977                                                 n1[2]= imat[2][0]*nor[0]+imat[2][1]*nor[1]+imat[2][2]*nor[2];
978                                                 Normalise(n1);
979                                         
980                                                 fastshade(vec, n1, fp, ma, (char *)col1, 0, 0);
981                                                 
982                                                 fp+= 3; col1++; nor+= 3;
983                                         }
984                                 }
985                         }
986                         dl= dl->next;
987                 }
988         }
989         
990         end_fastshade_for_ob(ob);
991 }
992
993 void reshadeall_displist(void)
994 {
995         Base *base;
996         Object *ob;
997         
998         freefastshade();
999         
1000         base= G.scene->base.first;
1001         while(base) {
1002                 if(base->lay & G.scene->lay) {
1003                         ob= base->object;
1004                         
1005                         /* Metaballs have standard displist at the Object */
1006                         if(ob->type==OB_MBALL) shadeDispList(ob);
1007                         else freedisplist(&ob->disp);
1008                 }
1009                 base= base->next;
1010         }
1011 }
1012
1013 void count_displist(ListBase *lb, int *totvert, int *totface)
1014 {
1015         DispList *dl;
1016         
1017         dl= lb->first;
1018         while(dl) {
1019                 
1020                 switch(dl->type) {
1021                 case DL_SURF:
1022                         *totvert+= dl->nr*dl->parts;
1023                         *totface+= (dl->nr-1)*(dl->parts-1);
1024                         break;
1025                 case DL_INDEX3:
1026                 case DL_INDEX4:
1027                         *totvert+= dl->nr;
1028                         *totface+= dl->parts;
1029                         break;
1030                 case DL_POLY:
1031                 case DL_SEGM:
1032                         *totvert+= dl->nr*dl->parts;
1033                 }
1034                 
1035                 dl= dl->next;
1036         }
1037 }
1038
1039 static void curve_to_displist(Curve *cu, ListBase *nubase, ListBase *dispbase)
1040 {
1041         Nurb *nu;
1042         DispList *dl;
1043         BezTriple *bezt, *prevbezt;
1044         BPoint *bp;
1045         float *data, *v1, *v2;
1046         int a, len;
1047         
1048         nu= nubase->first;
1049         while(nu) {
1050                 if(nu->hide==0) {
1051                         if((nu->type & 7)==CU_BEZIER) {
1052                                 
1053                                 /* count */
1054                                 len= 0;
1055                                 a= nu->pntsu-1;
1056                                 if(nu->flagu & 1) a++;
1057
1058                                 prevbezt= nu->bezt;
1059                                 bezt= prevbezt+1;
1060                                 while(a--) {
1061                                         if(a==0 && (nu->flagu & 1)) bezt= nu->bezt;
1062                                         
1063                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) len++;
1064                                         else len+= nu->resolu;
1065                                         
1066                                         if(a==0 && (nu->flagu & 1)==0) len++;
1067                                         
1068                                         prevbezt= bezt;
1069                                         bezt++;
1070                                 }
1071                                 
1072                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbez");
1073                                 /* len+1 because of 'forward_diff_bezier' function */
1074                                 dl->verts= MEM_callocN( (len+1)*3*sizeof(float), "dlverts");
1075                                 BLI_addtail(dispbase, dl);
1076                                 dl->parts= 1;
1077                                 dl->nr= len;
1078                                 dl->col= nu->mat_nr;
1079                                 dl->charidx= nu->charidx;
1080
1081                                 data= dl->verts;
1082
1083                                 if(nu->flagu & 1) {
1084                                         dl->type= DL_POLY;
1085                                         a= nu->pntsu;
1086                                 }
1087                                 else {
1088                                         dl->type= DL_SEGM;
1089                                         a= nu->pntsu-1;
1090                                 }
1091                                 
1092                                 prevbezt= nu->bezt;
1093                                 bezt= prevbezt+1;
1094                                 
1095                                 while(a--) {
1096                                         if(a==0 && dl->type== DL_POLY) bezt= nu->bezt;
1097                                         
1098                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
1099                                                 VECCOPY(data, prevbezt->vec[1]);
1100                                                 data+= 3;
1101                                         }
1102                                         else {
1103                                                 v1= prevbezt->vec[1];
1104                                                 v2= bezt->vec[0];
1105                                                 forward_diff_bezier(v1[0], v1[3], v2[0], v2[3], data, nu->resolu, 3);
1106                                                 forward_diff_bezier(v1[1], v1[4], v2[1], v2[4], data+1, nu->resolu, 3);
1107                                                 if((nu->type & 8)==0)
1108                                                         forward_diff_bezier(v1[2], v1[5], v2[2], v2[5], data+2, nu->resolu, 3);
1109                                                 data+= 3*nu->resolu;
1110                                         }
1111                                         
1112                                         if(a==0 && dl->type==DL_SEGM) {
1113                                                 VECCOPY(data, bezt->vec[1]);
1114                                         }
1115                                         
1116                                         prevbezt= bezt;
1117                                         bezt++;
1118                                 }
1119                         }
1120                         else if((nu->type & 7)==CU_NURBS) {
1121                                 len= nu->pntsu*nu->resolu;
1122                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1123                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1124                                 BLI_addtail(dispbase, dl);
1125                                 dl->parts= 1;
1126                                 dl->nr= len;
1127                                 dl->col= nu->mat_nr;
1128
1129                                 data= dl->verts;
1130                                 if(nu->flagu & 1) dl->type= DL_POLY;
1131                                 else dl->type= DL_SEGM;
1132                                 makeNurbcurve(nu, data, 3);
1133                         }
1134                         else if((nu->type & 7)==CU_POLY) {
1135                                 len= nu->pntsu;
1136                                 dl= MEM_callocN(sizeof(DispList), "makeDispListpoly");
1137                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1138                                 BLI_addtail(dispbase, dl);
1139                                 dl->parts= 1;
1140                                 dl->nr= len;
1141                                 dl->col= nu->mat_nr;
1142                                 dl->charidx = nu->charidx;
1143
1144                                 data= dl->verts;
1145                                 if(nu->flagu & 1) dl->type= DL_POLY;
1146                                 else dl->type= DL_SEGM;
1147                                 
1148                                 a= len;
1149                                 bp= nu->bp;
1150                                 while(a--) {
1151                                         VECCOPY(data, bp->vec);
1152                                         bp++;
1153                                         data+= 3;
1154                                 }
1155                         }
1156                 }
1157                 nu= nu->next;
1158         }
1159 }
1160
1161
1162 void filldisplist(ListBase *dispbase, ListBase *to)
1163 {
1164         EditVert *eve, *v1, *vlast;
1165         EditFace *efa;
1166         DispList *dlnew=0, *dl;
1167         float *f1;
1168         int colnr=0, charidx=0, cont=1, tot, a, *index;
1169         long totvert;
1170         
1171         if(dispbase==0) return;
1172         if(dispbase->first==0) return;
1173
1174         while(cont) {
1175                 cont= 0;
1176                 totvert=0;
1177                 
1178                 dl= dispbase->first;
1179                 while(dl) {
1180         
1181                         if(dl->type==DL_POLY) {
1182                                 if(charidx<dl->charidx) cont= 1;
1183                                 else if(charidx==dl->charidx) {
1184                         
1185                                         colnr= dl->col;
1186                                         charidx= dl->charidx;
1187                 
1188                                         /* make editverts and edges */
1189                                         f1= dl->verts;
1190                                         a= dl->nr;
1191                                         eve= v1= 0;
1192                                         
1193                                         while(a--) {
1194                                                 vlast= eve;
1195                                                 
1196                                                 eve= BLI_addfillvert(f1);
1197                                                 totvert++;
1198                                                 
1199                                                 if(vlast==0) v1= eve;
1200                                                 else {
1201                                                         BLI_addfilledge(vlast, eve);
1202                                                 }
1203                                                 f1+=3;
1204                                         }
1205                                 
1206                                         if(eve!=0 && v1!=0) {
1207                                                 BLI_addfilledge(eve, v1);
1208                                         }
1209                                 }
1210                         }
1211                         dl= dl->next;
1212                 }
1213                 
1214                 if(totvert && BLI_edgefill(0, (G.obedit && G.obedit->actcol)?(G.obedit->actcol-1):0)) {
1215
1216                         /* count faces  */
1217                         tot= 0;
1218                         efa= fillfacebase.first;
1219                         while(efa) {
1220                                 tot++;
1221                                 efa= efa->next;
1222                         }
1223
1224                         if(tot) {
1225                                 dlnew= MEM_callocN(sizeof(DispList), "filldisplist");
1226                                 dlnew->type= DL_INDEX3;
1227                                 dlnew->col= colnr;
1228                                 dlnew->nr= totvert;
1229                                 dlnew->parts= tot;
1230
1231                                 dlnew->index= MEM_mallocN(tot*3*sizeof(int), "dlindex");
1232                                 dlnew->verts= MEM_mallocN(totvert*3*sizeof(float), "dlverts");
1233                                 
1234                                 /* vert data */
1235                                 f1= dlnew->verts;
1236                                 totvert= 0;
1237                                 eve= fillvertbase.first;
1238                                 while(eve) {
1239                                         VECCOPY(f1, eve->co);
1240                                         f1+= 3;
1241         
1242                                         /* index number */
1243                                         eve->vn= (EditVert *)totvert;
1244                                         totvert++;
1245                                         
1246                                         eve= eve->next;
1247                                 }
1248                                 
1249                                 /* index data */
1250                                 efa= fillfacebase.first;
1251                                 index= dlnew->index;
1252                                 while(efa) {
1253                                         index[0]= (long)efa->v1->vn;
1254                                         index[1]= (long)efa->v2->vn;
1255                                         index[2]= (long)efa->v3->vn;
1256                                         
1257                                         index+= 3;
1258                                         efa= efa->next;
1259                                 }
1260                         }
1261
1262                         BLI_addhead(to, dlnew);
1263                         
1264                 }
1265                 BLI_end_edgefill();
1266
1267                 charidx++;
1268         }
1269         
1270         /* do not free polys, needed for wireframe display */
1271         
1272 }
1273
1274 static void bevels_to_filledpoly(Curve *cu, ListBase *dispbase)
1275 {
1276         ListBase front, back;
1277         DispList *dl, *dlnew;
1278         float *fp, *fp1;
1279         int a, dpoly;
1280         
1281         front.first= front.last= back.first= back.last= 0;
1282         
1283         dl= dispbase->first;
1284         while(dl) {
1285                 if(dl->type==DL_SURF) {
1286                         if( (dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U)==0 ) {
1287                                 if( (cu->flag & CU_BACK) && (dl->flag & DL_BACK_CURVE) ) {
1288                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1289                                         BLI_addtail(&front, dlnew);
1290                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1291                                         dlnew->nr= dl->parts;
1292                                         dlnew->parts= 1;
1293                                         dlnew->type= DL_POLY;
1294                                         dlnew->col= dl->col;
1295                                         
1296                                         fp= dl->verts;
1297                                         dpoly= 3*dl->nr;
1298                                         
1299                                         a= dl->parts;
1300                                         while(a--) {
1301                                                 VECCOPY(fp1, fp);
1302                                                 fp1+= 3;
1303                                                 fp+= dpoly;
1304                                         }
1305                                 }
1306                                 if( (cu->flag & CU_FRONT) && (dl->flag & DL_FRONT_CURVE) ) {
1307                                         dlnew= MEM_callocN(sizeof(DispList), "filldisp");
1308                                         BLI_addtail(&back, dlnew);
1309                                         dlnew->verts= fp1= MEM_mallocN(sizeof(float)*3*dl->parts, "filldisp1");
1310                                         dlnew->nr= dl->parts;
1311                                         dlnew->parts= 1;
1312                                         dlnew->type= DL_POLY;
1313                                         dlnew->col= dl->col;
1314                                         
1315                                         fp= dl->verts+3*(dl->nr-1);
1316                                         dpoly= 3*dl->nr;
1317                                         
1318                                         a= dl->parts;
1319                                         while(a--) {
1320                                                 VECCOPY(fp1, fp);
1321                                                 fp1+= 3;
1322                                                 fp+= dpoly;
1323                                         }
1324                                 }
1325                         }
1326                 }
1327                 dl= dl->next;
1328         }
1329
1330         filldisplist(&front, dispbase);
1331         filldisplist(&back, dispbase);
1332         
1333         freedisplist(&front);
1334         freedisplist(&back);
1335
1336         filldisplist(dispbase, dispbase);
1337         
1338 }
1339
1340 void curve_to_filledpoly(Curve *cu, ListBase *nurb, ListBase *dispbase)
1341 {
1342         if(cu->flag & CU_3D) return;
1343
1344         if(dispbase->first && ((DispList*) dispbase->first)->type==DL_SURF) {
1345                 bevels_to_filledpoly(cu, dispbase);
1346         }
1347         else {
1348                 filldisplist(dispbase, dispbase);
1349         }
1350 }
1351
1352
1353 /* taper rules:
1354   - only 1 curve
1355   - first point left, last point right
1356   - based on subdivided points in original curve, not on points in taper curve (still)
1357 */
1358 static float calc_taper(Object *taperobj, int cur, int tot)
1359 {
1360         Curve *cu;
1361         DispList *dl;
1362         
1363         if(taperobj==NULL) return 1.0;
1364         
1365         cu= taperobj->data;
1366         dl= cu->disp.first;
1367         if(dl==NULL) {
1368                 makeDispListCurveTypes(taperobj, 0);
1369                 dl= cu->disp.first;
1370         }
1371         if(dl) {
1372                 float fac= ((float)cur)/(float)(tot-1);
1373                 float minx, dx, *fp;
1374                 int a;
1375                 
1376                 /* horizontal size */
1377                 minx= dl->verts[0];
1378                 dx= dl->verts[3*(dl->nr-1)] - minx;
1379                 if(dx>0.0) {
1380                 
1381                         fp= dl->verts;
1382                         for(a=0; a<dl->nr; a++, fp+=3) {
1383                                 if( (fp[0]-minx)/dx >= fac) {
1384                                         /* interpolate with prev */
1385                                         if(a>0) {
1386                                                 float fac1= (fp[-3]-minx)/dx;
1387                                                 float fac2= (fp[0]-minx)/dx;
1388                                                 if(fac1!=fac2)
1389                                                         return fp[1]*(fac1-fac)/(fac1-fac2) + fp[-2]*(fac-fac2)/(fac1-fac2);
1390                                         }
1391                                         return fp[1];
1392                                 }
1393                         }
1394                         return fp[-2];  // last y coord
1395                 }
1396         }
1397         
1398         return 1.0;
1399 }
1400
1401 void makeDispListMBall(Object *ob)
1402 {
1403         if(!ob || (ob->flag&OB_FROMDUPLI) || ob->type!=OB_MBALL) return;
1404
1405         freedisplist(&(ob->disp));
1406         
1407         if(ob->type==OB_MBALL) {
1408                 if(ob==find_basis_mball(ob)) {
1409                         metaball_polygonize(ob);
1410                         tex_space_mball(ob);
1411
1412                         object_deform_mball(ob);
1413                 }
1414         }
1415         
1416         boundbox_displist(ob);
1417 }
1418
1419 static ModifierData *curve_get_tesselate_point(Object *ob, int forRender, int editmode)
1420 {
1421         ModifierData *md = modifiers_getVirtualModifierList(ob);
1422         ModifierData *preTesselatePoint;
1423
1424         preTesselatePoint = NULL;
1425         for (; md; md=md->next) {
1426                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1427
1428                 if (!(md->mode&(1<<forRender))) continue;
1429                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
1430                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1431
1432                 if (md->type==eModifierType_Hook) {
1433                         preTesselatePoint  = md;
1434                 }
1435         }
1436
1437         return preTesselatePoint;
1438 }
1439
1440 void curve_calc_modifiers_pre(Object *ob, ListBase *nurb, int forRender, float (**originalVerts_r)[3], float (**deformedVerts_r)[3], int *numVerts_r)
1441 {
1442         int editmode = (!forRender && ob==G.obedit);
1443         ModifierData *md = modifiers_getVirtualModifierList(ob);
1444         ModifierData *preTesselatePoint = curve_get_tesselate_point(ob, forRender, editmode);
1445         int numVerts = 0;
1446         float (*originalVerts)[3] = NULL;
1447         float (*deformedVerts)[3] = NULL;
1448
1449         if (preTesselatePoint) {
1450                 for (; md; md=md->next) {
1451                         ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1452
1453                         if (!(md->mode&(1<<forRender))) continue;
1454                         if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
1455                         if (mti->isDisabled && mti->isDisabled(md)) continue;
1456                         if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1457
1458                         if (!deformedVerts) {
1459                                 deformedVerts = curve_getVertexCos(ob->data, nurb, &numVerts);
1460                                 originalVerts = MEM_dupallocN(deformedVerts);
1461                         }
1462                         mti->deformVerts(md, ob, NULL, deformedVerts, numVerts);
1463
1464                         if (md==preTesselatePoint)
1465                                 break;
1466                 }
1467         }
1468
1469         if (deformedVerts) {
1470                 curve_applyVertexCos(ob->data, nurb, deformedVerts);
1471         }
1472
1473         *originalVerts_r = originalVerts;
1474         *deformedVerts_r = deformedVerts;
1475         *numVerts_r = numVerts;
1476 }
1477
1478 void curve_calc_modifiers_post(Object *ob, ListBase *nurb, ListBase *dispbase, int forRender, float (*originalVerts)[3], float (*deformedVerts)[3])
1479 {
1480         int editmode = (!forRender && ob==G.obedit);
1481         ModifierData *md = modifiers_getVirtualModifierList(ob);
1482         ModifierData *preTesselatePoint = curve_get_tesselate_point(ob, forRender, editmode);
1483         DispList *dl;
1484
1485         if (preTesselatePoint) {
1486                 md = preTesselatePoint->next;
1487         }
1488
1489         for (; md; md=md->next) {
1490                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1491
1492                 if (!(md->mode&(1<<forRender))) continue;
1493                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
1494                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1495                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1496
1497                 for (dl=dispbase->first; dl; dl=dl->next) {
1498                         mti->deformVerts(md, ob, NULL, (float(*)[3]) dl->verts, (dl->type==DL_INDEX3)?dl->nr:dl->parts*dl->nr);
1499                 }
1500         }
1501
1502         if (deformedVerts) {
1503                 curve_applyVertexCos(ob->data, nurb, originalVerts);
1504                 MEM_freeN(originalVerts);
1505                 MEM_freeN(deformedVerts);
1506         }
1507 }
1508
1509 void makeDispListSurf(Object *ob, ListBase *dispbase, int forRender)
1510 {
1511         ListBase *nubase;
1512         Nurb *nu;
1513         Curve *cu = ob->data;
1514         DispList *dl;
1515         float *data;
1516         int len;
1517         int numVerts;
1518         float (*originalVerts)[3];
1519         float (*deformedVerts)[3];
1520                 
1521         if(!forRender && ob==G.obedit) {
1522                 nubase= &editNurb;
1523         }
1524         else {
1525                 do_curve_key(cu);
1526                 nubase= &cu->nurb;
1527         }
1528
1529         curve_calc_modifiers_pre(ob, nubase, forRender, &originalVerts, &deformedVerts, &numVerts);
1530
1531         for (nu=nubase->first; nu; nu=nu->next) {
1532                 if(forRender || nu->hide==0) {
1533                         if(nu->pntsv==1) {
1534                                 len= nu->pntsu*nu->resolu;
1535                                 
1536                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1537                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1538                                 
1539                                 BLI_addtail(dispbase, dl);
1540                                 dl->parts= 1;
1541                                 dl->nr= len;
1542                                 dl->col= nu->mat_nr;
1543                                 dl->rt= nu->flag;
1544                                 
1545                                 data= dl->verts;
1546                                 if(nu->flagu & 1) dl->type= DL_POLY;
1547                                 else dl->type= DL_SEGM;
1548                                 
1549                                 makeNurbcurve(nu, data, 3);
1550                         }
1551                         else {
1552                                 len= nu->resolu*nu->resolv;
1553                                 
1554                                 dl= MEM_callocN(sizeof(DispList), "makeDispListsurf");
1555                                 dl->verts= MEM_callocN(len*3*sizeof(float), "dlverts");
1556                                 BLI_addtail(dispbase, dl);
1557
1558                                 dl->col= nu->mat_nr;
1559                                 dl->rt= nu->flag;
1560                                 
1561                                 data= dl->verts;
1562                                 dl->type= DL_SURF;
1563
1564                                 dl->parts= nu->resolu;  /* in reverse, because makeNurbfaces works that way */
1565                                 dl->nr= nu->resolv;
1566                                 if(nu->flagv & CU_CYCLIC) dl->flag|= DL_CYCL_U; /* reverse too! */
1567                                 if(nu->flagu & CU_CYCLIC) dl->flag|= DL_CYCL_V;
1568
1569                                 makeNurbfaces(nu, data, 0);
1570                         }
1571                 }
1572         }
1573
1574         if (!forRender) {
1575                 tex_space_curve(cu);
1576         }
1577
1578         curve_calc_modifiers_post(ob, nubase, dispbase, forRender, originalVerts, deformedVerts);
1579 }
1580 void makeDispListCurveTypes(Object *ob, int forOrco)
1581 {
1582         Curve *cu = ob->data;
1583         ListBase *dispbase;
1584
1585         if((ob->flag&OB_FROMDUPLI) || !ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) return;
1586         if(ob->flag & OB_FROMDUPLI) return;
1587
1588         freedisplist(&(ob->disp));
1589         dispbase= &(cu->disp);
1590         freedisplist(dispbase);
1591         
1592         if(ob->type==OB_SURF) {
1593                 makeDispListSurf(ob, dispbase, 0);
1594         }
1595         else if ELEM(ob->type, OB_CURVE, OB_FONT) {
1596                 int obedit= (G.obedit && G.obedit->data==ob->data);
1597                 int numVerts;
1598                 float (*originalVerts)[3];
1599                 float (*deformedVerts)[3];
1600                 ListBase *nubase = obedit?&editNurb:&cu->nurb;
1601                 
1602                 BLI_freelistN(&(cu->bev));
1603                 
1604                 if(cu->path) free_path(cu->path);
1605                 cu->path= NULL;
1606                 
1607                 if(ob->type==OB_FONT) text_to_curve(ob, 0);
1608                 
1609                 if(!obedit && !forOrco) do_curve_key(cu);
1610                 if(!forOrco) curve_calc_modifiers_pre(ob, nubase, 0, &originalVerts, &deformedVerts, &numVerts);
1611
1612                 makeBevelList(ob);
1613
1614                 if(obedit) {
1615                         if(ob->type==OB_CURVE) {
1616                                 curve_to_displist(cu, &editNurb, dispbase);
1617                                 curve_to_filledpoly(cu, &editNurb, dispbase);
1618                         }
1619                         else {
1620                                 curve_to_displist(cu, &cu->nurb, dispbase);
1621                                 if (!(cu->flag&CU_FAST)) {
1622                                         curve_to_filledpoly(cu, &cu->nurb, dispbase);
1623                                 }
1624                         }
1625                 }
1626                 else {
1627                         ListBase dlbev;;
1628
1629                                 /* If curve has no bevel will return nothing */
1630                         makebevelcurve(ob, &dlbev);
1631
1632                         if (!dlbev.first) {
1633                                 curve_to_displist(cu, &cu->nurb, dispbase);
1634                         } else {
1635                                 float widfac= cu->width-1.0;
1636                                 BevList *bl= cu->bev.first;
1637                                 Nurb *nu= cu->nurb.first;
1638
1639                                 for (; bl && nu; bl=bl->next,nu=nu->next) {
1640                                         DispList *dlb;
1641                                         
1642                                         for (dlb=dlbev.first; dlb; dlb=dlb->next) {
1643                                                 DispList *dl;
1644                                                 float *fp1, *data;
1645                                                 BevPoint *bevp;
1646                                                 int a,b;
1647
1648                                                         /* for each part of the bevel use a separate displblock */
1649                                                 dl= MEM_callocN(sizeof(DispList), "makeDispListbev1");
1650                                                 dl->verts= data= MEM_callocN(3*sizeof(float)*dlb->nr*bl->nr, "dlverts");
1651                                                 BLI_addtail(dispbase, dl);
1652
1653                                                 dl->type= DL_SURF;
1654                                                 
1655                                                 dl->flag= dlb->flag & (DL_FRONT_CURVE|DL_BACK_CURVE);
1656                                                 if(dlb->type==DL_POLY) dl->flag |= DL_CYCL_U;
1657                                                 if(bl->poly>=0) dl->flag |= DL_CYCL_V;
1658                                                 
1659                                                 dl->parts= bl->nr;
1660                                                 dl->nr= dlb->nr;
1661                                                 dl->col= nu->mat_nr;
1662                                                 dl->rt= nu->flag;
1663                                                 dl->bevelSplitFlag= MEM_callocN(sizeof(*dl->col2)*((bl->nr+0x1F)>>5), "col2");
1664                                                 bevp= (BevPoint *)(bl+1);
1665
1666                                                         /* for each point of poly make a bevel piece */
1667                                                 bevp= (BevPoint *)(bl+1);
1668                                                 for(a=0; a<bl->nr; a++,bevp++) {
1669                                                         float fac = calc_taper(cu->taperobj, a, bl->nr);
1670                                                         
1671                                                         if (bevp->f1) {
1672                                                                 dl->bevelSplitFlag[a>>5] |= 1<<(a&0x1F);
1673                                                         }
1674
1675                                                                 /* rotate bevel piece and write in data */
1676                                                         fp1= dlb->verts;
1677                                                         for (b=0; b<dlb->nr; b++,fp1+=3,data+=3) {
1678                                                                 if(cu->flag & CU_3D) {
1679                                                                         float vec[3];
1680
1681                                                                         vec[0]= fp1[1]+widfac;
1682                                                                         vec[1]= fp1[2];
1683                                                                         vec[2]= 0.0;
1684                                                                         
1685                                                                         Mat3MulVecfl(bevp->mat, vec);
1686                                                                         
1687                                                                         data[0]= bevp->x+ fac*vec[0];
1688                                                                         data[1]= bevp->y+ fac*vec[1];
1689                                                                         data[2]= bevp->z+ fac*vec[2];
1690                                                                 }
1691                                                                 else {
1692                                                                         data[0]= bevp->x+ fac*(widfac+fp1[1])*bevp->sina;
1693                                                                         data[1]= bevp->y+ fac*(widfac+fp1[1])*bevp->cosa;
1694                                                                         data[2]= bevp->z+ fac*fp1[2];
1695                                                                 }
1696                                                         }
1697                                                 }
1698                                         }
1699                                 }
1700
1701                                 freedisplist(&dlbev);
1702                         }
1703
1704                         curve_to_filledpoly(cu, &cu->nurb, dispbase);
1705                 }
1706                 if(cu->flag & CU_PATH) calc_curvepath(ob);
1707
1708                 if(!forOrco) curve_calc_modifiers_post(ob, nubase, &cu->disp, 0, originalVerts, deformedVerts);
1709                 tex_space_curve(cu);
1710         }
1711         
1712         boundbox_displist(ob);
1713 }
1714
1715 /*******************************/
1716 /*****       OUTLINE       *****/
1717 /*******************************/
1718
1719 typedef struct Sample{
1720         short x, y;
1721 } Sample;
1722
1723 typedef struct Segment{
1724         /* coordinates */
1725         struct Segment * next, * prev;
1726         float co[2];
1727 } Segment;
1728
1729
1730
1731 static int dflt_in_out(struct ImBuf * ibuf, int x, int y)
1732 {
1733         unsigned char * rect;
1734         
1735         if (ibuf == 0) return (0);
1736         if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y || ibuf->rect == 0) return (-1);
1737         
1738         rect = (unsigned char *) (ibuf->rect + (y * ibuf->x) + x);
1739         if (rect[0] > 0x81) return (1);
1740         return(0);
1741 }
1742
1743
1744 static Sample * outline(struct ImBuf * ibuf,
1745                                  int (*in_or_out)(struct ImBuf *, int, int))
1746 {
1747         static int dirs[8][2] = {
1748                 {-1,  0}, {-1,  1},     {0,  1}, {1,  1}, 
1749                 {1,  0}, {1, -1}, {0, -1}, {-1, -1}
1750         };
1751         
1752         int dir, x, y, in, i;
1753         int count, sampcount;
1754         int startx = 0, starty = 0;
1755         Sample * samp, * oldsamp;
1756         
1757         /* input:
1758          * 1 - image 
1759          * 2 - pointer to function that defines which pixel 'in' or 'out' is
1760          */
1761         
1762         if (ibuf == 0) return (0);
1763         if (ibuf->rect == 0) return (0);
1764         
1765         if (in_or_out == 0) in_or_out = dflt_in_out;
1766         in = in_or_out(ibuf, 0, 0);
1767         
1768         /* search for first transition, and continue from there */      
1769         for (y = 0; y < ibuf->y; y++) {
1770                 for (x = 0; x < ibuf->x; x++) {
1771                         if (in_or_out(ibuf, x, y) != in) {
1772                                 /* found first 'other' point !! */
1773                                 
1774                                 if (x != startx) dir = 0;
1775                                 else dir = 6;
1776                                 
1777                                 startx = x; starty = y;
1778                                 count = 1;
1779                                 sampcount = 2000;
1780                                 samp = MEM_mallocN(sampcount * sizeof(Sample), "wire_samples");
1781                                 
1782                                 do{
1783                                         samp[count].x = x; samp[count].y = y;
1784                                         count++;
1785                                         
1786                                         if (count >= sampcount) {
1787                                                 oldsamp = samp;
1788                                                 samp = MEM_mallocN(2 * sampcount * sizeof(Sample), "wire_samples");
1789                                                 memcpy(samp, oldsamp, sampcount * sizeof(Sample));
1790                                                 sampcount *= 2;
1791                                                 MEM_freeN(oldsamp);
1792                                         }
1793                                         
1794                                         i = 0;
1795                                         while(in_or_out(ibuf, x + dirs[dir][0], y + dirs[dir][1]) == in) {
1796                                                 dir = (dir + 1) & 0x7;
1797                                                 if (i++ == 9) break;
1798                                         }
1799                                         
1800                                         if (i >= 8) {
1801                                                 /* this has to be a loose point */
1802                                                 break;
1803                                         }
1804                                         
1805                                         x += dirs[dir][0];
1806                                         y += dirs[dir][1];
1807                                         dir = (dir - 3) & 0x7;
1808                                 } while(x != startx || y != starty);
1809                                 
1810                                 if (i >= 8) {
1811                                         /* patch for loose points */
1812                                         MEM_freeN(samp);
1813                                 } else {
1814                                         count = count - 1;
1815                                         samp[0].x = count >> 16;
1816                                         samp[0].y = count;
1817                                         return(samp);
1818                                 }
1819                         }
1820                 }
1821         }
1822         /* printf("no transition \n"); */
1823         return(0);
1824 }
1825
1826
1827
1828 /*******************************/
1829 /*****      WIREFRAME      *****/
1830 /*******************************/
1831
1832
1833 static float DistToLine2D(short *v1, short *v2, short *v3)   /* using Hesse formula :NO LINE PIECE! */
1834 {
1835         float a[2],deler;
1836
1837         a[0] = v2[1]-v3[1];
1838         a[1] = v3[0]-v2[0];
1839         deler = sqrt(a[0]*a[0]+a[1]*a[1]);
1840         if(deler == 0.0) return 0;
1841
1842         return fabs((v1[0]-v2[0])*a[0]+(v1[1]-v2[1])*a[1])/deler;
1843
1844 }
1845
1846 static float ComputeMaxShpError(Sample *samp, int first, int last, int *splitPoint)
1847     /* samp:  Array of digitized points */
1848     /* first, last:  Indices defining region    */
1849     /* splitpoint:  Point of maximum error      */
1850 {
1851     int         i;
1852     float       maxDist;                                /*  Maximum error               */
1853     float       dist;                                   /*  Current error               */
1854  
1855     *splitPoint = (last - first + 1) / 2;
1856     maxDist = 0.0;
1857         
1858     for (i = first + 1; i < last; i++) {                                
1859                 dist = DistToLine2D((short *)(samp+i), (short *)(samp+first), (short *)(samp+last));
1860
1861                 if (dist >= maxDist) {
1862                 maxDist = dist;
1863                 *splitPoint = i;
1864                 }
1865     }
1866
1867     return (maxDist);
1868 }
1869
1870
1871 static void FitPoly(Sample *samp, int first, int last, float shperr, ListBase *seglist)
1872     /* Samp: Array of digitized points */
1873     /* first,last: Indices of first and last pts in region */
1874     /* spherr: User-defined error squared          */
1875 {
1876     Segment     * seg;                          /* Control points segment*/
1877     float       maxError;                       /*  Maximum fitting error        */
1878     int         splitPoint;                     /*  Point to split point set at  */
1879     int         nPts;                           /*  Number of points in subset  */
1880         
1881     nPts = last - first + 1;
1882
1883     /*  Use heuristic if region only has two points in it */
1884
1885         seg = MEM_mallocN(sizeof(Segment), "wure_segment");
1886
1887         seg->co[0] = samp[first].x;
1888         seg->co[1] = samp[first].y;
1889         
1890     if (nPts == 2) {
1891                 BLI_addtail(seglist, seg);
1892                 return;
1893     }
1894
1895         maxError = ComputeMaxShpError(samp, first, last, &splitPoint);
1896         if (maxError < shperr) {
1897                 BLI_addtail(seglist, seg);
1898                 return;
1899         }
1900         
1901     /* Fitting failed -- split at max error point and fit recursively */
1902         
1903     FitPoly(samp, first, splitPoint, shperr, seglist);
1904     FitPoly(samp, splitPoint, last, shperr, seglist);
1905         
1906         MEM_freeN(seg);
1907 }
1908
1909
1910 static void ibuf2wire(ListBase * wireframe, struct ImBuf * ibuf)
1911 {
1912         int count;
1913         Sample * samp;
1914         
1915         /* first make a list of samples */
1916         
1917         samp = outline(ibuf, 0);
1918         if (samp == 0) return;
1919         
1920         count = (samp[0].x << 16) + samp[0].y;
1921         if (count) FitPoly(samp, 1, count, 1.0, wireframe); /* was 3.0. Frank */
1922
1923         MEM_freeN(samp);
1924 }
1925
1926
1927
1928 void imagestodisplist(void)
1929 {
1930         Base *base;
1931         Object *ob;
1932         Material *ma;
1933         Tex *tex;
1934         Mesh *me;
1935         ListBase _wireframe, *wireframe;
1936         DispList *dl;
1937         Segment *seg;
1938         float *data, xfac, yfac, xsi, ysi, vec[3], dum;
1939         int tot;
1940         
1941         _wireframe.first= 0;
1942         _wireframe.last= 0;
1943         wireframe = &_wireframe;
1944         
1945         init_render_textures();
1946         
1947         base= G.scene->base.first;
1948         while(base) {
1949                 if(( (base->flag & SELECT) && (base->lay & G.scene->lay) ) ) {
1950                         if( base->object->type==OB_MESH) {
1951                                 ob= base->object;
1952                                 me= ob->data;
1953                                 
1954                                 ma= give_current_material(ob, 1);
1955         
1956                                 if(ma && ma->mtex[0] && ma->mtex[0]->tex) {
1957                                         tex= ma->mtex[0]->tex;
1958                                         
1959                                         /* this takes care of correct loading of new imbufs */
1960                                         externtex(ma->mtex[0], vec, &dum, &dum, &dum, &dum, &dum);
1961                                         
1962                                         if(tex->type==TEX_IMAGE && tex->ima && tex->ima->ibuf) {                                
1963                                                 
1964                                                 ob->dtx |= OB_DRAWIMAGE;
1965                                                 
1966                                                 ibuf2wire(wireframe, tex->ima->ibuf);
1967
1968                                                 tot= 0;
1969                                                 seg = wireframe->first;
1970                                                 while (seg) {
1971                                                         tot++;
1972                                                         seg = seg->next;
1973                                                 }
1974         
1975                                                 if(tot) {
1976                                                         float size[3];
1977
1978                                                         freedisplist(&(ob->disp));
1979
1980                                                         dl= MEM_callocN(sizeof(DispList), "makeDispListimage");
1981                                                         dl->verts= MEM_callocN(3*sizeof(float)*tot, "dlverts");
1982                                                         
1983                                                         BLI_addtail(&(ob->disp), dl);
1984                                                         dl->type= DL_POLY;
1985                                                         dl->parts= 1;
1986                                                         dl->nr= tot;
1987                                                         
1988                                                         xsi= 0.5*(tex->ima->ibuf->x);
1989                                                         ysi= 0.5*(tex->ima->ibuf->y);
1990
1991                                                         mesh_get_texspace(me, NULL, NULL, size);
1992                                                         xfac= size[0]/xsi;
1993                                                         yfac= size[1]/ysi;
1994                                                                                                 
1995                                                         data= dl->verts;
1996                                                         seg = wireframe->first;
1997                                                         while (seg) {
1998                                                                 data[0]= xfac*(seg->co[0]-xsi);
1999                                                                 data[1]= yfac*(seg->co[1]-ysi);
2000                                                                 data+= 3;
2001                                                                 seg = seg->next;
2002                                                         }
2003                                                         BLI_freelistN(wireframe);
2004                                                 }
2005                                         }
2006                                 }
2007                         }
2008                 }
2009                 base= base->next;
2010         }
2011         
2012         end_render_textures();
2013         
2014         allqueue(REDRAWVIEW3D, 0);
2015 }
2016
2017 static void boundbox_displist(Object *ob)
2018 {
2019         BoundBox *bb=0;
2020         float min[3], max[3];
2021         DispList *dl;
2022         float *vert;
2023         int a, tot=0;
2024         
2025         INIT_MINMAX(min, max);
2026
2027         if(ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
2028                 Curve *cu= ob->data;
2029
2030                 if(cu->bb==0) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");        
2031                 bb= cu->bb;
2032                 
2033                 dl= cu->disp.first;
2034
2035                 while (dl) {
2036                         if(dl->type==DL_INDEX3) tot= dl->nr;
2037                         else tot= dl->nr*dl->parts;
2038                         
2039                         vert= dl->verts;
2040                         for(a=0; a<tot; a++, vert+=3) {
2041                                 DO_MINMAX(vert, min, max);
2042                         }
2043
2044                         dl= dl->next;
2045                 }
2046         }
2047         
2048         if(bb) {
2049                 boundbox_set_from_min_max(bb, min, max);
2050         }
2051 }
2052
2053 void displistmesh_add_edges(DispListMesh *dlm)
2054 {
2055         EdgeHash *eh = BLI_edgehash_new();
2056         EdgeHashIterator *ehi;
2057         int i;
2058
2059         for (i=0; i<dlm->totface; i++) {
2060                 MFace *mf = &dlm->mface[i];
2061
2062                 if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
2063                         BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
2064                 if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3))
2065                         BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL);
2066                 
2067                 if (mf->v4) {
2068                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4))
2069                                 BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL);
2070                         if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1))
2071                                 BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL);
2072                 } else {
2073                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1))
2074                                 BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL);
2075                 }
2076         }
2077
2078         dlm->totedge = BLI_edgehash_size(eh);
2079         dlm->medge = MEM_callocN(dlm->totedge*sizeof(*dlm->medge), "medge");
2080
2081         ehi = BLI_edgehashIterator_new(eh);
2082         for (i=0; !BLI_edgehashIterator_isDone(ehi); BLI_edgehashIterator_step(ehi)) {
2083                 MEdge *med = &dlm->medge[i++];
2084
2085                 BLI_edgehashIterator_getKey(ehi, &med->v1, &med->v2);
2086
2087                 med->flag = ME_EDGEDRAW|ME_EDGERENDER;
2088         }
2089         BLI_edgehashIterator_free(ehi);
2090
2091         BLI_edgehash_free(eh, NULL);
2092 }