1 /* exotic.c mei 95 MIXED MODEL
5 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version. The Blender
11 * Foundation also sells licenses for use in proprietary software under
12 * the Blender License. See http://www.blender.org/BL/ for information
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
25 * All rights reserved.
27 * The Original Code is: all of this file.
29 * Contributor(s): none yet.
31 * ***** END GPL/BL DUAL LICENSE BLOCK *****
33 * eigen videoscape formaat:
49 5 of 11 (curve of surf)
53 mat[0][0] mat[0][1] mat[0][2] mat[0][3]
54 mat[1][0] mat[1][1] mat[1][2] mat[1][3]
63 (als type==nurb) x y z w
66 (als type==bez) xyz xyz xyz h1 h2 h3
74 #include <ctype.h> /* isdigit, isspace */
83 #include "BLI_winstuff.h"
87 #include "MEM_guardedalloc.h"
89 #include "DNA_object_types.h"
90 #include "DNA_mesh_types.h"
91 #include "DNA_material_types.h"
92 #include "DNA_lamp_types.h"
93 #include "DNA_curve_types.h"
94 #include "DNA_image_types.h"
95 #include "DNA_camera_types.h"
96 #include "DNA_scene_types.h"
97 #include "DNA_view3d_types.h"
98 #include "DNA_userdef_types.h"
100 #include "BKE_bad_level_calls.h"
101 #include "BKE_utildefines.h"
102 #include "BLI_blenlib.h"
103 #include "BLI_arithb.h"
104 #include "BLI_editVert.h"
106 #include "BKE_global.h"
107 #include "BKE_main.h"
108 #include "BKE_mesh.h"
109 #include "BKE_library.h"
110 #include "BKE_global.h"
111 #include "BKE_object.h"
112 #include "BKE_material.h"
113 #include "BKE_exotic.h"
114 /* #include "BKE_error.h" */
115 #include "BKE_screen.h"
116 #include "BKE_displist.h"
117 #include "BKE_curve.h"
119 #include "BPY_extern.h"
123 static int is_dxf(char *str);
124 static void dxf_read(char *filename);
128 static void read_videoscape_mesh(char *str)
136 float *vertdata, *vd, min[3], max[3], cent[3], ftemp;
137 unsigned int color[32], col;
138 int totcol, a, b, verts, tottria=0, totquad=0, totedge=0, poly, nr0, nr, first;
142 fp= fopen(str, "rb");
144 error("Can't read file");
148 fscanf(fp, "%40s", s);
150 fscanf(fp, "%d\n", &verts);
158 error("too many vertices");
163 INIT_MINMAX(min, max);
164 vd= vertdata= MEM_mallocN(sizeof(float)*3*verts, "videoscapelezer");
166 for(a=0; a<verts; a++) {
167 fscanf(fp, "%f %f %f", vd, vd+1, vd+2);
168 DO_MINMAX(vd, min, max);
172 /* de vlakken en kleuren tellen */
173 for(a=0; a<32; a++) color[a]= 0;
177 end= fscanf(fp,"%d", &poly);
180 if(poly==3) tottria++;
181 else if(poly==4) totquad++;
184 for(a=0;a<poly;a++) {
185 end= fscanf(fp,"%d", &nr);
190 end= fscanf(fp,"%i\n", &col);
192 for(a=0; a<totcol; a++) {
193 if(color[a]==col) break;
195 if(a>=totcol && totcol<32) {
201 if(totedge+tottria+totquad>65000) {
202 printf(" var1: %d, var2: %d, var3: %d \n", totedge, tottria, totquad);
203 error("too many faces");
210 ob= add_object(OB_MESH);
213 me->totface= totedge+tottria+totquad;
215 me->mvert= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
216 if(me->totface) me->mface= MEM_callocN(me->totface*sizeof(MFace), "mface");
220 ob->mat= MEM_callocN(sizeof(void *)*totcol, "ob->mat");
221 me->mat= MEM_callocN(sizeof(void *)*totcol, "me->mat");
223 ob->totcol= (unsigned char) me->totcol;
228 for(a=0; a<totcol; a++) {
229 ma= G.main->mat.first;
232 col= rgb_to_cpack(ma->r, ma->g, ma->b);
242 ma= add_material("ext");
244 cpack_to_rgb(color[a], cent, cent+1, cent+2);
254 cent[0]= (min[0]+max[0])/2.0f;
255 cent[1]= (min[1]+max[1])/2.0f;
256 cent[2]= (min[2]+max[2])/2.0f;
257 VECCOPY(ob->loc, cent);
263 VecSubf(mvert->co, vd, cent);
272 fscanf(fp, "%40s", s);
273 fscanf(fp, "%d\n", &verts);
275 for(a=0;a<verts;a++) {
276 fscanf(fp, "%f %f %f", &ftemp, &ftemp, &ftemp);
282 end= fscanf(fp,"%d", &poly);
285 if(poly==3 || poly==4) {
286 fscanf(fp,"%d", &nr);
287 mface->v1= MIN2(nr, me->totvert-1);
288 fscanf(fp,"%d", &nr);
289 mface->v2= MIN2(nr, me->totvert-1);
290 fscanf(fp,"%d", &nr);
291 mface->v3= MIN2(nr, me->totvert-1);
293 if( fscanf(fp,"%d", &nr) <=0 ) break;
294 mface->v4= MIN2(nr, me->totvert-1);
298 test_index_mface(mface, poly);
303 if( fscanf(fp,"%d", &nr0) <=0) break;
305 for(b=1; b<poly; b++) {
306 end= fscanf(fp,"%d", &nr);
308 nr= MIN2(nr, me->totvert-1);
321 end= fscanf(fp,"%i", &col);
325 for(b=0; b<totcol; b++) {
327 (mface-1)->mat_nr= b;
346 static void read_radiogour(char *str)
353 float *vertdata, *vd, min[3], max[3], cent[3], ftemp;
354 unsigned int *colv, *colf, *colvertdata;
355 int itemp, a, b, verts, tottria=0, totquad=0, totedge=0, poly, nr0, nr, first;
359 fp= fopen(str, "rb");
361 error("Can't read file");
365 fscanf(fp, "%40s", s);
367 fscanf(fp, "%d\n", &verts);
375 error("too many vertices");
380 INIT_MINMAX(min, max);
381 vd= vertdata= MEM_mallocN(sizeof(float)*3*verts, "videoscapelezer");
382 colv= colvertdata= MEM_mallocN(verts*sizeof(float), "coldata");
384 for(a=0; a<verts; a++) {
385 fscanf(fp, "%f %f %f %i", vd, vd+1, vd+2, colv);
386 DO_MINMAX(vd, min, max);
391 /* de vlakken tellen */
394 end= fscanf(fp,"%d", &poly);
397 if(poly==3) tottria++;
398 else if(poly==4) totquad++;
401 for(a=0;a<poly;a++) {
402 end= fscanf(fp,"%d", &nr);
409 if(totedge+tottria+totquad>65000) {
410 printf(" var1: %d, var2: %d, var3: %d \n", totedge, tottria, totquad);
411 error("too many faces");
413 MEM_freeN(colvertdata);
419 ob= add_object(OB_MESH);
422 me->totface= totedge+tottria+totquad;
424 me->mvert= MEM_callocN(me->totvert*sizeof(MVert), "mverts");
425 if(me->totface) me->mface= MEM_callocN(me->totface*sizeof(MFace), "mface");
429 cent[0]= (min[0]+max[0])/2.0f;
430 cent[1]= (min[1]+max[1])/2.0f;
431 cent[2]= (min[2]+max[2])/2.0f;
432 VECCOPY(ob->loc, cent);
438 VecSubf(mvert->co, vd, cent);
447 fscanf(fp, "%40s", s);
448 fscanf(fp, "%d\n", &verts);
449 for(a=0;a<verts;a++) {
450 fscanf(fp, "%f %f %f %i", &ftemp, &ftemp, &ftemp, &itemp);
456 end= fscanf(fp,"%d", &poly);
459 if(poly==3 || poly==4) {
460 fscanf(fp,"%d", &nr);
461 mface->v1= MIN2(nr, me->totvert-1);
462 fscanf(fp,"%d", &nr);
463 mface->v2= MIN2(nr, me->totvert-1);
464 fscanf(fp,"%d", &nr);
465 mface->v3= MIN2(nr, me->totvert-1);
467 if( fscanf(fp,"%d", &nr) <=0 ) break;
468 mface->v4= MIN2(nr, me->totvert-1);
472 test_index_mface(mface, poly);
477 if( fscanf(fp,"%d", &nr0) <=0) break;
479 for(b=1; b<poly; b++) {
480 end= fscanf(fp,"%d", &nr);
482 nr= MIN2(nr, me->totvert-1);
492 mface->flag= ME_SMOOTH;
499 /* mcol is per vlak 4 kleuren */
500 me->mcol= MEM_mallocN(4*sizeof(int)*me->totface, "mcol");
501 colf= (unsigned int *)me->mcol;
507 colf[0]= colvertdata[mface->v1];
508 colf[1]= colvertdata[mface->v2];
509 colf[2]= colvertdata[mface->v3];
510 colf[3]= colvertdata[mface->v4];
516 MEM_freeN(colvertdata);
534 static void read_videoscape_lamp(char *str)
543 fp= fopen(str, "rb");
545 error("Can't read file");
549 fscanf(fp, "%40s", s);
550 fscanf(fp, "%d\n", &tot);
553 ob= add_object(OB_LAMP);
556 fscanf(fp, "%d\n", &val);
558 if(la->type==1) la->type= LA_SPOT;
559 else if(la->type==2) la->type= LA_SUN;
561 fscanf(fp, "%f %f\n", &la->spotsize, &la->spotblend);
563 fscanf(fp, "%f %f %f %f\n", &la->r, &la->g, &la->b, &la->energy);
565 fscanf(fp, "%f %f %f\n", ob->loc, ob->loc+1, ob->loc+2);
566 val= fscanf(fp, "%f %f %f\n", vec, vec+1, vec+2);
567 q1= vectoquat(vec, 5, 2);
568 QuatToEul(q1, ob->rot);
576 static void read_videoscape_nurbs(char *str)
584 float tmat[4][4], omat[3][3], imat[3][3], mat[3][3];
585 int a, tot, type, val;
588 fp= fopen(str, "rb");
590 error("Can't read file");
594 fscanf(fp, "%40s", s);
595 fscanf(fp, "%d\n", &type);
597 if(type==5) ob= add_object(OB_SURF);
598 else ob= add_object(OB_CURVE);
601 fscanf(fp, "%d\n", &tot);
602 fscanf(fp, "%d %d\n", &type, &val);
604 cu->ext1= 0.002f*type;
605 cu->ext2= 0.002f*val;
607 for(a=0; a<4; a++) fscanf(fp, "%e %e %e %e\n", tmat[a], tmat[a]+1, tmat[a]+2, tmat[a]+3);
609 VECCOPY(ob->loc, tmat[3]);
611 Mat3CpyMat4(omat, tmat);
612 Mat3ToEul(omat, ob->rot);
613 EulToMat3(ob->rot, mat);
615 Mat3MulMat3(tmat, imat, omat);
618 nu= (Nurb*)MEM_callocN(sizeof(Nurb),"nu from exotic");
619 BLI_addtail(&cu->nurb, nu);
621 fscanf(fp, "%d\n", &type);
624 fscanf(fp, "%d %d\n", &type, &val);
625 nu->pntsu= type; nu->pntsv= val;
626 fscanf(fp, "%d %d\n", &type, &val);
627 nu->resolu= type; nu->resolv= val;
628 fscanf(fp, "%d %d\n", &type, &val);
629 nu->orderu= type; nu->orderv= val;
630 fscanf(fp, "%d %d\n", &type, &val);
631 nu->flagu= type; nu->flagv= val;
633 if( (nu->type & 7)==CU_BEZIER) {
635 nu->bezt= bezt= MEM_callocN(a*sizeof(BezTriple), "bezt from exotic");
637 fscanf(fp, "%f %f %f ", bezt->vec[0], bezt->vec[0]+1, bezt->vec[0]+2);
638 Mat4MulVecfl(tmat, bezt->vec[0]);
639 fscanf(fp, "%f %f %f ", bezt->vec[1], bezt->vec[1]+1, bezt->vec[1]+2);
640 Mat4MulVecfl(tmat, bezt->vec[1]);
641 fscanf(fp, "%f %f %f ", bezt->vec[2], bezt->vec[2]+1, bezt->vec[2]+2);
642 Mat4MulVecfl(tmat, bezt->vec[2]);
643 fscanf(fp, "%d %d\n", &type, &val);
650 a= nu->pntsu*nu->pntsv;
652 nu->bp= bp= MEM_callocN(a*sizeof(BPoint), "bp from exotic");
654 fscanf(fp, "%f %f %f %f\n", bp->vec, bp->vec+1, bp->vec+2, bp->vec+3);
655 Mat4MulVecfl(tmat, bp->vec);
660 nu->knotsu= MEM_mallocN(sizeof(float)*val, "knots");
661 for(a=0; a<val; a++) fscanf(fp, "%f\n", nu->knotsu+a);
665 nu->knotsv= MEM_mallocN(sizeof(float)*val, "knots");
666 for(a=0; a<val; a++) fscanf(fp, "%f\n", nu->knotsv+a);
670 BLI_remlink(&cu->nurb, nu);
679 static void read_videoscape(char *str)
682 unsigned short val, numlen;
683 char name[FILE_MAXDIR+FILE_MAXFILE], head[FILE_MAXFILE], tail[FILE_MAXFILE];
688 file= open(name, O_BINARY|O_RDONLY);
691 read(file, &type, 4);
694 if(type==DDG1) read_videoscape_mesh(name);
695 else if(type==DDG2) read_videoscape_lamp(name);
696 else if(type==DDG3) read_videoscape_nurbs(name);
699 val = BLI_stringdec(name, head, tail, &numlen);
700 BLI_stringenc(name, head, tail, numlen, val + 1);
706 /* ***************** INVENTOR ******************* */
709 #define IV_MAXSTACK 500000
710 #define IV_MAXFIELD 10
713 static float *iv_data_stack;
714 static float ivcolors[IV_MAXCOL][3];
715 static Object *ivsurf;
716 static ListBase ivbase;
719 struct IvNode *next, *prev;
721 char *fieldname[IV_MAXFIELD];
722 int datalen[IV_MAXFIELD];
723 float *data[IV_MAXFIELD];
726 static int iv_curcol=0;
728 static int iv_colornumber(struct IvNode *iv)
730 float *fp, fr = 0.0, fg = 0.0, fb = 0.0;
734 /* terugzoeken naar laatste materiaal */
736 if( strcmp(iv->nodename, "Material")==0) {
738 if(fp==0) fp= iv->data[1];
746 else if( strcmp(iv->nodename, "BaseColor")==0) {
753 else if( strcmp(iv->nodename, "PackedColor")==0) {
754 cp= (char *)iv->data[0];
764 if(iv->datalen[0]<3) return 0;
766 for(a=0; a<iv_curcol; a++) {
768 if(ivcolors[a][0]== fr)
769 if(ivcolors[a][1]== fg)
770 if(ivcolors[a][2]== fb) return a+1
774 if(a>=IV_MAXCOL) a= IV_MAXCOL-1;
783 static int iv_finddata(struct IvNode *iv, char *field, int fieldnr)
785 /* zoek naar "field", tel lengte data en maak datablok. return skipdata */
787 int len, stackcount, skipdata=0;
788 char *cpa, terminator, str[64];
794 while( *cpa != '}' ) {
796 if( *cpa == *field ) {
797 if( strncmp(cpa, field, len)==0 ) {
798 iv->fieldname[fieldnr]= cpa;
800 /* lezen tot aan eerste karakter */
807 while( *cpa==32 || *cpa==13 || *cpa==10 || *cpa==9) cpa++;
818 while( *cpa!=terminator && *cpa != '}' ) {
820 /* in fact, isdigit should include the dot and minus */
821 if( (isdigit(*cpa) || *cpa=='.' || *cpa=='-') && (isspace(cpa[-1]) || cpa[-1]==0 || cpa[-1]==',') ) {
823 memcpy(str, cpa, 16);
826 sscanf(str, "%x", (int *)fp);
829 /* atof doesn't stop after the first float
830 * in a long string at Windows... so we copy
831 * the float to a new string then atof... */
833 i=strpbrk(cpa, ", \n")-cpa;
840 *fp= (float) atof(str);
845 if(stackcount>=IV_MAXSTACK) {
846 printf("stackoverflow in IV read\n");
855 iv->datalen[fieldnr]= stackcount;
857 iv->data[fieldnr]= MEM_mallocN(sizeof(float)*stackcount, "iv_finddata");
858 memcpy(iv->data[fieldnr], iv_data_stack, sizeof(float)*stackcount);
860 else iv->data[fieldnr]= 0;
872 static void read_iv_index(float *data, float *baseadr, float *index, int nr, int coordtype)
874 /* in data schrijven: baseadr met offset index (aantal nr)*/
880 fp= baseadr+coordtype*ofs;
889 static void read_inventor(char *str, struct ListBase *listb)
891 struct IvNode *iv, *ivp, *ivn;
892 char *maindata, *md, *cpa;
893 float *index, *data, *fp;
894 int file, filelen, count, face, nr = 0;
895 int skipdata, ok, a, b, tot, first, colnr, coordtype, polytype, *idata;
898 ivbase.first= ivbase.last= 0;
902 file= open(str, O_BINARY|O_RDONLY);
904 error("Can't read file\n");
907 filelen= BLI_filesize(file);
909 maindata= MEM_mallocN(filelen, "leesInventor");
910 read(file, maindata, filelen);
913 iv_data_stack= MEM_mallocN(sizeof(float)*IV_MAXSTACK, "ivstack");
915 /* eerste preprocess: commentar eruit */
918 while(count<filelen) {
919 if( *md=='#' ) { /* comment */
920 while( *md!=13 && *md!=10) { /* enters */
924 if(count>=filelen) break;
932 /* we gaan alles ordenen: welke zijn de nodes en de fields? */
935 while(count<filelen) {
936 if( *md=='{' ) { /* terug lezen */
939 while( *cpa==32 || *cpa==13 || *cpa==10 || *cpa==9) { /* spaties/enters/tab weg */
944 while( *cpa>32 && *cpa<128) cpa--;
950 iv= MEM_callocN(sizeof(struct IvNode), "leesInventor");
953 if(strcmp(cpa, "Coordinate3")==0 || strcmp(cpa, "Coordinate4")==0) {
954 skipdata= iv_finddata(iv, "point", 0);
957 else if(strcmp(cpa, "VertexProperty")==0) {
958 skipdata= iv_finddata(iv, "vertex", 0);
961 else if(strcmp(cpa, "IndexedLineSet")==0) {
962 skipdata= iv_finddata(iv, "coordIndex", 0);
965 else if(strcmp(cpa, "IndexedTriangleMesh")==0) {
966 skipdata= iv_finddata(iv, "coordIndex", 0);
969 else if(strcmp(cpa, "IndexedFaceSet")==0) {
970 skipdata= iv_finddata(iv, "coordIndex", 0);
973 else if(strcmp(cpa, "FaceSet")==0) {
974 skipdata= iv_finddata(iv, "numVertices", 0);
977 else if(strcmp(cpa, "Material")==0) {
978 iv_finddata(iv, "diffuseColor", 0);
979 iv_finddata(iv, "ambientColor", 1);
982 else if(strcmp(cpa, "BaseColor")==0) {
983 iv_finddata(iv, "rgb", 0);
986 else if(strcmp(cpa, "PackedColor")==0) {
987 iv_finddata(iv, "rgba", 0);
990 else if(strcmp(cpa, "QuadMesh")==0) {
991 iv_finddata(iv, "verticesPerColumn", 0);
992 iv_finddata(iv, "verticesPerRow", 1);
996 else if(strcmp(cpa, "IndexedTriangleStripSet")==0) {
997 skipdata= iv_finddata(iv, "coordIndex", 0);
1000 else if(strcmp(cpa, "TriangleStripSet")==0) {
1001 skipdata= iv_finddata(iv, "numVertices", 0);
1004 else if(strcmp(cpa, "IndexedNurbsSurface")==0 || strcmp(cpa, "NurbsSurface")==0) {
1005 iv_finddata(iv, "numUControlPoints", 0);
1006 iv_finddata(iv, "numVControlPoints", 1);
1007 iv_finddata(iv, "uKnotVector", 2);
1008 iv_finddata(iv, "vKnotVector", 3);
1013 while( *md != '}') {
1016 if(count<filelen) break;
1022 BLI_addtail(&ivbase, iv);
1033 /* nodes samenvoegen */
1039 if( strncmp(iv->nodename, "Indexed", 7)==0) {
1040 /* terugzoeken: zelfde naam? */
1044 if(strcmp(iv->nodename, ivp->nodename)==0) break;
1046 if(strcmp(ivp->nodename, "Coordinate3")==0 ||
1047 strcmp(ivp->nodename, "Coordinate4")==0 ||
1048 strcmp(ivp->nodename, "VertexProperty")==0) {
1056 /* iv bij ivp voegen */
1058 tot= iv->datalen[0] + ivp->datalen[0];
1060 data= MEM_mallocN(tot*sizeof(float), "samenvoeg iv");
1061 memcpy(data, ivp->data[0], sizeof(float)*ivp->datalen[0]);
1062 memcpy(data+ivp->datalen[0], iv->data[0], sizeof(float)*iv->datalen[0]);
1064 ivp->datalen[0]+= iv->datalen[0];
1065 MEM_freeN(ivp->data[0]);
1068 BLI_remlink(&ivbase, iv);
1069 MEM_freeN(iv->data[0]);
1079 /* Nodes omzetten naar DispLists */
1083 /* printf(" Node: %s\n", iv->nodename); */
1084 /* if(iv->fieldname[0]) printf(" Field: %s len %d\n", iv->fieldname[0], iv->datalen[0]); */
1087 if( strcmp(iv->nodename, "IndexedLineSet")==0 ) {
1089 colnr= iv_colornumber(iv);
1091 /* terugzoeken naar data */
1095 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1099 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1106 /* tel het aantal lijnen */
1109 for(a=0; a<iv->datalen[0]-1; a++) {
1110 if(index[0]!= -1 && index[1]!= -1) tot++;
1114 tot*= 2; /* aantal vertices */
1115 dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor1");
1116 BLI_addtail(listb, dl);
1121 data= (float *)(dl+1);
1124 for(a=0; a<iv->datalen[0]-1; a++) {
1125 if(index[0]!= -1 && index[1]!= -1) {
1126 read_iv_index(data, ivp->data[0], index, 2, coordtype);
1133 else if( strcmp(iv->nodename, "FaceSet")==0 ) {
1135 colnr= iv_colornumber(iv);
1137 /* terugzoeken naar data */
1141 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1145 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1152 /* tel het aantal driehoeken */
1156 polytype= (int) index[0];
1158 for(a=0; a<iv->datalen[0]; a++) {
1159 if(index[0]== polytype) tot++; /* een soort? */
1164 tot*= polytype; /* aantal vertices */
1165 dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor4");
1166 BLI_addtail(listb, dl);
1169 dl->parts= tot/polytype;
1171 data= (float *)(dl+1);
1173 index= ivp->data[0];
1175 for(a=0; a<iv->datalen[0]; a++) {
1177 VECCOPY(data, index);
1181 VECCOPY(data, index);
1185 VECCOPY(data, index);
1190 VECCOPY(data, index);
1197 else if( strcmp(iv->nodename, "TriangleStripSet")==0 ) {
1199 colnr= iv_colornumber(iv);
1201 /* terugzoeken naar data */
1205 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1209 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1216 /* tel het aantal driehoeken */
1220 index= iv->data[0]; /* afmeting strip */
1222 for(a=0; a<iv->datalen[0]; a++) {
1223 tot+= (int) index[0];
1224 face+= ((int) index[0]) - 2;
1228 dl= MEM_callocN(sizeof(struct DispList), "leesInventor4");
1229 dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1230 dl->index= MEM_callocN( face*3*sizeof(int), "dl index");
1232 dl->type= DL_INDEX3;
1236 BLI_addtail(listb, dl);
1239 index= iv->data[0]; /* afmeting strip */
1240 fp= ivp->data[0]; /* vertices */
1245 for(a=0; a<iv->datalen[0]; a++) {
1248 for(b=0; b<index[0]; b++) {
1255 for(b=0; b<index[0]-2; b++) {
1268 else if( strcmp(iv->nodename, "IndexedFaceSet")==0 ) {
1270 colnr= iv_colornumber(iv);
1272 /* terugzoeken naar data */
1276 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1280 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1287 /* tel het aantal driehoeken */
1290 for(a=0; a<iv->datalen[0]-2; a++) {
1291 if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) face++;
1295 /* aantal vertices */
1296 tot= ivp->datalen[0]/coordtype;
1299 dl= MEM_callocN(sizeof(struct DispList), "leesInventor5");
1300 BLI_addtail(listb, dl);
1301 dl->type= DL_INDEX3;
1306 dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1307 dl->index= MEM_callocN(sizeof(int)*3*face, "dl index");
1312 for(b=tot; b>0; b--) {
1323 for(a=0; a<iv->datalen[0]-2; a++) {
1325 if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) {
1327 /* deze truuk is om poly's met meer dan 3 vertices correct te vullen */
1333 idata[1]= (int) index[1];
1334 idata[2]= (int) index[2];
1344 else if( strcmp(iv->nodename, "IndexedTriangleMesh")==0 ||
1345 strcmp(iv->nodename, "IndexedTriangleStripSet")==0 ) {
1347 colnr= iv_colornumber(iv);
1349 /* terugzoeken naar data */
1353 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1357 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1364 /* tel het aantal driehoeken */
1367 for(a=0; a<iv->datalen[0]-2; a++) {
1368 if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) face++;
1372 /* aantal vertices */
1373 tot= ivp->datalen[0]/coordtype;
1375 dl= MEM_callocN(sizeof(struct DispList), "leesInventor6");
1376 BLI_addtail(listb, dl);
1377 dl->type= DL_INDEX3;
1382 dl->verts= MEM_callocN( tot*3*sizeof(float), "dl verts");
1383 dl->index= MEM_callocN(sizeof(int)*3*face, "dl index");
1388 for(b=tot; b>0; b--) {
1398 for(a=iv->datalen[0]-2; a>0; a--) {
1400 if(index[0]!= -1 && index[1]!= -1 && index[2]!= -1) {
1401 idata[0]= (int) index[0];
1402 idata[1]= (int) index[1];
1403 idata[2]= (int) index[2];
1410 else if( strcmp(iv->nodename, "QuadMesh")==0 ) {
1412 colnr= iv_colornumber(iv);
1414 /* terugzoeken naar data */
1418 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1422 if( strcmp(ivp->nodename, "VertexProperty")==0 ) {
1426 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1433 tot= (int) (floor(*(iv->data[0])+0.5) * floor(*(iv->data[1])+0.5));
1436 dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor8");
1437 BLI_addtail(listb, dl);
1439 dl->parts= (int) floor(*(iv->data[0])+0.5);
1440 dl->nr= (int) floor(*(iv->data[1])+0.5);
1442 data= (float *)(dl+1);
1443 memcpy(data, ivp->data[0], tot*3*sizeof(float));
1447 else if(strcmp(iv->nodename, "IndexedNurbsSurface")==0 || strcmp(iv->nodename, "NurbsSurface")==0) {
1449 colnr= iv_colornumber(iv);
1451 /* terugzoeken naar data */
1455 if( strcmp(ivp->nodename, "Coordinate3")==0 ) {
1459 if( strcmp(ivp->nodename, "Coordinate4")==0 ) {
1465 a= (int) *(iv->data[0]);
1466 b= (int) *(iv->data[1]);
1470 if( (a>=4 || b>=4) && tot>6) {
1477 ob= add_object(OB_SURF);
1482 nu = (Nurb*) MEM_callocN(sizeof(Nurb),"addNurbprim") ;
1483 BLI_addtail(&cu->nurb, nu);
1495 (BPoint*)MEM_callocN(tot * sizeof(BPoint), "addNurbprim3");
1499 VECCOPY(bp->vec, data);
1501 bp->vec[3]= data[3];
1502 VecMulf(bp->vec, 1.0f/data[3]);
1504 else bp->vec[3]= 1.0;
1509 /* iv->datalen[2] / [3] is aantal knots */
1510 nu->orderu= iv->datalen[2] - nu->pntsu;
1511 nu->orderv= iv->datalen[3] - nu->pntsv;
1513 nu->knotsu= MEM_mallocN( sizeof(float)*(iv->datalen[2]), "knots");
1514 memcpy(nu->knotsu, iv->data[2], sizeof(float)*(iv->datalen[2]));
1515 nu->knotsv= MEM_mallocN( sizeof(float)*(iv->datalen[3]), "knots");
1516 memcpy(nu->knotsv, iv->data[3], sizeof(float)*(iv->datalen[3]));
1518 switchdirectionNurb(nu);
1522 dl= MEM_callocN(sizeof(struct DispList)+tot*3*sizeof(float), "leesInventor3");
1523 BLI_addtail(listb, dl);
1525 dl->nr= (int) *(iv->data[0]);
1526 dl->parts= (int) *(iv->data[1]);
1528 data= (float *)(dl+1);
1546 for(a=0; a<IV_MAXFIELD; a++) {
1547 if(iv->data[a]) MEM_freeN(iv->data[a]);
1552 BLI_freelistN(&ivbase);
1553 MEM_freeN(maindata);
1554 MEM_freeN(iv_data_stack);
1558 /* ************************************************************ */
1560 static void displist_to_mesh(DispList *dlfirst)
1568 float *data, vec[3], min[3], max[3];
1569 int a, b, startve, *idata, totedge=0, tottria=0, totquad=0, totvert=0, totvlak, totcol=0, colnr;
1573 INIT_MINMAX(min, max);
1578 /* PATCH 1 (polyfill) kan hier niet, er wordt geen listbase meegegeven. eerder doen! */
1580 if(dl->type==DL_SEGM && dl->nr>2) {
1581 data= (float *)(dl+1);
1582 if(data[0]==data[3*(dl->nr-1)]) {
1583 if(data[1]==data[3*(dl->nr-1)+1]) {
1584 if(data[2]==data[3*(dl->nr-1)+2]) {
1593 if(dl->col > totcol) totcol= dl->col;
1595 /* afmeting en tellen */
1596 if(dl->type==DL_SURF) {
1599 if(dl->flag & 1) a++;
1600 if(dl->flag & 2) b++;
1604 totvert+= dl->nr*dl->parts;
1606 data= (float *)(dl+1);
1607 for(a= dl->nr*dl->parts; a>0; a--) {
1608 DO_MINMAX(data, min, max);
1612 else if(dl->type==DL_POLY) {
1613 if(dl->nr==3 || dl->nr==4) {
1614 if(dl->nr==3) tottria+= dl->parts;
1615 else totquad+= dl->parts;
1617 totvert+= dl->nr*dl->parts;
1619 data= (float *)(dl+1);
1620 for(a= dl->nr*dl->parts; a>0; a--) {
1621 DO_MINMAX(data, min, max);
1627 tottria+= dl->nr*dl->parts;
1628 totvert+= dl->nr*dl->parts;
1630 data= (float *)(dl+1);
1631 for(a= dl->nr*dl->parts; a>0; a--) {
1632 DO_MINMAX(data, min, max);
1638 else if(dl->type==DL_INDEX3) {
1639 tottria+= dl->parts;
1643 for(a= dl->nr; a>0; a--) {
1644 DO_MINMAX(data, min, max);
1648 else if(dl->type==DL_SEGM) {
1650 tottria+= (dl->nr-1)*dl->parts;
1651 totvert+= dl->nr*dl->parts;
1653 data= (float *)(dl+1);
1654 for(a= dl->nr*dl->parts; a>0; a--) {
1655 DO_MINMAX(data, min, max);
1666 if(totvert>=65000 || tottria>=65000) {
1667 if (totvert>=65000) {
1668 error("Too many vertices (%d)", totvert);
1670 error("Too many faces (%d)", tottria);
1677 error("Found more than 16 different colors");
1681 vec[0]= (min[0]+max[0])/2;
1682 vec[1]= (min[1]+max[1])/2;
1683 vec[2]= (min[2]+max[2])/2;
1685 ob= add_object(OB_MESH);
1686 VECCOPY(ob->loc, vec);
1687 where_is_object(ob);
1693 ob->mat= MEM_callocN(sizeof(void *)*totcol, "ob->mat");
1694 me->mat= MEM_callocN(sizeof(void *)*totcol, "me->mat");
1696 ob->totcol= (unsigned char) me->totcol;
1701 for(a=0; a<totcol; a++) {
1702 ma= G.main->mat.first;
1704 if(ma->mtex[0]==0) {
1705 if(ivcolors[a][0]==ma->r && ivcolors[a][1]==ma->g && ivcolors[a][2]==ma->b) {
1714 ma= add_material("ext");
1716 ma->r= ivcolors[a][0];
1717 ma->g= ivcolors[a][1];
1718 ma->b= ivcolors[a][2];
1723 totvlak= totquad+tottria+totedge;
1725 printf("Import: %d vertices %d faces\n", totvert, totvlak);
1727 if(totvert) me->mvert= MEM_callocN(totvert*sizeof(MVert), "mvert");
1728 if(totvlak) me->mface= MEM_callocN(totvlak*sizeof(MFace), "mface");
1729 me->totvert= totvert;
1730 me->totface= totvlak;
1740 colnr= (dl->col>15 ? 15: dl->col);
1743 if(dl->type==DL_SURF) {
1744 data= (float *)(dl+1);
1746 for(a=dl->parts*dl->nr; a>0; a--) {
1747 mvert->co[0]= data[0] -vec[0];
1748 mvert->co[1]= data[1] -vec[1];
1749 mvert->co[2]= data[2] -vec[2];
1755 for(a=0; a<dl->parts; a++) {
1757 DL_SURFINDEX(dl->flag & 1, dl->flag & 2, dl->nr, dl->parts);
1763 for(; b<dl->nr; b++) {
1770 mface->mat_nr= colnr;
1771 test_index_mface(mface, 4);
1782 startve += dl->parts*dl->nr;
1785 else if(dl->type==DL_POLY) {
1787 if(dl->nr==3 || dl->nr==4) {
1788 data= (float *)(dl+1);
1790 for(a=dl->parts*dl->nr; a>0; a--) {
1791 mvert->co[0]= data[0] -vec[0];
1792 mvert->co[1]= data[1] -vec[1];
1793 mvert->co[2]= data[2] -vec[2];
1798 for(a=0; a<dl->parts; a++) {
1800 mface->v1= startve+a*dl->nr;
1801 mface->v2= startve+a*dl->nr+1;
1802 mface->v3= startve+a*dl->nr+2;
1803 mface->mat_nr= colnr;
1804 test_index_mface(mface, 3);
1808 mface->v1= startve+a*dl->nr;
1809 mface->v2= startve+a*dl->nr+1;
1810 mface->v3= startve+a*dl->nr+2;
1811 mface->v4= startve+a*dl->nr+3;
1812 mface->mat_nr= colnr;
1813 test_index_mface(mface, 4);
1817 startve += dl->parts*dl->nr;
1820 data= (float *)(dl+1);
1822 for(a=dl->parts*dl->nr; a>0; a--) {
1823 mvert->co[0]= data[0] -vec[0];
1824 mvert->co[1]= data[1] -vec[1];
1825 mvert->co[2]= data[2] -vec[2];
1831 for(b=0; b<dl->parts; b++) {
1832 for(a=0; a<dl->nr; a++) {
1833 mface->v1= startve+a;
1835 if(a==dl->nr-1) mface->v2= startve;
1836 else mface->v2= startve+a+1;
1838 mface->mat_nr= colnr;
1839 test_index_mface(mface, 2);
1847 else if(dl->type==DL_INDEX3) {
1850 for(a=dl->nr; a>0; a--) {
1851 mvert->co[0]= data[0] -vec[0];
1852 mvert->co[1]= data[1] -vec[1];
1853 mvert->co[2]= data[2] -vec[2];
1859 for(b=dl->parts; b>0; b--) {
1860 mface->v1= startve+idata[0];
1861 mface->v2= startve+idata[1];
1862 mface->v3= startve+idata[2];
1863 mface->mat_nr= colnr;
1865 if (mface->v1>me->totvert-1) mface->v1= me->totvert-1;
1866 if (mface->v2>me->totvert-1) mface->v2= me->totvert-1;
1867 if (mface->v3>me->totvert-1) mface->v3= me->totvert-1;
1869 test_index_mface(mface, 3);
1875 else if(dl->type==DL_SEGM) {
1876 data= (float *)(dl+1);
1878 for(a=dl->parts*dl->nr; a>0; a--) {
1879 mvert->co[0]= data[0] -vec[0];
1880 mvert->co[1]= data[1] -vec[1];
1881 mvert->co[2]= data[2] -vec[2];
1886 for(b=0; b<dl->parts; b++) {
1887 for(a=0; a<dl->nr-1; a++) {
1888 mface->v1= startve+a;
1889 mface->v2= startve+a+1;
1890 mface->mat_nr= colnr;
1891 test_index_mface(mface, 2);
1910 static void displist_to_objects(ListBase *lbase)
1912 DispList *dl, *first, *prev, *next;
1914 int maxaantal, curcol, totvert=0, vert;
1916 /* eerst dit: is nu nog actief */
1918 where_is_object(ivsurf);
1926 /* PATCH 1: polyfill */
1927 if(dl->type==DL_POLY && dl->nr>4) {
1928 /* oplossing: bij elkaar in aparte listbase zetten */
1931 /* PATCH 2: poly's van 2 punten */
1932 if(dl->type==DL_POLY && dl->nr==2) dl->type= DL_SEGM;
1937 /* vertices tellen */
1942 if(dl->type==DL_SURF) totvert+= dl->nr*dl->parts;
1943 else if(dl->type==DL_POLY) {
1944 if(dl->nr==3 || dl->nr==4) totvert+= dl->nr*dl->parts;
1945 else if(dl->nr>4) totvert+= dl->nr*dl->parts;
1947 else if(dl->type==DL_INDEX3) totvert+= dl->nr;
1948 else if(dl->type==DL_SEGM) totvert+= dl->nr*dl->parts;
1955 if(ivsurf==0) error("Found no data");
1956 if(lbase->first) BLI_freelistN(lbase);
1963 if(totvert>maxaantal) {
1965 /* probeer kleuren bij elkaar te zetten */
1967 tempbase.first= tempbase.last= 0;
1969 while(lbase->first) {
1973 if(dl->col==curcol) {
1974 BLI_remlink(lbase, dl);
1975 BLI_addtail(&tempbase, dl);
1982 /* in tempbase zitten alle kleuren 'curcol' */
1984 dl= first= tempbase.first;
1988 if(dl->type==DL_SURF) vert= dl->nr*dl->parts;
1989 else if(dl->type==DL_POLY) {
1990 if(dl->nr==3 || dl->nr==4) vert= dl->nr*dl->parts;
1991 else if(dl->nr>4) vert= dl->nr*dl->parts;
1993 else if(dl->type==DL_INDEX3) totvert+= dl->nr;
1994 else if(dl->type==DL_SEGM) vert= dl->nr*dl->parts;
1997 if(totvert > maxaantal || dl->next==0) {
1999 displist_to_mesh(first);
2004 displist_to_mesh(first);
2014 freedisplist(&tempbase);
2019 else displist_to_mesh(lbase->first);
2021 freedisplist(lbase);
2025 int BKE_read_exotic(char *name)
2027 ListBase lbase={0, 0};
2030 int *s0 = (int*) str;
2033 // make sure we're not trying to read a directory....
2036 if (name[len-1] !='/' && name[len-1] != '\\') {
2037 file = open(name, O_BINARY|O_RDONLY);
2040 error("Can't open file: %s", name);
2042 read(file, str, 31);
2045 if ((*s0 != FORM) && (strncmp(str, "BLEN", 4) != 0)) {
2051 error("Unable to perform function in EditMode");
2053 read_radiogour(name);
2057 else if ELEM4(*s0, DDG1, DDG2, DDG3, DDG4) {
2059 error("Unable to perform function in EditMode");
2061 read_videoscape(name);
2065 else if(strncmp(str, "#Inventor V1.0", 14)==0) {
2066 if( strncmp(str+15, "ascii", 5)==0) {
2067 read_inventor(name, &lbase);
2068 displist_to_objects(&lbase);
2071 error("Can only read Inventor 1.0 ascii");
2074 else if((strncmp(str, "#VRML V1.0 asc", 14)==0)) {
2075 read_inventor(name, &lbase);
2076 displist_to_objects(&lbase);
2079 else if(is_dxf(name)) {
2083 // TODO: this should not be in the kernel...
2084 else { // unknown format, call Python importloader
2085 if (BPY_call_importloader(name)) {
2088 error("Unknown file type or error, check console");
2098 strcpy(G.sce, name);
2105 /* ************************ WRITE ************************** */
2108 char videosc_dir[160]= {0, 0};
2111 static void write_videoscape_mesh(Object *ob, char *str)
2120 unsigned int kleur[32];
2126 if(ob && ob->type==OB_MESH);
2131 kleur[0]= 0x00C0C0C0;
2132 if(G.order==L_ENDIAN) SWITCH_INT(kleur[0]);
2135 for(a=0; a<ob->totcol; a++, cp+=4) {
2137 ma= give_current_material(ob, a+1);
2139 cp[0]= (unsigned char) (255.0*ma->emit);
2140 cp[1]= (unsigned char) (255.0*ma->b);
2141 cp[2]= (unsigned char) (255.0*ma->g);
2142 cp[3]= (unsigned char) (255.0*ma->r);
2144 else kleur[a]= 0x00C0C0C0;
2146 if(G.order==L_ENDIAN) SWITCH_INT(kleur[a]);
2151 fp= fopen(str, "wb");
2152 if(fp==NULL) return;
2154 fprintf(fp,"3DG1\n");
2158 fprintf(fp, "%d\n", G.totvert);
2163 VECCOPY(co, eve->co);
2164 Mat4MulVecfl(ob->obmat, co);
2165 fprintf(fp, "%f %f %f\n", co[0], co[1], co[2] );
2166 eve->vn= (struct EditVert *)tot;
2174 fprintf(fp, "3 %p %p %p 0x%x\n", evl->v1->vn, evl->v2->vn, evl->v3->vn, kleur[evl->mat_nr]);
2177 fprintf(fp, "4 %p %p %p %p 0x%x\n", evl->v1->vn, evl->v2->vn, evl->v3->vn, evl->v4->vn, kleur[evl->mat_nr]);
2186 dl= find_displist(&ob->disp, DL_VERTS);
2187 if(dl) extverts= dl->verts;
2191 fprintf(fp, "%d\n", me->totvert);
2195 for(a=0; a<me->totvert; a++, mvert++) {
2197 VECCOPY(co, extverts);
2201 VECCOPY(co, mvert->co);
2203 Mat4MulVecfl(ob->obmat, co);
2204 fprintf(fp, "%f %f %f\n", co[0], co[1], co[2] );
2206 for(a=0; a<me->totface; a++, mface++) {
2208 fprintf(fp, "2 %d %d 0x%x\n", mface->v1, mface->v2, kleur[mface->mat_nr]);
2210 else if(mface->v4==0) {
2211 fprintf(fp, "3 %d %d %d 0x%x\n", mface->v1, mface->v2, mface->v3, kleur[mface->mat_nr]);
2214 fprintf(fp, "4 %d %d %d %d 0x%x\n", mface->v1, mface->v2, mface->v3, mface->v4, kleur[mface->mat_nr]);
2224 void write_videoscape(char *str)
2227 int file, val, lampdone=0;
2228 unsigned short numlen;
2229 char head[FILE_MAXFILE], tail[FILE_MAXFILE];
2231 if(BLI_testextensie(str,".trace")) str[ strlen(str)-6]= 0;
2232 if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2233 if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2234 if(BLI_testextensie(str,".obj")==0) strcat(str, ".obj");
2236 file= open(str,O_BINARY|O_RDONLY);
2238 if(file>-1) if(saveover(str)==0) return;
2240 strcpy(videosc_dir, str);
2242 base= G.scene->base.first;
2244 if((base->flag & SELECT) && (base->lay & G.scene->lay)) {
2245 if(base->object->type==OB_MESH) {
2246 write_videoscape_mesh(base->object, str);
2247 val = BLI_stringdec(str, head, tail, &numlen);
2248 BLI_stringenc(str, head, tail, numlen, val + 1);
2250 else if(base->object->type==OB_CURVE || base->object->type==OB_SURF) {
2251 /* write_videoscape_nurbs(base->object, str); */
2252 /* val = stringdec(str, head, tail, &numlen); */
2253 /* stringenc(str, head, tail, numlen, val + 1); */
2255 else if(lampdone==0 && base->object->type==OB_LAMP) {
2257 /* write_videoscape_lamps(str); */
2258 /* val = stringdec(str, head, tail, &numlen); */
2259 /* stringenc(str, head, tail, numlen, val + 1); */
2266 /* weggooien als nog hogere nummers bestaan */
2267 while(remove(str)==0) {
2269 val = BLI_stringdec(str, head, tail, &numlen);
2270 BLI_stringenc(str, head, tail, numlen, val + 1);
2274 /* ******************************* WRITE VRML ***************************** */
2276 static void replace_chars(char *str1, char *str2)
2278 int a= strlen(str2);
2282 if(str2[a]=='.' || str2[a]==' ') str1[a]= '_';
2283 else str1[a]= str2[a];
2288 static void write_material_vrml(FILE *fp, Material *ma)
2292 replace_chars(str, ma->id.name+2);
2294 fprintf(fp, "\tDEF %s\n", str);
2295 fprintf(fp, "\tMaterial {\n");
2297 fprintf(fp, "\t\tdiffuseColor %f %f %f\n", ma->r, ma->g, ma->b);
2298 fprintf(fp, "\t\tspecularColor %f %f %f\n", ma->specr, ma->specg, ma->specb);
2299 fprintf(fp, "\t\tshininess %f \n", ((float)ma->har)/100.0);
2300 fprintf(fp, "\t\ttransparency %f \n", 1.0-ma->alpha);
2302 fprintf(fp, "\t}\n");
2306 unsigned int *mcol_to_vcol(Mesh *me)
2309 unsigned int *mcol, *mcoln, *mcolmain;
2312 if(me->totface==0 || me->mcol==0) return 0;
2314 mcoln= mcolmain= MEM_mallocN(sizeof(int)*me->totvert, "mcoln");
2315 mcol = (unsigned int *)me->mcol;
2318 for(a=me->totface; a>0; a--, mface++) {
2319 mcoln[mface->v1]= mcol[0];
2320 mcoln[mface->v2]= mcol[1];
2321 if(mface->v3) mcoln[mface->v3]= mcol[2];
2322 if(mface->v4) mcoln[mface->v4]= mcol[3];
2330 void mcol_to_rgba(unsigned int col, float *r, float *g, float *b, float *a)
2349 static void write_mesh_vrml(FILE *fp, Mesh *me)
2359 replace_chars(str, me->id.name+2);
2361 fprintf(fp, "\tDEF %s\n", str);
2362 fprintf(fp, "\tSeparator {\n");
2365 ima= ((TFace *)me->tface)->tpage;
2367 fprintf(fp, "\t\tTexture2 {\n");
2368 fprintf(fp, "\t\t\tfilename %s\n", ima->name);
2369 fprintf(fp, "\t\t\twrapS REPEAT \n");
2370 fprintf(fp, "\t\t\twrapT REPEAT \n");
2371 fprintf(fp, "\t\t}\n");
2377 unsigned int *mcol, *mcolmain;
2378 float r, g, b, cola;
2380 fprintf(fp, "\t\tMaterial {\n");
2381 fprintf(fp, "\t\t\tdiffuseColor [\n");
2384 mcol= mcolmain= mcol_to_vcol(me);
2387 mcol_to_rgba(*mcol, &r, &g, &b, &cola);
2388 fprintf(fp, "\t\t\t\t %f %f %f,\n", r, g, b);
2391 MEM_freeN(mcolmain);
2393 fprintf(fp, "\t\t\t]\n");
2394 fprintf(fp, "\t\t}\n");
2396 fprintf(fp, "\t\tMaterialBinding { value PER_VERTEX_INDEXED }\n");
2400 fprintf(fp, "\t\tCoordinate3 {\n");
2401 fprintf(fp, "\t\t\tpoint [\n");
2406 fprintf(fp, "\t\t\t\t %f %f %f,\n", mvert->co[0], mvert->co[1], mvert->co[2]);
2409 fprintf(fp, "\t\t\t]\n");
2410 fprintf(fp, "\t\t}\n");
2414 if(totcol==0) totcol= 1;
2416 for(b=0; b<totcol; b++) {
2422 replace_chars(str, ma->id.name+2);
2424 fprintf(fp, "\t\tUSE %s\n\n", str);
2430 fprintf(fp, "\t\tTextureCoordinate2 {\n");
2431 fprintf(fp, "\t\t\tpoint [\n");
2437 if(mface->mat_nr==b) {
2438 fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[0][0], tface->uv[0][1]);
2439 fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[3][0], tface->uv[3][1]);
2440 if(mface->v3) fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[2][0], tface->uv[2][1]);
2441 if(mface->v4) fprintf(fp, "\t\t\t\t %f %f,\n", tface->uv[1][0], tface->uv[1][1]);
2446 fprintf(fp, "\t\t\t]\n");
2447 fprintf(fp, "\t\t}\n");
2450 fprintf(fp, "\t\tIndexedFaceSet {\n");
2451 fprintf(fp, "\t\t\tcoordIndex [\n");
2456 if(mface->mat_nr==b) {
2457 if(mface->v4) fprintf(fp, "\t\t\t\t %d, %d, %d, %d, -1,\n", mface->v1, mface->v2, mface->v3, mface->v4);
2458 else if(mface->v3) fprintf(fp, "\t\t\t\t %d, %d, %d, -1,\n", mface->v1, mface->v2, mface->v3);
2462 fprintf(fp, "\t\t\t]\n");
2463 fprintf(fp, "\t\t}\n");
2467 fprintf(fp, "\t}\n");
2470 MEM_freeN(me->mcol);
2476 static void write_camera_vrml(FILE *fp, Object *ob)
2481 Mat4Invert(ob->imat, ob->obmat);
2483 fprintf(fp, "\tMatrixTransform {\n");
2485 fprintf(fp, "\tmatrix \n");
2487 fprintf(fp, "\t\t%f %f %f %f\n", ob->imat[0][0], ob->imat[0][1], ob->imat[0][2], ob->imat[0][3]);
2488 fprintf(fp, "\t\t%f %f %f %f\n", ob->imat[1][0], ob->imat[1][1], ob->imat[1][2], ob->imat[1][3]);
2489 fprintf(fp, "\t\t%f %f %f %f\n", ob->imat[2][0], ob->imat[2][1], ob->imat[2][2], ob->imat[2][3]);
2490 fprintf(fp, "\t\t%f %f %f %f\n", ob->imat[3][0], ob->imat[3][1], ob->imat[3][2], ob->imat[3][3]);
2492 fprintf(fp, "\t}\n");
2496 fprintf(fp, "\tPerspectiveCamera {\n");
2497 fprintf(fp, "\t\tfocalDistance %f\n", cam->lens/10.0);
2499 fprintf(fp, "\t}\n");
2503 static void write_object_vrml(FILE *fp, Object *ob)
2508 fprintf(fp, "\tSeparator {\n");
2509 fprintf(fp, "\t\tMatrixTransform {\n");
2511 fprintf(fp, "\t\tmatrix \n");
2513 fprintf(fp, "\t\t\t%f %f %f %f\n", ob->obmat[0][0], ob->obmat[0][1], ob->obmat[0][2], ob->obmat[0][3]);
2514 fprintf(fp, "\t\t\t%f %f %f %f\n", ob->obmat[1][0], ob->obmat[1][1], ob->obmat[1][2], ob->obmat[1][3]);
2515 fprintf(fp, "\t\t\t%f %f %f %f\n", ob->obmat[2][0], ob->obmat[2][1], ob->obmat[2][2], ob->obmat[2][3]);
2516 fprintf(fp, "\t\t\t%f %f %f %f\n", ob->obmat[3][0], ob->obmat[3][1], ob->obmat[3][2], ob->obmat[3][3]);
2518 fprintf(fp, "\t\t}\n");
2522 replace_chars(str, id->name+2);
2524 fprintf(fp, "\t\tUSE %s\n", str);
2525 fprintf(fp, "\t}\n");
2529 void write_vrml(char *str)
2536 if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2537 if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2538 if(BLI_testextensie(str,".wrl")==0) strcat(str, ".wrl");
2540 if(saveover(str)==0) return;
2542 fp= fopen(str, "w");
2545 error("Can't write file");
2548 strcpy(videosc_dir, str);
2552 /* FIRST: write all the datablocks */
2554 fprintf(fp, "#VRML V1.0 ascii\n\n# Blender V2.0\n\n# 'Switch' is used as a hack, to ensure it is not part of the drawing\n\n");
2555 fprintf(fp, "Separator {\n");
2556 fprintf(fp, "Switch {\n");
2558 ma= G.main->mat.first;
2561 write_material_vrml(fp, ma);
2566 me= G.main->mesh.first;
2569 write_mesh_vrml(fp, me);
2574 /* THEN:Hidden Objects */
2575 fprintf(fp, "\n\t# Hidden Objects, in invisible layers\n\n");
2576 base= G.scene->base.first;
2578 if(base->object->type== OB_MESH) {
2579 if( (base->lay & G.scene->lay)==0 ) {
2580 write_object_vrml(fp, base->object);
2587 fprintf(fp, "\n# Visible Objects\n\n");
2588 fprintf(fp, "Separator {\n");
2592 write_camera_vrml(fp, G.scene->camera);
2594 /* THEN:The Objects */
2596 base= G.scene->base.first;
2598 if(base->object->type== OB_MESH) {
2599 if(base->lay & G.scene->lay) {
2600 write_object_vrml(fp, base->object);
2615 /* ******************************* WRITE DXF ***************************** */
2617 #define write_group(id,data) fprintf(fp, "%d\n%s\n", id, data)
2619 /* A completely wacky function to try and make good
2620 indexed (AutoCAD index) values out of straight rgb
2623 static int rgb_to_dxf_col (float rf, float gf, float bf)
2625 int r= (int) (rf*255.0f);
2626 int g= (int) (gf*255.0f);
2627 int b= (int) (bf*255.0f);
2631 /* Grayscale value */
2632 if (((int)r/10)==((int)g/10) && ((int)g/10)==((int)b/10)) ret= 250+((int)r/51);
2633 /* A nice chroma value */
2635 rgb_to_hsv (rf,gf,bf,&h,&s,&v);
2637 ret= (int) (10.0f + (h*239.0f));
2640 /* If its whitish make the index odd */
2641 if (s<.5 || v>.5) if(ret%2) ret++;
2647 /* And its completely wacky complement */
2649 static void dxf_col_to_rgb (int cid, float *rf, float *gf, float *bf)
2653 /* Grayscale values */
2654 if (cid>=250 && cid <= 255) {
2655 *rf= *gf= *bf= (float) ((cid-250)*51)/255;
2656 CLAMP(*rf, 0.0, 1.0);
2657 CLAMP(*gf, 0.0, 1.0);
2658 CLAMP(*bf, 0.0, 1.0);
2661 } else if (cid<10) {
2699 /* Get chroma values */
2701 h= (float) (cid-10)/239;
2704 /* If its odd make it a bit whitish */
2705 if (cid%2) { s=.75; v= 0.25;
2706 } else { s= 0.25; v= 0.75;}
2708 hsv_to_rgb (h, s, v, rf, gf, bf);
2712 static void write_mesh_dxf(FILE *fp, Mesh *me)
2720 replace_chars(str, me->id.name+2);
2722 write_group(0, "BLOCK");
2724 write_group(2, str); /* The name */
2726 write_group(8, "Meshes"); /* DXF Layer */
2727 write_group(70, "64"); /* DXF block flags */
2729 write_group(10, "0.0"); /* X of base */
2730 write_group(20, "0.0"); /* Y of base */
2731 write_group(30, "0.0"); /* Z of base */
2733 write_group(3, str); /* The name (again) */
2735 write_group(0, "POLYLINE"); /* Start the mesh */
2736 write_group(66, "1"); /* Vertices follow flag */
2737 write_group(8,"Meshes"); /* DXF Layer */
2742 sprintf(str,"%d",rgb_to_dxf_col(ma->r,ma->g,ma->b));
2743 write_group(62, str); /* Color index */
2747 write_group(70, "64"); /* Polymesh mesh flag */
2749 fprintf(fp, "71\n%d\n", me->totvert); /* Total vertices */
2750 fprintf(fp, "72\n%d\n", me->totface); /* Total faces */
2752 /* Write the vertices */
2756 write_group(0, "VERTEX"); /* Start a new vertex */
2757 write_group(8, "Meshes"); /* DXF Layer */
2758 fprintf (fp, "10\n%f\n", mvert->co[0]); /* X cord */
2759 fprintf (fp, "20\n%f\n", mvert->co[1]); /* Y cord */
2760 fprintf (fp, "30\n%f\n", mvert->co[2]); /* Z cord */
2761 write_group(70, "192"); /* Polymesh vertex flag */
2766 /* Write the face entries */
2770 if (mface->v4 || mface->v3) {
2771 write_group(0, "VERTEX"); /* Start a new face */
2772 write_group(8, "Meshes");
2774 /* Write a face color */
2776 ma= me->mat[mface->mat_nr];
2778 sprintf(str,"%d",rgb_to_dxf_col(ma->r,ma->g,ma->b));
2779 write_group(62, str); /* Color index */
2782 else write_group(62, "254"); /* Color Index */
2784 /* Not sure what this really corresponds too */
2785 write_group(10, "0.0"); /* X of base */
2786 write_group(20, "0.0"); /* Y of base */
2787 write_group(30, "0.0"); /* Z of base */
2789 write_group(70, "128"); /* Polymesh face flag */
2792 fprintf (fp, "71\n%d\n", mface->v1+1);
2793 fprintf (fp, "72\n%d\n", mface->v2+1);
2794 fprintf (fp, "73\n%d\n", mface->v3+1);
2795 fprintf (fp, "74\n%d\n", mface->v4+1);
2796 } else if(mface->v3) {
2797 fprintf (fp, "71\n%d\n", mface->v1+1);
2798 fprintf (fp, "72\n%d\n", mface->v2+1);
2799 fprintf (fp, "73\n%d\n", mface->v3+1);
2805 write_group(0, "SEQEND");
2807 write_group(0, "ENDBLK");
2810 static void write_object_dxf(FILE *fp, Object *ob, int layer)
2817 write_group(0, "INSERT"); /* Start an insert group */
2819 sprintf(str, "%d", layer);
2820 write_group(8, str);
2822 replace_chars(str, id->name+2);
2823 write_group(2, str);
2825 fprintf (fp, "10\n%f\n", ob->loc[0]); /* X of base */
2826 fprintf (fp, "20\n%f\n", ob->loc[1]); /* Y of base */
2827 fprintf (fp, "30\n%f\n", ob->loc[2]); /* Z of base */
2829 fprintf (fp, "41\n%f\n", ob->size[0]); /* X scale */
2830 fprintf (fp, "42\n%f\n", ob->size[1]); /* Y scale */
2831 fprintf (fp, "43\n%f\n", ob->size[2]); /* Z scale */
2833 fprintf (fp, "50\n%f\n", (float) ob->rot[2]*180/M_PI); /* Can only write the Z rot */
2836 void write_dxf(char *str)
2842 if(BLI_testextensie(str,".blend")) str[ strlen(str)-6]= 0;
2843 if(BLI_testextensie(str,".ble")) str[ strlen(str)-4]= 0;
2844 if(BLI_testextensie(str,".dxf")==0) strcat(str, ".dxf");
2846 if (BLI_exists(str))
2847 if(saveover(str)==0)
2850 fp= fopen(str, "w");
2853 error("Can't write file");
2856 strcpy(videosc_dir, str);
2860 /* The header part of the DXF */
2862 write_group(0, "SECTION");
2863 write_group(2, "HEADER");
2864 write_group(0, "ENDSEC");
2866 /* The blocks part of the DXF */
2868 write_group(0, "SECTION");
2869 write_group(2, "BLOCKS");
2871 /* Write all the meshes */
2872 me= G.main->mesh.first;
2875 write_mesh_dxf(fp, me);
2880 write_group(0, "ENDSEC");
2882 /* The entities part of the DXF */
2884 write_group(0, "SECTION");
2885 write_group(2, "ENTITIES");