3DSCON1.C

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 2.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 - 2007 OpenFX Development Team
00004 -- */
00005 
00006 /* 3DSCON1.C */
00007 
00008 #define MODULE_SAPC
00009 
00010 #include <stdlib.h>
00011 #include <stdio.h>
00012 #include <math.h>
00013 #include <string.h>
00014 #include <windows.h>
00015 
00016 #undef OPAQUE
00017 
00018 #include "3dscon.h"
00019 
00020 extern HWND      dlg;
00021 extern HWND      hWndParent;
00022 extern HINSTANCE hInstance;
00023 
00024 extern BOOL CALLBACK DlgProcScale(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00025 extern BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00026 
00027 extern void UpdateProgress(FILE *);
00028 extern int (*X__pri)(const char *,...);
00029 
00030 #ifdef __ZTC__
00031   #ifdef DOS386
00032     #define _X__FAR
00033   #else
00034     #define _X__FAR _far
00035   #endif
00036 #endif
00037 
00038 #define DOS386
00039 #define _X__FAR
00040 #define PI  3.1415926
00041 
00042 #ifndef min
00043 #define min(a,b)  ( ((a) < (b)) ? (a) : (b) )
00044 #endif
00045 #ifndef max
00046 #define max(a,b)  ( ((a) > (b)) ? (a) : (b) )
00047 #endif
00048 
00049 typedef double vector[3];
00050 typedef long   point[3];
00051 typedef long   twoLongs[2];
00052 
00053 typedef struct MAPGLUE {
00054  float x,y;
00055 } mapglue;
00056 
00057 typedef struct WIREFRAME {
00058  long Np,Ne;
00059  twoLongs *e;
00060  point *p;
00061 } wireframe;
00062 
00063 typedef struct SKELETON {
00064  struct SKELETON _X__FAR *at,*next,*last;
00065  short  id,offset;
00066  vector u,v,w;
00067  double t[4][4];
00068  point  xyz,tp,bx[8];
00069  float  p[3];
00070  char   name[16];
00071  double weight;
00072 } skel;
00073 
00074 typedef struct VERTEX {
00075  struct VERTEX    *last;
00076  struct VERTEX    *next;
00077  struct VERTEX    **adj;
00078  struct SKELETON  *sp;
00079  struct MAPGLUE   *gp;
00080  char   status,pad;
00081  unsigned short id,n;
00082  float  p[3];
00083  long   xyz[3];
00084 } vertex;
00085 
00086 typedef struct EDGE {
00087  struct EDGE _X__FAR *last;
00088  struct EDGE _X__FAR *next;
00089  struct VERTEX _X__FAR *V[2];
00090 } edge;
00091 
00092 
00093 typedef struct FACE{
00094  struct FACE _X__FAR *last;
00095  struct FACE _X__FAR *next;
00096  struct VERTEX _X__FAR *V[3];
00097  unsigned char color[3],matcol[3],texture,brush,axis;
00098 } face;
00099 
00100 typedef struct {
00101        vertex Origin;
00102        long Xdirection[3];
00103        long Ydirection[3];
00104        long Zdirection[3];
00105        vertex Offset;
00106 } axis;
00107 
00108 typedef struct {
00109   char  name[8];
00110   short Nc,Np,flag;
00111   vertex **Vc,**Vp;
00112 } bezier_curve;
00113 
00114 typedef struct tagIMAGEMAP {
00115 short   in_use;
00116 char    N[32],*S,*R,*B;
00117 char    s,r,b;
00118 short   sp,rp,bp;
00119 char    Type,Map,Lock;
00120 short   AnimFF,AnimDL,AnimLF,Angle,SkId;
00121 point   P,X,Y;
00122 skel    *Sk;
00123 vertex  *Vlock[3];
00124 unsigned char d_colour[3],s_colour[3],a_colour[3];
00125 } IMAGEMAP;
00126 
00127 typedef struct tagSHADERAXIS {
00128 char    N[32];
00129 point   P,X,Y;
00130 } SHADERAXIS;
00131 
00132 typedef struct tagXSHADER {
00133   long in_use;
00134   char name[128],*parameters;
00135 } XSHADER;
00136 
00137 short silent_3ds=0;
00138 
00139 #define PLANE            0              /* brush map types */
00140 #define CYLINDER         1
00141 #define PLANE_MOZIAC     2
00142 #define CYLINDER_MOZIAC  3
00143 #define MAP_BY_VERTEX    4
00144 #define OPAQUE           0              /* brush color 0   */
00145 #define CLEAR            1
00146 #define GLOSS            0              /* brush shiny     */
00147 #define MATT             1
00148 #define INEDITOR         6
00149 
00150 #define TRIVIEW   0
00151 #define FRONTVIEW 1
00152 #define TOPVIEW   2
00153 #define RIGHTVIEW 3
00154 #define PERSPVIEW 4
00155 #define BIGWIN    0
00156 #define TRITOP    1
00157 #define TRIFRONT  2
00158 #define TRIRIGHT  3
00159 
00160 /* definition of unit dimensions */
00161 #define UNIT         32768L
00162 #define UNIT2        65536L
00163 #define MINUNIT       2048L     // was 1024 trying larger to prevent error
00164 #define MAXUNIT    4194304L     //1073741824L
00165 #define PI2            PI/2
00166 /* definition of flags and identifiers*/
00167 #define DESELECTED       0
00168 #define SELECTED         1
00169 #define HIDDEN           2
00170 #define NO               0
00171 #define YES              1
00172 #define OK               1   // used to be 0
00173 #define FAIL            -1   // used to be 1
00174 
00175 #define STATICBRUSH      0
00176 #define ANIMBRUSH        1
00177 #define BUMPBRUSH        2
00178 #define REFLBRUSH        3
00179 #define PSETBRUSH        4
00180 
00181 #define G_FILEOPENFAIL 1000
00182 
00183 unsigned short _cdecl ERRORFLAG= 1,
00184                LOADOPERATION=0,
00185                Xres=640,Yres=480,Xres1=639,Yres1=479,
00186                StringToggle=0,CursorToggle=0,HelpBarToggle=0,Hires1=0,
00187                UNDO_ON=YES,EDIT_ACTION=NO,
00188                zoom_redraw=NO,zoom_abort=NO,
00189                WindowBox_view=0,icon_arrow_ids=1;
00190 char   *WorkDrive;                      /* from TEMP environment */
00191 char   ruler_name[8]={"None "};
00192 double ruler=(double)UNIT;
00193 long   rulerx=0,rulery=0,rulerz=0,    /* relative */
00194        lrulerx=0,lrulery=0,lrulerz=0, /* origin of ruler */
00195        draw_grid_on=0,grid_on=0,grid_size=UNIT;
00196 long NpointerX=0,NpointerY=0,NpointerZ=0
00197     ,TVpointX=(-UNIT),TVpointY=(-UNIT),TVpointZ=(-UNIT)
00198     ,TVsizeX=UNIT2,TVsizeY=UNIT2,TVsizeZ=UNIT2;
00199 
00200 wireframe w_frame={0,0,NULL,NULL};
00201 bezier_curve *Bcurves=NULL;
00202 
00203 axis _X__FAR ObjectAxis={NULL,NULL,0,0,
00204                       0L,0L,0L,
00205                       0,0L,0L,
00206                       UNIT,0L,0L,
00207                       0L,UNIT,0L,
00208                       0L,0L,0L};
00209 
00210 vertex _X__FAR *MainVp=NULL,*LastToolVertex=NULL;
00211 edge   _X__FAR *MainEp=NULL;
00212 face   _X__FAR *MainFp=NULL;
00213 skel   _X__FAR *MainSp=NULL,*LastSkelVertex=NULL,*FirstSp=NULL,
00214            *current_skeleton=NULL;
00215 unsigned long   Nvert=0,NvertSelect=0,NvertDeselect=0,NvertGlue=0,
00216                 Nface=0,Nedge=0,Nskel=0,N_Bcurves=0;
00217 short        SelectedBrush = -1,FontLoaded=NO,ShadowFlag=NO,MirrorFlag = -1;
00218 short    nImaps=0,nLockediMaps=0,nImap4=0,nSha=0;
00219 SHADERAXIS iSha[64];
00220 IMAGEMAP iMap[64];
00221 XSHADER  xShaders[8];
00222 unsigned char _X__FAR LocalColour[7]={0,0,0,0,128,128,128},
00223               _X__FAR FaceColour[3]={255,255,255},
00224               _X__FAR ShaderColour[3]={128,128,128};
00225 long DefaultBrilliance=8,DefaultTransparency=8,DefaultReflectivity=0,
00226      DefaultTexture=0,DefaultShiny=0,DefaultSmoothing=0;
00227 
00228 void SaveObject(char *Filename, short status);
00229 void WriteTextures(short );
00230 void WriteVertices(short );
00231 void WriteEdges(short );
00232 void WriteFaces(short );
00233 void WriteSkeleton(void);
00234 
00235 
00236 #define BRUSHBIT   0x40
00237 #define SMOOTHBIT  0x80
00238 #define TILEBIT    0x20;
00239 static vertex **p=NULL;
00240 static FILE *fp,*fo;
00241 static long newface,newedge,newvert,newglue,minX,minY,minZ,maxX,maxY,maxZ;
00242 static char str[10];
00243 static vertex *vlast;
00244 static axis _X__FAR TempAxis;
00245 static face _X__FAR *FaceBase;
00246 void outsht(short sht){
00247  fputc((char)(sht >>  8),fo);
00248  fputc((char)(sht      ),fo);
00249 }
00250 void outlng(long lng){
00251  fputc((char)(lng >> 24),fo);
00252  fputc((char)(lng >> 16),fo);
00253  fputc((char)(lng >>  8),fo);
00254  fputc((char)(lng      ),fo);
00255 }
00256 short intriview(vertex _X__FAR *vp){return 1;}
00257 void Output_Message(short id){;}
00258 void disinform(void){;}
00259 void inform(char *aa){;}
00260 void pause(short delay){;}
00261 void DrawOldMarker(void){;}
00262 short RequestItemSelect(short Nitems, char **ItemList, char *Message){
00263  return 1;
00264 }
00265 
00266 
00267 /* uncomment to allow saving of mapping co-ords */
00268 #define __RSF__
00269 #include "3dssave.c"
00270 
00271 static void mes3ds(char *s){
00272 // if(silent_3ds)return;
00273 // X__pri("%s",s);
00274 }
00275 
00276 void CreateEdge1(vertex _X__FAR *v1, vertex _X__FAR *v2, edge *ep){
00277  edge _X__FAR *temp;
00278  if(( temp=(edge _X__FAR *)malloc(sizeof(edge)) ) == NULL){
00279    mes3ds("out of memory\n");
00280    return;
00281  }
00282  if(MainEp != NULL)MainEp->next=temp;
00283  temp->last=MainEp;
00284  temp->V[0]=v1;
00285  temp->V[1]=v2;
00286  temp->next=NULL;
00287  MainEp=temp;
00288  Nedge++;
00289 }
00290 
00291 void CreateFace(vertex _X__FAR *v1, vertex _X__FAR *v2,
00292                 vertex _X__FAR *v3, short check){
00293  face _X__FAR *temp;
00294  if(check){
00295  temp=MainFp;  /* check to see if face exists */
00296  while(temp != NULL)   /* check to see if this face already exists */
00297  {if(   ((temp->V[0] == v1) && (temp->V[1] == v2) && (temp->V[2] == v3))
00298      || ((temp->V[0] == v1) && (temp->V[1] == v3) && (temp->V[2] == v2))
00299      || ((temp->V[0] == v2) && (temp->V[1] == v3) && (temp->V[2] == v1))
00300      || ((temp->V[0] == v2) && (temp->V[1] == v1) && (temp->V[2] == v3))
00301      || ((temp->V[0] == v3) && (temp->V[1] == v1) && (temp->V[2] == v2))
00302      || ((temp->V[0] == v3) && (temp->V[1] == v2) && (temp->V[2] == v1))
00303   )return;
00304  temp=temp->last;}}
00305  if(( temp=(face _X__FAR *)malloc(sizeof(face)) ) == NULL){
00306    mes3ds("out of memory\n");
00307    return;
00308  }
00309  if(MainFp != NULL)MainFp->next=temp;
00310  temp->last=MainFp;
00311  temp->V[0]=v1;
00312  temp->V[1]=v2;
00313  temp->V[2]=v3;
00314  temp->next=NULL;
00315  temp->color[0]=FaceColour[0];
00316  temp->color[1]=FaceColour[1];
00317  temp->color[2]=FaceColour[2];
00318  temp->matcol[0]=(((unsigned char)(DefaultBrilliance) << 4)
00319                 + (ShaderColour[0] >> 4) );
00320  temp->matcol[1]=(((unsigned char)(DefaultTransparency) << 4)
00321                 + (ShaderColour[1] >> 4) );
00322  temp->matcol[2]=(((unsigned char)(DefaultReflectivity) << 4)
00323                 + (ShaderColour[2] >> 4) );
00324  temp->texture=DefaultTexture;
00325  if(DefaultShiny)temp->texture     |= 0x20;  /* set bit #5 */
00326  if(DefaultSmoothing)temp->texture |= 0x80;  /* set smoothing bit */
00327  temp->brush=0;
00328  temp->axis=0;
00329  MainFp=temp;
00330  Nface++;
00331 }
00332 void CreateVertex(void){
00333  vertex _X__FAR *temp;
00334  if(( temp=(vertex _X__FAR *)malloc(sizeof(vertex)) ) == NULL){
00335    mes3ds("out of memory\n");
00336    return;
00337  }
00338  if(MainVp != NULL)MainVp->next=temp;
00339  temp->last=MainVp;
00340  temp->xyz[0]=NpointerX;
00341  temp->xyz[1]=NpointerY;
00342  temp->xyz[2]=NpointerZ;
00343  temp->next=NULL;
00344  temp->status=SELECTED;
00345  temp->sp=NULL;
00346  temp->gp=NULL;
00347  temp->adj=NULL;
00348  temp->n=0;
00349  temp->p[0]=0.0;
00350  temp->p[1]=0.0;
00351  temp->p[2]=0.0;
00352  MainVp=temp;
00353  Nvert++;
00354  NvertSelect++;
00355 }
00356 void CreateSkeleton(skel *at){
00357  skel _X__FAR *temp;
00358  int i,j;
00359  if(( temp=(skel _X__FAR *)malloc(sizeof(skel)) ) == NULL){
00360    mes3ds("out of memory\n");
00361    return;
00362  }
00363  if(MainSp != NULL)MainSp->next=temp;
00364  temp->last=MainSp;
00365  temp->xyz[0]=NpointerX;
00366  temp->xyz[1]=NpointerY;
00367  temp->xyz[2]=NpointerZ;
00368  temp->p[0]=temp->p[1]=temp->p[2]=0.0;
00369  temp->next=NULL;
00370  temp->at=at;
00371  temp->id=0;
00372  temp->offset=0;
00373  for(i=0;i<4;i++)
00374   for(j=0;j<4;j++){
00375     temp->t[i][j]=0.0;
00376     if(i == j)temp->t[i][j]=1.0;
00377   }
00378  MainSp=temp;
00379  Nskel++;
00380  if(Nskel == 1)strcpy(temp->name,"Root");
00381  else sprintf(temp->name,"%ld",Nskel-1);
00382 }
00383 static short CreateModelerBrush(void){
00384   long i;
00385   EDIT_ACTION=YES;
00386   if(nImaps > 62)return FAIL;
00387   iMap[nImaps].s=0;    iMap[nImaps].r=0;    iMap[nImaps].b=0;
00388   iMap[nImaps].S=NULL; iMap[nImaps].R=NULL; iMap[nImaps].B=NULL;
00389   iMap[nImaps].sp=100; iMap[nImaps].rp=50; iMap[nImaps].bp=50;
00390   for(i=0;i<3;i++){
00391     iMap[nImaps].d_colour[i]=0;
00392     iMap[nImaps].s_colour[i]=0;
00393     iMap[nImaps].a_colour[i]=0;
00394   }
00395   sprintf(iMap[nImaps].N,"unnamed_%ld",nImaps+1);
00396   {
00397     iMap[nImaps].P[0] = TVpointX + TVsizeX/10;
00398     iMap[nImaps].P[1] = TVpointY + TVsizeY/2;
00399     iMap[nImaps].P[2] = TVpointZ + TVsizeZ - TVsizeZ/10;
00400     iMap[nImaps].X[0] = TVpointX + TVsizeX - TVsizeX/10;
00401     iMap[nImaps].X[1] = TVpointY + TVsizeY/2;
00402     iMap[nImaps].X[2] = TVpointZ + TVsizeZ - TVsizeZ/10;
00403     iMap[nImaps].Y[0] = TVpointX + TVsizeX/10;
00404     iMap[nImaps].Y[1] = TVpointY + TVsizeY/2;
00405     iMap[nImaps].Y[2] = TVpointZ + TVsizeZ/10;
00406   }
00407   iMap[nImaps].Type = STATICBRUSH;    /* Still Anim */
00408   iMap[nImaps].Angle  = 360;          /* full rotation if required */
00409   iMap[nImaps].AnimFF = 1;
00410   iMap[nImaps].AnimDL = 1;
00411   iMap[nImaps].AnimLF = 1;
00412   iMap[nImaps].SkId = -1;        /* Brush is not assigned to any skeletal */
00413   iMap[nImaps].Sk = NULL;        /* component */
00414   iMap[nImaps].Lock = 0;         /* not locked */
00415   iMap[nImaps].Vlock[0] = NULL;  /* lock P */
00416   iMap[nImaps].Vlock[1] = NULL;  /* lock X */
00417   iMap[nImaps].Vlock[2] = NULL;  /* lock Y */
00418   iMap[nImaps].Map=MAP_BY_VERTEX;  /* brush map */
00419 //  iMap[nImaps].Map=PLANE;  /* brush map */
00420   nImap4++;
00421   nImaps++;
00422   return OK;
00423 }
00424 
00425 #define _DOS_GETDRIVE _dos_getdrive
00426 #define _DOS_SETDRIVE _dos_setdrive
00427 
00428 typedef struct tagMAP3DS {
00429   char    N[32];
00430   short   shiny,transp,evi;
00431   unsigned char d_colour[3];
00432 } MAP3DS;
00433 
00434 static float scale_3ds,gmax[3],gmin[3];
00435 static long posn,nJmaps=0;
00436 static FILE *fi;
00437 static MAP3DS *jMap;
00438 static unsigned drive;
00439 static vertex *vertex_object_start=NULL;
00440 
00441 void InsertInVertexList(vertex *vj, vertex *vi);
00442 void BuildEdges(void);
00443 
00444 static long GetLong(FILE *fp){
00445  long val;
00446  fread(&val,sizeof(long),1,fp);
00447  return val;
00448 }
00449 
00450 static short GetShort(FILE *fp, short *val){
00451  return fread(val,sizeof(short),1,fp);
00452 }
00453 
00454 static char GetByte(FILE *fp){
00455  char val;
00456 // fread(&val,sizeof(char),1,fp);
00457  val  = ((char)getc(fp));
00458  return val;
00459 }
00460 
00461 void get_material(long count, FILE *fi){
00462   char c,mapname[128],imagename[32],str[128];
00463   unsigned char r,g,b;
00464   unsigned short id,sv,id1,first_time;
00465   long cc,p,i,l,j;
00466   while(1){
00467     p=ftell(fi);  GetShort(fi,&id); cc=GetLong(fi);
00468     if(id < 0xa000 || id >= 0xafff)break;
00469     if(id == 0xa000){
00470       j=0; while((c=GetByte(fi)) != 0)mapname[j++]=c;
00471       mapname[j]=0;
00472       sprintf(str,"Material [%s]\n",mapname);
00473       mes3ds(str);
00474       first_time=1; nJmaps++;
00475       if(nJmaps == 1){
00476         if((jMap=(MAP3DS *)malloc(nJmaps*sizeof(MAP3DS))) == NULL){
00477           mes3ds("Out of memory\n"); return;
00478         }
00479       }
00480       else {
00481         if((jMap=(MAP3DS *)realloc(jMap,nJmaps*sizeof(MAP3DS))) == NULL){
00482           mes3ds("Out of memory\n"); return;
00483         }
00484       }
00485       jMap[nJmaps-1].evi = -1;
00486       strcpy(jMap[nJmaps-1].N,mapname);
00487     }
00488     else if(id == 0xa010){
00489       GetShort(fi,&id); l=GetLong(fi);
00490       if(id != 0x11){mes3ds("Error in material\n"); break;}
00491       r=GetByte(fi); g=GetByte(fi); b=GetByte(fi);
00492     }
00493     else if(id == 0xa020){
00494       GetShort(fi,&id); l=GetLong(fi);
00495       if(id != 0x11){mes3ds("Error in material\n"); break;}
00496       r=GetByte(fi); g=GetByte(fi); b=GetByte(fi);
00497       jMap[nJmaps-1].d_colour[0]=r;
00498       jMap[nJmaps-1].d_colour[1]=g;
00499       jMap[nJmaps-1].d_colour[2]=b;
00500     }
00501     else if(id == 0xa030){
00502       GetShort(fi,&id); l=GetLong(fi);
00503       if(id != 0x11){mes3ds("Error in material\n"); break;}
00504       r=GetByte(fi); g=GetByte(fi); b=GetByte(fi);
00505     }
00506     else if(id == 0xa200 || id == 0xa210 || id == 0xa220 || id == 0xa230){
00507       if(first_time){
00508         CreateModelerBrush();
00509         strcpy(iMap[nImaps-1].N,jMap[nJmaps-1].N);
00510         jMap[nJmaps-1].evi=nImaps-1;
00511         first_time=0;
00512       }
00513       if(id == 0xa200){mes3ds("Texture map "); id1=1;}
00514       if(id == 0xa210){mes3ds("Opacity map "); id1=2;}
00515       if(id == 0xa220){mes3ds("Reflect map "); id1=3;}
00516       if(id == 0xa230){mes3ds("Bump    map "); id1=4;}
00517       GetShort(fi,&id); l=GetLong(fi);
00518       if(id != 0x30){mes3ds("Error in material\n"); break;}
00519       GetShort(fi,&sv);
00520       sprintf(str,"Map value %d ",sv);
00521       mes3ds(str);
00522       GetShort(fi,&id); l=GetLong(fi);
00523       if(id != 0xa300){mes3ds("Error in material\n"); break;}
00524       j=0; while((c=GetByte(fi)) != 0)imagename[j++]=c;
00525       imagename[j]=0;
00526       sprintf(str,"Map [%s]\n",imagename);
00527       mes3ds(str);
00528       if(id1 == 1){
00529         iMap[nImaps-1].s=1;
00530         iMap[nImaps-1].sp=sv;
00531         iMap[nImaps-1].S=(char *)malloc(256);
00532         sprintf(iMap[nImaps-1].S,"%c:\\3ds2\\maps\\",drive+'@');
00533         strcat(iMap[nImaps-1].S,imagename);
00534       }
00535       if(id1 == 3){
00536         iMap[nImaps-1].r=1;
00537         iMap[nImaps-1].rp=sv;
00538         iMap[nImaps-1].R=(char *)malloc(256);
00539         sprintf(iMap[nImaps-1].R,"%c:\\3ds2\\maps\\",drive+'@');
00540         strcat(iMap[nImaps-1].R,imagename);
00541       }
00542       if(id1 == 4){
00543         iMap[nImaps-1].b=1;
00544         iMap[nImaps-1].bp=sv;
00545         iMap[nImaps-1].B=(char *)malloc(256);
00546         sprintf(iMap[nImaps-1].B,"%c:\\3ds2\\maps\\",drive+'@');
00547         strcat(iMap[nImaps-1].B,imagename);
00548       }
00549     }
00550     fseek(fi,p,SEEK_SET); for(i=0;i<cc;i++)GetByte(fi);
00551   }
00552 }
00553 
00554 void get_vertices(FILE *fi){
00555  float f1,f2,f3,fmin[3],fmax[3];
00556  unsigned short n,i;
00557  long l;
00558  GetShort(fi,&n);
00559  if((p=(vertex **)malloc((long)n*sizeof(vertex *))) == NULL){
00560   mes3ds("out of memory\n");
00561   return;
00562  }
00563  for(i=0;i<3;i++){
00564    fmin[i] =   1.e30;
00565    fmax[i] = - 1.e30;
00566  }
00567  for(i=0;i<n;i++){
00568    fread(&f1,sizeof(float),1,fi);
00569    fread(&f2,sizeof(float),1,fi);
00570    fread(&f3,sizeof(float),1,fi);
00571    if(f1 < fmin[0])fmin[0]=f1; if(f1 > fmax[0])fmax[0]=f1;
00572    if(f2 < fmin[1])fmin[1]=f2; if(f2 > fmax[1])fmax[1]=f2;
00573    if(f3 < fmin[2])fmin[2]=f3; if(f3 > fmax[2])fmax[2]=f3;
00574    if(f1 < gmin[0])gmin[0]=f1; if(f1 > gmax[0])gmax[0]=f1;
00575    if(f2 < gmin[1])gmin[1]=f2; if(f2 > gmax[1])gmax[1]=f2;
00576    if(f3 < gmin[2])gmin[2]=f3; if(f3 > gmax[2])gmax[2]=f3;
00577    CreateVertex();
00578    MainVp->p[0]=f1;
00579    MainVp->p[1]=f2;
00580    MainVp->p[2]=f3;
00581    p[i]=MainVp;
00582    MainVp->sp=MainSp;
00583  }
00584  for(i=0;i<3;i++){
00585    MainSp->p[i]=(fmax[i]+fmin[i])/2.0;
00586 //   MainSp->xyz[i]=(long)(scale_3ds*MainSp->p[i]);
00587  }
00588  GetShort(fi,&n); GetShort(fi,&l);
00589 //mes3ds("Next packet=%x\n",n);
00590  UpdateProgress(fi);
00591 }
00592 
00593 void get_mapping_coords(FILE *fi){
00594  float f1,f2;
00595  unsigned short n,i;
00596  long l;
00597  GetShort(fi,&n);
00598 //mes3ds("%d Vertices have mapping\n",n);
00599  for(i=0;i<n;i++){
00600    fread(&f1,sizeof(float),1,fi);
00601    fread(&f2,sizeof(float),1,fi);
00602 //printf("%ld\t\t %lf \t%lf\n",i,f1,f2);
00603    if((p[i]->gp=(mapglue _X__FAR *)malloc(sizeof(mapglue)) ) == NULL){
00604      mes3ds("out of memory\n");
00605      return;
00606    }
00607    NvertGlue++;
00608    p[i]->gp->x=f1;
00609    p[i]->gp->y=f2;
00610  }
00611  GetShort(fi,&n); l=GetShort(fi,&n);
00612 //mes3ds("Next packet=%x\n",n);
00613 }
00614 
00615 
00616 long GetMap(char *mapname){
00617   long i;
00618   if(nJmaps > 0){
00619     for(i=0;i<nJmaps;i++){
00620       if(strcmp(jMap[i].N,mapname) == 0)return i;
00621     }
00622   }
00623   return -1; /* no map found */
00624 }
00625 
00626 void InsertInVertexList(vertex *vj, vertex *vi){
00627  long i,j;
00628  if(vi->n > 0)for(i=0;i<vi->n;i++)if(vi->adj[i] == vj)return;
00629  if(vj->n > 0)for(j=0;j<vj->n;j++)if(vj->adj[j] == vi)return;
00630  if(vi->n == 0){
00631    if((vi->adj = (vertex **)malloc((vi->n + 1)*sizeof(vertex *))) == NULL){
00632      mes3ds("out of memory\n");  return;
00633    }
00634  }
00635  else{
00636    if((vi->adj = (vertex **)realloc(vi->adj,(vi->n + 1)*sizeof(vertex *))) == NULL){
00637      mes3ds("out of memory\n");  return;
00638    }
00639  }
00640  vi->adj[vi->n] = vj;
00641  (vi->n)++;
00642 }
00643 
00644 void get_faces(FILE *fi){
00645  char cc,matname[128],str[128];
00646  unsigned short i,n,nf;
00647  unsigned short a,b,c,f;
00648  long j,l,mn;
00649  edge *last_edge=MainEp;
00650  face **ff=NULL;
00651  GetShort(fi,&n); nf=n;
00652 //mes3ds("%d Faces ",n);
00653  if((ff=(face **)malloc((long)nf*sizeof(face *))) == NULL){
00654   mes3ds("out of memory\n");
00655   return;
00656  }
00657  for(i=0;i<n;i++){
00658   GetShort(fi,&a);  GetShort(fi,&b);  GetShort(fi,&c);  GetShort(fi,&f);
00659   CreateFace(*(p+a),*(p+b),*(p+c),0); *(ff+i)=MainFp;
00660   InsertInVertexList(*(p+b),*(p+a));
00661   InsertInVertexList(*(p+c),*(p+b));
00662   InsertInVertexList(*(p+a),*(p+c));
00663  }
00664  UpdateProgress(fi);
00665  BuildEdges();
00666  loop:
00667  if(GetShort(fi,&n) != 1){ 
00668    //MessageBox(NULL,"End of File",NULL,MB_OK);
00669    goto end; 
00670  }
00671  l=GetLong(fi);
00672 sprintf(str,"Next packet=%x  length=%ld\n",n,l);
00673 //MessageBox(NULL,str,"Packet",MB_OK);
00674  if(n == 0x4130){
00675 //MessageBox(NULL,"reading material name",NULL,MB_OK);
00676    j=0; while((cc=GetByte(fi)) != 0){
00677       //sprintf(str,"j=%ld cc=%ld",j,cc); if(MessageBox(NULL,str,"Matname",MB_YESNO) != IDYES)exit(0);
00678       matname[j++]=cc;
00679    }
00680    matname[j]=0;
00681    sprintf(str,"Material [%s] on surfaces\n",matname);
00682 //MessageBox(NULL,str,"Material in get faces",MB_OK);
00683    mes3ds(str);
00684    mn=GetMap(matname);
00685    GetShort(fi,&n);
00686    for(i=0;i<n;i++){
00687      GetShort(fi,&f);
00688      if(mn >= 0){
00689        for(j=0;j<3;j++){
00690          (*(ff+f))->color[j]=jMap[mn].d_colour[j];
00691        }
00692        (*(ff+f))->texture |= 0x20;  /* brilliance bit */
00693        if(jMap[mn].evi >= 0){                   /* brush is on */
00694          (*(ff+f))->texture |= 0x40;            /* image map on       */
00695          (*(ff+f))->brush = jMap[mn].evi;       /* image map          */
00696          (*(ff+f))->brush |= 0x80;              /* tiled by default   */
00697        }
00698      }
00699    }
00700 //MessageBox(NULL,"End of material in get faces",NULL,MB_OK);
00701    goto loop;
00702  }
00703  else if(n == 0x4150){ /* smothing group */
00704    if((l-6)/4 != nf){
00705      mes3ds("Error in face data\n"); return;
00706    }
00707    for(i=0;i<nf;i++){
00708      l=GetLong(fi);
00709      (*(ff+i))->texture |= 0x80;  /* smoothing */
00710    }
00711    goto loop;
00712  }
00713  end:
00714  if(ff != NULL)free(ff);
00715  UpdateProgress(fi);
00716 }
00717 
00718 
00719 void BuildEdges(void){
00720  long i;
00721  vertex *vp;
00722 //MessageBox(NULL,"Buiding Edges",NULL,MB_OK);
00723  vp=MainVp; while(vp != vertex_object_start){
00724   if(vp->n > 0)for(i=0;i<vp->n;i++)CreateEdge1(vp,vp->adj[i],NULL);
00725   vp=vp->last;
00726  }
00727 //MessageBox(NULL,"Built Edges",NULL,MB_OK);
00728 }
00729 
00730 void get_object(long count, FILE *fi){
00731   char c,objectname[64],str[128];
00732   unsigned short id;
00733   long cc,j,i,l;
00734   p=NULL; vertex_object_start=MainVp;
00735 //printf("loading Object\n");
00736   j=0; while((c=GetByte(fi)) != 0){
00737     objectname[j++]=c;
00738 //printf("j=%ld c=%c\n",j,c);
00739   }
00740   objectname[j]=0;
00741 //printf("object name loaded\n",j,c);
00742   sprintf(str,"\n");
00743   mes3ds(str);
00744   sprintf(str,"Object [%s]\n",objectname);
00745   mes3ds(str);
00746   GetShort(fi,&id); 
00747   l=GetLong(fi);
00748   if(id == 0x4100){  /* mesh object */
00749     CreateSkeleton(FirstSp);
00750     strcpy(MainSp->name,objectname);
00751     while(1){
00752       j=ftell(fi);  
00753       if(GetShort(fi,&id) != 1){
00754         //MessageBox(NULL,"end of file",NULL,MB_OK);
00755         break;
00756       }
00757       cc=GetLong(fi);
00758       if(id < 0x4001 || id > 0x5000)break;
00759 //printf("Object Section %x\t length=%ld\n",id,cc);
00760       if(id == 0x4110)get_vertices(fi);
00761       if(id == 0x4140)get_mapping_coords(fi);
00762       if(id == 0x4120)get_faces(fi);
00763       fseek(fi,j,SEEK_SET); for(i=0;i<cc;i++)GetByte(fi);
00764     }
00765   }
00766   else if(id == 0x4600){
00767     mes3ds("Light\n");
00768   }
00769   else if(id == 0x4700){
00770     mes3ds("Camera\n");
00771   }
00772   if(p != NULL)free(p);
00773 }
00774 
00775 void rescale_model(short option){
00776   vertex *vp;
00777   skel   *sp;
00778   char str[128];
00779   long i;
00780   float c[3],maxd=1.e-30;
00781   double scan;
00782   for(i=0;i<3;i++){
00783     FirstSp->p[i]=c[i]=(gmax[i]+gmin[i])/2.0;
00784     if(gmax[i]-gmin[i] > maxd)maxd=gmax[i]-gmin[i];
00785   }
00786   if(option == 0){
00787     scale_3ds=(float)MAXUNIT/2.0/maxd;
00788   }
00789   else if(option == 1){ /* scale set by param  centre from file */
00790     struct SC {double scan,xc,yc,zc,c;} Sc;
00791     Sc.xc=c[0]; Sc.yc=c[1]; Sc.zc=c[2]; Sc.c=1;
00792     Sc.scan=(double)(UNIT2)/(double)maxd;
00793     scale_3ds=(float)Sc.scan;
00794     if(DialogBoxParam(hInstance,MAKEINTRESOURCE(DLG_SCALE),hWndParent,
00795                      (DLGPROC)DlgProcScale,(LPARAM)&Sc)){
00796       scale_3ds=(float)Sc.scan;
00797       if(Sc.c == 1){c[0]=c[1]=c[2]=0.0;}
00798       else{c[0]=Sc.xc; c[1]=Sc.yc; c[2]=Sc.zc;}
00799     }
00800   }
00801   vp=MainVp; while(vp != NULL){
00802     for(i=0;i<3;i++)vp->xyz[i] = (long)(scale_3ds*(vp->p[i]-c[i]));
00803     vp=vp->last;
00804   }
00805   sp=MainSp; while(sp != NULL){
00806     for(i=0;i<3;i++)sp->xyz[i] = (long)(scale_3ds*(sp->p[i]-c[i]));
00807     sp=sp->last;
00808   }
00809 }
00810 
00811 BOOL Do3dsConvert(int argc, char **argv){
00812  float size=500.0;
00813  long count,fze,sc=1;
00814  unsigned short i,id;
00815  char c,argv1[128],str[128];
00816  if(argc < 2)return FALSE;
00817  strcpy(argv1,argv[1]);
00818  if((i=strlen(argv1)) < 4 || argv1[i-4] != '.')strcat(argv1,".3DS");
00819  nImaps=0; nLockediMaps=0;
00820  strcpy(iSha[0].N,"INTERNAL"); nSha=1;
00821  iSha[0].P[0]=0;          iSha[0].P[1]=0;         iSha[0].P[2]=TVpointZ;
00822  iSha[0].X[0]=TVsizeX/2;  iSha[0].X[1]=0;         iSha[0].X[2]=TVpointZ;
00823  iSha[0].Y[0]=0;          iSha[0].Y[1]=TVsizeZ/2; iSha[0].Y[2]=TVpointZ;
00824  xShaders[0].in_use=0; xShaders[1].in_use=0;
00825  xShaders[2].in_use=0; xShaders[3].in_use=0;
00826  xShaders[4].in_use=0; xShaders[5].in_use=0;
00827  xShaders[6].in_use=0; xShaders[7].in_use=0;
00828  CreateSkeleton(NULL); FirstSp=MainSp;
00829  drive=3;
00830  posn=0;
00831  if(argc > 3){
00832    i=3;
00833    while(i < argc){
00834      if(strcmp(argv[i],"-sc") == 0){
00835        i++; size=atof(argv[i]);
00836        scale_3ds=(float)MAXUNIT/2.0/size;
00837        sc=1;
00838      }
00839      else if(strcmp(argv[i],"-q") == 0)silent_3ds=1;
00840      i++;
00841    }
00842  }
00843  for(i=0;i<3;i++){
00844    gmax[i] = -1.0e30;
00845    gmin[i] =  1.0e30;
00846  }
00847 
00848  if((fi=fopen(argv1,"rb")) == NULL){
00849   mes3ds("Failed to open file\n");
00850   return FALSE;
00851  }
00852  GetShort(fi,&id); posn += 2;
00853  if(id != 0x4d4d){
00854   mes3ds("Not 3DS file\n"); goto EXIT;
00855  }
00856  fze=count=GetLong(fi); posn += 4;
00857  while(1){
00858    if(GetShort(fi,&id) != 1)goto EXIT;
00859    if(posn >= fze)goto EXIT;
00860    count=GetLong(fi);
00861 //MessageBox(NULL,"Reading Chunk",NULL,MB_OK);
00862    if(id == 0x3d3d){
00863      posn += 6;
00864      while(1){
00865        if(GetShort(fi,&id) != 1)goto EXIT; 
00866        if(posn >= fze)goto EXIT;
00867        count=GetLong(fi);
00868        if(id == 0x4000){
00869          UpdateProgress(fi);
00870 //MessageBox(NULL,"Getting Object",NULL,MB_OK);
00871          get_object(count,fi);
00872 //MessageBox(NULL,"Got Object",NULL,MB_OK);
00873          fseek(fi,posn,SEEK_SET);
00874        }
00875        if(id == 0xafff){
00876 //MessageBox(NULL,"Getting Material",NULL,MB_OK);
00877          UpdateProgress(fi);
00878          get_material(count,fi);
00879 //MessageBox(NULL,"Got Material",NULL,MB_OK);
00880          fseek(fi,posn,SEEK_SET);
00881        }
00882        posn += count;
00883        fseek(fi,posn,SEEK_SET);
00884      }
00885    }
00886    else{
00887 //MessageBox(NULL,"Skipping",NULL,MB_OK);
00888      posn += count;
00889      fseek(fi,posn,SEEK_SET);
00890      UpdateProgress(fi);
00891    }
00892  }
00893  EXIT:
00894  UpdateProgress(fi);
00895  fclose(fi);
00896 #if 0
00897  sprintf(str,"%ld Vertices   %ld Edges  and  %ld Faces\n",Nvert,Nedge,Nface);
00898  mes3ds(str);
00899  sprintf(str,"\txmin=%8.4f\txmax=%8.4f\n",gmin[0],gmax[0]);
00900  mes3ds(str);
00901  sprintf(str,"\tymin=%8.4f\tymax=%8.4f\n",gmin[1],gmax[1]);
00902  mes3ds(str);
00903  sprintf(str,"\tzmin=%8.4f\tzmax=%8.4f\n",gmin[2],gmax[2]);
00904  mes3ds(str);
00905 #endif
00906  if(dlg != NULL)DestroyWindow(dlg); dlg=NULL;
00907 
00908  if(sc)rescale_model(1);
00909  else  rescale_model(0);
00910  if(argc < 3)return FALSE;
00911  strcpy(argv1,argv[2]);
00912  if((i=strlen(argv1)) < 4 || argv1[i-4] != '.')strcat(argv1,".mfx");
00913  dlg=CreateDialog(hInstance,MAKEINTRESOURCE(DLGWRIT),hWndParent,(DLGPROC)DlgProc);
00914  SaveObject(argv1,1);
00915  return TRUE;
00916 }
00917 

Generated on Sun Apr 27 14:20:10 2014 for OpenFX by  doxygen 1.5.6