Exemple #1
0
void YafFile::processLights(TiXmlElement* lights)
{
	float location[4], ambient[4], diffuse[4], specular[4];
	float angle = 0, exponent = 0, direction[3];
	bool enabled;
	string type;
	char* id;
	Light* light;
	TiXmlElement* element = lights->FirstChildElement();
	while(element!=NULL)
	{
		element->QueryBoolAttribute("enabled",&enabled);
		id = (char*)element->Attribute("id");
		read3Float("location", element, location[0], location[1], location[2]);
		read4Float("ambient", element, ambient[0], ambient[1], ambient[2],
			ambient[3]);
		read4Float("diffuse", element, diffuse[0], diffuse[1], diffuse[2],
			diffuse[3]);
		read4Float("specular", element, specular[0], specular[1], specular[2],
			specular[3]);
		if(element->Attribute("angle")!=NULL)
		{
			type="spot";
			element->QueryFloatAttribute("angle", &angle);
			element->QueryFloatAttribute("exponent", &exponent);
			read3Float("direction", element, direction[0], direction[1],
				direction[2]);
		}
		light = new Light(id,enabled,type,location,ambient,diffuse,specular,angle,exponent,direction);
		this->sceneLights.push_back(light);
		element=element->NextSiblingElement();
	}
}
Exemple #2
0
void YafFile::processCameras(TiXmlElement* camerasElement)
{
    char* rootId = (char*)camerasElement->Attribute("initial");
    char* id;
    string testRoot=rootId;
    TiXmlElement* orthoElement = camerasElement->FirstChildElement("ortho");
    TiXmlElement* perspectiveElement = camerasElement->FirstChildElement("perspective");
    CGFcamera* cam;
    float near, far, left, right, top, bottom;
    unsigned int i = 0;
    while(orthoElement != NULL)
    {
        orthoElement->QueryFloatAttribute("near", &near);
        orthoElement->QueryFloatAttribute("far", &far);
        orthoElement->QueryFloatAttribute("left", &left);
        orthoElement->QueryFloatAttribute("right", &right);
        orthoElement->QueryFloatAttribute("top", &top);
        orthoElement->QueryFloatAttribute("bottom", &bottom);
        id=(char*)orthoElement->Attribute("id");
        cam = new OrthoCamera(near, far, left, right, top, bottom,id==rootId);
        sceneCameras.push_back(make_pair(id,cam));
        orthoElement = orthoElement->NextSiblingElement("ortho");
        string test = id;
        if(test==testRoot)
        {
            camera=i;
        }
        else
            i++;
    }

    while(perspectiveElement != NULL)
    {
        float position[3],target[3],angle;
        perspectiveElement->QueryFloatAttribute("near", &near);
        perspectiveElement->QueryFloatAttribute("far", &far);
        perspectiveElement->QueryFloatAttribute("angle", &angle);
        read3Float("pos", perspectiveElement, position[0], position[1], position[2]);
        read3Float("target", perspectiveElement, target[0], target[1], target[2]);
        id=(char*)perspectiveElement->Attribute("id");
        cam = new PerspectiveCamera(id==rootId,near, far, angle, position[0], position[1],
                                    position[2], target[0], target[1], target[2]);
        sceneCameras.push_back(make_pair(id,cam));
        string test = id;
        if(test==testRoot)
        {
            camera=i;
        }
        else
            i++;
        perspectiveElement = perspectiveElement->NextSiblingElement("perspective");
    }
}
Exemple #3
0
void XMLScene::processLights(TiXmlElement* lights)
{
	float position[4], ambient[4], diffuse[4], specular[4];
	float angle = 0, exponent = 0, target[3];
	bool enabled, marked;
	string type;
	char* id;
	Light* light;
	TiXmlElement* element = lights->FirstChildElement();
	TiXmlElement* component = element ->FirstChildElement();
	while(element!=NULL)
	{
		element->QueryBoolAttribute("enabled",&enabled);
		element->QueryBoolAttribute("marker",&marked);
		id = (char*)element->Attribute("id");
		read3Float("pos", element, position[0], position[1], position[2]);
		if(element->Attribute("target")!=NULL)
		{
			type="spot";
			read3Float("target", element, target[0], target[1],
				target[2]);
			read1Float("angle",element, angle);
			read1Float("exponent",element, exponent);

		}
		while(component != NULL)
		{
			if(component->Attribute("type")=="ambient"){
				read4Float("value", element, ambient[0], ambient[1], ambient[2],ambient[3]);
			}

			if(component->Attribute("type")=="diffuse"){
				read4Float("value", element, diffuse[0], diffuse[1], diffuse[2],diffuse[3]);
			}

			if(component->Attribute("type")=="specular"){
				read4Float("value", element, specular[0], specular[1], specular[2],specular[3]);
			}
			component=component->NextSiblingElement();
		}


		light = new Light(id,enabled,type,position,ambient,diffuse,specular,angle,exponent,target,marked);
		this->sceneLights.push_back(light);
		if(enabled)
			activeLights.push_back(1);
		else 
			activeLights.push_back(0);

		element=element->NextSiblingElement();
	}
}
Exemple #4
0
void XMLScene::processCameras(TiXmlElement* camerasElement)
{
	string rootId = camerasElement->Attribute("initial");
	char* id;
	TiXmlElement* orthoElement = camerasElement->FirstChildElement("ortho");
	TiXmlElement* perspectiveElement = camerasElement->FirstChildElement("perspective");
	CGFcamera* cam;
	float near, far, left, right, top, bottom;
	unsigned int i = 0;
	while(orthoElement != NULL)
	{
		read1Float("near",orthoElement, near);
		read1Float("far",orthoElement, far);
		read1Float("left",orthoElement, left);
		read1Float("right",orthoElement, right);
		read1Float("top",orthoElement, top);
		read1Float("bottom",orthoElement, bottom);
		id=(char*)orthoElement->Attribute("id");
		bool activated=id==rootId;
		cam = new OrthoCamera(near, far, left, right, top, bottom,activated);
		sceneCameras.push_back(make_pair(id,cam));
		orthoElement = orthoElement->NextSiblingElement("ortho");
		if(id==rootId)
			camera=i;
		else
			i++;
	}

	while(perspectiveElement != NULL)
	{
		float position[3],target[3],angle;
		read1Float("near",perspectiveElement, near);
		read1Float("far",perspectiveElement, far);
		read1Float("angle",perspectiveElement, angle);
		read3Float("pos", perspectiveElement, position[0], position[1], position[2]);
		read3Float("target", perspectiveElement, target[0], target[1], target[2]);
		id=(char*)perspectiveElement->Attribute("id");
		bool activated=id==rootId;
		cam = new PerspectiveCamera(id==rootId,near, far, angle, position[0], position[1],
			position[2], target[0], target[1], target[2]);
		sceneCameras.push_back(make_pair(id,cam));
		if(id==rootId)
			camera=i;
		else
			i++;
		perspectiveElement = perspectiveElement->NextSiblingElement("perspective");
	}
}
Exemple #5
0
void XMLScene::processAnimations(TiXmlElement* animationElement){
	TiXmlElement* element = animationElement->FirstChildElement("animation");
	while(element!=NULL)
	{
		string id = element->Attribute("id"),type=element->Attribute("type");
		float span;
		element->QueryFloatAttribute("span",&span);
		vector<Point> points;
		if(type == "linear"){
			Point p;
			TiXmlElement* child = element->FirstChildElement("controlpoint");
			while(child!=NULL)
			{
				child->QueryFloatAttribute("xx", &p.x);
				child->QueryFloatAttribute("yy",&p.y);
				child->QueryFloatAttribute("zz",&p.z);
				points.push_back(p);
				child=child->NextSiblingElement();
			}
			Animation* animation = new Animation(span,type,points);
			Animations[id]=animation;
			element=element->NextSiblingElement();
		}
		else{
			float center[3], radius, startang, rotang;
			read3Float("center", element, center[0], center[1], center[2]);
			element->QueryFloatAttribute("radius",&radius);
			element->QueryFloatAttribute("startang",&startang);
			element->QueryFloatAttribute("rotang",&rotang);
			Animation* animation = new Animation(span,type,center[0],center[1],center[2],radius,startang,rotang);
			Animations[id]=animation;
			element=element->NextSiblingElement();
		}
	}
}
Exemple #6
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();
}
Exemple #7
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");

	}
}