- 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*);
- 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);
- 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);
}
- 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
}
- 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
}