//parse a config file and generate a Renderer Renderer* ConfigParser::parse(std::string filename){ std::cout << "ConfigParser::parse config file... " << filename <<" \n"; struct basicxmlnode * rootNode = NULL; //open file FILE * fp = fopen(filename.c_str(), "rt"); if (!fp) { std::cerr << "ConfigParser - Error: Failed opening file " << filename << "\n"; return NULL; } rootNode = readbasicxmlnode(fp); fclose(fp); if (!rootNode) { std::cerr << "ConfigParser - Error: Failed reading file " << filename << ". Maybe an XML syntax error?\n"; return NULL; } //create renderer Renderer* renderer = new Renderer(); //read renderer properties if (!addRendererProperties(rootNode, renderer)) { std::cerr << "ConfigParser - Error: Failed reading renderer properties in " << filename << "\n"; deletebasicxmlnode(rootNode); delete(renderer); return NULL; } //read sampler struct basicxmlnode * samplerNode = getchildnodebyname(rootNode, "Sampler"); if (!addSampler(samplerNode, renderer)) { std::cerr << "ConfigParser - Error: Failed reading sampler description in " << filename << "\n"; deletebasicxmlnode(rootNode); delete(renderer); return NULL; } //read shader struct basicxmlnode * shaderNode = getchildnodebyname(rootNode, "Shader"); if (!addShader(shaderNode, renderer)) { std::cerr << "ConfigParser - Error: Failed reading shader description in " << filename << "\n"; deletebasicxmlnode(rootNode); delete(renderer); return NULL; } //free xml memory deletebasicxmlnode(rootNode); std::cout << "[done]\n\n"; return renderer; }
bool SceneParser::addTorus(struct basicxmlnode * elementNode, Scene * scene) { if (!elementNode) { std::cout << "SceneParser::addQuadric: empty node \n"; return false; } if (std::string(elementNode->tag) != "Torus") { return false; } // create torus Torus * torus = new Torus(scene->getDefaultMaterial()); Vector3 center; double R, r; double reflectionPercentage; double refractionIndex; double refractionPercentage; Vector4 color; // read user specified values char * attributeValue; if (attributeValue = getattributevaluebyname(elementNode, "center")) { if (!stringToVector3<double>(center, attributeValue)) { delete(torus); return false; } torus->setCenter(center); } if ( (attributeValue = getattributevaluebyname(elementNode, "R")) ) { if (!stringToNumber<double>(R, attributeValue)) { delete(torus); return false; } torus->setR(R); } if ( (attributeValue = getattributevaluebyname(elementNode, "r")) ) { if (!stringToNumber<double>(r, attributeValue)) { delete(torus); return false; } torus->setr(r); } if (attributeValue = getattributevaluebyname(elementNode, "color")) { if (!stringToVector4<double>(color, attributeValue)) { delete(torus); return false; } torus->setColor(color); } if (attributeValue = getattributevaluebyname(elementNode, "reflectionPercentage")) { if (!stringToNumber<double>(reflectionPercentage, attributeValue)) { delete(torus); return false; } torus->setReflectionPercentage(reflectionPercentage); } if ( attributeValue = getattributevaluebyname(elementNode, "refractionIndex") ) { if ( !stringToNumber<double>(refractionIndex, attributeValue) ) { delete(torus); return false; } torus->setRefractionIndex(refractionIndex); } if ( attributeValue = getattributevaluebyname(elementNode, "refractionPercentage") ) { if ( !stringToNumber<double>(refractionPercentage, attributeValue) ) { delete(torus); return false; } torus->setRefractionPercentage(refractionPercentage); } // read material struct basicxmlnode * materialNode = getchildnodebyname(elementNode, "Material"); Material* material = getMaterialReference(materialNode, scene); torus->setMaterial(material); // add it to scene scene->addElement(torus); std::cout << "SceneParser::addTorus: added Torus\n"; return true; }
bool SceneParser::addTriangle(struct basicxmlnode * elementNode, Scene * scene){ if (!elementNode) { std::cout << "SceneParser::addTriangle: empty node \n"; return false; } if (std::string(elementNode->tag) != "Triangle") { return false; } // create triangle Triangle * triangle = new Triangle(scene->getMaterial("material_default")); Vector3 point1; Vector3 point2; Vector3 point3; Vector3 normal1; Vector3 normal2; Vector3 normal3; Vector4 color; double reflectionPercentage; double refractionIndex; double refractionPercentage; // read user specified values char * attributeValue; if (attributeValue = getattributevaluebyname(elementNode, "point1")) { if (!stringToVector3<double>(point1, attributeValue)) { delete(triangle); return false; } triangle->setPoint1(point1); } if (attributeValue = getattributevaluebyname(elementNode, "point2")) { if (!stringToVector3<double>(point2, attributeValue)) { delete(triangle); return false; } triangle->setPoint2(point2); } if (attributeValue = getattributevaluebyname(elementNode, "point3")) { if (!stringToVector3<double>(point3, attributeValue)) { delete(triangle); return false; } triangle->setPoint3(point3); } if (attributeValue = getattributevaluebyname(elementNode, "normal1")) { if (!stringToVector3<double>(normal1, attributeValue)) { delete(triangle); return false; } triangle->setNormal1(normal1); } if (attributeValue = getattributevaluebyname(elementNode, "normal2")) { if (!stringToVector3<double>(normal2, attributeValue)) { delete(triangle); return false; } triangle->setNormal2(normal2); } if (attributeValue = getattributevaluebyname(elementNode, "normal3")) { if (!stringToVector3<double>(normal3, attributeValue)) { delete(triangle); return false; } triangle->setNormal3(normal3); } if (attributeValue = getattributevaluebyname(elementNode, "color")) { if (!stringToVector4<double>(color, attributeValue)) { delete(triangle); return false; } triangle->setColor(color); } if (attributeValue = getattributevaluebyname(elementNode, "reflectionPercentage")) { if (!stringToNumber<double>(reflectionPercentage, attributeValue)) { delete(triangle); return false; } triangle->setReflectionPercentage(reflectionPercentage); } if ( attributeValue = getattributevaluebyname(elementNode, "refractionIndex") ) { if ( !stringToNumber<double>(refractionIndex, attributeValue) ) { delete(triangle); return false; } triangle->setRefractionIndex(refractionIndex); } if ( attributeValue = getattributevaluebyname(elementNode, "refractionPercentage") ) { if ( !stringToNumber<double>(refractionPercentage, attributeValue) ) { delete(triangle); return false; } triangle->setRefractionPercentage(refractionPercentage); } // read material struct basicxmlnode * materialNode = getchildnodebyname(elementNode, "Material"); Material* material = getMaterialReference(materialNode, scene); triangle->setMaterial(material); // and add it to scene scene->addElement(triangle); std::cout << "SceneParser::addTriangle: added Triangle\n"; return true; }
bool SceneParser::addQuadric(struct basicxmlnode * elementNode, Scene * scene) { if (!elementNode) { std::cout << "SceneParser::addQuadric: empty node \n"; return false; } if (std::string(elementNode->tag) != "Quadric") { return false; } // create quadric Quadric * quadric = new Quadric(scene->getDefaultMaterial()); Vector3 center; double a_real, a_img, b_real, b_img, c_real, c_img; double reflectionPercentage; double refractionIndex; double refractionPercentage; Vector4 color; // read user specified values char * attributeValue1, * attributeValue2; if (attributeValue1 = getattributevaluebyname(elementNode, "center")) { if (!stringToVector3<double>(center, attributeValue1)) { delete(quadric); return false; } quadric->setCenter(center); } if ( (attributeValue1 = getattributevaluebyname(elementNode, "aReal")) && (attributeValue2 = getattributevaluebyname(elementNode, "aImg")) ) { if (!stringToNumber<double>(a_real, attributeValue1) || !stringToNumber<double>(a_img, attributeValue2)) { delete(quadric); return false; } quadric->setA(ComplexNumber(a_real,a_img)); } if ( (attributeValue1 = getattributevaluebyname(elementNode, "bReal")) && (attributeValue2 = getattributevaluebyname(elementNode, "bImg")) ) { if (!stringToNumber<double>(b_real, attributeValue1) || !stringToNumber<double>(b_img, attributeValue2)) { delete(quadric); return false; } quadric->setB(ComplexNumber(b_real,b_img)); } if ( (attributeValue1 = getattributevaluebyname(elementNode, "cReal")) && (attributeValue2 = getattributevaluebyname(elementNode, "cImg")) ) { if (!stringToNumber<double>(c_real, attributeValue1) || !stringToNumber<double>(c_img, attributeValue2)) { delete(quadric); return false; } quadric->setC(ComplexNumber(c_real,c_img)); } if (attributeValue1 = getattributevaluebyname(elementNode, "color")) { if (!stringToVector4<double>(color, attributeValue1)) { delete(quadric); return false; } quadric->setColor(color); } if (attributeValue1 = getattributevaluebyname(elementNode, "reflectionPercentage")) { if (!stringToNumber<double>(reflectionPercentage, attributeValue1)) { delete(quadric); return false; } quadric->setReflectionPercentage(reflectionPercentage); } if ( attributeValue1 = getattributevaluebyname(elementNode, "refractionIndex") ) { if ( !stringToNumber<double>(refractionIndex, attributeValue1) ) { delete(quadric); return false; } quadric->setRefractionIndex(refractionIndex); } if ( attributeValue1 = getattributevaluebyname(elementNode, "refractionPercentage") ) { if ( !stringToNumber<double>(refractionPercentage, attributeValue1) ) { delete(quadric); return false; } quadric->setRefractionPercentage(refractionPercentage); } // read material struct basicxmlnode * materialNode = getchildnodebyname(elementNode, "Material"); Material* material = getMaterialReference(materialNode, scene); quadric->setMaterial(material); // add it to scene scene->addElement(quadric); std::cout << "SceneParser::addQuadric: added Quadric\n"; return true; }
//parse a scene description file and generate a Scene Scene* SceneParser::parse(const char* filename){ std::cout << "SceneParser::parse parses scene... " << filename <<" \n"; struct basicxmlnode * rootNode = NULL; //open file FILE * fp = fopen(filename, "rt"); if (!fp) { std::cerr << "SceneParser - Error: Failed opening file " << filename << "\n"; return NULL; } std::string filenameString = std::string(filename); size_t index = filenameString.find_last_of("/"); if (index == std::string::npos) { index = filenameString.find_last_of("\\"); } if (index == std::string::npos) { directory = ""; } else { directory = filenameString.substr(0,index+1); } //read xml tree rootNode = readbasicxmlnode(fp); fclose(fp); if (!rootNode) { std::cerr << "SceneParser - Error: Failed reading file " << filename << ". Maybe an XML syntax error?\n"; return NULL; } //construct scene Scene* scene = new Scene(); //read scene properties if (!addSceneProperties(rootNode, scene)) { std::cerr << "SceneParser - Error: Failed to read Scene Properties in " << filename << "\n"; deletebasicxmlnode(rootNode); delete(scene); return NULL; } //read camera struct basicxmlnode * cameraNode = getchildnodebyname(rootNode, "Camera"); if (!addCamera(cameraNode, scene)) { std::cerr << "SceneParser - Error: Failed reading camera description in " << filename << "\n"; deletebasicxmlnode(rootNode); delete(scene); return NULL; } //read lights struct basicxmlnode * lightsNode = getchildnodebyname(rootNode, "Lights"); if (!lightsNode) { std::cout << "SceneParser - Warning: No Lights specified in " << filename << "\n"; } else if (!lightsNode->children[0]) { std::cout << "SceneParser - Warning: No Lights specified in " << filename << "\n"; } else { for(int lightsIndex = 0; lightsNode->children[lightsIndex]; lightsIndex++) { if(!addLight(lightsNode->children[lightsIndex], scene)) { std::cerr << "SceneParser - Error: Failed reading light description in " << filename << "\n"; deletebasicxmlnode(rootNode); delete(scene); return NULL; } } } //read materials struct basicxmlnode * materialsNode = getchildnodebyname(rootNode, "Materials"); if (!materialsNode) { std::cout << "SceneParser - No global Materials specified in " << filename << "\n"; } else if (!materialsNode->children[0]) { std::cout << "SceneParser - Empty Materials node in " << filename << "\n"; } else { for(int materialsIndex = 0; materialsNode->children[materialsIndex]; materialsIndex++) { if(!addGlobalMaterial(materialsNode->children[materialsIndex], scene)) { std::cerr << "SceneParser - Error: Failed reading global material description in " << filename << "\n"; deletebasicxmlnode(rootNode); delete(scene); return NULL; } } } //read textures struct basicxmlnode* texturesNode = getchildnodebyname(rootNode, "Textures"); if (!texturesNode) { std::cout << "SceneParser - No global Textures specified in " << filename << "\n"; } else if (!texturesNode->children[0]) { std::cout << "SceneParser - Empty Textures node in " << filename << "\n"; } else { for(int texturesIndex = 0; texturesNode->children[texturesIndex]; texturesIndex++) { if(!addGlobalTexture(texturesNode->children[texturesIndex], scene)) { std::cerr << "SceneParser - Error: Failed reading global texture description in " << filename << "\n"; deletebasicxmlnode(rootNode); delete(scene); return NULL; } } } //read elements struct basicxmlnode * elementsNode = getchildnodebyname(rootNode, "Elements"); if (!elementsNode) { std::cout << "SceneParser - Warning: No Elements specified in " << filename << "\n"; } else if (!elementsNode->children[0]) { std::cout << "SceneParser - Warning: No Elements specified in " << filename << "\n"; } for(int elementsIndex = 0; elementsNode->children[elementsIndex]; elementsIndex++) { if(!addElement(elementsNode->children[elementsIndex], scene)) { std::cerr << "SceneParser - Error: Failed reading element description in " << filename << "\n"; deletebasicxmlnode(rootNode); delete(scene); return NULL; } } //free xml memory deletebasicxmlnode(rootNode); std::cout << "[done]\n\n"; return scene; }
bool SceneParser::addSphere(struct basicxmlnode * elementNode, Scene * scene){ if (!elementNode) { std::cout << "SceneParser::addSphere: empty node \n"; return false; } if (std::string(elementNode->tag) != "Sphere") { return false; } // create sphere Sphere * sphere = new Sphere(scene->getDefaultMaterial()); Vector3 center; double radius; double reflectionPercentage; double refractionIndex; double refractionPercentage; Vector4 color; // read user specified values char * attributeValue; if (attributeValue = getattributevaluebyname(elementNode, "center")) { if (!stringToVector3<double>(center, attributeValue)) { delete(sphere); return false; } sphere->setCenter(center); } if (attributeValue = getattributevaluebyname(elementNode, "radius")) { if (!stringToNumber<double>(radius, attributeValue)) { delete(sphere); return false; } sphere->setRadius(radius); } if (attributeValue = getattributevaluebyname(elementNode, "color")) { if (!stringToVector4<double>(color, attributeValue)) { delete(sphere); return false; } sphere->setColor(color); } if (attributeValue = getattributevaluebyname(elementNode, "reflectionPercentage")) { if (!stringToNumber<double>(reflectionPercentage, attributeValue)) { delete(sphere); return false; } sphere->setReflectionPercentage(reflectionPercentage); } if ( attributeValue = getattributevaluebyname(elementNode, "refractionIndex") ) { if ( !stringToNumber<double>(refractionIndex, attributeValue) ) { delete(sphere); return false; } sphere->setRefractionIndex(refractionIndex); } if ( attributeValue = getattributevaluebyname(elementNode, "refractionPercentage") ) { if ( !stringToNumber<double>(refractionPercentage, attributeValue) ) { delete(sphere); return false; } sphere->setRefractionPercentage(refractionPercentage); } // read material struct basicxmlnode * materialNode = getchildnodebyname(elementNode, "Material"); Material* material = getMaterialReference(materialNode, scene); sphere->setMaterial(material); // add it to scene scene->addElement(sphere); std::cout << "SceneParser::addSphere: added Sphere\n"; return true; }
bool SceneParser::addCirclePlane(struct basicxmlnode * elementNode, Scene * scene){ if (!elementNode) { std::cout << "SceneParser::addCirclePlane: empty node \n"; return false; } if (std::string(elementNode->tag) != "CirclePlane") { return false; } // create plane CirclePlane* circleplane = new CirclePlane(scene->getMaterial("default material")); Vector3 point; Vector3 normal; Vector4 color; double reflectionPercentage; double refractionIndex; double refractionPercentage; // read user specified values char * attributeValue; if (attributeValue = getattributevaluebyname(elementNode, "point")) { if (!stringToVector3<double>(point, attributeValue)) { delete(circleplane); return false; } circleplane->setPoint(point); } if (attributeValue = getattributevaluebyname(elementNode, "normal")) { if (!stringToVector3<double>(normal, attributeValue)) { delete(circleplane); return false; } circleplane->setNormal(normal); } if (attributeValue = getattributevaluebyname(elementNode, "color")) { if (!stringToVector4<double>(color, attributeValue)) { delete(circleplane); return false; } circleplane->setColor(color); } if (attributeValue = getattributevaluebyname(elementNode, "reflectionPercentage")) { if (!stringToNumber<double>(reflectionPercentage, attributeValue)) { delete(circleplane); return false; } circleplane->setReflectionPercentage(reflectionPercentage); } if ( attributeValue = getattributevaluebyname(elementNode, "refractionIndex") ) { if ( !stringToNumber<double>(refractionIndex, attributeValue) ) { delete(circleplane); return false; } circleplane->setRefractionIndex(refractionIndex); } if ( attributeValue = getattributevaluebyname(elementNode, "refractionPercentage") ) { if ( !stringToNumber<double>(refractionPercentage, attributeValue) ) { delete(circleplane); return false; } circleplane->setRefractionPercentage(refractionPercentage); } // read material struct basicxmlnode * materialNode = getchildnodebyname(elementNode, "Material"); Material* material = getMaterialReference(materialNode, scene); circleplane->setMaterial(material); // add it to scene scene->addElement(circleplane); std::cout << "SceneParser::addCirclePlane: added CirclePlane\n"; return true; }
bool SceneParser::addTriangleMesh(struct basicxmlnode * elementNode, Scene * scene){ if (!elementNode) { std::cout << "SceneParser::addTriangleMesh: empty node \n"; return false; } if (std::string(elementNode->tag) != "TriangleMesh") { return false; } // read filename char * cobjFileName; if (!(cobjFileName = getattributevaluebyname(elementNode, "OBJFileName"))) { std::cerr << "SceneParser::addTriangleMesh: no file specified!" << "\n"; return false; } // build obj object std::string objFileName(cobjFileName); char * attributeValue; Vector3 translate; translate[0]=0.0;translate[1]=0.0;translate[2]=0.0; if (attributeValue = getattributevaluebyname(elementNode, "translateFromOrigin")) { if (!stringToVector3<double>(translate, attributeValue)) { return false; } } double newRadius=1.0; if (attributeValue = getattributevaluebyname(elementNode, "radius")) { if (!stringToNumber<double>(newRadius, attributeValue)) { return false; } } Material* material; struct basicxmlnode * materialNode = getchildnodebyname(elementNode, "Material"); if(materialNode) { material = getMaterialReference(materialNode, scene); } // set Texture struct basicxmlnode * textureNode = getchildnodebyname(elementNode, "Texture"); ITexture* textureImage = getTextureReference(textureNode, scene); // set Bumpmap struct basicxmlnode * bumpmapNode = getchildnodebyname(elementNode, "Bumpmap"); ITexture* bumpmap = getTextureReference(bumpmapNode, scene); Vector4 color = Vector4(1,0,0,1); double reflectionPercentage = 0; if (attributeValue = getattributevaluebyname(elementNode, "color")) { if (!stringToVector4<double>(color, attributeValue)) { return false; } } if (attributeValue = getattributevaluebyname(elementNode, "reflectionPercentage")) { if (!stringToNumber<double>(reflectionPercentage, attributeValue)) { return false; } } double refractionPercentage = 0; double refractionIndex = 1; if (attributeValue = getattributevaluebyname(elementNode, "refractionPercentage")) { if (!stringToNumber<double>(refractionPercentage, attributeValue)) { return false; } } if (attributeValue = getattributevaluebyname(elementNode, "refractionIndex")) { if (!stringToNumber<double>(refractionIndex, attributeValue)) { return false; } } Mesh *m = new Mesh(scene,0,0); scene->addMesh(m); bool readSuccess = Mesh3DReader::read(objFileName,*m); if (!readSuccess) { std::cerr << "SceneParser::addTriangleMesh: Error while importing from Obj file: "<< objFileName << "\n"; return false; } /* bool readVertexNormals = false; bool readTexture = false; readOBJFile(objFileName,m,scene,readVertexNormals,readTexture); if(!readVertexNormals) { calcVertexNormals(m); } */ preprocessing(m,newRadius,translate); for(unsigned int i=0;i<m->numberOfFaces();i++) { m->getFace(i)->setTexture(textureImage); m->getFace(i)->setBumpmap(bumpmap); m->getFace(i)->setColor(color); m->getFace(i)->setMaterial(material); m->getFace(i)->setReflectionPercentage(reflectionPercentage); m->getFace(i)->setRefractionPercentage(refractionPercentage); m->getFace(i)->setRefractionIndex(refractionIndex); } return true; }
bool ConfigParser::addIntegrator( struct basicxmlnode * integratorNode, Renderer * renderer ) { if (!integratorNode) { std::cout << "ConfigParser::addIntegrator: empty integrator node\n"; return false; } if (std::string(integratorNode->tag) != "Integrator") { std::cout << "ConfigParser::addIntegrator: wrong integrator tag name\n"; return false; } std::string type = getattributevaluebyname(integratorNode, "type"); if (type == "WhittedIntegrator") { struct basicxmlnode * shaderNode = getchildnodebyname(integratorNode, "Shader"); WhittedIntegrator* integrator = new WhittedIntegrator(); if(addShader(shaderNode,integrator)){ renderer->setIntegrator( integrator ); }else{ return false; } //read recursion depth unsigned int recursionDepth; char * attributeValue; if (attributeValue = getattributevaluebyname(integratorNode, "recursionDepth")) { if (!stringToNumber<unsigned int>(recursionDepth, attributeValue)) { return false; } integrator->setRecursionDepth(recursionDepth); } }else if (type == "DirectLighting") { DirectLighting* integrator = new DirectLighting(); renderer->setIntegrator(integrator); //read number of samples unsigned int nSamples; char * attributeValue; if (attributeValue = getattributevaluebyname(integratorNode, "nSamples")) { if (!stringToNumber<unsigned int>(nSamples, attributeValue)) { return false; } integrator->setNSamples(nSamples); } }else if (type == "PathTracer") { PathTracer* integrator = new PathTracer(); renderer->setIntegrator(integrator); //read number of samples unsigned int nSamples; char * attributeValue; if (attributeValue = getattributevaluebyname(integratorNode, "nSamples")) { if (!stringToNumber<unsigned int>(nSamples, attributeValue)) { return false; } integrator->setNSamples(nSamples); } //read max. depth unsigned int maxDepth; if (attributeValue = getattributevaluebyname(integratorNode, "maxDepth")) { if (!stringToNumber<unsigned int>(maxDepth, attributeValue)) { return false; } integrator->setMaxDepth(maxDepth); } //read sample depth unsigned int sampleDepth; if (attributeValue = getattributevaluebyname(integratorNode, "sampleDepth")) { if (!stringToNumber<unsigned int>(sampleDepth, attributeValue)) { return false; } integrator->setSampleDepth(sampleDepth); } //read continue probabilty double p; if (attributeValue = getattributevaluebyname(integratorNode, "pContinue")) { if (!stringToNumber<double>(p, attributeValue)) { return false; } integrator->setContinueProbabilty(p); } } //Add loading of other integrator types here return true; }