veins.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 //  viens.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 "veins.h"
00036 
00037 #if __WATCOMC__
00038 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00039 #else
00040 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00041 #endif
00042   HANDLE ghMod;
00043   switch (dwReason) {
00044     case DLL_PROCESS_ATTACH:
00045 #if __X__MIPS__
00046       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00047 #endif
00048       hDLLinstance = hDLL;  /* handle to DLL file */
00049       break;
00050     case DLL_PROCESS_DETACH:
00051 #if __X__MIPS__
00052       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00053 #endif
00054       break;
00055   }
00056   return (int)TRUE;
00057 }
00058 
00059 #if __SC__
00060 #pragma startaddress(DllMain)
00061 #endif
00062 
00063 static int type=0;
00064 static double c1[3]={255.0,255.0, 0.0};
00065 static double x=1.0,y=1.0,z=1.0;
00066 
00067 #if 0
00068 long CALLBACK ExternalTextureStartup(
00069 #else
00070 long _ExternalTextureStartup(
00071 #endif
00072   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00073  if(parameter_list[0] != '#'){
00074    MessageBox ( GetFocus(),
00075                 (LPCTSTR) "External texture: Startup",
00076                 (LPCTSTR) "Parameter list missing",
00077                 MB_OK | MB_SYSTEMMODAL );
00078  }
00079  else {
00080   parameter_list++;
00081   sscanf(parameter_list,"%f %f %f %ld %f %f %f",
00082          &x,&y,&z,&type,
00083          &c1[0],&c1[1],&c1[2]);
00084  }
00085  return LoadAndCompileShader("veins");
00086 }
00087 
00088 #if 0
00089 long CALLBACK ExternalTextureMorph(
00090 #else
00091 long _ExternalTextureMorph(
00092 #endif
00093  char *parameter_list, double mr){
00094  int i,id;
00095  double c1_m[3],c2_m[3],c3_m[3];
00096  double x_m,y_m,z_m;
00097  parameter_list++;
00098  sscanf(parameter_list,"%f %f %f  %ld  %f %f %f",
00099         &x_m,&y_m,&z_m,&id,
00100         &c1_m[0],&c1_m[1],&c1_m[2]);
00101  for(i=0;i<3;i++)c1[i]=c1_m[i]+(c1[i]-c1_m[i])*mr;
00102  x=x_m+(x-x_m)*mr;
00103  y=y_m+(y-y_m)*mr;
00104  z=z_m+(z-z_m)*mr;
00105  return 1;
00106 }
00107 
00108 #define VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00109 #define VECSUM(a,b,c)   { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00110 #define VECSUB(a,b,c)   { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00111 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00112 #define DOT(a,b)        ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00113 #define CROSS(v1,v2,r)  { \
00114                           r[0] = (v1[1]*v2[2]) - (v2[1]*v1[2]);  \
00115                           r[1] = (v1[2]*v2[0]) - (v1[0]*v2[2]);  \
00116                           r[2] = (v1[0]*v2[1]) - (v2[0]*v1[1]);  \
00117                         }
00118 #define IMOD(z,a) ((z) - ((z)/(a))*(a))
00119 #define PATTERN(x,y,z)   ( ((z)+16384L)*64536L +  \
00120                            ((y)+16384L)*32768L +  \
00121                            ((x)+16384L))
00122 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00123 #define FMOD(x,a) ((x) >= 0.0 ? fmod(x,a) :(a - fmod(-(x),a)))
00124 #define ROOT32 0.866025403
00125 #define ROOT3   1.73205080
00126 
00127 
00128 #if 0
00129 long CALLBACK ExternalTextureProcedure(
00130 #else
00131 long _ExternalTextureProcedure(
00132 #endif
00133   long coord_type,  vector p, vector n,
00134   double alpha, double beta, double gamma,
00135   double bump,  double map_x,  double map_y,
00136   double *alpha_channel, unsigned char sc[3], double colour[3],
00137   double *reflectivity, double *transparency,
00138   X__SHADER *lpEVI
00139 ){
00140  static
00141  vector pp[35]={{0.25,0.0,      0.0},                     //0
00142                 {0.75,0.0,      0.0},
00143                 {0.0, ROOT32/2, 0.0},
00144                 {0.5, ROOT32/2, 0.0},
00145                 {1.0, ROOT32/2, 0.0},
00146                 {0.25,ROOT32,   0.0},
00147                 {0.75,ROOT32,   0.0},
00148 
00149                 {-0.25,0.0,     0.0},
00150                 { 1.25,0.0,     0.0},
00151                 {-0.25,ROOT32,  0.0},
00152                 { 1.25,ROOT32,  0.0},
00153                 {-0.25,0.0,    ROOT32},
00154                 { 1.25,0.0,    ROOT32},
00155                 {-0.25,ROOT32, ROOT32},
00156                 { 1.25,ROOT32, ROOT32},
00157                 {-0.25,1.0/2.0/ROOT3,ROOT32/2.0},
00158                 { 1.75,1.0/2.0/ROOT3,ROOT32/2.0},
00159                 {0.0,1.0/2.0/ROOT3+ROOT32,ROOT32/2.0},
00160                 {0.5,1.0/2.0/ROOT3+ROOT32,ROOT32/2.0},
00161                 {1.0,1.0/2.0/ROOT3+ROOT32,ROOT32/2.0},
00162 
00163                 {0.0, 1.0/2.0/ROOT3-ROOT32/2,ROOT32/2.0},  //7
00164                 {0.5, 1.0/2.0/ROOT3-ROOT32/2,ROOT32/2.0},
00165                 {1.0, 1.0/2.0/ROOT3-ROOT32/2,ROOT32/2.0},
00166                 {0.25,1.0/2.0/ROOT3,         ROOT32/2.0},
00167                 {0.75,1.0/2.0/ROOT3,         ROOT32/2.0},
00168                 {0.0, 1.0/2.0/ROOT3+ROOT32/2,ROOT32/2.0}, //12
00169                 {0.5, 1.0/2.0/ROOT3+ROOT32/2,ROOT32/2.0},
00170                 {1.0, 1.0/2.0/ROOT3+ROOT32/2,ROOT32/2.0},
00171                 {0.25,0.0,      ROOT32},
00172                 {0.75,0.0,      ROOT32},
00173                 {0.0, ROOT32/2, ROOT32},
00174                 {0.5, ROOT32/2, ROOT32},
00175                 {1.0, ROOT32/2, ROOT32},
00176                 {0.25,ROOT32,   ROOT32},
00177                 {0.75,ROOT32,   ROOT32}};                 //21
00178  vector u,v;
00179  static double cc=0.4;
00180  double xx,yy,zz,a;
00181  double dx,dy,dz,zb,ccc,dc,zbb;
00182  BOOL ins;
00183  int i,id;
00184  xx=alpha / x;
00185  yy=beta  / y;
00186  zz=gamma / z;
00187 
00188  vNoise(v,xx,yy,zz);
00189  xx += 0.7*v[0];
00190  yy += 0.7*v[1];
00191  zz += 0.7*v[2];
00192 
00193  yy /= ROOT32;
00194  xx=FMOD(xx,1.0);
00195  yy=FMOD(yy,ROOT32);
00196  zz /= ROOT32;
00197  zz=FMOD(zz,ROOT32);
00198  zb=-10.0;
00199  ins=TRUE;
00200  for(i=0;i<35;i++){
00201    dx=(xx-pp[i][0]);
00202    dy=(yy-pp[i][1]);
00203    dz=(zz-pp[i][2]);
00204    if((a=sqrt(dx*dx+dy*dy+dz*dz)) < cc){
00205      dc=(cc-a);
00206      if(dc > zb){
00207        id=i;
00208        zb=dc;
00209      }
00210    }
00211  }
00212  zbb= -10.0;
00213  for(i=0;i<35;i++){
00214    if(i == id)continue;
00215    dx=(xx-pp[i][0]);
00216    dy=(yy-pp[i][1]);
00217    dz=(zz-pp[i][2]);
00218    if((a=sqrt(dx*dx+dy*dy+dz*dz)) < cc){
00219      dc=(cc-a);
00220      if(dc > zbb){
00221        zbb=dc;
00222      }
00223    }
00224  }
00225  if((ccc=fabs(zbb - zb)) < 0.05)ins=FALSE;   // flat edge
00226  if(!ins){                     // if !ins then viened glass
00227    if(type == 0){
00228      v[0]=(xx-pp[id][0]);
00229      v[1]=(yy-pp[id][1]);
00230      v[2]=(zz-pp[id][2]);
00231      normalize(v);
00232      CROSS(v,n,u)
00233      CROSS(n,u,v)
00234      normalize(v);
00235      a=0.4;
00236      VECSCALE(a,v,v)
00237      VECSCALE((1.0-a),n,n)
00238      VECSUM(v,n,n)
00239      normalize(n);
00240    }
00241    colour[0]=c1[0];
00242    colour[1]=c1[1];
00243    colour[2]=c1[2];
00244    *transparency=0.0;
00245  }
00246  else *transparency=1.0;
00247  return 1;
00248 }
00249 
00250 void _ExternalTextureClose(X__SHADER *lpEVI){
00251  UnloadCompiledShader(tGLshaderID);
00252 }
00253 
00254 long _ExternalTextureProcedureGL(
00255   double bump_scale,
00256   unsigned char sc[3],
00257   unsigned char ac[3],
00258   X__SHADER *lpEVI
00259 ){
00260  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)c1[0]/255.0,(GLfloat)c1[1]/255.0,(GLfloat)c1[2]/255.0);
00261  SetUniformVector(tGLshaderID,"ScalingV",1.0/x,1.0/z,1.0/y);
00262  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00263  return 1;
00264 }
00266 
00267 void CentreDialogOnScreen(HWND hwnd){
00268  RECT rcDlg;
00269  long Xres,Yres;
00270  Yres=GetSystemMetrics(SM_CYSCREEN);
00271  Xres=GetSystemMetrics(SM_CXSCREEN);
00272  GetWindowRect(hwnd,&rcDlg);
00273  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00274  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00275  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00276  return;
00277 }
00278 
00279 static void SetColour(double *colour, HWND parent){
00280  CHOOSECOLOR cc;
00281  static COLORREF CustColours[16]={
00282    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00283    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00284    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00285    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00286  cc.lStructSize=sizeof(CHOOSECOLOR);
00287  cc.hwndOwner=parent;
00288  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00289  cc.lpCustColors=(LPDWORD)CustColours;
00290  cc.Flags= CC_RGBINIT;
00291  cc.lCustData=(DWORD)0;
00292  if(ChooseColor(&cc)){
00293    colour[0]=(double)GetRValue(cc.rgbResult);
00294    colour[1]=(double)GetGValue(cc.rgbResult);
00295    colour[2]=(double)GetBValue(cc.rgbResult);
00296  }
00297 }
00298 
00299 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00300 
00301 #if 0
00302 char * CALLBACK SetExternalParameters(
00303 #else
00304 char * _SetExternalParameters(
00305 #endif
00306   char *Op,                 /* string for the parameters                  */
00307   HWND hWnd,                /* parent window                              */
00308   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00309                              ){
00310  char szbuf[255],*Op1;
00311  if(Op != NULL){  /* parameters exist so read them off the list */
00312    Op1=Op;
00313    Op1++;
00314    sscanf(Op1,"%f %f %f  %ld  %f %f %f",
00315          &x,&y,&z,&type,
00316          &c1[0],&c1[1],&c1[2]);
00317  }
00318  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00319               (DLGPROC)DlgProc) == FALSE)return Op;
00320  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00321  sprintf(szbuf,"# %.2f %.2f %.2f  %ld "
00322                  "%.2f %.2f %.2f",
00323          x,y,z,type,
00324          c1[0],c1[1],c1[2]);
00325  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00326   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00327                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00328    return NULL;
00329  }
00330  strcpy(Op,szbuf);
00331  return Op;
00332 }
00333 
00334 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00335  char str[16];
00336  switch( msg ) {
00337    case WM_INITDIALOG:
00338      sprintf(str,"%.2f",x);
00339      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00340      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00341      sprintf(str,"%.2f",y);
00342      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00343      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00344      sprintf(str,"%.2f",z);
00345      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00346      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00347      if(type == 0)SendDlgItemMessage(hwnd,DLG_BUMP,BM_SETCHECK,TRUE,0);
00348      CentreDialogOnScreen(hwnd);
00349      return TRUE;
00350    case WM_DRAWITEM:{
00351        LPDRAWITEMSTRUCT lpdis;
00352        HBRUSH   hbr,hbrold;
00353        BYTE r,g,b;
00354        lpdis=(LPDRAWITEMSTRUCT)lparam;
00355        if(lpdis->CtlID == DLG_COLOUR){
00356          if(lpdis->CtlID == DLG_COLOUR){
00357            r=(BYTE)c1[0]; g=(BYTE)c1[1]; b=(BYTE)c1[2];
00358          }
00359          if(lpdis->itemState & ODS_SELECTED)
00360             InvertRect(lpdis->hDC,&(lpdis->rcItem));
00361          else{
00362            hbr=CreateSolidBrush(RGB(r,g,b));
00363            hbrold=SelectObject(lpdis->hDC,hbr);
00364            Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
00365                          lpdis->rcItem.right,lpdis->rcItem.bottom);
00366            SelectObject(lpdis->hDC,hbrold);
00367            DeleteObject(hbr);
00368          }
00369        }
00370      }
00371      break;
00372    case WM_COMMAND:
00373      switch(LOWORD(wparam)){
00374         case DLG_COLOUR:
00375           SetColour(c1,hwnd);
00376           InvalidateRect(GetDlgItem(hwnd,DLG_COLOUR),NULL,FALSE);
00377           break;
00378         case IDCANCEL:
00379           EndDialog(hwnd,FALSE);
00380           return(TRUE);
00381         case IDOK:
00382           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00383              EndDialog(hwnd,FALSE);
00384           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00385           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00386              EndDialog(hwnd,FALSE);
00387           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00388           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00389              EndDialog(hwnd,FALSE);
00390           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00391           if(SendDlgItemMessage(hwnd,DLG_BUMP,BM_GETCHECK,0,0))
00392             type=0; else type=1;
00393           EndDialog(hwnd,TRUE);
00394           return(TRUE);
00395         default:
00396           break;
00397       }
00398       break;
00399     default: break;
00400  }
00401  return FALSE;
00402 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3