stucco2.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 // stucco2.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 "stucco2.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 #if __WATCOMC__
00051 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00052 #else
00053 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00054 #endif
00055   HANDLE ghMod;
00056   switch (dwReason) {
00057     case DLL_PROCESS_ATTACH:
00058 #if __X__MIPS__
00059       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00060 #endif
00061       hDLLinstance = hDLL;  /* handle to DLL file */
00062       break;
00063     case DLL_PROCESS_DETACH:
00064 #if __X__MIPS__
00065       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00066 #endif
00067       break;
00068   }
00069   return (int)TRUE;
00070 }
00071 
00072 #if __SC__
00073 #pragma startaddress(DllMain)
00074 #endif
00075 
00076 static long   type=0,bumpy=0;
00077 static double x=1.0,y=1.0,z=1.0;
00078 
00079 #if 0
00080 long CALLBACK ExternalTextureStartup(
00081 #else
00082 long _ExternalTextureStartup(
00083 #endif
00084   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00085  if(parameter_list[0] != '#'){
00086    MessageBox ( GetFocus(),
00087                 (LPCTSTR) "External texture: Startup",
00088                 (LPCTSTR) "Parameter list missing",
00089                 MB_OK | MB_SYSTEMMODAL );
00090  }
00091  else {
00092   parameter_list++;
00093   sscanf(parameter_list,"%f %f %f %ld %ld",
00094          &x,&y,&z,&type,&bumpy);
00095  }
00096  return LoadAndCompileShader("stucco2");
00097 }
00098 
00099 #if 0
00100 long CALLBACK ExternalTextureMorph(
00101 #else
00102 long _ExternalTextureMorph(
00103 #endif
00104  char *parameter_list, double mr){
00105  double x_m,y_m,z_m;
00106  parameter_list++;
00107  sscanf(parameter_list,"%f %f %f",
00108         &x_m,&y_m,&z_m);
00109  x=x_m+(x-x_m)*mr;
00110  y=y_m+(y-y_m)*mr;
00111  z=z_m+(z-z_m)*mr;
00112  return 1;
00113 }
00114 
00115 #if 0
00116 long CALLBACK ExternalTextureProcedure(
00117 #else
00118 long _ExternalTextureProcedure(
00119 #endif
00120   long coord_type,  vector p, vector n,
00121   double alpha, double beta, double gamma,
00122   double bump,  double map_x,  double map_y,
00123   double *alpha_channel, unsigned char sc[3], double colour[3],
00124   double *reflectivity, double *transparency,
00125   X__SHADER *lpEVI
00126 ){
00127  // stucco ridges
00128  vector v,vt;
00129  double t=0.48,dt=0.05,s=0.5,turb=0.2;
00130  double xx,yy,zz,noise;
00131  double xr,xl,yr,yl,zr,zl;
00132  xx=10.0*alpha / x;
00133  yy=10.0*beta  / y;
00134  zz=10.0*gamma / z;
00135  if(bumpy){
00136    vNoise(v,xx*2.5,yy*2.5,zz*2.5);  // undulating  surface
00137    VECSUM(0.5*v,n,n)
00138    normalize(n);
00139  }
00140  vTurbulence(v, xx, yy, zz);
00141  xx += v[0] * turb;
00142  yy += v[1] * turb;
00143  zz += v[2] * turb;
00144  nNoise(xx, yy, zz,&noise);
00145  if(noise > t && noise < t+3*dt){
00146    nNoise(xx+0.01, yy, zz, &xr);
00147    nNoise(xx-0.01, yy, zz, &xl);
00148    nNoise(xx, yy+0.01,zz, &yr);
00149    nNoise(xx, yy-0.01,zz, &yl);
00150    nNoise(xx, yy,zz+0.01, &zr);
00151    nNoise(xx, yy,zz-0.01, &zl);
00152    v[0]= -(xr-xl); v[1]= -(yr-yl); v[2]= -(zr-zl);
00153    normalize(v);
00154    VECSCALE(v[0],tAxis_unorm,vt)
00155    VECSUM(v[1]*tAxis_vnorm,vt,vt)
00156    VECSUM(-v[2]*tAxis_n,vt,vt)
00157    normalize(vt);
00158    if(noise < t+dt){
00159      s=(noise-t)/dt;
00160      s=-s;
00161    }
00162    else if(noise < t+2*dt)s=0.0;
00163    else{
00164      s=1.0-(noise-(t+2*dt))/dt;
00165    }
00166    VECSUM(-s*vt,n,n)
00167    if(type == 1){
00168      vTurbulence(vt,xx*8,yy*8,zz*8);  // rough ridges
00169      VECSUM(0.8*vt,n,n)
00170    }
00171    normalize(n);
00172  }
00173  return 1;
00174 }
00175 
00176 void _ExternalTextureClose(X__SHADER *lpEVI){
00177  UnloadCompiledShader(tGLshaderID);
00178 }
00179 
00180 long _ExternalTextureProcedureGL(
00181   double bump_scale,
00182   unsigned char sc[3],
00183   unsigned char ac[3],
00184   X__SHADER *lpEVI
00185 ){
00186  SetUniformVector(tGLshaderID,"ScalingV",10.0/x,10.0/y,10.0/z);
00187  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00188  return 1;
00189 }
00191 
00192 void CentreDialogOnScreen(HWND hwnd){
00193  RECT rcDlg;
00194  long Xres,Yres;
00195  Yres=GetSystemMetrics(SM_CYSCREEN);
00196  Xres=GetSystemMetrics(SM_CXSCREEN);
00197  GetWindowRect(hwnd,&rcDlg);
00198  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00199  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00200  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00201  return;
00202 }
00203 
00204 static void SetColour(double *colour, HWND parent){
00205  CHOOSECOLOR cc;
00206  static COLORREF CustColours[16]={
00207    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00208    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00209    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00210    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00211  cc.lStructSize=sizeof(CHOOSECOLOR);
00212  cc.hwndOwner=parent;
00213  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00214  cc.lpCustColors=(LPDWORD)CustColours;
00215  cc.Flags= CC_RGBINIT;
00216  cc.lCustData=(DWORD)0;
00217  if(ChooseColor(&cc)){
00218    colour[0]=(double)GetRValue(cc.rgbResult);
00219    colour[1]=(double)GetGValue(cc.rgbResult);
00220    colour[2]=(double)GetBValue(cc.rgbResult);
00221  }
00222 }
00223 
00224 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00225 
00226 #if 0
00227 char * CALLBACK SetExternalParameters(
00228 #else
00229 char * _SetExternalParameters(
00230 #endif
00231   char *Op,                 /* string for the parameters                  */
00232   HWND hWnd,                /* parent window                              */
00233   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00234                              ){
00235  char szbuf[255],*Op1;
00236  if(Op != NULL){  /* parameters exist so read them off the list */
00237    Op1=Op;
00238    Op1++;
00239    sscanf(Op1,"%f %f %f %ld %ld",
00240          &x,&y,&z,&type,&bumpy);
00241  }
00242  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00243               (DLGPROC)DlgProc) == FALSE)return Op;
00244  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00245  sprintf(szbuf,"# %.3f %.3f %.3f %ld %ld",
00246          x,y,z,type,bumpy);
00247  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00248   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00249                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00250    return NULL;
00251  }
00252  strcpy(Op,szbuf);
00253  return Op;
00254 }
00255 
00256 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00257  char str[16];
00258  switch( msg ) {
00259    case WM_INITDIALOG:
00260      sprintf(str,"%.2f",x);
00261      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00262      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00263      sprintf(str,"%.2f",y);
00264      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00265      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00266      sprintf(str,"%.2f",z);
00267      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00268      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00269      if(type == 1)
00270        SendDlgItemMessage(hwnd,DLG_TYPE,BM_SETCHECK,TRUE,0);
00271      if(bumpy == 1)
00272        SendDlgItemMessage(hwnd,DLG_BUMPY,BM_SETCHECK,TRUE,0);
00273      CentreDialogOnScreen(hwnd);
00274      return TRUE;
00275    case WM_COMMAND:
00276      switch(LOWORD(wparam)){
00277         case IDCANCEL:
00278           EndDialog(hwnd,FALSE);
00279           return(TRUE);
00280         case IDOK:
00281           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00282              EndDialog(hwnd,FALSE);
00283           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00284           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00285              EndDialog(hwnd,FALSE);
00286           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00287           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00288              EndDialog(hwnd,FALSE);
00289           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00290           if(SendDlgItemMessage(hwnd,DLG_TYPE,BM_GETCHECK,0,0))type=1;
00291           else type =0;
00292           if(SendDlgItemMessage(hwnd,DLG_BUMPY,BM_GETCHECK,0,0))bumpy=1;
00293           else bumpy=0;
00294           EndDialog(hwnd,TRUE);
00295           return(TRUE);
00296         default:
00297           break;
00298       }
00299       break;
00300     default: break;
00301  }
00302  return FALSE;
00303 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3