fccdots.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 //    FCCDOTS.C    xternal texture for 3D Basic Fractal texture
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 "fccdots.h"
00054 
00055 
00056 #if __WATCOMC__
00057 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00058 #else
00059 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00060 #endif
00061   HANDLE ghMod;
00062   switch (dwReason) {
00063     case DLL_PROCESS_ATTACH:
00064 #if __X__MIPS__
00065       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00066 #endif
00067       hDLLinstance = hDLL;  /* handle to DLL file */
00068       break;
00069     case DLL_PROCESS_DETACH:
00070 #if __X__MIPS__
00071       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00072 #endif
00073       break;
00074   }
00075   return (int)TRUE;
00076 }
00077 
00078 #if __SC__
00079 #pragma startaddress(DllMain)
00080 #endif
00081 
00082 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00083 
00084 static vector pp[6]={{0.0,0.5,0.5},
00085                      {0.5,1.0,0.5},
00086                      {1.0,0.5,0.5},
00087                      {0.5,0.0,0.5},
00088                      {0.5,0.5,0.0},
00089                      {0.5,0.5,1.0}};
00090 
00091 static void mix(double *r, double *g, double *b, double mx,
00092                 double r1, double g1, double b1,
00093                 double r2, double g2, double b2){
00094  *r = mx*r1 + (1.0-mx)*r2;
00095  *g = mx*g1 + (1.0-mx)*g2;
00096  *b = mx*b1 + (1.0-mx)*b2;
00097 }
00098 
00099 
00100 static double size=0.4,edge=0.8;
00101 static double c1[3]={255.0,0.0,0.0};
00102 static double c2[3]={0.0,255.0,0.0};
00103 static double c3[3]={0.0,0.0,255.0};
00104 static double x=1.0,y=1.0,z=1.0;
00105 
00106 #if 0
00107 long CALLBACK ExternalTextureStartup(
00108 #else
00109 long _ExternalTextureStartup(
00110 #endif
00111   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00112  if(parameter_list[0] != '#'){
00113    MessageBox ( GetFocus(),
00114                 (LPCTSTR) "External texture: Startup",
00115                 (LPCTSTR) "Parameter list missing",
00116                 MB_OK | MB_SYSTEMMODAL );
00117  }
00118  else {
00119   parameter_list++;
00120   sscanf(parameter_list,"%f %f %f  %f %f  %f %f %f  %f %f %f   %f %f %f",
00121          &x,&y,&z,&size,&edge,
00122          &c1[0],&c1[1],&c1[2],
00123          &c2[0],&c2[1],&c2[2],
00124          &c3[0],&c3[1],&c3[2]);
00125  }
00126  return LoadAndCompileShader("fccdots");;
00127 }
00128 
00129 #if 0
00130 long CALLBACK ExternalTextureMorph(
00131 #else
00132 long _ExternalTextureMorph(
00133 #endif
00134  char *parameter_list, double mr){
00135  int i;
00136  double size_m,edge_m;
00137  double c1_m[3],c2_m[3],c3_m[3];
00138  double x_m,y_m,z_m;
00139  parameter_list++;
00140  sscanf(parameter_list,"%f %f %f  %f %f  %f %f %f  %f %f %f   %f %f %f",
00141         &x_m,&y_m,&z_m,&size_m,&edge_m,
00142         &c1_m[0],&c1_m[1],&c1_m[2],
00143         &c2_m[0],&c2_m[1],&c2_m[2],
00144         &c3_m[0],&c3_m[1],&c3_m[2]);
00145  for(i=0;i<3;i++)c1[i]=c1_m[i]+(c1[i]-c1_m[i])*mr;
00146  for(i=0;i<3;i++)c2[i]=c2_m[i]+(c2[i]-c2_m[i])*mr;
00147  for(i=0;i<3;i++)c3[i]=c3_m[i]+(c3[i]-c3_m[i])*mr;
00148  size=size_m+(size-size_m)*mr;
00149  edge=edge_m+(edge-edge_m)*mr;
00150  x=x_m+(x-x_m)*mr;
00151  y=y_m+(y-y_m)*mr;
00152  z=z_m+(z-z_m)*mr;
00153  return 1;
00154 }
00155 
00156 #if 0
00157 long CALLBACK ExternalTextureProcedure(
00158 #else
00159 long _ExternalTextureProcedure(
00160 #endif
00161   long coord_type,  vector p, vector n,
00162   double alpha, double beta, double gamma,
00163   double bump,  double map_x,  double map_y,
00164   double *alpha_channel, unsigned char sc[3], double colour[3],
00165   double *reflectivity, double *transparency,
00166   X__SHADER *lpEVI
00167 ){
00168  long i;
00169  double xx,yy,zz,r,g,b;
00170  double dx,dy,dz,zb,e,a;
00171  xx=alpha / x;
00172  yy=beta  / y;
00173  zz=gamma / z;
00174  xx=(xx-FLOOR(xx));
00175  yy=(yy-FLOOR(yy));
00176  zz=(zz-FLOOR(zz));
00177  zb=0.0;
00178  e=(edge/100.0)*size;
00179  r=(double)colour[0];
00180  g=(double)colour[1];
00181  b=(double)colour[2];
00182  for(i=0;i<6;i++){
00183    dx=(xx-pp[i][0]);
00184    dy=(yy-pp[i][1]);
00185    dz=(zz-pp[i][2]);
00186    if((a=sqrt(dx*dx+dy*dy+dz*dz)) < size){
00187      if((size-a) > zb){
00188        zb=(size-a);
00189        if(i == 0 || i == 2){
00190          r=c1[0];
00191          g=c1[1];
00192          b=c1[2];
00193        }
00194        else if(i == 1 || i == 3){
00195          r=c2[0];
00196          g=c2[1];
00197          b=c2[2];
00198        }
00199        else{
00200          r=c3[0];
00201          g=c3[1];
00202          b=c3[2];
00203        }
00204        if(a > e){
00205          mix(&r,&g,&b,(a-e)/(size-e),
00206           (double)colour[0],
00207           (double)colour[1],
00208           (double)colour[2],
00209           r,g,b);
00210        }
00211      }
00212    }
00213  }
00214  colour[0]  = (unsigned char)r;
00215  colour[1]  = (unsigned char)g;
00216  colour[2]  = (unsigned char)b;
00217  return 1;
00218 }
00219 
00220 void _ExternalTextureClose(X__SHADER *lpEVI){
00221  UnloadCompiledShader(tGLshaderID);
00222 }
00223 
00224 long _ExternalTextureProcedureGL(
00225   double bump_scale,
00226   unsigned char sc[3],
00227   unsigned char ac[3],
00228   X__SHADER *lpEVI
00229 ){
00230  SetUniformVector(tGLshaderID,"ScalingV",1.0/x,1.0/z,1.0/y);
00231  SetUniformVector(tGLshaderID,"ColourC1",(GLfloat)c1[0]/255.0,(GLfloat)c1[1]/255.0,(GLfloat)c1[2]/255.0);
00232  SetUniformVector(tGLshaderID,"ColourC2",(GLfloat)c2[0]/255.0,(GLfloat)c2[1]/255.0,(GLfloat)c2[2]/255.0);
00233  SetUniformVector(tGLshaderID,"ColourC3",(GLfloat)c3[0]/255.0,(GLfloat)c3[1]/255.0,(GLfloat)c3[2]/255.0);
00234  SetUniformVariable(tGLshaderID,"Size",size);
00235  SetUniformVariable(tGLshaderID,"Edge",edge);
00236  // Callback to Render the Polygons - this allows for multiple passes - through same shaders
00237  // possibly with different parameters - including vertex offset - blending depth enabling 
00238  // to allow for multipass textures e.g. hair and fur (shell model) it's arguments must not be altered!!!
00239  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00240  return 1;
00241 }
00243 
00244 void CentreDialogOnScreen(HWND hwnd){
00245  RECT rcDlg;
00246  long Xres,Yres;
00247  Yres=GetSystemMetrics(SM_CYSCREEN);
00248  Xres=GetSystemMetrics(SM_CXSCREEN);
00249  GetWindowRect(hwnd,&rcDlg);
00250  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00251  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00252  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00253  return;
00254 }
00255 
00256 static void SetColour(double *colour, HWND parent){
00257  CHOOSECOLOR cc;
00258  static COLORREF CustColours[16]={
00259    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00260    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00261    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00262    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00263  cc.lStructSize=sizeof(CHOOSECOLOR);
00264  cc.hwndOwner=parent;
00265  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00266  cc.lpCustColors=(LPDWORD)CustColours;
00267 // cc.Flags=CC_FULLOPEN | CC_RGBINIT | CC_ENABLEHOOK;
00268  cc.Flags= CC_RGBINIT;
00269  cc.lCustData=(DWORD)0;
00270  if(ChooseColor(&cc)){
00271    colour[0]=(double)GetRValue(cc.rgbResult);
00272    colour[1]=(double)GetGValue(cc.rgbResult);
00273    colour[2]=(double)GetBValue(cc.rgbResult);
00274  }
00275 }
00276 
00277 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00278 
00279 #if 0
00280 char * CALLBACK SetExternalParameters(
00281 #else
00282 char * _SetExternalParameters(
00283 #endif
00284   char *Op,                 /* string for the parameters                  */
00285   HWND hWnd,                /* parent window                              */
00286   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00287                              ){
00288  char szbuf[255],*Op1;
00289  if(Op != NULL){  /* parameters exist so read them off the list */
00290    Op1=Op;
00291    Op1++;
00292    sscanf(Op1,"%f %f %f  %f %f  %f %f %f  %f %f %f   %f %f %f",
00293          &x,&y,&z,&size,&edge,
00294          &c1[0],&c1[1],&c1[2],
00295          &c2[0],&c2[1],&c2[2],
00296          &c3[0],&c3[1],&c3[2]);
00297  }
00298  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00299               (DLGPROC)DlgProc) == FALSE)return Op;
00300  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00301  sprintf(szbuf,"# %.2f %.2f %.2f  %.3f %.2f "
00302                  "%.2f %.2f %.2f "
00303                  "%.2f %.2f %.2f "
00304                  "%.2f %.2f %.2f",
00305          x,y,z,size,edge,
00306          c1[0],c1[1],c1[2],
00307          c2[0],c2[1],c2[2],
00308          c3[0],c3[1],c3[2]);
00309  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00310   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00311                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00312    return NULL;
00313  }
00314  strcpy(Op,szbuf);
00315  return Op;
00316 }
00317 
00318 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00319  char str[16];
00320  switch( msg ) {
00321    case WM_INITDIALOG:
00322      sprintf(str,"%.2f",x);
00323      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00324      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00325      sprintf(str,"%.2f",y);
00326      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00327      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00328      sprintf(str,"%.2f",z);
00329      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00330      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00331      sprintf(str,"%.2f",size);
00332      SendDlgItemMessage(hwnd,DLG_SIZE,WM_SETTEXT,0,(LPARAM)str);
00333      SendDlgItemMessage(hwnd,DLG_SIZE,EM_LIMITTEXT,(WPARAM)12,0);
00334      sprintf(str,"%.2f",edge);
00335      SendDlgItemMessage(hwnd,DLG_EDGE,WM_SETTEXT,0,(LPARAM)str);
00336      SendDlgItemMessage(hwnd,DLG_EDGE,EM_LIMITTEXT,(WPARAM)12,0);
00337      CentreDialogOnScreen(hwnd);
00338      return TRUE;
00339    case WM_DRAWITEM:{
00340        LPDRAWITEMSTRUCT lpdis;
00341        HBRUSH   hbr,hbrold;
00342        BYTE r,g,b;
00343        lpdis=(LPDRAWITEMSTRUCT)lparam;
00344        if(lpdis->CtlID == DLG_COLOUR1 ||
00345           lpdis->CtlID == DLG_COLOUR2 ||
00346           lpdis->CtlID == DLG_COLOUR3){
00347          if(lpdis->CtlID == DLG_COLOUR1){
00348            r=(BYTE)c1[0]; g=(BYTE)c1[1]; b=(BYTE)c1[2];
00349          }
00350          else if(lpdis->CtlID == DLG_COLOUR2){
00351            r=(BYTE)c2[0]; g=(BYTE)c2[1]; b=(BYTE)c2[2];
00352          }
00353          else if(lpdis->CtlID == DLG_COLOUR3){
00354            r=(BYTE)c3[0]; g=(BYTE)c3[1]; b=(BYTE)c3[2];
00355          }
00356          if(lpdis->itemState & ODS_SELECTED)
00357             InvertRect(lpdis->hDC,&(lpdis->rcItem));
00358          else{
00359            hbr=CreateSolidBrush(RGB(r,g,b));
00360            hbrold=SelectObject(lpdis->hDC,hbr);
00361            Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
00362                          lpdis->rcItem.right,lpdis->rcItem.bottom);
00363            SelectObject(lpdis->hDC,hbrold);
00364            DeleteObject(hbr);
00365          }
00366        }
00367      }
00368      break;
00369    case WM_COMMAND:
00370      switch(LOWORD(wparam)){
00371         case DLG_COLOUR1:
00372           SetColour(c1,hwnd);
00373           InvalidateRect(GetDlgItem(hwnd,DLG_COLOUR1),NULL,FALSE);
00374           break;
00375         case DLG_COLOUR2:
00376           SetColour(c2,hwnd);
00377           InvalidateRect(GetDlgItem(hwnd,DLG_COLOUR2),NULL,FALSE);
00378           break;
00379         case DLG_COLOUR3:
00380           SetColour(c3,hwnd);
00381           InvalidateRect(GetDlgItem(hwnd,DLG_COLOUR3),NULL,FALSE);
00382           break;
00383         case IDCANCEL:
00384           EndDialog(hwnd,FALSE);
00385           return(TRUE);
00386         case IDOK:
00387           if(GetDlgItemText(hwnd,DLG_EDGE,str,10) == 0)
00388              EndDialog(hwnd,FALSE);
00389           if((edge=atof(str)) == 0)EndDialog(hwnd,FALSE);
00390           if(GetDlgItemText(hwnd,DLG_SIZE,str,10) == 0)
00391              EndDialog(hwnd,FALSE);
00392           if((size=atof(str)) == 0)EndDialog(hwnd,FALSE);
00393           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00394              EndDialog(hwnd,FALSE);
00395           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00396           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00397              EndDialog(hwnd,FALSE);
00398           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00399           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00400              EndDialog(hwnd,FALSE);
00401           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00402           EndDialog(hwnd,TRUE);
00403           return(TRUE);
00404         default:
00405           break;
00406       }
00407       break;
00408     default: break;
00409  }
00410  return FALSE;
00411 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3