plates.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 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 You may contact the OpenFX development team via elecronic mail
00020 at core@openfx.org, or visit our website at http://openfx.org for
00021 further information and support details.
00022 -- */
00023 
00024 //  plates.c
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <math.h>
00029 #include <float.h>
00030 #include <windows.h>
00031 #include <gl/gl.h>
00032 
00033 #if __ZTC__ || __SC__
00034 #ifndef max
00035 #define max(a,b)  ( ((a) > (b)) ? (a) : (b) )
00036 #endif
00037 #endif
00038 
00039 #if __WATCOMC__
00040 #define PI 3.1415926
00041 #endif
00042 
00043 #if __X__MIPS__
00044 BOOL WINAPI _CRT_INIT(HINSTANCE ,DWORD , LPVOID );
00045 #endif
00046 
00047 static HINSTANCE hDLLinstance=NULL;
00048 
00049 #include "..\animate\memory.h" /* for memory definition */
00050 #include "..\animate\memdef.h" /* local names           */
00051 #include "defines.h"
00052 #include "rstruct.h"
00053 #include "plates.h"
00054 
00055 #if __WATCOMC__
00056 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00057 #else
00058 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00059 #endif
00060   HANDLE ghMod;
00061   switch (dwReason) {
00062     case DLL_PROCESS_ATTACH:
00063 #if __X__MIPS__
00064       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00065 #endif
00066       hDLLinstance = hDLL;  /* handle to DLL file */
00067       break;
00068     case DLL_PROCESS_DETACH:
00069 #if __X__MIPS__
00070       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00071 #endif
00072       break;
00073   }
00074   return (int)TRUE;
00075 }
00076 
00077 #if __SC__
00078 #pragma startaddress(DllMain)
00079 #endif
00080 
00081 static int type=1, dirty=1;
00082 static double dhue=0.05,dval=0.3;
00083 static double x=1.0,y=1.0,z=1.0;
00084 
00085 #if 0
00086 long CALLBACK ExternalTextureStartup(
00087 #else
00088 long _ExternalTextureStartup(
00089 #endif
00090   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI
00091 ){
00092  if(parameter_list[0] != '#'){
00093    MessageBox ( GetFocus(),
00094                 (LPCTSTR) "External texture: Startup",
00095                 (LPCTSTR) "Parameter list missing",
00096                 MB_OK | MB_SYSTEMMODAL );
00097  }
00098  else {
00099   parameter_list++;
00100   sscanf(parameter_list,"%f %f %f  %f %f  %ld %ld",
00101          &x,&y,&z,&dhue,&dval,&type,&dirty);
00102  }
00103  return LoadAndCompileShader("plates");
00104 }
00105 
00106 #if 0
00107 long CALLBACK ExternalTextureMorph(
00108 #else
00109 long _ExternalTextureMorph(
00110 #endif
00111  char *parameter_list, double mr){
00112  double dhue_m,dval_m;
00113  double x_m,y_m,z_m;
00114  parameter_list++;
00115  sscanf(parameter_list,"%f %f %f  %f %f",
00116         &x_m,&y_m,&z_m,&dhue_m,&dval_m);
00117  dhue=dhue_m+(dhue-dhue_m)*mr;
00118  dval=dval_m+(dval-dval_m)*mr;
00119  x=x_m+(x-x_m)*mr;
00120  y=y_m+(y-y_m)*mr;
00121  z=z_m+(z-z_m)*mr;
00122  return 1;
00123 }
00124 
00125 #define VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00126 #define VECSUM(a,b,c)   { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00127 #define VECSUB(a,b,c)   { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00128 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00129 #define DOT(a,b)        ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00130 #define CROSS(v1,v2,r)  { \
00131                           r[0] = (v1[1]*v2[2]) - (v2[1]*v1[2]);  \
00132                           r[1] = (v1[2]*v2[0]) - (v1[0]*v2[2]);  \
00133                           r[2] = (v1[0]*v2[1]) - (v2[0]*v1[1]);  \
00134                         }
00135 #define IMOD(z,a) ((z) - ((z)/(a))*(a))
00136 #define PATTERN(x,y,z)   ( ((z)+16384L)*64536L +  \
00137                            ((y)+16384L)*32768L +  \
00138                            ((x)+16384L))
00139 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00140 #define FMOD(x,a) ((x) >= 0.0 ? fmod(x,a) :(a - fmod(-(x),a)))
00141 #define ROOT32 0.866025403
00142 #define ROOT3   1.73205080
00143 
00144 #include "randno.c"
00145 
00146 /* h is 0->360  s and v are 0-1 */
00147 void Hsv2Rgb(double h, double s, double v,
00148              double *r, double *g, double *b){
00149  double rr,gg,bb,hh,ss,vv,maxcol,mincol;
00150  hh=(double)h; ss=(double)s; vv=(double)v;
00151  mincol=vv*(1.0-ss);
00152  if(hh <= 120.0){bb=mincol;
00153    if(hh <=  60){rr=vv; gg=mincol+hh*(vv-mincol)/(120.0-hh);}
00154    else         {gg=vv; rr=mincol+(120.0-hh)*(vv-mincol)/hh;}
00155  }
00156  else if( hh <= 240){rr=mincol;
00157    if(hh <= 180){gg=vv; bb=mincol+(hh-120.0)*(vv-mincol)/(240.0-hh);}
00158    else         {bb=vv; gg=mincol+(240.0-hh)*(vv-mincol)/(hh-120.0);}
00159  }
00160  else {gg=mincol;
00161    if(hh <= 300){bb=vv; rr=mincol+(hh-240.0)*(vv-mincol)/(360.0-hh);}
00162    else         {rr=vv; bb=mincol+(360.0-hh)*(vv-mincol)/(hh-240.0);}
00163  }
00164  *r = rr;
00165  *g = gg;
00166  *b = bb;
00167 }
00168 
00169 void Rgb2Hsv(double rr, double gg, double bb,  // rr etc. are (0-1)
00170              double *h, double *s, double *v){ // h (0-360)  s v (0-1)
00171  double maxcol,mincol;
00172  maxcol=max(rr,max(gg,bb));
00173  mincol=min(rr,min(gg,bb));
00174  *v=maxcol;
00175  if(maxcol == mincol){*s=0; *h=0;}
00176  else{
00177   *s=(maxcol-mincol)/maxcol;
00178   if(mincol == bb)
00179     *h = 120.0*(gg-mincol)/(rr+gg-2.0*mincol);
00180   else if(mincol == rr)
00181     *h = 120.0*(1.0+(bb-mincol)/(bb+gg-2.0*mincol));
00182   else
00183     *h = 120.0*(2.0+(rr-mincol)/(rr+bb-2.0*mincol));
00184  }
00185 }
00186 
00187 #if 0
00188 long CALLBACK ExternalTextureProcedure(
00189 #else
00190 long _ExternalTextureProcedure(
00191 #endif
00192   long coord_type,  vector p, vector n,
00193   double alpha, double beta, double gamma,
00194   double bump,  double map_x,  double map_y,
00195   double *alpha_channel, unsigned char sc[3], double colour[3],
00196   double *reflectivity, double *transparency,
00197   X__SHADER *lpEVI
00198 ){
00199  long id;
00200  double xx,yy,zz,noise;
00201  double xr,xl,yr,yl,zr,zl,d=0.05;
00202  vector v;
00203  xx=5.0*alpha / x;
00204  yy=5.0*beta  / y;
00205  zz=5.0*gamma / z;
00206 
00207  zz *=2.0;
00208 
00209  id=PATTERN((long)FLOOR(xx),(long)FLOOR(yy),(long)FLOOR(zz));
00210  id=IMOD(id,1900);
00211  if(type == 0){
00212    colour[0] = rand_v[id][0]*255.0;
00213    colour[1] = rand_v[id][1]*255.0;
00214    colour[2] = rand_v[id][2]*255.0;
00215  }
00216  else{
00217    double hue,sat,val;
00218    Rgb2Hsv(colour[0]/255.0,colour[1]/255.0,colour[2]/255.0,&hue,&sat,&val);
00219    hue += dhue*(rand_v[id][0]-0.5)*360.0;
00220    if(hue <   0.0)hue += 360.0;
00221    if(hue > 360.0)hue -= 360.0;
00222    val += val*dval*(rand_v[id][1]-0.5);
00223    val=min(val,1.0);
00224    val=max(val,0.0);
00225    Hsv2Rgb(hue,sat,val,&colour[0],&colour[1],&colour[2]);
00226    colour[0] *= 255.0;
00227    colour[1] *= 255.0;
00228    colour[2] *= 255.0;
00229  }
00230  if(dirty){
00231    nNoise(xx*2.0,yy*2.0,zz*1.0,&noise);
00232    colour[0] -= (noise*0.3)*255.0;
00233    colour[1] -= (noise*0.3)*255.0;
00234    colour[2] -= (noise*0.3)*255.0;
00235    colour[0]=max(colour[0],0.0);
00236    colour[1]=max(colour[1],0.0);
00237    colour[2]=max(colour[2],0.0);
00238  }
00239 
00240  xx -= FLOOR(xx);
00241  yy -= FLOOR(yy);
00242  zz -= FLOOR(zz);
00243  v[0]=0.0; v[1]=0.0; v[2]=0.0;
00244  if(xx < d){
00245    VECCOPY(-tAxis_u,v);
00246    normalize(v);
00247  }
00248  else if(xx > 1.0-d){
00249    VECCOPY(tAxis_u,v);
00250    normalize(v);
00251  }
00252  else if(yy < d){
00253    VECCOPY(-tAxis_v,v);
00254    normalize(v);
00255  }
00256  else if(yy > 1.0-d){
00257    VECCOPY(tAxis_v,v);
00258    normalize(v);
00259  }
00260  else if(zz < d){
00261    VECCOPY(-tAxis_v,v);
00262    normalize(v);
00263  }
00264  else if(zz > 1.0-d){
00265    VECCOPY(tAxis_v,v);
00266    normalize(v);
00267  }
00268  VECSUM(0.25*v,n,n);
00269 
00270  return 1;
00271 }
00272 
00273 void _ExternalTextureClose(X__SHADER *lpEVI){
00274  UnloadCompiledShader(tGLshaderID);
00275 }
00276 
00277 long _ExternalTextureProcedureGL(
00278   double bump_scale,
00279   unsigned char sc[3],
00280   unsigned char ac[3],
00281   X__SHADER *lpEVI
00282 ){
00283  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00284  SetUniformVector(tGLshaderID,"ScalingV",5.0/x,5.0/z,5.0/y);
00285  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00286  return 1;
00287 }
00289 
00290 void CentreDialogOnScreen(HWND hwnd){
00291  RECT rcDlg;
00292  long Xres,Yres;
00293  Yres=GetSystemMetrics(SM_CYSCREEN);
00294  Xres=GetSystemMetrics(SM_CXSCREEN);
00295  GetWindowRect(hwnd,&rcDlg);
00296  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00297  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00298  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00299  return;
00300 }
00301 
00302 static void SetColour(double *colour, HWND parent){
00303  CHOOSECOLOR cc;
00304  static COLORREF CustColours[16]={
00305    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00306    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00307    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00308    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00309  cc.lStructSize=sizeof(CHOOSECOLOR);
00310  cc.hwndOwner=parent;
00311  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00312  cc.lpCustColors=(LPDWORD)CustColours;
00313  cc.Flags= CC_RGBINIT;
00314  cc.lCustData=(DWORD)0;
00315  if(ChooseColor(&cc)){
00316    colour[0]=(double)GetRValue(cc.rgbResult);
00317    colour[1]=(double)GetGValue(cc.rgbResult);
00318    colour[2]=(double)GetBValue(cc.rgbResult);
00319  }
00320 }
00321 
00322 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00323 
00324 #if 0
00325 char * CALLBACK SetExternalParameters(
00326 #else
00327 char * _SetExternalParameters(
00328 #endif
00329   char *Op,                 /* string for the parameters                  */
00330   HWND hWnd,                /* parent window                              */
00331   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00332                              ){
00333  char szbuf[255],*Op1;
00334  if(Op != NULL){  /* parameters exist so read them off the list */
00335    Op1=Op;
00336    Op1++;
00337    sscanf(Op1,"%f %f %f  %f %f  %ld %ld",
00338          &x,&y,&z,&dhue,&dval,&type,&dirty);
00339  }
00340  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00341               (DLGPROC)DlgProc) == FALSE)return Op;
00342  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00343  sprintf(szbuf,"# %.2f %.2f %.2f  %.3f %.3f  %ld %ld",
00344         x,y,z,dhue,dval,type,dirty);
00345  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00346   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00347                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00348    return NULL;
00349  }
00350  strcpy(Op,szbuf);
00351  return Op;
00352 }
00353 
00354 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00355  char str[16];
00356  switch( msg ) {
00357    case WM_INITDIALOG:
00358      sprintf(str,"%.2f",x);
00359      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00360      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00361      sprintf(str,"%.2f",y);
00362      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00363      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00364      sprintf(str,"%.2f",z);
00365      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00366      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00367      sprintf(str,"%.3f",dval);
00368      SendDlgItemMessage(hwnd,DLG_DVAL,WM_SETTEXT,0,(LPARAM)str);
00369      SendDlgItemMessage(hwnd,DLG_DVAL,EM_LIMITTEXT,(WPARAM)12,0);
00370      sprintf(str,"%.3f",dhue);
00371      SendDlgItemMessage(hwnd,DLG_DHUE,WM_SETTEXT,0,(LPARAM)str);
00372      SendDlgItemMessage(hwnd,DLG_DHUE,EM_LIMITTEXT,(WPARAM)12,0);
00373      if(dirty)SendDlgItemMessage(hwnd,DLG_DIRTY,BM_SETCHECK,TRUE,0);
00374      CentreDialogOnScreen(hwnd);
00375      return TRUE;
00376    case WM_COMMAND:
00377      switch(LOWORD(wparam)){
00378         case IDCANCEL:
00379           EndDialog(hwnd,FALSE);
00380           return(TRUE);
00381         case IDOK:
00382           if(GetDlgItemText(hwnd,DLG_DHUE,str,10) == 0)
00383              EndDialog(hwnd,FALSE);
00384           if((dhue=atof(str)) == 0)EndDialog(hwnd,FALSE);
00385           if(GetDlgItemText(hwnd,DLG_DVAL,str,10) == 0)
00386              EndDialog(hwnd,FALSE);
00387           if((dval=atof(str)) == 0)EndDialog(hwnd,FALSE);
00388           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00389              EndDialog(hwnd,FALSE);
00390           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00391           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00392              EndDialog(hwnd,FALSE);
00393           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00394           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00395              EndDialog(hwnd,FALSE);
00396           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00397           if(SendDlgItemMessage(hwnd,DLG_DIRTY,BM_GETCHECK,0,0))dirty=1;
00398           else dirty=0;
00399           EndDialog(hwnd,TRUE);
00400           return(TRUE);
00401         default:
00402           break;
00403       }
00404       break;
00405     default: break;
00406  }
00407  return FALSE;
00408 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3