CPSC 102 Computer Science II
(Advanced C Programming)

Assignment 2

Objectives:

To implement and exercise the camera object

Description:

  1. Following the code pattern developed so far, namely functions to initialize, load, print, and access objects, write the following functions:
    void    camera_init(FILE*,camera_t**,int);
    void    camera_del(camera_t**);
    void    camera_load_attributes(FILE*, camera_t*);
    void    camera_print(camera_t*,FILE*);
    char*   camera_getname(camera_t*);
    	
  2. Don't forget to include a pointer to the camera object in the model object struct, i.e.,
    typedef struct model_type
    {
      camera_t      *cam;
      list_t        *mats;
      list_t        *objs;
    } model_t;
    	
  3. Your code should be such that the following main() routine works unaltered:
    int main(int argc, char *argv[])
    {
            model_t         *model;
            FILE            *model_file=NULL;
    
      if(argc != 2) {
        fprintf(stderr,"Usage: %s \n",argv[0]);
        exit(1);
      }
    
      // open model file
      if((model_file = fopen(argv[1],"r")) == NULL) {
         printf("Error opening input file: %s\n",argv[1]);
         return;
      }
    
      // create model
      model = model_init(model_file);
    
      // close file
      fclose(model_file);
    
      // verify that we have the model
      // (can implement either way, with FILE* or without)
    //model_print(model);
      model_print(model,stdout);
    
      // free up memory
      model_del(model);
    
      return(0);
    }
    	
  4. Sample input:
    camera cam1
    {
       pixeldim 640 480
       worlddim 8   6
       viewpoint 4 3 6
    }
    
    material green
    {
       ambient 0 5 0
    }
    
    material yellow
    {
       ambient  5 4 0
       diffuse  4 4 0
      specular  1 1 1
    }
    
    plane leftwall
    {
       material green
       normal 3 0 1
       point  0 0 0
    }
    
    plane rightwall
    {
       material yellow
       normal -3 0 1
       point   8 0 0
    }
    
    material gray
    {
       ambient 2 2 2
    }
    
    plane floor
    {
       material gray
       normal 0 1 0
       point  0 -0.2 0
    }
    	
  5. Sample output (note that the output order of objects differs from the input, this is ok):
    loaded cam1 
    loaded green 
    loaded yellow 
    loaded leftwall 
    loaded rightwall 
    loaded gray 
    loaded floor 
    camera cam1
    {
       pixeldim 640 480
       worlddim   8   6
       viewpoint 4 3 6
    }
    
    material green
    {
       ambient 0 5 0
    }
    
    material yellow
    {
       ambient 5 4 0
       diffuse 4 4 0
      specular 1 1 1
    }
    
    material gray
    {
       ambient 2 2 2
    }
    
    plane leftwall
    {
       material green
       normal 3 0 1
       point  0 0 0
    }
    
    plane rightwall
    {
       material yellow
       normal -3 0 1
       point  8 0 0
    }
    
    plane floor
    {
       material gray
       normal 0 1 0
       point  0 -0.2 0
    }
    	

What to hand in:

A tar.gz archive of your asg2/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Assignment description
    4. Brief solution description (e.g., program design, description of algorithm, etc., however appropriate).
    5. Lessons learned, identified interesting features of your program
    6. Any special usage instructions
  2. Makefile
  3. source code (.h headers and .c source)
  4. object code (do a make clean before tar)

How to hand in:

See submit notes.