cracked.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 //  cracked.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 "cracked.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 double flat_edge=0.15;//0.085;
00064 static double bevel_edge=0.28;//0.25;//0.15;
00065 static double blend_edge=0.285;//0.15;
00066 static double c1[3]={255.0,255.0, 0.0};
00067 static double x=1.0,y=1.0,z=1.0;
00068 
00069 #if 0
00070 long CALLBACK ExternalTextureStartup(
00071 #else
00072 long _ExternalTextureStartup(
00073 #endif
00074   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00075  if(parameter_list[0] != '#'){
00076    MessageBox ( GetFocus(),
00077                 (LPCTSTR) "External texture: Startup",
00078                 (LPCTSTR) "Parameter list missing",
00079                 MB_OK | MB_SYSTEMMODAL );
00080  }
00081  else {
00082   parameter_list++;
00083   sscanf(parameter_list,"%f %f %f  %f %f %f  %f %f %f",
00084          &x,&y,&z,&flat_edge,&bevel_edge,&blend_edge,
00085          &c1[0],&c1[1],&c1[2]);
00086  }
00087  return LoadAndCompileShader("cracked");
00088 }
00089 
00090 #if 0
00091 long CALLBACK ExternalTextureMorph(
00092 #else
00093 long _ExternalTextureMorph(
00094 #endif
00095  char *parameter_list, double mr){
00096  int i;
00097  double flat_edge_m,bevel_edge_m,blend_edge_m;
00098  double c1_m[3],c2_m[3],c3_m[3];
00099  double x_m,y_m,z_m;
00100  parameter_list++;
00101  sscanf(parameter_list,"%f %f %f  %f %f %f  %f %f %f",
00102         &x_m,&y_m,&z_m,&flat_edge_m,&bevel_edge_m,&blend_edge_m,
00103         &c1_m[0],&c1_m[1],&c1_m[2]);
00104  for(i=0;i<3;i++)c1[i]=c1_m[i]+(c1[i]-c1_m[i])*mr;
00105  flat_edge=flat_edge_m+(flat_edge-flat_edge_m)*mr;
00106  bevel_edge=bevel_edge_m+(bevel_edge-bevel_edge_m)*mr;
00107  blend_edge=blend_edge_m+(blend_edge-blend_edge_m)*mr;
00108  x=x_m+(x-x_m)*mr;
00109  y=y_m+(y-y_m)*mr;
00110  z=z_m+(z-z_m)*mr;
00111  return 1;
00112 }
00113 
00114 #define VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00115 #define VECSUM(a,b,c)   { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00116 #define VECSUB(a,b,c)   { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00117 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00118 #define DOT(a,b)        ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00119 #define CROSS(v1,v2,r)  { \
00120                           r[0] = (v1[1]*v2[2]) - (v2[1]*v1[2]);  \
00121                           r[1] = (v1[2]*v2[0]) - (v1[0]*v2[2]);  \
00122                           r[2] = (v1[0]*v2[1]) - (v2[0]*v1[1]);  \
00123                         }
00124 #define IMOD(z,a) ((z) - ((z)/(a))*(a))
00125 #define PATTERN(x,y,z)   ( ((z)+16384L)*64536L +  \
00126                            ((y)+16384L)*32768L +  \
00127                            ((x)+16384L))
00128 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00129 #define FMOD(x,a) ((x) >= 0.0 ? fmod(x,a) :(a - fmod(-(x),a)))
00130 #define ROOT32 0.866025403
00131 #define ROOT3   1.73205080
00132 
00133 #include "randno.c"
00134 
00135 static long ip[27][3]={
00136    { 0, 0, 0},
00137    { 1, 0, 0},
00138    { 1, 1, 0},
00139    { 0, 1, 0},
00140    {-1, 1, 0},
00141    {-1, 0, 0},
00142    {-1,-1, 0},
00143    { 0,-1, 0},
00144    { 1,-1, 0},
00145    { 0, 0,-1},
00146    { 1, 0,-1},
00147    { 1, 1,-1},
00148    { 0, 1,-1},
00149    {-1, 1,-1},
00150    {-1, 0,-1},
00151    {-1,-1,-1},
00152    { 0,-1,-1},
00153    { 1,-1,-1},
00154    { 0, 0, 1},
00155    { 1, 0, 1},
00156    { 1, 1, 1},
00157    { 0, 1, 1},
00158    {-1, 1, 1},
00159    {-1, 0, 1},
00160    {-1,-1, 1},
00161    { 0,-1, 1},
00162    { 1,-1, 1}
00163  };
00164 
00165 static void mix(double *r, double *g, double *b, double mx,
00166                 double r1, double g1, double b1,
00167                 double r2, double g2, double b2){
00168  *r = mx*r1 + (1.0-mx)*r2;
00169  *g = mx*g1 + (1.0-mx)*g2;
00170  *b = mx*b1 + (1.0-mx)*b2;
00171 }
00172 
00173 #if 0
00174 long CALLBACK ExternalTextureProcedure(
00175 #else
00176 long _ExternalTextureProcedure(
00177 #endif
00178   long coord_type,  vector p, vector n,
00179   double alpha, double beta, double gamma,
00180   double bump,  double map_x,  double map_y,
00181   double *alpha_channel, unsigned char sc[3], double colour[3],
00182   double *reflectivity, double *transparency,
00183   X__SHADER *lpEVI
00184 ){
00185  double xx,yy,zz,r,g,b;
00186  double a;
00187  vector u,v,ppc;
00188  vector vclr[27];
00189  double dpoint[27];
00190  vector pp[27];
00191  double dx,dy,dz,cx,cy,cz,zb,ccc,dc,zbb,noiseH,len;
00192  long ix,iy,iz;
00193  BOOL ins;
00194  long j,id,pattern,patterni;
00195  static double radius,bradius=1.0;
00196  static double vc=0.2;   // random color
00197  xx = 4.0*alpha / x;
00198  yy = 4.0*beta  / y;
00199  zz = 4.0*gamma / z;
00200 
00201  id=-1;
00202  ix=FLOOR(xx);
00203  iy=FLOOR(yy);
00204  iz=FLOOR(zz);
00205  cx=xx-(double)ix;
00206  cy=yy-(double)iy;
00207  cz=zz-(double)iz;
00208  zb=-10.0;
00209  ins=TRUE;
00210 
00211 // len=sqrt(tAxis_u[0]*tAxis_u[0]+
00212 //          tAxis_u[1]*tAxis_u[1]+
00213 //          tAxis_u[2]*tAxis_u[2]);
00214  len=tAxis_u_length;
00215 
00216  VECCOPY(-tAxis_n,u)
00217  VECSCALE(len,u,u)
00218  VECCOPY(tAxis_o,v)
00219  VECSUM(v,cx*tAxis_u,v)
00220  VECSUM(v,cy*tAxis_v,v)
00221  VECSUM(v,cz*u,v)
00222  radius=bradius*len;
00223 
00224 // dx = *(tAxis_itr+2*4+3);  // [2][3]   example of addressing the tr
00225 // dx = *(tAxis_itr+2*4+0);  // [2][0]   matrices
00226 
00227  for(j=0;j<27;j++){
00228    pattern=PATTERN(ix+ip[j][0],iy+ip[j][1],iz+ip[j][2]);
00229    pattern=IMOD(pattern,1900);   // should be N*pattern for n > 1
00230    patterni=pattern;
00231 
00232    VECSUM((double)ip[j],rand_v[patterni],ppc)
00233 
00234    VECSUM(tAxis_o,ppc[0]*tAxis_u,pp[j])
00235    VECSUM(pp[j],ppc[1]*tAxis_v,pp[j])
00236    VECSUM(pp[j],ppc[2]*u,      pp[j])
00237 
00238    VECCOPY(rand_v[patterni],vclr[j])
00239    dpoint[j] = -1.0;
00240    dx=(v[0]-pp[j][0]);
00241    dy=(v[1]-pp[j][1]);
00242    dz=(v[2]-pp[j][2]);
00243    if(fabs(dx) > radius)continue;
00244    if(fabs(dy) > radius)continue;
00245    if(fabs(dz) > radius)continue;
00246    if((a=sqrt(dx*dx+dy*dy+dz*dz)) < radius){
00247      a /= len;
00248      dpoint[j]=a;
00249      dc=(bradius-a);
00250      if(dc > zb){
00251        id=j;
00252        zb=dc;
00253      }
00254    }
00255  }
00256  zbb= -10.0;
00257  for(j=0;j<27;j++){
00258    if(j == id)continue;
00259    if(dpoint[j] >= 0.0){
00260      dc=(bradius - dpoint[j]);
00261      if(dc > zbb){
00262        zbb=dc;
00263      }
00264    }
00265  }
00266  if((ccc=fabs(zbb - zb)) < flat_edge)ins=FALSE;
00267  if(id >= 0 && ins){
00268    if(0){
00269      r=vclr[id][0];
00270      g=vclr[id][1];
00271      b=vclr[id][2];
00272    }
00273    else{
00274 //     r=colour[0]/255.0;
00275 //     g=colour[1]/255.0;
00276 //     b=colour[2]/255.0;
00277      r=colour[0]/255.0 + vc*(vclr[id][0]-0.5);
00278      g=colour[1]/255.0 + vc*(vclr[id][1]-0.5);
00279      b=colour[2]/255.0 + vc*(vclr[id][2]-0.5);
00280      r=min(r,1.0); g=min(g,1.0); b=min(b,1.0);
00281      nNoise(xx*2.0,yy*2.0,zz*2.0,&noiseH);
00282      r -= (noiseH*0.13);
00283      g -= (noiseH*0.13);
00284      b -= (noiseH*0.13);
00285      r=max(r,0.0); g=max(g,0.0); b=max(b,0.0);
00286    }
00287    if(ccc < bevel_edge){
00288      vector ppc;
00289      VECCOPY(pp[id],ppc)
00290      VECSUB(v,ppc,u)
00291      if((a=sqrt((u[0])*(u[0])+
00292                 (u[1])*(u[1])+
00293                 (u[2])*(u[2]))) < radius){
00294        double mu,moddn,rmu;
00295        vector c,dn;
00296        mu=DOT(u,n);
00297        c[0]=ppc[0]+mu*n[0];
00298        c[1]=ppc[1]+mu*n[1];
00299        c[2]=ppc[2]+mu*n[2];
00300        VECSUB(v,c,dn)
00301        moddn=sqrt(dn[0]*dn[0]+dn[1]*dn[1]+dn[2]*dn[2]);
00302        rmu=sqrt(radius*radius-mu*mu);
00303        if(moddn < rmu){
00304          normalize(dn);
00305          dn[0]=0.25*(double)((int)(dn[0]*4.0)); //(4)
00306          dn[1]=0.25*(double)((int)(dn[1]*4.0));
00307          dn[2]=0.25*(double)((int)(dn[2]*4.0));
00308          normalize(dn);                        //(4)
00309          a=0.5;
00310          VECSUM((1.0-a)*dn,(a)*n,n)
00311          normalize(n);
00312        }
00313      }
00314    }
00315    if(ccc < blend_edge)mix(&r,&g,&b,(ccc-flat_edge)/(blend_edge-flat_edge),
00316          r,g,b,
00317          (double)c1[0]/255.0,
00318          (double)c1[1]/255.0,
00319          (double)c1[2]/255.0
00320          );
00321    *transparency=0.0;
00322  }
00323  else{
00324    r=c1[0]/255.0;
00325    g=c1[1]/255.0;
00326    b=c1[2]/255.0;
00327  }
00328  colour[0]=r*255;
00329  colour[1]=g*255;
00330  colour[2]=b*255;
00331 
00332  return 1;
00333 }
00334 
00335 void _ExternalTextureClose(X__SHADER *lpEVI){
00336  UnloadCompiledShader(tGLshaderID);
00337 }
00338 
00339 long _ExternalTextureProcedureGL(
00340   double bump_scale,
00341   unsigned char sc[3],
00342   unsigned char ac[3],
00343   X__SHADER *lpEVI
00344 ){
00345  // Set any shader specific uniform variables here.
00346  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00347  SetUniformVector(tGLshaderID,"ScalingV",1.0/x,1.0/z,1.0/y);
00348  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00349  return 1;
00350 }
00352 
00353 void CentreDialogOnScreen(HWND hwnd){
00354  RECT rcDlg;
00355  long Xres,Yres;
00356  Yres=GetSystemMetrics(SM_CYSCREEN);
00357  Xres=GetSystemMetrics(SM_CXSCREEN);
00358  GetWindowRect(hwnd,&rcDlg);
00359  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00360  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00361  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00362  return;
00363 }
00364 
00365 static void SetColour(double *colour, HWND parent){
00366  CHOOSECOLOR cc;
00367  static COLORREF CustColours[16]={
00368    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00369    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00370    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00371    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00372  cc.lStructSize=sizeof(CHOOSECOLOR);
00373  cc.hwndOwner=parent;
00374  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00375  cc.lpCustColors=(LPDWORD)CustColours;
00376  cc.Flags= CC_RGBINIT;
00377  cc.lCustData=(DWORD)0;
00378  if(ChooseColor(&cc)){
00379    colour[0]=(double)GetRValue(cc.rgbResult);
00380    colour[1]=(double)GetGValue(cc.rgbResult);
00381    colour[2]=(double)GetBValue(cc.rgbResult);
00382  }
00383 }
00384 
00385 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00386 
00387 #if 0
00388 char * CALLBACK SetExternalParameters(
00389 #else
00390 char * _SetExternalParameters(
00391 #endif
00392   char *Op,                 /* string for the parameters                  */
00393   HWND hWnd,                /* parent window                              */
00394   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00395                              ){
00396  char szbuf[255],*Op1;
00397  if(Op != NULL){  /* parameters exist so read them off the list */
00398    Op1=Op;
00399    Op1++;
00400    sscanf(Op1,"%f %f %f  %f %f %f  %f %f %f",
00401          &x,&y,&z,&flat_edge,&bevel_edge,&blend_edge,
00402          &c1[0],&c1[1],&c1[2]);
00403  }
00404  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00405               (DLGPROC)DlgProc) == FALSE)return Op;
00406  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00407  sprintf(szbuf,"# %.2f %.2f %.2f  %.3f %.3f %.3f "
00408                  "%.2f %.2f %.2f",
00409          x,y,z,flat_edge,bevel_edge,blend_edge,
00410          c1[0],c1[1],c1[2]);
00411  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00412   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00413                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00414    return NULL;
00415  }
00416  strcpy(Op,szbuf);
00417  return Op;
00418 }
00419 
00420 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00421  char str[16];
00422  switch( msg ) {
00423    case WM_INITDIALOG:
00424      sprintf(str,"%.2f",x);
00425      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00426      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00427      sprintf(str,"%.2f",y);
00428      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00429      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00430      sprintf(str,"%.2f",z);
00431      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00432      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00433      CentreDialogOnScreen(hwnd);
00434      return TRUE;
00435    case WM_DRAWITEM:{
00436        LPDRAWITEMSTRUCT lpdis;
00437        HBRUSH   hbr,hbrold;
00438        BYTE r,g,b;
00439        lpdis=(LPDRAWITEMSTRUCT)lparam;
00440        if(lpdis->CtlID == DLG_COLOUR){
00441          if(lpdis->CtlID == DLG_COLOUR){
00442            r=(BYTE)c1[0]; g=(BYTE)c1[1]; b=(BYTE)c1[2];
00443          }
00444          if(lpdis->itemState & ODS_SELECTED)
00445             InvertRect(lpdis->hDC,&(lpdis->rcItem));
00446          else{
00447            hbr=CreateSolidBrush(RGB(r,g,b));
00448            hbrold=SelectObject(lpdis->hDC,hbr);
00449            Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
00450                          lpdis->rcItem.right,lpdis->rcItem.bottom);
00451            SelectObject(lpdis->hDC,hbrold);
00452            DeleteObject(hbr);
00453          }
00454        }
00455      }
00456      break;
00457    case WM_COMMAND:
00458      switch(LOWORD(wparam)){
00459         case DLG_COLOUR:
00460           SetColour(c1,hwnd);
00461           InvalidateRect(GetDlgItem(hwnd,DLG_COLOUR),NULL,FALSE);
00462           break;
00463         case IDCANCEL:
00464           EndDialog(hwnd,FALSE);
00465           return(TRUE);
00466         case IDOK:
00467           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00468              EndDialog(hwnd,FALSE);
00469           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00470           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00471              EndDialog(hwnd,FALSE);
00472           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00473           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00474              EndDialog(hwnd,FALSE);
00475           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00476           EndDialog(hwnd,TRUE);
00477           return(TRUE);
00478         default:
00479           break;
00480       }
00481       break;
00482     default: break;
00483  }
00484  return FALSE;
00485 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3