Lab 4: object_t

Objectives

To construct a C-style object object library and then parse an input file containing both material and object definitions. The idea is to be able to detect what is being read in (e.g., a material or an object, the only object that are currently known of are planes), and then in turn parse that entry (material or plane), adding each onto its respective linked list, i.e., either the list of materials or objects. To test whether this has been done successfully, write code to iterate through the lists to print out what was read in.

Assignment

  1. Use the following object.h header file:
    
    #ifndef NAME_LEN
    #define NAME_LEN   16        // max length of entity names
    #endif
    
    #define OBJ_COOKIE 12345678
    
    typedef struct object_type
    {
      int        cookie;
      char       type[NAME_LEN];    // e.g., plane, sphere, etc.
      char       name[NAME_LEN];    // left_wall, center_sphere, etc.
    
      material_t *mat;              // material
    
      void       (*printer)(struct object_type*,FILE*);
      double     (*hits)(struct object_type*,vec_t,vec_t);
    
      void       (*ambient) (material_t *,drgb_t);
      void       (*diffuse) (material_t *,drgb_t);
      void       (*specular)(material_t *,drgb_t);
    
      vec_t      last_hit;          // last hit point
      vec_t      last_normal;       // normal at last hit point
    } object_t;
    
    void   object_init(object_t*,FILE*,list_t*,list_t*);
    double object_no_hit(object_t*,vec_t,vec_t);
    void   object_list_print(list_t*,FILE*);
    void   object_print(object_t*,FILE*);
    char*  object_getname(object_t*);
    	
  2. Use the following plane.h header file:
    
    #ifndef NAME_LEN
    #define NAME_LEN   16        // max length of entity names
    #endif
    
    typedef struct plane_type
    {
      object_t   obj;
      vec_t      normal;
      vec_t      point;
      double     ndotq;
    } plane_t;
    
    // constructor
    void      plane_init(FILE*,list_t*,list_t*,int);
    
    // virtual functions (signature must match, name doesn't have to)
    void      plane_print(object_t*,FILE*);
    double    plane_hits(object_t*,vec_t,vec_t);
    	
  3. You code should be such that the following main.c file works unaltered:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    #include <math.h>
    
    #include "vector.h"
    #include "pixel.h"
    #include "list.h"
    #include "material.h"
    #include "object.h"
    
    int main()
    {
            list_t          *mats;
            list_t          *objs;
            material_t      *mat;
            object_t        *obj;
            char            token[16];
            drgb_t          pix;
    
      // create material and object lists
      mats = list_init();
      objs = list_init();
    
      // input should consist of material definitions followed by
      // object definitions
      while(fscanf(stdin,"%s",token) == 1) {
    
        if(!strcmp(token,"material")) {
          // create material_t structure and read attributes
          material_init(stdin,mats,0);
    
          // this test is designed to ensure that list_add
          // pointed current to the material just loaded
          mat = (material_t *)list_get_data(mats);
          assert(mat->cookie == MAT_COOKIE);
          fprintf(stderr,"loaded %s \n",material_getname(mat));
        }
    
        if(!strcmp(token,"plane")) {
          // create plane object
          plane_init(stdin,objs,mats,0);
    
          // this test is designed to ensure that list_add
          // pointed current to the object just loaded
          obj = (object_t *)list_get_data(objs);
          assert(obj->cookie == OBJ_COOKIE);
          fprintf(stderr,"loaded %s \n",object_getname(obj));
        }
      }
    
      // verify that we have all materials
      material_list_print(mats,stdout);
    
      // verify that we have all objects
      object_list_print(objs,stdout);
    
      return(0);
    }
    	
  4. Sample input:
    material green
    {
       ambient 0 5 0
    }
    
    material brown
    {
       ambient 3 3 0
    }
    
    plane wall
    {
       material green
       normal 0 0 1
       point  0 0 -7
    }
    
    plane floor
    {
       material brown
       normal 0 1 1
       point  0 0 -7
    }
    	
  5. Sample output:
    loaded green 
    loaded brown 
    loaded wall 
    loaded floor 
    material green
    {
       ambient 0 5 0
    }
    
    material brown
    {
       ambient 3 3 0
    }
    
    plane wall
    {
       material green
       normal 0 0 1
       point  0 0 -7
    }
    
    plane floor
    {
       material brown
       normal 0 1 1
       point  0 0 -7
    }
    	

Turn in

Turn in all of your code, in one tar.gz archive of your lab04/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Lab 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.