DROPLETS.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 /* Droplets  Image post-processor using Z buffer                            */
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <float.h>
00029 #include <math.h>
00030 #include <windows.h>
00031 #include <commctrl.h>
00032 #include "struct.h"           /* general structures    */
00033 #include "..\common\postprocess\ximage.h"
00034 #include "local.h"
00035 
00036 #include "droplets.h"
00037 
00038 static long version=0;
00039 
00040 #if __X__MIPS__
00041 BOOL WINAPI _CRT_INIT(HINSTANCE ,DWORD , LPVOID );
00042 #endif
00043 
00044 static HINSTANCE hDLLinstance=NULL; /* use to pick up resources from DLL  */
00045 
00046 #include "utils.h"
00047 #include "paint.c"
00048 
00049 //#include "..\animate\wspaces.c"
00050 
00051 unsigned char *LoadMAP(char *name, int *x, int *y);
00052 
00053 #if __WATCOMC__
00054 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00055 #elif __BC__
00056 BOOL WINAPI DllEntryPoint(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00057 #else
00058 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00059 #endif
00060   switch (dwReason) {
00061     case DLL_PROCESS_ATTACH:
00062 #if __X__MIPS__
00063       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00064 #endif
00065       hDLLinstance = hDLL;  /* handle to DLL file */
00066       break;
00067     case DLL_PROCESS_DETACH:
00068 #if __X__MIPS__
00069       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00070 #endif
00071       break;
00072   }
00073 return (int)TRUE;
00074 }
00075 
00076 #if __SC__
00077 #pragma startaddress(DllMain)
00078 #endif
00079 
00080 static void DrawRingInBuffer(fullscreenbuffer *Screen, long X, long Y,
00081                              long x, long y, long radius){
00082  long i,j;
00083  double d,dc;
00084  long oradius,iradius;
00085  double minV=0.0,maxV=1.0;
00086  fullscreenbuffer *S;
00087  oradius=radius;
00088  iradius=oradius-20;
00089  dc=0.5*(double)(iradius+oradius);
00090  for(i=x-oradius;i<=x+oradius;i++)for(j=y-oradius;j<=y+oradius;j++){
00091    if(i < 0 || i >= X || j < 0 || j >= Y)continue;
00092    S=(Screen + j*X + i);
00093    d=sqrt((double)(i-x)*(double)(i-x) + (double)(j-y)*(double)(j-y));
00094    if(d >= (double)iradius && d <= (double)oradius){
00095      if(d > dc){
00096        d=maxV-(maxV-minV)*(d-dc)/((double)oradius-dc);
00097      }
00098      else{
00099        d=minV+(maxV-minV)*(d-(double)iradius)/(dc-(double)iradius);
00100      }
00101      S->R = (unsigned char)min(255.0,(double)S->R+d*255);
00102      S->G = (unsigned char)min(255.0,(double)S->G+d*255);
00103      S->B = (unsigned char)min(255.0,(double)S->B+d*255);
00104    }
00105  }
00106  return;
00107 }
00108 
00109 static void DrawRingsInBuffer(fullscreenbuffer *B, long X, long Y){
00110  DrawRingInBuffer(B,X,Y,X/2,Y/2,X/4);
00111  DrawRingInBuffer(B,X,Y,X/4,Y/3,X/6);
00112  DrawRingInBuffer(B,X,Y,3*X/4,2*Y/3,X/6);
00113  return;
00114 }
00115 
00116 long _RenderImageProcess(char *PrmList, XIMAGE *lpXimage){
00117  double mr;
00118  char name_path[256],imagefile[256];
00119  fullscreenbuffer *Screen,*BackBuffer;
00120  BOOL bMorph=FALSE;
00121  long cR,cG,cB,mcR,mcG,mcB,dummy,type,hold,back,lframe,nx,ny,Xmax,Ymax,frame;
00122  sscanf(PrmList,"%s %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %s",name_path,
00123         &version,&type,&hold,
00124         &nx,&ny,&cR,&cG,&cB,&back,&lframe,imagefile);
00125  if(lpXimage->Morph && lpXimage->mParameters != NULL){
00126    bMorph=TRUE; mr=lpXimage->MorphRatio;
00127    sscanf(lpXimage->mParameters,
00128           "%s %ld %ld %ld %ld %ld %ld %ld %ld",
00129           name_path,&version,&dummy,&dummy,&dummy,&dummy,&dummy,
00130           &mcR,&mcG,&mcB);
00131  }
00132  if(!bMorph){
00133    mr=(double)(lpXimage->last_frame - lpXimage->first_frame);
00134    if(mr < 1.0)mr=0.0; //return 1;
00135    else mr=(double)(lpXimage->this_frame - lpXimage->first_frame)/mr;
00136  }
00137  frame=lpXimage->this_frame - lpXimage->first_frame + 1;
00138  if     (hold == 1)mr=(1.0-mr);
00139  else if(hold == 2)mr=1.0;
00140  else if(hold == 3)mr=0.0;
00141  BackBuffer=NULL;
00142  Xmax=lpXimage->Xmax; Ymax=lpXimage->Ymax;
00143  Screen=lpXimage->Screen;
00144  if((BackBuffer=(fullscreenbuffer *)X__Malloc(Xmax*Ymax*
00145    sizeof(fullscreenbuffer))) == NULL)return 0;
00146  memset(BackBuffer,0,Xmax*Ymax*sizeof(fullscreenbuffer));
00147  if(BackBuffer != NULL){
00148    long i,j;
00149    fullscreenbuffer *S,*B;
00150    double shade,background,NdotL,spec;
00151    double a,spower,
00152           azimuth=0.7777,
00153           elevation=0.7777,
00154           Nx,Ny,Nz,Lx,Ly,Lz,Nz2,NzLz,Vx=0.0,Vy=0.0,Vz=255.9,VzNz,len;
00155    spower=(double)10;
00156    B=BackBuffer;
00157    azimuth = (double)ny*3.1415926/180.0;
00158    Lx=cos(azimuth)*cos(elevation)*255.9;
00159    Ly=sin(azimuth)*cos(elevation)*255.9;
00160    Lz=sin(elevation)*255.9;
00161    Vx += Lx; Vy += Ly; Vz += Lz;
00162    NdotL=1.0/sqrt(Vx*Vx+Vy*Vy+Vz*Vz);
00163    Vx *= NdotL; Vy *= NdotL; Vz *= NdotL;
00164    for(i=1;i<Ymax-1;i++)for(j=1;j<Xmax-1;j++){
00165      a=((double)(B->R)+(double)(B->G)+(double)(B->B))/3.0;
00166      B->R=(unsigned char)a;
00167      B->G=(unsigned char)a;
00168      B->B=(unsigned char)a;
00169      B++;
00170    }
00171    B=BackBuffer;
00172    DrawRingsInBuffer(B,Xmax,Ymax);
00173    for(i=1;i<Ymax-1;i++){
00174      for(j=1;j<Xmax-1;j++){
00175        a=(double)(B+(i-1)*Xmax+(j-1))->R+
00176          (double)(B+(i-1)*Xmax+(j  ))->R+
00177          (double)(B+(i-1)*Xmax+(j+1))->R+
00178          (double)(B+(i  )*Xmax+(j-1))->R+
00179          (double)(B+(i  )*Xmax+(j  ))->R+
00180          (double)(B+(i  )*Xmax+(j+1))->R+
00181          (double)(B+(i+1)*Xmax+(j-1))->R+
00182          (double)(B+(i+1)*Xmax+(j  ))->R+
00183          (double)(B+(i+1)*Xmax+(j+1))->R;
00184        (B+i*Xmax+j)->R=(unsigned char)(a/9.0);
00185        a=(double)(B+(i-1)*Xmax+(j-1))->G+
00186          (double)(B+(i-1)*Xmax+(j  ))->G+
00187          (double)(B+(i-1)*Xmax+(j+1))->G+
00188          (double)(B+(i  )*Xmax+(j-1))->G+
00189          (double)(B+(i  )*Xmax+(j  ))->G+
00190          (double)(B+(i  )*Xmax+(j+1))->G+
00191          (double)(B+(i+1)*Xmax+(j-1))->G+
00192          (double)(B+(i+1)*Xmax+(j  ))->G+
00193          (double)(B+(i+1)*Xmax+(j+1))->G;
00194        (B+i*Xmax+j)->G=(unsigned char)(a/9.0);
00195        a=(double)(B+(i-1)*Xmax+(j-1))->B+
00196          (double)(B+(i-1)*Xmax+(j  ))->B+
00197          (double)(B+(i-1)*Xmax+(j+1))->B+
00198          (double)(B+(i  )*Xmax+(j-1))->B+
00199          (double)(B+(i  )*Xmax+(j  ))->B+
00200          (double)(B+(i  )*Xmax+(j+1))->B+
00201          (double)(B+(i+1)*Xmax+(j-1))->B+
00202          (double)(B+(i+1)*Xmax+(j  ))->B+
00203          (double)(B+(i+1)*Xmax+(j+1))->B;
00204        (B+i*Xmax+j)->B=(unsigned char)(a/9.0);
00205      }
00206    }
00207    B=BackBuffer;
00208    nx = max(1,nx);
00209    Nz=(6.0*255.0)/((double)nx/10.0);
00210    Nz2=Nz*Nz;
00211    NzLz=Nz*Lz;
00212    VzNz=Vz*Nz;
00213    background=Lz/255.0;
00214    for(i=0;i<Ymax;i++){
00215      for(j=0;j<Xmax;j++){
00216        S=(Screen+i*Xmax+j);
00217        spec=0.0;
00218        if(i > 0 && i < Ymax-1 && j > 0 && j < Xmax-1){
00219          Ny= -(double)(B+(i-1)*Xmax+(j-1))->R
00220              -(double)(B+(i-1)*Xmax+(j  ))->R
00221              -(double)(B+(i-1)*Xmax+(j+1))->R+
00222               (double)(B+(i+1)*Xmax+(j-1))->R+
00223               (double)(B+(i+1)*Xmax+(j  ))->R+
00224               (double)(B+(i+1)*Xmax+(j+1))->R;
00225 
00226          Nx= -(double)(B+(i-1)*Xmax+(j-1))->R
00227              -(double)(B+(i  )*Xmax+(j-1))->R
00228              -(double)(B+(i+1)*Xmax+(j-1))->R+
00229               (double)(B+(i-1)*Xmax+(j+1))->R+
00230               (double)(B+(i  )*Xmax+(j+1))->R+
00231               (double)(B+(i+1)*Xmax+(j+1))->R;
00232 
00233          if(fabs(Nx) < 1.0 && fabs(Ny) < 1.0){
00234            shade=background;
00235          }
00236          else if((NdotL=Nx*Lx+Ny*Ly+NzLz) < 0.0){
00237            shade=0;
00238          }
00239          else{
00240            len=1.0/sqrt(Nx*Nx+Ny*Ny+Nz2);
00241            shade=NdotL*len/255.0;
00242            NdotL=(Vx*Nx+Vy*Ny+VzNz)*len;
00243            if(NdotL > 0)spec=pow((2*NdotL*NdotL-1.0),spower);
00244          }
00245        }
00246        else shade=background;
00247        S->R = (unsigned char)(min(255,(double)S->R*shade+spec*255.0));
00248        S->G = (unsigned char)(min(255,(double)S->G*shade+spec*255.0));
00249        S->B = (unsigned char)(min(255,(double)S->B*shade+spec*255.0));
00250      }
00251    }
00252  }
00253  if(BackBuffer != NULL)X__Free(BackBuffer);
00254  return 1;
00255 }
00256 
00257 /*************** Function that renders any of the OpenGL Functionality ************/
00258 
00259 long _RenderGLexternal(char *PrmList, XIMAGE *lpXimage){
00260 MessageBox(NULL,"OpenGL function called","OK",MB_OK);
00261  return 1;
00262 }
00263 /**********************************************************************************/
00264 
00265 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00266 
00267 // ny is angle
00268 // nx is % of slope  100 is highest 0 is lowest
00269 
00270 
00271 static long colour[3]={255,255,255};
00272 static long type=0,hold=0,back=0,lframe=9999,nx=50,ny=45;
00273 static char iname[256];
00274 static X__MEMORY_MANAGER *lpLocalEVI;
00275 
00276 char * _SetExternalParameters(
00277   char *Op,                 /* string for the parameters                  */
00278   HWND hWnd,                /* parent window                              */
00279   long ruler,               /* ruler scale value to facilitate scaling    */
00280   char *name,               /* name of DLL file with the effect           */
00281   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00282                                     ){
00283  char buffer[1024];
00284  long id1,id2;   /* integer scaled radius value */
00285  strcpy(iname," ");
00286  if(Op != NULL){  /* parameters exist so read them off the list */
00287    sscanf(Op,"%s %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %s",buffer,&version,
00288           &type,&hold,&nx,&ny,&colour[0],&colour[1],&colour[2],
00289           &back,&lframe,iname);
00290  }
00291  lpLocalEVI=lpEVI;
00292  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG_VIDEO),hWnd,
00293               (DLGPROC)DlgProc) == FALSE)return Op;
00294  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00295  sprintf(buffer,"%s %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %s",
00296            name,version,type,hold,
00297            nx,ny,colour[0],colour[1],colour[2],back,lframe,iname);
00298  if((Op=(char *)CALL_MALLOC(strlen(buffer)+1)) == NULL){
00299   MessageBox (GetFocus(),"External effect: Out of memory","Error",
00300                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00301    return NULL;
00302  }
00303  strcpy(Op,buffer);
00304  return Op;
00305 }
00306 
00307 
00308 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00309  BOOL err;
00310  int i;
00311  char lname[256],text[512];
00312  switch( msg ) {
00313    case WM_INITDIALOG:
00314      if     (hold == 0)
00315        SendDlgItemMessage(hwnd,DLG_VIDEO_FADE_IN,BM_SETCHECK,TRUE,0);
00316      else if(hold == 1)
00317        SendDlgItemMessage(hwnd,DLG_VIDEO_FADE_OUT,BM_SETCHECK,TRUE,0);
00318      else if(hold == 2)
00319        SendDlgItemMessage(hwnd,DLG_VIDEO_HOLD_IN,BM_SETCHECK,TRUE,0);
00320      else if(hold == 3)
00321        SendDlgItemMessage(hwnd,DLG_VIDEO_HOLD_OUT,BM_SETCHECK,TRUE,0);
00322      SetDlgItemInt(hwnd,DLG_VIDEO_NX,nx,TRUE);
00323      SetDlgItemInt(hwnd,DLG_VIDEO_NY,ny,TRUE);
00324      CentreDialogOnScreen(hwnd);
00325    case WM_PAINT:
00326      PaintBackground(hwnd);
00327      break;
00328      return TRUE;
00329    case WM_COMMAND:
00330      switch(LOWORD(wparam)){
00331         case IDCANCEL:
00332           EndDialog(hwnd,FALSE);
00333           return(TRUE);
00334         case IDOK:
00335           nx=GetDlgItemInt(hwnd,DLG_VIDEO_NX,&err,FALSE);
00336           ny=GetDlgItemInt(hwnd,DLG_VIDEO_NY,&err,FALSE);
00337           if(SendDlgItemMessage(hwnd,DLG_VIDEO_FADE_IN,BM_GETCHECK,0,0))
00338             hold=0;
00339           if(SendDlgItemMessage(hwnd,DLG_VIDEO_FADE_OUT,BM_GETCHECK,0,0))
00340             hold=1;
00341           if(SendDlgItemMessage(hwnd,DLG_VIDEO_HOLD_IN,BM_GETCHECK,0,0))
00342             hold=2;
00343           if(SendDlgItemMessage(hwnd,DLG_VIDEO_HOLD_OUT,BM_GETCHECK,0,0))
00344             hold=3;
00345           EndDialog(hwnd,TRUE);
00346           return(TRUE);
00347         default:
00348           break;
00349       }
00350       break;
00351     default: break;
00352  }
00353  return FALSE;
00354 }
00355 

Generated on Sun Apr 27 14:20:12 2014 for OpenFX by  doxygen 1.5.6