stucco.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 // stucco.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 "stucco.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 double pos=0.48,width=0.06,turb=0.2;
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  %f %f %f",
00094          &x,&y,&z,&pos,&width,&turb);
00095  }
00096  return LoadAndCompileShader("stucco");
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  vector v,vt;
00128  double xx,yy,zz,noise,s;
00129  double xr,xl,yr,yl,zr,zl;
00130  xx=10.0*alpha / x;
00131  yy=10.0*beta  / y;
00132  zz=10.0*gamma / z;
00133  vTurbulence(v, xx, yy, zz);
00134  xx += v[0] * turb;
00135  yy += v[1] * turb;
00136  zz += v[2] * turb;
00137  nNoise(xx, yy, zz, &noise);
00138  if(noise > pos && noise < pos+2*width){
00139    nNoise(xx+0.01, yy, zz, &xr);
00140    nNoise(xx-0.01, yy, zz, &xl);
00141    nNoise(xx, yy+0.01,zz, &yr);
00142    nNoise(xx, yy-0.01,zz, &yl);
00143    nNoise(xx, yy,zz+0.01, &zr);
00144    nNoise(xx, yy,zz-0.01, &zl);
00145    v[0]= -(xr-xl); v[1]= -(yr-yl); v[2]= -(zr-zl);
00146    normalize(v);
00147    VECSCALE(v[0],tAxis_unorm,vt)
00148    VECSUM(v[1]*tAxis_vnorm,vt,vt)
00149    VECSUM(-v[2]*tAxis_n,vt,vt)
00150    normalize(vt);
00151    if(noise < pos+width){
00152      s=(noise-pos)/width;
00153      s=s*s;
00154    }
00155    else{
00156      s=1.0-(noise-(pos+width))/width;
00157      s=s*s;
00158    }
00159    VECSUM(s*vt,n,n)
00160    normalize(n);
00161  }
00162 
00163  return 1;
00164 }
00165 
00166 
00167 void _ExternalTextureClose(X__SHADER *lpEVI){
00168  UnloadCompiledShader(tGLshaderID);
00169 }
00170 
00171 long _ExternalTextureProcedureGL(
00172   double bump_scale,
00173   unsigned char sc[3],
00174   unsigned char ac[3],
00175   X__SHADER *lpEVI
00176 ){
00177  // Set any shader specific uniform variables here.
00178  SetUniformVector(tGLshaderID,"ScalingV",10.0/x,10.0/y,10.0/z);
00179  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00180  return 1;
00181 }
00183 
00184 void CentreDialogOnScreen(HWND hwnd){
00185  RECT rcDlg;
00186  long Xres,Yres;
00187  Yres=GetSystemMetrics(SM_CYSCREEN);
00188  Xres=GetSystemMetrics(SM_CXSCREEN);
00189  GetWindowRect(hwnd,&rcDlg);
00190  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00191  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00192  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00193  return;
00194 }
00195 
00196 static void SetColour(double *colour, HWND parent){
00197  CHOOSECOLOR cc;
00198  static COLORREF CustColours[16]={
00199    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00200    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00201    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00202    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00203  cc.lStructSize=sizeof(CHOOSECOLOR);
00204  cc.hwndOwner=parent;
00205  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00206  cc.lpCustColors=(LPDWORD)CustColours;
00207  cc.Flags= CC_RGBINIT;
00208  cc.lCustData=(DWORD)0;
00209  if(ChooseColor(&cc)){
00210    colour[0]=(double)GetRValue(cc.rgbResult);
00211    colour[1]=(double)GetGValue(cc.rgbResult);
00212    colour[2]=(double)GetBValue(cc.rgbResult);
00213  }
00214 }
00215 
00216 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00217 
00218 #if 0
00219 char * CALLBACK SetExternalParameters(
00220 #else
00221 char * _SetExternalParameters(
00222 #endif
00223   char *Op,                 /* string for the parameters                  */
00224   HWND hWnd,                /* parent window                              */
00225   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00226                              ){
00227  char szbuf[255],*Op1;
00228  if(Op != NULL){  /* parameters exist so read them off the list */
00229    Op1=Op;
00230    Op1++;
00231    sscanf(Op1,"%f %f %f  %f %f %f",
00232          &x,&y,&z,&pos,&width,&turb);
00233  }
00234  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00235               (DLGPROC)DlgProc) == FALSE)return Op;
00236  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00237  sprintf(szbuf,"# %.3f %.3f %.3f %.3 %.3 %.3",
00238          x,y,z,pos,width,turb);
00239  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00240   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00241                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00242    return NULL;
00243  }
00244  strcpy(Op,szbuf);
00245  return Op;
00246 }
00247 
00248 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00249  char str[16];
00250  switch( msg ) {
00251    case WM_INITDIALOG:
00252      sprintf(str,"%.2f",x);
00253      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00254      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00255      sprintf(str,"%.2f",y);
00256      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00257      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00258      sprintf(str,"%.2f",z);
00259      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00260      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00261      CentreDialogOnScreen(hwnd);
00262      return TRUE;
00263    case WM_COMMAND:
00264      switch(LOWORD(wparam)){
00265         case IDCANCEL:
00266           EndDialog(hwnd,FALSE);
00267           return(TRUE);
00268         case IDOK:
00269           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00270              EndDialog(hwnd,FALSE);
00271           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00272           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00273              EndDialog(hwnd,FALSE);
00274           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00275           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00276              EndDialog(hwnd,FALSE);
00277           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00278           EndDialog(hwnd,TRUE);
00279           return(TRUE);
00280         default:
00281           break;
00282       }
00283       break;
00284     default: break;
00285  }
00286  return FALSE;
00287 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3