int main()
{
list_t *mats;
list_t *objs;
material_t *mat;
object_t *obj;
char token[16];
drgb_t pix;
vec_t pos = { 4.0, 3.0, 5.0};
vec_t dir = { 0.0, 0.0, -1.0};
int firsttime=1;
// 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));
// test hits function
pos[0] = 4.0; pos[1] = 3.0; pos[2] = 5.0;
dir[0] = 0.0; dir[1] = 0.0; dir[2] = -1.0;
test_hits(obj,pos,dir);
if(firsttime) {
// make sure we don't get a hit on a miss...shoot
// straight down at backwall
fprintf(stderr,"vertical ray test\n");
pos[0] = 4.0; pos[1] = 3.0; pos[2] = 5.0;
dir[0] = 0.0; dir[1] = -2.1; dir[2] = 0.0;
test_hits(obj,pos,dir);
firsttime = 1-firsttime;
} else {
// make sure we don't get a hit in +z space
fprintf(stderr,"positive z test\n");
pos[0] = 4.0; pos[1] = 3.0; pos[2] = 5.0;
dir[0] = 0.0; dir[1] = -2.1; dir[2] = -1.0;
test_hits(obj,pos,dir);
}
}
}
// verify that we have all materials
material_list_print(mats,stdout);
// verify that we have all objects
object_list_print(objs,stdout);
return(0);
}
void test_hits(object_t *obj,vec_t pos,vec_t dir)
{
double dist=0.0;
vec_unit(dir,dir);
memset(obj->last_hit,0,sizeof(vec_t));
dist = obj->hits(obj,pos,dir);
fprintf(stderr,"dist to plane %s %8.3lf \n",obj->name,dist);
vec_print(stderr,"hit point",obj->last_hit);
}