peas.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 // peas.c
00007 
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <math.h>
00011 #include <float.h>
00012 #include <windows.h>
00013 #include <gl/gl.h>
00014 
00015 #if __ZTC__ || __SC__
00016 #ifndef max
00017 #define max(a,b)  ( ((a) > (b)) ? (a) : (b) )
00018 #endif
00019 #endif
00020 
00021 #if __WATCOMC__
00022 #define PI 3.1415926
00023 #endif
00024 
00025 #if __X__MIPS__
00026 BOOL WINAPI _CRT_INIT(HINSTANCE ,DWORD , LPVOID );
00027 #endif
00028 
00029 static HINSTANCE hDLLinstance=NULL;
00030 
00031 #include "..\animate\memory.h" /* for memory definition */
00032 #include "..\animate\memdef.h" /* local names           */
00033 #include "defines.h"
00034 #include "rstruct.h"
00035 #include "peas.h"
00036 
00037 #define VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00038 #define VECSUM(a,b,c)   { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00039 #define VECSUB(a,b,c)   { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00040 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00041 #define DOT(a,b)        ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00042 
00043 #define IMOD(z,a) ((z) - ((z)/(a))*(a))
00044 #define PATTERN(x,y,z)   ( ((z)+16384L)*64536L +  \
00045                            ((y)+16384L)*32768L +  \
00046                            ((x)+16384L))
00047 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00048 #define FMOD(x,a) ((x) >= 0.0 ? fmod(x,a) :(a - fmod(-(x),a)))
00049 
00050 #include "randno.c"
00051 
00052 static long ip[27][3]={
00053    { 0, 0, 0},
00054    { 1, 0, 0},
00055    { 1, 1, 0},
00056    { 0, 1, 0},
00057    {-1, 1, 0},
00058    {-1, 0, 0},
00059    {-1,-1, 0},
00060    { 0,-1, 0},
00061    { 1,-1, 0},
00062    { 0, 0,-1},
00063    { 1, 0,-1},
00064    { 1, 1,-1},
00065    { 0, 1,-1},
00066    {-1, 1,-1},
00067    {-1, 0,-1},
00068    {-1,-1,-1},
00069    { 0,-1,-1},
00070    { 1,-1,-1},
00071    { 0, 0, 1},
00072    { 1, 0, 1},
00073    { 1, 1, 1},
00074    { 0, 1, 1},
00075    {-1, 1, 1},
00076    {-1, 0, 1},
00077    {-1,-1, 1},
00078    { 0,-1, 1},
00079    { 1,-1, 1}
00080  };
00081 
00082 #if __WATCOMC__
00083 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00084 #else
00085 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00086 #endif
00087   HANDLE ghMod;
00088   switch (dwReason) {
00089     case DLL_PROCESS_ATTACH:
00090 #if __X__MIPS__
00091       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00092 #endif
00093       hDLLinstance = hDLL;  /* handle to DLL file */
00094       break;
00095     case DLL_PROCESS_DETACH:
00096 #if __X__MIPS__
00097       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00098 #endif
00099       break;
00100   }
00101   return (int)TRUE;
00102 }
00103 
00104 #if __SC__
00105 #pragma startaddress(DllMain)
00106 #endif
00107 
00108 static int type=0,flatter=0;
00109 static double x=1.0,y=1.0,z=1.0;
00110 
00111 #if 0
00112 long CALLBACK ExternalTextureStartup(
00113 #else
00114 long _ExternalTextureStartup(
00115 #endif
00116   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00117  if(parameter_list[0] != '#'){
00118    MessageBox ( GetFocus(),
00119                 (LPCTSTR) "External texture: Startup",
00120                 (LPCTSTR) "Parameter list missing",
00121                 MB_OK | MB_SYSTEMMODAL );
00122  }
00123  else {
00124   parameter_list++;
00125   sscanf(parameter_list,"%f %f %f %ld %ld",
00126          &x,&y,&z,&type,&flatter);
00127  }
00128  return LoadAndCompileShader("peas");
00129 }
00130 
00131 #if 0
00132 long CALLBACK ExternalTextureMorph(
00133 #else
00134 long _ExternalTextureMorph(
00135 #endif
00136  char *parameter_list, double mr){
00137  double x_m,y_m,z_m;
00138  parameter_list++;
00139  sscanf(parameter_list,"%f %f %f",
00140         &x_m,&y_m,&z_m);
00141  x=x_m+(x-x_m)*mr;
00142  y=y_m+(y-y_m)*mr;
00143  z=z_m+(z-z_m)*mr;
00144  return 1;
00145 }
00146 
00147 #if 0
00148 long CALLBACK ExternalTextureProcedure(
00149 #else
00150 long _ExternalTextureProcedure(
00151 #endif
00152   long coord_type,  vector p, vector n,
00153   double alpha, double beta, double gamma,
00154   double bump,  double map_x,  double map_y,
00155   double *alpha_channel, unsigned char sc[3], double colour[3],
00156   double *reflectivity, double *transparency,
00157   X__SHADER *lpEVI
00158 ){
00159  static long N1=3;
00160  static double bradius1=0.55,lradius1=0.2;
00161  static long N2=1;
00162  static double bradius2=1.2,lradius2=1.0;
00163  long i,N;
00164  double a;
00165  vector u,v;
00166  double xx,yy,zz;
00167  double  dpoint[81];
00168  double dradius[81];
00169  vector pp[81];
00170  double dx,dy,dz,cx,cy,cz,zb,ccc,dc,zbb,radius,bradius,lradius;
00171  long ix,iy,iz;
00172  BOOL ins;
00173  long j,jid,id,pattern,patterni;
00174  double len,rad;
00175  vector ppc;
00176  if(type == 1){
00177    bradius=bradius2;
00178    lradius=lradius2;
00179    N=N2;
00180  }
00181  else{
00182    bradius=bradius1;
00183    lradius=lradius1;
00184    N=N1;
00185  }
00186  xx=alpha / x;
00187  yy=beta  / y;
00188  zz=gamma / z;
00189  xx *= 4.0;
00190  yy *= 4.0;
00191  zz *= 4.0;
00192 
00193  id=0;
00194  ix=FLOOR(xx);
00195  iy=FLOOR(yy);
00196  iz=FLOOR(zz);
00197  cx=xx-(double)ix;
00198  cy=yy-(double)iy;
00199  cz=zz-(double)iz;
00200  zb=-10.0;
00201  ins=TRUE;
00202  id = -1;
00203  len=sqrt(tAxis_u[0]*tAxis_u[0]+
00204           tAxis_u[1]*tAxis_u[1]+
00205           tAxis_u[2]*tAxis_u[2]);
00206  VECCOPY(-tAxis_n,u)
00207  VECSCALE(len,u,u)         // third axis
00208  VECCOPY(tAxis_o,v)
00209  VECSUM(v,cx*tAxis_u,v)
00210  VECSUM(v,cy*tAxis_v,v)
00211  VECSUM(v,cz*u,v)         // coord of point on surface
00212  for(j=0;j<27;j++){
00213  pattern=PATTERN(ix+ip[j][0],iy+ip[j][1],iz+ip[j][2]);
00214  pattern=IMOD(N*pattern,1900);
00215  for(i=0;i<N;i++){
00216    jid=j*N+i;
00217    patterni=pattern+i;
00218    VECSUM((double)ip[j],rand_v[patterni],ppc)
00219    VECSUM(tAxis_o,ppc[0]*tAxis_u,pp[jid])
00220    VECSUM(pp[jid],ppc[1]*tAxis_v,pp[jid])
00221    VECSUM(pp[jid],ppc[2]*u,      pp[jid])
00222    radius=(bradius+lradius*(rand_v[patterni][1]-0.5))*len;
00223    dradius[jid]=radius;
00224    dpoint[jid] = -1.0;
00225    dx=(v[0]-pp[jid][0]);
00226    dy=(v[1]-pp[jid][1]);
00227    dz=(v[2]-pp[jid][2]);
00228    if(fabs(dx) > radius)continue;
00229    if(fabs(dy) > radius)continue;
00230    if(fabs(dz) > radius)continue;
00231    if((a=sqrt(dx*dx+dy*dy+dz*dz)) < radius){
00232      dpoint[jid]=a;
00233      dc=(radius-a);
00234      if(dc > zb){
00235        id=jid;
00236        zb=dc;
00237      }
00238    }
00239  }
00240  }
00241  if(id >= 0 && ins){
00242    {
00243      VECCOPY(pp[id],ppc)
00244      rad=dradius[id];
00245      VECSUB(v,ppc,u)          //(v-pp) vector from centre of pattern
00246      if((a=sqrt((u[0])*(u[0])+
00247                 (u[1])*(u[1])+
00248                 (u[2])*(u[2]))) < rad){
00249        double mu,moddn,rmu;
00250        vector c,dn;
00251        mu=DOT(u,n);
00252        c[0]=ppc[0]+mu*n[0];
00253        c[1]=ppc[1]+mu*n[1];
00254        c[2]=ppc[2]+mu*n[2];
00255        VECSUB(v,c,dn)
00256        moddn=sqrt(dn[0]*dn[0]+dn[1]*dn[1]+dn[2]*dn[2]);
00257        rmu=sqrt(rad*rad-mu*mu);
00258        if(moddn < rmu){
00259          a=1.0-moddn/rmu;
00260          normalize(dn);
00261          if(flatter == 1)a=sin(a*1.57079);
00262          VECSUM((1.0-a)*dn,(a)*n,n)
00263          normalize(n);
00264        }
00265      }
00266    }
00267  }
00268  return 1;
00269 }
00270 
00271 void _ExternalTextureClose(X__SHADER *lpEVI){
00272  UnloadCompiledShader(tGLshaderID);
00273 }
00274 
00275 long _ExternalTextureProcedureGL(
00276   double bump_scale,
00277   unsigned char sc[3],
00278   unsigned char ac[3],
00279   X__SHADER *lpEVI
00280 ){
00281  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00282  SetUniformVector(tGLshaderID,"ScalingV",4.0/x,4.0/y,4.0/z);
00283  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00284  return 1;
00285 }
00287 
00288 void CentreDialogOnScreen(HWND hwnd){
00289  RECT rcDlg;
00290  long Xres,Yres;
00291  Yres=GetSystemMetrics(SM_CYSCREEN);
00292  Xres=GetSystemMetrics(SM_CXSCREEN);
00293  GetWindowRect(hwnd,&rcDlg);
00294  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00295  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00296  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00297  return;
00298 }
00299 
00300 static void SetColour(double *colour, HWND parent){
00301  CHOOSECOLOR cc;
00302  static COLORREF CustColours[16]={
00303    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00304    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00305    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00306    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00307  cc.lStructSize=sizeof(CHOOSECOLOR);
00308  cc.hwndOwner=parent;
00309  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00310  cc.lpCustColors=(LPDWORD)CustColours;
00311  cc.Flags= CC_RGBINIT;
00312  cc.lCustData=(DWORD)0;
00313  if(ChooseColor(&cc)){
00314    colour[0]=(double)GetRValue(cc.rgbResult);
00315    colour[1]=(double)GetGValue(cc.rgbResult);
00316    colour[2]=(double)GetBValue(cc.rgbResult);
00317  }
00318 }
00319 
00320 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00321 
00322 #if 0
00323 char * CALLBACK SetExternalParameters(
00324 #else
00325 char * _SetExternalParameters(
00326 #endif
00327   char *Op,                 /* string for the parameters                  */
00328   HWND hWnd,                /* parent window                              */
00329   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00330                              ){
00331  char szbuf[255],*Op1;
00332  if(Op != NULL){  /* parameters exist so read them off the list */
00333    Op1=Op;
00334    Op1++;
00335    sscanf(Op1,"%f %f %f %ld %ld",
00336          &x,&y,&z,&type,&flatter);
00337  }
00338  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00339               (DLGPROC)DlgProc) == FALSE)return Op;
00340  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00341  sprintf(szbuf,"# %.3f %.3f %.3f %ld %ld",
00342          x,y,z,type,flatter);
00343  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00344   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00345                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00346    return NULL;
00347  }
00348  strcpy(Op,szbuf);
00349  return Op;
00350 }
00351 
00352 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00353  char str[16];
00354  switch( msg ) {
00355    case WM_INITDIALOG:
00356      sprintf(str,"%.2f",x);
00357      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00358      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00359      sprintf(str,"%.2f",y);
00360      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00361      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00362      sprintf(str,"%.2f",z);
00363      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00364      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00365      if(type == 1)SendDlgItemMessage(hwnd,DLG_TYPE,BM_SETCHECK,TRUE,0);
00366      if(flatter == 1)SendDlgItemMessage(hwnd,DLG_FLATTER,BM_SETCHECK,TRUE,0);
00367      CentreDialogOnScreen(hwnd);
00368      return TRUE;
00369    case WM_COMMAND:
00370      switch(LOWORD(wparam)){
00371         case IDCANCEL:
00372           EndDialog(hwnd,FALSE);
00373           return(TRUE);
00374         case IDOK:
00375           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00376              EndDialog(hwnd,FALSE);
00377           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00378           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00379              EndDialog(hwnd,FALSE);
00380           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00381           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00382              EndDialog(hwnd,FALSE);
00383           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00384           if(SendDlgItemMessage(hwnd,DLG_TYPE,BM_GETCHECK,0,0))
00385             type=1; else type=0;
00386           if(SendDlgItemMessage(hwnd,DLG_FLATTER,BM_GETCHECK,0,0))
00387             flatter=1; else flatter=0;
00388           EndDialog(hwnd,TRUE);
00389           return(TRUE);
00390         default:
00391           break;
00392       }
00393       break;
00394     default: break;
00395  }
00396  return FALSE;
00397 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3