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