main.c: #include "ray.h" int main(int argc, char *argv[]){ FILE *inFP; FILE *outputFP; model_t *model = (model_t *)malloc(sizeof(model_t)); int rc; double outcol; double outrow; /* Check command line arguments */ if (argc != 5){ fprintf(stderr, "Usage: ./raytracer1 worldDataFile outputPPMFile columns rows\n"); exit(1); } /* Open the output ppm file */ if ((inFP = fopen(argv[1], "r")) == NULL) { perror("Data File Open Error"); exit(1); } if ((outputFP = fopen(argv[2], "w")) == NULL) { perror("Output PPM Open Error"); exit(1); } if ((outcol=atoi(argv[3]))<= 0){ perror("Invalid columns"); exit(1); } if ((outrow=atoi(argv[4]))<= 0){ perror("Invalid rows"); exit(1); } model->proj = projection_init(inFP, outcol, outrow); projection_dump(model->proj); model->lights = list_init(); model->scene = list_init(); rc = model_init(outputFP, model); model_dump(stderr, model); if(rc ==0) make_image(model); return 0; } image.c: #include "ray.h" void make_image(model_t *model){ unsigned char *pixmap; int x = model->proj->win_size_pixel[0]; int y = model->proj->win_size_pixel[1]; int size = x * y * 3; pixmap = malloc(size); int outcol = x; int outrow = y; /* Walk through and create image */ for (y=0; youtFP, "P6 %d %d %d\n", outcol, outrow, brightness); fwrite(pixmap, 1, size, model->outFP); } void make_pixel(model_t *model, int x, int y, unsigned char *pixval){ double world[3]; double intensity[3]; double dir[3]; int i=0; int cols = model->proj->win_size_pixel[0]; int rows = model->proj->win_size_pixel[1]; map_pix_to_world(model->proj,x, rows-1-y, world); intensity[0] = 0.0; intensity[1] = 0.0; intensity[2] = 0.0; vl_diff3(model->proj->view_point,world,dir); vl_unitvec3(dir,dir); ray_trace(model, model->proj->view_point, dir, intensity, 0.0, NULL); for(i=0;i<3;i++) if(intensity[i] > 1.0) intensity[i] = 1.0; *(pixval + y*3*cols + x*3 + 0) = 255 * intensity[0]; *(pixval + y*3*cols + x*3 + 1) = 255 * intensity[1]; *(pixval + y*3*cols + x*3 + 2) = 255 * intensity[2]; } model.c: #include "ray.h" /**/ int model_init( FILE *in, model_t *model) { FILE *fp; char buf[256]; char fileName[128]; int objtype; obj_t *new; /* now load the objects in the scene */ while (fscanf(in, "%d %128s", &objtype, fileName) == 2) { fgets(buf, 256, in); /* consume rest of line */ if ((fp=fopen(fileName, "r")) == NULL) { perror("Object file open error"); fprintf(stderr, "Error: cannot open object file %s\n", fileName); exit(2); } switch (objtype) { case SPHERE: new = sphere_init(fp, objtype); break; case PLANE: new = plane_init(fp, objtype); break; default: fprintf(stderr, "Bad code %d \n", objtype); exit(1); } fclose(fp); if (new == NULL) { fprintf(stderr, "failed to load type %d \n", objtype); model_dump(stderr, model); exit(2); } else { list_add(model->scene, new); } } return(0); } /**/ void model_dump( FILE *out, model_t *model) { obj_t *obj; obj = model->scene->head; while (obj != NULL) { switch (obj->objtype) { case SPHERE: sphere_dump(obj); break; case PLANE: plane_dump(obj); break; default: fprintf(stderr, "Bad code %d \n", obj->objtype); exit(1); } obj = obj->next; } }