2.5: various warning fixes. There was one actual bug in a few different
[blender.git] / source / blender / blenkernel / intern / exotic.c
1 /*  exotic.c   
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): 
25  * - Martin DeMello
26  *   Added dxf_read_arc, dxf_read_ellipse and dxf_read_lwpolyline
27  *   Copyright (C) 2004 by Etheract Software Labs
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  *  
31  *  eigen videoscape formaat:
32  *
33  * 
34  * lamp:
35  *              3DG2
36                 aantal_lampen
37                 
38                 type
39                 spsi spbl
40                 r, g, b, energy
41                 locx, locy, locz
42                 vecx, vecy, vecz
43
44                 
45         curve / nurbs:
46                 3DG3
47                 5 of 11 (curve of surf)
48                 aantal_nurbs
49                 extr1 extr2
50                 
51                 mat[0][0] mat[0][1] mat[0][2] mat[0][3]
52                 mat[1][0] mat[1][1] mat[1][2] mat[1][3]
53                 ...             
54                 
55                 type
56                 pntsu, pntsv
57                 resolu, resolv
58                 orderu, orderv
59                 flagu, flagv
60                 
61                 (als type==nurb) x y z w
62                                                  x y z w
63                                                  ...
64                 (als type==bez)  xyz xyz xyz h1 h2 h3
65                                                  xyz xyz xyz h1 h2 h3
66                                                  ...
67  *  
68  * 
69  */
70
71
72 #include <ctype.h> /* isdigit, isspace */
73 #include <math.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <fcntl.h>
77 #include <string.h>
78
79 #ifndef _WIN32 
80 #include <unistd.h>
81 #else
82 #include <io.h>
83 #define open _open
84 #define read _read
85 #define close _close
86 #define write _write
87 #endif
88
89 #include "MEM_guardedalloc.h"
90
91 #include "DNA_object_types.h"
92 #include "DNA_mesh_types.h"
93 #include "DNA_meshdata_types.h"
94 #include "DNA_material_types.h"
95 #include "DNA_lamp_types.h"
96 #include "DNA_curve_types.h"
97 #include "DNA_image_types.h"
98 #include "DNA_camera_types.h"
99 #include "DNA_scene_types.h"
100 #include "DNA_view3d_types.h"
101 #include "DNA_userdef_types.h"
102
103 #include "BKE_utildefines.h"
104 #include "BLI_blenlib.h"
105 #include "BLI_arithb.h"
106 #include "BLI_editVert.h"
107
108 #include "BKE_blender.h"
109 #include "BKE_global.h"
110 #include "BKE_main.h"
111 #include "BKE_mesh.h"
112 #include "BKE_library.h"
113 #include "BKE_global.h"
114 #include "BKE_object.h"
115 #include "BKE_material.h"
116 #include "BKE_exotic.h"
117 /*  #include "BKE_error.h" */
118 #include "BKE_screen.h"
119 #include "BKE_displist.h"
120 #include "BKE_DerivedMesh.h"
121 #include "BKE_curve.h"
122 #include "BKE_customdata.h"
123
124 #ifndef DISABLE_PYTHON
125 #include "BPY_extern.h"
126 #endif
127
128 #include "zlib.h"
129
130 static int is_dxf(char *str);
131 static void dxf_read(char *filename);
132 static int is_stl(char *str);
133
134 static int is_stl_ascii(char *str)
135 {       
136         FILE *fpSTL;
137         char buffer[1000];
138         int  numread, i;
139
140         fpSTL = fopen(str, "rb");
141         if ( (numread = fread( (void *) buffer, sizeof(char), 1000, fpSTL)) <= 0 )
142           { fclose(fpSTL); return 0; }
143
144         for (i=0; i < numread; ++i) {
145           /* if bit 8 is set we assume binary */
146           if (buffer[i] & 0x80)
147                 { fclose(fpSTL); return 0; }
148         }
149
150         buffer[5] = '\0';
151         if ( !(strstr(buffer, "solid")) && !(strstr(buffer, "SOLID")) ) 
152           { fclose(fpSTL); return 0; }
153
154         fclose(fpSTL);
155         
156         return 1;
157 }
158
159 static int is_stl(char *str)
160 {
161         int i;
162         i = strlen(str) - 3;
163         if ( (str[i] !='s') && (str[i] !='S'))
164                 return 0;
165         i++;
166         if ( (str[i] !='t') && (str[i] !='T'))
167                 return 0;
168         i++;
169         if ( (str[i] !='l') && (str[i] !='L'))
170                 return 0;
171
172         return 1;
173 }
174
175 #define READSTLVERT {                                   \
176   if (fread(mvert->co, sizeof(float), 3, fpSTL) != 3) { \
177     char error_msg[255];                                \
178     MEM_freeN(vertdata);                                \
179     MEM_freeN(facedata);                                \
180     fclose(fpSTL);                                      \
181     sprintf(error_msg, "Problems reading face %d!", i); \
182     return;                                             \
183   }                                                     \
184   else {                                                \
185     if (ENDIAN_ORDER==B_ENDIAN) {                       \
186       SWITCH_INT(mvert->co[0]);                         \
187       SWITCH_INT(mvert->co[1]);                         \
188       SWITCH_INT(mvert->co[2]);                         \
189     }                                                   \
190   }                                                     \
191 }
192
193 static void simple_vertex_normal_blend(short *no, short *ble)
194 {
195         if(no[0]==0 && no[1]==0 && no[2]==0) {
196                 VECCOPY(no, ble);
197         }
198         else {
199                 no[0]= (2*no[0] + ble[0])/3;
200                 no[1]= (2*no[1] + ble[1])/3;
201                 no[2]= (2*no[2] + ble[2])/3;
202         }
203 }
204
205 static void mesh_add_normals_flags(Mesh *me)
206 {
207         MVert *v1, *v2, *v3, *v4;
208         MFace *mface;
209         float nor[3];
210         int a;
211         short sno[3];
212         
213         mface= me->mface;
214         for(a=0; a<me->totface; a++, mface++) {
215                 v1= me->mvert+mface->v1;
216                 v2= me->mvert+mface->v2;
217                 v3= me->mvert+mface->v3;
218                 v4= me->mvert+mface->v4;
219                 
220                 CalcNormFloat(v1->co, v2->co, v3->co, nor);
221                 sno[0]= 32767.0*nor[0];
222                 sno[1]= 32767.0*nor[1];
223                 sno[2]= 32767.0*nor[2];
224                 
225                 simple_vertex_normal_blend(v1->no, sno);
226                 simple_vertex_normal_blend(v2->no, sno);
227                 simple_vertex_normal_blend(v3->no, sno);
228                 if(mface->v4) {
229                         simple_vertex_normal_blend(v4->no, sno);
230                 }
231                 mface->edcode= ME_V1V2|ME_V2V3;
232         }       
233 }
234
235 static void read_stl_mesh_binary(char *str)
236 {
237         FILE   *fpSTL;
238         Object *ob;
239         Mesh   *me;
240         MVert  *mvert, *vertdata;
241         MFace  *mface, *facedata;
242         unsigned int numfacets = 0, i, j, vertnum;
243         unsigned int maxmeshsize, nummesh, lastmeshsize;
244         unsigned int totvert, totface;
245
246         fpSTL= fopen(str, "rb");
247         if(fpSTL==NULL) {
248                 //XXX error("Can't read file");
249                 return;
250         }
251
252         fseek(fpSTL, 80, SEEK_SET);
253         fread(&numfacets, 4*sizeof(char), 1, fpSTL);
254         if (ENDIAN_ORDER==B_ENDIAN) {
255                 SWITCH_INT(numfacets);
256         }
257
258         maxmeshsize = MESH_MAX_VERTS/3;
259
260         nummesh      = (numfacets / maxmeshsize) + 1;
261         lastmeshsize = numfacets % maxmeshsize;
262
263         if (numfacets) {
264                 for (j=0; j < nummesh; ++j) {
265                         /* new object */
266                         if (j == nummesh-1) {
267                                 totface = lastmeshsize;
268                         }
269                         else {
270                                 totface = maxmeshsize;
271                         }
272                         totvert = 3 * totface;
273         
274                         vertdata = MEM_callocN(totvert*sizeof(MVert), "mverts");
275                         facedata = MEM_callocN(totface*sizeof(MFace), "mface");
276
277                         vertnum = 0;
278                         mvert= vertdata;
279                         mface = facedata;
280                         for (i=0; i < totface; i++) {
281                                 fseek(fpSTL, 12, SEEK_CUR); /* skip the face normal */
282                                 READSTLVERT;
283                                 mvert++;
284                                 READSTLVERT;
285                                 mvert++;
286                                 READSTLVERT;
287                                 mvert++;
288
289                                 mface->v1 = vertnum++;
290                                 mface->v2 = vertnum++;
291                                 mface->v3 = vertnum++;
292                                 mface++;
293
294                                 fseek(fpSTL, 2, SEEK_CUR);
295                         }
296
297                         ob= add_object(OB_MESH);
298                         me= ob->data;
299                         me->totvert = totvert;
300                         me->totface = totface;
301                         me->mvert = CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN,
302                                                          vertdata, totvert);
303                         me->mface = CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN,
304                                                          facedata, totface);
305
306                         mesh_add_normals_flags(me);
307                         make_edges(me, 0);
308                 }
309                 //XXX waitcursor(1);
310         }
311         fclose(fpSTL);
312
313 }
314 #undef READSTLVERT
315
316 #define STLALLOCERROR { \
317         char error_msg[255]; \
318         fclose(fpSTL); \
319         sprintf(error_msg, "Can't allocate storage for %d faces!", \
320                         numtenthousand * 10000); \
321         return; \
322 }
323
324 #define STLBAILOUT(message) { \
325         char error_msg[255]; \
326         fclose(fpSTL); \
327         free(vertdata); \
328         sprintf(error_msg, "Line %d: %s", linenum, message); \
329         return; \
330 }
331
332 #define STLREADLINE { \
333         if (!fgets(buffer, 2048, fpSTL)) STLBAILOUT("Can't read line!"); \
334         linenum++; \
335 }
336
337 #define STLREADVERT { \
338         STLREADLINE; \
339         if ( !(cp = strstr(buffer, "vertex")) && \
340                  !(cp = strstr(buffer, "VERTEX")) ) STLBAILOUT("Bad vertex!"); \
341         vp = vertdata + 3 * totvert; \
342         if (sscanf(cp + 6, "%f %f %f", vp, vp+1, vp+2) != 3) \
343                 STLBAILOUT("Bad vertex!"); \
344         ++totvert; \
345 }
346 static void read_stl_mesh_ascii(char *str)
347 {
348         FILE   *fpSTL;
349         char   buffer[2048], *cp;
350         Object *ob;
351         Mesh   *me;
352         MVert  *mvert;
353         MFace  *mface;
354         float  *vertdata, *vp;
355         unsigned int numtenthousand, linenum;
356         unsigned int i, vertnum;
357         unsigned int totvert, totface;
358
359         /* ASCII stl sucks ... we don't really know how many faces there
360            are until the file is done, so lets allocate faces 10000 at a time */
361
362         fpSTL= fopen(str, "r");
363         if(fpSTL==NULL) {
364                 //XXX error("Can't read file");
365                 return;
366         }
367         
368         /* we'll use the standard malloc/realloc for now ... 
369          * lets allocate enough storage to hold 10000 triangles,
370          * i.e. 30000 verts, i.e., 90000 floats.
371          */
372         numtenthousand = 1;
373         vertdata = malloc(numtenthousand*3*30000*sizeof(float));        // uses realloc!
374         if (!vertdata); STLALLOCERROR;
375
376         linenum = 1;
377         /* Get rid of the first line */
378         STLREADLINE;
379
380         totvert = 0;
381         totface = 0;
382         while(1) {
383                 /* Read in the next line */
384                 STLREADLINE;
385
386                 /* lets check if this is the end of the file */
387                 if ( strstr(buffer, "endsolid") || strstr(buffer, "ENDSOLID") ) 
388                         break;
389
390                 /* Well, guess that wasn't the end, so lets make
391                  * sure we have enough storage for some more faces
392                  */
393                 if ( (totface) && ( (totface % 10000) == 0 ) ) {
394                         ++numtenthousand;
395                         vertdata = realloc(vertdata, 
396                                                            numtenthousand*3*30000*sizeof(float));
397                         if (!vertdata); STLALLOCERROR;
398                 }
399                 
400                 /* Don't read normal, but check line for proper syntax anyway
401                  */
402                 if ( !(cp = strstr(buffer, "facet")) && 
403                          !(cp = strstr(buffer, "FACET")) ) STLBAILOUT("Bad normal line!");
404                 if ( !(strstr(cp+5, "normal")) && 
405                          !(strstr(cp+5, "NORMAL")) )       STLBAILOUT("Bad normal line!");
406
407                 /* Read in what should be the outer loop line 
408                  */
409                 STLREADLINE;
410                 if ( !(cp = strstr(buffer, "outer")) &&
411                          !(cp = strstr(buffer, "OUTER")) ) STLBAILOUT("Bad outer loop!");
412                 if ( !(strstr(cp+5, "loop")) &&
413                          !(strstr(cp+5, "LOOP")) )         STLBAILOUT("Bad outer loop!");
414
415                 /* Read in the face */
416                 STLREADVERT;
417                 STLREADVERT;
418                 STLREADVERT;
419
420                 /* Read in what should be the endloop line 
421                  */
422                 STLREADLINE;
423                 if ( !strstr(buffer, "endloop") && !strstr(buffer, "ENDLOOP") ) 
424                         STLBAILOUT("Bad endloop!");
425
426                 /* Read in what should be the endfacet line 
427                  */
428                 STLREADLINE;
429                 if ( !strstr(buffer, "endfacet") && !strstr(buffer, "ENDFACET") ) 
430                         STLBAILOUT("Bad endfacet!");
431
432                 /* Made it this far? Increment face count */
433                 ++totface;
434         }
435         fclose(fpSTL);
436
437         /* OK, lets create our mesh */
438         ob = add_object(OB_MESH);
439         me = ob->data;
440
441         me->totface = totface;
442         me->totvert = totvert;
443         me->mvert = CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC,
444                                          NULL, totvert);
445         me->mface = CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC,
446                                          NULL, totface);
447
448         /* Copy vert coords and create topology */
449         mvert = me->mvert;
450         mface = me->mface;
451         vertnum = 0;
452         for (i=0; i < totface; ++i) {
453                 memcpy(mvert->co, vertdata+3*vertnum, 3*sizeof(float) );
454                 mface->v1 = vertnum;
455                 mvert++;
456                 vertnum++;
457
458                 memcpy(mvert->co, vertdata+3*vertnum, 3*sizeof(float) );
459                 mface->v2 = vertnum;
460                 mvert++;
461                 vertnum++;
462
463                 memcpy(mvert->co, vertdata+3*vertnum, 3*sizeof(float) );
464                 mface->v3 = vertnum;
465                 mvert++;
466                 vertnum++;
467
468                 mface++;
469         }
470         free(vertdata);
471
472         mesh_add_normals_flags(me);
473         make_edges(me, 0);
474
475         //XXX waitcursor(1);
476 }
477
478 #undef STLALLOCERROR
479 #undef STLBAILOUT
480 #undef STLREADLINE
481 #undef STLREADVERT
482
483 static void read_videoscape_mesh(char *str)
484 {
485         Object *ob;
486         Mesh *me;
487         MVert *mvert;
488         MFace *mface;
489         Material *ma;
490         FILE *fp;
491         float *vertdata, *vd, min[3], max[3], cent[3], ftemp;
492         unsigned int color[32], col;
493         int totcol, a, b, verts, tottria=0, totquad=0, totedge=0, poly, nr0, nr, first;
494         int end;
495         char s[50];
496         
497         fp= fopen(str, "rb");
498         if(fp==NULL) {
499                 //XXX error("Can't read file");
500                 return;
501         }
502         
503         fscanf(fp, "%40s", s);
504         
505         fscanf(fp, "%d\n", &verts);
506         if(verts<=0) {
507                 fclose(fp);
508                 //XXX error("Read error");
509                 return;
510         }
511         
512         if(verts>MESH_MAX_VERTS) {
513                 //XXX error("too many vertices");
514                 fclose(fp);
515                 return;
516         }
517         
518         INIT_MINMAX(min, max);
519         vd= vertdata= MEM_mallocN(sizeof(float)*3*verts, "videoscapelezer");
520         
521         for(a=0; a<verts; a++) {
522                 fscanf(fp, "%f %f %f", vd, vd+1, vd+2);
523                 DO_MINMAX(vd, min, max);
524                 vd+=3;
525         }
526         
527         /* count faces and colors */
528         for(a=0; a<32; a++) color[a]= 0;
529         totcol= 0;
530         end= 1;
531         while(end>0) {
532                 end= fscanf(fp,"%d", &poly);
533                 if(end<=0) break;
534         
535                 if(poly==3) tottria++;
536                 else if(poly==4) totquad++;
537                 else totedge+= poly;
538         
539                 for(a=0;a<poly;a++) {
540                         end= fscanf(fp,"%d", &nr);
541                         if(end<=0) break;
542                 }
543                 if(end<=0) break;
544                 
545                 end= fscanf(fp,"%i\n", &col);
546                 col &= 0xF0F0F0;
547                 for(a=0; a<totcol; a++) {
548                         if(color[a]==col) break;
549                 }
550                 if(a>=totcol && totcol<32) {
551                         color[totcol]= col;
552                         totcol++;
553                 }
554         }
555
556         /* new object */
557         ob= add_object(OB_MESH);
558         me= ob->data;
559         me->totvert= verts;
560         me->totface= totedge+tottria+totquad;
561         
562         me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC,
563                                         NULL, me->totvert);
564         me->mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC,
565                                         NULL, me->totface);
566         
567         /* colors */
568         if(totcol) {
569                 ob->mat= MEM_callocN(sizeof(void *)*totcol, "ob->mat");
570                 me->mat= MEM_callocN(sizeof(void *)*totcol, "me->mat");
571                 me->totcol= totcol;
572                 ob->totcol= (unsigned char) me->totcol;
573                 ob->actcol= 1;
574         }
575         
576         /* materials */
577         for(a=0; a<totcol; a++) {
578                 ma= G.main->mat.first;
579                 while(ma) {
580                         if(ma->mtex[0]==0) {
581                                 col= rgb_to_cpack(ma->r, ma->g, ma->b);
582                                 if(color[a]==col) {
583                                         me->mat[a]= ma;
584                                         ma->id.us++;
585                                         break;
586                                 }
587                         }
588                         ma= ma->id.next;
589                 }
590                 if(ma==0) {
591                         ma= add_material("ext");
592                         me->mat[a]= ma;
593                         cpack_to_rgb(color[a], cent, cent+1, cent+2);
594                         ma->r= cent[0];
595                         ma->g= cent[1];
596                         ma->b= cent[2];
597                         automatname(ma);
598                 }
599         }
600         
601         /* verts */
602         
603         cent[0]= (min[0]+max[0])/2.0f;
604         cent[1]= (min[1]+max[1])/2.0f;
605         cent[2]= (min[2]+max[2])/2.0f;
606         VECCOPY(ob->loc, cent);
607         
608         a= me->totvert;
609         vd= vertdata;
610         mvert= me->mvert;
611         while(a--) {
612                 VecSubf(mvert->co, vd, cent);
613                 mvert++;
614                 vd+= 3;
615         }
616         
617         /* faces */
618         if(me->totface) {
619                 rewind(fp);
620         
621                 fscanf(fp, "%40s", s);
622                 fscanf(fp, "%d\n", &verts);
623                 /* fake read */
624                 for(a=0;a<verts;a++) {
625                         fscanf(fp, "%f %f %f", &ftemp, &ftemp, &ftemp);
626                 }
627                 
628                 a= me->totface;
629                 mface= me->mface;
630                 while(a--) {
631                         end= fscanf(fp,"%d", &poly);
632                         if(end<=0) break;
633         
634                         if(poly==3 || poly==4) {
635                                 fscanf(fp,"%d", &nr);
636                                 mface->v1= MIN2(nr, me->totvert-1);
637                                 fscanf(fp,"%d", &nr);
638                                 mface->v2= MIN2(nr, me->totvert-1);
639                                 fscanf(fp,"%d", &nr);
640                                 mface->v3= MIN2(nr, me->totvert-1);
641                                 if(poly==4) {
642                                         if( fscanf(fp,"%d", &nr) <=0 ) break;
643                                         mface->v4= MIN2(nr, me->totvert-1);
644                                 }
645                                 
646                                 test_index_face(mface, NULL, 0, poly);
647                                 
648                                 mface++;
649                         }
650                         else {
651                                 if( fscanf(fp,"%d", &nr0) <=0) break;
652                                 first= nr0;
653                                 for(b=1; b<poly; b++) {
654                                         end= fscanf(fp,"%d", &nr);
655                                         if(end<=0) break;
656                                         nr= MIN2(nr, me->totvert-1);
657                                         mface->v1= nr;
658                                         mface->v2= nr0;
659                                         nr0= nr;
660                                         mface++;
661                                         a--;
662                                 }
663                                 mface->v1= first;
664                                 mface->v2= nr;
665                                 mface++;
666                                 if(end<=0) break;
667                         }
668                         end= fscanf(fp,"%i", &col);
669                         col &= 0xF0F0F0;
670                         if(end<=0) break;
671                         
672                         for(b=0; b<totcol; b++) {
673                                 if(color[b]==col) {
674                                         (mface-1)->mat_nr= b;
675                                         break;
676                                 }
677                         }
678                 }
679         }
680         
681         fclose(fp);
682         MEM_freeN(vertdata);
683         
684         mesh_add_normals_flags(me);
685         make_edges(me, 0);
686
687         //XXX waitcursor(1);
688 }
689
690 static void read_radiogour(char *str)
691 {
692         Object *ob;
693         Mesh *me;
694         MVert *mvert;
695         MFace *mface;
696         FILE *fp;
697         float *vertdata, *vd, min[3], max[3], cent[3], ftemp;
698         unsigned int *colv, *colf, *colvertdata;
699         int  itemp, a, b, verts, tottria=0, totquad=0, totedge=0, poly, nr0, nr, first;
700         int end;
701         char s[50];
702         
703         fp= fopen(str, "rb");
704         if(fp==NULL) {
705                 //XXX error("Can't read file");
706                 return;
707         }
708         
709         fscanf(fp, "%40s", s);
710         
711         fscanf(fp, "%d\n", &verts);
712         if(verts<=0) {
713                 fclose(fp);
714                 //XXX error("Read error");
715                 return;
716         }
717         
718         if(verts>MESH_MAX_VERTS) {
719                 //XXX error("too many vertices");
720                 fclose(fp);
721                 return;
722         }
723         
724         INIT_MINMAX(min, max);
725         vd= vertdata= MEM_mallocN(sizeof(float)*3*verts, "videoscapelezer");
726         colv= colvertdata= MEM_mallocN(verts*sizeof(float), "coldata");
727         
728         for(a=0; a<verts; a++) {
729                 fscanf(fp, "%f %f %f %i", vd, vd+1, vd+2, colv);
730                 DO_MINMAX(vd, min, max);
731                 vd+=3;
732                 colv++;
733         }
734         
735         /* count faces */
736         end= 1;
737         while(end>0) {
738                 end= fscanf(fp,"%d", &poly);
739                 if(end<=0) break;
740         
741                 if(poly==3) tottria++;
742                 else if(poly==4) totquad++;
743                 else totedge+= poly;
744         
745                 for(a=0;a<poly;a++) {
746                         end= fscanf(fp,"%d", &nr);
747                         if(end<=0) break;
748                 }
749                 if(end<=0) break;
750                 
751         }
752         
753         if(totedge+tottria+totquad>MESH_MAX_VERTS) {
754                 printf(" var1: %d, var2: %d, var3: %d \n", totedge, tottria, totquad);
755                 //XXX error("too many faces");
756                 MEM_freeN(vertdata);
757                 MEM_freeN(colvertdata);
758                 fclose(fp);
759                 return;
760         }
761         
762         /* new object */
763         ob= add_object(OB_MESH);
764         me= ob->data;
765         me->totvert= verts;
766         me->totface= totedge+tottria+totquad;
767         me->flag= 0;
768
769         me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC,
770                                         NULL, me->totvert);
771         me->mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC,
772                                         NULL, me->totface);
773         
774         /* verts */
775         
776         cent[0]= (min[0]+max[0])/2.0f;
777         cent[1]= (min[1]+max[1])/2.0f;
778         cent[2]= (min[2]+max[2])/2.0f;
779         VECCOPY(ob->loc, cent);
780         
781         a= me->totvert;
782         vd= vertdata;
783         mvert= me->mvert;
784         while(a--) {
785                 VecSubf(mvert->co, vd, cent);
786                 mvert++;
787                 vd+= 3;
788         }
789         
790         /* faces */
791         if(me->totface) {
792                 rewind(fp);
793         
794                 fscanf(fp, "%40s", s);
795                 fscanf(fp, "%d\n", &verts);
796                 for(a=0;a<verts;a++) {
797                         fscanf(fp, "%f %f %f %i", &ftemp, &ftemp, &ftemp, &itemp);
798                 }
799                 
800                 a= me->totface;
801                 mface= me->mface;
802                 while(a--) {
803                         end= fscanf(fp,"%d", &poly);
804                         if(end<=0) break;
805         
806                         if(poly==3 || poly==4) {
807                                 fscanf(fp,"%d", &nr);
808                                 mface->v1= MIN2(nr, me->totvert-1);
809                                 fscanf(fp,"%d", &nr);
810                                 mface->v2= MIN2(nr, me->totvert-1);
811                                 fscanf(fp,"%d", &nr);
812                                 mface->v3= MIN2(nr, me->totvert-1);
813                                 if(poly==4) {
814                                         if( fscanf(fp,"%d", &nr) <=0 ) break;
815                                         mface->v4= MIN2(nr, me->totvert-1);
816                                 }
817                                 
818                                 test_index_face(mface, NULL, 0, poly);
819                                 
820                                 mface++;
821                         }
822                         else {
823                                 if( fscanf(fp,"%d", &nr0) <=0) break;
824                                 first= nr0;
825                                 for(b=1; b<poly; b++) {
826                                         end= fscanf(fp,"%d", &nr);
827                                         if(end<=0) break;
828                                         nr= MIN2(nr, me->totvert-1);
829                                         mface->v1= nr;
830                                         mface->v2= nr0;
831                                         nr0= nr;
832                                         mface++;
833                                         a--;
834                                 }
835                                 mface->v1= first;
836                                 mface->v2= nr;
837                                 mface->flag= ME_SMOOTH;
838                                 
839                                 mface++;
840                                 if(end<=0) break;
841                         }
842                 }
843                 
844                 /* mcol is 4 colors per face */
845                 me->mcol= MEM_mallocN(4*sizeof(int)*me->totface, "mcol");
846                 colf= (unsigned int *)me->mcol;
847
848                 a= me->totface;
849                 mface= me->mface;
850                 while(a--) {
851                         
852                         colf[0]= colvertdata[mface->v1];
853                         colf[1]= colvertdata[mface->v2];
854                         colf[2]= colvertdata[mface->v3];
855                         colf[3]= colvertdata[mface->v4];
856                         
857                         colf+= 4;
858                         mface++;
859                 }
860                 
861                 MEM_freeN(colvertdata);
862         }
863         
864         fclose(fp);
865         MEM_freeN(vertdata);
866         
867         mesh_add_normals_flags(me);
868         make_edges(me, 0);
869
870         //XXX waitcursor(1);
871 }
872
873
874 static void read_videoscape_lamp(char *str)
875 {
876         Object *ob;
877         Lamp *la;
878         FILE *fp;
879         float vec[3], q1[4];
880         int tot, val;
881         char s[50];
882         
883         fp= fopen(str, "rb");
884         if(fp==NULL) {
885                 //XXX error("Can't read file");
886                 return;
887         }
888
889         fscanf(fp, "%40s", s);
890         fscanf(fp, "%d\n", &tot);
891         
892         while(tot--) {
893                 ob= add_object(OB_LAMP);
894                 la= ob->data;
895                 
896                 fscanf(fp, "%d\n", &val);
897                 la->type= val;
898                 if(la->type==1) la->type= LA_SPOT;
899                 else if(la->type==2) la->type= LA_SUN;
900                 
901                 fscanf(fp, "%f %f\n", &la->spotsize, &la->spotblend);
902                 
903                 fscanf(fp, "%f %f %f %f\n", &la->r, &la->g, &la->b, &la->energy);               
904                 
905                 fscanf(fp, "%f %f %f\n", ob->loc, ob->loc+1, ob->loc+2);
906                 val= fscanf(fp, "%f %f %f\n", vec, vec+1, vec+2);
907                 vectoquat(vec, 5, 2, q1);
908                 QuatToEul(q1, ob->rot);
909                 
910                 if(val<=0) break;
911                 
912         }
913         fclose(fp);
914 }
915
916 static void read_videoscape_nurbs(char *str)
917 {
918         Object *ob;
919         Curve *cu;
920         Nurb *nu;
921         BezTriple *bezt;
922         BPoint *bp;
923         FILE *fp;
924         float tmat[4][4], omat[3][3], imat[3][3], mat[3][3];
925         int a, tot, type, val;
926         char s[50];
927
928         fp= fopen(str, "rb");
929         if(fp==NULL) {
930                 //XXX error("Can't read file");
931                 return;
932         }
933
934         fscanf(fp, "%40s", s);
935         fscanf(fp, "%d\n", &type);
936         
937         if(type==5) ob= add_object(OB_SURF);
938         else ob= add_object(OB_CURVE);
939         cu= ob->data;
940         
941         fscanf(fp, "%d\n", &tot);
942         fscanf(fp, "%d %d\n", &type, &val);
943         
944         cu->ext1= 0.002f*type;
945         cu->ext2= 0.002f*val;
946
947         for(a=0; a<4; a++) fscanf(fp, "%e %e %e %e\n", tmat[a], tmat[a]+1, tmat[a]+2, tmat[a]+3);
948
949         VECCOPY(ob->loc, tmat[3]);
950
951         Mat3CpyMat4(omat, tmat);
952         Mat3ToEul(omat, ob->rot);
953         EulToMat3(ob->rot, mat);
954         Mat3Inv(imat, mat);
955         Mat3MulMat3((float ( * )[3])tmat, imat, omat);
956         
957         while(tot--) {
958                 nu= (Nurb*)MEM_callocN(sizeof(Nurb),"nu from exotic");
959                 BLI_addtail(&cu->nurb, nu);
960                 
961                 fscanf(fp, "%d\n", &type);
962                 nu->type= type;
963
964                 fscanf(fp, "%d %d\n", &type, &val);
965                 nu->pntsu= type; nu->pntsv= val;
966                 fscanf(fp, "%d %d\n", &type, &val);
967                 nu->resolu= type; nu->resolv= val;
968                 fscanf(fp, "%d %d\n", &type, &val);
969                 nu->orderu= type; nu->orderv= val;
970                 fscanf(fp, "%d %d\n", &type, &val);
971                 nu->flagu= type; nu->flagv= val;
972                 
973                 if( (nu->type & 7)==CU_BEZIER) {
974                         a= nu->pntsu;
975                         nu->bezt= bezt= MEM_callocN(a*sizeof(BezTriple), "bezt from exotic");
976                         while(a--) {
977                                 fscanf(fp, "%f %f %f ", bezt->vec[0], bezt->vec[0]+1, bezt->vec[0]+2);
978                                 Mat4MulVecfl(tmat, bezt->vec[0]);
979                                 fscanf(fp, "%f %f %f ", bezt->vec[1], bezt->vec[1]+1, bezt->vec[1]+2);
980                                 Mat4MulVecfl(tmat, bezt->vec[1]);
981                                 fscanf(fp, "%f %f %f ", bezt->vec[2], bezt->vec[2]+1, bezt->vec[2]+2);
982                                 Mat4MulVecfl(tmat, bezt->vec[2]);
983                                 fscanf(fp, "%d %d\n", &type, &val);
984                                 bezt->h1= type;
985                                 bezt->h2= val;
986                                 bezt++;
987                         }
988                 }
989                 else {
990                         a= nu->pntsu*nu->pntsv;
991                         if(a) {
992                                 nu->bp= bp= MEM_callocN(a*sizeof(BPoint), "bp from exotic");
993                                 while(a--) {
994                                         fscanf(fp, "%f %f %f %f\n", bp->vec, bp->vec+1, bp->vec+2, bp->vec+3);
995                                         Mat4MulVecfl(tmat, bp->vec);
996                                         bp++;
997                                 }
998                                 
999                                 val= KNOTSU(nu);
1000                                 nu->knotsu= MEM_mallocN(sizeof(float)*val, "knots");
1001                                 for(a=0; a<val; a++) fscanf(fp, "%f\n", nu->knotsu+a);
1002                                 
1003                                 if(nu->pntsv>1) {
1004                                         val= KNOTSV(nu);
1005                                         nu->knotsv= MEM_mallocN(sizeof(float)*val, "knots");
1006                                         for(a=0; a<val; a++) fscanf(fp, "%f\n", nu->knotsv+a);
1007                                 }
1008                         }
1009                         else {
1010                                 BLI_remlink(&cu->nurb, nu);
1011                                 MEM_freeN(nu);
1012                         }
1013                 }
1014         }
1015         fclose(fp);
1016 }
1017
1018 static void read_videoscape(char *str)
1019 {
1020         int file, type;
1021         unsigned int val;
1022         unsigned short numlen;
1023         char name[FILE_MAXDIR+FILE_MAXFILE], head[FILE_MAXDIR+FILE_MAXFILE], tail[FILE_MAXFILE];
1024         
1025         strcpy(name, str);
1026
1027         while( TRUE ) {
1028                 file= open(name, O_BINARY|O_RDONLY);
1029                 if(file<=0) break;
1030                 else {
1031                         read(file, &type, 4);
1032                         close(file);
1033                         
1034                         if(type==DDG1) read_videoscape_mesh(name);
1035                         else if(type==DDG2) read_videoscape_lamp(name);
1036                         else if(type==DDG3) read_videoscape_nurbs(name);
1037                 }
1038
1039                 val = BLI_stringdec(name, head, tail, &numlen);
1040                 BLI_stringenc(name, head, tail, numlen, val + 1);
1041
1042         }
1043 }
1044
1045
1046 /* ***************** INVENTOR ******************* */
1047
1048
1049 #define IV_MAXSTACK 3000000
1050 #define IV_MAXFIELD 10
1051 #define IV_MAXCOL 16
1052
1053 static float *iv_data_stack;
1054 static float ivcolors[IV_MAXCOL][3];
1055 static Object *ivsurf;
1056 static ListBase ivbase;
1057
1058 struct IvNode {
1059         struct IvNode *next, *prev;
1060         char *nodename;
1061         char *fieldname[IV_MAXFIELD];
1062         int datalen[IV_MAXFIELD];
1063         float *data[IV_MAXFIELD];
1064 };
1065
1066 static int iv_curcol=0;
1067
1068 static int iv_colornumber(struct IvNode *iv)
1069 {
1070         float *fp, fr = 0.0, fg = 0.0, fb = 0.0;
1071         int a;
1072         char *cp;
1073         
1074         /* search back to last material */
1075         while(iv) {
1076                 if( strcmp(iv->nodename, "Material")==0) {
1077                         fp= iv->data[0];
1078                         if(fp==0) fp= iv->data[1];
1079                         if(fp) {
1080                                 fr= fp[0];
1081                                 fg= fp[1];
1082                                 fb= fp[2];
1083                         }
1084                         break;
1085                 }
1086                 else if( strcmp(iv->nodename, "BaseColor")==0) {
1087                         fp= iv->data[0];
1088                         fr= fp[0];
1089                         fg= fp[1];
1090                         fb= fp[2];
1091                         break;
1092                 }
1093                 else if( strcmp(iv->nodename, "PackedColor")==0) {
1094                         cp= (char *)iv->data[0];
1095                         fr= cp[3]/255.0f;
1096                         fg= cp[2]/255.0f;
1097                         fb= cp[1]/255.0f;
1098                         break;
1099                 }
1100                 iv= iv->prev;
1101                 
1102         }
1103         if(iv==0) return 0;
1104         if(iv->datalen[0]<3) return 0;
1105         
1106         for(a=0; a<iv_curcol; a++) {
1107         
1108                 if(ivcolors[a][0]== fr)
1109                         if(ivcolors[a][1]== fg)
1110                                 if(ivcolors[a][2]== fb) return a+1
1111                                 ;
1112         }
1113         
1114         if(a>=IV_MAXCOL) a= IV_MAXCOL-1;
1115         iv_curcol= a+1;
1116         ivcolors[a][0]= fr;
1117         ivcolors[a][1]= fg;
1118         ivcolors[a][2]= fb;
1119         
1120         return iv_curcol;
1121 }
1122
1123 static int iv_finddata(struct IvNode *iv, char *field, int fieldnr)
1124 {
1125         /* search for "field", count data size and make datablock. return skipdata */
1126         float *fp;
1127         int len, stackcount, skipdata=0;
1128         char *cpa, terminator, str[64];
1129         long i;
1130         
1131         len= strlen(field);
1132
1133         cpa= iv->nodename+1;
1134         while( *cpa != '}' ) {
1135                 
1136                 if( *cpa == *field ) {
1137                         if( strncmp(cpa, field, len)==0 ) {
1138                                 iv->fieldname[fieldnr]= cpa;
1139                                 
1140                                 /* read until first character */
1141                                 cpa+= len;
1142                                 skipdata+= len;
1143                                 *cpa= 0;
1144                                 cpa++;
1145                                 skipdata++;
1146                                 
1147                                 while( *cpa==32 || *cpa==13 || *cpa==10 || *cpa==9) cpa++;
1148                                 if( *cpa=='[' ) {
1149                                         terminator= ']';
1150                                         cpa++;
1151                                         skipdata++;
1152                                 }
1153                                 else terminator= 13;
1154                                 
1155                                 stackcount= 0;
1156                                 fp= iv_data_stack;
1157                                 
1158                                 while( *cpa!=terminator && *cpa != '}' ) {
1159                                         
1160                                         /* in fact, isdigit should include the dot and minus */
1161                                         if( (isdigit(*cpa) || *cpa=='.' || *cpa=='-') && (isspace(cpa[-1]) || cpa[-1]==0 || cpa[-1]==',') ) {
1162                                                 if(cpa[1]=='x') {
1163                                                         memcpy(str, cpa, 16);
1164                                                         str[16]= 0;
1165                                                         
1166                                                         sscanf(str, "%x", (int *)fp);
1167                                                 }
1168                                                 else {
1169                                                         /* atof doesn't stop after the first float
1170                                                          * in a long string at Windows... so we copy 
1171                                                          * the float to a new string then atof... */
1172                                                         char *cpa_temp = strpbrk(cpa, ", \n");
1173                                                         i = cpa_temp - cpa;
1174                                                         
1175                                                         if (i>63) *fp= 0.0;
1176                                                         else {
1177                                                                 memcpy(str, cpa, i);
1178                                                                 str[i]=0;
1179                                                         
1180                                                                 *fp= (float) atof(str);
1181                                                         }
1182                                                 }
1183                                                                                                 
1184                                                 stackcount++;
1185                                                 if(stackcount>=IV_MAXSTACK) {
1186                                                         printf("stackoverflow in IV read\n");
1187                                                         break;
1188                                                 }
1189                                                 fp++;
1190                                         }
1191                                         cpa++;
1192                                         skipdata++;
1193                                 }
1194                                 
1195                                 iv->datalen[fieldnr]= stackcount;
1196                                 if(stackcount) {
1197                                         iv->data[fieldnr]= MEM_mallocN(sizeof(float)*stackcount, "iv_finddata");
1198                                         memcpy(iv->data[fieldnr], iv_data_stack, sizeof(float)*stackcount);
1199                                 }
1200                                 else iv->data[fieldnr]= 0;
1201                                 
1202                                 return skipdata;
1203                         }
1204                 }
1205                 cpa++;
1206                 skipdata++;
1207         }
1208         
1209         return skipdata;
1210 }
1211
1212 static void read_iv_index(float *data, float *baseadr, float *index, int nr, int coordtype)
1213 {
1214         /* write in data: baseadr with offset index (and number nr) */
1215         float *fp;
1216         int ofs;
1217         
1218         while(nr--) {
1219                 ofs= (int) *index;
1220                 fp= baseadr+coordtype*ofs;
1221                 VECCOPY(data, fp);
1222                 data+= 3;
1223                 index++;
1224         }
1225 }
1226
1227
1228
1229 static void read_inventor(char *str, struct ListBase *listb)
1230 {
1231         struct IvNode *iv, *ivp, *ivn;
1232         char *maindata, *md, *cpa;
1233         float *index, *data, *fp;
1234         int file, filelen, count, lll, face, nr = 0;
1235         int skipdata, ok, a, b, tot, first, colnr, coordtype, polytype, *idata;
1236         struct DispList *dl;
1237         
1238         ivbase.first= ivbase.last= 0;
1239         iv_curcol= 0;
1240         ivsurf= 0;
1241         
1242         file= open(str, O_BINARY|O_RDONLY);
1243         if(file== -1) {
1244                 //XXX error("Can't read file\n");
1245                 return;
1246         }
1247
1248         filelen= BLI_filesize(file);
1249         if(filelen < 1) {
1250                 close(file);
1251                 return;
1252         }
1253         
1254         maindata= MEM_mallocN(filelen, "leesInventor");
1255         read(file, maindata, filelen);
1256         close(file);
1257
1258         iv_data_stack= MEM_mallocN(sizeof(float)*IV_MAXSTACK, "ivstack");
1259
1260         /* preprocess: remove comments */
1261         md= maindata+20;
1262         count= 20;
1263         while(count<filelen) {
1264                 if( *md=='#' ) {        /* comment */
1265                         while( *md!=13 && *md!=10) {    /* enters */
1266                                 *md= 32;
1267                                 md++;
1268                                 count++;
1269                                 if(count>=filelen) break;
1270                         }
1271                 }
1272                 md++;
1273                 count++;        
1274         }
1275         
1276
1277         /* now time to collect: which are the nodes and fields? */
1278         md= maindata;
1279         count= 0;
1280         while(count<filelen) {
1281                 if( *md=='{' ) {        /* read back */
1282                 
1283                         cpa= md-1;
1284                         while( *cpa==32 || *cpa==13 || *cpa==10 || *cpa==9) {   /* remove spaces/enters/tab  */
1285                                 *cpa= 0;
1286                                 cpa--;
1287                         }               
1288                                 
1289                         while( *cpa>32 && *cpa<128) cpa--;
1290                         cpa++;
1291                         *md= 0;
1292                         
1293                         ok= 0;
1294                         skipdata= 0;
1295                         iv= MEM_callocN(sizeof(struct IvNode), "leesInventor");
1296                         iv->nodename= cpa;
1297
1298                         if(strcmp(cpa, "Coordinate3")==0 || strcmp(cpa, "Coordinate4")==0) {
1299                                 skipdata= iv_finddata(iv, "point", 0);
1300                                 ok= 1;
1301                         }
1302                         else if(strcmp(cpa, "VertexProperty")==0) {
1303                                 skipdata= iv_finddata(iv, "vertex", 0);
1304                                 ok= 1;
1305                         }
1306                         else if(strcmp(cpa, "IndexedLineSet")==0) {
1307                                 skipdata= iv_finddata(iv, "coordIndex", 0);
1308                                 ok= 1;
1309                         }
1310                         else if(strcmp(cpa, "IndexedTriangleMesh")==0) {
1311                                 skipdata= iv_finddata(iv, "coordIndex", 0);
1312                                 ok= 1;
1313                         }
1314                         else if(strcmp(cpa, "IndexedFaceSet")==0) {
1315                                 skipdata= iv_finddata(iv, "coordIndex", 0);
1316                                 ok= 1;
1317                         }
1318                         else if(strcmp(cpa, "FaceSet")==0) {
1319                                 skipdata= iv_finddata(iv, "numVertices", 0);
1320                                 ok= 1;
1321                         }
1322                         else if(strcmp(cpa, "Material")==0) {
1323                                 iv_finddata(iv, "diffuseColor", 0);
1324                                 iv_finddata(iv, "ambientColor", 1);
1325                                 ok= 1;
1326                         }
1327                         else if(strcmp(cpa, "BaseColor")==0) {
1328                                 iv_finddata(iv, "rgb", 0);
1329                                 ok= 1;
1330                         }
1331                         else if(strcmp(cpa, "PackedColor")==0) {
1332                                 iv_finddata(iv, "rgba", 0);
1333                                 ok= 1;
1334                         }
1335                         else if(strcmp(cpa, "QuadMesh")==0) {
1336                                 iv_finddata(iv, "verticesPerColumn", 0);
1337                                 iv_finddata(iv, "verticesPerRow", 1);
1338                                 
1339                                 ok= 1;
1340                         }
1341                         else if(strcmp(cpa, "IndexedTriangleStripSet")==0) {
1342                                 skipdata= iv_finddata(iv, "coordIndex", 0);
1343                                 ok= 1;
1344                         }
1345                         else if(strcmp(cpa, "TriangleStripSet")==0) {
1346                                 skipdata= iv_finddata(iv, "numVertices", 0);
1347                                 ok= 1;
1348                         }
1349                         else if(strcmp(cpa, "IndexedNurbsSurface")==0 || strcmp(cpa, "NurbsSurface")==0) {
1350                                 iv_finddata(iv, "numUControlPoints", 0);
1351                                 iv_finddata(iv, "numVControlPoints", 1);
1352                                 iv_finddata(iv, "uKnotVector", 2);
1353                                 iv_finddata(iv, "vKnotVector", 3);
1354                                 ok= 1;
1355                         }
1356                         else {
1357                                 /* to the end */
1358                                 while( *md != '}') {
1359                                         md++;
1360                                         count++;
1361                                         if(count<filelen) break;
1362                                 }
1363                         }
1364                         
1365                         
1366                         if(ok) {
1367                                 BLI_addtail(&ivbase, iv);
1368                                 md+= skipdata;
1369                                 count+= skipdata;
1370                         }
1371                         else MEM_freeN(iv);
1372                         
1373                 }
1374                 md++;
1375                 count++;
1376         }
1377         
1378         /* join nodes */
1379         iv= ivbase.first;
1380         
1381         while(iv) {
1382                 ivn= iv->next;
1383                 
1384                 if( strncmp(iv->nodename, "Indexed", 7)==0) {
1385                         /* seek back: same name? */
1386                         
1387                         ivp= iv->prev;
1388                         while(ivp) {
1389                                 if(strcmp(iv->nodename, ivp->nodename)==0) break;
1390
1391                                 if(strcmp(ivp->nodename, "Coordinate3")==0 || 
1392                                    strcmp(ivp->nodename, "Coordinate4")==0 ||
1393                                    strcmp(ivp->nodename, "VertexProperty")==0) {
1394                                         ivp= 0;
1395                                         break;
1396                                 }
1397                                 ivp= ivp->prev;
1398                         }
1399                         
1400                         if(ivp) {
1401                                 /* add iv to ivp */
1402                                 
1403                                 tot= iv->datalen[0] + ivp->datalen[0];
1404                                 if(tot) {
1405                                         data= MEM_mallocN(tot*sizeof(float), "samenvoeg iv");
1406                                         memcpy(data, ivp->data[0], sizeof(float)*ivp->datalen[0]);
1407                                         memcpy(data+ivp->datalen[0], iv->data[0], sizeof(float)*iv->datalen[0]);
1408                                         
1409                                         ivp->datalen[0]+= iv->datalen[0];
1410                                         MEM_freeN(ivp->data[0]);
1411                                         ivp->data[0]= data;
1412                                         
1413                                         BLI_remlink(&ivbase, iv);
1414                                         MEM_freeN(iv->data[0]);
1415                                         MEM_freeN(iv);
1416                                 }
1417                         }
1418                 }
1419                 
1420                 iv= ivn;
1421         }
1422
1423         
1424         /* convert Nodes to DispLists */
1425         iv= ivbase.first;
1426         while(iv) {
1427                 
1428                 /* printf(" Node: %s\n", iv->nodename); */
1429                 /* if(iv->fieldname[0]) printf(" Field: %s len %d\n", iv->fieldname[0], iv->datalen[0]); */
1430                 coordtype= 3;
1431                 
1432                 if( strcmp(iv->nodename, "IndexedLineSet")==0 ) {
1433                         
1434                         colnr= iv_colornumber(iv);
1435
1436                         /* seek back to data */
1437                         ivp= iv;
1438                         while(ivp->prev) {
1439                                 ivp= ivp->prev;
1440                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1441                                         coordtype= 3;
1442                                         break;
1443                                 }
1444                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1445                                         coordtype= 4;
1446                                         break;
1447                                 }
1448                         }
1449                         if(ivp) {
1450                         
1451                                 /* count the nr of lines */
1452                                 tot= 0;
1453                                 index= iv->data[0];
1454                                 lll = iv->datalen[0]-1;
1455                                 for(a=0; a<lll; a++) {
1456                                         if(index[0]!= -1 && index[1]!= -1) tot++;
1457                                         index++;
1458                                 }
1459                                 
1460                                 tot*= 2;        /* nr of vertices */
1461                                 dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor1");
1462                                 BLI_addtail(listb, dl);
1463                                 dl->type= DL_SEGM;
1464                                 dl->nr= 2;
1465                                 dl->parts= tot/2;
1466                                 dl->col= colnr;
1467                                 data= (float *)(dl+1);
1468                                 
1469                                 index= iv->data[0];
1470                                 for(a=0; a<lll; a++) {
1471                                         if(index[0]!= -1 && index[1]!= -1) {
1472                                                 read_iv_index(data, ivp->data[0], index, 2, coordtype);
1473                                                 data+= 6;
1474                                         }
1475                                         index++;
1476                                 }
1477                         }
1478                 }
1479                 else if( strcmp(iv->nodename, "FaceSet")==0 ) {
1480                         
1481                         colnr= iv_colornumber(iv);
1482                 
1483                         /* seek back to data */
1484                         ivp= iv;
1485                         while(ivp->prev) {
1486                                 ivp= ivp->prev;
1487                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1488                                         coordtype= 3;
1489                                         break;
1490                                 }
1491                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1492                                         coordtype= 4;
1493                                         break;
1494                                 }
1495                         }
1496                         
1497                         if(ivp) {
1498                                 /* count triangles */
1499                                 tot= 0;
1500                                 
1501                                 index= iv->data[0];
1502                                 polytype= (int) index[0];
1503                                 
1504                                 for(a=0; a<iv->datalen[0]; a++) {
1505                                         if(index[0]== polytype) tot++;  /* one kind? */
1506                                         index++;
1507                                 }
1508                                 
1509                                 
1510                                 tot*= polytype;         /* nr of vertices */
1511                                 dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor4");
1512                                 BLI_addtail(listb, dl);
1513                                 dl->type= DL_POLY;
1514                                 dl->nr= polytype;
1515                                 dl->parts= tot/polytype;
1516                                 dl->col= colnr;
1517                                 data= (float *)(dl+1);
1518
1519                                 index= ivp->data[0];
1520                                 first= 1;
1521                                 for(a=0; a<iv->datalen[0]; a++) {
1522                                         
1523                                         VECCOPY(data, index);
1524                                         data+= 3;
1525                                         index+= 3;
1526
1527                                         VECCOPY(data, index);
1528                                         data+= 3;
1529                                         index+= 3;
1530
1531                                         VECCOPY(data, index);
1532                                         data+= 3;
1533                                         index+= 3;
1534
1535                                         if(polytype==4) {
1536                                                 VECCOPY(data, index);
1537                                                 data+= 3;
1538                                                 index+= 3;
1539                                         }
1540                                 }
1541                         }
1542                 }
1543                 else if( strcmp(iv->nodename, "TriangleStripSet")==0 ) {
1544                         
1545                         colnr= iv_colornumber(iv);
1546                 
1547                         /* seek back to data */
1548                         ivp= iv;
1549                         while(ivp->prev) {
1550                                 ivp= ivp->prev;
1551                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1552                                         coordtype= 3;
1553                                         break;
1554                                 }
1555                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1556                                         coordtype= 4;
1557                                         break;
1558                                 }
1559                         }
1560                         
1561                         if(ivp) {
1562                                 /* count triangles */
1563                                 tot= 0;
1564                                 face= 0;
1565                                 
1566                                 index= iv->data[0];             /* strip size */ 
1567                                 
1568                                 for(a=0; a<iv->datalen[0]; a++) {
1569                                         tot+= (int) index[0];
1570                                         face+= ((int) index[0]) - 2;
1571                                         index++;
1572                                 }
1573                                 
1574                                 dl= MEM_callocN(sizeof(struct DispList), "leesInventor4");
1575                                 dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1576                                 dl->index= MEM_callocN( face*3*sizeof(int), "dl index");
1577                                 
1578                                 dl->type= DL_INDEX3;
1579                                 dl->nr= tot;
1580                                 dl->parts= face;
1581
1582                                 BLI_addtail(listb, dl);
1583                                 dl->col= colnr;
1584
1585                                 index= iv->data[0];             /* strip size */ 
1586                                 fp= ivp->data[0];               /* vertices */
1587                                 data= dl->verts;
1588                                 idata= dl->index;
1589                                 first= 0;
1590                                 
1591                                 for(a=0; a<iv->datalen[0]; a++) {
1592                                         
1593                                         /* vertices */
1594                                         for(b=0; b<index[0]; b++) {
1595                                                 VECCOPY(data, fp);
1596                                                 data+= 3; 
1597                                                 fp+= coordtype;
1598                                         }
1599                                                 
1600                                         /* indices */
1601                                         lll = index[0] - 2;
1602                                         for(b=0; b<lll; b++) {
1603                                                 idata[0]= first;
1604                                                 idata[1]= first+1;
1605                                                 idata[2]= first+2;
1606                                                 first++;
1607                                                 idata+= 3;
1608                                         }
1609                                         first+= 2;
1610                                         
1611                                         index++;
1612                                 }
1613                         }
1614                 }
1615                 else if( strcmp(iv->nodename, "IndexedFaceSet")==0 ) {
1616                         
1617                         colnr= iv_colornumber(iv);
1618                 
1619                         /* seek back to data */
1620                         ivp= iv;
1621                         while(ivp->prev) {
1622                                 ivp= ivp->prev;
1623                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1624                                         coordtype= 3;
1625                                         break;
1626                                 }
1627                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1628                                         coordtype= 4;
1629                                         break;
1630                                 }
1631                         }
1632                         if(ivp) {
1633                         
1634                                 /* count triangles */
1635                                 face= 0;
1636                                 index= iv->data[0];
1637                 lll = iv->datalen[0]-2;
1638                                 for(a=0; a<lll; a++) {
1639                                         if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) face++;
1640                                         index++;
1641                                 }
1642
1643                                 /*number of vertices */
1644                                 tot= ivp->datalen[0]/coordtype;
1645
1646                                 if(tot) {
1647                                         dl= MEM_callocN(sizeof(struct DispList), "leesInventor5");
1648                                         BLI_addtail(listb, dl);
1649                                         dl->type= DL_INDEX3;
1650                                         dl->nr= tot;
1651                                         dl->parts= face;
1652                                         dl->col= colnr;
1653         
1654                                         dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1655                                         dl->index= MEM_callocN(sizeof(int)*3*face, "dl index");
1656         
1657                                         /* vertices */
1658                                         fp= ivp->data[0];
1659                                         data= dl->verts;
1660                                         for(b=tot; b>0; b--) {
1661                                                 VECCOPY(data, fp);
1662                                                 data+= 3; 
1663                                                 fp+= coordtype;
1664                                         }
1665                                         
1666                                         /* indices */
1667                                         index= iv->data[0];
1668                                         idata= dl->index;
1669                                         first= 1;
1670                                         lll=iv->datalen[0]-2;
1671                                         for(a=0; a<lll; a++) {
1672                                                 
1673                                                 if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) {
1674         
1675                                                         /* this trick is to fill poly's with more than 3 vertices correctly */
1676                                                         if(first) {
1677                                                                 nr= (int) index[0];
1678                                                                 first= 0;
1679                                                         }
1680                                                         idata[0]= nr;
1681                                                         idata[1]= (int) index[1];
1682                                                         idata[2]= (int) index[2];
1683                                                         idata+= 3;
1684                                                 }
1685                                                 else first= 1;
1686                                                 
1687                                                 index++;
1688                                         }
1689                                 }
1690                         }
1691                 }
1692                 else if( strcmp(iv->nodename, "IndexedTriangleMesh")==0 || 
1693                                  strcmp(iv->nodename, "IndexedTriangleStripSet")==0 ) {
1694                         
1695                         colnr= iv_colornumber(iv);
1696                 
1697                         /* seek back to data */
1698                         ivp= iv;
1699                         while(ivp->prev) {
1700                                 ivp= ivp->prev;
1701                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1702                                         coordtype= 3;
1703                                         break;
1704                                 }
1705                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1706                                         coordtype= 4;
1707                                         break;
1708                                 }
1709                         }
1710                         if(ivp) {
1711                         
1712                                 /* count triangles */
1713                                 face= 0;
1714                                 index= iv->data[0];
1715                                 lll=iv->datalen[0]-2;
1716                                 for(a=0; a<lll; a++) {
1717                                         if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) face++;
1718                                         index++;
1719                                 }
1720                                 
1721                                 /* nr of vertices */
1722                                 tot= ivp->datalen[0]/coordtype;
1723                                 
1724                                 dl= MEM_callocN(sizeof(struct DispList), "leesInventor6");
1725                                 BLI_addtail(listb, dl);
1726                                 dl->type= DL_INDEX3;
1727                                 dl->nr= tot;
1728                                 dl->parts= face;
1729                                 dl->col= colnr;
1730                                 
1731                                 dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1732                                 dl->index= MEM_callocN(sizeof(int)*3*face, "dl index");
1733
1734                                 /* vertices */
1735                                 fp= ivp->data[0];
1736                                 data= dl->verts;
1737                                 for(b=tot; b>0; b--) {
1738                                         VECCOPY(data, fp);
1739                                         data+= 3; 
1740                                         fp+= coordtype;
1741                                 }
1742                                 
1743                                 /* indices */
1744                                 index= iv->data[0];
1745                                 idata= dl->index;
1746                                 
1747                                 lll=iv->datalen[0]-2;
1748                                 for(a=lll; a>0; a--) {
1749                                 
1750                                         if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) {
1751                                                 idata[0]= (int) index[0];
1752                                                 idata[1]= (int) index[1];
1753                                                 idata[2]= (int) index[2];
1754                                                 idata+= 3;
1755                                         }
1756                                         index++;
1757                                 }
1758                         }
1759                 }
1760                 else if( strcmp(iv->nodename, "QuadMesh")==0 ) {
1761                         
1762                         colnr= iv_colornumber(iv);
1763                 
1764                         /* seek back to data */
1765                         ivp= iv;
1766                         while(ivp->prev) {
1767                                 ivp= ivp->prev;
1768                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1769                                         coordtype= 3;
1770                                         break;
1771                                 }
1772                                 if( strcmp(ivp->nodename, "VertexProperty")==0 ) {
1773                                         coordtype= 3;
1774                                         break;
1775                                 }
1776                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1777                                         coordtype= 4;
1778                                         break;
1779                                 }
1780                         }
1781                         
1782                         if(ivp) {
1783                                 tot= (int) (floor(*(iv->data[0])+0.5) * floor(*(iv->data[1])+0.5));
1784
1785                                 if(tot>0) {
1786                                         dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor8");
1787                                         BLI_addtail(listb, dl);
1788                                         dl->type= DL_SURF;
1789                                         dl->parts= (int) floor(*(iv->data[0])+0.5);
1790                                         dl->nr= (int) floor(*(iv->data[1])+0.5);
1791                                         dl->col= colnr;
1792                                         data= (float *)(dl+1);
1793                                         memcpy(data, ivp->data[0], tot*3*sizeof(float));
1794                                 }
1795                         }
1796                 }
1797                 else if(strcmp(iv->nodename, "IndexedNurbsSurface")==0 || strcmp(iv->nodename, "NurbsSurface")==0) {
1798                         
1799                         colnr= iv_colornumber(iv);
1800                 
1801                         /* sek back to data */
1802                         ivp= iv;
1803                         while(ivp->prev) {
1804                                 ivp= ivp->prev;
1805                                 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1806                                         coordtype= 3;
1807                                         break;
1808                                 }
1809                                 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1810                                         coordtype= 4;
1811                                         break;
1812                                 }
1813                         }
1814                         if(ivp) {
1815                                 a= (int) *(iv->data[0]);
1816                                 b= (int) *(iv->data[1]);
1817                                 
1818                                 tot= a*b;
1819
1820                                 if( (a>=4 || b>=4) && tot>6) {
1821                                         Object *ob;
1822                                         Curve *cu;
1823                                         Nurb *nu;
1824                                         BPoint *bp;
1825                                         
1826                                         if(ivsurf==0) {
1827                                                 ob= add_object(OB_SURF);
1828                                                 ivsurf= ob;
1829                                         }
1830                                         else ob= ivsurf;
1831                                         cu= ob->data;
1832                                         nu = (Nurb*) MEM_callocN(sizeof(Nurb),"addNurbprim") ;
1833                                         BLI_addtail(&cu->nurb, nu);
1834                                         nu->type= CU_NURBS;
1835
1836                                         nu->pntsu= a;
1837                                         nu->pntsv= b;
1838                                         nu->resolu= 2*a;
1839                                         nu->resolv= 2*b;
1840
1841                                         nu->flagu= 0;
1842                                         nu->flagv= 0;
1843                                         
1844                                         nu->bp = bp =
1845                                                 (BPoint*)MEM_callocN(tot * sizeof(BPoint), "addNurbprim3");
1846                                         a= tot;
1847                                         data= ivp->data[0];
1848                                         while(a--) {
1849                                                 VECCOPY(bp->vec, data);
1850                                                 if(coordtype==4) {
1851                                                         bp->vec[3]= data[3];
1852                                                         VecMulf(bp->vec, 1.0f/data[3]);
1853                                                 }
1854                                                 else bp->vec[3]= 1.0;
1855                                                 data+= coordtype;
1856                                                 bp++;
1857                                         }
1858                                         
1859                                         /* iv->datalen[2] / [3] is number of knots */
1860                                         nu->orderu= iv->datalen[2] - nu->pntsu;
1861                                         nu->orderv= iv->datalen[3] - nu->pntsv;
1862                                         
1863                                         nu->knotsu= MEM_mallocN( sizeof(float)*(iv->datalen[2]), "knots");
1864                                         memcpy(nu->knotsu, iv->data[2], sizeof(float)*(iv->datalen[2]));
1865                                         nu->knotsv= MEM_mallocN( sizeof(float)*(iv->datalen[3]), "knots");
1866                                         memcpy(nu->knotsv, iv->data[3], sizeof(float)*(iv->datalen[3]));                                        
1867
1868                                         switchdirectionNurb(nu);
1869
1870                                 }
1871                                 else {
1872                                         dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor3");
1873                                         BLI_addtail(listb, dl);
1874                                         dl->type= DL_SURF;
1875                                         dl->nr= (int) *(iv->data[0]);
1876                                         dl->parts= (int) *(iv->data[1]);
1877                                         dl->col= colnr;
1878                                         data= (float *)(dl+1);
1879                                         
1880                                         a= tot;
1881                                         fp= ivp->data[0];
1882                                         while(a--) {
1883                                                 VECCOPY(data, fp);
1884                                                 fp+= coordtype;
1885                                                 data+= 3;
1886                                         }
1887                                 }
1888                         }
1889                 }
1890                 iv= iv->next;
1891         }
1892
1893         /* free */
1894         iv= ivbase.first;
1895         while(iv) {
1896                 for(a=0; a<IV_MAXFIELD; a++) {
1897                         if(iv->data[a]) MEM_freeN(iv->data[a]);
1898                 }
1899                 iv= iv->next;
1900         }
1901
1902         BLI_freelistN(&ivbase);
1903         MEM_freeN(maindata);
1904         MEM_freeN(iv_data_stack);
1905         
1906 }
1907
1908 /* ************************************************************ */
1909
1910 static void displist_to_mesh(DispList *dlfirst)
1911 {
1912         Object *ob;
1913         Mesh *me;
1914         Material *ma;
1915         DispList *dl;
1916         MVert *mvert;
1917         MFace *mface;
1918         float *data, vec[3], min[3], max[3];
1919         int a, b, startve, *idata, totedge=0, tottria=0, totquad=0, totvert=0, totface, totcol=0, colnr;
1920         int p1, p2, p3, p4;
1921         unsigned int maxvertidx;
1922
1923         /* count first */
1924         INIT_MINMAX(min, max);
1925
1926         dl= dlfirst;
1927         while(dl) {
1928         
1929                 /* PATCH 1 (polyfill) can't be done, there's no listbase here. do that first! */
1930                 /* PATCH 2 */
1931                 if(dl->type==DL_SEGM && dl->nr>2) {
1932                         data= (float *)(dl+1);
1933                         if(data[0]==data[3*(dl->nr-1)]) {
1934                                 if(data[1]==data[3*(dl->nr-1)+1]) {
1935                                         if(data[2]==data[3*(dl->nr-1)+2]) {
1936                                                 dl->type= DL_POLY;
1937                                                 dl->nr--;
1938                                         }
1939                                 }
1940                         }
1941                 }
1942                 
1943                 /* colors */
1944                 if(dl->col > totcol) totcol= dl->col;
1945                 
1946                 /* size and count */
1947                 if(dl->type==DL_SURF) {
1948                         a= dl->nr;
1949                         b= dl->parts;
1950                         if(dl->flag & DL_CYCL_U) a++;
1951                         if(dl->flag & DL_CYCL_V) b++;
1952                         
1953                         totquad+= a*b;
1954
1955                         totvert+= dl->nr*dl->parts;
1956
1957                         data= (float *)(dl+1);
1958                         for(a= dl->nr*dl->parts; a>0; a--) {
1959                                 DO_MINMAX(data, min, max);
1960                                 data+= 3;
1961                         }
1962                 }
1963                 else if(dl->type==DL_POLY) {
1964                         if(dl->nr==3 || dl->nr==4) {
1965                                 if(dl->nr==3) tottria+= dl->parts;
1966                                 else totquad+= dl->parts;
1967                                 
1968                                 totvert+= dl->nr*dl->parts;
1969
1970                                 data= (float *)(dl+1);
1971                                 for(a= dl->nr*dl->parts; a>0; a--) {
1972                                         DO_MINMAX(data, min, max);
1973                                         data+= 3;
1974                                 }
1975                         }
1976                         else if(dl->nr>4) {
1977                                 
1978                                 tottria+= dl->nr*dl->parts;
1979                                 totvert+= dl->nr*dl->parts;
1980                                 
1981                                 data= (float *)(dl+1);
1982                                 for(a= dl->nr*dl->parts; a>0; a--) {
1983                                         DO_MINMAX(data, min, max);
1984                                         data+= 3;
1985                                 }
1986                                 
1987                         }
1988                 }
1989                 else if(dl->type==DL_INDEX3) {
1990                         tottria+= dl->parts;
1991                         totvert+= dl->nr;
1992                         
1993                         data= dl->verts;
1994                         for(a= dl->nr; a>0; a--) {
1995                                 DO_MINMAX(data, min, max);
1996                                 data+= 3;
1997                         }
1998                 }
1999                 else if(dl->type==DL_SEGM) {
2000                         
2001                         tottria+= (dl->nr-1)*dl->parts;
2002                         totvert+= dl->nr*dl->parts;
2003                         
2004                         data= (float *)(dl+1);
2005                         for(a= dl->nr*dl->parts; a>0; a--) {
2006                                 DO_MINMAX(data, min, max);
2007                                 data+= 3;
2008                         }
2009                 }
2010
2011                 dl= dl->next;
2012         }
2013
2014         if(totvert==0) {
2015                 return;
2016         }
2017         
2018         if(totcol>16) {
2019                 //XXX error("Found more than 16 different colors");
2020                 totcol= 16;
2021         }
2022
2023         vec[0]= (min[0]+max[0])/2;
2024         vec[1]= (min[1]+max[1])/2;
2025         vec[2]= (min[2]+max[2])/2;
2026
2027         ob= add_object(OB_MESH);
2028         VECCOPY(ob->loc, vec);
2029         where_is_object(ob);
2030
2031         me= ob->data;
2032         
2033         /* colors */
2034         if(totcol) {
2035                 ob->mat= MEM_callocN(sizeof(void *)*totcol, "ob->mat");
2036                 me->mat= MEM_callocN(sizeof(void *)*totcol, "me->mat");
2037                 me->totcol= totcol;
2038                 ob->totcol= (unsigned char) me->totcol;
2039                 ob->actcol= 1;
2040         }
2041         
2042         /* materials */
2043         for(a=0; a<totcol; a++) {
2044                 ma= G.main->mat.first;
2045                 while(ma) {
2046                         if(ma->mtex[0]==0) {
2047                                 if(ivcolors[a][0]==ma->r && ivcolors[a][1]==ma->g && ivcolors[a][2]==ma->b) {
2048                                         me->mat[a]= ma;
2049                                         ma->id.us++;
2050                                         break;
2051                                 }
2052                         }
2053                         ma= ma->id.next;
2054                 }
2055                 if(ma==0) {
2056                         ma= add_material("ext");
2057                         me->mat[a]= ma;
2058                         ma->r= ivcolors[a][0];
2059                         ma->g= ivcolors[a][1];
2060                         ma->b= ivcolors[a][2];
2061                         automatname(ma);
2062                 }
2063         }
2064         
2065         totface= totquad+tottria+totedge;
2066
2067         printf("Import: %d vertices %d faces\n", totvert, totface);
2068         
2069         me->totvert= totvert;
2070         me->totface= totface;
2071         me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC,
2072                                         NULL, me->totvert);
2073         me->mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC,
2074                                         NULL, me->totface);
2075         maxvertidx= totvert-1;
2076         
2077         mvert= me->mvert;
2078         mface= me->mface;
2079
2080         startve= 0;
2081
2082         dl= dlfirst;
2083         while(dl) {
2084                 
2085                 colnr= (dl->col>15 ? 15: dl->col);
2086                 if(colnr) colnr--;
2087                 
2088                 if(dl->type==DL_SURF) {
2089                         data= (float *)(dl+1);
2090
2091                         for(a=dl->parts*dl->nr; a>0; a--) {
2092                                 mvert->co[0]= data[0] -vec[0];
2093                                 mvert->co[1]= data[1] -vec[1];
2094                                 mvert->co[2]= data[2] -vec[2];
2095                                 
2096                                 data+=3;
2097                                 mvert++;
2098                         }
2099
2100                         for(a=0; a<dl->parts; a++) {
2101                                 
2102                                 if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
2103                                         break;
2104                                 
2105                                 p1+= startve; 
2106                                 p2+= startve; 
2107                                 p3+= startve; 
2108                                 p4+= startve;
2109
2110                                 for(; b<dl->nr; b++) {
2111                                 
2112                                         mface->v1= p1;
2113                                         mface->v2= p2;
2114                                         mface->v3= p4;
2115                                         mface->v4= p3;
2116                                         
2117                                         mface->mat_nr= colnr;
2118                                         test_index_face(mface, NULL, 0, 4);
2119                                         
2120                                         mface++;
2121                                         
2122                                         p4= p3; 
2123                                         p3++;
2124                                         p2= p1; 
2125                                         p1++;
2126                                 }
2127                         }
2128                         
2129                         startve += dl->parts*dl->nr;
2130
2131                 }
2132                 else if(dl->type==DL_POLY) {
2133                 
2134                         if(dl->nr==3 || dl->nr==4) {
2135                                 data= (float *)(dl+1);
2136
2137                                 for(a=dl->parts*dl->nr; a>0; a--) {
2138                                         mvert->co[0]= data[0] -vec[0];
2139                                         mvert->co[1]= data[1] -vec[1];
2140                                         mvert->co[2]= data[2] -vec[2];
2141                                         data+=3;
2142                                         mvert++;
2143                                 }
2144
2145                                 for(a=0; a<dl->parts; a++) {
2146                                         if(dl->nr==3) {
2147                                                 mface->v1= startve+a*dl->nr;
2148                                                 mface->v2= startve+a*dl->nr+1;
2149                                                 mface->v3= startve+a*dl->nr+2;
2150                                                 mface->mat_nr= colnr;
2151                                                 test_index_face(mface, NULL, 0, 3);
2152                                                 mface++;
2153                                         }
2154                                         else {
2155                                                 mface->v1= startve+a*dl->nr;
2156                                                 mface->v2= startve+a*dl->nr+1;
2157                                                 mface->v3= startve+a*dl->nr+2;
2158                                                 mface->v4= startve+a*dl->nr+3;
2159                                                 mface->mat_nr= colnr;
2160                                                 test_index_face(mface, NULL, 0, 4);
2161                                                 mface++;
2162                                         }
2163                                 }
2164                                 startve += dl->parts*dl->nr;
2165                         }
2166                         else if(dl->nr>4) {
2167                                 data= (float *)(dl+1);
2168
2169                                 for(a=dl->parts*dl->nr; a>0; a--) {
2170                                         mvert->co[0]= data[0] -vec[0];
2171                                         mvert->co[1]= data[1] -vec[1];
2172                                         mvert->co[2]= data[2] -vec[2];
2173                                         
2174                                         data+=3;
2175                                         mvert++;
2176                                 }
2177
2178                                 for(b=0; b<dl->parts; b++) {
2179                                         for(a=0; a<dl->nr; a++) {
2180                                                 mface->v1= startve+a;
2181                                                 
2182                                                 if(a==dl->nr-1) mface->v2= startve;
2183                                                 else mface->v2= startve+a+1;
2184                                                 
2185                                                 mface->mat_nr= colnr;
2186
2187                                                 mface++;
2188                                         }
2189                                         startve += dl->nr;
2190                                 }
2191                         }
2192                 }
2193                 else if(dl->type==DL_INDEX3) {
2194                         data= dl->verts;
2195                         
2196                         for(a=dl->nr; a>0; a--) {
2197                                 mvert->co[0]= data[0] -vec[0];
2198                                 mvert->co[1]= data[1] -vec[1];
2199                                 mvert->co[2]= data[2] -vec[2];
2200                                 data+=3;
2201                                 mvert++;
2202                         }
2203
2204                         idata= dl->index;
2205                         for(b=dl->parts; b>0; b--) {
2206                                 mface->v1= startve+idata[0];
2207                                 mface->v2= startve+idata[1];
2208                                 mface->v3= startve+idata[2];
2209                                 mface->mat_nr= colnr;
2210                                 
2211                                 if (mface->v1>maxvertidx) mface->v1= maxvertidx;
2212                                 if (mface->v2>maxvertidx) mface->v2= maxvertidx;
2213                                 if (mface->v3>maxvertidx) mface->v3= maxvertidx;
2214
2215                                 test_index_face(mface, NULL, 0, 3);
2216                                 mface++;
2217                                 idata+= 3;
2218                         }
2219                         startve += dl->nr;
2220                 }
2221                 else if(dl->type==DL_SEGM) {
2222                         data= (float *)(dl+1);
2223
2224                         for(a=dl->parts*dl->nr; a>0; a--) {
2225                                 mvert->co[0]= data[0] -vec[0];
2226                                 mvert->co[1]= data[1] -vec[1];
2227                                 mvert->co[2]= data[2] -vec[2];
2228                                 data+=3;
2229                                 mvert++;
2230                         }
2231
2232                         for(b=0; b<dl->parts; b++) {
2233                                 for(a=0; a<dl->nr-1; a++) {
2234                                         mface->v1= startve+a;
2235                                         mface->v2= startve+a+1;
2236                                         mface->mat_nr= colnr;
2237                                         mface++;
2238                                 }
2239                                 startve += dl->nr;
2240                         }
2241                 }
2242                 dl= dl->next;
2243         }
2244
2245         mesh_add_normals_flags(me);
2246         make_edges(me, 0);
2247 }
2248
2249 static void displist_to_objects(ListBase *lbase)
2250 {
2251         DispList *dl, *first, *prev, *next;
2252         ListBase tempbase;
2253         int maxaantal, curcol, totvert=0, vert;
2254         
2255         /* irst this: is still active */
2256         if(ivsurf) {
2257                 where_is_object(ivsurf);
2258 // XXX          docenter_new();
2259         }
2260
2261         dl= lbase->first;
2262         while(dl) {
2263                 next= dl->next;
2264                 
2265                 /* PATCH 1: polyfill */
2266                 if(dl->type==DL_POLY && dl->nr>4) {
2267                         /* solution: put them together in separate listbase */
2268                         ;
2269                 }
2270                 /* PATCH 2: poly's of 2 points */
2271                 if(dl->type==DL_POLY && dl->nr==2) dl->type= DL_SEGM;
2272                 
2273                 dl= next;
2274         }
2275
2276         /* count vertices */
2277
2278         dl= lbase->first;
2279         while(dl) {
2280
2281                 if(dl->type==DL_SURF) totvert+= dl->nr*dl->parts;
2282                 else if(dl->type==DL_POLY) {
2283                         if(dl->nr==3 || dl->nr==4) totvert+= dl->nr*dl->parts;
2284                         else if(dl->nr>4) totvert+= dl->nr*dl->parts;
2285                 }
2286                 else if(dl->type==DL_INDEX3) totvert+= dl->nr;
2287                 else if(dl->type==DL_SEGM) totvert+= dl->nr*dl->parts;
2288
2289                 dl= dl->next;
2290         }
2291
2292         if(totvert==0) {
2293                 
2294                 if(ivsurf==0) ; //XXX error("Found no data");
2295                 if(lbase->first) BLI_freelistN(lbase);
2296                 
2297                 return;
2298         }
2299
2300         maxaantal= 32000;
2301         
2302         if(totvert>maxaantal) {
2303         
2304                 /* try to put colors together */
2305                 curcol= 0;
2306                 tempbase.first= tempbase.last= 0;
2307
2308                 while(lbase->first) {
2309                         dl= lbase->first;
2310                         while(dl) {
2311                                 next= dl->next;
2312                                 if(dl->col==curcol) {
2313                                         BLI_remlink(lbase, dl);
2314                                         BLI_addtail(&tempbase, dl);
2315                                         dl->col= 0;
2316                                 }
2317                                 
2318                                 dl= next;
2319                         }
2320                         
2321                         /* in tempbase are all 'curcol' */
2322                         totvert= 0;
2323                         dl= first= tempbase.first;
2324                         while(dl) {
2325                                 vert= 0;
2326                                 
2327                                 if(dl->type==DL_SURF) vert= dl->nr*dl->parts;
2328                                 else if(dl->type==DL_POLY) {
2329                                         if(dl->nr==3 || dl->nr==4) vert= dl->nr*dl->parts;
2330                                         else if(dl->nr>4) vert= dl->nr*dl->parts;
2331                                 }
2332                                 else if(dl->type==DL_INDEX3) totvert+= dl->nr;
2333                                 else if(dl->type==DL_SEGM) vert= dl->nr*dl->parts;
2334                                 
2335                                 totvert+= vert;
2336                                 if(totvert > maxaantal || dl->next==0) {
2337                                         if(dl->next==0) {
2338                                                 displist_to_mesh(first);
2339                                         }
2340                                         else if(dl->prev) {
2341                                                 prev= dl->prev;
2342                                                 prev->next= 0;
2343                                                 displist_to_mesh(first);
2344                                                 prev->next= dl;
2345                                                 first= dl;
2346                                                 totvert= 0;
2347                                         }
2348                                 }
2349                                 
2350                                 dl= dl->next;
2351                         }
2352                         
2353                         freedisplist(&tempbase);
2354                         
2355                         curcol++;
2356                 }
2357         }
2358         else displist_to_mesh(lbase->first);
2359
2360         freedisplist(lbase);
2361
2362 }
2363
2364 int BKE_read_exotic(char *name)
2365 {
2366         ListBase lbase={0, 0};
2367         int len;
2368         gzFile gzfile;
2369         char str[32];
2370         int *s0 = (int*) str;
2371         int retval = 0;
2372
2373         // make sure we're not trying to read a directory....
2374
2375         len= strlen(name);
2376         if (name[len-1] !='/' && name[len-1] != '\\') {
2377                 gzfile = gzopen(name,"rb");
2378
2379                 if (NULL == gzfile ) {
2380                         //XXX error("Can't open file: %s", name);
2381                         retval= -1;
2382                 } else {
2383                         gzread(gzfile, str, 31);
2384                         gzclose(gzfile);
2385
2386                         if ((*s0 != FORM) && (strncmp(str, "BLEN", 4) != 0) && !BLI_testextensie(name,".blend.gz")) {
2387
2388                                 //XXX waitcursor(1);
2389                                 
2390                                 if(*s0==GOUR) {
2391                                         if(G.obedit) {
2392                                                 //XXX error("Unable to perform function in EditMode");
2393                                         } else {
2394                                                 read_radiogour(name);
2395                                                 retval = 1;
2396                                         }
2397                                 }
2398                                 else if ELEM4(*s0, DDG1, DDG2, DDG3, DDG4) {
2399                                         if(G.obedit) {
2400                                                 //XXX error("Unable to perform function in EditMode");
2401                                         } else {
2402                                                 read_videoscape(name);
2403                                                 retval = 1;
2404                                         }
2405                                 }
2406                                 else if(strncmp(str, "#Inventor V1.0", 14)==0) {
2407                                         if( strncmp(str+15, "ascii", 5)==0) {
2408                                                 read_inventor(name, &lbase);
2409                                                 displist_to_objects(&lbase);                            
2410                                                 retval = 1;
2411                                         } else {
2412                                                 //XXX error("Can only read Inventor 1.0 ascii");
2413                                         }
2414                                 }
2415                                 else if((strncmp(str, "#VRML V1.0 asc", 14)==0)) {
2416                                         read_inventor(name, &lbase);
2417                                         displist_to_objects(&lbase);                            
2418                                         retval = 1;
2419                                 }
2420                                 else if(is_dxf(name)) {
2421                                         dxf_read(name);
2422                                         retval = 1;
2423                                 }
2424                                 else if(is_stl(name)) {
2425                                         if (is_stl_ascii(name))
2426                                                 read_stl_mesh_ascii(name);
2427                                         else
2428                                                 read_stl_mesh_binary(name);
2429                                         retval = 1;
2430                                 }
2431 #ifndef DISABLE_PYTHON
2432                                 // TODO: this should not be in the kernel...
2433                                 else { // unknown format, call Python importloader 
2434                                         if (BPY_call_importloader(name)) {
2435                                                 retval = 1;
2436                                         } else {        
2437                                                 //XXX error("Unknown file type or error, check console");
2438                                         }       
2439                                 
2440                                 }
2441 #endif /* DISABLE_PYTHON */
2442                                 //XXX waitcursor(0);
2443                         }
2444                 }
2445         }
2446         
2447         return (retval);
2448 }
2449
2450
2451 /* ************************ WRITE ************************** */
2452
2453
2454 char temp_dir[160]= {0, 0};
2455
2456 static void write_vert_stl(Object *ob, MVert *verts, int index, FILE *fpSTL)
2457 {
2458         float vert[3];
2459
2460         VECCOPY(vert, verts[(index)].co);
2461         Mat4MulVecfl(ob->obmat, vert);
2462
2463         if (ENDIAN_ORDER==B_ENDIAN) {
2464                 SWITCH_INT(vert[0]);
2465                 SWITCH_INT(vert[1]);
2466                 SWITCH_INT(vert[2]);
2467         }
2468
2469         fwrite(vert, sizeof(float), 3, fpSTL);
2470 }
2471
2472 static int write_derivedmesh_stl(FILE *fpSTL, Object *ob, DerivedMesh *dm)
2473 {
2474         MVert *mvert = dm->getVertArray(dm);
2475         MFace *mface = dm->getFaceArray(dm);
2476         int i, numfacets = 0, totface = dm->getNumFaces(dm);
2477         float zero[3] = {0.0f, 0.0f, 0.0f};
2478
2479         for (i=0; i<totface; i++, mface++) {
2480                 fwrite(zero, sizeof(float), 3, fpSTL);
2481                 write_vert_stl(ob, mvert, mface->v1, fpSTL);
2482                 write_vert_stl(ob, mvert, mface->v2, fpSTL);
2483                 write_vert_stl(ob, mvert, mface->v3, fpSTL);
2484                 fprintf(fpSTL, "  ");
2485                 numfacets++;
2486
2487                 if(mface->v4) { /* quad = 2 tri's */
2488                         fwrite(zero, sizeof(float), 3, fpSTL);
2489                         write_vert_stl(ob, mvert, mface->v1, fpSTL);
2490                         write_vert_stl(ob, mvert, mface->v3, fpSTL);
2491                         write_vert_stl(ob, mvert, mface->v4, fpSTL);
2492                         fprintf(fpSTL, "  ");
2493                         numfacets++;
2494                 }
2495         }
2496
2497         return numfacets;
2498 }
2499
2500 static int write_object_stl(FILE *fpSTL, Object *ob, Mesh *me)
2501 {
2502         int  numfacets = 0;
2503         DerivedMesh *dm = mesh_get_derived_final(ob, CD_MASK_BAREMESH);
2504
2505         numfacets += write_derivedmesh_stl(fpSTL, ob, dm);
2506
2507         dm->release(dm);
2508
2509         return numfacets;
2510 }
2511
2512 void write_stl(char *str)
2513 {
2514         Object *ob;
2515         Mesh   *me;
2516         Base   *base;
2517         FILE   *fpSTL;
2518         int    numfacets = 0;
2519         
2520         if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2521         if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2522         if(BLI_testextensie(str,".stl")==0) strcat(str, ".stl");
2523
2524         if (!during_script()) {
2525                 if (BLI_exists(str))
2526                         ; //XXX if(saveover(str)==0)
2527                         //XXX   return;
2528         }
2529
2530         fpSTL= fopen(str, "wb");
2531         
2532         if(fpSTL==NULL) {
2533                 if (!during_script()) ; //XXX error("Can't write file");
2534                 return;
2535         }
2536         strcpy(temp_dir, str);
2537         
2538         //XXX waitcursor(1);
2539         
2540         /* The header part of the STL */
2541         /* First 80 characters are a title or whatever you want.
2542            Lets make the first 32 of those spam and the rest the filename.
2543            Those first 80 characters will be followed by 4 bytes
2544            which will be overwritten later with an integer holding
2545            how many facets are written (we set them to ' ' for now).
2546         */
2547         fprintf(fpSTL, "Binary STL output from Blender: %-48.48s    ", str);
2548
2549         /* Write all selected mesh objects */
2550         base= G.scene->base.first;
2551         while(base) {
2552                 if (base->flag & SELECT) {
2553                         ob = base->object;
2554                         if (ob->type == OB_MESH) {
2555                                 me = ob->data;
2556                                 if (me)
2557                                         numfacets += write_object_stl(fpSTL, ob, me);
2558                         }
2559                 }
2560                 base= base->next;
2561         }
2562
2563         /* time to write the number of facets in the 4 bytes
2564            starting at byte 81
2565         */
2566         fseek(fpSTL, 80, SEEK_SET);
2567
2568         if (ENDIAN_ORDER==B_ENDIAN) {
2569                 SWITCH_INT(numfacets);
2570         }
2571         fwrite(&numfacets, 4*sizeof(char), 1, fpSTL);
2572
2573         fclose(fpSTL);
2574         
2575         //XXX waitcursor(0);
2576 }
2577
2578 static void write_videoscape_mesh(Object *ob, char *str)
2579 {
2580         EditMesh *em = G.editMesh;
2581         Mesh *me;
2582         Material *ma;
2583         MFace *mface;
2584         FILE *fp;
2585         EditVert *eve;
2586         EditFace *evl;
2587         unsigned int kleur[32];
2588         float co[3];
2589         int a;
2590         long tot;
2591         char *cp;
2592         
2593         if(ob && ob->type==OB_MESH);
2594         else {
2595                 return;
2596         }
2597
2598         kleur[0]= 0x00C0C0C0;
2599
2600         cp= (char *)kleur;
2601         for(a=0; a<ob->totcol; a++, cp+=4) {
2602                 
2603                 ma= give_current_material(ob, a+1);
2604                 if(ma) {
2605                         cp[0]= (unsigned char) (255.0*ma->emit);
2606                         cp[1]= (unsigned char) (255.0*ma->b);
2607                         cp[2]= (unsigned char) (255.0*ma->g);
2608                         cp[3]= (unsigned char) (255.0*ma->r);
2609                         if(ENDIAN_ORDER==L_ENDIAN) SWITCH_INT(kleur[a]);
2610                 }
2611                 else kleur[a]= 0x00C0C0C0;
2612         
2613                 if(a>30) break;
2614         }
2615         
2616         fp= fopen(str, "wb");
2617         if(fp==NULL) return;
2618
2619         fprintf(fp,"3DG1\n");
2620
2621         if(G.obedit) {
2622
2623                 fprintf(fp, "%d\n", G.totvert);
2624         
2625                 tot= 0;
2626                 eve= em->verts.first;
2627                 while(eve) {
2628                         VECCOPY(co, eve->co);
2629                         Mat4MulVecfl(ob->obmat, co);
2630                         fprintf(fp, "%f %f %f\n", co[0], co[1], co[2] );
2631                         eve->tmp.l = tot;
2632                         tot++;
2633                         eve= eve->next;
2634                 }
2635                 evl= em->faces.first;
2636                 while(evl) {
2637
2638                         if(evl->v4==0) {
2639                                 fprintf(fp, "3 %ld %ld %ld 0x%x\n", 
2640                                                 (long int) evl->v1->tmp.l,
2641                                                 (long int) evl->v2->tmp.l,
2642                                                 (long int) evl->v3->tmp.l, 
2643                                                 kleur[evl->mat_nr]);
2644                         }
2645                         else {
2646                                 fprintf(fp, "4 %ld %ld %ld %ld 0x%x\n", 
2647                                                 (long int) evl->v1->tmp.l, 
2648                                                 (long int) evl->v2->tmp.l, 
2649                                                 (long int) evl->v3->tmp.l, 
2650                                                 (long int) evl->v4->tmp.l, 
2651                                                 kleur[evl->mat_nr]);
2652                         }
2653                         evl= evl->next;
2654                 }
2655         }
2656         else {
2657                 DerivedMesh *dm = mesh_get_derived_deform(ob, CD_MASK_BAREMESH);
2658                 
2659                 me= ob->data;
2660                 
2661                 fprintf(fp, "%d\n", me->totvert);
2662                 
2663                 mface= me->mface;
2664                 for(a=0; a<me->totvert; a++) {
2665                         dm->getVertCo(dm, a, co);
2666                         Mat4MulVecfl(ob->obmat, co);
2667                         fprintf(fp, "%f %f %f\n", co[0], co[1], co[2] );
2668                 }
2669                 for(a=0; a<me->totface; a++, mface++) {
2670                         if(mface->v4==0) {
2671                                 fprintf(fp, "3 %d %d %d 0x%x\n", mface->v1, mface->v2, mface->v3, kleur[(int)mface->mat_nr]);
2672                         }
2673                         else {
2674                                 fprintf(fp, "4 %d %d %d %d 0x%x\n", mface->v1, mface->v2, mface->v3, mface->v4, kleur[(int)mface->mat_nr]);
2675                         }
2676                 }
2677
2678                 dm->release(dm);
2679         }
2680         
2681         fclose(fp);
2682         
2683 }
2684
2685
2686 void write_videoscape(char *str)
2687 {
2688         Base *base;
2689         int file, val, lampdone=0;
2690         unsigned short numlen;
2691         char head[FILE_MAXFILE], tail[FILE_MAXFILE];
2692         
2693         if(BLI_testextensie(str,".trace")) str[ strlen(str)-6]= 0;
2694         if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2695         if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2696         if(BLI_testextensie(str,".obj")==0) strcat(str, ".obj");
2697
2698         file= open(str,O_BINARY|O_RDONLY);
2699         close(file);
2700         //XXX saveover()
2701         // if(file>-1) if(!during_script() && saveover(str)==0) return;
2702
2703         strcpy(temp_dir, str);
2704
2705         base= G.scene->base.first;
2706         while(base) {
2707                 if((base->flag & SELECT) && (base->lay & G.scene->lay))  {
2708                         if(base->object->type==OB_MESH) {
2709                                 write_videoscape_mesh(base->object, str);
2710                                 val = BLI_stringdec(str, head, tail, &numlen);
2711                                 BLI_stringenc(str, head, tail, numlen, val + 1);
2712                         }
2713                         else if(base->object->type==OB_CURVE || base->object->type==OB_SURF) {
2714                                 /* write_videoscape_nurbs(base->object, str); */
2715                                 /* val = stringdec(str, head, tail, &numlen); */
2716                                 /* stringenc(str, head, tail, numlen, val + 1); */
2717                         }
2718                         else if(lampdone==0 && base->object->type==OB_LAMP) {
2719                                 /* lampdone= 1; */
2720                                 /* write_videoscape_lamps(str); */
2721                                 /* val = stringdec(str, head, tail, &numlen); */
2722                                 /* stringenc(str, head, tail, numlen, val + 1); */
2723                         }
2724                 }
2725                 base= base->next;
2726         }
2727         
2728         
2729         /* remove when higher numbers exist */
2730         while(remove(str)==0) {
2731                 
2732                 val = BLI_stringdec(str, head, tail, &numlen);
2733                 BLI_stringenc(str, head, tail, numlen, val + 1);
2734         }
2735 }
2736
2737 /* ******************************* WRITE VRML ***************************** */
2738
2739 static void replace_chars(char *str1, char *str2)
2740 {
2741         int a= strlen(str2);
2742         
2743         str1[a]= 0;
2744         while(a--) {
2745                 if(str2[a]=='.' || str2[a]==' ') str1[a]= '_';
2746                 else str1[a]= str2[a];
2747         }
2748 }
2749
2750
2751 static void write_material_vrml(FILE *fp, Material *ma)
2752 {
2753         char str[32];
2754         
2755         replace_chars(str, ma->id.name+2);
2756         
2757         fprintf(fp, "\tDEF %s\n", str);
2758         fprintf(fp, "\tMaterial {\n");
2759         
2760         fprintf(fp, "\t\tdiffuseColor %f %f %f\n", ma->r, ma->g, ma->b);
2761         fprintf(fp, "\t\tspecularColor %f %f %f\n", ma->specr, ma->specg, ma->specb);
2762         fprintf(fp, "\t\tshininess %f \n", ((float)ma->har)/100.0);
2763         fprintf(fp, "\t\ttransparency %f \n", 1.0-ma->alpha);
2764         
2765         fprintf(fp, "\t}\n");
2766         
2767 }
2768
2769 unsigned int *mcol_to_vcol(Mesh *me)
2770 {
2771         MFace *mface;
2772         unsigned int *mcol, *mcoln, *mcolmain;
2773         int a;
2774
2775         if(me->totface==0 || me->mcol==0) return 0;
2776         
2777         mcoln= mcolmain= MEM_mallocN(sizeof(int)*me->totvert, "mcoln");
2778         mcol = (unsigned int *)me->mcol;
2779         mface= me->mface;
2780         
2781         for(a=me->totface; a>0; a--, mface++) {
2782                 mcoln[mface->v1]= mcol[0];
2783                 mcoln[mface->v2]= mcol[1];
2784                 mcoln[mface->v3]= mcol[2];
2785                 if(mface->v4) mcoln[mface->v4]= mcol[3];
2786
2787                 mcol+= 4;
2788         }
2789         
2790         return mcolmain;
2791 }
2792
2793 void mcol_to_rgba(unsigned int col, float *r, float *g, float *b, float *a)
2794 {
2795         char *cp;
2796         
2797         cp = (char *)&col;
2798         
2799         *r= cp[3];
2800         *r /= 255.0;
2801
2802         *g= cp[2];
2803         *g /= 255.0;
2804
2805         *b= cp[1];
2806         *b /= 255.0;
2807
2808         *a= cp[0];
2809         *a /= 255.0;
2810 }
2811
2812 static void write_mesh_vrml(FILE *fp, Mesh *me)
2813 {
2814         Material *ma;
2815         MVert *mvert;
2816         MFace *mface;
2817         MTFace *tface;
2818         Image *ima;
2819         int a, b, totcol, texind;
2820         char str[32];
2821         
2822         replace_chars(str, me->id.name+2);
2823
2824         fprintf(fp, "\tDEF %s\n", str);
2825         fprintf(fp, "\tSeparator {\n");
2826         
2827         if(me->mtface) {
2828                 ima= ((MTFace *)me->mtface)->tpage;
2829                 if(ima) {
2830                         fprintf(fp, "\t\tTexture2 {\n");
2831                         fprintf(fp, "\t\t\tfilename %s\n", ima->name);
2832                         fprintf(fp, "\t\t\twrapS REPEAT \n");
2833                         fprintf(fp, "\t\t\twrapT REPEAT \n");
2834                         fprintf(fp, "\t\t}\n");
2835                 }
2836         }
2837         
2838         if(me->mcol) {
2839                 unsigned int *mcol, *mcolmain;
2840                 float r, g, b, cola;
2841                 
2842                 fprintf(fp, "\t\tMaterial {\n");
2843                 fprintf(fp, "\t\t\tdiffuseColor [\n");
2844                 
2845                 a= me->totvert;
2846                 mcol= mcolmain= mcol_to_vcol(me);
2847                 if(mcol) {
2848                         while(a--) {
2849                                 mcol_to_rgba(*mcol, &r, &g, &b, &cola);
2850                                 fprintf(fp, "\t\t\t\t %f %f %f,\n", r, g, b);
2851                                 mcol++;
2852                         }
2853                         MEM_freeN(mcolmain);
2854                 }
2855                 fprintf(fp, "\t\t\t]\n");
2856                 fprintf(fp, "\t\t}\n");
2857
2858                 fprintf(fp, "\t\tMaterialBinding { value PER_VERTEX_INDEXED }\n");
2859         }
2860
2861
2862         fprintf(fp, "\t\tCoordinate3 {\n");
2863         fprintf(fp, "\t\t\tpoint [\n");
2864         
2865         a= me->totvert;
2866         mvert= me->mvert;
2867         while(a--) {
2868                 fprintf(fp, "\t\t\t\t %f %f %f,\n", mvert->co[0], mvert->co[1], mvert->co[2]);
2869                 mvert++;
2870         }
2871         fprintf(fp, "\t\t\t]\n");
2872         fprintf(fp, "\t\t}\n");
2873         
2874         
2875         totcol= me->totcol;
2876         if(totcol==0) totcol= 1;
2877         texind= 0; // index for uv coords
2878         
2879         for(b=0; b<totcol; b++) {
2880