liqurish.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 // liquorice.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 "liqurish.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 #define VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00082 #define VECSUM(a,b,c)   { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00083 #define VECSUB(a,b,c)   { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00084 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00085 #define DOT(a,b)        ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00086 #define CROSS(v1,v2,r)  { \
00087                           r[0] = (v1[1]*v2[2]) - (v2[1]*v1[2]);  \
00088                           r[1] = (v1[2]*v2[0]) - (v1[0]*v2[2]);  \
00089                           r[2] = (v1[0]*v2[1]) - (v2[0]*v1[1]);  \
00090                         }
00091 
00092 #define IMOD(z,a) ((z) - ((z)/(a))*(a))
00093 
00094 #define PATTERN(x,y,z)   ( ((z)+16384L)*64536L +  \
00095                            ((y)+16384L)*32768L +  \
00096                            ((x)+16384L))
00097 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00098 #define FMOD(x,a) ((x) >= 0.0 ? fmod(x,a) :(a - fmod(-(x),a)))
00099 #define ROOT32 0.866025403
00100 #define ROOT3   1.73205080
00101 
00102 
00103 static double sharpness=0.0,coverage=0.8;
00104 static double c1[3]={0.0,0.0,200.0};
00105 static double x=1.0,y=1.0,z=1.0;
00106 
00107 
00108 #if 0
00109 long CALLBACK ExternalTextureStartup(
00110 #else
00111 long _ExternalTextureStartup(
00112 #endif
00113   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00114  long result;
00115  if(parameter_list[0] != '#'){
00116    MessageBox ( GetFocus(),
00117                 (LPCTSTR) "External texture: Startup",
00118                 (LPCTSTR) "Parameter list missing",
00119                 MB_OK | MB_SYSTEMMODAL );
00120  }
00121  else {
00122   parameter_list++;
00123   sscanf(parameter_list,"%f %f %f  %f %f  %f %f %f",
00124          &x,&y,&z,&coverage,&sharpness,
00125          &c1[0],&c1[1],&c1[2]);
00126  }
00127  result=LoadAndCompileShader("liqurish");
00128  return result;
00129 }
00130 
00131 #if 0
00132 long CALLBACK ExternalTextureMorph(
00133 #else
00134 long _ExternalTextureMorph(
00135 #endif
00136  char *parameter_list, double mr){
00137  int i;
00138  double coverage_m,sharpness_m;
00139  double c1_m[3];
00140  double x_m,y_m,z_m;
00141  parameter_list++;
00142  sscanf(parameter_list,"%f %f %f  %f %f  %f %f %f",
00143         &x_m,&y_m,&z_m,&coverage_m,&sharpness_m,
00144         &c1_m[0],&c1_m[1],&c1_m[2]);
00145  for(i=0;i<3;i++)c1[i]=c1_m[i]+(c1[i]-c1_m[i])*mr;
00146  coverage=coverage_m+(coverage-coverage_m)*mr;
00147  sharpness=sharpness_m+(sharpness-sharpness_m)*mr;
00148  x=x_m+(x-x_m)*mr;
00149  y=y_m+(y-y_m)*mr;
00150  z=z_m+(z-z_m)*mr;
00151  return 1;
00152 }
00153 
00154 static void mix(double *r, double *g, double *b, double mx,
00155                 double r1, double g1, double b1,
00156                 double r2, double g2, double b2){
00157  *r = mx*r1 + (1.0-mx)*r2;
00158  *g = mx*g1 + (1.0-mx)*g2;
00159  *b = mx*b1 + (1.0-mx)*b2;
00160 }
00161 
00162 #if 0
00163 long CALLBACK ExternalTextureProcedure(
00164 #else
00165 long _ExternalTextureProcedure(
00166 #endif
00167   long coord_type,  vector p, vector n,
00168   double alpha, double beta, double gamma,
00169   double bump,  double map_x,  double map_y,
00170   double *alpha_channel, unsigned char sc[3], double colour[3],
00171   double *reflectivity, double *transparency,
00172   X__SHADER *lpEVI
00173 ){
00174  static
00175  vector pp[7]={{0.0,     0.25,0.5},
00176                {0.0,     0.75,0.5},
00177                {ROOT32/2,0.0, 0.5},
00178                {ROOT32/2,0.5, 0.5},
00179                {ROOT32/2,1.0, 0.5},
00180                {ROOT32,  0.25,0.5},
00181                {ROOT32,  0.75,0.5}};
00182  static double cc=0.25;   // just spots
00183  double dx,dy,dz,zb;
00184  long i;
00185  double a,xx,yy,zz,r,g,b;
00186  xx=alpha / x;
00187  yy=beta  / y;
00188  zz=gamma / z;
00189  xx /= ROOT32;
00190  zz=FMOD(3*zz,2*3.1415926);
00191  xx += 0.11*sin(zz);
00192  yy += 0.15*cos(zz);
00193  zz=0.5;
00194  xx=FMOD(xx,ROOT32);
00195  yy=FMOD(yy,1.0);
00196  zb=0.0;
00197  r=colour[0];
00198  g=colour[1];
00199  b=colour[2];
00200  for(i=0;i<7;i++){
00201    dx=(xx-pp[i][0]);
00202    dy=(yy-pp[i][1]);
00203    dz=(zz-pp[i][2]);
00204    if(fabs(dx) > cc)continue;
00205    if(fabs(dy) > cc)continue;
00206    if(fabs(dz) > cc)continue;
00207    if((a=sqrt(dx*dx+dy*dy+dz*dz)) < cc){
00208      if((cc-a) > zb){
00209        zb=(cc-a);
00210        r=c1[0];
00211        g=c1[1];
00212        b=c1[2];
00213        if(a > 0.15){
00214          mix(&r,&g,&b,(a-0.15)/(cc-0.15),
00215           (double)colour[0],
00216           (double)colour[1],
00217           (double)colour[2],
00218           r,g,b);
00219        }
00220      }
00221    }
00222  }
00223  colour[0]=r;
00224  colour[1]=g;
00225  colour[2]=b;
00226  return 1;
00227 }
00228 
00229 
00230 void _ExternalTextureClose(X__SHADER *lpEVI){
00231  UnloadCompiledShader(tGLshaderID);
00232 }
00233 
00234 long _ExternalTextureProcedureGL(
00235   double bump_scale,
00236   unsigned char sc[3],
00237   unsigned char ac[3],
00238   X__SHADER *lpEVI
00239 ){
00240  // Set any shader specific uniform variables here.
00241  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)c1[0]/255.0,(GLfloat)c1[1]/255.0,(GLfloat)c1[2]/255.0);
00242  SetUniformVector(tGLshaderID,"ScalingV",1.0/x,1.0/y,1.0/z);
00243  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00244  return 1;
00245 }
00247 
00248 void CentreDialogOnScreen(HWND hwnd){
00249  RECT rcDlg;
00250  long Xres,Yres;
00251  Yres=GetSystemMetrics(SM_CYSCREEN);
00252  Xres=GetSystemMetrics(SM_CXSCREEN);
00253  GetWindowRect(hwnd,&rcDlg);
00254  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00255  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00256  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00257  return;
00258 }
00259 
00260 static void SetColour(double *colour, HWND parent){
00261  CHOOSECOLOR cc;
00262  static COLORREF CustColours[16]={
00263    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00264    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00265    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00266    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00267  cc.lStructSize=sizeof(CHOOSECOLOR);
00268  cc.hwndOwner=parent;
00269  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00270  cc.lpCustColors=(LPDWORD)CustColours;
00271  cc.Flags= CC_RGBINIT;
00272  cc.lCustData=(DWORD)0;
00273  if(ChooseColor(&cc)){
00274    colour[0]=(double)GetRValue(cc.rgbResult);
00275    colour[1]=(double)GetGValue(cc.rgbResult);
00276    colour[2]=(double)GetBValue(cc.rgbResult);
00277  }
00278 }
00279 
00280 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00281 
00282 #if 0
00283 char * CALLBACK SetExternalParameters(
00284 #else
00285 char * _SetExternalParameters(
00286 #endif
00287   char *Op,                 /* string for the parameters                  */
00288   HWND hWnd,                /* parent window                              */
00289   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00290                              ){
00291  char szbuf[255],*Op1;
00292  if(Op != NULL){  /* parameters exist so read them off the list */
00293    Op1=Op;
00294    Op1++;
00295    sscanf(Op1,"%f %f %f  %f %f  %f %f %f",
00296          &x,&y,&z,&coverage,&sharpness,
00297          &c1[0],&c1[1],&c1[2]);
00298  }
00299  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00300               (DLGPROC)DlgProc) == FALSE)return Op;
00301  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00302  sprintf(szbuf,"# %.2f %.2f %.2f  %.3f %.2f "
00303                  "%.2f %.2f %.2f ",
00304          x,y,z,coverage,sharpness,
00305          c1[0],c1[1],c1[2]);
00306  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00307   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00308                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00309    return NULL;
00310  }
00311  strcpy(Op,szbuf);
00312  return Op;
00313 }
00314 
00315 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00316  char str[16];
00317  switch( msg ) {
00318    case WM_INITDIALOG:
00319      CentreDialogOnScreen(hwnd);
00320      return TRUE;
00321    case WM_DRAWITEM:{
00322        LPDRAWITEMSTRUCT lpdis;
00323        HBRUSH   hbr,hbrold;
00324        BYTE r,g,b;
00325        lpdis=(LPDRAWITEMSTRUCT)lparam;
00326        if(lpdis->CtlID == DLG_COLOUR1){
00327          if(lpdis->CtlID == DLG_COLOUR1){
00328            r=(BYTE)c1[0]; g=(BYTE)c1[1]; b=(BYTE)c1[2];
00329          }
00330          if(lpdis->itemState & ODS_SELECTED)
00331             InvertRect(lpdis->hDC,&(lpdis->rcItem));
00332          else{
00333            hbr=CreateSolidBrush(RGB(r,g,b));
00334            hbrold=SelectObject(lpdis->hDC,hbr);
00335            Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
00336                          lpdis->rcItem.right,lpdis->rcItem.bottom);
00337            SelectObject(lpdis->hDC,hbrold);
00338            DeleteObject(hbr);
00339          }
00340        }
00341      }
00342      break;
00343    case WM_COMMAND:
00344      switch(LOWORD(wparam)){
00345         case DLG_COLOUR1:
00346           SetColour(c1,hwnd);
00347           InvalidateRect(GetDlgItem(hwnd,DLG_COLOUR1),NULL,FALSE);
00348           break;
00349         case IDCANCEL:
00350           EndDialog(hwnd,FALSE);
00351           return(TRUE);
00352         case IDOK:
00353           EndDialog(hwnd,TRUE);
00354           return(TRUE);
00355         default:
00356           break;
00357       }
00358       break;
00359     default: break;
00360  }
00361  return FALSE;
00362 }
00363 
00364 
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3