v_dots.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 /* V_DOTS.C   External texture that creates 3 dimensional dots */
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 #ifndef PI
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; /* use to pick up resources from DLL   */
00030 
00031 /**************  definitions needed ********************************/
00032 /* Very important to make sure that float is used for real variables       */
00033 /* NOTE!!!!!! The modeler uses DOUBLES as they are supposed to be used     */
00034 /*            when setting up the parametes it is VITAL that this is       */
00035 /*            taken into account !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!      */
00036 
00037 #include "..\animate\memory.h" /* for memory definition */
00038 #include "..\animate\memdef.h" /* local names           */
00039 #include "defines.h"    /* define names for callback functions          */
00040 #include "rstruct.h"    /* define struct with callback functions etc.   */
00041 #include "v_dots.h"     /* shader specific header file                  */
00042 
00043 /************************** DLL entry point ********************************/
00044 
00045 #if __WATCOMC__
00046 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00047 #else
00048 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00049 #endif
00050   HANDLE ghMod;
00051   switch (dwReason) {
00052     case DLL_PROCESS_ATTACH:
00053 //MessageBox(NULL,"Attaching","Debug",MB_OK);
00054 #if __X__MIPS__
00055       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00056 #endif
00057       hDLLinstance = hDLL;  /* handle to DLL file */
00058       break;
00059     case DLL_PROCESS_DETACH:
00060 #if __X__MIPS__
00061       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00062 #endif
00063 //MessageBox(NULL,"Detaching","Debug",MB_OK);
00064       break;
00065   }
00066   return (int)TRUE;
00067 }
00068 
00069 #if __SC__
00070 #pragma startaddress(DllMain)
00071 #endif
00072 
00073 /*******************************************************************\
00074 |                Code that define the textures                       |
00075 \*******************************************************************/
00076 
00077 #define VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00078 
00079 /* set default values for any parameters in case list is lost */
00080 
00081 static double ground_range=0.4,
00082               dots_xscale=1.0,dots_yscale=1.0,dots_zscale=1.0;
00083 static long tr_type;
00084 
00085 long _ExternalTextureStartup(
00086   long frame,  /* identify the frame in the animation                     */
00087   long nframes,/* number of frames in the animation                       */
00088   char *parameter_list, X__SHADER *lpEVI
00089 ){
00090  if(parameter_list[0] != '#'){
00091    MessageBox ( GetFocus(),
00092                 (LPCTSTR) "External texture: Startup",
00093                 (LPCTSTR) "Parameter list missing",
00094                 MB_OK | MB_SYSTEMMODAL );
00095  }
00096  else { /* use parameters as required */
00097    parameter_list++; /* skip over the idetifying # symbol and read any
00098                         parameters that may be required for this shader
00099                      */
00100    sscanf(parameter_list,"%f %f %f %f %ld",&ground_range,&dots_xscale,
00101           &dots_yscale,&dots_zscale,&tr_type);
00102  }
00103 return LoadAndCompileShader("v_dots");
00104 }
00105 
00106 long _ExternalTextureMorph(char *parameter_list, double mr){
00107 /*  mr is 0.0 -> 1.0 morph ratio - if object is being morphed           */
00108 /*  this function is called to allow internal parameters to be adjusted */
00109 /*  so that they may change over the duration of the morph              */
00110  double ground_range1;
00111  parameter_list++; /* skip over the idetifying # symbol and read any
00112                       parameters that may be required for this shader
00113                    */
00114  sscanf(parameter_list,"%f",&ground_range1);
00115  ground_range = ground_range1+(ground_range - ground_range1)*mr;
00116  return 1;
00117 }
00118 
00119 #define DLTA 0.05
00120 
00121 long _ExternalTextureProcedure(
00122   long coord_type, /* 0 if alpha/beta/gamma must be used 1 if map-coords available */
00123   vector p,    /* point in World co-ordinates of the suface point to be mapped */
00124   vector n,    /* normal to surface at point to be mapped                  */
00125   double alpha,/* co-ordinates relative to the texture axes  0.0 -> 1.0 is */
00126   double beta, /* inside the parallipiped formed from the texture axes     */
00127   double gamma,
00128   double bump, /* parameter relative to height set for bumps               */
00129   double map_x,/* surface mapping co-ordinates over the face to which the  */
00130   double map_y,/* texture is to be applied                                 */
00131   double *alpha_channel, /* pointer to texture alpha channel        */
00132   unsigned char sc[3], /* original texture/secondary colour as set by user */
00133   double colour[3],    /* return the RGB value in floating point  0-255    */
00134                        /* On calling, the colour contains the colour that  */
00135                        /* has been calculated by any previous shader or    */
00136                        /* the original color                               */
00137   double *reflectivity, /* not used at present  */
00138   double *transparency, /* not used at present  */
00139   X__SHADER *lpEVI     /* pointer to structure */
00140 ){
00141  double r;
00142  alpha /= dots_xscale;
00143  beta  /= dots_yscale;
00144  gamma /= dots_zscale;
00145  alpha=(alpha-floor(alpha))-0.5;
00146  beta=(beta-floor(beta))-0.5;
00147  gamma=(gamma-floor(gamma))-0.5;
00148  if((r=sqrt(beta*beta+alpha*alpha+gamma*gamma)) < ground_range){ // dot
00149    VECCOPY((double)sc,colour)
00150    if     (tr_type == 1)*transparency=1.0;
00151    else if(tr_type == 2)*transparency=0.0;
00152  }
00153  else if(r < ground_range+DLTA){
00154    r= (r-ground_range)/DLTA;
00155    colour[0] = colour[0]*r + (double)sc[0]*(1.0-r);
00156    colour[1] = colour[1]*r + (double)sc[1]*(1.0-r);
00157    colour[2] = colour[2]*r + (double)sc[2]*(1.0-r);
00158    if     (tr_type == 1)*transparency=r;
00159    else if(tr_type == 2)*transparency=(1.0-r);
00160  }
00161  else{ /* background */
00162    if     (tr_type == 1)*transparency=0.0;
00163    else if(tr_type == 2)*transparency=1.0;
00164  }
00165  return 1;
00166 }
00167 
00168 void _ExternalTextureClose(X__SHADER *lpEVI){
00169  UnloadCompiledShader(tGLshaderID);
00170 }
00171 
00172 long _ExternalTextureProcedureGL(
00173   double bump_scale,
00174   unsigned char sc[3],
00175   unsigned char ac[3],
00176   X__SHADER *lpEVI
00177 ){
00178  // Set any shader specific uniform variables here.
00179  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00180  SetUniformVector(tGLshaderID,"ScalingV",1.0/dots_xscale,1.0/dots_zscale,1.0/dots_yscale);
00181  SetUniformInteger(tGLshaderID,"Ttype",tr_type);
00182  SetUniformVariable(tGLshaderID,"GroundRange",ground_range);
00183  // Callback to Render the Polygons - this allows for multiple passes - through same shaders
00184  // possibly with different parameters - including vertex offset - blending depth enabling 
00185  // to allow for multipass textures e.g. hair and fur (shell model) it's arguments must not be altered!!!
00186  DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00187 
00188  return 1;
00189 }
00191 
00192 /************************** Local Utility Functions ***********************/
00193 
00194 void CentreDialogOnScreen(HWND hwnd){
00195  RECT rcDlg;
00196  long Xres,Yres;
00197  Yres=GetSystemMetrics(SM_CYSCREEN);
00198  Xres=GetSystemMetrics(SM_CXSCREEN);
00199  GetWindowRect(hwnd,&rcDlg);
00200  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00201  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00202  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00203  return;
00204 }
00205 
00206 /********************** Functions used to set up shader *******************/
00207 
00208 /* Dialog box callback  */
00209 BOOL CALLBACK DotsDlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00210 
00211 /* Set defaults for the parameter variables, declare them as static so that
00212    they are available to the dialog function. They are here only for the
00213    function that edits the parameter list and must not be used in the
00214    code that is called from the renderer to execute the shader             */
00215 
00216 /************ Functions called by the modeler to set up shader *************/
00217 
00218 #if 0
00219 char * CALLBACK SetExternalParameters(
00220 #else
00221 char * _SetExternalParameters(
00222 #endif
00223   char *Op,                 /* string for the parameters                  */
00224   HWND hWnd,                /* parent window                              */
00225   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00226   ){
00227  char szbuf[128],*Op1;
00228  if(Op != NULL){  /* parameters exist so read them off the list */
00229    Op1=Op;
00230    Op1++; /* skip over the idetifying # symbol and read any
00231              parameters that may be required for this shader
00232            */
00233    sscanf(Op1,"%f %f %f %f %ld",&ground_range,&dots_xscale,&dots_yscale,
00234           &dots_zscale,&tr_type);
00235  }
00236  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG_DOTS),hWnd,
00237               (DLGPROC)DotsDlgProc) == FALSE)return Op;
00238  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00239  sprintf(szbuf,"%c %.2f %.2f %.2f %.2f %ld",'#',ground_range,dots_xscale,
00240          dots_yscale,dots_zscale,tr_type);
00241  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00242   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00243                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00244    return NULL;
00245  }
00246  strcpy(Op,szbuf);
00247  return Op;
00248 }
00249 
00250 BOOL CALLBACK DotsDlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00251  char str[16];
00252  switch( msg ) {
00253    case WM_INITDIALOG:
00254      sprintf(str,"%.2f",ground_range);
00255      SendDlgItemMessage(hwnd,DLG_DOTS_RADIUS,WM_SETTEXT,0,(LPARAM)str);
00256      SendDlgItemMessage(hwnd,DLG_DOTS_RADIUS,EM_LIMITTEXT,(WPARAM)12,0);
00257      sprintf(str,"%.2f",dots_xscale);
00258      SendDlgItemMessage(hwnd,DLG_DOTS_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00259      SendDlgItemMessage(hwnd,DLG_DOTS_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00260      sprintf(str,"%.2f",dots_yscale);
00261      SendDlgItemMessage(hwnd,DLG_DOTS_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00262      SendDlgItemMessage(hwnd,DLG_DOTS_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00263      sprintf(str,"%.2f",dots_zscale);
00264      SendDlgItemMessage(hwnd,DLG_DOTS_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00265      SendDlgItemMessage(hwnd,DLG_DOTS_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00266      if     (tr_type == 0)
00267         SendDlgItemMessage(hwnd,DLG_DOTS_TRANSALL,BM_SETCHECK,TRUE,0);
00268      else if(tr_type == 1)
00269         SendDlgItemMessage(hwnd,DLG_DOTS_TRANSB,BM_SETCHECK,TRUE,0);
00270      else if(tr_type == 2)
00271         SendDlgItemMessage(hwnd,DLG_DOTS_TRANSM,BM_SETCHECK,TRUE,0);
00272      CentreDialogOnScreen(hwnd);
00273      return TRUE;
00274    case WM_COMMAND:
00275      switch(LOWORD(wparam)){
00276         case IDCANCEL:
00277         case DLG_DOTS_CANCEL:
00278           EndDialog(hwnd,FALSE);
00279           return(TRUE);
00280         case IDOK:
00281         case DLG_DOTS_OK:
00282           if(GetDlgItemText(hwnd,DLG_DOTS_RADIUS,str,10) == 0)
00283              EndDialog(hwnd,FALSE);
00284           if((ground_range=atof(str)) == 0)EndDialog(hwnd,FALSE);
00285           if(GetDlgItemText(hwnd,DLG_DOTS_XSCALE,str,10) == 0)
00286              EndDialog(hwnd,FALSE);
00287           if((dots_xscale=atof(str)) == 0)EndDialog(hwnd,FALSE);
00288           if(GetDlgItemText(hwnd,DLG_DOTS_YSCALE,str,10) == 0)
00289              EndDialog(hwnd,FALSE);
00290           if((dots_yscale=atof(str)) == 0)EndDialog(hwnd,FALSE);
00291           if(GetDlgItemText(hwnd,DLG_DOTS_ZSCALE,str,10) == 0)
00292              EndDialog(hwnd,FALSE);
00293           if((dots_zscale=atof(str)) == 0)EndDialog(hwnd,FALSE);
00294           if     (SendDlgItemMessage(hwnd,DLG_DOTS_TRANSALL,BM_GETCHECK,0,0))
00295             tr_type=0;
00296           if     (SendDlgItemMessage(hwnd,DLG_DOTS_TRANSB,BM_GETCHECK,0,0))
00297             tr_type=1;
00298           if     (SendDlgItemMessage(hwnd,DLG_DOTS_TRANSM,BM_GETCHECK,0,0))
00299             tr_type=2;
00300           EndDialog(hwnd,TRUE);
00301           return(TRUE);
00302         default:
00303           break;
00304       }
00305       break;
00306     default: break;
00307  }
00308  return FALSE;
00309 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3