/** \file SceneContentGenerator.cpp \brief Creates content for a scene. */ #include "OgreSceneContentGenerator.h" // limit for failed attempts to create objects (to avoids infinite loop) #define MAX_FAILED_ATTEMPTS 500000 namespace Ogre { /*******************************************************/ /* SceneContentGenerator implementation */ /*******************************************************/ //----------------------------------------------------------------------- SceneContentGenerator::SceneContentGenerator(SceneManager *sm): mSceneMgr(sm), mMinPos(Vector3(-70.0f, -70.0f, 0.0f)), mMaxPos(Vector3(70.0f, 70.0f, 600.0f)), mMinAngle(Vector3(0.0f, 0.0f, 0.0f)), mMaxAngle(Vector3(360, 360, 360)), mScale(0.1, 0.1, 0.1) //, mObjectCount(0) { } //----------------------------------------------------------------------- void SceneContentGenerator::GenerateScene(int numObjects, const String &objName) { // initialise random generator srand (time(0)); Vector3 rotationRatio; Vector3 translationRatio; int old_count = GetObjectCount(); int failed_attempts = 0; // counter used to avoid invinite loop //-- create random values between zero and one while ((GetObjectCount() < numObjects + old_count) && (failed_attempts < MAX_FAILED_ATTEMPTS)) { rotationRatio.x = rand() / (float) RAND_MAX; rotationRatio.y = rand() / (float) RAND_MAX; rotationRatio.z = rand() / (float) RAND_MAX; translationRatio.x = rand() / (float) RAND_MAX; translationRatio.y = rand() / (float) RAND_MAX; translationRatio.z = rand() / (float) RAND_MAX; // Setup the ray scene query Vector3 rotation = mMinAngle + rotationRatio * (mMaxAngle - mMinAngle); Vector3 position = mMinPos + translationRatio * (mMaxPos - mMinPos); // failed to generate new object if (!GenerateSceneObject(position, rotation, objName)) ++failed_attempts; } } //----------------------------------------------------------------------- SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position, const Quaternion &orientation, const String& objName) { char name[25]; sprintf(name, "%s Entity%d", objName.c_str(), GetObjectCount()); Entity *ent = mSceneMgr->createEntity(name, objName + ".mesh"); SceneNode *currentObject = mSceneMgr->getRootSceneNode()-> createChildSceneNode(String(name) + "Node", position); //ent->setCastShadows(false); currentObject->attachObject(ent); currentObject->setScale(mScale); currentObject->setOrientation(orientation); //-- store pointer to object mSceneObjects.push_back(currentObject); return currentObject; } //----------------------------------------------------------------------- SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position, const Vector3 &rotation, const String& objName) { Matrix3 mat; mat.FromEulerAnglesYXZ(Degree(rotation.x), Degree(rotation.y), Degree(rotation.z)); return GenerateSceneObject(position, Quaternion(mat), objName); } //----------------------------------------------------------------------- void SceneContentGenerator::SetMinAngle(Vector3 minAngle) { mMinAngle = minAngle; } //----------------------------------------------------------------------- void SceneContentGenerator::SetMaxAngle(Vector3 maxAngle) { mMaxAngle = maxAngle; } //----------------------------------------------------------------------- void SceneContentGenerator::SetMinPos(Vector3 minPos) { mMinPos = minPos; } //----------------------------------------------------------------------- void SceneContentGenerator::SetMaxPos(Vector3 maxPos) { mMaxPos = maxPos; } //----------------------------------------------------------------------- int SceneContentGenerator::GetObjectCount() { return (int)mSceneObjects.size(); } //----------------------------------------------------------------------- void SceneContentGenerator::SetScale(Vector3 scale) { mScale = scale; } //----------------------------------------------------------------------- bool SceneContentGenerator::WriteObjects(const std::string &filename) { std::ofstream ofstr(filename.c_str()); std::vector::const_iterator it, it_end; it_end = mSceneObjects.end(); if(!ofstr.is_open()) return false; char str[100]; for(it = mSceneObjects.begin(); it < it_end; ++it) { SceneNode *node = (*it); sscanf(node->getName().c_str(), "%s ", str); // write name of mesh ofstr << str << " " << StringConverter::toString(node->getPosition()) << " " << StringConverter::toString(node->getOrientation()) << " " << StringConverter::toString(node->getScale()) << "\n"; } ofstr.close(); return true; } //----------------------------------------------------------------------- bool SceneContentGenerator::LoadObjects(const std::string &filename) { std::ifstream ifstr(filename.c_str()); char line[256]; Vector3 position; Quaternion orientation; char objName[100]; if (!ifstr.is_open()) return false; //mSceneObjects.clear(); // reset list of objects while (!ifstr.eof()) { ifstr.getline(line, 256); sscanf(line, "%s %f %f %f %f %f %f %f %f %f %f", objName, &position.x, &position.y, &position.z, &orientation.w, &orientation.x, &orientation.y, &orientation.z, &mScale.x, &mScale.y, &mScale.z); /*Vector3 rot(orientation.getYaw().valueDegrees(), orientation.getPitch().valueDegrees(), orientation.getRoll().valueDegrees());*/ GenerateSceneObject(position, orientation, objName); //std::stringstream d; d << StringConverter::toString(position) << " " << StringConverter::toString(orientation); //LogManager::getSingleton().logMessage(d.str()); } ifstr.close(); return true; } } // namespace Ogre