Category : C Source Code
Archive   : CADSRC.ZIP
Filename : T-14.C

 
Output of file : T-14.C contained in archive : CADSRC.ZIP
/*__________________________________________________________________

t-14.c

FUNCTION: Demonstrates high speed frame animation of
a 3D intuitively shaded object.

Compatibility: Supports VGA/EGA graphics adapters and monitors
using 640x200, 16 color mode.

NOTE: You need the *.BGI files that come with Turbo-C for this to run.

Also note... the pre-sort method of hidden surface removal is used.
___________________________________________________________________

INCLUDE FILES */

#include
#include
#include
#include
#include

/*________________________________________________________________
DECLARATIONS */

void graphics_setup(void);void keyboard(void);void quit_pgm(void);
void notice(int x, int y);void coords(void);
void labels(void); void draw_object(void);
void calc_3D(void); void rotation(void); void window(void);
void get_coords(void); void draw_poly(void);
void animVGAEGA(void);

int t1=1,t2=2,t3=1, p1=0,p2=0;
float x_res, y_res;
int C0=0,C1=1,C2=2,C3=3,C4=4,C5=5,C6=6,C7=7,C8=8,C9=9,C10=10,
C11=11,C12=12,C13=13,C14=14,C15=15,mode_flag=0;
int CLR,CLR1,CLR2,CLR3,EDGE;

float x=0.0,y=0.0,z=0.0;
float sx=0,sy=0;
float xa=0,ya=0,za=0;
float sxa=0,sya=0,sxb=0,syb=0;
float sxs=0.0,sys=0.0;
float d=1200.0;

double r1=0.48539;
double r2=6.28319;
double r3=5.79778;
double sr1=0,sr2=0,sr3=0;
double cr1=0,cr2=0,cr3=0;
float mx=0,my=-5.0,mz=-250.0;
int maxx=639,minx=0,maxy=199,miny=0;
float screen_x=639,screen_y=199;
float rx=0,ry=0;

int array_xyz[][3]= {
20,-20,-30, 20, 20,-30, 20, 20,-20, 20,-20,-20, 20,-20,-30, 20, 0,-25,
20,-20,-30, -20,-20,-30, -20,-20,-20, 20,-20,-20, 20,-20,-30, 0,-20,-25,

-30, 30,-30, 30, 30,-30, 30, 30,-20, -30, 30,-20, -30, 30,-30, 0, 30,-25,
-30, 30,-30, -30, 30,-20, -30,-30,-20, -30,-30,-30, -30, 30,-30, -30, 0,-25,
-30, 30,-20, 30, 30,-20, 30, 20,-20, -30, 20,-20, -30, 30,-20, 0, 25,-20,
30, 30,-20, 30,-30,-20, 20,-30,-20, 20, 30,-20, 30, 30,-20, 25, 0,-20,
30,-30,-20, -30,-30,-20, -30,-20,-20, 30,-20,-20, 30,-30,-20, 0,-25,-20,
-30,-30,-20, -30, 30,-20, -20, 30,-20, -20,-30,-20, -30,-30,-20, -25, 0,-20,

20,-30,-20, 20,-20,-20, 20,-20, 20, 20,-30, 20, 20,-30,-20, 20,-25, 0,

-30,-30, 30, -30,-20, 30, 30,-20, 30, 30,-30, 30, -30,-30, 30, 0,-25, 30,
-30,-30,-30, -30,-20,-30, -30,-20, 30, -30,-30, 30, -30,-30,-30, -30,-25, 0,
-30,-20,-20, -20,-20,-20, -20,-20, 30, -30,-20, 30, -30,-20,-20, -25,-20, 0,
30,-20,-20, 30,-20, 30, 20,-20, 30, 20,-20,-20, 30,-20,-20, 25,-20, 0,
-30,-20, 30, -30,-20, 20, 30,-20, 20, 30,-20, 30, -30,-20, 30, 0,-20, 25,

20, 30,-20, 20, 30, 30, 20, 20, 30, 20, 20,-20, 20, 30,-20, 20, 25, 0,
20, 30, 30, 20,-20, 30, 20,-20, 20, 20, 30, 20, 20, 30, 30, 20, 0, 25,

-30, 30,-30, -20, 30,-30, -20, 30, 30, -30, 30, 30, -30, 30,-30, -25, 30, 0,
20, 30,-30, 30, 30,-30, 30, 30, 30, 20, 30, 30, 20, 30,-30, 25, 30, 0,
-30, 30, 30, -30, 30, 20, 30, 30, 20, 30, 30, 30, -30, 30, 30, 0, 30, 25,

-30,-30, 30, -30, 30, 30, -20, 30, 30, -20,-30, 30, -30,-30, 30, -25, 0, 30,
20,-30, 30, 20, 30, 30, 30, 30, 30, 30,-30, 30, 20,-30, 30, 25, 0, 30,
-30, 30, 30, 30, 30, 30, 30, 20, 30, -30, 20, 30, -30, 30, 30, 0, 25, 30,
-30, 20,-30, -30, 30,-30, -30, 30, 30, -30, 20, 30, -30, 20,-30, -30, 25, 0,
-30,-30, 20, -30, 30, 20, -30, 30, 30, -30,-30, 30, -30,-30, 20, -30, 0, 25
};

/*___________________________________________________________

MAIN ROUTINE */

main()
{
graphics_setup();
setviewport(0,0,maxx,maxy,1);
CLR1=C7;CLR2=C9;CLR3=C1;EDGE=C1;

setvisualpage(0);setactivepage(0);cleardevice();
r1=0.58539;r2=6.28319;r3=5.79778;labels();draw_object();keyboard();

setvisualpage(1);setactivepage(1);cleardevice();
r1=r1+.08727;r2=6.28319;r3=5.79778;labels();draw_object();keyboard();

setvisualpage(2);setactivepage(2);cleardevice();
r1=r1+.08727;r2=6.28319;r3=5.79778;labels();draw_object();keyboard();

setvisualpage(3);setactivepage(3);cleardevice();
r1=r1+.08727;r2=6.28319;r3=5.79778;labels();draw_object();keyboard();

setvisualpage(0);setactivepage(0);
for (t1=1;t1<=30000;t1++);

animVGAEGA();
quit_pgm();
}

/*_________________________________________________________________

FUNCTION: display the alphanumeric labels
*/
void labels(void)
{
setcolor(C7);moveto(0,192);
outtext("640x200 16-color VGA + EGA mode");
moveto(168,0); setcolor(C12);
outtext("USING TURBO C FOR HIGH SPEED FRAME ANIMATION");
moveto(464,192);outtext("Press any key to stop.");
setcolor(C14);moveto(176,16);
outtext("Animated rotation of solid 3D model");
setcolor(C7);moveto(0,40);
return;
}

/*_________________________________________________________________

FUNCTION:
*/
void draw_object(void)
{
rotation();
p1=0;

CLR=CLR3;EDGE=CLR3;draw_poly();
CLR=CLR1;EDGE=CLR1;draw_poly();

draw_poly();
CLR=CLR3;EDGE=CLR3;draw_poly();
CLR=CLR2;EDGE=CLR2;for (t3=1;t3<=4;t3++) {draw_poly();}

CLR=CLR3;EDGE=CLR3;draw_poly();

CLR=CLR2;EDGE=CLR2;draw_poly();
CLR=CLR3;EDGE=CLR3;draw_poly();
CLR=CLR1;EDGE=CLR1;for (t3=1;t3<=3;t3++) {draw_poly();}

CLR=CLR3;EDGE=CLR3;for (t3=1;t3<=2;t3++) {draw_poly();}
CLR=CLR1;EDGE=CLR1;for (t3=1;t3<=3;t3++) {draw_poly();}
CLR=CLR2;EDGE=CLR2;for (t3=1;t3<=3;t3++) {draw_poly();}
CLR=CLR3;EDGE=CLR3;for (t3=1;t3<=2;t3++) {draw_poly();}

setcolor(C7);sx=5;sy=400;coords();notice(sx,sy);
return;
}

/*_________________________________________________________________

FUNCTION: draw and fill polygon in 3D space.
*/
void draw_poly(void)
{
setcolor(C13);
setfillstyle(SOLID_FILL,C13);
get_coords();
calc_3D();
window();
sxa=sx;sya=sy;

for (t1=1;t1<=4;t1++)
{
get_coords();
calc_3D();
window();
sxs=sx;sys=sy;sxb=sx;syb=sy;
moveto(sxa,sya);lineto(sxb,syb);
sxa=sxs;sya=sys;
}
get_coords();
calc_3D();
window();
floodfill(sx,sy,C13);

setcolor(CLR);

setfillstyle(SOLID_FILL,CLR);
EDGE=CLR;
p1=p1-6;
get_coords(); calc_3D();window();sxa=sx;sya=sy;
for (t1=1;t1<=4;t1++)
{
get_coords();
calc_3D();
window();
sxs=sx;sys=sy;sxb=sx;syb=sy;
moveto(sxa,sya);lineto(sxb,syb);
sxa=sxs;sya=sys;
}
get_coords();
calc_3D();
window();
floodfill(sx,sy,EDGE);
return;
}

/*_________________________________________________________________

FUNCTION: frame animation manager for VGA + EGA
*/
void animVGAEGA(void)
{
for (t1=1;t1!=2;)
{
setvisualpage(1);for (t2=1;t2<=2500;t2++);
setvisualpage(2);for (t2=1;t2<=2500;t2++);
setvisualpage(3);for (t2=1;t2<=30000;t2++);keyboard();
setvisualpage(2);for (t2=1;t2<=2500;t2++);
setvisualpage(1);for (t2=1;t2<=2500;t2++);
setvisualpage(0);for (t2=1;t2<=30000;t2++);keyboard();
setvisualpage(1);for (t2=1;t2<=2500;t2++);
setvisualpage(2);for (t2=1;t2<=2500;t2++);
setvisualpage(3);for (t2=1;t2<=30000;t2++);keyboard();
setvisualpage(2);for (t2=1;t2<=2500;t2++);
setvisualpage(1);for (t2=1;t2<=2500;t2++);
setvisualpage(0);for (t2=1;t2<=30000;t2++);keyboard();
}
return;
}

/*_________________________________________________________________

FUNCTION: retrieve xyz world coordinates from database
*/
void get_coords(void)
{
x=array_xyz[p1][0];y=array_xyz[p1][1];z=array_xyz[p1][2];
p1++;
return;
}

/*_________________________________________________________________

FUNCTION: calculate SIN,COS factors
*/
void rotation(void)
{
sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);cr1=cos(r1);cr2=cos(r2);
cr3=cos(r3);
return;
}

/*_________________________________________________________________

FUNCTION: standard 3D formulas
*/
void calc_3D(void)
{
x=(-1)*x;xa=cr1*x-sr1*z;za=sr1*x+cr1*z;x=cr2*xa+sr2*y;
ya=cr2*y-sr2*xa;z=cr3*-sr3*ya;y=sr3*za+cr3*ya;x=x+mx;y=y+my;
z=z+mz;sx=d*x/z;sy=d*y/z;
return;
}

/*_________________________________________________________________

FUNCTION: map cartesian coords to physical screen coords.
*/
void window(void)
{
sx=sx+399;sy=sy+299;rx=screen_x/799;ry=screen_y/599;sx=sx*rx;
sy=sy*ry;
return;
}


/*____________________________________________________________________

FUNCTION: press any key to quit... */

void keyboard(void)
{
if (bioskey(1) == 0) return; else quit_pgm();
}

/*____________________________________________________________________

FUNCTION: graceful exit from the program */

void quit_pgm(void)
{
if (mode_flag == 3)
{
setvisualpage(0);setactivepage(0);
}
cleardevice(); restorecrtmode();exit(0);

}
/*___________________________________________________________________
FUNCTION: vga/ega/cga/mcga compatibility module */

void graphics_setup(void)
{
int graphics_adapter,graphics_mode;
detectgraph(&graphics_adapter,&graphics_mode);
if (graphics_adapter==VGA) goto EGA_SCD_mode;
if (graphics_mode==EGAHI) goto EGA_SCD_mode;
if (graphics_mode==EGALO) goto EGA_SCD_mode;
if (graphics_adapter==CGA) goto abort_message;
if (graphics_adapter==MCGA) goto abort_message;
goto abort_message;

EGA_SCD_mode:
graphics_adapter=EGA;graphics_mode=EGALO;
initgraph(&graphics_adapter,&graphics_mode,"");
x_res=640;y_res=200;mode_flag=3;
maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
return;

abort_message:
printf("\n\nUnable to proceed.\n");
printf("Requires VGA or EGA adapter\n");
printf(" with appropriate monitor.\n");
printf("Please refer to LEE ADAMS book HIGH-Perforamnce CAD Graphics in TURBO C.\n\n");
exit(0);
}
/*__________________________________________________________
FUNCTION: coords */
void coords(void)
{
sx=sx*(x_res/640);sy=sy*(y_res/480);return;
}

/*__________________________________________________________
FUNCTION: copyright notice */

int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};

void notice(int x, int y)
{
int a,b,c; int t1=0;
for (t1=0;t1<=6;t1++){a=copyright[t1][0];b=copyright[t1][1];
c=copyright[t1][2];
setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
moveto(x,y);lineto(x+15,y);
setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
moveto(x+16,y);lineto(x+31,y);
setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
moveto(x+32,y);lineto(x+47,y);y=y+1;
}
setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
return;
}
/* END */



  3 Responses to “Category : C Source Code
Archive   : CADSRC.ZIP
Filename : T-14.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/