regbmps1.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 //    regsbmps1.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 "regbmps1.h"
00036 
00037 
00038 #if __WATCOMC__
00039 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00040 #else
00041 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00042 #endif
00043   HANDLE ghMod;
00044   switch (dwReason) {
00045     case DLL_PROCESS_ATTACH:
00046 #if __X__MIPS__
00047       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00048 #endif
00049       hDLLinstance = hDLL;  /* handle to DLL file */
00050       break;
00051     case DLL_PROCESS_DETACH:
00052 #if __X__MIPS__
00053       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00054 #endif
00055       break;
00056   }
00057   return (int)TRUE;
00058 }
00059 
00060 #if __SC__
00061 #pragma startaddress(DllMain)
00062 #endif
00063 
00064 static int    colour_flag=0;
00065 static int    type=0;
00066 static double size=0.5,depth=1.0;
00067 static double c1[3]={255.0,255.0,255.0};
00068 static double x=1.0,y=1.0,z=1.0;
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  %f %f  %f %f %f  %ld %ld",
00085          &x,&y,&z,&size,&depth,
00086          &c1[0],&c1[1],&c1[2],&type,&colour_flag);
00087  }
00088  return LoadAndCompileShader("regbmps1");
00089 }
00090 
00091 #if 0
00092 long CALLBACK ExternalTextureMorph(
00093 #else
00094 long _ExternalTextureMorph(
00095 #endif
00096  char *parameter_list, double mr){
00097  int i;
00098  double size_m,depth_m;
00099  double c1_m[3],c2_m[3],c3_m[3];
00100  double x_m,y_m,z_m;
00101  parameter_list++;
00102  sscanf(parameter_list,"%f %f %f  %f %f  %f %f %f ",
00103         &x_m,&y_m,&z_m,&size_m,&depth_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  size=size_m+(size-size_m)*mr;
00107  depth=depth_m+(depth-depth_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 
00120 double hermite(double t, double tmin, double tmax){ // not a real hermite
00121  t=(t-tmin)/(tmax-tmin);
00122  return (t - t*t);
00123 }
00124 
00125 double sigmoid(double t, double tmin, double tmax){
00126  t=3.1415926*(2.0*(t-tmin)/(tmax-tmin));
00127  return 0.5*(1.0-cos(t));
00128 }
00129 
00130 
00131 #if 0
00132 long CALLBACK ExternalTextureProcedure(
00133 #else
00134 long _ExternalTextureProcedure(
00135 #endif
00136   long coord_type,  vector p, vector n,
00137   double alpha, double beta, double gamma,
00138   double bump,  double map_x,  double map_y,
00139   double *alpha_channel, unsigned char sc[3], double colour[3],
00140   double *reflectivity, double *transparency,
00141   X__SHADER *lpEVI
00142 ){
00143  double xx,yy,zz;
00144  double a;
00145  vector u,v,pp;
00146  double rad;
00147  xx=alpha / x;
00148  yy=beta  / y;
00149  zz=gamma / z;
00150  rad=size*sqrt(tAxis_u[0]*tAxis_u[0]+
00151                tAxis_u[1]*tAxis_u[1]+
00152                tAxis_u[2]*tAxis_u[2]);
00153  VECCOPY(-tAxis_n,u)
00154  VECSCALE(rad,u,u)
00155  VECSUM(tAxis_o,0.5*tAxis_u,pp)
00156  VECSUM(pp,0.5*tAxis_v,pp)
00157  VECSUM(pp,u,pp)               // coord of centre of pattern
00158  xx=(xx-floor(xx));
00159  yy=(yy-floor(yy));
00160  zz=(zz-floor(zz));
00161  VECCOPY(tAxis_o,v)
00162  VECSUM(v,xx*tAxis_u,v)
00163  VECSUM(v,yy*tAxis_v,v)
00164  VECSUM(v,2.0*zz*u,v)         // coord of point on surface (relative to cell
00165  VECSUB(v,pp,u)               //(v-pp) vector from centre of pattern
00166  if((a=sqrt((u[0])*(u[0])+
00167             (u[1])*(u[1])+
00168             (u[2])*(u[2]))) < rad){
00169    double mu,moddn,rmu;
00170    vector c,dn;
00171    mu=DOT(u,n);
00172    c[0]=pp[0]+mu*n[0];
00173    c[1]=pp[1]+mu*n[1];
00174    c[2]=pp[2]+mu*n[2];
00175    VECSUB(v,c,dn)
00176    moddn=sqrt(dn[0]*dn[0]+dn[1]*dn[1]+dn[2]*dn[2]);
00177    rmu=sqrt(rad*rad-mu*mu);
00178    if(moddn < rmu){
00179      a=1.0-moddn/rmu;
00180      normalize(dn);
00181 //     VECSCALE(-1.0,dn,dn);  // dimples if < 0
00182      if(type == 0)a=sin(a*1.57079);      // rounded
00183      else{
00184        if(a < 0.5){    // studs mesas
00185          if(type == 2)a=0.5;
00186          else if(type == 1)a=1.0-sigmoid(a,0.0,0.5);
00187        }
00188        else a=1;
00189      }
00190      a=1.0-(1.0-a)*depth;
00191      VECSUM((1.0-a)*dn,(a)*n,n)
00192      normalize(n);
00193      if(colour_flag)VECCOPY(c1,colour)
00194    }
00195  }
00196  return 1;
00197 }
00198 
00199 void _ExternalTextureClose(X__SHADER *lpEVI){
00200  UnloadCompiledShader(tGLshaderID);
00201 }
00202 
00203 long _ExternalTextureProcedureGL(
00204   double bump_scale,
00205   unsigned char sc[3],
00206   unsigned char ac[3],
00207   X__SHADER *lpEVI
00208 ){
00209  // Set any shader specific uniform variables here.
00210  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00211  if(colour_flag)SetUniformInteger(tGLshaderID,"ColourType",1);
00212  else           SetUniformInteger(tGLshaderID,"ColourType",0);
00213  SetUniformInteger(tGLshaderID,"ShaderID",type);
00214  SetUniformVector(tGLshaderID,"ScalingV",1.0/x,1.0/z,1.0/y);
00215  SetUniformVariable(tGLshaderID,"BumpDepth",depth);
00216  SetUniformVariable(tGLshaderID,"BumpSize",size);
00217  // Callback to Render the Polygons - this allows for multiple passes - through same shaders
00218  // possibly with different parameters - including vertex offset - blending depth enabling 
00219  // to allow for multipass textures e.g. hair and fur (shell model) it's arguments must not be altered!!!
00220  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00221  return 1;
00222 }
00224 
00225 
00226 void CentreDialogOnScreen(HWND hwnd){
00227  RECT rcDlg;
00228  long Xres,Yres;
00229  Yres=GetSystemMetrics(SM_CYSCREEN);
00230  Xres=GetSystemMetrics(SM_CXSCREEN);
00231  GetWindowRect(hwnd,&rcDlg);
00232  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00233  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00234  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00235  return;
00236 }
00237 
00238 static void SetColour(double *colour, HWND parent){
00239  CHOOSECOLOR cc;
00240  static COLORREF CustColours[16]={
00241    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00242    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00243    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00244    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00245  cc.lStructSize=sizeof(CHOOSECOLOR);
00246  cc.hwndOwner=parent;
00247  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00248  cc.lpCustColors=(LPDWORD)CustColours;
00249  cc.Flags= CC_RGBINIT;
00250  cc.lCustData=(DWORD)0;
00251  if(ChooseColor(&cc)){
00252    colour[0]=(double)GetRValue(cc.rgbResult);
00253    colour[1]=(double)GetGValue(cc.rgbResult);
00254    colour[2]=(double)GetBValue(cc.rgbResult);
00255  }
00256 }
00257 
00258 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00259 
00260 #if 0
00261 char * CALLBACK SetExternalParameters(
00262 #else
00263 char * _SetExternalParameters(
00264 #endif
00265   char *Op,                 /* string for the parameters                  */
00266   HWND hWnd,                /* parent window                              */
00267   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00268                              ){
00269  char szbuf[255],*Op1;
00270  if(Op != NULL){  /* parameters exist so read them off the list */
00271    Op1=Op;
00272    Op1++;
00273    sscanf(Op1,"%f %f %f  %f %f  %f %f %f  %ld %ld",
00274          &x,&y,&z,&size,&depth,
00275          &c1[0],&c1[1],&c1[2],&type,&colour_flag);
00276  }
00277  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00278               (DLGPROC)DlgProc) == FALSE)return Op;
00279  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00280  sprintf(szbuf,"# %.2f %.2f %.2f  %.3f %.2f "
00281                  "%.2f %.2f %.2f %ld %ld",
00282          x,y,z,size,depth,
00283          c1[0],c1[1],c1[2],type,colour_flag);
00284  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00285   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00286                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00287    return NULL;
00288  }
00289  strcpy(Op,szbuf);
00290  return Op;
00291 }
00292 
00293 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00294  char str[16];
00295  switch( msg ) {
00296    case WM_INITDIALOG:
00297      sprintf(str,"%.2f",x);
00298      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00299      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00300      sprintf(str,"%.2f",y);
00301      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00302      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00303      sprintf(str,"%.2f",z);
00304      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00305      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00306      sprintf(str,"%.2f",size);
00307      SendDlgItemMessage(hwnd,DLG_SIZE,WM_SETTEXT,0,(LPARAM)str);
00308      SendDlgItemMessage(hwnd,DLG_SIZE,EM_LIMITTEXT,(WPARAM)12,0);
00309      sprintf(str,"%.2f",depth);
00310      SendDlgItemMessage(hwnd,DLG_DEPTH,WM_SETTEXT,0,(LPARAM)str);
00311      SendDlgItemMessage(hwnd,DLG_DEPTH,EM_LIMITTEXT,(WPARAM)12,0);
00312      if(colour_flag)
00313        SendDlgItemMessage(hwnd,DLG_COLOURFLAG,BM_SETCHECK,TRUE,0);
00314      if(type == 0)
00315        SendDlgItemMessage(hwnd,DLG_TYPE1,BM_SETCHECK,TRUE,0);
00316      else if(type == 1)
00317        SendDlgItemMessage(hwnd,DLG_TYPE2,BM_SETCHECK,TRUE,0);
00318      else if(type == 2)
00319        SendDlgItemMessage(hwnd,DLG_TYPE3,BM_SETCHECK,TRUE,0);
00320      CentreDialogOnScreen(hwnd);
00321      return TRUE;
00322    case WM_DRAWITEM:{
00323        LPDRAWITEMSTRUCT lpdis;
00324        HBRUSH   hbr,hbrold;
00325        BYTE r,g,b;
00326        lpdis=(LPDRAWITEMSTRUCT)lparam;
00327        if(lpdis->CtlID == DLG_COLOUR){
00328          if(lpdis->CtlID == DLG_COLOUR){
00329            r=(BYTE)c1[0]; g=(BYTE)c1[1]; b=(BYTE)c1[2];
00330          }
00331          if(lpdis->itemState & ODS_SELECTED)
00332             InvertRect(lpdis->hDC,&(lpdis->rcItem));
00333          else{
00334            hbr=CreateSolidBrush(RGB(r,g,b));
00335            hbrold=SelectObject(lpdis->hDC,hbr);
00336            Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
00337                          lpdis->rcItem.right,lpdis->rcItem.bottom);
00338            SelectObject(lpdis->hDC,hbrold);
00339            DeleteObject(hbr);
00340          }
00341        }
00342      }
00343      break;
00344    case WM_COMMAND:
00345      switch(LOWORD(wparam)){
00346         case DLG_COLOUR:
00347           SetColour(c1,hwnd);
00348           InvalidateRect(GetDlgItem(hwnd,DLG_COLOUR),NULL,FALSE);
00349           break;
00350         case IDCANCEL:
00351           EndDialog(hwnd,FALSE);
00352           return(TRUE);
00353         case IDOK:
00354           if(GetDlgItemText(hwnd,DLG_SIZE,str,10) == 0)
00355              EndDialog(hwnd,FALSE);
00356           if((size=atof(str)) == 0)EndDialog(hwnd,FALSE);
00357           if(GetDlgItemText(hwnd,DLG_DEPTH,str,10) == 0)
00358              EndDialog(hwnd,FALSE);
00359           if((depth=atof(str)) == 0)EndDialog(hwnd,FALSE);
00360           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00361              EndDialog(hwnd,FALSE);
00362           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00363           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00364              EndDialog(hwnd,FALSE);
00365           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00366           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00367              EndDialog(hwnd,FALSE);
00368           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00369           if(SendDlgItemMessage(hwnd,DLG_COLOURFLAG,BM_GETCHECK,0,0))
00370             colour_flag=1; else colour_flag=0;
00371           if(SendDlgItemMessage(hwnd,DLG_TYPE1,BM_GETCHECK,0,0))type=0;
00372           else if(SendDlgItemMessage(hwnd,DLG_TYPE2,BM_GETCHECK,0,0))type=1;
00373           else if(SendDlgItemMessage(hwnd,DLG_TYPE3,BM_GETCHECK,0,0))type=2;
00374           EndDialog(hwnd,TRUE);
00375           return(TRUE);
00376         default:
00377           break;
00378       }
00379       break;
00380     default: break;
00381  }
00382  return FALSE;
00383 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3