crspots.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 // crspots.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 "crspots.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 #define CROSS(v1,v2,r)  { \
00043                           r[0] = (v1[1]*v2[2]) - (v2[1]*v1[2]);  \
00044                           r[1] = (v1[2]*v2[0]) - (v1[0]*v2[2]);  \
00045                           r[2] = (v1[0]*v2[1]) - (v2[0]*v1[1]);  \
00046                         }
00047 
00048 #define IMOD(z,a) ((z) - ((z)/(a))*(a))
00049 
00050 #define PATTERN(x,y,z)   ( ((z)+16384L)*64536L +  \
00051                            ((y)+16384L)*32768L +  \
00052                            ((x)+16384L))
00053 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00054 #define FMOD(x,a) ((x) >= 0.0 ? fmod(x,a) :(a - fmod(-(x),a)))
00055 #define ROOT32 0.866025403
00056 #define ROOT3   1.73205080
00057 
00058 static void mix(double *r, double *g, double *b, double mx,
00059                 double r1, double g1, double b1,
00060                 double r2, double g2, double b2){
00061  *r = mx*r1 + (1.0-mx)*r2;
00062  *g = mx*g1 + (1.0-mx)*g2;
00063  *b = mx*b1 + (1.0-mx)*b2;
00064 }
00065 
00066 static long ip[27][3]={
00067    { 0, 0, 0},
00068    { 1, 0, 0},
00069    { 1, 1, 0},
00070    { 0, 1, 0},
00071    {-1, 1, 0},
00072    {-1, 0, 0},
00073    {-1,-1, 0},
00074    { 0,-1, 0},
00075    { 1,-1, 0},
00076    { 0, 0,-1},
00077    { 1, 0,-1},
00078    { 1, 1,-1},
00079    { 0, 1,-1},
00080    {-1, 1,-1},
00081    {-1, 0,-1},
00082    {-1,-1,-1},
00083    { 0,-1,-1},
00084    { 1,-1,-1},
00085    { 0, 0, 1},
00086    { 1, 0, 1},
00087    { 1, 1, 1},
00088    { 0, 1, 1},
00089    {-1, 1, 1},
00090    {-1, 0, 1},
00091    {-1,-1, 1},
00092    { 0,-1, 1},
00093    { 1,-1, 1}
00094  };
00095 
00096 #include "randno.c"
00097 
00098 #if __WATCOMC__
00099 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00100 #else
00101 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00102 #endif
00103   HANDLE ghMod;
00104   switch (dwReason) {
00105     case DLL_PROCESS_ATTACH:
00106 #if __X__MIPS__
00107       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00108 #endif
00109       hDLLinstance = hDLL;  /* handle to DLL file */
00110       break;
00111     case DLL_PROCESS_DETACH:
00112 #if __X__MIPS__
00113       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00114 #endif
00115       break;
00116   }
00117   return (int)TRUE;
00118 }
00119 
00120 static double x=1.0,y=1.0,z=1.0,bradius=0.5,lradius=0.005,rradius=0.75;
00121 static int    rtype=0;
00122 
00123 #if 0
00124 long CALLBACK ExternalTextureStartup(
00125 #else
00126 long _ExternalTextureStartup(
00127 #endif
00128   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00129  if(parameter_list[0] != '#'){
00130    MessageBox ( GetFocus(),
00131                 (LPCTSTR) "External texture: Startup",
00132                 (LPCTSTR) "Parameter list missing",
00133                 MB_OK | MB_SYSTEMMODAL );
00134  }
00135  else {
00136   parameter_list++;
00137   sscanf(parameter_list,"%f %f %f  %ld  %f %f %f",
00138          &x,&y,&z,&rtype,&bradius,&lradius,&rradius);
00139  }
00140  return LoadAndCompileShader("crspots");
00141 }
00142 
00143 #if 0
00144 long CALLBACK ExternalTextureMorph(
00145 #else
00146 long _ExternalTextureMorph(
00147 #endif
00148  char *parameter_list, double mr){
00149  double x_m,y_m,z_m;
00150  double bradius_m,lradius_m,rradius_m;
00151  parameter_list++;
00152  sscanf(parameter_list,"%f %f %f  %ld  %f %f %f",
00153         &x_m,&y_m,&z_m,&rtype,&bradius_m,&lradius_m,&rradius_m);
00154  x=x_m+(x-x_m)*mr;
00155  y=y_m+(y-y_m)*mr;
00156  z=z_m+(z-z_m)*mr;
00157  bradius=bradius_m+(bradius-bradius_m)*mr;
00158  lradius=lradius_m+(lradius-lradius_m)*mr;
00159  rradius=rradius_m+(rradius-rradius_m)*mr;
00160  return 1;
00161 }
00162 
00163 
00164 #if 0
00165 long CALLBACK ExternalTextureProcedure(
00166 #else
00167 long _ExternalTextureProcedure(
00168 #endif
00169   long coord_type,  vector p, vector n,
00170   double alpha, double beta, double gamma,
00171   double bump,  double map_x,  double map_y,
00172   double *alpha_channel, unsigned char sc[3], double colour[3],
00173   double *reflectivity, double *transparency,
00174   X__SHADER *lpEVI
00175 ){
00176  double xx,yy,zz,noiseH,r,g,b;
00177  vector  vclr[27];   // N=1
00178  double  dpoint[27]; //
00179  double dradius[27];
00180  vector u,v,pp[10];
00181  double a,dx,dy,dz,cx,cy,cz,zb,ccc,dc,zbb,radius;
00182  long ix,iy,iz;
00183  BOOL ins;
00184  long i,j,jid,id,pattern,patterni;
00185  long N=1;
00186 
00187  xx=alpha / x;
00188  yy=beta  / y;
00189  zz=gamma / z;
00190  xx *= 4.0;
00191  yy *= 4.0;
00192  zz *= 4.0;
00193  id=0;
00194  ix=FLOOR(xx);
00195  iy=FLOOR(yy);
00196  iz=FLOOR(zz);
00197  cx=xx-(double)ix;
00198  cy=yy-(double)iy;
00199  cz=zz-(double)iz;
00200  zb=-10.0;
00201  ins=TRUE;
00202  id = -1;
00203  for(j=0;j<27;j++){
00204 // for(j=0;j<4;j++){
00205  pattern=PATTERN(ix+ip[j][0],iy+ip[j][1],iz+ip[j][2]);
00206  pattern=IMOD(N*pattern,1900);
00207  for(i=0;i<N;i++){
00208    jid=j*N+i;
00209    patterni=pattern+i;
00210    VECSUM((double)ip[j],rand_v[patterni],pp[i])
00211 //VECCOPY((double)ip[j],pp[i])
00212    VECCOPY(rand_v[patterni],vclr[jid])
00213    radius=bradius+lradius*(rand_v[patterni][1]-0.5);
00214 //if(j == 0)radius=0.5;
00215 //else radius=0.4;
00216    dradius[jid]=radius;
00217    dpoint[jid] = -1.0;
00218    dx=(cx-pp[i][0]);
00219    dy=(cy-pp[i][1]);
00220    dz=(cz-pp[i][2]);
00221    if(fabs(dx) > radius)continue;
00222    if(fabs(dy) > radius)continue;
00223    if(fabs(dz) > radius)continue;
00224    if((a=sqrt(dx*dx+dy*dy+dz*dz)) < radius){
00225      dpoint[jid]=a;
00226      dc=(radius-a);
00227      if(dc > zb){
00228        id=jid;
00229        zb=dc;
00230      }
00231    }
00232  }
00233  }
00234  zbb= -10.0;
00235  for(i=0;i<N*27;i++){
00236    if(i == id)continue;
00237    if(dpoint[i] >= 0.0){
00238      dc=(dradius[i]-dpoint[i]);
00239      if(dc > zbb){
00240        zbb=dc;
00241      }
00242    }
00243  }
00244  if((ccc=fabs(zbb - zb)) < 0.0)ins=FALSE;   // flat edge
00245  if(id >= 0 && ins
00246    && (rtype == 0 || ( zb < dradius[id]-rradius*dradius[id] ) ) // rings
00247    ){
00248    r=vclr[id][0];
00249    g=vclr[id][1];
00250    b=vclr[id][2];
00251 //if(id == 0){r=g=0.0; b=1.0;}
00252    *transparency=0.0;
00253    if(zb < 0.1){
00254      mix(&r,&g,&b,
00255          zb/(0.1),
00256          r,g,b,
00257          (double)colour[0]/255.0,
00258          (double)colour[1]/255.0,
00259          (double)colour[2]/255.0
00260          );
00261    }
00262  }
00263  else{
00264    r=colour[0]/255.0;
00265    g=colour[1]/255.0;
00266    b=colour[2]/255.0;
00267  }
00268  colour[0]  = r*255.0;
00269  colour[1]  = g*255.0;
00270  colour[2]  = b*255.0;
00271  return 1;
00272 }
00273 
00274 void _ExternalTextureClose(X__SHADER *lpEVI){
00275  UnloadCompiledShader(tGLshaderID);
00276 }
00277 
00278 long _ExternalTextureProcedureGL(
00279   double bump_scale,
00280   unsigned char sc[3],
00281   unsigned char ac[3],
00282   X__SHADER *lpEVI
00283 ){
00284  // Set any shader specific uniform variables here.
00285  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00286 // SetUniformInteger(tGLshaderID,"ShaderID",1);
00287  SetUniformVector(tGLshaderID,"ScalingV",4.0/x,4.0/z,4.0/y);
00288 // SetUniformInteger(tGLshaderID,"Type",type);
00289 // SetUniformVariable(tGLshaderID,"BumpSlope",slope/0.4);
00290 // SetUniformVariable(tGLshaderID,"RidgeThickness",thickness);
00291 // SetUniformVariable(tGLshaderID,"EdgeThickness",inner);
00292 // SetUniformVariable(tGLshaderID,"BumpSize",size);
00293  // Callback to Render the Polygons - this allows for multiple passes - through same shaders
00294  // possibly with different parameters - including vertex offset - blending depth enabling 
00295  // to allow for multipass textures e.g. hair and fur (shell model) it's arguments must not be altered!!!
00296  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00297  return 1;
00298 }
00300 
00301 
00302 void CentreDialogOnScreen(HWND hwnd){
00303  RECT rcDlg;
00304  long Xres,Yres;
00305  Yres=GetSystemMetrics(SM_CYSCREEN);
00306  Xres=GetSystemMetrics(SM_CXSCREEN);
00307  GetWindowRect(hwnd,&rcDlg);
00308  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00309  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00310  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00311  return;
00312 }
00313 
00314 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00315 
00316 #if 0
00317 char * CALLBACK SetExternalParameters(
00318 #else
00319 char * _SetExternalParameters(
00320 #endif
00321   char *Op,                 /* string for the parameters                  */
00322   HWND hWnd,                /* parent window                              */
00323   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00324                              ){
00325  char szbuf[255],*Op1;
00326  if(Op != NULL){  /* parameters exist so read them off the list */
00327    Op1=Op;
00328    Op1++;
00329    sscanf(Op1,"%f %f %f  %ld  %f %f %f",
00330           &x,&y,&z,&rtype,&bradius,&lradius,&rradius);
00331  }
00332  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00333               (DLGPROC)DlgProc) == FALSE)return Op;
00334  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00335  sprintf(szbuf,"# %.2f %.2f %.2f  %ld  %.4f %.4f %.4f",
00336          x,y,z,rtype,bradius,lradius,rradius);
00337  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00338   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00339                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00340    return NULL;
00341  }
00342  strcpy(Op,szbuf);
00343  return Op;
00344 }
00345 
00346 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00347  char str[16];
00348  double scale=120.0*3.0;
00349  switch( msg ) {
00350    case WM_INITDIALOG:
00351      sprintf(str,"%.2f",x);
00352      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00353      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00354      sprintf(str,"%.2f",y);
00355      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00356      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00357      sprintf(str,"%.2f",z);
00358      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00359      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00360      sprintf(str,"%.3f",bradius);
00361      SendDlgItemMessage(hwnd,DLG_BRADIUS,WM_SETTEXT,0,(LPARAM)str);
00362      sprintf(str,"%.3f",lradius);
00363      SendDlgItemMessage(hwnd,DLG_LRADIUS,WM_SETTEXT,0,(LPARAM)str);
00364      sprintf(str,"%.3f",rradius);
00365      SendDlgItemMessage(hwnd,DLG_RRADIUS,WM_SETTEXT,0,(LPARAM)str);
00366      if(rtype == 0)
00367         SendDlgItemMessage(hwnd,DLG_SPOTS,BM_SETCHECK,TRUE,0);
00368      if(rtype == 1)
00369         SendDlgItemMessage(hwnd,DLG_RINGS,BM_SETCHECK,TRUE,0);
00370      CentreDialogOnScreen(hwnd);
00371      return TRUE;
00372    case WM_COMMAND:
00373      switch(LOWORD(wparam)){
00374         case IDCANCEL:
00375           EndDialog(hwnd,FALSE);
00376           return(TRUE);
00377         case IDOK:
00378           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00379              EndDialog(hwnd,FALSE);
00380           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00381           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00382              EndDialog(hwnd,FALSE);
00383           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00384           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00385              EndDialog(hwnd,FALSE);
00386           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00387           if(GetDlgItemText(hwnd,DLG_BRADIUS,str,10) == 0)
00388              EndDialog(hwnd,FALSE);
00389           if((bradius=atof(str)) == 0)EndDialog(hwnd,FALSE);
00390           if(GetDlgItemText(hwnd,DLG_LRADIUS,str,10) == 0)
00391              EndDialog(hwnd,FALSE);
00392           if((lradius=atof(str)) == 0)EndDialog(hwnd,FALSE);
00393           if(GetDlgItemText(hwnd,DLG_RRADIUS,str,10) == 0)
00394              EndDialog(hwnd,FALSE);
00395           if((rradius=atof(str)) == 0)EndDialog(hwnd,FALSE);
00396           if(SendDlgItemMessage(hwnd,DLG_SPOTS,BM_GETCHECK,0,0))
00397             rtype=0; else rtype=1;
00398           EndDialog(hwnd,TRUE);
00399           return(TRUE);
00400         default:
00401           break;
00402       }
00403       break;
00404     default: break;
00405  }
00406  return FALSE;
00407 }
00408 
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3