#include "FileReader.h" #include #include #include #include #include //#include "defs.h" using namespace std; void FileReader::parseFile(Renderer &r) { parseFile(r.spheres,r.triangles,r.lights,&r.viewport,&r.h,&r.v,&r.l,r.ambient); } void FileReader::parseFile(vector *spheres, vector *triangles, vector *lights, Point *viewport, Point *h, Point *v, Point *l, m_float &ambient) { char *input = (char*) malloc(sizeof(char)*81); //Skip the Image Dimensions, 800x800 is where its at cin.getline(input,81); //Viewpoint Location scanPoint(viewport,input); //l vector scanPoint(l,input); //h vector scanPoint(h,input); //v vector scanPoint(v,input); //light source Light *light = new Light();//(Light*)malloc(sizeof(Light)); scanLight(light,input); lights->push_back(light); //Ambient Light intensity cin.getline(input,81); ambient = strtod(input, NULL); //Get Primitives cin.getline(input,81); scanPrimitives(strtol(input,NULL,10), spheres, triangles, input); } void FileReader::scanPrimitives(int count, vector *spheres, vector *triangles, char *input) { int i; for (i = 0; i < count; i++) { cin.getline(input,81); switch (input[0]) { case 'S': { Sphere* sphere = new Sphere(); scanSphere(sphere,input); spheres->push_back(sphere); //sphere->printMe(); break; } case 'T': { Triangle* triangle = new Triangle(); scanTriangle(triangle,input); triangles->push_back(triangle); break; } default: cout << "Error bad\n"; break; }; } } void FileReader::scanMaterials(MaterialAttributes &attrib, char *input) { char *pEnd; cin.getline(input,81); attrib.k_dr = strtod(input,&pEnd); attrib.k_dg = strtod(pEnd,&pEnd); attrib.k_db = strtod(pEnd,&pEnd); attrib.k_ar = strtod(pEnd,&pEnd); attrib.k_ag = strtod(pEnd,&pEnd); attrib.k_ab = strtod(pEnd,&pEnd); attrib.k_s = strtod(pEnd,&pEnd); attrib.n_spec = strtod(pEnd,NULL); } void FileReader::scanLight(Light *light, char *input) { char* pEnd; cin.getline(input,81); light->center.x = strtod(input, &pEnd); light->center.y = strtod(pEnd, &pEnd); light->center.z = strtod(pEnd, &pEnd); //For proper orientation if (Renderer::getInstance()->viewport.z - Renderer::getInstance()->l.z < 0) { light->center.x *= -1.; } light->intensity = strtod(pEnd, NULL); } void FileReader::scanSphere(Sphere *sphere, char *input) { //Sphere* sphere = new Sphere(); char *pEnd; assert(sphere != NULL); cin.getline(input,81); sphere->center.x = strtod(input, &pEnd); sphere->center.y = strtod(pEnd, &pEnd); sphere->center.z = strtod(pEnd, &pEnd); if (Renderer::getInstance()->viewport.z - Renderer::getInstance()->l.z < 0) { sphere->center.x *= -1.; } sphere->radius = fabs(strtod(pEnd,NULL)); scanMaterials(sphere->m_attr, input); //primitives.push_back(sphere); } void FileReader::scanTriangle(Triangle *triangle, char *input) { //Triangle* triangle = new Triangle(); assert(triangle != NULL); scanPoint(&triangle->a1,input); scanPoint(&triangle->a2,input); scanPoint(&triangle->a3,input); if (Renderer::getInstance()->viewport.z - Renderer::getInstance()->l.z < 0) { triangle->a1.x *= -1.; triangle->a2.x *= -1.; triangle->a3.x *= -1.; } scanMaterials(triangle->m_attr, input); } void FileReader::scanPoint(Point* point, char *input) { char* pEnd; cin.getline(input, 81); point->x = strtod(input,&pEnd); point->y = strtod(pEnd,&pEnd); point->z = strtod(pEnd,NULL); }