00001
00002
00003
00004
00005
00006
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"
00032 #include "..\animate\memdef.h"
00033 #include "defines.h"
00034 #include "rstruct.h"
00035 #include "regdmpl1.h"
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;
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 static int colour_flag=0;
00065 static int type=0;
00066 static double size=0.5,depth=1.0;
00067 static double c1[3]={255.0,255.0,255.0};
00068 static double x=1.0,y=1.0,z=1.0;
00069
00070 #if 0
00071 long CALLBACK ExternalTextureStartup(
00072 #else
00073 long _ExternalTextureStartup(
00074 #endif
00075 long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00076 if(parameter_list[0] != '#'){
00077 MessageBox ( GetFocus(),
00078 (LPCTSTR) "External texture: Startup",
00079 (LPCTSTR) "Parameter list missing",
00080 MB_OK | MB_SYSTEMMODAL );
00081 }
00082 else {
00083 parameter_list++;
00084 sscanf(parameter_list,"%f %f %f %f %f %f %f %f %ld %ld",
00085 &x,&y,&z,&size,&depth,
00086 &c1[0],&c1[1],&c1[2],&type,&colour_flag);
00087 }
00088 return LoadAndCompileShader("regdmpl1");
00089 }
00090
00091 #if 0
00092 long CALLBACK ExternalTextureMorph(
00093 #else
00094 long _ExternalTextureMorph(
00095 #endif
00096 char *parameter_list, double mr){
00097 int i;
00098 double size_m,depth_m;
00099 double c1_m[3],c2_m[3],c3_m[3];
00100 double x_m,y_m,z_m;
00101 parameter_list++;
00102 sscanf(parameter_list,"%f %f %f %f %f %f %f %f ",
00103 &x_m,&y_m,&z_m,&size_m,&depth_m,
00104 &c1_m[0],&c1_m[1],&c1_m[2]);
00105 for(i=0;i<3;i++)c1[i]=c1_m[i]+(c1[i]-c1_m[i])*mr;
00106 size=size_m+(size-size_m)*mr;
00107 depth=depth_m+(depth-depth_m)*mr;
00108 x=x_m+(x-x_m)*mr;
00109 y=y_m+(y-y_m)*mr;
00110 z=z_m+(z-z_m)*mr;
00111 return 1;
00112 }
00113
00114 #define VECCOPY(a,b) { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00115 #define VECSUM(a,b,c) { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00116 #define VECSUB(a,b,c) { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00117 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00118 #define DOT(a,b) ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00119
00120 double hermite(double t, double tmin, double tmax){
00121 t=(t-tmin)/(tmax-tmin);
00122 return (t - t*t);
00123 }
00124
00125 double sigmoid(double t, double tmin, double tmax){
00126 t=3.1415926*(2.0*(t-tmin)/(tmax-tmin));
00127 return 0.5*(1.0-cos(t));
00128 }
00129
00130
00131 #if 0
00132 long CALLBACK ExternalTextureProcedure(
00133 #else
00134 long _ExternalTextureProcedure(
00135 #endif
00136 long coord_type, vector p, vector n,
00137 double alpha, double beta, double gamma,
00138 double bump, double map_x, double map_y,
00139 double *alpha_channel, unsigned char sc[3], double colour[3],
00140 double *reflectivity, double *transparency,
00141 X__SHADER *lpEVI
00142 ){
00143 double xx,yy,zz;
00144 double a;
00145 vector u,v,pp;
00146 double rad;
00147 xx=alpha / x;
00148 yy=beta / y;
00149 zz=gamma / z;
00150 rad=size*sqrt(tAxis_u[0]*tAxis_u[0]+
00151 tAxis_u[1]*tAxis_u[1]+
00152 tAxis_u[2]*tAxis_u[2]);
00153 VECCOPY(-tAxis_n,u)
00154 VECSCALE(rad,u,u)
00155 VECSUM(tAxis_o,0.5*tAxis_u,pp)
00156 VECSUM(pp,0.5*tAxis_v,pp)
00157 VECSUM(pp,u,pp)
00158 xx=(xx-floor(xx));
00159 yy=(yy-floor(yy));
00160 zz=(zz-floor(zz));
00161 VECCOPY(tAxis_o,v)
00162 VECSUM(v,xx*tAxis_u,v)
00163 VECSUM(v,yy*tAxis_v,v)
00164 VECSUM(v,2.0*zz*u,v)
00165 VECSUB(v,pp,u)
00166 if((a=sqrt((u[0])*(u[0])+
00167 (u[1])*(u[1])+
00168 (u[2])*(u[2]))) < rad){
00169 double mu,moddn,rmu;
00170 vector c,dn;
00171 mu=DOT(u,n);
00172 c[0]=pp[0]+mu*n[0];
00173 c[1]=pp[1]+mu*n[1];
00174 c[2]=pp[2]+mu*n[2];
00175 VECSUB(v,c,dn)
00176 moddn=sqrt(dn[0]*dn[0]+dn[1]*dn[1]+dn[2]*dn[2]);
00177 rmu=sqrt(rad*rad-mu*mu);
00178 if(moddn < rmu){
00179 a=1.0-moddn/rmu;
00180 normalize(dn);
00181 VECSCALE(-1.0,dn,dn);
00182 if(type == 0)a=sin(a*1.57079);
00183 else if(type == 1)a=0.5;
00184 else{
00185 if(a < 0.5){
00186
00187 a=1.0-sigmoid(a,0.0,0.5);
00188 }
00189 else a=1;
00190 }
00191 a=1.0-(1.0-a)*depth;
00192 VECSUM((1.0-a)*dn,(a)*n,n)
00193 normalize(n);
00194 if(colour_flag)VECCOPY(c1,colour)
00195 }
00196 }
00197 return 1;
00198 }
00199
00200 void _ExternalTextureClose(X__SHADER *lpEVI){
00201 UnloadCompiledShader(tGLshaderID);
00202 }
00203
00204 long _ExternalTextureProcedureGL(
00205 double bump_scale,
00206 unsigned char sc[3],
00207 unsigned char ac[3],
00208 X__SHADER *lpEVI
00209 ){
00210
00211
00212 SetUniformVector(tGLshaderID,"MaterialC",(GLfloat)ac[0]/255.0,(GLfloat)ac[1]/255.0,(GLfloat)ac[2]/255.0);
00213 if(colour_flag)SetUniformInteger(tGLshaderID,"ColourType",1);
00214 else SetUniformInteger(tGLshaderID,"ColourType",0);
00215 SetUniformInteger(tGLshaderID,"ShaderID",type);
00216 SetUniformVector(tGLshaderID,"ScalingV",1.0/x,1.0/z,1.0/y);
00217 SetUniformVariable(tGLshaderID,"BumpDepth",depth);
00218 SetUniformVariable(tGLshaderID,"BumpSize",size);
00219
00220
00221
00222 DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00223 return 1;
00224 }
00226
00227 void CentreDialogOnScreen(HWND hwnd){
00228 RECT rcDlg;
00229 long Xres,Yres;
00230 Yres=GetSystemMetrics(SM_CYSCREEN);
00231 Xres=GetSystemMetrics(SM_CXSCREEN);
00232 GetWindowRect(hwnd,&rcDlg);
00233 OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00234 OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00235 SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00236 return;
00237 }
00238
00239 static void SetColour(double *colour, HWND parent){
00240 CHOOSECOLOR cc;
00241 static COLORREF CustColours[16]={
00242 RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00243 RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00244 RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00245 RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00246 cc.lStructSize=sizeof(CHOOSECOLOR);
00247 cc.hwndOwner=parent;
00248 cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00249 cc.lpCustColors=(LPDWORD)CustColours;
00250 cc.Flags= CC_RGBINIT;
00251 cc.lCustData=(DWORD)0;
00252 if(ChooseColor(&cc)){
00253 colour[0]=(double)GetRValue(cc.rgbResult);
00254 colour[1]=(double)GetGValue(cc.rgbResult);
00255 colour[2]=(double)GetBValue(cc.rgbResult);
00256 }
00257 }
00258
00259 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00260
00261 #if 0
00262 char * CALLBACK SetExternalParameters(
00263 #else
00264 char * _SetExternalParameters(
00265 #endif
00266 char *Op,
00267 HWND hWnd,
00268 X__MEMORY_MANAGER *lpEVI
00269 ){
00270 char szbuf[255],*Op1;
00271 if(Op != NULL){
00272 Op1=Op;
00273 Op1++;
00274 sscanf(Op1,"%f %f %f %f %f %f %f %f %ld %ld",
00275 &x,&y,&z,&size,&depth,
00276 &c1[0],&c1[1],&c1[2],&type,&colour_flag);
00277 }
00278 if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00279 (DLGPROC)DlgProc) == FALSE)return Op;
00280 if(Op != NULL)CALL_FREE(Op);
00281 sprintf(szbuf,"# %.2f %.2f %.2f %.3f %.2f "
00282 "%.2f %.2f %.2f %ld %ld",
00283 x,y,z,size,depth,
00284 c1[0],c1[1],c1[2],type,colour_flag);
00285 if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00286 MessageBox (GetFocus(),"External shader: Out of memory","Error",
00287 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00288 return NULL;
00289 }
00290 strcpy(Op,szbuf);
00291 return Op;
00292 }
00293
00294 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00295 char str[16];
00296 switch( msg ) {
00297 case WM_INITDIALOG:
00298 sprintf(str,"%.2f",x);
00299 SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00300 SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00301 sprintf(str,"%.2f",y);
00302 SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00303 SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00304 sprintf(str,"%.2f",z);
00305 SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00306 SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00307 sprintf(str,"%.2f",size);
00308 SendDlgItemMessage(hwnd,DLG_SIZE,WM_SETTEXT,0,(LPARAM)str);
00309 SendDlgItemMessage(hwnd,DLG_SIZE,EM_LIMITTEXT,(WPARAM)12,0);
00310 sprintf(str,"%.2f",depth);
00311 SendDlgItemMessage(hwnd,DLG_DEPTH,WM_SETTEXT,0,(LPARAM)str);
00312 SendDlgItemMessage(hwnd,DLG_DEPTH,EM_LIMITTEXT,(WPARAM)12,0);
00313 if(colour_flag)
00314 SendDlgItemMessage(hwnd,DLG_COLOURFLAG,BM_SETCHECK,TRUE,0);
00315 if(type == 0)
00316 SendDlgItemMessage(hwnd,DLG_TYPE1,BM_SETCHECK,TRUE,0);
00317 else if(type == 1)
00318 SendDlgItemMessage(hwnd,DLG_TYPE2,BM_SETCHECK,TRUE,0);
00319 else if(type == 2)
00320 SendDlgItemMessage(hwnd,DLG_TYPE3,BM_SETCHECK,TRUE,0);
00321 CentreDialogOnScreen(hwnd);
00322 return TRUE;
00323 case WM_DRAWITEM:{
00324 LPDRAWITEMSTRUCT lpdis;
00325 HBRUSH hbr,hbrold;
00326 BYTE r,g,b;
00327 lpdis=(LPDRAWITEMSTRUCT)lparam;
00328 if(lpdis->CtlID == DLG_COLOUR){
00329 if(lpdis->CtlID == DLG_COLOUR){
00330 r=(BYTE)c1[0]; g=(BYTE)c1[1]; b=(BYTE)c1[2];
00331 }
00332 if(lpdis->itemState & ODS_SELECTED)
00333 InvertRect(lpdis->hDC,&(lpdis->rcItem));
00334 else{
00335 hbr=CreateSolidBrush(RGB(r,g,b));
00336 hbrold=SelectObject(lpdis->hDC,hbr);
00337 Rectangle(lpdis->hDC,lpdis->rcItem.left,lpdis->rcItem.top,
00338 lpdis->rcItem.right,lpdis->rcItem.bottom);
00339 SelectObject(lpdis->hDC,hbrold);
00340 DeleteObject(hbr);
00341 }
00342 }
00343 }
00344 break;
00345 case WM_COMMAND:
00346 switch(LOWORD(wparam)){
00347 case DLG_COLOUR:
00348 SetColour(c1,hwnd);
00349 InvalidateRect(GetDlgItem(hwnd,DLG_COLOUR),NULL,FALSE);
00350 break;
00351 case IDCANCEL:
00352 EndDialog(hwnd,FALSE);
00353 return(TRUE);
00354 case IDOK:
00355 if(GetDlgItemText(hwnd,DLG_SIZE,str,10) == 0)
00356 EndDialog(hwnd,FALSE);
00357 if((size=atof(str)) == 0)EndDialog(hwnd,FALSE);
00358 if(GetDlgItemText(hwnd,DLG_DEPTH,str,10) == 0)
00359 EndDialog(hwnd,FALSE);
00360 if((depth=atof(str)) == 0)EndDialog(hwnd,FALSE);
00361 if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00362 EndDialog(hwnd,FALSE);
00363 if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00364 if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00365 EndDialog(hwnd,FALSE);
00366 if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00367 if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00368 EndDialog(hwnd,FALSE);
00369 if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00370 if(SendDlgItemMessage(hwnd,DLG_COLOURFLAG,BM_GETCHECK,0,0))
00371 colour_flag=1; else colour_flag=0;
00372 if(SendDlgItemMessage(hwnd,DLG_TYPE1,BM_GETCHECK,0,0))type=0;
00373 else if(SendDlgItemMessage(hwnd,DLG_TYPE2,BM_GETCHECK,0,0))type=1;
00374 else if(SendDlgItemMessage(hwnd,DLG_TYPE3,BM_GETCHECK,0,0))type=2;
00375 EndDialog(hwnd,TRUE);
00376 return(TRUE);
00377 default:
00378 break;
00379 }
00380 break;
00381 default: break;
00382 }
00383 return FALSE;
00384 }