#include #include #include #include "colors.h" #include "gtools.h" #include "gdraws.h" int color,register_no; int LINEWIDTH=1, OPERATOR=0, ANGLE, XCENTER, YCENTER; unsigned long int PATTERN=0xFFFFFFFF; unsigned long int style[8] = { 0xFFFFFFFF,0xC0C0C0C0, 0xFF00FF00,0xFFF0FFF0,0xF000F000, 0xFFFF0000,0xFFFFF0F0,0xFFF0F0F0}; float rad_per_degree=0.0174533,x_angle,y_angle,z_angle, step, x3, y3, z3, x_angle, y_angle, z_angle; int x,y,z,rz_scale,vert_scale=1,horz_scale=1,color,type,offset; char ch; float degrees_to_radians(float degrees); void projection ( float x3, float y3, float z3, int color); void wait(char title[]); /* prompt to continue, wait for keypress */ void draw_cube(float rot_pts[8][3],int vx[],int vy[]); void perspective(int no_of_points,float rot_pts[][3],float pt[][3],int vx[], int vy[]); void setColorReg(int reg_no, int blue, int green, int red); float surface_test(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3); void draw_sphere(); void draw_side(int i1, int i2, int i3, int i4,float rot_pts[8][3], int vx[],int vy[]); void fill_cube(float rot_pts[8][3],int vx[],int vy[]); void fill_side(int i1, int i2, int i3, int i4,float rot_pts[8][3], int vx[],int vy[]); void fill_sphere(); int getPalette(int register); int shaded_surface_test(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3); void pattern_plot(int x, int y, int color); float yaw, roll, pitch, latitude, longitude, xls=.57735,yls=.57735,zls=.57735; int k,d=375, mx=0, my=0, mz=-200, off_x,off_y,sx, sy; int vx1[37],vy1[37],vx2[37],vy2[37],i,j; float rot_pts1[37][3],pt1[37][3],pt6[8][3]={{30,-30,30},{30,30,30}, {-30,30,30},{-30,-30,30},{30,30,-30},{-30,30,-30},{-30,-30,-30}, {30,-30,-30}},rot_pts2[37][3],pt2[37][3]; main () { int i,angle,green; char adapt; adapt = getAdapter(); if ((adapt != 'E') && (adapt != 'C') && (adapt != 'V')) { printf("Cannot Run Demo -- No CGA, EGA or VGA Installed!"); printf("\nAdapter is: %c",adapt);} else { if (adapt == 'V') setMode(0x12); if (adapt == 'E') setMode(16); if (adapt == 'C') { setMode(4); setCGAPalette(1); } cls(1); while(ch != 0x0D) { cls(0); drawRect(-316,-235,315,235,11); off_x = -200; off_y = 100; for(i=0; i<6; i++) { yaw=degrees_to_radians(rand()/92); roll=degrees_to_radians(rand()/92); pitch=degrees_to_radians(rand()/92); color=rand()/4681+9; perspective(8,rot_pts1,pt6,vx1,vy1); draw_cube(rot_pts1,vx1,vy1); off_x += 200; if (off_x>210) { off_x= -200; off_y= -100; } } gotoxy(21,0); wait("Cubes"); } ch = 0; while(ch != 0x0D) { d = 800; cls(0); off_x = 0; off_y = 0; latitude = 0; longitude = 0; drawRect(-316,-235,315,235,11); mx = 0; my = 0; mz = -150; yaw=degrees_to_radians(rand()/92); roll=degrees_to_radians(rand()/92); pitch=degrees_to_radians(rand()/92); color=rand()/4681+9; draw_sphere(); wait("Sphere"); } #ifdef VGA for (i=0; i<16; i++) { setEGApalette(i,i); writeColorReg(i,0,4*i,0); } #endif #ifdef EGA setEGApalette(0,0); setEGApalette(1,16); setEGApalette(2,2); setEGApalette(3,34); setEGApalette(4,19); setEGApalette(5,26); setEGApalette(6,8); setEGApalette(7,27); setEGApalette(8,63); setEGApalette(9,2); setEGApalette(10,3); setEGApalette(11,4); setEGApalette(12,5); setEGApalette(13,6); setEGApalette(14,7); setEGApalette(15,63); #endif cls(0); ch = 0; PATTERN = style[0]; while(ch != 0x0D) { d = 375; mz = -200; cls(0); drawRect(-316,-235,315,235,11); off_x = -200; off_y = 100; for(i=0; i<6; i++) { yaw=degrees_to_radians(rand()/92); roll=degrees_to_radians(rand()/92); pitch=degrees_to_radians(rand()/92); color=(rand()/4681+9)+18; perspective(8,rot_pts1,pt6,vx1,vy1); fill_cube(rot_pts1,vx1,vy1); off_x += 200; if (off_x>210) { off_x= -200; off_y= -100; } } wait("Shaded Cubes"); } ch = 0; while(ch != 0x0D) { d=800; cls(0); off_x = 0; off_y = 0; latitude = 0; longitude = 0; drawRect(-316,-235,315,235,11); mx = 0; my = 0; mz = -150; yaw=degrees_to_radians(rand()/92); roll=degrees_to_radians(rand()/92); pitch=degrees_to_radians(rand()/92); fill_sphere(); wait("Shaded Sphere"); } } } void draw_cube(float rot_pts[8][3],int vx[],int vy[]) { long int sp; draw_side(7,0,3,6,rot_pts,vx,vy); draw_side(6,5,4,7,rot_pts,vx,vy); draw_side(3,2,5,6,rot_pts,vx,vy); draw_side(0,1,2,3,rot_pts,vx,vy); draw_side(7,4,1,0,rot_pts,vx,vy); draw_side(1,4,5,2,rot_pts,vx,vy); } void fill_cube(float rot_pts[8][3],int vx[],int vy[]) { long int sp; fill_side(7,0,3,6,rot_pts,vx,vy); fill_side(6,5,4,7,rot_pts,vx,vy); fill_side(3,2,5,6,rot_pts,vx,vy); fill_side(0,1,2,3,rot_pts,vx,vy); fill_side(7,4,1,0,rot_pts,vx,vy); fill_side(1,4,5,2,rot_pts,vx,vy); } void draw_side(int i1, int i2, int i3, int i4,float rot_pts[8][3], int vx[],int vy[]) { float sp; sp = surface_test(rot_pts[i1][0],rot_pts[i1][1],rot_pts[i1][2], rot_pts[i2][0],rot_pts[i2][1],rot_pts[i2][2], rot_pts[i3][0],rot_pts[i3][1],rot_pts[i3][2]); if (sp<=0) drawPoly(color,vx[i1],+vy[i1],vx[i2],+vy[i2],vx[i3],+vy[i3], vx[i4],+vy[i4],-999); } void fill_side(int i1, int i2, int i3, int i4,float rot_pts[8][3], int vx[],int vy[]) { color = shaded_surface_test(rot_pts[i1][0],rot_pts[i1][1],rot_pts[i1][2], rot_pts[i2][0],rot_pts[i2][1],rot_pts[i2][2], rot_pts[i3][0],rot_pts[i3][1],rot_pts[i3][2]); if (color < 999) { fillPoly(color,vx[i1],vy[i1],vx[i2],vy[i2] ,vx[i3],vy[i3],vx[i4],vy[i4],-999); } } void draw_sphere() { float sp; int i,j,k,radius; radius = 30; pt1[0][0] = 0; pt1[0][1] = radius; pt1[0][2] = 0; perspective(1,rot_pts1,pt1,vx1,vy1); longitude = .17453; for (latitude=0,i=0; i<36; i++,latitude+=.17453) { pt2[i][0] = cos(latitude)*sin(longitude)*radius; pt2[i][1] = cos(longitude)*radius; pt2[i][2] = sin(latitude)*sin(longitude)*radius; } pt2[36][0] = pt2[0][0]; pt2[36][1] = pt2[0][1]; pt2[36][2] = pt2[0][2]; perspective(37,rot_pts2,pt2,vx2,vy2); for (i=0; i<36; i++) { sp = surface_test(rot_pts2[i+1][0],rot_pts2[i+1][1], rot_pts2[i+1][2],rot_pts2[i][0],rot_pts2[i][1], rot_pts2[i][2],rot_pts1[0][0],rot_pts1[0][1], rot_pts1[0][2]); if (sp<=0) drawPoly(color,vx2[i+1],vy2[i+1],vx2[i],vy2[i], vx1[0],vy1[0],-999); } for (j=0; j<16; j++) { longitude += .17453; for (latitude=0,i=0; i<36; i++,latitude+=.17453) { pt2[i][0] = cos(latitude)*sin(longitude)*radius; pt2[i][1] = cos(longitude)*radius; pt2[i][2] = sin(latitude)*sin(longitude)*radius; } pt2[36][0] = pt2[0][0]; pt2[36][1] = pt2[0][1]; pt2[36][2] = pt2[0][2]; for (i=0; i<37; i++) { for (k=0; k<3; k++) rot_pts1[i][k] = rot_pts2[i][k]; vx1[i] = vx2[i]; vy1[i] = vy2[i]; } perspective(37,rot_pts2,pt2,vx2,vy2); for (i=0; i<36; i++) { sp = surface_test(rot_pts1[i][0],rot_pts1[i][1], rot_pts1[i][2],rot_pts1[i+1][0],rot_pts1[i+1][1], rot_pts1[i+1][2],rot_pts2[i+1][0],rot_pts2[i+1][1], rot_pts2[i+1][2]); if (sp<=0) drawPoly(color,vx1[i],vy1[i],vx1[i+1], vy1[i+1],vx2[i+1],vy2[i+1], vx2[i],vy2[i],-999); } } pt1[0][0] = 0; pt1[0][1] = -radius; pt1[0][2] = 0; perspective(1,rot_pts1,pt1,vx1,vy1); for (i=0; i<36; i++) { sp = surface_test(rot_pts2[i][0],rot_pts2[i][1], rot_pts2[i][2],rot_pts2[i+1][0],rot_pts2[i+1][1], rot_pts2[i+1][2],rot_pts1[0][0],rot_pts1[0][1], rot_pts1[0][2]); if (sp<=0) drawPoly(color,vx2[i+1],+vy2[i+1],vx2[i],+vy2[i], vx1[0],+vy1[0],-999); } } void fill_sphere() { float sp; int i,j,k,radius; radius = 30; pt1[0][0] = 0; pt1[0][1] = radius; pt1[0][2] = 0; perspective(1,rot_pts1,pt1,vx1,vy1); longitude = .17453; for (latitude=0,i=0; i<36; i++,latitude+=.17453) { pt2[i][0] = cos(latitude)*sin(longitude)*radius; pt2[i][1] = cos(longitude)*radius; pt2[i][2] = sin(latitude)*sin(longitude)*radius; } pt2[36][0] = pt2[0][0]; pt2[36][1] = pt2[0][1]; pt2[36][2] = pt2[0][2]; perspective(37,rot_pts2,pt2,vx2,vy2); for (i=0; i<36; i++) { color = shaded_surface_test(rot_pts2[i+1][0],rot_pts2[i+1][1], rot_pts2[i+1][2],rot_pts2[i][0],rot_pts2[i][1], rot_pts2[i][2],rot_pts1[0][0],rot_pts1[0][1], rot_pts1[0][2]); if (color<=999) fillPoly(color,vx2[i+1],vy2[i+1],vx2[i],vy2[i], vx1[0],vy1[0],-999); } for (j=0; j<16; j++) { longitude += .17453; for (latitude=0,i=0; i<36; i++,latitude+=.17453) { pt2[i][0] = cos(latitude)*sin(longitude)*radius; pt2[i][1] = cos(longitude)*radius; pt2[i][2] = sin(latitude)*sin(longitude)*radius; } pt2[36][0] = pt2[0][0]; pt2[36][1] = pt2[0][1]; pt2[36][2] = pt2[0][2]; for (i=0; i<37; i++) { for (k=0; k<3; k++) rot_pts1[i][k] = rot_pts2[i][k]; vx1[i] = vx2[i]; vy1[i] = vy2[i]; } perspective(37,rot_pts2,pt2,vx2,vy2); for (i=0; i<36; i++) { color = shaded_surface_test(rot_pts1[i][0],rot_pts1[i][1], rot_pts1[i][2],rot_pts1[i+1][0],rot_pts1[i+1][1], rot_pts1[i+1][2],rot_pts2[i+1][0],rot_pts2[i+1][1], rot_pts2[i+1][2]); if (color<=999) { fillPoly(color,vx1[i],vy1[i],vx1[i+1], vy1[i+1],vx2[i+1],vy2[i+1], vx2[i],vy2[i],-999); } } } pt1[0][0] = 0; pt1[0][1] = -radius; pt1[0][2] = 0; perspective(1,rot_pts1,pt1,vx1,vy1); for (i=0; i<36; i++) { color = shaded_surface_test(rot_pts2[i][0],rot_pts2[i][1], rot_pts2[i][2],rot_pts2[i+1][0],rot_pts2[i+1][1], rot_pts2[i+1][2],rot_pts1[0][0],rot_pts1[0][1], rot_pts1[0][2]); if (color<=999) fillPoly(color,vx2[i+1],+vy2[i+1],vx2[i],+vy2[i], vx1[0],+vy1[0],-999); } } void perspective(int no_of_points,float rot_pts[][3],float pt[][3],int vx[], int vy[]) { int i,j; float xa,ya,za,x,y,z; for (i=0; i= 360) degrees -= 360; while (degrees < 0) degrees += 360; angle = rad_per_degree*degrees; return angle; } void projection (float x3, float y3, float z3, int color) { float temp_x, temp_y; int x,y; temp_x = x3*cos(x_angle) + y3*cos(y_angle) + z3*cos(z_angle); temp_y = x3*sin(x_angle) + y3*sin(y_angle) + z3*sin(z_angle); x = temp_x*horz_scale; y = temp_y*vert_scale; plot (x,y,color); } void wait(char title[]) { int tab,width; getMode(&width); tab = (width - strlen(title))/2; gotoxy(tab,0); writString(title,15,0); #ifdef VGA gotoxy((width-40)/2,29); #endif #ifndef VGA gotoxy((width-40)/2,24); #endif writString(" 'Ent' = next demo; other key = repeat..", 15,0); ch = getch(); cls(1); }