Exemplo n.º 1
0
void YafFile::processGraph(TiXmlElement* graphElement)
{
	string rootid=graphElement->Attribute("rootid");
	TiXmlElement* nodeElement = graphElement->FirstChildElement("node");
	sceneGraph = new Graph();
	sceneGraph->setRootId(rootid);
	while(nodeElement!=NULL)
	{
		Node* node = new Node(nodeElement->Attribute("id"));
		bool displaylist=false;
		nodeElement->QueryBoolAttribute("displaylist",&displaylist);
		if(displaylist!=NULL)
			node->setDisplayList(displaylist);

		string animID;
		TiXmlElement* animationsN = nodeElement->FirstChildElement("animationref");
		if(animationsN!=NULL)
			animID=animationsN->Attribute("id");


		TiXmlElement* transformations = nodeElement->FirstChildElement("transforms");
		TiXmlElement* transformation=transformations->FirstChildElement();

		while(transformation!=NULL)
		{
			if(transformation->Attribute("axis")!=NULL)
			{
				string axis=transformation->Attribute("axis");
				float angle;
				transformation->QueryFloatAttribute("angle",&angle);
				Transformation* rot = new Rotation(axis,angle);
				node->addTransformation(rot);
			}
			else if (transformation->Attribute("to")!=NULL)
			{
				float translation[3];
				read3Float("to",transformation,translation[0],translation[1],translation[2]);
				Transformation* translationElement = new Translation(translation[0],translation[1],translation[2]);
				node->addTransformation(translationElement);
			}

			else if (transformation->Attribute("factor")!=NULL)
			{
				float factor[3];
				read3Float("factor",transformation,factor[0],factor[1],factor[2]);
				Transformation* scaling = new Scaling(factor[0],factor[1],factor[2]);
				node->addTransformation(scaling);
			}

			transformation = transformation->NextSiblingElement();
		}

		TiXmlElement* appearanceRef = nodeElement->FirstChildElement("appearanceref");
		if(appearanceRef!=NULL)
		{

			node->setNodeAppearance(make_pair(appearanceRef->Attribute("id"),appearances[appearanceRef->Attribute("id")]));
		}
		else
		{
			node->setNodeAppearance(make_pair("",new Appearance()));
		}
		TiXmlElement* children = nodeElement->FirstChildElement("children");
		TiXmlElement* childrenAnalyzer = children->FirstChildElement();
		while(childrenAnalyzer!=NULL)
		{
			if(childrenAnalyzer->Attribute("xy1")!=NULL)
			{
				float xy1[2],xy2[2];
				read2Float("xy1",childrenAnalyzer,xy1[0],xy1[1]);
				read2Float("xy2",childrenAnalyzer,xy2[0],xy2[1]);
				SceneObject* rectangle = new SceneRectangle(xy1[0],xy1[1],xy2[0],xy2[1]);
				node->addObject(rectangle);
			}

			else if(childrenAnalyzer->Attribute("xyz1")!=NULL)
			{
				float xyz1[3],xyz2[3],xyz3[3];
				read3Float("xyz1",childrenAnalyzer,xyz1[0],xyz1[1],xyz1[2]);
				read3Float("xyz2",childrenAnalyzer,xyz2[0],xyz2[1],xyz2[2]);
				read3Float("xyz3",childrenAnalyzer,xyz3[0],xyz3[1],xyz3[2]);
				SceneObject* triangle = new SceneTriangle(xyz1,xyz2,xyz3);
				node->addObject(triangle);
			}

			else if(childrenAnalyzer->Attribute("base")!=NULL)
			{
				float base,top,height;
				int slices,stacks;
				childrenAnalyzer->QueryFloatAttribute("base",&base);
				childrenAnalyzer->QueryFloatAttribute("top",&top);
				childrenAnalyzer->QueryFloatAttribute("height",&height);
				childrenAnalyzer->QueryIntAttribute("slices",&slices);
				childrenAnalyzer->QueryIntAttribute("stacks",&stacks);
				SceneObject* cylinder = new SceneCylinder(base,top,height,slices,stacks);
				node->addObject(cylinder);
			}
			else if(childrenAnalyzer->Attribute("radius")!=NULL)
			{
				float radius;
				int slices,stacks;
				childrenAnalyzer->QueryFloatAttribute("radius",&radius);
				childrenAnalyzer->QueryIntAttribute("slices",&slices);
				childrenAnalyzer->QueryIntAttribute("stacks",&stacks);
				SceneObject* sphere = new SceneSphere(radius,slices,stacks);
				node->addObject(sphere);
			}
			else if(childrenAnalyzer->Attribute("inner")!=NULL)
			{
				float inner,outer;
				int slices,loops;
				childrenAnalyzer->QueryFloatAttribute("inner",&inner);
				childrenAnalyzer->QueryFloatAttribute("outer",&outer);
				childrenAnalyzer->QueryIntAttribute("slices",&slices);
				childrenAnalyzer->QueryIntAttribute("loops",&loops);
				SceneObject* thorus = new SceneTorus(inner,outer,slices,loops);
				node->addObject(thorus);
			}
			else if(childrenAnalyzer->Attribute("parts")!=NULL)
			{
				int parts;
				childrenAnalyzer->QueryIntAttribute("parts",&parts);
				SceneObject* plane = new ScenePlane(parts);
				node->addObject(plane);
			}
			else if(strcmp(childrenAnalyzer->Value(),"patch")==0)
			{
				int order,partsU,partsV;
				float x,y,z;
				string compute;

				childrenAnalyzer->QueryIntAttribute("order",&order);
				childrenAnalyzer->QueryIntAttribute("partsU",&partsU);
				childrenAnalyzer->QueryIntAttribute("partsV",&partsV);
				compute=childrenAnalyzer->Attribute("compute");

				ScenePatch* patch = new ScenePatch(order,partsU,partsV,compute);

				TiXmlElement* points = childrenAnalyzer->FirstChildElement("controlpoint");

				while(points!=NULL)
				{
					points->QueryFloatAttribute("x",&x);
					points->QueryFloatAttribute("y",&y);
					points->QueryFloatAttribute("z",&z);

					cout<<x<<","<<y<<","<<z<<endl;
					patch->addCntrPoint(x,y,z);

					points = points->NextSiblingElement("controlpoint");
				}
				node->addObject(patch);

			}

			else if(strcmp(childrenAnalyzer->Value(),"vehicle")==0)
			{
				SceneObject* vehicle = new SceneVehicle();
				node->addObject(vehicle);
			}

			else if(childrenAnalyzer->Attribute("heightmap")!=NULL)
			{
				string heightMap,textureMap,fragmentShader,vertexShader;
				heightMap=childrenAnalyzer->Attribute("heightmap");
				textureMap=childrenAnalyzer->Attribute("texturemap");
				fragmentShader=childrenAnalyzer->Attribute("fragmentshader");
				vertexShader=childrenAnalyzer->Attribute("vertexshader");

				SceneObject* shader = new SceneShader(heightMap,textureMap,fragmentShader,vertexShader);
				node->addObject(shader);
			}


			else if (childrenAnalyzer->Attribute("id")!=NULL)
			{
				node->addchildId(childrenAnalyzer->Attribute("id"));
			}
			childrenAnalyzer=childrenAnalyzer->NextSiblingElement();
		}
		//set animation
		Animation* anime=animations[animID];

		if(anime!=NULL)
		{
			node->setAnimation(anime);
		}
		sceneGraph->addNode(node);
		nodeElement=nodeElement->NextSiblingElement("node");
	}
	sceneGraph->createSonNodes();
}
Exemplo n.º 2
0
void XMLScene::processGraph(TiXmlElement* graphElement)
{
	string rootid=graphElement->Attribute("rootid");
	TiXmlElement* nodeElement = graphElement->FirstChildElement("node");
	sceneGraph = new Graph();
	sceneGraph->setRootId(rootid);
	while(nodeElement!=NULL)
	{
		bool displayList=false;
		string nodeId = nodeElement->Attribute("id");
		if(nodeElement->Attribute("displaylist") != NULL)
			nodeElement->QueryBoolAttribute("displaylist",&displayList);
		Node* node = new Node(nodeId, displayList);

		string animationID;
		TiXmlElement* animations = nodeElement->FirstChildElement("animationref");
		if(animations!=NULL)
			animationID=animations->Attribute("id");

		TiXmlElement* transformations = nodeElement->FirstChildElement("transforms");
		TiXmlElement* transformation = transformations->FirstChildElement();

		while(transformation!=NULL)
		{
			if(transformation->Attribute("axis")!=NULL)
			{
				string axis=transformation->Attribute("axis");
				float angle;
				read1Float("angle",transformation,angle);
				Transformation* rot = new Rotation(axis,angle);
				node->addTransformation(rot);
			}
			else if (transformation->Attribute("to")!=NULL)
			{
				float translation[3];
				read3Float("to",transformation,translation[0],translation[1],translation[2]);
				Transformation* translationElement = new Translation(translation[0],translation[1],translation[2]);
				node->addTransformation(translationElement);
			}

			else if (transformation->Attribute("factor")!=NULL)
			{
				float factor[3];
				read3Float("factor",transformation,factor[0],factor[1],factor[2]);
				Transformation* scaling = new Scaling(factor[0],factor[1],factor[2]);
				node->addTransformation(scaling);
			}

			transformation = transformation->NextSiblingElement();
		}

		TiXmlElement* appearanceRef = nodeElement->FirstChildElement("appearanceref");
		if(appearanceRef->Attribute("id")!="inherit")
		{
			node->setNodeAppearance(make_pair(appearanceRef->Attribute("id"),appearances[appearanceRef->Attribute("id")]));
		}
		else
		{
			node->setNodeAppearance(make_pair("inherit",new Appearance()));
		}
		TiXmlElement* primitive = nodeElement->FirstChildElement("primitives");
		TiXmlElement* primitiveAnalyzer = primitive->FirstChildElement();
		while(primitiveAnalyzer!=NULL)
		{
			if(primitiveAnalyzer->Attribute("xy1")!=NULL)
			{
				float xy1[2],xy2[2];
				read2Float("xy1",primitiveAnalyzer,xy1[0],xy1[1]);
				read2Float("xy2",primitiveAnalyzer,xy2[0],xy2[1]);
				SceneObject* rectangle = new SceneRectangle(xy1[0],xy1[1],xy2[0],xy2[1]);
				node->addObject(rectangle);
			}

			else if(primitiveAnalyzer->Attribute("xyz1")!=NULL)
			{
				float xyz1[3],xyz2[3],xyz3[3];
				read3Float("xyz1",primitiveAnalyzer,xyz1[0],xyz1[1],xyz1[2]);
				read3Float("xyz2",primitiveAnalyzer,xyz2[0],xyz2[1],xyz2[2]);
				read3Float("xyz3",primitiveAnalyzer,xyz3[0],xyz3[1],xyz3[2]);
				
				SceneObject* triangle = new SceneTriangle(xyz1,xyz2,xyz3);
				node->addObject(triangle);
			}

			else if(primitiveAnalyzer->Attribute("base")!=NULL)
			{
				float base,top,height;
				int slices,stacks;
				read1Float("base",primitiveAnalyzer,base);
				read1Float("top",primitiveAnalyzer,top);
				read1Float("height",primitiveAnalyzer,height);
				read1Int("slices",primitiveAnalyzer,slices);
				read1Int("stacks",primitiveAnalyzer,stacks);
				SceneObject* cylinder = new SceneCylinder(base,top,height,slices,stacks);
				node->addObject(cylinder);
			}

			else if(primitiveAnalyzer->Attribute("radius")!=NULL)
			{
				float radius;
				int slices,stacks;
				read1Float("radius",primitiveAnalyzer,radius);
				read1Int("slices",primitiveAnalyzer,slices);
				read1Int("stacks",primitiveAnalyzer,stacks);
				SceneObject* sphere = new SceneSphere(radius,slices,stacks);
				node->addObject(sphere);
			}

			else if(primitiveAnalyzer->Attribute("inner")!=NULL)
			{
				float inner,outer;
				int slices,loops;
				read1Float("inner",primitiveAnalyzer,inner);
				read1Float("outer",primitiveAnalyzer,outer);
				read1Int("slices",primitiveAnalyzer,slices);
				read1Int("loops",primitiveAnalyzer,loops);
				
				SceneObject* thorus = new SceneTorus(inner,outer,slices,loops);
				node->addObject(thorus);
			}

			else if(primitiveAnalyzer->Attribute("parts")!=NULL)
			{
				int parts;
				read1Int("parts",primitiveAnalyzer,parts);

				SceneObject* plane = new ScenePlane(parts);
				node->addObject(plane);

			}
			else if(strcmp(primitiveAnalyzer->Value(),"patch")==0)
			{
				int order,partsU,partsV;
				float x,y,z;
				string compute;

				primitiveAnalyzer->QueryIntAttribute("order",&order);
				primitiveAnalyzer->QueryIntAttribute("partsU",&partsU);
				primitiveAnalyzer->QueryIntAttribute("partsV",&partsV);
				compute=primitiveAnalyzer->Attribute("compute");

				ScenePatch* patch = new ScenePatch(order,partsU,partsV,compute);

				TiXmlElement* ControlPoints = primitiveAnalyzer->FirstChildElement("controlpoint");

				while(ControlPoints!=NULL)
				{
					ControlPoints->QueryFloatAttribute("x",&x);
					ControlPoints->QueryFloatAttribute("y",&y);
					ControlPoints->QueryFloatAttribute("z",&z);

					patch->addCntrPoint(x,y,z);

					ControlPoints = ControlPoints->NextSiblingElement("controlpoint");
				}
				node->addObject(patch);

			}
			else if(primitiveAnalyzer->Attribute("texture") != NULL) {
				std::string texture;
				texture = primitiveAnalyzer->Attribute("texture");
				SceneObject* flag = new SceneFlag(texture);
				node->addObject(flag);
				std::cout << "Added flag with texture: " << texture << std::endl;
			}

			primitiveAnalyzer = primitiveAnalyzer->NextSiblingElement();
		}
		TiXmlElement* descendants = nodeElement->FirstChildElement("descendants");
		TiXmlElement* descendantsAnalyzer = descendants->FirstChildElement();
		while(descendantsAnalyzer!=NULL)
		{
			node->addchildId(descendantsAnalyzer->Attribute("id"));
			
			descendantsAnalyzer=descendantsAnalyzer->NextSiblingElement();
		}

		sceneGraph->addNode(node);
		nodeElement=nodeElement->NextSiblingElement("node");

	}
}