Node* Node::deepCopyNode() const{ Node *node = createNewInstance(); if( node == NULL ){ return NULL; } //Copy this node into the node node->setNodeID( nodeID ); node->setDepth( depth ); node->setIsLeafNode( isLeafNode ); //Recursively deep copy the left child if( getHasLeftChild() ){ node->setLeftChild( leftChild->deepCopyNode() ); node->leftChild->setParent( node ); } //Recursively deep copy the right child if( getHasRightChild() ){ node->setRightChild( rightChild->deepCopyNode() ); node->rightChild->setParent( node ); } return node; }
ObjectPool::ObjectPool(const std::string& name, const std::string& meshFilename, const std::string& textureFilename, const std::string& texture2Filename, bool physics, ObjectType objectType, const std::string& materialName, const std::string& material2Name, unsigned int num, unsigned int category, float friction, float mass, const irr::core::vector3df& center, bool _near) : name(name), objectList(), objectMesh(0), treeGenerator(0), hkShape(0), category(category), texture(0), texture2(0), objectType(objectType), material(irr::video::EMT_SOLID), material2(irr::video::EMT_SOLID), friction(friction), mass(mass), center(center), num(num), inUse(0), receiveShadow(false), _near(_near) { if (textureFilename != "") { texture = TheGame::getInstance()->getDriver()->getTexture(textureFilename.c_str()); } if (texture2Filename != "") { texture2 = TheGame::getInstance()->getDriver()->getTexture(texture2Filename.c_str()); } if (materialName != "") { material = Shaders::getInstance()->materialMap[materialName]; } if (material2Name != "") { material2 = Shaders::getInstance()->materialMap[material2Name]; } if (materialName == "vehicle" || materialName == "normal_shadow" || materialName == "normal_shadow_t") { receiveShadow = true; } switch (objectType) { case Grass: objectMesh = generateGrassMesh(); break; case Tree: { treeGenerator = new irr::scene::CTreeGenerator(TheGame::getInstance()->getSmgr()); irr::io::IXMLReader* xml = TheGame::getInstance()->getDevice()->getFileSystem()->createXMLReader(meshFilename.c_str()); treeGenerator->loadFromXML(xml); xml->drop(); break; } default: if (meshFilename.rfind(".mso") != std::string::npos) { objectMesh = readMySimpleObject(meshFilename, (objectType!=Vehicle)?mass:1.0f); } else { objectMesh = TheGame::getInstance()->getSmgr()->getMesh(meshFilename.c_str()); } break; } if (physics) { hkShape = calculateCollisionMesh(objectMesh, objectType); } if (Settings::getInstance()->preloadObjects) { for (unsigned int i = 0; i < num; i++) { OffsetObject* offsetObject = createNewInstance(); objectList.push_back(offsetObject); } } }
OffsetObject* ObjectPool::getObject(const irr::core::vector3df& apos, const irr::core::vector3df& scale, const irr::core::vector3df& rot, bool addToOffsetManager) { //dprintf(MY_DEBUG_NOTE, "ObjectPool::getObject(): %s\n", name.c_str()); OffsetObject* offsetObject = 0; if (!objectList.empty()) { offsetObject = objectList.front(); // *objectList.begin(); objectList.erase(objectList.begin()); } else { offsetObject = createNewInstance(); } offsetObject->setUpdateCB(0); //offsetObject->setPos(apos); offsetObject->getNode()->setPosition(apos); offsetObject->getNode()->setScale(scale); offsetObject->getNode()->setRotation(rot); offsetObject->getNode()->setMaterialType(material); if (Shaders::getInstance()->getSupportedSMVersion() < 2) { offsetObject->getNode()->setMaterialFlag(irr::video::EMF_LIGHTING, Settings::getInstance()->nonshaderLight); } else { offsetObject->getNode()->setMaterialFlag(irr::video::EMF_LIGHTING, false); } //printf("-------------- texture: %p\n", texture); offsetObject->getNode()->setMaterialTexture(0, texture); if (hkShape) { hk::lock(); hkpRigidBodyCinfo groundInfo; groundInfo.m_shape = hkShape; groundInfo.m_position.set(apos.X, apos.Y, apos.Z); if (rot != irr::core::vector3df()) { irr::core::vector3df rotRad = rot * irr::core::DEGTORAD; irr::core::quaternion rotQuat(rotRad); groundInfo.m_rotation = hkQuaternion(rotQuat.X, rotQuat.Y, rotQuat.Z, rotQuat.W); } if (objectType == Vehicle) { groundInfo.m_motionType = hkpMotion::MOTION_BOX_INERTIA; groundInfo.m_mass = mass; // TODO //groundInfo.m_position.set(0.0f, 0.0f, 0.0f); groundInfo.m_inertiaTensor.setDiagonal(1.0f, 1.0f, 1.0f); groundInfo.m_centerOfMass.set(center.X, center.Y, center.Z); groundInfo.m_collisionFilterInfo = hkpGroupFilter::calcFilterInfo(hk::materialType::vehicleId); } else { groundInfo.m_motionType = hkpMotion::MOTION_FIXED; groundInfo.m_collisionFilterInfo = hkpGroupFilter::calcFilterInfo(hk::materialType::treeId); } groundInfo.m_friction = friction; hkpRigidBody* hkBody = new hkpRigidBody(groundInfo); if (objectType != Vehicle) { hkpPropertyValue val(1); hkBody->addProperty(hk::materialType::treeId, val); hk::hkWorld->addEntity(hkBody); } else { hkpPropertyValue val(1); hkBody->addProperty(hk::materialType::vehicleId, val); } hk::unlock(); offsetObject->setBody(hkBody); //hkBody->activate(); } offsetObject->setPool(this); inUse++; if (addToOffsetManager) { offsetObject->addToManager(); } offsetObject->getNode()->setVisible(true); switch (objectType) { case Grass: case MyTree: break; default: ObjectPoolManager::getInstance()->addShadowNode(offsetObject->getNode()); break; } return offsetObject; }
bool Node::loadFromFile(fstream &file){ //Clear any previous nodes clear(); if(!file.is_open()) { errorLog << "loadFromFile(fstream &file) - File is not open!" << endl; return false; } string word; bool hasLeftChild = false; bool hasRightChild = false; file >> word; if( word != "NodeType:" ){ errorLog << "loadFromFile(fstream &file) - Failed to find Node header!" << endl; return false; } file >> nodeType; file >> word; if( word != "Depth:" ){ errorLog << "loadFromFile(fstream &file) - Failed to find Depth header!" << endl; return false; } file >> depth; file >> word; if( word != "NodeID:" ){ errorLog << "loadFromFile(fstream &file) - Failed to find NodeID header!" << endl; return false; } file >> nodeID; file >> word; if( word != "IsLeafNode:" ){ errorLog << "loadFromFile(fstream &file) - Failed to find IsLeafNode header!" << endl; return false; } file >> isLeafNode; file >> word; if( word != "HasLeftChild:" ){ errorLog << "loadFromFile(fstream &file) - Failed to find HasLeftChild header!" << endl; return false; } file >> hasLeftChild; file >> word; if( word != "HasRightChild:" ){ errorLog << "loadFromFile(fstream &file) - Failed to find HasRightChild header!" << endl; return false; } file >> hasRightChild; if( hasLeftChild ){ file >> word; if( word != "LeftChild" ){ errorLog << "loadFromFile(fstream &file) - Failed to find LeftChild header!" << endl; return false; } leftChild = createNewInstance(); leftChild->setParent( this ); if( !leftChild->loadFromFile(file) ){ errorLog << "loadFromFile(fstream &file) - Failed to load left child at depth: " << depth << endl; return false; } } if( hasRightChild ){ file >> word; if( word != "RightChild" ){ errorLog << "loadFromFile(fstream &file) - Failed to find RightChild header!" << endl; return false; } rightChild = createNewInstance(); rightChild->setParent( this ); if( !rightChild->loadFromFile( file ) ){ errorLog << "loadFromFile(fstream &file) - Failed to load right child at depth: " << depth << endl; return false; } } //Load the custom parameters from a file if( !loadParametersFromFile( file ) ){ errorLog << "loadParametersFromFile(fstream &file) - Failed to load parameters from file at depth: " << depth << endl; return false; } return true; }