PLUGINS.C

Go to the documentation of this file.
00001 // file PLUGINS.C
00002 
00003 // This is the file where the MESH effects POSTPROCESSOR
00004 // and TEXTURE plugins are loaded and called.
00005 
00006 #define MODULE_FX
00007 
00008 #include "render.h"
00009 #include "..\shaders\shaders.h"
00010 #include "..\gl2.h"
00011 
00012 static void ExternalFX(long global, char *);
00013 static short AllocateAnotherShader(void);
00014 
00015 #include "..\effects\sfxinfo.h"
00016 
00017 static sfxinfo SFXinfo;
00018 static sfxdata SFXdata;
00019 static long FXObj;
00020 static short AllocateAnotherShader(void);
00021 
00022 #include "..\common\animate\wspaces.c"
00023 
00024 long MotionBlurFrame(long frame, long subframe, long command_id){
00025   long i,j;
00026   fullscreenbuffer *f,*b;
00027   double df1,df,dft[9]={0.9,0.85,0.8,0.75,0.7,0.65,0.6,0.55,0.5};
00028   if(mblurON < 2)return 0;  /* do it in a DLL */
00029 //  if(mblurON > 10)mblurON=10;
00030   f=FullScreenBuffer;
00031   b=MotionBlurBuffer;
00032   if(mblurON > 10){
00033     df1=1.0/(double)subframe;
00034     df=(double)(subframe-1);
00035     for(i=0;i<ResolutionY;i++){
00036       for(j=0;j<ResolutionX;j++){
00037         b->R = (unsigned char)(((double)(f->R)+(double)(b->R)*df)*df1);
00038         b->G = (unsigned char)(((double)(f->G)+(double)(b->G)*df)*df1);
00039         b->B = (unsigned char)(((double)(f->B)+(double)(b->B)*df)*df1);
00040 //        f->R = b->R; f->G = b->G; f->B = b->B;
00041         b++; f++;
00042       }
00043     }
00044     if(command_id == 0){
00045       memcpy(FullScreenBuffer,MotionBlurBuffer,ResolutionX*ResolutionY*(long)sizeof(fullscreenbuffer));
00046       memset(MotionBlurBuffer,0,ResolutionX*ResolutionY*(long)sizeof(fullscreenbuffer));
00047     }
00048     return 0;
00049   }
00050   if(frame == 1)df=1.0;
00051   else          df=dft[mblurON-2];
00052   df1=1.0-df;
00053   for(i=0;i<ResolutionY;i++){
00054     for(j=0;j<ResolutionX;j++){
00055       b->R = (unsigned char)((double)(f->R)*df+(double)(b->R)*df1);
00056       b->G = (unsigned char)((double)(f->G)*df+(double)(b->G)*df1);
00057       b->B = (unsigned char)((double)(f->B)*df+(double)(b->B)*df1);
00058       f->R = b->R; f->G = b->G; f->B = b->B;
00059       b++; f++;
00060     }
00061   }
00062   return 0;
00063 }
00064 
00065 void EffectObjectVertices(long global, long O){ // Apply any modifications to the vertex positions due to external motion effects
00066   double amplitude,wavelength,displacement;
00067   long i,j,n;
00068   vertex *v;
00069   double t,vmax[3],vmin[3];
00070   if((n=(long)Object[O].effect) < 4)return;
00071   if(global == 1 && n < 50)return; /* no globals for internal */
00072   for(i=0;i<3;i++){vmax[i] = (double)(BIGN); vmin[i] = (double)BIGP;}
00073   t=Object[O].effectratio;
00074   v=Object[O].Vbase;
00075   for(i=0;i<Object[O].NoVertices;i++){ /* get object limits */
00076     for(j=0;j<3;j++){
00077        if(global == 0)v[i].p[j] -= Object[O].Centre[j]; /* apply offset */
00078        vmin[j] = min(vmin[j],v[i].p[j]);
00079        vmax[j] = max(vmax[j],v[i].p[j]);
00080     }
00081   }
00082   FXObj=O;
00083   SFXinfo.version=1;
00084   SFXinfo.nvert=Object[O].NoVertices;
00085   SFXinfo.time=t;
00086   SFXinfo.ltime=(long)(t*1000);
00087   SFXinfo.vmin[0]=(long)vmin[0];   SFXinfo.vmax[0]=(long)vmax[0];
00088   SFXinfo.vmin[1]=(long)vmin[1];   SFXinfo.vmax[1]=(long)vmax[1];
00089   SFXinfo.vmin[2]=(long)vmin[2];   SFXinfo.vmax[2]=(long)vmax[2];
00090   SFXinfo.origin[0]=Object[O].Centre[0];
00091   SFXinfo.origin[1]=Object[O].Centre[1];
00092   SFXinfo.origin[2]=Object[O].Centre[2];
00093   SFXinfo.Nfaces=Object[O].NoFaces;
00094   SFXinfo.Faces=(void *)Object[O].Fbase;
00095   if(global == 1){ /* origin include in positon matrix already applied */
00096     memset(&SFXdata,0,sizeof(sfxdata));
00097     SFXdata.size=sizeof(sfxdata);
00098     for(i=0;i<3;i++){
00099       SFXdata.pos[i]=(long)Object[O].Offset[i];
00100       SFXdata.camera_pos[i]=(long)ViewPoint[i];
00101     }
00102     if(R_Nground == 1){
00103       SFXdata.ground=TRUE;
00104       for(i=0;i<3;i++)SFXdata.ground_pos[i]=(long)Ground.pin[i];
00105     }
00106   }
00107   ExternalFX(global, Object[O].effect_param_list);
00108   if(global == 1)return;
00109   for(i=0;i<Object[O].NoVertices;i++){
00110     for(j=0;j<3;j++){
00111       v[i].p[j] += Object[O].Centre[j];  /* undo offset */
00112     }
00113   }
00114 }
00115 
00116 extern vector Wave_Sources[];
00117 
00118 extern char gszHomeDir[];
00119 
00120 void init_structures(void);
00121 
00122 #include "..\textures\rstruct.h"
00123 
00124 X__SHADER EviShader;  
00126 void init_structures(void){
00127  X__SHADER *lpEVI;
00128  lpEVI=&EviShader;
00129  lpEVI->fp_sNoise=Snoise2;
00130  lpEVI->fp_sTurbulence=STurbulence;
00131  lpEVI->fp_Cycloidal=Cycloidal;
00132  lpEVI->fp_vNoise=Dnoise2;
00133  lpEVI->fp_vTurbulence=DTurbulence;
00134  lpEVI->fp_normalize=normalize;
00135  lpEVI->fp_Wave_Sources=Wave_Sources;
00136  lpEVI->fp_UseSingleShaderProgram=UseSingleShaderProgram;
00137  lpEVI->fp_LoadAndCompileShader=LoadAndCompileShader;
00138  lpEVI->fp_UnloadCompiledShader=UnloadCompiledShader;
00139  lpEVI->fp_SetUniformInteger=SetUniformInteger;
00140  lpEVI->fp_SetUniformVariable=SetUniformVariable;
00141  lpEVI->fp_SetUniformVector=SetUniformVector;
00142  lpEVI->fp_SetUniformVector2=SetUniformVector2;
00143  lpEVI->fp_SetUniformMatrix3=SetUniformMatrix3;
00144  lpEVI->fp_SetUniformMatrix4=SetUniformMatrix4;
00145  lpEVI->fp_SetAttributeInteger=SetAttributeInteger;
00146  lpEVI->fp_SetAttributeVariable=SetAttributeVariable;
00147  lpEVI->fp_SetAttributeVector=SetAttributeVector;
00148  lpEVI->fp_SetAttributeVectorID=SetAttributeVectorID;
00149  lpEVI->fp_BindAttributeVector=BindAttributeVector;
00150  lpEVI->fp_GetAttibuteLocation=GetAttibuteLocation;
00151  lpEVI->version=2;
00152  return;
00153 }
00154 
00155 static void ExternalFX(long global, char *parameters){
00156  // these old effects are identified by a number  
00157  static char *FxDLLnameList[]={"wave.dll","modulate.dll","squash.dll","warp.dll","spring.dll","shake.dll"};
00158  char fname[256],tparam[256];
00159  HMODULE hLib;
00160  FARPROC fpFun;
00161  long l,result=0;
00162  long (*fpFun1)(char *, sfxinfo *, vertex *);
00163  long (*fpFun2)(char *, sfxinfo *, sfxdata *, vertex *);
00164  sscanf(parameters,"%s",tparam);
00165  // tparam will hold the name of external dll or a number in range 4-9
00166  // if <  4 then this effect is no-longer supported so silently ignore.
00167  if(tparam[0] > 48 && tparam[0] < 58){ // this is a numeric ID 
00168    l=tparam[0] - 48;
00169    if(l < 4 || l > 9)return; // No effect applied
00170    l -= 4;
00171    strcpy(fname,gszHomeDir);  strcat(fname,"effects\\");
00172    strcat(fname,FxDLLnameList[l]);
00173    //if(debug != NULL)fprintf(debug,"Using internal effect [%s]\n",fname);
00174  }
00175  else {
00176    RestoreWhiteSpaces(tparam);
00177    sscanf(tparam,"%[^\0]",fname);
00178    //if(debug != NULL)fprintf(debug,"Using external effect [%s]\n",fname);
00179  }
00180  l=strlen(fname);
00181  if((hLib=AddLoadedLibrary(fname)) == NULL){ /* try to get 'dll' */
00182    hLib=AddLoadedLibrary(fname);
00183  }
00184  if(hLib == NULL){ /* try the default effect directory */
00185    char *sfname,tfname[256];
00186    sfname=short_form(fname);
00187    strcpy(tfname,gszHomeDir);  strcat(tfname,"effects\\");
00188    strcat(tfname,sfname);
00189    hLib=AddLoadedLibrary(tfname);
00190  }
00191  if(hLib != NULL){
00192    if(global == 0){
00193      if((fpFun=GetProcAddress(hLib,"_RenderExternalEffect")) != NULL){
00194        fpFun1 = (void *)fpFun;
00195        result=(*fpFun1)(parameters,&SFXinfo,Object[FXObj].Vbase);
00196      }
00197      else MessageBox(GetFocus(),(LPCTSTR)"Function Fail in DLL",NULL,MB_OK);
00198    }
00199    else{
00200      if((fpFun=GetProcAddress(hLib,"_RenderGlobalEffect")) != NULL){
00201        fpFun2 = (void *)fpFun;
00202        result=(*fpFun2)(parameters,&SFXinfo,&SFXdata,Object[FXObj].Vbase);
00203      }
00204    }
00205  }
00206  else MessageBox(GetFocus(),(LPCTSTR) "Load fail on DLL",NULL, MB_OK);
00207 }
00208 
00209 short LoadExternalTexture(char *fname, char *parameters){
00210  HMODULE hLib;
00211  FARPROC fpFun;
00212  long result=0;
00213  long GLshaderID = -1;
00214  long (*fpFun1)(long, long, char *, X__SHADER *);
00215  void (*fpFun2)(X__SHADER *);
00216  long (*fpTexFun1)(long,
00217                    vector, vector,
00218                    double,double,double,
00219                    double,
00220                    double,double,
00221                    double *,
00222                    unsigned char [3],
00223                    double *, double *);
00224  long (*fpTexFunGL)();
00225  fpTexFun1=NULL;
00226  fpTexFunGL=NULL;
00227  hLib=AddLoadedLibrary(fname);
00228  if(hLib == NULL){ /* try the default textures directory */
00229    char *sfname,tfname[256];
00230    sfname=short_form(fname);
00231    strcpy(tfname,gszHomeDir);  strcat(tfname,"textures\\");
00232    strcat(tfname,sfname);
00233    hLib=AddLoadedLibrary(tfname);
00234  }
00235  if(hLib != NULL){
00236    if((fpFun=GetProcAddress(hLib,"_ExternalTextureStartup")) != NULL){
00237      fpFun1 = (void *)fpFun;
00238      if(bRenderOpenGL)SetUpCorrectContext(TRUE);
00239      GLshaderID=(*fpFun1)(RenderCurrentFrame,0,parameters,&EviShader);
00240      if(bRenderOpenGL)SetUpCorrectContext(FALSE);
00241    }
00242    if((fpFun=GetProcAddress(hLib,"_ExternalTextureClose"))
00243      != NULL)fpFun2= (void *)fpFun;
00244    else      fpFun2=NULL;
00245    if((fpFun=GetProcAddress(hLib,"_ExternalTextureProcedure")) != NULL){
00246      fpTexFun1 = (void *)fpFun;
00247    }
00248    else{
00249      MessageBox(GetFocus(),"Failed Texture Function",NULL, MB_OK);
00250      return FAIL;
00251    }
00252    if((fpFun=GetProcAddress(hLib,"_ExternalTextureProcedureGL")) != NULL){ // Get address of OpenGL texture
00253      fpTexFunGL = (void *)fpFun;
00254    }
00255  }
00256  else {
00257    MessageBox(GetFocus(),"Failed Texture Load",NULL,MB_OK);
00258     return FAIL;
00259  }
00260  if(AllocateAnotherShader() == FAIL){
00261    return FAIL;
00262  }
00263  strcpy(Shader[Nshader].file,fname);
00264  strncpy(Shader[Nshader].parameters,parameters,128);
00265  Shader[Nshader].module     = (void *)hLib;
00266  Shader[Nshader].function   = (void *)fpTexFun1;
00267  Shader[Nshader].functionGL = (void *)fpTexFunGL;
00268  Shader[Nshader].shaderID   = GLshaderID;
00269  Shader[Nshader].close      = (void *)fpFun2;
00270  Shader[Nshader].morph      = NULL;  /* to be used later */
00271  //if(debug != NULL)fprintf(debug,"Loaded shader %ld [%s} with params [%s]\n",Nshader,fname,parameters);
00272  // if OpenGL renderer call the OpenGL setup function to load shaders etc!
00273  Nshader++;
00274  return OK;
00275 }
00276 
00277 static short AllocateAnotherShader(void){
00278  shader *sp;
00279  if(Shader == NULL)sp=(shader *)X__Malloc(sizeof(shader));
00280  else              sp=(shader *)X__Realloc(Shader,sizeof(shader)*(Nshader+1));
00281  if(sp == NULL){
00282    Render_Message(13,0,NULL);
00283    return FAIL;
00284  }
00285  Shader=sp;
00286  return OK;
00287 }
00288 
00289 void FreeExternalTextures(void){
00290  HMODULE hLibTex;
00291  void (*fpFun2)(X__SHADER *);
00292  long i;
00293  if(Nshader > 0){
00294    for(i=0;i<Nshader;i++){
00295      if(Shader[i].close != NULL){
00296        EviShader.GLshaderID=Shader[i].shaderID;
00297        fpFun2 = (void *)Shader[i].close;
00298        if(bRenderOpenGL)SetUpCorrectContext(TRUE);
00299        (*fpFun2)(&EviShader);
00300        if(bRenderOpenGL)SetUpCorrectContext(FALSE);
00301      }
00302      hLibTex=(HMODULE)Shader[i].module;
00303    }
00304  }
00305  X__Free(Shader);
00306  //if(debug != NULL)fprintf(debug,"Shader list freed\n");
00307  Shader=NULL;
00308 }
00309 
00310 BOOL RenderExternalTexture(long tid,     
00311                         vector p,     
00312                         vector n,     
00313                         double alpha, double beta, double gamma, 
00314                         double bump_scale,  
00315                         double *ac,   
00316                         double *transparency,  
00317                         unsigned char sc[3], 
00318                         long obj, 
00319                         face *fp, 
00320                         double colour[3]){  
00321  long (*fpTexFun1)(long, // function pointer prototype
00322                    vector, vector,
00323                    double,double,double,
00324                    double,
00325                    double,double,
00326                    double *,
00327                    unsigned char [3],
00328                    double [3],
00329                    double *, double *,
00330                    X__SHADER *);
00331  long result;
00332  double reflectivity;
00333  double unity=1.0;
00334  vector zerov={0.0,0.0,0.0};
00335  vector unitvx={1.0,0.0,0.0};
00336  vector unitvy={0.0,1.0,0.0};
00337  vector unitvz={0.0,0.0,1.0};
00338  /*  x and y are same as alpha beta */
00339  if(tid < 0 || tid >= Nshader)return FALSE;
00340  fpTexFun1=(void *)(Shader[tid].function);  // assign the function pointer to the correct function for the chosen texture
00341  EviShader.fp_axis_o=&zerov;
00342  EviShader.fp_axis_u=&unitvx;
00343  EviShader.fp_axis_v=&unitvy;
00344  EviShader.fp_axis_n=&unitvz;
00345  EviShader.u_length=&unity;
00346  EviShader.v_length=&unity;
00347  EviShader.fp_axis_unorm=&unitvx;
00348  EviShader.fp_axis_vnorm=&unitvy;
00349  EviShader.fp_FullSat=FALSE;  // use full saturation 
00350  result=(*fpTexFun1)(   // CALL ExternalTextureProcedure() in plugin
00351                      1, // always use map coordinates
00352                      p,
00353                      n, // surface normal vector (might be modifed in shader if it is a 'bump' shader
00354                      alpha,beta,gamma, // 3D texture/map coordinates
00355                      bump_scale,
00356                      alpha,beta, // 2D map coordinates
00357                      ac,    // alpha channel
00358                      sc,      
00359                      colour,  // Returned - surfcae colour
00360                      &reflectivity, // retuned but NOT used 
00361                      transparency,  // returned 
00362                      &EviShader);  // carry in extra parameters that might be needed by the shader
00363  return EviShader.fp_FullSat;
00364 }
00365 
00366 short MorphExternalTexture(char *name, char *parameters, long obj){
00367  HMODULE hLibTex;
00368  FARPROC fpFun;
00369  long (*fpFun1)(char *, double);
00370  long n,result;
00371  if((n=Nshader-1) < 0)return FAIL; /* assumes called IMMEDIATELY after load */
00372  if((hLibTex=(HMODULE)Shader[n].module) != NULL){
00373    if((fpFun=GetProcAddress(hLibTex,"_ExternalTextureMorph")) != NULL){
00374      fpFun1 = (void *)fpFun;
00375      result=(*fpFun1)(parameters,Object[obj].morphratio);
00376    }
00377    else return FAIL;
00378  }
00379  else return FAIL;
00380  return OK;
00381 }
00382 
00383 
00384 #include "..\common\postprocess\ximage.h"
00385 
00386 long ExternalImageProcess(long frame, long HWrender){
00387  int i,j,k;
00388  char fname[256],tparam[256];
00389  long AnimatorIdList[256];
00390  HMODULE hLib;
00391  FARPROC fpFun;
00392  XIMAGE Ximage,*lpXimage;
00393  long l,result=0;
00394  long (*fpFun1)(char *, XIMAGE *);
00395  if(nImageProcesses == 0 || ImageProcesses == NULL)return 0;
00396  if(FullScreenBuffer == NULL){
00397    MessageBox(GetFocus(),
00398               "Image Processing not possible without full screen buffer",
00399               NULL,MB_OK);
00400    return FAIL;
00401  }
00402  Ximage.version=3;
00403  Ximage.HWrender=HWrender;
00404  Ximage.Frame=frame;
00405  Ximage.Xscale=scalex;
00406  Ximage.Yscale=scaley;
00407  if(anti_alias == LOW)   {Ximage.Xscale /= 2;}
00408  if(anti_alias == MEDIUM){Ximage.Xscale /= 2; Ximage.Yscale /= 2;}
00409  if(anti_alias == HIGH)  {Ximage.Xscale /= 3; Ximage.Yscale /= 2;}
00410  if(anti_alias == ULTRA) {Ximage.Xscale /= 3; Ximage.Yscale /= 3;}
00411  Ximage.Xmax=ResolutionX;
00412  Ximage.Ymax=ResolutionY;
00413  Ximage.Screen=FullScreenBuffer;
00414  Ximage.Blur=MotionBlurBuffer;
00415  Ximage.ObjectBuffer=ObjectIdBuffer;
00416  Ximage.Zbuffer=fszBuffer;
00417  Ximage.Nlights=Nlights;
00418  Ximage.Lights=Lights;
00419  Ximage.Ntargets=0;
00420  Ximage.Targets=NULL;
00421  Ximage.shadow_buffer_size=shadow_buffer_size;
00422  for(k=0;k<min(250,ObjectCount);k++)AnimatorIdList[k]=Object[k].AnimatorID;
00423  Ximage.AnimatorIdList=AnimatorIdList;
00424  Ximage.ViewPoint = (double *)ViewPoint;
00425  Ximage.CamPhi=CamPhi;
00426  Ximage.CamTheta=CamTheta;
00427  Ximage.CamAlpha=CamAlpha;
00428  Ximage.CamSx=CamSx;
00429  Ximage.CamSy=CamSy;
00430  Ximage.CamSz=CamSz;
00431  Ximage.CamIma=CamIma;
00432  Ximage.CamIm=CamIm;
00433  for(k=0;k<5;k++)Ximage.Pad[k]=0;
00434  Ximage.fp_Terminate=R_terminator;
00435  Ximage.fp_Yield=RenderYield;
00436  Ximage.fp_sNoise=Snoise2;
00437  Ximage.fp_sTurbulence=STurbulence;
00438  Ximage.fp_Cycloidal=Cycloidal;
00439  Ximage.fp_vNoise=Dnoise2;
00440  Ximage.fp_vTurbulence=DTurbulence;
00441  Ximage.fp_normalize=normalize;
00442  lpXimage=&Ximage;
00443  for(k=0;k<nImageProcesses;k++){
00444    lpXimage->Morph=ImageProcesses[k].imo;
00445    lpXimage->MorphRatio=ImageProcesses[k].fmr;
00446    lpXimage->first_frame=ImageProcesses[k].first_frame;
00447    lpXimage->this_frame=ImageProcesses[k].this_frame;
00448    lpXimage->last_frame=ImageProcesses[k].last_frame;
00449    lpXimage->aParameters=ImageProcesses[k].parameters;
00450    lpXimage->mParameters=ImageProcesses[k].mparameters;
00451    sscanf(ImageProcesses[k].parameters,"%s",tparam);
00452    RestoreWhiteSpaces(tparam);
00453    sscanf(tparam,"%[^\0]",fname);
00454    Render_Message(98,0,fname);
00455 //MessageBox(NULL,fname,"Original Name",MB_OK);
00456    hLib=AddLoadedLibrary(fname);
00457    lpXimage->ModuleName=fname;
00458    if(hLib == NULL){ /* try the default ximage directory */
00459      char *sfname,tfname[256];
00460      sfname=short_form(fname);
00461      strcpy(tfname,gszHomeDir);  strcat(tfname,"postprocess\\");
00462      strcat(tfname,sfname);
00463 //MessageBox(NULL,fname,"Relpacement name",MB_OK);
00464      hLib=AddLoadedLibrary(tfname);
00465      lpXimage->ModuleName=tfname;
00466    }
00467    if(hLib != NULL){
00468      if((fpFun=GetProcAddress(hLib,"_RenderImageProcess")) != NULL){
00469        for(i=0;i<4;i++)for(j=0;j<4;j++)Ximage.ViewTransform[i][j]=ViewTransform[i][j];
00470        fpFun1 = (void *)fpFun;
00471        result=(*fpFun1)(ImageProcesses[k].parameters,lpXimage);
00472      }
00473      else MessageBox(GetFocus(),
00474         (LPCTSTR) "Failed Function", NULL, MB_OK | MB_SYSTEMMODAL );
00475      //FreeLibrary(hLib);
00476    }
00477    else MessageBox(GetFocus(),(LPCTSTR) "Failed Load", NULL, MB_OK | MB_SYSTEMMODAL );
00478    if(!result)return FAIL;
00479  }
00480  return 0;
00481 }

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