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