leather2.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 // leather2.c
00008 
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 #include <math.h>
00012 #include <float.h>
00013 #include <windows.h>
00014 #include <gl/gl.h>
00015 
00016 #if __ZTC__ || __SC__
00017 #ifndef max
00018 #define max(a,b)  ( ((a) > (b)) ? (a) : (b) )
00019 #endif
00020 #endif
00021 
00022 #if __WATCOMC__
00023 #define PI 3.1415926
00024 #endif
00025 
00026 #if __X__MIPS__
00027 BOOL WINAPI _CRT_INIT(HINSTANCE ,DWORD , LPVOID );
00028 #endif
00029 
00030 static HINSTANCE hDLLinstance=NULL;
00031 
00032 #include "..\animate\memory.h" /* for memory definition */
00033 #include "..\animate\memdef.h" /* local names           */
00034 #include "defines.h"
00035 #include "rstruct.h"
00036 #include "leather2.h"
00037 
00038 #define VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00039 #define VECSUM(a,b,c)   { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00040 #define VECSUB(a,b,c)   { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00041 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00042 #define DOT(a,b)        ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00043 
00044 #define IMOD(z,a) ((z) - ((z)/(a))*(a))
00045 #define PATTERN(x,y,z)   ( ((z)+16384L)*64536L +  \
00046                            ((y)+16384L)*32768L +  \
00047                            ((x)+16384L))
00048 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00049 #define FMOD(x,a) ((x) >= 0.0 ? fmod(x,a) :(a - fmod(-(x),a)))
00050 
00051 #if __WATCOMC__
00052 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00053 #else
00054 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00055 #endif
00056   HANDLE ghMod;
00057   switch (dwReason) {
00058     case DLL_PROCESS_ATTACH:
00059 #if __X__MIPS__
00060       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00061 #endif
00062       hDLLinstance = hDLL;  /* handle to DLL file */
00063       break;
00064     case DLL_PROCESS_DETACH:
00065 #if __X__MIPS__
00066       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00067 #endif
00068       break;
00069   }
00070   return (int)TRUE;
00071 }
00072 
00073 #if __SC__
00074 #pragma startaddress(DllMain)
00075 #endif
00076 
00077 static long iturb=1;
00078 static double x=1.0,y=1.0,z=1.0;
00079 
00080 #if 0
00081 long CALLBACK ExternalTextureStartup(
00082 #else
00083 long _ExternalTextureStartup(
00084 #endif
00085   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00086  if(parameter_list[0] != '#'){
00087    MessageBox ( GetFocus(),
00088                 (LPCTSTR) "External texture: Startup",
00089                 (LPCTSTR) "Parameter list missing",
00090                 MB_OK | MB_SYSTEMMODAL );
00091  }
00092  else {
00093   parameter_list++;
00094   sscanf(parameter_list,"%f %f %f %ld",
00095          &x,&y,&z,&iturb);
00096  }
00097  return LoadAndCompileShader("leather2");
00098 }
00099 
00100 #if 0
00101 long CALLBACK ExternalTextureMorph(
00102 #else
00103 long _ExternalTextureMorph(
00104 #endif
00105  char *parameter_list, double mr){
00106  double x_m,y_m,z_m;
00107  parameter_list++;
00108  sscanf(parameter_list,"%f %f %f",
00109         &x_m,&y_m,&z_m);
00110  x=x_m+(x-x_m)*mr;
00111  y=y_m+(y-y_m)*mr;
00112  z=z_m+(z-z_m)*mr;
00113  return 1;
00114 }
00115 
00116 #if 0
00117 long CALLBACK ExternalTextureProcedure(
00118 #else
00119 long _ExternalTextureProcedure(
00120 #endif
00121   long coord_type,  vector p, vector n,
00122   double alpha, double beta, double gamma,
00123   double bump,  double map_x,  double map_y,
00124   double *alpha_channel, unsigned char sc[3], double colour[3],
00125   double *reflectivity, double *transparency,
00126   X__SHADER *lpEVI
00127 ){
00128  // overlapping vallies (leather2)
00129  int i;
00130  BOOL ins;
00131  vector v,dn,vt;
00132  double t=0.48,dt=0.06,s=0.5,turb=0.4;
00133  double xx,yy,zz,noise;
00134  double xr,xl,yr,yl,zr,zl;
00135  xx=10.0*alpha / x;
00136  yy=10.0*beta  / y;
00137  zz=10.0*gamma / z;
00138  VECCOPY(n,dn)
00139  ins=FALSE;
00140  if(iturb){
00141    vTurbulence(v, xx, yy, zz);
00142    xx += v[0] * turb;
00143    yy += v[1] * turb;
00144    zz += v[2] * turb;
00145  }
00146  for(i=0;i<4;i++){
00147  nNoise(xx, yy, zz, &noise);
00148  if(noise > t && noise < t+3*dt){
00149    nNoise(xx+0.01, yy, zz, &xr);
00150    nNoise(xx-0.01, yy, zz, &xl);
00151    nNoise(xx, yy+0.01,zz, &yr);
00152    nNoise(xx, yy-0.01,zz, &yl);
00153    nNoise(xx, yy,zz+0.01, &zr);
00154    nNoise(xx, yy,zz-0.01, &zl);
00155    v[0]= -(xr-xl); v[1]= -(yr-yl); v[2]= -(zr-zl);
00156    normalize(v);
00157    VECSCALE(v[0],tAxis_unorm,vt)
00158    VECSUM(v[1]*tAxis_vnorm,vt,vt)
00159    VECSUM(-v[2]*tAxis_n,vt,vt)
00160    normalize(vt);
00161    if(noise < t+dt){
00162      s=(noise-t)/dt;
00163      s = -s*s;
00164    }
00165    else if(noise < t+2*dt){
00166      ins=TRUE;
00167      s=0.0;
00168    }
00169    else{
00170      s=1.0-(noise-(t+2*dt))/dt;
00171      s = s*s;
00172    }
00173    VECSUM(s*vt,n,n)
00174    normalize(n);
00175  }
00176  xx += 30; yy += 40; zz += 50;
00177  xx *= 1.01; yy *= 1.02; zz *= 1.03;
00178  }
00179  if(ins)VECCOPY(dn,n)
00180  return 1;
00181 }
00182 
00183 
00184 
00185 void _ExternalTextureClose(X__SHADER *lpEVI){
00186  UnloadCompiledShader(tGLshaderID);
00187 }
00188 
00189 long _ExternalTextureProcedureGL(
00190   double bump_scale,
00191   unsigned char sc[3],
00192   unsigned char ac[3],
00193   X__SHADER *lpEVI
00194 ){
00195  // Set any shader specific uniform variables here.
00196  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00197  return 1;
00198 }
00200 
00201 void CentreDialogOnScreen(HWND hwnd){
00202  RECT rcDlg;
00203  long Xres,Yres;
00204  Yres=GetSystemMetrics(SM_CYSCREEN);
00205  Xres=GetSystemMetrics(SM_CXSCREEN);
00206  GetWindowRect(hwnd,&rcDlg);
00207  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00208  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00209  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00210  return;
00211 }
00212 
00213 static void SetColour(double *colour, HWND parent){
00214  CHOOSECOLOR cc;
00215  static COLORREF CustColours[16]={
00216    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00217    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00218    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00219    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00220  cc.lStructSize=sizeof(CHOOSECOLOR);
00221  cc.hwndOwner=parent;
00222  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00223  cc.lpCustColors=(LPDWORD)CustColours;
00224  cc.Flags= CC_RGBINIT;
00225  cc.lCustData=(DWORD)0;
00226  if(ChooseColor(&cc)){
00227    colour[0]=(double)GetRValue(cc.rgbResult);
00228    colour[1]=(double)GetGValue(cc.rgbResult);
00229    colour[2]=(double)GetBValue(cc.rgbResult);
00230  }
00231 }
00232 
00233 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00234 
00235 #if 0
00236 char * CALLBACK SetExternalParameters(
00237 #else
00238 char * _SetExternalParameters(
00239 #endif
00240   char *Op,                 /* string for the parameters                  */
00241   HWND hWnd,                /* parent window                              */
00242   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00243                              ){
00244  char szbuf[255],*Op1;
00245  if(Op != NULL){  /* parameters exist so read them off the list */
00246    Op1=Op;
00247    Op1++;
00248    sscanf(Op1,"%f %f %f %ld",
00249          &x,&y,&z,&iturb);
00250  }
00251  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00252               (DLGPROC)DlgProc) == FALSE)return Op;
00253  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00254  sprintf(szbuf,"# %.3f %.3f %.3f %ld",
00255          x,y,z,iturb);
00256  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00257   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00258                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00259    return NULL;
00260  }
00261  strcpy(Op,szbuf);
00262  return Op;
00263 }
00264 
00265 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00266  char str[16];
00267  switch( msg ) {
00268    case WM_INITDIALOG:
00269      sprintf(str,"%.2f",x);
00270      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00271      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00272      sprintf(str,"%.2f",y);
00273      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00274      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00275      sprintf(str,"%.2f",z);
00276      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00277      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00278      if(!iturb)SendDlgItemMessage(hwnd,DLG_TURB,BM_SETCHECK,TRUE,0);
00279      CentreDialogOnScreen(hwnd);
00280      return TRUE;
00281    case WM_COMMAND:
00282      switch(LOWORD(wparam)){
00283         case IDCANCEL:
00284           EndDialog(hwnd,FALSE);
00285           return(TRUE);
00286         case IDOK:
00287           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00288              EndDialog(hwnd,FALSE);
00289           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00290           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00291              EndDialog(hwnd,FALSE);
00292           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00293           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00294              EndDialog(hwnd,FALSE);
00295           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00296           if(SendDlgItemMessage(hwnd,DLG_TURB,BM_GETCHECK,0,0))iturb=0;
00297           else iturb=1;
00298           EndDialog(hwnd,TRUE);
00299           return(TRUE);
00300         default:
00301           break;
00302       }
00303       break;
00304     default: break;
00305  }
00306  return FALSE;
00307 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3