v_ripple.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 /* V_RIPPLE.DLL     External texture for 3D Ripple texture */
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 #ifndef PI
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 "v_ripple.h"
00036 
00037 
00038 #if __WATCOMC__
00039 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00040 #else
00041 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00042 #endif
00043   HANDLE ghMod;
00044   switch (dwReason) {
00045     case DLL_PROCESS_ATTACH:
00046 #if __X__MIPS__
00047       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00048 #endif
00049       hDLLinstance = hDLL;  /* handle to DLL file */
00050       break;
00051     case DLL_PROCESS_DETACH:
00052 #if __X__MIPS__
00053       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00054 #endif
00055       break;
00056   }
00057   return (int)TRUE;
00058 }
00059 
00060 #if __SC__
00061 #pragma startaddress(DllMain)
00062 #endif
00063 
00064 #define VECSUB(a,b,c)   { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00065 #define VECSUM(a,b,c)   { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00066 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00067 #define DOT(a,b)        ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00068 
00069 static long   period=10;
00070 static double phase=0.0,freq=1.0,depth=1.5;
00071 static double x=1.0,y=1.0,z=1.0;
00072 
00073 #if 0
00074 long CALLBACK ExternalTextureStartup(
00075 #else
00076 long _ExternalTextureStartup(
00077 #endif
00078   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00079  if(parameter_list[0] != '#'){
00080    MessageBox ( GetFocus(),
00081                 (LPCTSTR) "External texture: Startup",
00082                 (LPCTSTR) "Parameter list missing",
00083                 MB_OK | MB_SYSTEMMODAL );
00084  }
00085  else {
00086   parameter_list++;
00087   sscanf(parameter_list,"%f %f  %f %f %f %ld",
00088          &freq,&depth,&x,&y,&z,&period);
00089   phase=freq*(double)(frame%period)/(double)period;
00090  }
00091  return LoadAndCompileShader("v_ripple");
00092 }
00093 
00094 #if 0
00095 long CALLBACK ExternalTextureMorph(
00096 #else
00097 long _ExternalTextureMorph(
00098 #endif
00099  char *parameter_list, double mr){
00100  int i;
00101  long mperiod;
00102  double mx,my,mz;
00103  double mfreq,mdepth;
00104  parameter_list++;
00105  sscanf(parameter_list,"%f %f  %f %f %f %ld",
00106          &mfreq,&mdepth,&mx,&my,&mz,&mperiod);
00107  freq=mfreq+(freq-mfreq)*mr;
00108  depth=mdepth+(depth-mdepth)*mr;
00109  x=mx+(x-mx)*mr;
00110  y=my+(y-my)*mr;
00111  z=mz+(z-mz)*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  int i;
00128  vector point;
00129  double length, scalar, index;
00130  alpha /= x;
00131  beta  /= y;
00132  gamma /= z;
00133  for(i=0;i<10;i++){
00134    point[0] = alpha;
00135    point[1] = beta;
00136    point[2] = gamma;
00137    VECSUB(point, Wave_Sources[i], point)
00138    length=DOT(point,point);
00139    if(length == 0.0)length = 1.0;
00140    length = sqrt(length);
00141    index = length*freq+phase;
00142    Cycloidal(index,&scalar);
00143    scalar = scalar*depth;
00144    VECSCALE(scalar/length/(double)10,point,point)
00145    VECSUM(n,point,n)
00146  }
00147  normalize(n);
00148  return 1;
00149 }
00150 
00151 void _ExternalTextureClose(X__SHADER *lpEVI){
00152  UnloadCompiledShader(tGLshaderID);
00153 }
00154 
00155 long _ExternalTextureProcedureGL(
00156   double bump_scale,
00157   unsigned char sc[3],
00158   unsigned char ac[3],
00159   X__SHADER *lpEVI
00160 ){
00161  // Set any shader specific uniform variables here.
00162  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00163  SetUniformVector(tGLshaderID,"ScalingV",1.0/x,1.0/z,1.0/y);
00164  SetUniformVariable(tGLshaderID,"Freq",freq);
00165  SetUniformVariable(tGLshaderID,"Phase",phase);
00166  SetUniformVariable(tGLshaderID,"Depth",depth);
00167  // Callback to Render the Polygons - this allows for multiple passes - through same shaders
00168  // possibly with different parameters - including vertex offset - blending depth enabling 
00169  // to allow for multipass textures e.g. hair and fur (shell model) it's arguments must not be altered!!!
00170  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00171 
00172  return 1;
00173 }
00175 
00176 void CentreDialogOnScreen(HWND hwnd){
00177  RECT rcDlg;
00178  long Xres,Yres;
00179  Yres=GetSystemMetrics(SM_CYSCREEN);
00180  Xres=GetSystemMetrics(SM_CXSCREEN);
00181  GetWindowRect(hwnd,&rcDlg);
00182  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00183  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00184  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00185  return;
00186 }
00187 
00188 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00189 
00190 #if 0
00191 char * CALLBACK SetExternalParameters(
00192 #else
00193 char * _SetExternalParameters(
00194 #endif
00195   char *Op,                 /* string for the parameters                  */
00196   HWND hWnd,                /* parent window                              */
00197   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00198                              ){
00199  char szbuf[255],*Op1;
00200  if(Op != NULL){  /* parameters exist so read them off the list */
00201    Op1=Op;
00202    Op1++;
00203    sscanf(Op1,"%f %f  %f %f %f %ld",
00204           &freq,&depth,&x,&y,&z,&period);
00205  }
00206  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG_RIPPLE),hWnd,
00207               (DLGPROC)DlgProc) == FALSE)return Op;
00208  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00209  sprintf(szbuf,"# %.2f %.2f  %.2f %.2f %.2f %ld",
00210           freq,depth,x,y,z,period);
00211  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00212   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00213                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00214    return NULL;
00215  }
00216  strcpy(Op,szbuf);
00217  return Op;
00218 }
00219 
00220 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00221  char str[16];
00222  switch( msg ) {
00223    case WM_INITDIALOG:
00224      SetDlgItemInt(hwnd,DLG_RIPPLE_PERIOD,period,FALSE);
00225      SendDlgItemMessage(hwnd,DLG_RIPPLE_PERIOD,EM_LIMITTEXT,(WPARAM)12,0);
00226      sprintf(str,"%.2f",freq);
00227      SendDlgItemMessage(hwnd,DLG_RIPPLE_FREQ,WM_SETTEXT,0,(LPARAM)str);
00228      SendDlgItemMessage(hwnd,DLG_RIPPLE_FREQ,EM_LIMITTEXT,(WPARAM)12,0);
00229      sprintf(str,"%.2f",depth);
00230      SendDlgItemMessage(hwnd,DLG_RIPPLE_DEPTH,WM_SETTEXT,0,(LPARAM)str);
00231      SendDlgItemMessage(hwnd,DLG_RIPPLE_DEPTH,EM_LIMITTEXT,(WPARAM)12,0);
00232      sprintf(str,"%.2f",x);
00233      SendDlgItemMessage(hwnd,DLG_RIPPLE_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00234      SendDlgItemMessage(hwnd,DLG_RIPPLE_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00235      sprintf(str,"%.2f",y);
00236      SendDlgItemMessage(hwnd,DLG_RIPPLE_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00237      SendDlgItemMessage(hwnd,DLG_RIPPLE_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00238      sprintf(str,"%.2f",z);
00239      SendDlgItemMessage(hwnd,DLG_RIPPLE_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00240      SendDlgItemMessage(hwnd,DLG_RIPPLE_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00241      CentreDialogOnScreen(hwnd);
00242      return TRUE;
00243    case WM_COMMAND:
00244      switch(LOWORD(wparam)){
00245         case IDCANCEL:
00246         case DLG_RIPPLE_CANCEL:
00247           EndDialog(hwnd,FALSE);
00248           return(TRUE);
00249         case IDOK:
00250         case DLG_RIPPLE_OK:
00251           if(GetDlgItemText(hwnd,DLG_RIPPLE_PERIOD,str,10) == 0)
00252              EndDialog(hwnd,FALSE);
00253           if((period=atoi(str)) == 0)EndDialog(hwnd,FALSE);
00254           if(GetDlgItemText(hwnd,DLG_RIPPLE_FREQ,str,10) == 0)
00255              EndDialog(hwnd,FALSE);
00256           if((freq=atof(str)) == 0)EndDialog(hwnd,FALSE);
00257           if(GetDlgItemText(hwnd,DLG_RIPPLE_DEPTH,str,10) == 0)
00258              EndDialog(hwnd,FALSE);
00259           if((depth=atof(str)) == 0)EndDialog(hwnd,FALSE);
00260           if(GetDlgItemText(hwnd,DLG_RIPPLE_XSCALE,str,10) == 0)
00261              EndDialog(hwnd,FALSE);
00262           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00263           if(GetDlgItemText(hwnd,DLG_RIPPLE_YSCALE,str,10) == 0)
00264              EndDialog(hwnd,FALSE);
00265           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00266           if(GetDlgItemText(hwnd,DLG_RIPPLE_ZSCALE,str,10) == 0)
00267              EndDialog(hwnd,FALSE);
00268           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00269           EndDialog(hwnd,TRUE);
00270           return(TRUE);
00271         default:
00272           break;
00273       }
00274       break;
00275     default: break;
00276  }
00277  return FALSE;
00278 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3