void SceneParser::parseFile() {
	//
	// at the top level, the scene can have a camera, 
	// background color and a group of objects
	// (we add lights and other things in future assignments)
	//
	char token[MAX_PARSER_TOKEN_LENGTH];
	while (getToken(token)) {
		if (!strcmp(token, "OrthographicCamera")) {
			parseOrthographicCamera();
		}
		else if (!strcmp(token, "PerspectiveCamera")) {
			parsePerspectiveCamera();
		}
		else if (!strcmp(token, "Background")) {
			parseBackground();
		}
		else if (!strcmp(token, "Lights")) {
			parseLights();
		}
		else if (!strcmp(token, "Materials")) {
			parseMaterials();
		}
		else if (!strcmp(token, "Group")) {
			group = parseGroup();
		}
		else {
			printf("Unknown token in parseFile: '%s'\n", token);
			exit(0);
		}
	}
}
Exemple #2
0
void scene::parse(const char *filename)
{
    //some default values in case parsing fails
    myObjGroup = NULL;
    bgColor = Vec3f(0.5,0.5,0.5);
    ambLight = Vec3f(0.5,0.5,0.5);
    eye = Vec3f(0,0,0);
    lookAt = Vec3f(0,0,-1);
    up = Vec3f(0,1,0);
    fovy = 45;
    file = NULL;
    curline = 1;

    //the file-extension needs to be "ray"
    assert(filename != NULL);
    const char *ext = &filename[strlen(filename)-4];
    assert(!strcmp(ext,".ray"));
    fopen_s(&file, filename, "r");
    assert(file != NULL);

    char token[MAX_PARSER_TOKEN_LENGTH];

    //prime the parser pipe
    parse_char = fgetc(file);

    while (getToken(token))
    {
        if(!strcmp(token, "Background"))
            parseBackground();
        else if(!strcmp(token, "Camera"))
            parseCamera();
        else if(!strcmp(token, "Materials"))
            parseMaterials();
        else if(!strcmp(token, "Group"))
            myObjGroup = parseGroup();
        else if(!strcmp(token, "Lights"))
            parseLights();
        else
        {
            cout<<"Unknown token in parseFile: '"<<token<<
                "' at input line "<<curline<<"\n";
            exit(-1);
        }
    }
}
XMLScene::XMLScene(char *filename, GlobalData &globals, Graph &graphScene)
{

	// Read XML from file

	doc=new TiXmlDocument( filename );
	bool loadOkay = doc->LoadFile();

	if ( !loadOkay )
	{
		printf( "Could not load file '%s'. Error='%s'. Exiting.\n", filename, doc->ErrorDesc() );
		exit( 1 );
	}

	TiXmlElement* anfElement= doc->FirstChildElement( "anf" );

	if (anfElement == NULL)
	{
		printf("Main anf block element not found! Exiting!\n");
		exit(1);
	}

	globalsElement = anfElement->FirstChildElement( "globals" );
	cameraElement = anfElement->FirstChildElement("cameras");
	lightElement = anfElement->FirstChildElement("lights");
	textureElement = anfElement->FirstChildElement("textures");
	appearanceElement = anfElement->FirstChildElement("appearances");
	graphElement = anfElement->FirstChildElement("graph");
	animsElement = anfElement->FirstChildElement("animations");

	parseGlobals(globals);
	parseCameras(graphScene);
	parseLights(graphScene);
	parseTextures(graphScene);
	parseAppearances(graphScene);
	parseAnimations(graphScene);
	parseGraph(graphScene);
}
bool ANFparser::parse(Scene * scene,const char* filename){
	this->scene = scene;

	try{
		TiXmlDocument * doc = new TiXmlDocument(filename);

		//First things first, reading the XML/ANF file
		if (!doc->LoadFile()){
			issue("Could not load file '%"+str(filename)+"'. Error='" + str(doc->ErrorDesc()) + "'.", ERROR);
		}

		//Must exist anf root block
		TiXmlElement* anfRoot = doc->FirstChildElement("anf");
		if (!anfRoot){
			issue("Main block 'anf' block not found!",ERROR);
		}

		//Lets process the global variables
		TiXmlElement* anfGlobals = anfRoot->FirstChildElement("globals");
		if(!anfGlobals){
			issue("Block 'globals' not found!",WARNING);
		}else{
			parseGlobals(anfGlobals);
		}
		
		//Lets process the cameras
		TiXmlElement* anfCameras = anfRoot->FirstChildElement("cameras");
		if(!anfCameras){
			issue("Block 'cameras' not found!",ERROR);
		}else{
			std::string firstCameraId = str(anfCameras->Attribute("initial"));
			if(firstCameraId == "" ){
				issue("Cameras block must declare the first camera.. ", WARNING);
			}
			parseCameras(anfCameras,firstCameraId);
		}

		//Lets process the lights
		TiXmlElement* anfLights = anfRoot->FirstChildElement("lights");
		if(!anfLights){
			issue("Block 'lights' not found!",WARNING);
		}else{
			parseLights(anfLights);
		}

		//If textures extist, we process it
		TiXmlElement* anfTextures = anfRoot->FirstChildElement("textures");
		map<std::string,Texture *> textures;
		if(!anfTextures){
			issue("Block 'textures' not found!",WARNING);
		}else{
			parseTextures(anfTextures,textures);
		}


		//If appearances block exist, we call its parser
		TiXmlElement* anfAppearances = anfRoot->FirstChildElement("appearances");
		map<std::string,Appearance *> appearances;
		if(!anfAppearances){
			issue("Block 'appearances' not found!",WARNING);
		}else{
			parseAppearances(anfAppearances,appearances,textures);
		}
		
		//If animations extist, we process it
		TiXmlElement* anfAnimations = anfRoot->FirstChildElement("animations");
		map<std::string,Animation *> animations;
		if(!anfAnimations){
			issue("Block 'animations' not found!",WARNING);
		}else{
			parseAnimations(anfAnimations,animations);
		}


		//If graph block exist, we call its parser
		TiXmlElement* anfGraph = anfRoot->FirstChildElement("graph");
		if(!anfGraph){
			issue("Block 'graph' not found!",ERROR);
		}else{
			this->scene->setRoot(parseGraph(anfGraph,appearances,animations));
		}

		return true; 
	}catch(...){
		return false;
	}
}