v_brick.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_BRICK.DLL     External texture for 3D BRICKS */
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;
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 "v_brick.h"    /* shader specific header file */
00036 
00037 
00038 #if __WATCOMC__
00039 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00040 #else
00041 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00042 #endif
00043   HANDLE ghMod;
00044   switch (dwReason) {
00045     case DLL_PROCESS_ATTACH:
00046 #if __X__MIPS__
00047       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00048 #endif
00049       hDLLinstance = hDLL;  /* handle to DLL file */
00050       break;
00051     case DLL_PROCESS_DETACH:
00052 #if __X__MIPS__
00053       if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00054 #endif
00055       break;
00056   }
00057   return (int)TRUE;
00058 }
00059 
00060 #if __SC__
00061 #pragma startaddress(DllMain)
00062 #endif
00063 
00064 #define VECCOPY(a,b)    { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00065 
00066 /* set default values for any parameters in case list is lost */
00067 
00068 static double ground_range=0.1,
00069               dots_xscale=1.0,dots_yscale=1.0,dots_zscale=1.0;
00070 static long tr_type=0;
00071 
00072 
00073 #if 0
00074 long CALLBACK ExternalTextureStartup(
00075 #else
00076 long _ExternalTextureStartup(
00077 #endif
00078   long frame,  /* identify the frame in the animation                     */
00079   long nframes,/* number of frames in the animation                       */
00080   char *parameter_list, X__SHADER *lpEVI){
00081  if(parameter_list[0] != '#'){
00082    MessageBox ( GetFocus(),
00083                 (LPCTSTR) "External texture: Startup",
00084                 (LPCTSTR) "Parameter list missing",
00085                 MB_OK | MB_SYSTEMMODAL );
00086  }
00087  else { /* use parameters as required */
00088    parameter_list++; /* skip over the idetifying # symbol and read any
00089                         parameters that may be required for this shader
00090                      */
00091    sscanf(parameter_list,"%f %f %f %f %ld",&ground_range,&dots_xscale,
00092           &dots_yscale,&dots_zscale,&tr_type);
00093  }
00094  return LoadAndCompileShader("v_brick");
00095 }
00096 
00097 #if 0
00098 long CALLBACK ExternalTextureMorph(char *parameter_list, double mr){
00099 #else
00100 long _ExternalTextureMorph(char *parameter_list, double mr){
00101 #endif
00102 /*  mr is 0.0 -> 1.0 morph ratio - if object is being morphed           */
00103 /*  this function is called to allow internal parameters to be adjusted */
00104 /*  so that they may change over the duration of the morph              */
00105  double ground_range1,gsx,gsy,gsz;
00106  parameter_list++; /* skip over the idetifying # symbol and read any
00107                       parameters that may be required for this shader
00108                    */
00109  sscanf(parameter_list,"%f %f %f %f",&ground_range1,&gsx,&gsy,&gsz);
00110  ground_range = ground_range1+(ground_range - ground_range1)*mr;
00111  dots_xscale=gsx+(dots_xscale-gsx)*mr;
00112  dots_yscale=gsy+(dots_yscale-gsy)*mr;
00113  dots_zscale=gsz+(dots_zscale-gsz)*mr;
00114  return 1;
00115 }
00116 
00117 #if 0
00118 long CALLBACK ExternalTextureProcedure(
00119 #else
00120 long _ExternalTextureProcedure(
00121 #endif
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
00140 ){
00141  alpha /= dots_xscale;
00142  beta  /= dots_yscale;
00143  gamma /= dots_zscale;
00144  if((long)gamma % 2){
00145    alpha += 0.5; beta += 0.5;
00146  }
00147  alpha=(alpha-floor(alpha));
00148  beta=(beta-floor(beta));
00149  gamma=(gamma-floor(gamma));
00150  if(gamma > 1.0-ground_range ||
00151          (beta > 1.0-ground_range ||
00152     (beta > 0.5-ground_range && beta < 0.5)) ||
00153     (beta < 0.5 && ( (alpha > 0.5-ground_range && alpha < 0.5) ||
00154                       alpha > 1.0-ground_range) ) ||
00155     (beta > 0.5 && ((alpha > 0.3-ground_range && alpha < 0.3) ||
00156     (alpha > 0.7 && alpha < 0.7+ground_range)))){
00157    colour[0]=(double)sc[0];
00158    colour[1]=(double)sc[1];
00159    colour[2]=(double)sc[2];
00160    if     (tr_type == 1)*transparency=0.0;
00161    else if(tr_type == 2)*transparency=1.0;
00162  }
00163  else{
00164    if     (tr_type == 1)*transparency=1.0;
00165    else if(tr_type == 2)*transparency=0.0;
00166  }
00167  return 1;
00168 }
00169 
00170 void _ExternalTextureClose(X__SHADER *lpEVI){
00171  UnloadCompiledShader(tGLshaderID);
00172 }
00173 
00174 long _ExternalTextureProcedureGL(
00175   double bump_scale,
00176   unsigned char sc[3],
00177   unsigned char ac[3],
00178   X__SHADER *lpEVI
00179 ){
00180  // Set any shader specific uniform variables here.
00181  SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00182  SetUniformVector(tGLshaderID,"ScalingV",1.0/dots_xscale,1.0/dots_zscale,1.0/dots_yscale);
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  return 1;
00188 }
00190 
00191 
00192 void CentreDialogOnScreen(HWND hwnd){
00193  RECT rcDlg;
00194  long Xres,Yres;
00195  Yres=GetSystemMetrics(SM_CYSCREEN);
00196  Xres=GetSystemMetrics(SM_CXSCREEN);
00197  GetWindowRect(hwnd,&rcDlg);
00198  OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00199  OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00200  if(rcDlg.left < 0)OffsetRect(&rcDlg,-rcDlg.left,0);
00201  if(rcDlg.top  < 0)OffsetRect(&rcDlg,0,-rcDlg.top);
00202  if(rcDlg.right  > Xres)OffsetRect(&rcDlg,Xres-rcDlg.right,0);
00203  if(rcDlg.bottom > Yres)OffsetRect(&rcDlg,Yres-rcDlg.bottom,0);
00204  SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00205  return;
00206 }
00207 
00208 BOOL CALLBACK DotsDlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00209 
00210 #if 0
00211 char * CALLBACK SetExternalParameters(
00212 #else
00213 char * _SetExternalParameters(
00214 #endif
00215   char *Op,                 /* string for the parameters                  */
00216   HWND hWnd,                /* parent window                              */
00217   X__MEMORY_MANAGER *lpEVI /* pointer to structure with memory functions */
00218                              ){
00219 /* output name and buffer should be as long as necessary to hold full string */
00220  char szbuf[128],*Op1;
00221  if(Op != NULL){  /* parameters exist so read them off the list */
00222    Op1=Op;
00223    Op1++; /* skip over the idetifying # symbol and read any
00224              parameters that may be required for this shader
00225            */
00226    sscanf(Op1,"%f %f %f %f %ld",&ground_range,&dots_xscale,&dots_yscale,
00227           &dots_zscale,&tr_type);
00228  }
00229  if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG_DOTS),hWnd,
00230               (DLGPROC)DotsDlgProc) == FALSE)return Op;
00231  if(Op != NULL)CALL_FREE(Op);  /* free the old string */
00232  sprintf(szbuf,"%c %.2f %.2f %.2f %.2f %ld",'#',ground_range,dots_xscale,
00233          dots_yscale,dots_zscale,tr_type);
00234 /* print the parameters into the buffer  DON'T FORGET THE ~ END OF LIST MARK */
00235 /* Prepare the output buffer to take copy of parameter list */
00236  if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00237   MessageBox (GetFocus(),"External shader: Out of memory","Error",
00238                 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00239    return NULL;
00240  }
00241  /* Copy the parameter string to the output buffer */
00242  strcpy(Op,szbuf);
00243 // MessageBox (GetFocus(),Op,"Dots!",MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00244  return Op;
00245 }
00246 
00247 BOOL CALLBACK DotsDlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00248  char str[16];
00249  switch( msg ) {
00250    case WM_INITDIALOG:
00251      sprintf(str,"%.2f",ground_range);
00252      SendDlgItemMessage(hwnd,DLG_DOTS_RADIUS,WM_SETTEXT,0,(LPARAM)str);
00253      SendDlgItemMessage(hwnd,DLG_DOTS_RADIUS,EM_LIMITTEXT,(WPARAM)12,0);
00254      sprintf(str,"%.2f",dots_xscale);
00255      SendDlgItemMessage(hwnd,DLG_DOTS_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00256      SendDlgItemMessage(hwnd,DLG_DOTS_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00257      sprintf(str,"%.2f",dots_yscale);
00258      SendDlgItemMessage(hwnd,DLG_DOTS_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00259      SendDlgItemMessage(hwnd,DLG_DOTS_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00260      sprintf(str,"%.2f",dots_zscale);
00261      SendDlgItemMessage(hwnd,DLG_DOTS_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00262      SendDlgItemMessage(hwnd,DLG_DOTS_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00263      if     (tr_type == 0)
00264         SendDlgItemMessage(hwnd,DLG_BRICK_TRANSALL,BM_SETCHECK,TRUE,0);
00265      else if(tr_type == 1)
00266         SendDlgItemMessage(hwnd,DLG_BRICK_TRANSB,BM_SETCHECK,TRUE,0);
00267      else if(tr_type == 2)
00268         SendDlgItemMessage(hwnd,DLG_BRICK_TRANSM,BM_SETCHECK,TRUE,0);
00269      CentreDialogOnScreen(hwnd);
00270      return TRUE;
00271    case WM_COMMAND:
00272      switch(LOWORD(wparam)){
00273         case IDCANCEL:
00274         case DLG_DOTS_CANCEL:
00275           EndDialog(hwnd,FALSE);
00276           return(TRUE);
00277         case IDOK:
00278         case DLG_DOTS_OK:
00279           if(GetDlgItemText(hwnd,DLG_DOTS_RADIUS,str,10) == 0)
00280              EndDialog(hwnd,FALSE);
00281           if((ground_range=atof(str)) == 0)EndDialog(hwnd,FALSE);
00282           if(GetDlgItemText(hwnd,DLG_DOTS_XSCALE,str,10) == 0)
00283              EndDialog(hwnd,FALSE);
00284           if((dots_xscale=atof(str)) == 0)EndDialog(hwnd,FALSE);
00285           if(GetDlgItemText(hwnd,DLG_DOTS_YSCALE,str,10) == 0)
00286              EndDialog(hwnd,FALSE);
00287           if((dots_yscale=atof(str)) == 0)EndDialog(hwnd,FALSE);
00288           if(GetDlgItemText(hwnd,DLG_DOTS_ZSCALE,str,10) == 0)
00289              EndDialog(hwnd,FALSE);
00290           if((dots_zscale=atof(str)) == 0)EndDialog(hwnd,FALSE);
00291           if     (SendDlgItemMessage(hwnd,DLG_BRICK_TRANSALL,BM_GETCHECK,0,0))
00292             tr_type=0;
00293           if     (SendDlgItemMessage(hwnd,DLG_BRICK_TRANSB,BM_GETCHECK,0,0))
00294             tr_type=1;
00295           if     (SendDlgItemMessage(hwnd,DLG_BRICK_TRANSM,BM_GETCHECK,0,0))
00296             tr_type=2;
00297           EndDialog(hwnd,TRUE);
00298           return(TRUE);
00299         default:
00300           break;
00301       }
00302       break;
00303     default: break;
00304  }
00305  return FALSE;
00306 }
Generated on Tue Jan 28 06:18:32 2014 for OpenFX by  doxygen 1.6.3