object_find_closest()
object_find_closest()
function,
where the idea is to:
NULL
)
t
value)
list_get_data(objs)
)
continue;
)
In this lab the material and object lists are also placed within a
model_t
object which also contains the camera_t
object.
object_find_closest()
function:
object_t* object_find_closest(list_t*,vec_t,vec_t,object_t*,double*);
and write the body of the function.
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"
#include "model.h"
int main()
{
model_t *model;
object_t *obj;
vec_t pos = { 4.0, 3.0, 5.0};
vec_t dir = { 0.0, 0.0, -1.0};
double dist;
FILE *model_file=NULL, *hits_file=NULL;
if(argc != 3) {
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 all materials
material_list_print(model->mats,stdout);
// verify that we have all objects
object_list_print(model->objs,stdout);
// open hits file
if((hits_file = fopen(argv[2],"r")) == NULL) {
printf("Error opening input file: %s\n",argv[2]);
return;
}
// read direction vectors from hits_file and perform hit comptuations
while(fscanf(hits_file, "%lf %lf %lf",&dir[0],&dir[1],&dir[2]) == 3) {
fprintf(stderr,"\n\n");
vec_print(stderr,"Ray direction:", dir);
vec_unit(dir,dir);
obj = object_find_closest(model->objs,pos,dir,NULL,&dist);
if(dist > 0) {
fprintf(stderr,"Hit: %-12s ",obj->name);
fprintf(stderr,"Dist = %8.3lf ",dist);
vec_print(stderr,"Loc = ",obj->last_hit);
} else
fprintf(stderr,"Dist = %8.3lf \n", dist);
fprintf(stderr, "\n\n");
obj = object_find_closest(model->objs,pos,dir,obj,&dist);
if(dist > 0) {
fprintf(stderr,"Hit: %-12s ", obj->name);
fprintf(stderr,"Dist = %8.3lf ",dist);
vec_print(stderr,"Loc = ",obj->last_hit);
} else
fprintf(stderr,"Dist = %8.3lf \n", dist);
}
// close hits file
fclose(hits_file);
return(0);
}
model.txt
:
material green { ambient 0 5 0 } material yellow { ambient 6 5 0 } material gray { ambient 4 4 4 } 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 0 }
hittest.txt
:
-0.5 -0.1 -1 0.5 -0.1 -1 0 -0.5 -1.0 1 1 1
loaded green loaded yellow loaded gray loaded leftwall loaded rightwall loaded floor material green { ambient 0 5 0 } material yellow { ambient 6 5 0 } material gray { ambient 4 4 4 } plane leftwall { material green normal 1 0 0 point 0 0 0 } plane rightwall { material yellow normal -1 0 0 point 8 0 0 } plane floor { material gray normal 0 1 0 point 0 0 0 } Ray direction: -0.500 -0.100 -1.000 Hit: leftwall Dist = 7.633 Loc = 0.600 2.320 -1.800 Hit: floor Dist = 33.675 Loc = -11.000 0.000 -25.000 Ray direction: 0.500 -0.100 -1.000 Hit: rightwall Dist = 7.633 Loc = 7.400 2.320 -1.800 Hit: floor Dist = 33.675 Loc = 19.000 0.000 -25.000 Ray direction: 0.000 -0.500 -1.000 Hit: floor Dist = 6.708 Loc = 4.000 0.000 -1.000 Hit: leftwall Dist = 19.007 Loc = 4.000 -5.500 -12.000 Ray direction: 1.000 1.000 1.000 Dist = -1.000 Dist = -1.000
./main model.txt hittest.txt
tar.gz
archive of your lab05/ directory, including:
README
file containing
Makefile
.h
headers and .c
source)
make clean
before tar
)
submit
notes.