carving.c

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