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 "stucco1.h"
00036
00037 #define VECCOPY(a,b) { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; }
00038 #define VECSUM(a,b,c) { c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; c[2]=a[2]+b[2];}
00039 #define VECSUB(a,b,c) { c[0]=a[0]-b[0]; c[1]=a[1]-b[1]; c[2]=a[2]-b[2];}
00040 #define VECSCALE(a,b,c) { c[0]=(a)*b[0]; c[1]=(a)*b[1]; c[2]=(a)*b[2];}
00041 #define DOT(a,b) ( (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2]) )
00042
00043 #define IMOD(z,a) ((z) - ((z)/(a))*(a))
00044 #define PATTERN(x,y,z) ( ((z)+16384L)*64536L + \
00045 ((y)+16384L)*32768L + \
00046 ((x)+16384L))
00047 #define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
00048 #define FMOD(x,a) ((x) >= 0.0 ? fmod(x,a) :(a - fmod(-(x),a)))
00049
00050 #if __WATCOMC__
00051 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00052 #else
00053 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00054 #endif
00055 HANDLE ghMod;
00056 switch (dwReason) {
00057 case DLL_PROCESS_ATTACH:
00058 #if __X__MIPS__
00059 if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00060 #endif
00061 hDLLinstance = hDLL;
00062 break;
00063 case DLL_PROCESS_DETACH:
00064 #if __X__MIPS__
00065 if(!_CRT_INIT(hDLL,dwReason,lpReserved))return(int)FALSE;
00066 #endif
00067 break;
00068 }
00069 return (int)TRUE;
00070 }
00071
00072 #if __SC__
00073 #pragma startaddress(DllMain)
00074 #endif
00075
00076 static long type=0,bumpy=0;
00077 static double x=1.0,y=1.0,z=1.0;
00078
00079 #if 0
00080 long CALLBACK ExternalTextureStartup(
00081 #else
00082 long _ExternalTextureStartup(
00083 #endif
00084 long frame,long nframes,char *parameter_list, X__SHADER *lpEVI){
00085 if(parameter_list[0] != '#'){
00086 MessageBox ( GetFocus(),
00087 (LPCTSTR) "External texture: Startup",
00088 (LPCTSTR) "Parameter list missing",
00089 MB_OK | MB_SYSTEMMODAL );
00090 }
00091 else {
00092 parameter_list++;
00093 sscanf(parameter_list,"%f %f %f %ld %ld",
00094 &x,&y,&z,&type,&bumpy);
00095 }
00096 return LoadAndCompileShader("stucco1");
00097 }
00098
00099 #if 0
00100 long CALLBACK ExternalTextureMorph(
00101 #else
00102 long _ExternalTextureMorph(
00103 #endif
00104 char *parameter_list, double mr){
00105 double x_m,y_m,z_m;
00106 parameter_list++;
00107 sscanf(parameter_list,"%f %f %f",
00108 &x_m,&y_m,&z_m);
00109 x=x_m+(x-x_m)*mr;
00110 y=y_m+(y-y_m)*mr;
00111 z=z_m+(z-z_m)*mr;
00112 return 1;
00113 }
00114
00115 #if 0
00116 long CALLBACK ExternalTextureProcedure(
00117 #else
00118 long _ExternalTextureProcedure(
00119 #endif
00120 long coord_type, vector p, vector n,
00121 double alpha, double beta, double gamma,
00122 double bump, double map_x, double map_y,
00123 double *alpha_channel, unsigned char sc[3], double colour[3],
00124 double *reflectivity, double *transparency,
00125 X__SHADER *lpEVI
00126 ){
00127
00128 vector v,vt;
00129 double t=0.48,dt=0.075,s=0.5,turb=0.4;
00130 double xx,yy,zz,noise;
00131 double xr,xl,yr,yl,zr,zl;
00132 xx=10.0*alpha / x;
00133 yy=10.0*beta / y;
00134 zz=10.0*gamma / z;
00135 if(bumpy == 1){
00136 vNoise(v,xx*2.5,yy*2.5,zz*2.5);
00137 VECSUM(0.5*v,n,n)
00138 normalize(n);
00139 }
00140 if(type == 1){
00141 vTurbulence(v, xx, yy, zz);
00142 xx += v[0] * turb;
00143 yy += v[1] * turb;
00144 zz += v[2] * turb;
00145 }
00146 nNoise(xx, yy, zz, &noise);
00147 if(noise > t && noise < t+3*dt){
00148 nNoise(xx+0.01, yy, zz, &xr);
00149 nNoise(xx-0.01, yy, zz, &xl);
00150 nNoise(xx, yy+0.01,zz, &yr);
00151 nNoise(xx, yy-0.01,zz, &yl);
00152 nNoise(xx, yy,zz+0.01, &zr);
00153 nNoise(xx, yy,zz-0.01, &zl);
00154 v[0]= -(xr-xl); v[1]= -(yr-yl); v[2]= -(zr-zl);
00155 normalize(v);
00156 VECSCALE(v[0],tAxis_unorm,vt)
00157 VECSUM(v[1]*tAxis_vnorm,vt,vt)
00158 VECSUM(-v[2]*tAxis_n,vt,vt)
00159 normalize(vt);
00160 if(noise < t+dt){
00161 s=(noise-t)/dt;
00162 s=-s*s;
00163 }
00164 else if(noise < t+2*dt)s=0.0;
00165 else{
00166 s=1.0-(noise-(t+2*dt))/dt;
00167 s = s*s;
00168 }
00169 VECSUM(1.4*s*vt,n,n)
00170 normalize(n);
00171 }
00172 return 1;
00173 }
00174
00175
00176 void _ExternalTextureClose(X__SHADER *lpEVI){
00177 UnloadCompiledShader(tGLshaderID);
00178 }
00179
00180 long _ExternalTextureProcedureGL(
00181 double bump_scale,
00182 unsigned char sc[3],
00183 unsigned char ac[3],
00184 X__SHADER *lpEVI
00185 ){
00186 SetUniformVector(tGLshaderID,"ScalingV",10.0/x,10.0/y,10.0/z);
00187 DrawShadedPolygons(tmatpass,tpass,tprogID,tattrloc,tNface,tMainFp,tNvert,tMainVp);
00188 return 1;
00189 }
00191
00192
00193 void CentreDialogOnScreen(HWND hwnd){
00194 RECT rcDlg;
00195 long Xres,Yres;
00196 Yres=GetSystemMetrics(SM_CYSCREEN);
00197 Xres=GetSystemMetrics(SM_CXSCREEN);
00198 GetWindowRect(hwnd,&rcDlg);
00199 OffsetRect(&rcDlg,-rcDlg.left,-rcDlg.top);
00200 OffsetRect(&rcDlg,(Xres-rcDlg.right)/2,(Yres-rcDlg.bottom)/2);
00201 SetWindowPos(hwnd,HWND_TOP,rcDlg.left,rcDlg.top,0,0,SWP_NOSIZE);
00202 return;
00203 }
00204
00205 static void SetColour(double *colour, HWND parent){
00206 CHOOSECOLOR cc;
00207 static COLORREF CustColours[16]={
00208 RGB(255,255,255), RGB(239,239,239), RGB(223,223,223), RGB(207,207,207),
00209 RGB(191,191,191), RGB(175,175,175), RGB(159,159,159), RGB(143,143,143),
00210 RGB(127,127,127), RGB(111,111,111), RGB( 95, 95, 95), RGB( 79, 79, 79),
00211 RGB( 63, 63, 63), RGB( 47, 47, 47), RGB( 31, 31, 31), RGB( 15, 15, 15) };
00212 cc.lStructSize=sizeof(CHOOSECOLOR);
00213 cc.hwndOwner=parent;
00214 cc.rgbResult=RGB((BYTE)colour[0],(BYTE)colour[1],(BYTE)colour[2]);
00215 cc.lpCustColors=(LPDWORD)CustColours;
00216 cc.Flags= CC_RGBINIT;
00217 cc.lCustData=(DWORD)0;
00218 if(ChooseColor(&cc)){
00219 colour[0]=(double)GetRValue(cc.rgbResult);
00220 colour[1]=(double)GetGValue(cc.rgbResult);
00221 colour[2]=(double)GetBValue(cc.rgbResult);
00222 }
00223 }
00224
00225 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
00226
00227 #if 0
00228 char * CALLBACK SetExternalParameters(
00229 #else
00230 char * _SetExternalParameters(
00231 #endif
00232 char *Op,
00233 HWND hWnd,
00234 X__MEMORY_MANAGER *lpEVI
00235 ){
00236 char szbuf[255],*Op1;
00237 if(Op != NULL){
00238 Op1=Op;
00239 Op1++;
00240 sscanf(Op1,"%f %f %f %ld %ld",
00241 &x,&y,&z,&type,&bumpy);
00242 }
00243 if(DialogBox(hDLLinstance,MAKEINTRESOURCE(DLG),hWnd,
00244 (DLGPROC)DlgProc) == FALSE)return Op;
00245 if(Op != NULL)CALL_FREE(Op);
00246 sprintf(szbuf,"# %.3f %.3f %.3f %ld %ld",
00247 x,y,z,type,bumpy);
00248 if((Op=(char *)CALL_MALLOC(strlen(szbuf)+1)) == NULL){
00249 MessageBox (GetFocus(),"External shader: Out of memory","Error",
00250 MB_OK|MB_TASKMODAL|MB_ICONSTOP);
00251 return NULL;
00252 }
00253 strcpy(Op,szbuf);
00254 return Op;
00255 }
00256
00257 BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
00258 char str[16];
00259 switch( msg ) {
00260 case WM_INITDIALOG:
00261 sprintf(str,"%.2f",x);
00262 SendDlgItemMessage(hwnd,DLG_XSCALE,WM_SETTEXT,0,(LPARAM)str);
00263 SendDlgItemMessage(hwnd,DLG_XSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00264 sprintf(str,"%.2f",y);
00265 SendDlgItemMessage(hwnd,DLG_YSCALE,WM_SETTEXT,0,(LPARAM)str);
00266 SendDlgItemMessage(hwnd,DLG_YSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00267 sprintf(str,"%.2f",z);
00268 SendDlgItemMessage(hwnd,DLG_ZSCALE,WM_SETTEXT,0,(LPARAM)str);
00269 SendDlgItemMessage(hwnd,DLG_ZSCALE,EM_LIMITTEXT,(WPARAM)12,0);
00270 if(bumpy == 1)
00271 SendDlgItemMessage(hwnd,DLG_BUMPY,BM_SETCHECK,TRUE,0);
00272 if(type == 1)
00273 SendDlgItemMessage(hwnd,DLG_TYPE,BM_SETCHECK,TRUE,0);
00274 CentreDialogOnScreen(hwnd);
00275 return TRUE;
00276 case WM_COMMAND:
00277 switch(LOWORD(wparam)){
00278 case IDCANCEL:
00279 EndDialog(hwnd,FALSE);
00280 return(TRUE);
00281 case IDOK:
00282 if(GetDlgItemText(hwnd,DLG_XSCALE,str,10) == 0)
00283 EndDialog(hwnd,FALSE);
00284 if((x=atof(str)) == 0)EndDialog(hwnd,FALSE);
00285 if(GetDlgItemText(hwnd,DLG_YSCALE,str,10) == 0)
00286 EndDialog(hwnd,FALSE);
00287 if((y=atof(str)) == 0)EndDialog(hwnd,FALSE);
00288 if(GetDlgItemText(hwnd,DLG_ZSCALE,str,10) == 0)
00289 EndDialog(hwnd,FALSE);
00290 if((z=atof(str)) == 0)EndDialog(hwnd,FALSE);
00291 if(SendDlgItemMessage(hwnd,DLG_TYPE,BM_GETCHECK,0,0))type=1;
00292 else type =0;
00293 if(SendDlgItemMessage(hwnd,DLG_BUMPY,BM_GETCHECK,0,0))bumpy=1;
00294 else bumpy =0;
00295 EndDialog(hwnd,TRUE);
00296 return(TRUE);
00297 default:
00298 break;
00299 }
00300 break;
00301 default: break;
00302 }
00303 return FALSE;
00304 }