Category : C Source Code
Archive   : TC_3D.ZIP
Filename : TC-07.C

 
Output of file : TC-07.C contained in archive : TC_3D.ZIP
/* tc-07.c */
/* Demonstrates surface mapping of a 3d solid cube.
A two color design is draw on three sides of the carton */

/* ----------------------------------------------------------------------- */
/* INCLUDE FILES */
#include
#include
#include
#include
#include

/* ----------------------------------------------------------------------- */
/* DECLARATIONS */

float x=0.0,y=0.0,z=0.0; /* world coordinates */
float x1=0.0,x2=0.0,x3=0.0; /* polygon vertices */
float y01=0.0,y2=0.0,y3=0.0; /* polygon vertices */
float z1=0.0,z2=0.0,z3=0.0; /* polygon vertices */
float sx1=0.0,sx2=0.0,sx3=0.0,sx4=0.0,sx5=0.0; /* display coordinaes */
float sy1=0.0,sy2=0.0,sy3=0.0,sy4=0.0,sy5=0.0; /* display coordinates */
float sx=0.0,sy=0.0; /* output of 3d perspective formulas */
float xa=0.0,ya=0.0,za=0.0; /* temporary values in 3d formulas */
float d=1200.0; /* angular perspective value */
double r1=5.68319; /* yaw angle in radians */
double r2=6.28319; /* roll angle in radians */
double r3=5.79778; /* pitch angle in radians */
double sr1=0.0,sr2=0.0,sr3=0.0; /* sine rotation factors */
double cr1=0.0,cr2=0.0,cr3=0.0; /* cosine rotation factors */
float mx=0.0,my=0.0,mz=-450.0; /* viewpoint position */
int maxx=639,minx=0,maxy=199,miny=0; /* scaling viewport */
float screen_x=639,screen_y=199; /* dimensions of screen mode */
int C0=0,C1=1,C2=2,C3=3,C4=4,C5=5,C6=6,C7=7,C8=8, /* color varariables */
C9=9,C10=10,C11=11,C12=12,C13=13,C14=14,C15=15,
mode_flag=0;
float rx=0.0,ry=0.0; /* scaling values used in mapping routine */
int t=0,t1=0,t2=0; /* loop counters */
int p1=0; /* array indexer */
int key_matte_clr=6; /* exclusive key matte color */
int edge_clr=7; /* used to draw edges on models */
int solid_clr=2; /* used to fill surfaces on solid models */
int design_clr=1; /* used to draw the design on the model */
int polary[4][2]; /* my array - used in draw_poly */
float x_res,y_res; /* used for 2d mappinf from 640*480 template */
float sp=0.0; /* visibility factor in hidden surface routine */
float sp1=0.0,sp2=0.0,sp3=0.0; /* temporary values of sp */

/* database of vertices for 3d model */
int array1[][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};

/* database of fill origins for 3d model */
int array2[][3]={
0,-30,0, 0,0,-30, -30,0,0, 0,0,30, 30,0,0, 0,30,0};

/* database of vertices for lower design */
int array3[][3]={
10,3,30, 17,-15,30, 30,-10,30, 30,11,30, 30,10,-19,
30,28,-11};

/* database of vertices for upper design */
int array4[][3]={
5,15,30, 9,7,30, 30,15,30, 30,25,30, 30,30,18, 30,30,-7,
28,30,-10, 20,30,-6};

/* databse of fill seeds for designs */
int array5[][3]={
21,-2,30, 30,8,10, 18,15,30, 30,23,18, 26,30,-2};

float B1[8][3]; /* array of 8 sets of xyz points */
float B2[8][2]; /* array of 8 sets of sx,sy display coordinates */
float B3[6][2]; /* array of 6 sets of sx,sy fill coordinates */
float B4[6][2]; /* array of six sets display coordinates lower design */
float B5[8][2]; /* array of 8 sets of display coordinates upper design */
float B6[5][2]; /* array of 5 sets of fill seeds for design */

/* declare global subroutines */
void keyboard(void);void quit_pgm(void);void calc_3d(void);
void rotation(void);void window(void);void graphics_setup(void);
void coords(void);void draw_poly(void);void notice (int x,int y);
void visibility_test(void);void store_coords(void);
void draw_design(void);

/* ----------------------------------------------------------------------- */
/* MAIN ROUTINE */


main(){
graphics_setup();
setviewport(0,0,maxx,maxy,1);
key_matte_clr=C6;
edge_clr=C7;solid_clr=C3;
store_coords(); /* calculate and store 3d coords in arrays */

surface0:
x1=B1[7][0];y01=B1[7][1];z1=B1[7][2];x2=B1[0][0];y2=B1[0][1];
z2=B1[0][2];x3=B1[3][0];y3=B1[3][1];z3=B1[3][2];visibility_test();
if (sp>0) goto surface1;
sx1=B2[7][0];sy1=B2[7][1];sx2=B2[0][0];sy2=B2[0][1];sx3=B2[3][0];
sy3=B2[3][1];sx4=B2[6][0];sy4=B2[6][1];sx5=B3[0][0];sy5=B3[0][1];
draw_poly();

surface1:
x1=B1[6][0];y01=B1[6][1];z1=B1[6][2];x2=B1[5][0];y2=B1[5][1];
z2=B1[5][2];x3=B1[4][0];y3=B1[4][1];z3=B1[4][2];visibility_test();
if (sp>0) goto surface2;
sx1=B2[6][0];sy1=B2[6][1];sx2=B2[5][0];sy2=B2[5][1];sx3=B2[4][0];
sy3=B2[4][1];sx4=B2[7][0];sy4=B2[7][1];sx5=B3[1][0];sy5=B3[1][1];
draw_poly();

surface2:
x1=B1[3][0];y01=B1[3][1];z1=B1[3][2];x2=B1[2][0];y2=B1[2][1];
z2=B1[2][2];x3=B1[5][0];y3=B1[5][1];z3=B1[5][2];visibility_test();
if (sp>0) goto surface3;
sx1=B2[3][0];sy1=B2[3][1];sx2=B2[2][0];sy2=B2[2][1];sx3=B2[5][0];
sy3=B2[5][1];sx4=B2[6][0];sy4=B2[6][1];sx5=B3[2][0];sy5=B3[2][1];
draw_poly();

surface3:
x1=B1[0][0];y01=B1[0][1];z1=B1[0][2];x2=B1[1][0];y2=B1[1][1];
z2=B1[1][2];x3=B1[2][0];y3=B1[2][1];z3=B1[2][2];visibility_test();
if (sp>0) goto surface4;
sx1=B2[0][0];sy1=B2[0][1];sx2=B2[1][0];sy2=B2[1][1];sx3=B2[2][0];
sy3=B2[2][1];sx4=B2[3][0];sy4=B2[3][1];sx5=B3[3][0];sy5=B3[3][1];
draw_poly();
sx1=B4[0][0];sy1=B4[0][1];sx2=B4[1][0];sy2=B4[1][1];sx3=B4[2][0];
sy3=B4[2][1];sx4=B4[3][0];sy4=B4[3][1];sx5=B6[0][0];sy5=B6[0][1];
design_clr=C1;draw_design();
sx1=B5[0][0];sy1=B5[0][1];sx2=B5[1][0];sy2=B5[1][1];sx3=B5[2][0];
sy3=B5[2][1];sx4=B5[3][0];sy4=B5[3][1];sx5=B6[2][0];sy5=B6[2][1];
design_clr=C4;draw_design();

surface4:
x1=B1[7][0];y01=B1[7][1];z1=B1[7][2];x2=B1[4][0];y2=B1[4][1];
z2=B1[4][2];x3=B1[1][0];y3=B1[1][1];z3=B1[1][2];visibility_test();
if (sp>0) goto surface5;
sx1=B2[7][0];sy1=B2[7][1];sx2=B2[4][0];sy2=B2[4][1];sx3=B2[1][0];
sy3=B2[1][1];sx4=B2[0][0];sy4=B2[0][1];sx5=B3[4][0];sy5=B3[4][1];
draw_poly();
sx1=B4[3][0];sy1=B4[3][1];sx2=B4[2][0];sy2=B4[2][1];sx3=B4[4][0];
sy3=B4[4][1];sx4=B4[5][0];sy4=B4[5][1];sx5=B6[1][0];sy5=B6[1][1];
design_clr=C1;draw_design();
sx1=B5[3][0];sy1=B5[3][1];sx2=B5[2][0];sy2=B5[2][1];sx3=B5[5][0];
sy3=B5[5][1];sx4=B5[4][0];sy4=B5[4][1];sx5=B6[3][0];sy5=B6[3][1];
design_clr=C4;draw_design();

surface5:
x1=B1[1][0];y01=B1[1][1];z1=B1[1][2];x2=B1[4][0];y2=B1[4][1];
z2=B1[4][2];x3=B1[5][0];y3=B1[5][1];z3=B1[5][2];visibility_test();
if (sp>0) goto surfaces_done;
sx1=B2[1][0];sy1=B2[1][1];sx2=B2[4][0];sy2=B2[4][1];sx3=B2[5][0];
sy3=B2[5][1];sx4=B2[2][0];sy4=B2[2][1];sx5=B3[5][0];sy5=B3[5][1];
draw_poly();
sx1=B5[4][0];sy1=B5[4][1];sx2=B5[5][0];sy2=B5[5][1];sx3=B5[6][0];
sy3=B5[6][1];sx4=B5[7][0];sy4=B5[7][1];sx5=B6[4][0];sy5=B6[4][1];
design_clr=C4;draw_design();

surfaces_done:setcolor(C7);notice(0,0);
for (t1=1;t1!=2;) keyboard();
quit_pgm();}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: 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;}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: STANDARD 3D FORMULAS */
/* Pass: x,y,z cartesian world coordinates.
Returns: sx,sy cartesian display coordinates.
x,y,z catesian view coordinates */

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*za-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;}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: CALCULATE & STORE VIEW COORDINATES & DISPLAY COORDINATES */

void store_coords(void){
rotation();
for (t=0;t<=7;t++){ /* store view coordinates */
x=array1[t][0];y=array1[t][1];z=array1[t][2];
calc_3d();window();
B1[t][0]=x;B1[t][1]=y;B1[t][2]=z;
B2[t][0]=sx;B2[t][1]=sy;
};

for (t=0;t<=5;t++){ /* store fill coordinates */
x=array2[t][2];y=array1[t][1];z=array2[t][2];
calc_3d();window();
B3[t][0]=sx;B3[t][1]=sy;
};

for (t=0;t<=5;t++){
x=array3[t][0];y=array3[t][1];z=array3[t][2];
calc_3d();window();
B4[t][0]=sx;B4[t][1]=sy; /* store lower design display coordinates */
};

for (t=0;t<=7;t++){
x=array4[t][0];y=array4[t][1];z=array4[t][2];
calc_3d();window();
B5[t][0]=sx;B5[t][1]=sy; /* store upper design display coordinates */
};

for (t=0;t<=4;t++){
x=array5[t][0];y=array5[t][1];z=array5[t][2];
calc_3d();window();
B6[t][0]=sx;B6[t][1]=sy; /* store fill display coordinates */
};

return;}

/* ----------------------------------------------------------------------- */
/* HIDDEN SURFACE VISIBILTY TEST */
void visibility_test(void){
sp1=x1*(y2*z3-y3*z2);sp1=(-1)*sp1;sp2=x2*(y3*z1-y01*z3);
sp3=x3*(y01*z2-y2*z1);sp=sp1-sp2-sp3;return;}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: DRAW 4-SIDED SOLID POLYGON IN 3D SPACE */

void draw_poly(void){
setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
setfillstyle(SOLID_FILL,C0);
setcolor(C9);

polary[0][0]=sx2;polary[0][1]=sy2;polary[1][0]=sx3;polary[1][1]=sy3;
polary[2][0]=sx4;polary[2][1]=sy4;polary[3][0]=sx1;polary[3][1]=sy1;

fillpoly (4,(int far*) polary);
return;}

/* ----------------------------------------------------------------------- */
void draw_design(void){
setlinestyle(USERBIT_LINE,0xffff,NORM_WIDTH);
/* setcolor (key_matte_clr);*/
polary[0][0]=sx2;polary[0][1]=sy2;polary[1][0]=sx3;polary[1][1]=sy3;
polary[2][0]=sx4;polary[2][1]=sy4;polary[3][0]=sx1;polary[3][1]=sy1;
/* setfillstyle(SOLID_FILL,key_matte_clr);*/
/* fillpoly (4,(int far*) polary); */
setcolor(C9);
setfillstyle(SOLID_FILL,design_clr);
fillpoly (4,(int far*) polary);
return;}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: 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;}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: CHACK THE KEYBOARD BUFFER */
void keyboard(void){
if (bioskey(1)==0) return; else quit_pgm();}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: GRACEFUL EXIT FROM PROGRAM */

void quit_pgm(void){
cleardevice();restorecrtmode();exit(0);}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: VGA/EGA/MCGA/CGA COMPATIBILITY MODULE */

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

VGA_mode:
graphics_adapter=VGA;graphics_mode=VGAHI;
initgraph(&graphics_adapter,&graphics_mode,"");
x_res=640;y_res=480;mode_flag=1;
maxx=639;minx=0;maxy=479;miny=0;screen_x=639;screen_y=479;
setcolor(7);moveto(0,472);
outtext("Revisions by A. Helder");
moveto(472,472);
outtext("Press any key to quit");
moveto(144,0);
outtext("USING C TO MAP DESIGNS ONTO A SOLID 3D MODEL");
return;

EGA_ECD_mode:
graphics_adapter=EGA;graphics_mode=EGAHI;
initgraph(&graphics_adapter,&graphics_mode,"");
x_res=640;y_res=350;mode_flag=2;
maxx=639;minx=0;maxy=349;miny=0;screen_x=639;screen_y=349;
setcolor(7);moveto(0,342);
outtext("Revisions by A. Helder");
moveto(472,342);
outtext ("Press any key to quit");
moveto(144,0);
outtext("USING C TO MAP DESIGNS ONTO A SOLID 3D MODEL");
return;

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;
setcolor(7);moveto(0,192);
outtext("Revisions by A. Helder");
moveto(472,192);
outtext("PRESS ANY KEY TO QUIT");
moveto(144,0);
outtext("USING C TO MAP DESIGNS ONTO A SOLID 3D MODEL");
return;

CGA_mode:
graphics_adapter=CGA;graphics_mode=CGAC3;
initgraph(&graphics_adapter,&graphics_mode,"");
x_res=320;y_res=200;mode_flag=1;C7=3;
maxx=319;minx=0;maxy=199;miny=0;screen_x=319;screen_y=199;
C0=0;C1=1;C2=1;C3=1;C4=2;C5=1;C6=2;C7=3;C8=1;C9=3;C10=3;
C11=3,C12=3,C13=3;C14=3;C15=3;
setcolor(3);moveto(48,192);
outtext("Revisions by A. Helder");
moveto(104,0);
outtext ("SURFACE MAPPING");
return;

abort_message:
printf("\n\nUnable to proceed - Requires VGA,EGA,CGA or MCGA adapter");
printf("\nWith appropriate monitor");
exit(0);
}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: MAP 640*480 TEMPLATE TO 2D SCREEN */
void coords(void)
{
sx=sx*(x_res/640);sy=sy*(y_res/480);return;
}

/* ----------------------------------------------------------------------- */
/* SUBROUTINE: 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++;
};
setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
return;}



  3 Responses to “Category : C Source Code
Archive   : TC_3D.ZIP
Filename : TC-07.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/