v_dimple.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_DIMPLE.DLL     External texture for 3D Dimples */
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_dimple.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 VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[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 
00068 
00069 static double radius=0.3,depth=1.5;
00070 static double x=1.0,y=1.0,z=1.0;
00071 
00072 #if 0
00073 long CALLBACK ExternalTextureStartup(
00074 #else
00075 long _ExternalTextureStartup(
00076 #endif
00077   long frame,long nframes,char *parameter_list,X__SHADER *lpEVI){
00078  if(parameter_list[0] != '#'){
00079    MessageBox ( GetFocus(),
00080                 (LPCTSTR) "External texture: Startup",
00081                 (LPCTSTR) "Parameter list missing",
00082                 MB_OK | MB_SYSTEMMODAL );
00083  }
00084  else {
00085   parameter_list++;
00086   sscanf(parameter_list,"%f %f  %f %f %f",
00087          &radius,&depth,&x,&y,&z);
00088  }
00089  return LoadAndCompileShader("v_dimple");
00090 }
00091 
00092 #if 0
00093 long CALLBACK ExternalTextureMorph(
00094 #else
00095 long _ExternalTextureMorph(
00096 #endif
00097  char *parameter_list, double mr){
00098  double mx,my,mz;
00099  double mradius,mdepth,mc[3];
00100  parameter_list++;
00101  sscanf(parameter_list,"%f %f  %f %f %f",
00102          &mradius,&mdepth,&mx,&my,&mz);
00103  radius=mradius+(radius-mradius)*mr;
00104  depth=mdepth+(depth-mdepth)*mr;
00105  x=mx+(x-mx)*mr;
00106  y=my+(y-my)*mr;
00107  z=mz+(z-mz)*mr;
00108  return 1;
00109 }
00110 
00111 #if 0
00112 long CALLBACK ExternalTextureProcedure(
00113 #else
00114 long _ExternalTextureProcedure(
00115 #endif
00116   long coord_type,  vector p, vector n,
00117   double alpha, double beta, double gamma,
00118   double bump,  double map_x,  double map_y,
00119   double *alpha_channel, unsigned char sc[3], double colour[3],
00120   double *reflectivity, double *transparency,
00121   X__SHADER *lpEVI
00122 ){
00123  vector result;
00124  alpha /= x;
00125  beta  /= y;
00126  gamma /= z;
00127  alpha=(alpha-floor(alpha))-0.5;
00128  beta=(beta-floor(beta))-0.5;
00129  gamma=(gamma-floor(gamma))-0.5;
00130  if(beta*beta+alpha*alpha+gamma*gamma < radius){
00131    VECCOPY((double)sc,colour)
00132    vNoise(result,alpha,beta,gamma);
00133    VECSCALE(depth,result,result)
00134    VECSUM(n,result,n)
00135    normalize(n);
00136  }
00137  return 1;
00138 }
00139 
00140 void _ExternalTextureClose(X__SHADER *lpEVI){
00141  UnloadCompiledShader(tGLshaderID);
00142 }
00143 
00144 long _ExternalTextureProcedureGL(
00145   double bump_scale,
00146   unsigned char sc[3],
00147   unsigned char ac[3],
00148   X__SHADER *lpEVI
00149 ){
00150  // Set any shader specific uniform variables here.
00151  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00152  SetUniformVector(tGLshaderID,"ScalingV",1.0/x,1.0/z,1.0/y);
00153  SetUniformVariable(tGLshaderID,"Radius",radius);
00154  SetUniformVariable(tGLshaderID,"Depth",depth);
00155  // Callback to Render the Polygons - this allows for multiple passes - through same shaders
00156  // possibly with different parameters - including vertex offset - blending depth enabling 
00157  // to allow for multipass textures e.g. hair and fur (shell model) it's arguments must not be altered!!!
00158  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00159  return 1;
00160 }
00162 
00163 void CentreDialogOnScreen(HWND hwnd){
00164  RECT rcDlg;
00165  long Xres,Yres;
00166  Yres=GetSystemMetrics(SM_CYSCREEN);
00167  Xres=GetSystemMetrics(SM_CXSCREEN);
00168  GetWindowRect(hwnd,&rcDlg);
00169  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00170  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00171  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00172  return;
00173 }
00174 
00175 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00176 
00177 #if 0
00178 char * CALLBACK SetExternalParameters(
00179 #else
00180 char * _SetExternalParameters(
00181 #endif
00182   char *Op,                 /* string for the parameters                  */
00183   HWND hWnd,                /* parent window                              */
00184   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00185                              ){
00186  char szbuf[255],*Op1;
00187  if(Op != NULL){  /* parameters exist so read them off the list */
00188    Op1=Op;
00189    Op1++;
00190    sscanf(Op1,"%f %f  %f %f %f",
00191           &radius,&depth,&x,&y,&z);
00192  }
00193  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG_DIMPLE),hWnd,
00194               (DLGPROC)DlgProc) == FALSE)return Op;
00195  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00196  sprintf(szbuf,"# %.2f %.2f  %.2f %.2f %.2f",
00197           radius,depth,x,y,z);
00198  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00199   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00200                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00201    return NULL;
00202  }
00203  strcpy(Op,szbuf);
00204  return Op;
00205 }
00206 
00207 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00208  char str[16];
00209  switch( msg ) {
00210    case WM_INITDIALOG:
00211      sprintf(str,"%.2f",radius);
00212      SendDlgItemMessage(hwnd,DLG_DIMPLE_T1,WM_SETTEXT,0,(LPARAM)str);
00213      SendDlgItemMessage(hwnd,DLG_DIMPLE_T1,EM_LIMITTEXT,(WPARAM)12,0);
00214      sprintf(str,"%.2f",depth);
00215      SendDlgItemMessage(hwnd,DLG_DIMPLE_T2,WM_SETTEXT,0,(LPARAM)str);
00216      SendDlgItemMessage(hwnd,DLG_DIMPLE_T2,EM_LIMITTEXT,(WPARAM)12,0);
00217      sprintf(str,"%.2f",x);
00218      SendDlgItemMessage(hwnd,DLG_DIMPLE_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00219      SendDlgItemMessage(hwnd,DLG_DIMPLE_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00220      sprintf(str,"%.2f",y);
00221      SendDlgItemMessage(hwnd,DLG_DIMPLE_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00222      SendDlgItemMessage(hwnd,DLG_DIMPLE_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00223      sprintf(str,"%.2f",z);
00224      SendDlgItemMessage(hwnd,DLG_DIMPLE_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00225      SendDlgItemMessage(hwnd,DLG_DIMPLE_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00226      CentreDialogOnScreen(hwnd);
00227      return TRUE;
00228    case WM_COMMAND:
00229      switch(LOWORD(wparam)){
00230         case IDCANCEL:
00231         case DLG_DIMPLE_CANCEL:
00232           EndDialog(hwnd,FALSE);
00233           return(TRUE);
00234         case IDOK:
00235         case DLG_DIMPLE_OK:
00236           if(GetDlgItemText(hwnd,DLG_DIMPLE_T1,str,10) == 0)
00237              EndDialog(hwnd,FALSE);
00238           if((radius=atof(str)) == 0)EndDialog(hwnd,FALSE);
00239           if(GetDlgItemText(hwnd,DLG_DIMPLE_T2,str,10) == 0)
00240              EndDialog(hwnd,FALSE);
00241           if((depth=atof(str)) == 0)EndDialog(hwnd,FALSE);
00242           if(GetDlgItemText(hwnd,DLG_DIMPLE_XSCALE,str,10) == 0)
00243              EndDialog(hwnd,FALSE);
00244           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00245           if(GetDlgItemText(hwnd,DLG_DIMPLE_YSCALE,str,10) == 0)
00246              EndDialog(hwnd,FALSE);
00247           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00248           if(GetDlgItemText(hwnd,DLG_DIMPLE_ZSCALE,str,10) == 0)
00249              EndDialog(hwnd,FALSE);
00250           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00251           EndDialog(hwnd,TRUE);
00252           return(TRUE);
00253         default:
00254           break;
00255       }
00256       break;
00257     default: break;
00258  }
00259  return FALSE;
00260 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3