oranges.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 // oraange.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 "oranges.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 
00043 #define IMOD(z,a) ((z) - ((z)/(a))*(a))
00044 #define PATTERN(x,y,z)   ( ((z)+16384L)*64536L +  \
00045                            ((y)+16384L)*32768L +  \
00046                            ((x)+16384L))
00047 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00048 #define FMOD(x,a) ((x) >= 0.0 ? fmod(x,a) :(a - fmod(-(x),a)))
00049 
00050 #include "randno.c"
00051 
00052 static long ip[27][3]={
00053    { 0, 0, 0},
00054    { 1, 0, 0},
00055    { 1, 1, 0},
00056    { 0, 1, 0},
00057    {-1, 1, 0},
00058    {-1, 0, 0},
00059    {-1,-1, 0},
00060    { 0,-1, 0},
00061    { 1,-1, 0},
00062    { 0, 0,-1},
00063    { 1, 0,-1},
00064    { 1, 1,-1},
00065    { 0, 1,-1},
00066    {-1, 1,-1},
00067    {-1, 0,-1},
00068    {-1,-1,-1},
00069    { 0,-1,-1},
00070    { 1,-1,-1},
00071    { 0, 0, 1},
00072    { 1, 0, 1},
00073    { 1, 1, 1},
00074    { 0, 1, 1},
00075    {-1, 1, 1},
00076    {-1, 0, 1},
00077    {-1,-1, 1},
00078    { 0,-1, 1},
00079    { 1,-1, 1}
00080  };
00081 
00082 #if __WATCOMC__
00083 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00084 #else
00085 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00086 #endif
00087   HANDLE ghMod;
00088   switch (dwReason) {
00089     case DLL_PROCESS_ATTACH:
00090 #if __X__MIPS__
00091       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00092 #endif
00093       hDLLinstance = hDLL;  /* handle to DLL file */
00094       break;
00095     case DLL_PROCESS_DETACH:
00096 #if __X__MIPS__
00097       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00098 #endif
00099       break;
00100   }
00101   return (int)TRUE;
00102 }
00103 
00104 #if __SC__
00105 #pragma startaddress(DllMain)
00106 #endif
00107 
00108 static double x=1.0,y=1.0,z=1.0;
00109 
00110 #if 0
00111 long CALLBACK ExternalTextureStartup(
00112 #else
00113 long _ExternalTextureStartup(
00114 #endif
00115   long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00116  if(parameter_list[0] != '#'){
00117    MessageBox ( GetFocus(),
00118                 (LPCTSTR) "External texture: Startup",
00119                 (LPCTSTR) "Parameter list missing",
00120                 MB_OK | MB_SYSTEMMODAL );
00121  }
00122  else {
00123   parameter_list++;
00124   sscanf(parameter_list,"%f %f %f",
00125          &x,&y,&z);
00126  }
00127  return LoadAndCompileShader("******");
00128 }
00129 
00130 #if 0
00131 long CALLBACK ExternalTextureMorph(
00132 #else
00133 long _ExternalTextureMorph(
00134 #endif
00135  char *parameter_list, double mr){
00136  double x_m,y_m,z_m;
00137  parameter_list++;
00138  sscanf(parameter_list,"%f %f %f",
00139         &x_m,&y_m,&z_m);
00140  x=x_m+(x-x_m)*mr;
00141  y=y_m+(y-y_m)*mr;
00142  z=z_m+(z-z_m)*mr;
00143  return 1;
00144 }
00145 
00146 #if 0
00147 long CALLBACK ExternalTextureProcedure(
00148 #else
00149 long _ExternalTextureProcedure(
00150 #endif
00151   long coord_type,  vector p, vector n,
00152   double alpha, double beta, double gamma,
00153   double bump,  double map_x,  double map_y,
00154   double *alpha_channel, unsigned char sc[3], double colour[3],
00155   double *reflectivity, double *transparency,
00156   X__SHADER *lpEVI
00157 ){
00158 
00159  static long N=1;
00160  static double bradius=1.2,lradius=1.0;
00161  long i;
00162  double a;
00163  vector u,v;
00164  double xx,yy,zz;
00165  double  dpoint[27];
00166  double dradius[27];
00167  vector pp[27];
00168  double dx,dy,dz,cx,cy,cz,zb,ccc,dc,zbb,radius;
00169  long ix,iy,iz;
00170  BOOL ins;
00171  long j,jid,id,pattern,patterni;
00172  double len,rad;
00173  vector ppc;
00174 
00175  xx=alpha / x;
00176  yy=beta  / y;
00177  zz=gamma / z;
00178  xx *= 60.0;
00179  yy *= 60.0;
00180  zz *= 60.0;
00181 
00182  id=0;
00183  ix=FLOOR(xx);
00184  iy=FLOOR(yy);
00185  iz=FLOOR(zz);
00186  cx=xx-(double)ix;
00187  cy=yy-(double)iy;
00188  cz=zz-(double)iz;
00189  zb=-10.0;
00190  ins=TRUE;
00191  id = -1;
00192  len=sqrt(tAxis_u[0]*tAxis_u[0]+
00193           tAxis_u[1]*tAxis_u[1]+
00194           tAxis_u[2]*tAxis_u[2]);
00195  VECCOPY(-tAxis_n,u)
00196  VECSCALE(len,u,u)         // third axis
00197  VECCOPY(tAxis_o,v)
00198  VECSUM(v,cx*tAxis_u,v)
00199  VECSUM(v,cy*tAxis_v,v)
00200  VECSUM(v,cz*u,v)         // coord of point on surface
00201  for(j=0;j<27;j++){
00202  pattern=PATTERN(ix+ip[j][0],iy+ip[j][1],iz+ip[j][2]);
00203  pattern=IMOD(N*pattern,1900);
00204  for(i=0;i<N;i++){
00205    jid=j*N+i;
00206    patterni=pattern+i;
00207    VECSUM((double)ip[j],rand_v[patterni],ppc)
00208    VECSUM(tAxis_o,ppc[0]*tAxis_u,pp[jid])
00209    VECSUM(pp[jid],ppc[1]*tAxis_v,pp[jid])
00210    VECSUM(pp[jid],ppc[2]*u,      pp[jid])
00211    radius=(bradius+lradius*(rand_v[patterni][1]-0.5))*len;
00212    dradius[jid]=radius;
00213    dpoint[jid] = -1.0;
00214    dx=(v[0]-pp[jid][0]);
00215    dy=(v[1]-pp[jid][1]);
00216    dz=(v[2]-pp[jid][2]);
00217    if(fabs(dx) > radius)continue;
00218    if(fabs(dy) > radius)continue;
00219    if(fabs(dz) > radius)continue;
00220    if((a=sqrt(dx*dx+dy*dy+dz*dz)) < radius){
00221      dpoint[jid]=a;
00222      dc=(radius-a);
00223      if(dc > zb){
00224        id=jid;
00225        zb=dc;
00226      }
00227    }
00228  }
00229  }
00230  if(id >= 0 && ins){
00231    {
00232      VECCOPY(pp[id],ppc)
00233      rad=dradius[id];
00234      VECSUB(v,ppc,u)          //(v-pp) vector from centre of pattern
00235      if((a=sqrt((u[0])*(u[0])+
00236                 (u[1])*(u[1])+
00237                 (u[2])*(u[2]))) < rad){
00238        double mu,moddn,rmu;
00239        vector c,dn;
00240        mu=DOT(u,n);
00241        c[0]=ppc[0]+mu*n[0];
00242        c[1]=ppc[1]+mu*n[1];
00243        c[2]=ppc[2]+mu*n[2];
00244        VECSUB(v,c,dn)
00245        moddn=sqrt(dn[0]*dn[0]+dn[1]*dn[1]+dn[2]*dn[2]);
00246        rmu=sqrt(rad*rad-mu*mu);
00247        if(moddn < rmu){
00248          a=1.0-moddn/rmu;
00249          normalize(dn);
00250          a=sin(a*1.57079);
00251          a=sqrt(a);
00252          VECSUM((1.0-a)*dn,(a)*n,n)
00253          normalize(n);
00254        }
00255      }
00256    }
00257  }
00258  return 1;
00259 }
00260 
00261 void _ExternalTextureClose(X__SHADER *lpEVI){
00262  UnloadCompiledShader(tGLshaderID);
00263 }
00264 
00265 long _ExternalTextureProcedureGL(
00266   double bump_scale,
00267   unsigned char sc[3],
00268   unsigned char ac[3],
00269   X__SHADER *lpEVI
00270 ){
00271  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00272  SetUniformVector(tGLshaderID,"ScalingV",60.0/x,60.0/y,60.0/z);
00273  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00274  return 1;
00275 }
00277 
00278 void CentreDialogOnScreen(HWND hwnd){
00279  RECT rcDlg;
00280  long Xres,Yres;
00281  Yres=GetSystemMetrics(SM_CYSCREEN);
00282  Xres=GetSystemMetrics(SM_CXSCREEN);
00283  GetWindowRect(hwnd,&rcDlg);
00284  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00285  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00286  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00287  return;
00288 }
00289 
00290 static void SetColour(double *colour, HWND parent){
00291  CHOOSECOLOR cc;
00292  static COLORREF CustColours[16]={
00293    RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00294    RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00295    RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00296    RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00297  cc.lStructSize=sizeof(CHOOSECOLOR);
00298  cc.hwndOwner=parent;
00299  cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00300  cc.lpCustColors=(LPDWORD)CustColours;
00301  cc.Flags= CC_RGBINIT;
00302  cc.lCustData=(DWORD)0;
00303  if(ChooseColor(&cc)){
00304    colour[0]=(double)GetRValue(cc.rgbResult);
00305    colour[1]=(double)GetGValue(cc.rgbResult);
00306    colour[2]=(double)GetBValue(cc.rgbResult);
00307  }
00308 }
00309 
00310 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00311 
00312 #if 0
00313 char * CALLBACK SetExternalParameters(
00314 #else
00315 char * _SetExternalParameters(
00316 #endif
00317   char *Op,                 /* string for the parameters                  */
00318   HWND hWnd,                /* parent window                              */
00319   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00320                              ){
00321  char szbuf[255],*Op1;
00322  if(Op != NULL){  /* parameters exist so read them off the list */
00323    Op1=Op;
00324    Op1++;
00325    sscanf(Op1,"%f %f %f",
00326          &x,&y,&z);
00327  }
00328  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00329               (DLGPROC)DlgProc) == FALSE)return Op;
00330  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00331  sprintf(szbuf,"# %.3f %.3f %.3f",
00332          x,y,z);
00333  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00334   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00335                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00336    return NULL;
00337  }
00338  strcpy(Op,szbuf);
00339  return Op;
00340 }
00341 
00342 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00343  char str[16];
00344  switch( msg ) {
00345    case WM_INITDIALOG:
00346      sprintf(str,"%.2f",x);
00347      SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00348      SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00349      sprintf(str,"%.2f",y);
00350      SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00351      SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00352      sprintf(str,"%.2f",z);
00353      SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00354      SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00355      CentreDialogOnScreen(hwnd);
00356      return TRUE;
00357    case WM_COMMAND:
00358      switch(LOWORD(wparam)){
00359         case IDCANCEL:
00360           EndDialog(hwnd,FALSE);
00361           return(TRUE);
00362         case IDOK:
00363           if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00364              EndDialog(hwnd,FALSE);
00365           if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00366           if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00367              EndDialog(hwnd,FALSE);
00368           if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00369           if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00370              EndDialog(hwnd,FALSE);
00371           if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00372           EndDialog(hwnd,TRUE);
00373           return(TRUE);
00374         default:
00375           break;
00376       }
00377       break;
00378     default: break;
00379  }
00380  return FALSE;
00381 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3