/* assuming the spec is going to be properly written not error-checking here */ void read_spec(char *fname) { char buffer[300]; FILE *fp; fp = fopen(fname, "r"); my_assert(fp, "can't open spec"); while(!feof(fp)){ fgets(buffer, 300, fp); //printf("read line: %s\n", buffer); switch (buffer[0]) { case '#': break; case '1': //cube //read in the cube parse_obj(buffer); break; //etc case 'l': parse_light(buffer); break; case 'c': parse_camera(buffer); break; default: break; } } }
int parse_object(t_list **tokens) { while (parse_light(tokens)) continue ; while (parse_shape(tokens)) continue ; return (1); }
int main(int argc, const char * argv[]) { FILE *f; char object_name[256]; Scene *s = new Scene(); Light *light; Sphere *sphere; Plane *plane; int lights_loc=0; int spheres_loc=0; int planes_loc=0; f =fopen(filename, "r" ); if (f==NULL) { printf("%s not found",filename); } unsigned int number_of_lines = 0; int ch; while (EOF != (ch=getc(f))) if ('\n' == ch) ++number_of_lines; printf("%u\n", number_of_lines); fseek(f, 0, SEEK_SET); fseek(f, 5, SEEK_CUR); parse_scene(f,s); for (int i=0; i<number_of_lines ; i++) { printf("loop number: %d",i); fscanf(f,"%5s ", object_name); if (strcmp(object_name,"light")==0){ //equal printf("light detected"); light = new Light(); parse_light(f, light); lights[lights_loc++]=*light; } else if (strcmp(object_name,"spher")==0) { printf("sphere detected\n"); sphere = new Sphere(); parse_sphere(f, sphere); spheres[spheres_loc++]=*sphere; } else if (strcmp(object_name,"plane")==0) { printf("plane detected\n"); plane = new Plane(); parse_plane(f, plane); planes[planes_loc++]=*plane; } else { printf("Syntax input error - parser fail"); break; } } fclose(f); }
/* assuming the spec is going to be properly written not error-checking here */ void read_spec(char *fname) { char buffer[300]; FILE *fp; fp = fopen(fname, "r"); my_assert(fp, "can't open spec"); while(!feof(fp)) { buffer[0] = '#'; fgets(buffer, 300, fp); //printf("read line: %s\n", buffer); switch (buffer[0]) { case '#': break; case '1': case '2': case '3': case '4': case '5': case '6': { //You dont need seperate cases, you always just call parse_obj() parse_obj(buffer); break; } case 'l': parse_light(buffer); break; case 'c': parse_camera(buffer); break; default: break; } } }
/* assuming the spec is going to be properly written not error-checking here */ void read_spec(char *fname) { char buffer[300]; FILE *fp; fp = fopen(fname, "r"); my_assert(fp, "can't open spec"); while(!feof(fp)){ fgets(buffer, 300, fp); //printf("read line: %s\n", buffer); switch (buffer[0]) { case '#': break; case '0': // house case '1': //cube case '2': // sphere case '3': // cone case '4': // torus //read in the shape { printf("parse object"); parse_obj(buffer); } break; //etc case 'l': parse_light(buffer); break; case 'c': parse_camera(buffer); break; default: break; } } }
void start( void *data, const char *el, const char **attr ) { parse_data *pdata = (parse_data*)data; float xyz[3]; if( pdata == NULL ) { fprintf( stderr, "*** xml: parse data is NULL\n" ); exit( 1 ); } if( strcmp( el, "scene" ) == 0 ) { } else if( pdata->prim == NULL ) { pdata->prim = (primitive*)calloc( 1, sizeof( primitive ) ); if( strcmp( el, "sphere" ) == 0 ) { pdata->prim->type = SPHERE; pdata->prim->intersect = &sphere_isect; pdata->prim->normal = &sphere_normal; pdata->prim->data = (sphere_data*)malloc( sizeof( sphere_data ) ); parse_light( pdata->prim, attr ); parse_xyz( xyz, attr ); ((sphere_data*)pdata->prim->data)->radius = parse_attrf( "radius", attr, 0.0f ); ((sphere_data*)pdata->prim->data)->center.x = xyz[0]; ((sphere_data*)pdata->prim->data)->center.y = xyz[1]; ((sphere_data*)pdata->prim->data)->center.z = xyz[2]; vect_copy( &pdata->prim->center, &((sphere_data*)pdata->prim->data)->center ); /* set some defaults */ pdata->prim->mat.col.x = 1.0f; pdata->prim->mat.col.y = 1.0f; pdata->prim->mat.col.z = 1.0f; pdata->prim->mat.diffuse = 1.0f; pdata->prim->mat.specular = 0.0f; pdata->prim->mat.refl = 0.0f; pdata->prim->mat.is_refr = 0; } else if( strcmp( el, "plane" ) == 0 ) { pdata->prim->type = PLANE; pdata->prim->intersect = &plane_isect; pdata->prim->normal = &plane_normal; pdata->prim->data = (plane_data*)malloc( sizeof( plane_data ) ); parse_light( pdata->prim, attr ); parse_xyz( xyz, attr ); ((plane_data*)pdata->prim->data)->normal.x = xyz[0]; ((plane_data*)pdata->prim->data)->normal.y = xyz[1]; ((plane_data*)pdata->prim->data)->normal.z = xyz[2]; ((plane_data*)pdata->prim->data)->dist = parse_attrf( "distance", attr, 0.0f ); vect_copy( &pdata->prim->center, &((plane_data*)pdata->prim->data)->normal ); /* set some defaults */ pdata->prim->mat.col.x = 1.0f; pdata->prim->mat.col.y = 1.0f; pdata->prim->mat.col.z = 1.0f; pdata->prim->mat.diffuse = 1.0f; pdata->prim->mat.specular = 0.0f; pdata->prim->mat.refl = 0.0f; pdata->prim->mat.is_refr = 0; } else if( strcmp( el, "box" ) == 0 ) { pdata->prim->type = BOX; pdata->prim->intersect = &box_isect; pdata->prim->normal = &box_normal; pdata->prim->data = (box_data*)malloc( sizeof( box_data ) ); parse_xyz( xyz, attr ); ((box_data*)pdata->prim->data)->point.x = xyz[0]; ((box_data*)pdata->prim->data)->point.y = xyz[1]; ((box_data*)pdata->prim->data)->point.z = xyz[2]; ((box_data*)pdata->prim->data)->size.x = parse_attrf( "width", attr, 0.0f ); ((box_data*)pdata->prim->data)->size.y = parse_attrf( "height", attr, 0.0f ); ((box_data*)pdata->prim->data)->size.z = parse_attrf( "depth", attr, 0.0f ); pdata->prim->center.x = xyz[0] + (0.5f * ((box_data*)pdata->prim->data)->size.x); pdata->prim->center.y = xyz[1] + (0.5f * ((box_data*)pdata->prim->data)->size.y); pdata->prim->center.z = xyz[2] + (0.5f * ((box_data*)pdata->prim->data)->size.z); parse_light( pdata->prim, attr ); /* set some defaults */ pdata->prim->mat.col.x = 1.0f; pdata->prim->mat.col.y = 1.0f; pdata->prim->mat.col.z = 1.0f; pdata->prim->mat.diffuse = 1.0f; pdata->prim->mat.specular = 0.0f; pdata->prim->mat.refl = 0.0f; pdata->prim->mat.is_refr = 0; } else { fprintf( stderr, "*** xml: unknown primitive type \"%s\" at line %lu\n", el, XML_GetCurrentLineNumber( pdata->parser ) ); exit( 1 ); } pdata->prim->next = pdata->head; pdata->head = pdata->prim; } else if( strcmp( el, "material" ) == 0 ) { pdata->prim->mat.col.x = parse_attrf( "r", attr, 1.0f ); pdata->prim->mat.col.y = parse_attrf( "g", attr, 1.0f ); pdata->prim->mat.col.z = parse_attrf( "b", attr, 1.0f ); pdata->prim->mat.diffuse = parse_attrf( "diffuse", attr, 1.0f ); pdata->prim->mat.specular = parse_attrf( "specular", attr, 0.0f ); pdata->prim->mat.refl = parse_attrf( "reflectivity", attr, 0.0f ); pdata->prim->mat.is_refr = parse_attrb( "refractive", attr, 0 ); if( pdata->prim->mat.is_refr ) { pdata->prim->mat.refr = parse_attrf( "refractivity", attr, 1.0f ); } } else if( strcmp( el, "light" ) == 0 ) { } }