All pastes #26433 Raw Edit

Untitled

public text v1 · immutable
#26433 ·published 2005-10-24 02:36 UTC
rendered paste body
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; y<outrow; y++){
      for (x=0; x<outcol; x++){
         make_pixel(model, x, y, pixmap);
      }
   }
         

   /* Write header for new file and the new image */
   int brightness = 255;
   fprintf(model->outFP, "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];
}