Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!security!genrad!decvax!decwrl!flairvax!turtleva!ken From: ken@turtleva.UUCP (Ken Turkowski) Newsgroups: net.sources Subject: Graphics source in C: hsalgs/poly_drawl.c Message-ID: <286@turtleva.UUCP> Date: Thu, 15-Dec-83 23:41:27 EST Article-I.D.: turtleva.286 Posted: Thu Dec 15 23:41:27 1983 Date-Received: Sun, 18-Dec-83 06:22:48 EST Lines: 332 echo x - hsalgs/poly_drawl.c cat >hsalgs/poly_drawl.c <<'!Funky!Stuff!' /* -------------------------------------------------------------- Clip, and display, as lines, polygonal objects in the standard binary format. input format is defined in "man hsalg_input" --------------------------------------------------------------- */ #include #include #define TRUE 1 #define FALSE 0 #define NULLCHAR '\0' #define DtoR 3.14159 / 180. #define DET_CODE 0x20746564 #define IN 0 #define OUTLFT 1 #define OUTRGT 2 #define OUTTOP 4 #define OUTBOT 8 #define OUTHTR 16 #define POLYSIZE 64 #define MAXPTS 32768 /* must be <= 32768 indexed by shorts */ #define MAXPOLYS 32768 /* " */ #define MAXVTCES 131072 #define sqr(x) ((x)*(x)) #define LINE_LENGTH 81 static struct {float x,y,z;} pts[MAXPTS]; static short npts,npolys,nvtces,clipping,backfaces; static short cnt[MAXPTS],vtces[MAXVTCES]; static short bfacing[MAXPOLYS],clp[MAXPOLYS]; static double matrix[16]; /* current object transform matrix */ static double cot_x,cot_y; /* +++++++++++++++++++++++++ MAIN +++++++++++++++++++++++++++++++++++++++++ */ main() { char instrg[LINE_LENGTH],keywd[LINE_LENGTH],remainder[LINE_LENGTH],dvc[3]; char *eofchk,*gets(); short i,first_obj; long ivtx; double atof(),sqrt(),fabs(); FILE *popen(),*stream; nocore(); /* prevent dumping core on error */ clipping = TRUE; backfaces = FALSE; first_obj = TRUE; /* scan input for keywords */ do { eofchk = gets(instrg); if (eofchk != NULL) get_term(instrg,keywd,remainder); /* get keyword */ if ((eofchk == NULL) || ((strcmp(keywd,"object") == 0) && (!first_obj))) { prepare_obj(); /* transform, etc. last object if end or next obj */ /* ------ run through polygons, if not rejected, put out move and draw commands ------------------ */ ivtx = 0; for (i=0; i= 0) && (!bfacing[i] || backfaces))/*rejected?*/ { struct { double x,y,z; } poly [POLYSIZE],poly2[POLYSIZE]; short k,m,npts; npts = size; if (npts > POLYSIZE) error("(poly_drawl) %d > POLYSIZE\n",npts); for (k=0; k 0) polclp(0,&npts,poly,poly2); if (npts > 2) { for (k=0; k 4) { get_term(string,keywd,string); if (strcmp(keywd,"open") == 0) backfaces = TRUE; } } } fclose(input); /* close ".obj" file and open detail file */ input = fopen(fname,"r"); if (input == NULL) error("(poly_drawl) can't open .det file %s\n",fname); fread(&code,4,1,input); /* read file type header */ if (code != DET_CODE) { fprintf(stderr,"poly_drawl: %s notted tagged detail file\n",fname); close(input); return; /* apparently not a detail file, give up */ } fread(npts,2,1,input); fread(npolys,2,1,input); if ((*npts > MAXPTS) || (*npolys > MAXPOLYS)) error(" (poly_drawl) object too big!! %d > MAXPTS or %d > MAXPOLYS\n", *npts,*npolys); fread(pts,4,(*npts)*3,input); j = 0; for (i=0; i<*npolys; i++) { short num; long k; fread(&num,2,1,input); vtces[j++] = num; if (fread(&vtces[j],2,num,input) < num) error(" bad polygon # %d\n",i); for (k=j; k= 0.0) /* copy point if inside */ { pt2[m].x = pts[k].x; pt2[m].y = pts[k].y; pt2[m].z = pts[k].z; m++; } lk = k; last_dist = dist; } /* recurse for next plane */ *npts = m; polclp(++pass,npts,pt2,pts); } /* ++++++++++++++++++++++ PREPARE_OBJ ++++++++++++++++++++++++++++++++++ */ prepare_obj() /* transform vertices, compute normals, etc. */ { short i; /* ----- run through points, transform ----- */ for (i=0; i pts[i].z) cnt[i] |= OUTRGT; if ((pts[i].y)*cot_y < -pts[i].z) cnt[i] |= OUTBOT; if ((pts[i].y)*cot_y > pts[i].z) cnt[i] |= OUTTOP; } /* ------------- run through polygons, check backfacing, do trivial clip tests and tag polys -------------- */ { short in,out; long ivtx; ivtx = in = out = 0; for (i=0; i= 0.0) bfacing[i] = TRUE; /* backfacing */ else bfacing[i] = FALSE; } ivtx += size; /* increment to next poly */ } } } /* +++++++++++++++++++++++++ TRANSFORM ++++++++++++++++++++++++++++++++++++ */ transform(orgpt,mtx,pt) /* apply transform to point */ struct { float x,y,z; } *orgpt,*pt; double mtx[16]; { float tx,ty,tz; tx = mtx[0]*orgpt->x + mtx[4]*orgpt->y + mtx[8]*orgpt->z + mtx[12]; ty = mtx[1]*orgpt->x + mtx[5]*orgpt->y + mtx[9]*orgpt->z + mtx[13]; tz = mtx[2]*orgpt->x + mtx[6]*orgpt->y + mtx[10]*orgpt->z + mtx[14]; pt->x = tx; pt->y = ty; pt->z = tz; } /* ++++++++++++++++++++++++++ X_PROD +++++++++++++++++++++++++++++++++++ */ X_prod(vec,pt1,pt2,pt3) /* vector cross-product */ struct { float x,y,z; } *vec,pt1,pt2,pt3; { pt1.x = pt2.x - pt1.x; pt1.y = pt2.y - pt1.y; pt1.z = pt2.z - pt1.z; pt2.x = pt3.x - pt2.x; pt2.y = pt3.y - pt2.y; pt2.z = pt3.z - pt2.z; vec->x = pt1.y*pt2.z - pt1.z*pt2.y; vec->y = pt1.z*pt2.x - pt1.x*pt2.z; vec->z = pt1.x*pt2.y - pt1.y*pt2.x; } !Funky!Stuff!