/* wave.c cl /AL wave.c imga getfile or cl /AL wave.c vimage3 graphlib reads a wave front formatted file and makes a binary file all numbers are floats verticies normal x1,y1,z1,x2,y2,z2,x3,y3,z3, i1,j1,k1,i2,j2,k2 'wave.lit' 1.0 1.0 1.0 sun location 1.00 sun brite -1.0 -1.0 -1.0 moon location 0.00 moon brite 0.05 backlite 64 32 16 background color CONTROL FILE: input = [filename] the type must be '.bwv' but is not needed rot_x -90 pushes top of image back 90 degrees rot_y 90 pushes right side of image back 90 degrees rot_z 25 rotates image 25 degrees counter clockwise move_x 0.25 moves image to right 0.25 inches move_y 0.25 moves image up 0.25 inches move_z 0.25 moves image out of screen right 0.25 inches set_size 50 sets image scale to 50 pixels/inch scale_size 0.95 reduces images scale by 5% set_view 2500 sets viewing point 2500 pixels from screen (2500/50 = 50 inches) scale_view 1.05 moves view point out 5% vertical 2.0 doubles the z axis output = [filename] name of '.3d' file plot makes the 'filename.3d' image file mesh makes a mesh for checking file PSEUDO COLOR FILE: 'mont_y.pc': (projection is only needed for spherical earth) 0.0 = mid_z projection = spherical_earth 49.644 scale km/in 50.00 last vertical exaggeration 0.50 shadow brightness (0.0 to 1.0) min max r g b 1.000 1.500 255 128 0 0.500 1.000 255 255 0 0.000 0.500 0 255 0 -1.000 0.000 128 255 255 -2.000 -1.000 128 255 255 -3.000 -2.000 106 191 255 -4.000 -3.000 85 128 255 -5.000 -4.000 64 64 255 -6.000 -5.000 128 0 255 CONTOUR COLOR FILE: 'test.ctr' -2.756676392 = mid_z (km) 3.016983 scale km/in 10.00 last vertical exaggeration height(km) r g b 0.000 255 255 255 0.250 0 0 0 0.500 0 0 0 0.750 0 0 0 1.000 255 255 255 1.250 0 0 0 1.500 0 0 0 1.750 0 0 0 2.000 255 255 255 2.250 0 0 0 2.500 0 0 0 */ #include #include #include #include "nomouse.h" #include "font.h" #define MAX_PC 128 #define MAX_CT 190 #define VGA 1 #define MAX_T 55000 #define DEG_RAD 0.01745329251 /* converts degrees to radians */ #define TEMP_SIZE (long)5 char Auto[100],AutoName[100]; int ScreenXs=640,ScreenYs=480; struct Color { unsigned char r, g, b; }Lut[256]; int Xpart=450; /* color3d part of screen X dimension */ int D1,D2; int Color3D=0; int PseudoColor=0,PCnum=0,PCnumval=0,Contour=0,CTnum=0; float PCmid,PCscale,PCvert,PCmin[MAX_PC],PCmax[MAX_PC],PCshadow; float CTmid,CTscale,CTvert,CTval[MAX_CT]; FILE *FpPC; struct Color PCcolor[MAX_PC],CTcolor[MAX_CT]; VideoType='X'; struct board { int row,col,color; }B_Id; unsigned char Buffer[1000]; unsigned char Cbuff[1000][4]; float huge Dist[MAX_T]; unsigned huge Order[MAX_T]; unsigned char Color; long Max_T=MAX_T; float Vert=1.0; float T[4][4]= { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.02 }; float K=2500.0; /* K/Scale = inches from screen */ float Scale=50.0; /* screen_height (inches) / 480 (pixels) */ unsigned char TgaHdr[18]= {0,0,2,0,0,0,0,0,0,0,0,0,0x84,0x3,0xe0,0x1,0x20,0}; int Normal,PlotBack=0; FILE *Fp3d; char String3d[100]; long load_v(FILE *fpwvf); long load_f(FILE *fp,FILE *fpout,long numv); long count_v(FILE *fpwvf); long count_f(FILE *fpwvf); int mat_x_mat(float a[4][4],float b[4][4],float c[4][4]); int mat_x_vec(float a[4][4],float b[4],float c[4]); int rot_y(float deg,float t[4][4]); int rot_x(float deg,float t[4][4]); int rot_z(float deg,float t[4][4]); int tran_y(float dist,float t[4][4]); int tran_x(float dist,float t[4][4]); int tran_z(float dist,float t[4][4]); long plot_data(FILE *fpin); float dot(float a[3],float b[3]); long render_data(FILE *fpin,float xview); sort2(long n,float huge ra[],unsigned huge ib[]); int plot_tri(int hue,int x1,int y1,int x2,int y2,int x3,int y3,int val); void set_lut(char type,int iop); int cross(float a[3],float b[3], float c[3]); int normal(float dat[][3]); int plot_status(float deldist,float delang); float mid_z(float dat[5][3]); int contour_find(float h,int *fx,int *fy,float dat[5][3],int *ix,int *iy); main(int argc,char *argv[]) { int i,j,k; char string[100],cr=0xd,ans,name[100],ans1,stringn[50]; FILE *fpwvf,*fpbwv; long numv,numf,numplot; float delang=30,deldist=2.0; D1=(ScreenXs-Xpart)/2; D2=ScreenXs-D1; for(i=1;i1) { if(sscanf(argv[1],"%f",&Scale)==1&&Scale>0.0) { T[3][3]=1.0/Scale; printf("Scale reset to %f\n",T[3][3]); } } if(Auto[0]!=0) { auto3d(Auto); exit(0); } do { printf(" p -- plot figure\n"); printf(" 3 -- 3d (red/blue)\n"); printf(" C -- Color 3D '.tga'\n"); printf(" r -- render figure\n"); printf(" h -- help\n"); printf("\n x -- exit program\n"); ans=getch(); if(ans=='h') { printf("\n\n\n\nCommand line operation of 'wave'\n\n"); printf( "\n\nwave /pc <---------------------------- Pseudo Color for option 'r'\n"); printf( "wave /ct <---------------------------- Contour + Render option 'r'\n"); printf( "wave /cto <--------------------------- Contour Only option 'r'\n\n"); printf( "wave /auto [filename.ctl] <------- Scripts rendered images\n"); printf( "wave /auto [filename.ctl] /pc <---- Scripts rendered pseudo color images\n"); printf( "wave /auto3d [filename.ctl] <----- Scripts red/blue 3d images\n"); printf( "wave /auto3dc [filename.ctl] <---- Scripts half tone stereo images\n"); printf( "wave /auto3d [filename.ctl] /pc <-- Scripts pseudo color stereo images\n"); printf(" HIT ANY KEY TO CONTINUE\n");getch(); printf("\n\n\n\n\nCONTROL OF LIGHTING AND BACKGROUND COLOR\n\n"); printf("To control the location and brightness of lighting and the color\n"); printf("of the background make a file named 'wave.lit' and put it in the\n"); printf("directory where the program is running.\n\n"); printf("'wave.lit'\n\n"); printf(" 1.0 1.0 1.0 sun location\n"); printf(" 1.00 sun brite\n"); printf("-1.0 -1.0 -1.0 moon location\n"); printf(" 0.00 moon brite\n"); printf("0.05 backlite\n"); printf("64 32 16 background color\n\n"); printf(" HIT ANY KEY TO CONTINUE\n");getch(); printf("\n\n\n\n\n"); printf("SCRIPTING A SET OF IMAGES FOR BATCH MODE RUNNING:\n\n"); printf("CONTROL FILE:\n\n"); printf( "input = [filename] the type must be '.bwv' but is not needed\n"); printf( "rot_x -90 pushes top of image back 90 degrees\n"); printf( "rot_y 90 pushes right side of image back 90 degrees\n"); printf( "rot_z 25 rotates image 25 degrees counter clockwise\n"); printf( "move_x 0.25 moves image to right 0.25 inches\n"); printf( "move_y 0.25 moves image up 0.25 inches\n"); printf( "move_z 0.25 moves image out of screen right 0.25 inches\n"); printf( "set_size 50 sets image scale to 50 pixels/inch\n"); printf( "scale_size 0.95 reduces images scale by 5%\n"); printf( "set_view 2500 sets viewing point 2500 pixels from screen \n"); printf( " (2500/50 = 50 inches)\n"); printf( "scale_view 1.05 moves view point out 5%\n"); printf( "vertical 2.0 doubles the z axis\n"); printf( "output = [filename] name of '.3d' file \n"); printf( "plot makes the 'filename.3d' image file\n\n"); printf(" HIT ANY KEY TO CONTINUE\n");getch(); printf("\n\n\n\n\n"); printf("PSEUDO COLOR FILE:\n\n"); printf("'mont_y.pc': (projection is only needed for spherical earth)\n"); printf("\n"); printf("0.0 km projection = spherical_earth\n"); printf("49.644 scale km/in\n"); printf("50.00 last vertical exaggeration\n"); printf("0.00 shadow brightness (0.0 to 1.0)\n"); printf(" min max r g b\n"); printf(" 1.000 1.500 255 128 0\n"); printf(" 0.500 1.000 255 255 0\n"); printf(" 0.000 0.500 0 255 0\n"); printf("-1.000 0.000 128 255 255\n"); printf("-2.000 -1.000 128 255 255\n"); printf("-3.000 -2.000 106 191 255\n"); printf("-4.000 -3.000 85 128 255\n"); printf("-5.000 -4.000 64 64 255\n"); printf("-6.000 -5.000 128 0 255\n\n"); printf(" HIT ANY KEY TO CONTINUE\n");getch(); printf("\n\n\n\n\n"); printf("CONTOUR COLOR FILE:\n\n"); printf("'test.ctr'\n\n"); printf("-2.756676392 = mid_z (km)\n"); printf("3.016983 scale km/in\n"); printf("10.00 last vertical exaggeration\n"); printf("height(km) r g b\n"); printf("0.000 255 255 255\n"); printf("0.250 0 0 0\n"); printf("0.500 0 0 0\n"); printf("0.750 0 0 0\n"); printf("1.000 255 255 255\n"); printf("1.250 0 0 0\n"); printf("1.500 0 0 0\n"); printf("1.750 0 0 0\n"); printf("2.000 255 255 255\n"); printf("2.250 0 0 0\n"); printf("2.500 0 0 0\n\n"); printf(" HIT ANY KEY TO CONTINUE\n");getch(); } if(ans=='p') { if(get_file_name("*.bwv",string,0)>=0) plot_mesh(string); else printf("No files chosen\n\n"); } if(ans=='r') { if(get_file_name("*.bwv",string,0)>=0) plot_render(string); else printf("No files chosen\n\n"); } if(ans=='3') { if(get_file_name("*.bwv",string,0)>=0) { printf("Give output file name for 3-D image.\n\n"); scanf("%s",String3d); plot_3d(string); } else printf("No files chosen\n\n"); } if(ans=='C') { Color3D=1; if(get_file_name("*.bwv",string,0)>=0) { printf("Give output file name for 3-D image.\n\n"); scanf("%s",String3d); plot_3d(string); } else printf("No files chosen\n\n"); Color3D=0; } }while(ans!='x'); } /********************************************************************** ** ** ** **********************************************************************/ int auto3d(char *txtfile) { int i,j,k; char infile[100],outfile[100],string[100]; char str1[50],str2[50],str3[50]; FILE *fptxt; float angle,dist,index,scale,view,vert; infile[0]='\0'; outfile[0]='\0'; fptxt=fopen(txtfile,"rt"); if(!fptxt) { printf("Could not open '%s' to read instructions.\n\n",txtfile); printf( "CONTROL FILE:\n"); printf("\n"); printf( "input = [filename] the type must be '.bwv' but is not needed\n"); printf( "rot_x -90 pushes top of image back 90 degrees\n"); printf( "rot_y 90 pushes right side of image back 90 degrees\n"); printf( "rot_z 25 rotates image 25 degrees counter clockwise\n"); printf( "move_x 0.25 moves image to right 0.25 inches\n"); printf( "move_y 0.25 moves image up 0.25 inches\n"); printf( "move_z 0.25 moves image out of screen right 0.25 inches\n"); printf( "set_size 50 sets image scale to 50 pixels/inch\n"); printf( "scale_size 0.95 reduces images scale by 5%\n"); printf( "vertical 2 00 doubles vertical size\n"); printf( "set_view 2500 sets viewing point 2500 pixels from screen \n"); printf( " (2500/50 = 50 inches)\n"); printf( "scale_view 1.05 moves view point out 5%\n"); printf( "output = [filename] name of '.3d' file \n"); printf( "plot makes the 'filename.3d' image file\n"); printf( "mesh plots mesh image\n"); printf("\n"); exit(0); } while(fgets(string,100,fptxt)) { sscanf(string,"%s%s%s",str1,str2,str3); if(strcmpi(str1,"input")==0) { strcpy(infile,str3); for(k=0;k0) { printf("Vertical Exaggeration set to %0.3f\n",vert); Vert=vert; } } if(strcmpi(str1,"rot_x")==0) { if(sscanf(str2,"%f",&angle)>0) { printf("Rotating About X %0.3f degrees\n",angle); rot_x(angle,T); } } if(strcmpi(str1,"rot_y")==0) { if(sscanf(str2,"%f",&angle)>0) { printf("Rotating About Y %0.3f degrees\n",angle); rot_y(angle,T); } } if(strcmpi(str1,"rot_z")==0) { if(sscanf(str2,"%f",&angle)>0) { printf("Rotating About Z %0.3f degrees\n",angle); rot_z(angle,T); } } if(strcmpi(str1,"move_x")==0) { if(sscanf(str2,"%f",&dist)>0) { printf("Moving Along X %0.3f inches\n",dist); tran_x(dist,T); } } if(strcmpi(str1,"move_y")==0) { if(sscanf(str2,"%f",&dist)>0) { printf("Moving Along Y %0.3f inches\n",dist); tran_y(dist,T); } } if(strcmpi(str1,"move_z")==0) { if(sscanf(str2,"%f",&dist)>0) { printf("Moving Along Z %0.3f inches\n",dist); tran_z(dist,T); } } if(strcmpi(str1,"set_size")==0) { if(sscanf(str2,"%f",&index)) { printf("Image Scale set to %0.5f pixels/inch\n",index); Scale=index; T[3][3]=1.0/Scale; } } if(strcmpi(str1,"scale_size")==0) { if(sscanf(str2,"%f",&index)) { Scale*=index; T[3][3]=1.0/Scale; printf("Image Scale changed to %0.5f pixels/inch\n",Scale); } } if(strcmpi(str1,"set_view")==0) { if(sscanf(str2,"%f",&index)) { K=index; printf( "Image Viewing Point set to %0.0f pixels or %0.3 inch\n",K,K/Scale); } } if(strcmpi(str1,"scale_view")==0) { if(sscanf(str2,"%f",&index)) { K*=index; printf( "Image Viewing Point changed to %0.0f pixels or %0.3 inch\n",K,K/Scale); } } if(strcmpi(str1,"plot")==0) { strcpy(String3d,outfile); if(Auto[0]==0) plot_3d(infile); else { strcpy(AutoName,outfile); plot_render(infile); } } if(strcmpi(str1,"mesh")==0) { strcpy(String3d,outfile); plot_mesh(infile); } } if(fptxt) fclose(fptxt); exit(0); } /********************************************************************** ** ** ** **********************************************************************/ int plot_3d(char *string) { int i,j,k; char stringn[100]; FILE *fpbwv; int numplot; load_pc(string); if(Contour>0) load_ct(string); for(k=0;k=0) { printf("File '%s' chosen.\n\n",string); printf("Left Eye or red image\n"); fpbwv=fopen(string,"rb"); if(!fpbwv) { printf("Could not open '%s' to read.\n\n",string); exit(0); } numplot=render_data(fpbwv,(float)-72); if(fpbwv) fclose(fpbwv); video_off(); } else { printf("render failed.\n\n"); exit(0); } if(Fp3d) fclose(Fp3d); } /********************************************************************** ** ** ** **********************************************************************/ int plot_render(char *string) { int k; char str[100]; FILE *fpbwv; load_pc(string); if(Contour>0) load_ct(string); printf("File '%s' chosen.\n\n",string); fpbwv=fopen(string,"rb"); if(!fpbwv) { printf("Could not open '%s' to read.\n\n",string); exit(0); } render_data(fpbwv,(float)0.0); if(fpbwv) fclose(fpbwv); video_off(); } /********************************************************************** ** ** ** **********************************************************************/ int load_pc(char *string) { int i,j,k; char str[100]; int r,g,b; if(PseudoColor!=0) { PCnum=0; strcpy(str,string); for(k=0;k1.0) { printf("Shadow value must be between 0.0 and 1.0\n\n"); print_pchelp(); exit(0); } fgets(str,100,FpPC); while(fgets(str,100,FpPC)) { if(sscanf(str,"%f%f%d%d%d",PCmin+k,PCmax+k,&r,&g,&b)==5) { PCcolor[k].r=r; PCcolor[k].g=g; PCcolor[k].b=b; k+=1; if(k>=MAX_PC) { printf("Too many colors.\n\n"); exit(0); } } } if(FpPC) fclose(FpPC); if(k<1) { printf("No color information given\n"); print_pchelp(); exit(0); } else { PCnum=k; printf("There are %d colors.\n",PCnum); printf("%8.5f mid_z\n",PCmid); printf("%8.5f scale (km/in)\n",PCscale); printf("%8.5f last vertical exaggeration\n",PCvert); printf("%8.5f shadow brightness\n",PCshadow); for(k=0;k=MAX_CT) { printf("Too many colors.\n\n"); exit(0); } } } if(FpPC) fclose(FpPC); if(k<1) { printf("No color information given\n"); exit(0); } else { CTnum=k; printf("There are %d contours.\n",CTnum); printf("%8.5f mid_z\n",CTmid); printf("%8.5f scale (km/in)\n",CTscale); printf("%8.5f last vertical exaggeration\n",CTvert); for(k=0;k ",CTval[k]); CTval[k]=(CTval[k]-CTmid)/CTscale*CTvert; printf("%8.3f %3d %3d %3d\n",CTval[k], CTcolor[k].r,CTcolor[k].g,CTcolor[k].b); } } } } } /********************************************************************** ** ** ** **********************************************************************/ int print_pchelp() { printf("\nExample of File:\n\n"); printf(" -1.75 = mid_z\n"); printf("15.222711 = scale (km/inch)\n"); printf(" 0.50 = shadow brightness\n"); printf(" min max r g b\n"); printf("-4.000 -3.000 0 255 255\n"); printf("-3.000 -2.000 64 255 255\n"); printf("-2.000 -1.000 128 255 255\n"); printf("-1.000 0.000 191 255 255\n"); printf("\n"); printf("The values of 'mid_z' and 'scale' can be found in the '.wvf' file.\n"); printf("Any levels not given a color will not be plotted.\n\n"); } /********************************************************************** ** ** ** **********************************************************************/ int plot_mesh(char *string) { int numplot; FILE *fpbwv; char ans1; float delang=30,deldist=2.0; double dscale=sqrt((double)2); PlotBack=0; printf("\n\nDo you want to plot back sides? (y or n)\n\n"); if(getch()=='y') PlotBack=1; printf("File '%s' chosen.\n\n",string); fpbwv=fopen(string,"rb"); if(!fpbwv) { printf("Could not open '%s' to read.\n\n",string); exit(0); } plot_help(); video_on(); numplot=plot_data(fpbwv); do { ans1=getch(); if(ans1==13) plot_status(deldist,delang); if(ans1=='-') { if(delang>5.0) delang/=2.0; deldist/=2.0; dscale=sqrt(dscale); } if(ans1=='+') { if(delang<90.0) delang*=2.0; deldist*=2.0; dscale*=dscale; } if(ans1=='v') { Vert-=0.5; if(Vert==0.0) Vert=-0.5; } if(ans1=='V') { Vert+=0.5; if(Vert==0.0) Vert=+0.5; } if(ans1=='u') tran_y(deldist,T); if(ans1=='d') tran_y(-deldist,T); if(ans1=='r') tran_x(deldist,T); if(ans1=='l') tran_x(-deldist,T); if(ans1=='b') tran_z(-deldist,T); if(ans1=='f') tran_z(deldist,T); if(ans1=='L') { Scale*=dscale;; T[3][3]=1.0/Scale; } if(ans1=='S') { Scale/=dscale;; T[3][3]=1.0/Scale; } if(ans1=='F') K*=2.0; if(ans1=='C') K/=2.0; if(ans1=='S'||ans1=='L'||ans1=='f'||ans1=='b'|| ans1=='u'||ans1=='d'||ans1=='r'||ans1=='l'|| ans1=='C'||ans1=='F'||ans1=='v'||ans1=='V') { numplot=plot_data(fpbwv); } if(ans1==0) { ans1=getch(); if(ans1==80) rot_x(delang,T); if(ans1==72) rot_x(-delang,T); if(ans1==75) rot_y(-delang,T); if(ans1==77) rot_y(delang,T); numplot=plot_data(fpbwv); } }while(ans1!=27); video_off(); if(fpbwv) fclose(fpbwv); } /********************************************************************** ** ** ** **********************************************************************/ int plot_status(float deldist,float delang) { char string1[100],string2[100]; paint_box(0,0,Buffer,0,ScreenYs-18,ScreenXs,17); sprintf(string1,"Scale=%0.0f View Pt (z)=%0.0f pixels ",Scale,K); sprintf(string2,"del_ ang=%0.2f deg del_dist=%0.2f (%0.0f pixels)", delang,deldist,deldist*Scale); strcat(string1,string2); plot_font_h(48,2,ScreenYs-16,string1,2,Font); } /*********************************************************************** ** ** ** ** ************************************************************************/ int plot_tri(int hue,int x1,int y1,int x2,int y2,int x3,int y3,int val) { int k; double xx1,xx2,y,fy2; double ox[3],oy[3]; int hit,ix1,ix2,iy,iy2; double dxdy1,dxdy2; plotln(0,x1,y1,x2,y2,val); plotln(0,x2,y2,x3,y3,val); plotln(0,x3,y3,x1,y1,val); ox[0]=x1; ox[1]=x2; ox[2]=x3; oy[0]=y1; oy[1]=y2; oy[2]=y3; do /* order y from top to bottom */ { hit=0; for(k=1;k<3;k++) if(oy[k]0); if(oy[1]==oy[0]) /* horizontal base at top of triangle */ { if(oy[0]==oy[1]&&oy[1]==oy[2]) /* straight horizontal line */ { iy=oy[0]; if(ox[0]ox[0]) /* put 0 to left of 1 */ { fswap(&ox[0],&ox[1]); fswap(&oy[0],&oy[1]); } y=oy[0]; fy2=oy[2]; xx1=ox[0]; xx2=ox[1]; dxdy1=(ox[2]-ox[0])/(oy[2]-oy[0]); dxdy2=(ox[2]-ox[1])/(oy[2]-oy[1]); while(y<=fy2) { plotln(hue,(int)xx1,(int)y,(int)xx2,(int)y,val); xx1+=dxdy1; xx2+=dxdy2; y+=1; } return(0); } dxdy1=(ox[1]-ox[0])/(oy[1]-oy[0]); dxdy2=(ox[2]-ox[0])/(oy[2]-oy[0]); if(dxdy1>dxdy2) /* point 2 left of line to point 1 */ { fswap(&ox[1],&ox[2]); fswap(&oy[1],&oy[2]); fswap(&dxdy1,&dxdy2); } if(oy[1]0) { PCnumval=255/PCnum; fv=PCnumval-1; } xv[0]=xview; xv[1]=xv[2]=0.0; fpoc=fopen("wave.tmp","w+b"); if(!fpoc) { printf("Could not open temp file to write.\n\n"); exit(0); } sun[0]=1.0; sun[1]=1.0; sun[2]=1.0; sunbrite=0.9; moon[0]=-1.0; moon[1]=-1.0; moon[2]= 1.0; moonbrite=0.15; backlite=0.20; background.r=0; background.g=128; background.b=128; fplight=fopen("wave.lit","rt"); if(fplight) { fgets(string,100,fplight); sscanf(string,"%f%f%f",sun,sun+1,sun+2); fgets(string,100,fplight); sscanf(string,"%f",&sunbrite); fgets(string,100,fplight); sscanf(string,"%f%f%f%f",moon,moon+1,moon+2); fgets(string,100,fplight); sscanf(string,"%f",&moonbrite); fgets(string,100,fplight); sscanf(string,"%f",&backlite); fgets(string,100,fplight); sscanf(string,"%d%d%d",&background.r,&background.g,&background.b); if(fplight) fclose(fplight); } if(xview!=0.0) { sunbrite=1.0; moonbrite=0.0; backlite=0.25; } msum=sum=0; for(k=0;k<3;k++) { sum+=sun[k]*sun[k]; msum+=moon[k]*moon[k]; } sum=sqrt((double)sum); msum=sqrt((double)msum); for(k=0;k<3;k++) { sun[k]/=sum; moon[k]/=msum; } e[0]=e[1]=0.0; e[2]=K; rewind(fpin); tot=num=0; printf("Reading Triangle Data ...\n"); printf("Maximum number allowed = %ld\n",Max_T); printf(" Usable Available\n"); printf("------- ---------\n"); while((hit=fread((char *)dat,sizeof(float),15,fpin))==15&&num0) { val=getch(); if(val==27) return(-2); } onscreen=0; behind=0; for(i=0;i<3;i++) { for(k=0;k<3;k++) /* load corners of triangle */ a[k]=dat[i][k]; a[3]=1.0; mat_x_vec(T,a,c); for(k=0;k<3;k++) c[k]=c[k]/c[3]; if(c[2]=0&&fx[i]=0&&fy[i]0&&behind==0) { for(i=0;i<2;i++) { for(k=0;k<3;k++) /* load normal */ a[k]=dat[i+3][k]; a[3]=1.0; mat_x_vec(T,a,c); for(k=0;k<3;k++) p[i][k]=c[k]=c[k]/c[3]-xv[k]; } if(behind==0) { for(k=0;k<3;k++) { n[k]=p[1][k]-p[0][k]; /* normal vector */ d[k]=e[k]-p[0][k]; /* d[] points from center of triangle to eye */ } if(Contour==2||dot(d,n)>0.0) /* lines only or facing forward */ { if(PCnum==0) /* no pseudo color */ { Order[num]=num; fwrite((char *)&offset,sizeof(long),1,fpoc); Dist[num]=p[0][0]*p[0][0]+p[0][1]*p[0][1]+ (K-p[0][2])*(K-p[0][2]); fcolor=dot(n,sun)*sunbrite; if(fcolor<0.0) fcolor=0.0; mlite=dot(n,moon)*moonbrite; if(mlite>0) fcolor+=mlite; if(fcolor>0.0) color=64.0*(fcolor*T[3][3]*(1.0-backlite)+backlite); else color=backlite*64.0; if(color>0&&color<=63) Color=color; else if(color>63) Color=63; else Color=0; fwrite((char *)&Color,sizeof(char),1,fpoc); num+=1; } else /* pseudo color */ { lv=-1; for(k=0;k=PCmin[k]&&midz<=PCmax[k]) { lv=k; break; } } if(lv>=0) /* z has a color */ { Order[num]=num; fwrite((char *)&offset,sizeof(long),1,fpoc); Dist[num]=p[0][0]*p[0][0]+p[0][1]*p[0][1]+ (K-p[0][2])*(K-p[0][2]); fcolor=dot(n,sun)/64.0; if(fcolor<0.0) fcolor=0.0; if(fcolor>1.0) fcolor=1.0; Color=fcolor*fv+.5+lv*PCnumval; if(Color<0||Color>255) { printf("Invalid color:\n\n"); printf("fcolor=%f fv=%f lv=%d\n",fcolor,fv,lv); exit(0); } fwrite((char *)&Color,sizeof(char),1,fpoc); num+=1; } } } } } offset=ftell(fpin); if(tot%10==0) printf("%c%6ld of %ld",cr,num,tot); } printf("\n\n"); sort2(num,Dist,Order); video_on(); Lut[64].r=background.r; Lut[64].g=background.g; Lut[64].b=background.b; WritePalette(Lut); if(PseudoColor>0&&PCnum>0) { set_lut('p',0); Contour=0; } if(Contour>0&&CTnum>0) set_lut('t',0); if(PCnum==0) /* paint background for B/W color image */ paint_box(0,64,Buffer,0,0,ScreenXs,ScreenYs); else /* paint background for pseudo color image */ paint_box(0,255,Buffer,0,0,ScreenXs,ScreenYs); line=48; norm=240; back=0; /* plot triangles back to front */ for(t=num-1;t>=0;t--) { offset=(long)Order[t]*TEMP_SIZE; fseek(fpoc,offset,SEEK_SET); fread((char *)&offset,sizeof(long),1,fpoc); fread((char *)&Color,sizeof(char),1,fpoc); fseek(fpin,offset,SEEK_SET); fread((char *)dat,sizeof(float),15,fpin); if(Vert!=1.0) { for(i=0;i<4;i++) dat[i][2]*=Vert; normal(dat); } if(kbhit()>0) { val=getch(); if(val==27) return(-2); } onscreen=0; for(i=0;i<3;i++) { for(k=0;k<3;k++) a[k]=dat[i][k]; a[3]=1.0; mat_x_vec(T,a,c); if(c[3]==0) { printf("at 1 c[3] = 0.0\n"); exit(0); } for(k=0;k<3;k++) c[k]=c[k]/c[3]; behind=0; if((c[2]/-K+1.0)>0.0) { fx[i]=(c[0]-xview)/((c[2])/-K+1.0)+xcent+xview; fy[i]=-c[1]/(c[2]/-K+1.0)+ycent; } else behind=1; if(fx[i]>=0&&fx[i]=0&&fy[i]0) { behind=0; for(i=0;i<2;i++) { for(k=0;k<3;k++) a[k]=dat[i+3][k]; a[3]=1.0; mat_x_vec(T,a,c); for(k=0;k<3;k++) p[i][k]=c[k]=c[k]/c[3]-xv[k]; if((c[2]/-K+1.0)>0.0) { nx[i]=c[0]/(c[2]/-K+1.0)+xcent+xview; ny[i]=-c[1]/(c[2]/-K+1.0)+ycent; } else behind=1; } if(behind==0) { if(Contour<2) /* not lines only */ plot_tri(0,fx[0],fy[0],fx[1],fy[1],fx[2],fy[2], (int)Color); if(Contour>0) { for(cl=0;cl0) { val=getch(); if(val==27) { if(fpdat) fclose(fpdat); if(xview==0.0) { sprintf(string,"%s.pal",name); unlink(string); sprintf(string,"%s.dat",name); unlink(string); sprintf(string,"%s.lbl",name); unlink(string); } return(-1); } } getrow(0,0,ScreenXs-1,j,Buffer); if(Color3D==0) { if(xview==0.0&&fpdat) fwrite((char *)Buffer,sizeof(char),ScreenXs,fpdat); else if(Fp3d) { fwrite((char *)Buffer,sizeof(char),ScreenXs,Fp3d); fseek(Fp3d,offset,SEEK_CUR); } else { printf("Could not find output file.\n\n"); exit(0); } } else { offset=fs-(long)(j+1)*is; if(xview>0.0) offset+=sx*b; fseek(Fp3d,offset,SEEK_SET); for(m=0;m2) i1=0; if((z[i0]<=h&&z[i1]>h)||(z[i1]<=h&&z[i0]>h)) { d=z[i1]-z[i0]; if(d!=0.0) { d0=(h-z[i0])/d; d1=(z[i1]-h)/d; ix[hit]=(x[i0]*d1+x[i1]*d0); iy[hit]=(y[i0]*d1+y[i1]*d0); hit+=1; } } } return(hit); } /********************************************************************** ** ** ** **********************************************************************/ float mid_z(float dat[5][3]) { float maxz,minz,midz; maxz=minz=dat[0][2]; if(dat[1][2]>maxz) maxz=dat[1][2]; if(dat[1][2]maxz) maxz=dat[2][2]; if(dat[2][2]0.0) for(k=0;k<3;k++) c[k]/=len; for(i=0;i<3;i++) for(k=0;k<3;k++) norm[k]+=dat[i][k]/3.0; for(k=0;k<3;k++) { dat[3][k]=norm[k]; dat[4][k]=norm[k]+c[k]; } } /************************************************************************** ** ** ** ** ***************************************************************************/ int cross(float a[3],float b[3], float c[3]) { c[0]=a[1]*b[2]-a[2]*b[1]; c[1]=a[2]*b[0]-a[0]*b[2]; c[2]=a[0]*b[1]-a[1]*b[0]; } /************************************************************************** ** ** ** ************************************************************************* */ int sort2(long n,float huge ra[],unsigned huge ib[]) { long l,j,ir,i; float rra,temp; int iib,itemp; long hit; if(n<2) { printf("Not enough points to sort int 'sort2()'\n\n"); exit(0); } printf("Sorting triagles by distance from veiwing point "); l=(n>>1)+1; ir=n-1; for(;;) { if(l>1) { rra=ra[--l]; iib=ib[l]; } else { rra=ra[ir]; iib=ib[ir]; ra[ir]=ra[1]; ib[ir]=ib[1]; if(--ir==1) { ra[1]=rra; ib[1]=iib; printf("\nStarting final sort "); for(i=1;ira[i]) { hit+=1; itemp=ib[i]; ib[i]=ib[i-1]; ib[i-1]=itemp; temp=ra[i]; ra[i]=ra[i-1]; ra[i-1]=temp; } } printf("\n"); return(1); } } i=l; j=l<<1; while(j<=ir) { if(j255) { video_off(); printf("Error in set_lut\n\n"); exit(0); } } } } WritePalette(Lut); } /********************************************************************** ** ** ** **********************************************************************/ int video_off() { SetVideoMode(0); } /************************************************************************** ** ** ** ************************************************************************* */ int video_on() { int i,row; i=GetVideoBoardID(); if(i==0) VideoType='E'; else if(i==1&&(VideoType=='X'||VideoType=='Y')) VideoType='V'; if(VideoType=='X') { row=SetVideoMode(480,&B_Id); if(B_Id.color==256&&B_Id.row==480) { ScreenXs=640; ScreenYs=480; } else if(B_Id.color==256&&B_Id.row==400) VideoType='Y'; else VideoType='V'; } if(VideoType=='Y') { row=SetVideoMode(400,&B_Id); if(B_Id.row<400) VideoType='V'; else { ScreenXs=640; ScreenYs=row; } } if(VideoType=='V') { row=SetVideoMode(0x12,&B_Id); if(B_Id.row!=480||row!=480||B_Id.color!=16||B_Id.col!=640) VideoType='E'; else { ScreenXs=640; ScreenYs=480; } } if(VideoType=='S') { row=SetVideoMode(0x13,&B_Id); if(B_Id.row!=200) { SetVideoMode(0,&B_Id); printf("Could not boot color board.\n"); exit(0); } ScreenXs=320; ScreenYs=200; } if(VideoType=='E') { row=SetVideoMode(0x10,&B_Id); if(B_Id.row!=350||row<350||B_Id.col!=640) { SetVideoMode(0,&B_Id); printf("Could not boot color board.\n"); exit(0); } ScreenXs=640; ScreenYs=350; } if(B_Id.row<0) { SetVideoMode(0,&B_Id); printf("Could not boot board -- row = %d\n\n",row); exit(0); } set_lut('g',0); } /************************************************************************** ** ** ** ** ***************************************************************************/ long plot_data(FILE *fpin) { int i,j,k; float dat[5][3],a[4],c[4],d[3],n[3],e[3],p[5][3]; int ix[3],iy[3],val,xcent=ScreenXs/2,ycent=ScreenYs/2,nx[2],ny[2]; int onscreen; long numplot=0,numhit=0; int line,norm,behind,back; char string[100]; e[0]=e[1]=0.0; e[2]=K; rewind(fpin); paint_box(0,16,Buffer,0,0,ScreenXs,ScreenYs); plotln(0,D1,0,D1,ScreenYs-1,8); plotln(0,D2,0,D2,ScreenYs-1,8); line=48; norm=240; back=0; if(PlotBack==1) { while(fread((char *)dat,sizeof(float),15,fpin)==15) { if(Vert!=1.0) { for(i=0;i<4;i++) dat[i][2]*=Vert; normal(dat); } if(kbhit()>0) { val=getch(); if(val==27) return(-2); } onscreen=0; for(i=0;i<3;i++) { for(k=0;k<3;k++) a[k]=dat[i][k]; a[3]=1.0; mat_x_vec(T,a,c); if(c[3]==0) { printf("at 1 c[3] = 0.0\n"); exit(0); } for(k=0;k<3;k++) c[k]=c[k]/c[3]; behind=0; if((c[2]/-K+1.0)>0.0) { ix[i]=c[0]/(c[2]/-K+1.0)+xcent; iy[i]=-c[1]/(c[2]/-K+1.0)+ycent; } else behind=1; if(ix[i]>=0&&ix[i]=0&&iy[i]0) { behind=0; for(i=0;i<2;i++) { for(k=0;k<3;k++) a[k]=dat[i+3][k]; a[3]=1.0; mat_x_vec(T,a,c); for(k=0;k<3;k++) p[i][k]=c[k]=c[k]/c[3]; if((c[2]/-K+1.0)>0.0) { nx[i]=c[0]/(c[2]/-K+1.0)+xcent; ny[i]=-c[1]/(c[2]/-K+1.0)+ycent; } else behind=1; } if(behind==0) { for(k=0;k<3;k++) { n[k]=p[1][k]-p[0][k]; /* normal vector */ d[k]=e[k]-p[0][k]; /* d[] points from center of triangle to eye */ } if(dot(d,n)<=0.0) /* facing back */ { plotln(0,ix[0],iy[0],ix[1],iy[1],back); plotln(0,ix[1],iy[1],ix[2],iy[2],back); plotln(0,ix[2],iy[2],ix[0],iy[0],back); if(Normal==1) plotln(0,nx[1],ny[1],nx[0],ny[0],norm); } } } } } rewind(fpin); while(fread((char *)dat,sizeof(float),15,fpin)==15) { if(Vert!=1.0) { for(i=0;i<4;i++) dat[i][2]*=Vert; normal(dat); } if(kbhit()>0) { val=getch(); if(val==27) return(-2); } onscreen=0; for(i=0;i<3;i++) { for(k=0;k<3;k++) a[k]=dat[i][k]; a[3]=1.0; mat_x_vec(T,a,c); for(k=0;k<3;k++) c[k]=c[k]/c[3]; behind=0; if((c[2]/-K+1.0)>0.0) { ix[i]=c[0]/(c[2]/-K+1.0)+xcent; iy[i]=-c[1]/(c[2]/-K+1.0)+ycent; } else behind=1; if(ix[i]>=0&&ix[i]=0&&iy[i]0) { behind=0; for(i=0;i<2;i++) { for(k=0;k<3;k++) a[k]=dat[i+3][k]; a[3]=1.0; mat_x_vec(T,a,c); for(k=0;k<3;k++) p[i][k]=c[k]=c[k]/c[3]; if((c[2]/-K+1.0)>0.0) { nx[i]=c[0]/(c[2]/-K+1.0)+xcent; ny[i]=-c[1]/(c[2]/-K+1.0)+ycent; } else behind=1; } if(behind==0) { for(k=0;k<3;k++) { n[k]=p[1][k]-p[0][k]; /* normal vector */ d[k]=e[k]-p[0][k]; /* d[] points from center of triangle to eye */ } if(dot(d,n)>0.0) /* facing forward */ { plotln(0,ix[0],iy[0],ix[1],iy[1],line); plotln(0,ix[1],iy[1],ix[2],iy[2],line); plotln(0,ix[2],iy[2],ix[0],iy[0],line); if(Normal==1) plotln(0,nx[1],ny[1],nx[0],ny[0],norm); numhit+=1; } } } } paint_box(0,0,Buffer,0,ScreenYs-18,ScreenXs,17); sprintf(string,"There are %ld triangles plotted",numhit); plot_font_h(48,2,ScreenYs-16,string,2,Font); return(numplot); } /************************************************************************** ** ** ** ** ***************************************************************************/ int tran_x(float dist,float t[4][4]) { t[3][0]+=dist; } /************************************************************************** ** ** ** ** ***************************************************************************/ int tran_y(float dist,float t[4][4]) { t[3][1]+=dist; } /************************************************************************** ** ** ** ** ***************************************************************************/ int tran_z(float dist,float t[4][4]) { t[3][2]+=dist; } /************************************************************************** ** ** ** ** ***************************************************************************/ int rot_x(float deg,float t[4][4]) { int i,j,k; double ang=deg*DEG_RAD; float a[4][4],b[4][4]; for(i=0;i<4;i++) for(j=0;j<4;j++) a[i][j]=t[i][j]; for(i=0;i<4;i++) for(j=0;j<4;j++) { if(i==j) b[i][j]=1.0; else b[i][j]=0.0; } b[2][2]=b[1][1]=cos(ang); b[1][2]=b[2][1]=sin(ang); b[2][1]*=-1.0; mat_x_mat(a,b,t); } /************************************************************************** ** ** ** ** ***************************************************************************/ int rot_y(float deg,float t[4][4]) { int i,j,k; double ang=deg*DEG_RAD; float a[4][4],b[4][4]; for(i=0;i<4;i++) for(j=0;j<4;j++) a[i][j]=t[i][j]; for(i=0;i<4;i++) for(j=0;j<4;j++) { if(i==j) b[i][j]=1.0; else b[i][j]=0.0; } b[2][2]=b[0][0]=cos(ang); b[0][2]=b[2][0]=sin(ang); b[0][2]*=-1.0; mat_x_mat(a,b,t); } /************************************************************************** ** ** ** ** ***************************************************************************/ int rot_z(float deg,float t[4][4]) { int i,j,k; double ang=deg*DEG_RAD; float a[4][4],b[4][4]; for(i=0;i<4;i++) for(j=0;j<4;j++) a[i][j]=t[i][j]; for(i=0;i<4;i++) for(j=0;j<4;j++) { if(i==j) b[i][j]=1.0; else b[i][j]=0.0; } b[1][1]=b[0][0]=cos(ang); b[0][1]=b[1][0]=sin(ang); b[1][0]*=-1.0; mat_x_mat(a,b,t); } /************************************************************************** ** ** ** ** ***************************************************************************/ int mat_x_vec(float a[4][4],float b[4],float c[4]) { int i,j,k; for(k=0;k<4;k++) c[k]=0.0; for(j=0;j<4;j++) for(k=0;k<4;k++) c[j]+=a[k][j]*b[k]; } /************************************************************************** ** ** ** ** ***************************************************************************/ int mat_x_mat(float a[4][4],float b[4][4],float c[4][4]) { int i,j,k; for(i=0;i<4;i++) for(j=0;j<4;j++) c[i][j]=0.0; for(i=0;i<4;i++) for(j=0;j<4;j++) for(k=0;k<4;k++) c[i][j]+=a[i][k]*b[k][j]; } /************************************************************************** ** ** ** ** ***************************************************************************/ float dot(float a[3],float b[3]) { float c; c=a[0]*b[0]+a[1]*b[1]+a[2]*b[2]; return(c); }