domino.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 
00007 /* Domino.C external Startup Preset for OpenFX animate module            */
00008 /*                                                                         */
00009 
00010 #include <math.h>
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <windows.h>
00014 
00015 #include "domino.h"
00016 
00017 #if __ZTC__ || __SC__
00018 #ifndef max
00019 #define max(a,b)  ( ((a) > (b)) ? (a) : (b) )
00020 #endif
00021 #endif
00022 
00023 #if __X__MIPS__
00024 BOOL WINAPI _CRT_INIT(HINSTANCE ,DWORD , LPVOID );
00025 #endif
00026 
00027 static HINSTANCE hDLLinstance=NULL; /* use to pick up resources from DLL   */
00028 
00029 #include "..\animate\astruct.h"
00030 #include "..\animate\dstruct.h"
00031 
00032 static BOOL CALLBACK DominoDlg(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
00033 
00034 
00035 #if __WATCOMC__
00036 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00037 #else
00038 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00039 #endif
00040   switch (dwReason) {
00041     case DLL_PROCESS_ATTACH:
00042 #if __X__MIPS__
00043       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00044 #endif
00045       hDLLinstance = hDLL;  /* handle to DLL file */
00046       break;
00047     case DLL_PROCESS_DETACH:
00048 #if __X__MIPS__
00049       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00050 #endif
00051       break;
00052   }
00053 return (int)TRUE;
00054 }
00055 
00056 #if __SC__
00057 #pragma startaddress(LibMain)
00058 #endif
00059 
00060 /*******************************************************************\
00061 |                Code that define the preset                        |
00062 \*******************************************************************/
00063 
00064 static int rows=1,ndoms=50,nint=5;    // rows and how many and  time interval (frames)
00065 static double fint=0.85,fstep=1.5, // of height and width 
00066               fangle=30.0,langle=75.0,  // partial angle - full angle
00067               freq=2.0,ampx=0.0,ampz=0.0;  // for Z and X offsets
00068 static double Phi=0.0;
00069 static BOOL   bPath=FALSE,bAlign=FALSE; 
00070 
00071 
00072 double GetPathPos(double t, int nPath, vector pPath[], double pathParam[], double *x, double *y, double *z){
00073   int l,id=0;
00074   double f,dx,dy;
00075   for(l=1;l<nPath;l++)if(t < pathParam[l])break; else id++;
00076   if(id >= nPath-1){
00077     *x = pPath[nPath-1][0]; *y = pPath[nPath-1][1]; *z = pPath[nPath-1][2];
00078     dx=pPath[nPath-1][0] - pPath[nPath-2][0];
00079     dy=pPath[nPath-1][1] - pPath[nPath-2][1];
00080   }
00081   else{
00082     f = (t - pathParam[id])/(pathParam[id+1] - pathParam[id]);
00083     *x = pPath[id][0] + f*((dx=pPath[id+1][0] - pPath[id][0])); 
00084     *y = pPath[id][1] + f*((dy=pPath[id+1][1] - pPath[id][1]));
00085     *z = pPath[id][2] + f*(pPath[id+1][2] - pPath[id][2]);
00086   }
00087   return atan2(dx,dy)*180.0/3.1415926;
00088 }
00089 
00090 double PositionOnPath(position *fpos, long startY, double spacing, int id, int n,
00091                     int nPath, vector pPath[], double pathLength, double pathParam[],long xpos){
00092   double pscale,x,y,z,direction;
00093   pscale = (spacing * (double)n)/pathLength;
00094   direction=GetPathPos((double)id/(double)n,nPath,pPath,pathParam,&x,&y,&z);
00095   fpos->finish[0]=xpos+x*pscale;
00096   fpos->finish[1]=startY+y*pscale;
00097   fpos->finish[2]=z*pscale;
00098   return direction;
00099 }
00100 
00101  
00102 BOOL _ExternalPreset(HWND parent, ANI_STRUCTURE *lpevi){
00103  object   *Op;
00104  position *Pp,*Pc;
00105  align    *Ap;
00106  char f_name[256],f_dir[256],name[32];
00107  char f_path[256],d_path[256];
00108  long xmax,ymax,zmax,size,xmin,ymin,zmin;
00109  short s;
00110  int i,j,k,l,nf;
00111  double rey,pathLength,pathParam[100],direction;
00112  long xpos=0,zpos=0;
00113  long nPath;
00114  vector pathP[100]; // max 100 points
00115  lpEVI=lpevi;
00116  LoadString(hDLLinstance,IDS_TITLE,f_name,256);
00117  //MessageBox(parent,f_name,"Domino Fall",MB_OK);
00118  DialogBox(hDLLinstance,MAKEINTRESOURCE(ID_DIALOG1),GetFocus(),(DLGPROC)DominoDlg);
00119  nf=ndoms*nint+nint;
00120  SetNumberOfFrames((short)nf);
00121  /* add Camera position channel and move the camera - Camera always present */
00122  if(CreatePosition(FirstNp,1,(short)Nframes) == NULL)return FALSE;
00123  Pc=FirstNp->fpos;
00124  FirstNp->fpos->finish[0]=0;
00125  FirstNp->fpos->finish[1]=TVpointY+TVsizeY*2;
00126  FirstNp->fpos->finish[2]=0;
00127  /* create a Target Actor and Costume default at (0,0,0) */
00128  if(CreateNode() == NULL)return FALSE;
00129  strcpy(MainNp->actorname,"Target");
00130  MainNp->type=TARGET;
00131  if((Op=CreateCostume(MainNp,1,(short)Nframes)) == NULL)return FALSE;
00132  Op->type=MainNp->type;
00133  /* Now point the Camera to Look at the target */
00134  if(CreateAlign(FirstNp,1,(short)Nframes) == NULL)return FALSE;
00135  FirstNp->fali->type=TRACK; FirstNp->fali->topath=MainNp;
00136  /* Add Actors with Chosen Model for Costume */
00137  strcpy(f_dir,gszHomeDir); strcat(f_dir,"objects"); strcpy(f_name,"*.mfx");
00138  if(SelectFileName(0,f_name,f_dir,"Choose Model",
00139                    "(*.MFX)|*.mfx|",parent) != TRUE)return FALSE;
00140  if(bPath){
00141    strcpy(d_path,gszHomeDir); strcat(d_path,"presets"); strcpy(f_path,"*.txt");
00142    if(SelectFileName(0,f_path,d_path,"Choose Path",
00143                      "(*.TXT)|*.txt|",parent) != TRUE)bPath=FALSE;
00144    else{ // read path
00145      FILE *pPathFile=NULL;
00146      if((pPathFile=fopen(f_path,"r")) == NULL)bPath=FALSE;
00147      else{
00148        fscanf(pPathFile,"%ld",&nPath); nPath = min(nPath,100);
00149        for(l=0,pathLength=0.0;l<nPath;l++){
00150          fscanf(pPathFile,"%lf %lf %lf",&pathP[l][0],&pathP[l][1],&pathP[l][2]);
00151          if(l > 0)pathLength += sqrt((pathP[l][0]-pathP[l-1][0])*(pathP[l][0]-pathP[l-1][0])+
00152                                      (pathP[l][1]-pathP[l-1][1])*(pathP[l][1]-pathP[l-1][1])+
00153                                      (pathP[l][2]-pathP[l-1][2])*(pathP[l][2]-pathP[l-1][2]));
00154          pathParam[l]=pathLength;
00155        }
00156        fclose(pPathFile);
00157        for(l=0;l<nPath;l++)pathParam[l] /= pathLength;
00158 //{char xx[256]; sprintf(xx,"%ld %lf (%lf %lf %lf)",l,pathParam[l],pathP[l][0],pathP[l][1],pathP[l][2]);
00159 //MessageBox(NULL,xx,"path",MB_OK);}}
00160      }
00161    }
00162  }
00163  xpos=0.0;
00164  for(l=0;l<rows;l++){ 
00165    s=1; 
00166    for(k=0;k<ndoms;k++){
00167      if(CreateNode() == NULL)return FALSE;
00168      sprintf(name,"%Domino%ld",k+1);
00169      strcpy(MainNp->actorname,name);
00170      MainNp->type=NORMAL;
00171      if((Op=CreateCostume(MainNp,1,(short)Nframes)) == NULL)return FALSE;
00172      Op->type=MainNp->type;
00173      strcpy(Op->name,f_name);
00174      Op->morph=NO;
00175      if(LoadMeshObject(f_name,Op,YES,YES,NO) == FAIL)DeleteCostume(MainNp,1);
00176      /* put camera so that it shows object */
00177      if(l ==0 && k == 0 && Op->npoints > 0 && Op->points != NULL){
00178        xmax=ymax=zmax=size= -MAXUNIT;
00179        xmin=ymin=zmin=MAXUNIT;
00180        for(i=0;i<Op->npoints;i++){
00181          if(Op->points[i][0]  > xmax)xmax=Op->points[i][0];
00182          if(Op->points[i][1]  > ymax)ymax=Op->points[i][1];
00183          if(Op->points[i][2]  > zmax)zmax=Op->points[i][2];
00184          if(Op->points[i][0]  < xmin)xmin=Op->points[i][0];
00185          if(Op->points[i][1]  < ymin)ymin=Op->points[i][1];
00186          if(Op->points[i][2]  < zmin)zmin=Op->points[i][2];
00187        }
00188        size=max(xmax-xmin,ymax-ymin);
00189        size=max(size,zmax-zmin);
00190        Pc->finish[1]=TVpointY+max(size*3,TVsizeY*2);
00191      }
00192      if(CreatePosition(MainNp,1,(short)Nframes) == NULL)return FALSE;
00193      rey=(double)(((zmax-zmin)*fint * k))/(double)(((zmax-zmin)*fint * ndoms));
00194      if(bPath){
00195        direction=PositionOnPath(MainNp->fpos,TVpointY,(zmax-zmin)*fint,k,ndoms,nPath,pathP,pathLength,pathParam,xpos);
00196        if(bAlign)Phi=-direction;
00197      }
00198      else{
00199        MainNp->fpos->finish[0]=xpos+(long)((double)(xmax-xmin)*ampx*sin(rey*3.14159*freq));
00200        MainNp->fpos->finish[1]=TVpointY+((zmax-zmin)*fint * k);
00201        MainNp->fpos->finish[2]=zpos+(long)((double)(zmax-zmin)*ampz*sin(rey*3.14159*freq));
00202      }
00203      if((Ap=CreateAlign(MainNp,1,s)) == NULL)return FALSE;
00204      Ap->type=TWEEN;
00205      Ap->im=0; Ap->ima=0.0;
00206      Ap->alpha=(0.0);  Ap->phi=Phi;
00207      if((Ap=CreateAlign(MainNp,s+1,s+nint-1)) == NULL)return FALSE;
00208      Ap->type=TWEEN;
00209      Ap->im=0; Ap->ima=0.0;
00210      Ap->alpha=(-fangle); Ap->phi=Phi;
00211      if((Ap=CreateAlign(MainNp,s+nint,s+2*nint-1)) == NULL)return FALSE;
00212      Ap->type=TWEEN;
00213      Ap->im=0; Ap->ima=0.0;
00214      Ap->alpha=(-langle); Ap->phi=Phi;
00215      if(s+2*nint < nf){
00216        if((Ap=CreateAlign(MainNp,s+2*nint,nf)) == NULL)return FALSE;
00217        Ap->type=TWEEN;
00218        Ap->im=0; Ap->ima=0.0;
00219        Ap->alpha=(-langle);
00220      }
00221      s+=nint;
00222    }
00223    xpos += (xmax-xmin)*fstep;
00224  }
00225  return TRUE;
00226 }
00227 
00228 static BOOL CALLBACK DominoDlg(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){
00229  
00230  int i;
00231  char str[255];
00232  BOOL err;
00233  switch( msg ) {
00234    case WM_INITDIALOG:
00235      sprintf(str,"%d",ndoms);
00236      SetDlgItemText(hwnd,ID_NDOMS,str);
00237      sprintf(str,"%d",rows);
00238      SetDlgItemText(hwnd,ID_ROWS,str);
00239      sprintf(str,"%d",nint);
00240      SetDlgItemText(hwnd,ID_NINT,str);
00241      sprintf(str,"%5.2f",fint);
00242      SetDlgItemText(hwnd,ID_FINT,str);
00243      sprintf(str,"%5.2f",fstep);
00244      SetDlgItemText(hwnd,ID_FSTEP,str);
00245      sprintf(str,"%5.2f",fangle);
00246      SetDlgItemText(hwnd,ID_FANGLE,str);
00247      sprintf(str,"%5.2f",langle);
00248      SetDlgItemText(hwnd,ID_LANGLE,str);
00249      sprintf(str,"%5.2f",freq);
00250      SetDlgItemText(hwnd,ID_FREQ,str);
00251      sprintf(str,"%5.2f",ampx);
00252      SetDlgItemText(hwnd,ID_AMPX,str);
00253      sprintf(str,"%5.2f",ampz);
00254      SetDlgItemText(hwnd,ID_AMPZ,str);
00255      sprintf(str,"%5.2f",Phi);
00256      SetDlgItemText(hwnd,ID_PHI,str);
00257      if(bPath)SendDlgItemMessage(hwnd,ID_PATH,BM_SETCHECK,TRUE,0);
00258      if(bAlign)SendDlgItemMessage(hwnd,ID_ALIGN,BM_SETCHECK,TRUE,0);
00259      //CentreDialogOnScreen(hwnd);
00260          return TRUE;
00261    case WM_COMMAND:
00262      switch(LOWORD(wparam)){
00263        case IDOK:
00264          if(GetDlgItemText(hwnd,ID_NDOMS,str,12) != 0)sscanf(str,"%d",&ndoms);
00265          if(GetDlgItemText(hwnd,ID_ROWS,str,12) != 0)sscanf(str,"%d",&rows);
00266          if(GetDlgItemText(hwnd,ID_NINT,str,12) != 0)sscanf(str,"%d",&nint);
00267          if(GetDlgItemText(hwnd,ID_FINT,str,12) != 0)sscanf(str,"%lf",&fint);
00268          if(GetDlgItemText(hwnd,ID_FSTEP,str,12) != 0)sscanf(str,"%lf",&fstep);
00269          if(GetDlgItemText(hwnd,ID_FANGLE,str,12) != 0)sscanf(str,"%lf",&fangle);
00270          if(GetDlgItemText(hwnd,ID_LANGLE,str,12) != 0)sscanf(str,"%lf",&langle);
00271          if(GetDlgItemText(hwnd,ID_FREQ,str,12) != 0)sscanf(str,"%lf",&freq);
00272          if(GetDlgItemText(hwnd,ID_AMPX,str,12) != 0)sscanf(str,"%lf",&ampx);
00273          if(GetDlgItemText(hwnd,ID_AMPZ,str,12) != 0)sscanf(str,"%lf",&ampz);
00274          if(GetDlgItemText(hwnd,ID_PHI,str,12) != 0)sscanf(str,"%lf",&Phi);
00275          if(SendDlgItemMessage(hwnd,ID_PATH,BM_GETCHECK,0,0))bPath=TRUE; else bPath=FALSE;
00276          if(SendDlgItemMessage(hwnd,ID_ALIGN,BM_GETCHECK,0,0))bAlign=TRUE; else bAlign=FALSE;
00277                  EndDialog(hwnd,TRUE);
00278        return(TRUE);
00279          default:
00280            break;
00281        }
00282            break;
00283    default: break;
00284  }
00285  return(FALSE);
00286 }
00287 

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