#include "LBBCLeaf.h" namespace LBBC { Leaf::Leaf() { } Leaf::~Leaf() { } void Leaf::setPlaneD(float value) { mPlaneD = value; } float Leaf::getPlaneD() { return mPlaneD; } void Leaf::setPlaneNormal(Ogre::Vector3 value) { } Ogre::Vector3 Leaf::getPlaneNormal() { return mPlaneNormal; } void Leaf::setPosition(Ogre::Vector3 value) { mPosition = value; } Ogre::Vector3 Leaf::getPosition() { return mPosition; } Ogre::Quaternion Leaf::getOrientation() { return mOrientation; } void Leaf::setOrientation(const Ogre::Quaternion & orientation) { mOrientation = orientation; } void Leaf::addFaceNormal(Ogre::Vector3 value) { mFacesNormals.push_back(value); } void Leaf::removeFaceNormal(unsigned int value) { mFacesNormals.erase(mFacesNormals.begin()+value); } Ogre::Vector3 Leaf::getFaceNormal(unsigned int value) { return mFacesNormals[value]; } bool Leaf::hasFace(unsigned int value) { bool hasFace = false; if (mFacesEntitySet.size() > 0) { hasFace = mFacesEntitySet.find(value) != mFacesEntitySet.end(); } return hasFace; } void Leaf::addFace(unsigned int value) { mFacesEntitySet.insert(value); } void Leaf::removeFace(unsigned int value) { mFacesEntitySet.erase(value); } unsigned int Leaf::getFace(unsigned int value) { ID_FACESet::iterator itFacesEntitySet; itFacesEntitySet = mFacesEntitySet.begin(); unsigned int iElem = 0; while ((iElem < value) && (value < mFacesEntitySet.size())) { itFacesEntitySet++; iElem++; } return static_cast(* itFacesEntitySet); } unsigned int Leaf::getNumFaces() { return static_cast(mFacesEntitySet.size()); } unsigned int Leaf::getNumLeafNormals() { return static_cast(mFacesNormals.size()); } void Leaf::setLeafNormal(Ogre::Vector3 normal) { mLeafNormal = normal; } Ogre::Vector3 Leaf::getLeafNormal() { return mLeafNormal; } void Leaf::setLeafD(float leafD) { mLeafD = leafD; } float Leaf::getLeafD() { return mLeafD; } void Leaf::addFaceInfo(BBC::UniqueVertex v0, BBC::UniqueVertex v1, BBC::UniqueVertex v2) { // Add vertex 0 from the first face getSubEntity(0)->addUniqueVertex(v0); // Add vertex 1 from the first face getSubEntity(0)->addUniqueVertex(v1); // Add vertex 2 from the first face getSubEntity(0)->addUniqueVertex(v2); unsigned int numIndices = getSubEntity(0)->getNumFaces() * 3; Ogre::Vector3 indices = Ogre::Vector3(numIndices, numIndices + 1, numIndices + 2); getSubEntity(0)->addFaceVerticesIDs(indices); Ogre::Vector3 p1 = v0.position; Ogre::Vector3 p2 = v1.position; Ogre::Vector3 p3 = v2.position; Ogre::Vector3 v21 = p2 - p1; Ogre::Vector3 v31 = p3 - p1; Ogre::Vector3 normal = v21.crossProduct(v31); //if (dotProd < 0) if (normal[2] < 0) { normal = v31.crossProduct(v21); normal.normalise(); } addFaceNormal(normal); } void Leaf::readLeaf(TiXmlNode *leafNode) { unsigned int id = Ogre::StringConverter::parseInt(leafNode->ToElement()->Attribute("id")); setEntityHandle(id); Ogre::Vector3 normal; TiXmlNode *coord4dNode = leafNode->FirstChild("coord4d"); normal.x = Ogre::StringConverter::parseReal(coord4dNode->ToElement()->Attribute("nx")); normal.y = Ogre::StringConverter::parseReal(coord4dNode->ToElement()->Attribute("ny")); normal.z = Ogre::StringConverter::parseReal(coord4dNode->ToElement()->Attribute("nz")); float d = Ogre::StringConverter::parseReal(coord4dNode->ToElement()->Attribute("d")); Ogre::Vector3 position; TiXmlNode *positionNode = leafNode->FirstChild("position"); position.x = Ogre::StringConverter::parseReal(positionNode->ToElement()->Attribute("x")); position.y = Ogre::StringConverter::parseReal(positionNode->ToElement()->Attribute("y")); position.z = Ogre::StringConverter::parseReal(positionNode->ToElement()->Attribute("z")); setLeafNormal(normal); setPosition(position); setLeafD(d); TiXmlNode *facesNode = leafNode->FirstChild("faces"); unsigned int count = Ogre::StringConverter::parseInt(facesNode->ToElement()->Attribute("count")); for (TiXmlNode *faceNode = facesNode->FirstChild("face"); faceNode; faceNode = faceNode->NextSibling() ) { unsigned int id = Ogre::StringConverter::parseInt(faceNode->ToElement()->Attribute("id")); addFace(id); } } void Leaf::writeLeaf(TiXmlNode *leafNode) { leafNode->ToElement()->SetAttribute(Ogre::String("id"),Ogre::StringConverter::toString(getEntityHandle())); TiXmlNode *coord4dNode = leafNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("coord4d").c_str()))->ToElement(); Ogre::String sNx = Ogre::StringConverter::toString(getLeafNormal().x); Ogre::String sNy = Ogre::StringConverter::toString(getLeafNormal().y); Ogre::String sNz = Ogre::StringConverter::toString(getLeafNormal().z); Ogre::String sD = Ogre::StringConverter::toString(getLeafD()); coord4dNode->ToElement()->SetAttribute("nx", sNx.c_str()); coord4dNode->ToElement()->SetAttribute("ny", sNy.c_str()); coord4dNode->ToElement()->SetAttribute("nz", sNz.c_str()); coord4dNode->ToElement()->SetAttribute("d", sD.c_str()); TiXmlNode *positionNode = leafNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("position").c_str()))->ToElement(); Ogre::String sCoordX = Ogre::StringConverter::toString(getPosition().x); Ogre::String sCoordY = Ogre::StringConverter::toString(getPosition().y); Ogre::String sCoordZ = Ogre::StringConverter::toString(getPosition().z); positionNode->ToElement()->SetAttribute(Ogre::String("x").c_str(), sCoordX.c_str()); positionNode->ToElement()->SetAttribute(Ogre::String("y").c_str(), sCoordY.c_str()); positionNode->ToElement()->SetAttribute(Ogre::String("z").c_str(), sCoordZ.c_str()); TiXmlNode *facesNode; for (unsigned int i = 0; i < getNumFaces(); i++) { if (i == 0) { facesNode = leafNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("faces").c_str()))->ToElement(); Ogre::String sFace = Ogre::StringConverter::toString(getNumFaces()); facesNode->ToElement()->SetAttribute(Ogre::String("count").c_str(), sFace.c_str()); } TiXmlNode *faceNode = facesNode->ToElement()->InsertEndChild(TiXmlElement(Ogre::String("face").c_str()))->ToElement(); Ogre::String sId = Ogre::StringConverter::toString(getFace(i)); faceNode->ToElement()->SetAttribute("id", sId.c_str()); } } }