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