/*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+---*/ /* sample.c */ /* RayTracer Sample File */ /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+---*/ #include #include #include #include #include "sample.h" #define SUCCESS (0) #define FAILURE (!SUCCESS) #define ERROR (-1) #define MAX_OBJ 100 #define MAX_DEPTH 10 main(argc, argv) int argc; char **argv; { FILE *fpin; char com[80], sf[80]; int num_obj = 0; Object obj[MAX_OBJ], *op; Light lig; Color back; Camera cam; Image img; if(argc != 2) { fprintf(stderr, " Usage: %s [file]\n", argv[0]); exit FAILURE; } /* open input file */ if ((fpin = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Error opening file: %s\n", argv[1]); exit FAILURE; } for (EVER) { if(fscanf(fpin, "%s", com) == EOF) exit FAILURE; if (!strcmp(com, "sphe")) { op = &obj[num_obj]; fscanf(fpin, "%f %f %f %f", &op->p.x, &op->p.y, &op->p.z, &op->r); num_obj++; } else if (!strcmp(com, "opaq")) { fscanf(fpin, "%f %f %f %f %d", &op->m.co.r, &op->m.co.g, &op->m.co.b, &op->m.ks, &op->m.np); op->m.type = OPAQUE; op->m.kt = 0.; } else if (!strcmp(com, "mirr")) { op->m.type = MIRROR; } else if (!strcmp(com, "trpa")) { fscanf(fpin, "%f %f %f %d", &op->m.kt, &op->m.ir, &op->m.ks, &op->m.np); op->m.type = TRANSPARENT; } else if (!strcmp(com, "back")) { fscanf(fpin, "%f %f %f", &back.r, &back.g, &back.b); } else if (!strcmp(com, "plig")) { fscanf(fpin, "%f %f %f %f %f %f", &lig.ld.x, &lig.ld.y, &lig.ld.z, &lig.cl.r, &lig.cl.g, &lig.cl.b); normalize(&lig.ld); } else if (!strcmp(com, "elig")) { fscanf(fpin, "%f", &lig.ke); } else if (!strcmp(com, "eyep")) { fscanf(fpin, "%f %f %f", &cam.xv, &cam.yv, &cam.zv); } else if (!strcmp(com, "refp")) { fscanf(fpin, "%f %f %f", &cam.xf, &cam.yf, &cam.zf); } else if (!strcmp(com, "vang")) { fscanf(fpin, "%f %f", &cam.ax, &cam.ay); } else if (!strcmp(com, "size")) { fscanf(fpin, "%d", &img.hd.width); } else if (!strcmp(com, "rend")) { fscanf(fpin, "%s", sf); raytrace(&cam, obj, num_obj, &lig, &back, &img, sf); } else if (!strcmp(com, "quit")) { close(fpin); exit SUCCESS; } else { fprintf(stderr, " Command Error: %s\n", com); } } } void raytrace(cam, obj, num_obj, lig, back, img, sf) Camera *cam; Object *obj; int num_obj; Light *lig; Color *back; Image *img; char *sf; { int i, j; Ray ray; Color cl; int ir, ig, ib; Pixel *pix; int depth = 0; /* cut */ pix = img->pix; for(i=0; ihd.height; i++) { for(j=0; jhd.width; j++) { /* cut */ ray_trace(obj, num_obj, &ray, lig, back, &cl, depth); ir = min(cl.r, 255.); ig = min(cl.g, 255.); ib = min(cl.b, 255.); pix->r = (unsigned char)ir; pix->g = (unsigned char)ig; pix->b = (unsigned char)ib; pix++; } } imgout(img, sf); } void set_pers_mat(cam) Camera *cam; { } void set_screen(cam, img, xs, ys, dx, dy) Camera *cam; Image *img; float *xs, *ys, *dx, *dy; { } void set_image_header(img) Image *img; { Header *h; h = &img->hd; h->magic = 0x59a66a95; h->depth = 24; h->length = 3 * h->width * h->height; h->type = 1; h->maptype = 0; h->maplength = 0; if((img->pix = (Pixel *) malloc (h->length * sizeof(Pixel))) == NULL) { fprintf(stderr, " Memory Allocation Error\n"); exit FAILURE; } } void ray_trace(obj, num_obj, ray, lig, back, cl, depth) Object *obj; int num_obj; Ray *ray; Light *lig; Color *back, *cl; int depth; { } Object* intersect(obj, num_obj, ray, pl) Object *obj; int num_obj; Ray *ray; Plane *pl; { } void imgout(img, sf) Image *img; char *sf; { FILE *fpout; char dum = 0x0; /* open output file */ if ((fpout = fopen(sf, "w")) == NULL) { fprintf(stderr, "Error opening file: %s\n", sf); exit FAILURE; } fwrite(&img->hd, sizeof(Header), 1, fpout); fwrite(img->pix, img->hd.length, 1, fpout); close(fpout); } void normalize(vec) Point3D *vec; { } float iproduct(v1, v2) Point3D *v1, *v2; { } float powern(f1, n) float f1; int n; { } void mult_mat3D(mtm, mt1, mt2) Matrix3D mtm, mt1, mt2; { } void mult_vec_mat3D(vec, vc, mt) Point3D *vec, *vc; Matrix3D mt; { } void copy_mat3D(mt1, mt2) Matrix3D mt1, mt2; { } void mult_vec_mat2D(vec, vc, mt) Point2D *vec; Point3D *vc; Matrix2D mt; { } void mult_mat2D(mtm, mt1, mt2) Matrix2D mtm, mt1, mt2; { } void copy_mat2D(mt1, mt2) Matrix2D mt1, mt2; { } /*** EOF ***/