[107] | 1 | /**
|
---|
| 2 | \file
|
---|
| 3 | SceneContentGenerator.cpp
|
---|
| 4 | \brief
|
---|
| 5 | Creates content for a scene.
|
---|
| 6 | */
|
---|
| 7 | #include "OgreSceneContentGenerator.h"
|
---|
| 8 |
|
---|
| 9 | // limit for failed attempts to create objects (to avoids infinite loop)
|
---|
| 10 | #define MAX_FAILED_ATTEMPTS 500000
|
---|
| 11 |
|
---|
| 12 | namespace Ogre {
|
---|
| 13 |
|
---|
[160] | 14 | /*************************************************************/
|
---|
| 15 | /* SceneContentGenerator implementation */
|
---|
| 16 | /*************************************************************/
|
---|
[107] | 17 |
|
---|
| 18 | //-----------------------------------------------------------------------
|
---|
| 19 | SceneContentGenerator::SceneContentGenerator(SceneManager *sm):
|
---|
| 20 | mSceneMgr(sm),
|
---|
| 21 | mMinPos(Vector3(-70.0f, -70.0f, 0.0f)),
|
---|
| 22 | mMaxPos(Vector3(70.0f, 70.0f, 600.0f)),
|
---|
| 23 | mMinAngle(Vector3(0.0f, 0.0f, 0.0f)),
|
---|
| 24 | mMaxAngle(Vector3(360, 360, 360)),
|
---|
[160] | 25 | mScale(0.1, 0.1, 0.1)//, mObjectCount(0)
|
---|
[107] | 26 | {
|
---|
| 27 | }
|
---|
| 28 | //----------------------------------------------------------------------- |
---|
| 29 | void SceneContentGenerator::GenerateScene(int numObjects, const String &objName) |
---|
| 30 | { |
---|
[160] | 31 | int new_size = GetObjectCount() + numObjects; |
---|
[107] | 32 | int failed_attempts = 0; // counter used to avoid invinite loop |
---|
| 33 | |
---|
| 34 | //-- create random values between zero and one |
---|
[160] | 35 | while ((GetObjectCount() < new_size) && |
---|
[107] | 36 | (failed_attempts < MAX_FAILED_ATTEMPTS)) |
---|
| 37 | { |
---|
| 38 | // Setup the ray scene query
|
---|
[175] | 39 | Vector3 rotation = Vector3(Math::RangeRandom(mMinAngle.x, mMaxAngle.x), |
---|
| 40 | Math::RangeRandom(mMinAngle.y, mMaxAngle.y), |
---|
| 41 | Math::RangeRandom(mMinAngle.z, mMaxAngle.z)); |
---|
[107] | 42 | |
---|
[175] | 43 | Vector3 position = Vector3(Math::RangeRandom(mMinPos.x, mMaxPos.x),
|
---|
| 44 | Math::RangeRandom(mMinPos.y, mMaxPos.y),
|
---|
| 45 | Math::RangeRandom(mMinPos.z, mMaxPos.z));
|
---|
| 46 | |
---|
[107] | 47 | // failed to generate new object |
---|
| 48 | if (!GenerateSceneObject(position, rotation, objName)) |
---|
[175] | 49 | ++ failed_attempts; |
---|
[107] | 50 | } |
---|
| 51 | }
|
---|
| 52 | //-----------------------------------------------------------------------
|
---|
| 53 | SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position, |
---|
| 54 | const Quaternion &orientation, const String& objName)
|
---|
| 55 | {
|
---|
| 56 | char name[25];
|
---|
[159] | 57 |
|
---|
[107] | 58 | sprintf(name, "%s Entity%d", objName.c_str(), GetObjectCount());
|
---|
| 59 |
|
---|
| 60 | Entity *ent = mSceneMgr->createEntity(name, objName + ".mesh");
|
---|
[175] | 61 | ent->setCastShadows(true);
|
---|
| 62 |
|
---|
[160] | 63 | SceneNode *node = mSceneMgr->getRootSceneNode()->
|
---|
[107] | 64 | createChildSceneNode(String(name) + "Node", position);
|
---|
| 65 |
|
---|
[111] | 66 | //ent->setCastShadows(false);
|
---|
[160] | 67 | node->attachObject(ent);
|
---|
| 68 | node->setScale(mScale);
|
---|
| 69 | node->setOrientation(orientation);
|
---|
[107] | 70 |
|
---|
[160] | 71 | // store pointer to node and object
|
---|
| 72 | mSceneNodes.push_back(node);
|
---|
| 73 | mEntities.push_back(ent);
|
---|
[175] | 74 |
|
---|
[160] | 75 | return node;
|
---|
[107] | 76 | }
|
---|
| 77 | //-----------------------------------------------------------------------
|
---|
| 78 | SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position, |
---|
| 79 | const Vector3 &rotation, const String& objName)
|
---|
| 80 | {
|
---|
| 81 | Matrix3 mat;
|
---|
[112] | 82 | mat.FromEulerAnglesYXZ(Degree(rotation.x), Degree(rotation.y),
|
---|
| 83 | Degree(rotation.z));
|
---|
[107] | 84 |
|
---|
| 85 | return GenerateSceneObject(position, Quaternion(mat), objName);
|
---|
| 86 | }
|
---|
| 87 | //-----------------------------------------------------------------------
|
---|
| 88 | void SceneContentGenerator::SetMinAngle(Vector3 minAngle)
|
---|
| 89 | {
|
---|
| 90 | mMinAngle = minAngle;
|
---|
| 91 | }
|
---|
| 92 | //-----------------------------------------------------------------------
|
---|
| 93 | void SceneContentGenerator::SetMaxAngle(Vector3 maxAngle)
|
---|
| 94 | {
|
---|
| 95 | mMaxAngle = maxAngle;
|
---|
| 96 | }
|
---|
| 97 | //-----------------------------------------------------------------------
|
---|
| 98 | void SceneContentGenerator::SetMinPos(Vector3 minPos)
|
---|
| 99 | {
|
---|
| 100 | mMinPos = minPos;
|
---|
| 101 | }
|
---|
| 102 | //-----------------------------------------------------------------------
|
---|
| 103 | void SceneContentGenerator::SetMaxPos(Vector3 maxPos)
|
---|
| 104 | {
|
---|
| 105 | mMaxPos = maxPos;
|
---|
| 106 | }
|
---|
| 107 | //-----------------------------------------------------------------------
|
---|
| 108 | int SceneContentGenerator::GetObjectCount()
|
---|
| 109 | {
|
---|
[160] | 110 | return (int)mSceneNodes.size();
|
---|
[107] | 111 | }
|
---|
| 112 | //-----------------------------------------------------------------------
|
---|
| 113 | void SceneContentGenerator::SetScale(Vector3 scale)
|
---|
| 114 | {
|
---|
| 115 | mScale = scale;
|
---|
| 116 | }
|
---|
| 117 | //-----------------------------------------------------------------------
|
---|
| 118 | bool SceneContentGenerator::WriteObjects(const std::string &filename)
|
---|
| 119 | {
|
---|
| 120 | std::ofstream ofstr(filename.c_str());
|
---|
| 121 | std::vector<SceneNode *>::const_iterator it, it_end;
|
---|
| 122 |
|
---|
[160] | 123 | it_end = mSceneNodes.end();
|
---|
[107] | 124 |
|
---|
| 125 | if(!ofstr.is_open())
|
---|
| 126 | return false;
|
---|
| 127 |
|
---|
| 128 | char str[100];
|
---|
| 129 |
|
---|
[160] | 130 | for(it = mSceneNodes.begin(); it < it_end; ++it)
|
---|
[107] | 131 | {
|
---|
| 132 | SceneNode *node = (*it);
|
---|
| 133 | sscanf(node->getName().c_str(), "%s ", str); // write name of mesh
|
---|
| 134 |
|
---|
| 135 | ofstr << str << " "
|
---|
| 136 | << StringConverter::toString(node->getPosition()) << " "
|
---|
| 137 | << StringConverter::toString(node->getOrientation()) << " "
|
---|
| 138 | << StringConverter::toString(node->getScale()) << "\n";
|
---|
| 139 | }
|
---|
| 140 | ofstr.close();
|
---|
| 141 |
|
---|
| 142 | return true;
|
---|
| 143 | }
|
---|
| 144 | //-----------------------------------------------------------------------
|
---|
| 145 | bool SceneContentGenerator::LoadObjects(const std::string &filename)
|
---|
| 146 | {
|
---|
| 147 | std::ifstream ifstr(filename.c_str());
|
---|
| 148 |
|
---|
| 149 | char line[256];
|
---|
| 150 |
|
---|
| 151 | Vector3 position;
|
---|
| 152 | Quaternion orientation;
|
---|
| 153 | char objName[100];
|
---|
| 154 |
|
---|
[121] | 155 | if (!ifstr.is_open())
|
---|
[107] | 156 | return false;
|
---|
| 157 |
|
---|
[160] | 158 | //mSceneNodes.clear(); // reset list of objects
|
---|
[107] | 159 |
|
---|
| 160 | while (!ifstr.eof())
|
---|
| 161 | {
|
---|
| 162 | ifstr.getline(line, 256);
|
---|
| 163 | sscanf(line, "%s %f %f %f %f %f %f %f %f %f %f", objName,
|
---|
| 164 | &position.x, &position.y, &position.z,
|
---|
| 165 | &orientation.w, &orientation.x, &orientation.y, &orientation.z,
|
---|
| 166 | &mScale.x, &mScale.y, &mScale.z);
|
---|
| 167 |
|
---|
[121] | 168 | GenerateSceneObject(position, orientation, objName);
|
---|
[107] | 169 |
|
---|
[121] | 170 | //std::stringstream d; d << StringConverter::toString(position) << " " << StringConverter::toString(orientation);
|
---|
| 171 | //LogManager::getSingleton().logMessage(d.str());
|
---|
[107] | 172 | }
|
---|
| 173 | ifstr.close();
|
---|
| 174 |
|
---|
| 175 | return true;
|
---|
| 176 | }
|
---|
[160] | 177 | //-----------------------------------------------------------------------
|
---|
| 178 | SceneNodeList *SceneContentGenerator::GetGeneratedSceneNodes()
|
---|
| 179 | {
|
---|
| 180 | return &mSceneNodes;
|
---|
| 181 | }
|
---|
| 182 | //-----------------------------------------------------------------------
|
---|
| 183 | EntityList *SceneContentGenerator::GetGeneratedEntities()
|
---|
| 184 | {
|
---|
| 185 | return &mEntities;
|
---|
| 186 | }
|
---|
| 187 | //-----------------------------------------------------------------------
|
---|
| 188 | void SceneContentGenerator::RemoveGeneratedObjects()
|
---|
| 189 | {
|
---|
| 190 | //-- destroy scene nodes and detach entities
|
---|
| 191 | while (!mSceneNodes.empty())
|
---|
| 192 | {
|
---|
| 193 | SceneNode *node = mSceneNodes.back();
|
---|
| 194 | mSceneNodes.pop_back();
|
---|
| 195 |
|
---|
| 196 | //node->detachAllObjects();
|
---|
| 197 | mSceneMgr->destroySceneNode(node->getName());
|
---|
| 198 | }
|
---|
| 199 | //-- remove and destroy entities
|
---|
| 200 | while (!mEntities.empty())
|
---|
| 201 | {
|
---|
| 202 | Entity *ent = mEntities.back();
|
---|
| 203 | mEntities.pop_back();
|
---|
| 204 |
|
---|
| 205 | mSceneMgr->removeEntity(ent);
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
[107] | 208 | } // namespace Ogre |
---|