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