[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 |
|
---|
| 14 | /*******************************************************/
|
---|
| 15 | /* SceneContentGenerator implementation */
|
---|
| 16 | /*******************************************************/
|
---|
| 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)),
|
---|
| 25 | mScale(0.1, 0.1, 0.1)
|
---|
| 26 | //, mObjectCount(0)
|
---|
| 27 | {
|
---|
| 28 | }
|
---|
| 29 | //----------------------------------------------------------------------- |
---|
| 30 | void SceneContentGenerator::GenerateScene(int numObjects, const String &objName) |
---|
| 31 | { |
---|
| 32 | // initialise random generator |
---|
| 33 | srand (time(0)); |
---|
| 34 | |
---|
| 35 | Vector3 rotationRatio; |
---|
| 36 | Vector3 translationRatio; |
---|
| 37 | |
---|
| 38 | int old_count = GetObjectCount(); |
---|
| 39 | int failed_attempts = 0; // counter used to avoid invinite loop |
---|
| 40 | |
---|
| 41 | //-- create random values between zero and one |
---|
| 42 | while ((GetObjectCount() < numObjects + old_count) && |
---|
| 43 | (failed_attempts < MAX_FAILED_ATTEMPTS)) |
---|
| 44 | { |
---|
| 45 | rotationRatio.x = rand() / (float) RAND_MAX; |
---|
| 46 | rotationRatio.y = rand() / (float) RAND_MAX; |
---|
| 47 | rotationRatio.z = rand() / (float) RAND_MAX; |
---|
| 48 | |
---|
| 49 | translationRatio.x = rand() / (float) RAND_MAX; |
---|
| 50 | translationRatio.y = rand() / (float) RAND_MAX; |
---|
| 51 | translationRatio.z = rand() / (float) RAND_MAX; |
---|
| 52 | |
---|
| 53 | // Setup the ray scene query
|
---|
| 54 | Vector3 rotation = mMinAngle + rotationRatio * (mMaxAngle - mMinAngle); |
---|
| 55 | Vector3 position = mMinPos + translationRatio * (mMaxPos - mMinPos);
|
---|
| 56 | |
---|
| 57 | // failed to generate new object |
---|
| 58 | if (!GenerateSceneObject(position, rotation, objName)) |
---|
| 59 | ++failed_attempts; |
---|
| 60 | } |
---|
| 61 | }
|
---|
| 62 | //-----------------------------------------------------------------------
|
---|
| 63 | SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position, |
---|
| 64 | const Quaternion &orientation, const String& objName)
|
---|
| 65 | {
|
---|
| 66 | char name[25];
|
---|
| 67 | sprintf(name, "%s Entity%d", objName.c_str(), GetObjectCount());
|
---|
| 68 |
|
---|
| 69 | Entity *ent = mSceneMgr->createEntity(name, objName + ".mesh");
|
---|
| 70 |
|
---|
| 71 | SceneNode *currentObject = mSceneMgr->getRootSceneNode()->
|
---|
| 72 | createChildSceneNode(String(name) + "Node", position);
|
---|
| 73 |
|
---|
[111] | 74 | //ent->setCastShadows(false);
|
---|
[107] | 75 | currentObject->attachObject(ent);
|
---|
| 76 | currentObject->setScale(mScale);
|
---|
| 77 | currentObject->setOrientation(orientation);
|
---|
| 78 |
|
---|
| 79 | //-- store pointer to object
|
---|
| 80 | mSceneObjects.push_back(currentObject);
|
---|
| 81 |
|
---|
| 82 | return currentObject;
|
---|
| 83 | }
|
---|
| 84 | //-----------------------------------------------------------------------
|
---|
| 85 | SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position, |
---|
| 86 | const Vector3 &rotation, const String& objName)
|
---|
| 87 | {
|
---|
| 88 | Matrix3 mat;
|
---|
[112] | 89 | mat.FromEulerAnglesYXZ(Degree(rotation.x), Degree(rotation.y),
|
---|
| 90 | Degree(rotation.z));
|
---|
[107] | 91 |
|
---|
| 92 | return GenerateSceneObject(position, Quaternion(mat), objName);
|
---|
| 93 | }
|
---|
| 94 | //-----------------------------------------------------------------------
|
---|
| 95 | void SceneContentGenerator::SetMinAngle(Vector3 minAngle)
|
---|
| 96 | {
|
---|
| 97 | mMinAngle = minAngle;
|
---|
| 98 | }
|
---|
| 99 | //-----------------------------------------------------------------------
|
---|
| 100 | void SceneContentGenerator::SetMaxAngle(Vector3 maxAngle)
|
---|
| 101 | {
|
---|
| 102 | mMaxAngle = maxAngle;
|
---|
| 103 | }
|
---|
| 104 | //-----------------------------------------------------------------------
|
---|
| 105 | void SceneContentGenerator::SetMinPos(Vector3 minPos)
|
---|
| 106 | {
|
---|
| 107 | mMinPos = minPos;
|
---|
| 108 | }
|
---|
| 109 | //-----------------------------------------------------------------------
|
---|
| 110 | void SceneContentGenerator::SetMaxPos(Vector3 maxPos)
|
---|
| 111 | {
|
---|
| 112 | mMaxPos = maxPos;
|
---|
| 113 | }
|
---|
| 114 | //-----------------------------------------------------------------------
|
---|
| 115 | int SceneContentGenerator::GetObjectCount()
|
---|
| 116 | {
|
---|
| 117 | return (int)mSceneObjects.size();
|
---|
| 118 | }
|
---|
| 119 | //-----------------------------------------------------------------------
|
---|
| 120 | void SceneContentGenerator::SetScale(Vector3 scale)
|
---|
| 121 | {
|
---|
| 122 | mScale = scale;
|
---|
| 123 | }
|
---|
| 124 | //-----------------------------------------------------------------------
|
---|
| 125 | bool SceneContentGenerator::WriteObjects(const std::string &filename)
|
---|
| 126 | {
|
---|
| 127 | std::ofstream ofstr(filename.c_str());
|
---|
| 128 | std::vector<SceneNode *>::const_iterator it, it_end;
|
---|
| 129 |
|
---|
| 130 | it_end = mSceneObjects.end();
|
---|
| 131 |
|
---|
| 132 | if(!ofstr.is_open())
|
---|
| 133 | return false;
|
---|
| 134 |
|
---|
| 135 | char str[100];
|
---|
| 136 |
|
---|
| 137 | for(it = mSceneObjects.begin(); it < it_end; ++it)
|
---|
| 138 | {
|
---|
| 139 | SceneNode *node = (*it);
|
---|
| 140 | sscanf(node->getName().c_str(), "%s ", str); // write name of mesh
|
---|
| 141 |
|
---|
| 142 | ofstr << str << " "
|
---|
| 143 | << StringConverter::toString(node->getPosition()) << " "
|
---|
| 144 | << StringConverter::toString(node->getOrientation()) << " "
|
---|
| 145 | << StringConverter::toString(node->getScale()) << "\n";
|
---|
| 146 | }
|
---|
| 147 | ofstr.close();
|
---|
| 148 |
|
---|
| 149 | return true;
|
---|
| 150 | }
|
---|
| 151 | //-----------------------------------------------------------------------
|
---|
| 152 | bool SceneContentGenerator::LoadObjects(const std::string &filename)
|
---|
| 153 | {
|
---|
| 154 | std::ifstream ifstr(filename.c_str());
|
---|
| 155 |
|
---|
| 156 | char line[256];
|
---|
| 157 |
|
---|
| 158 | Vector3 position;
|
---|
| 159 | Quaternion orientation;
|
---|
| 160 | char objName[100];
|
---|
| 161 |
|
---|
[121] | 162 | if (!ifstr.is_open())
|
---|
[107] | 163 | return false;
|
---|
| 164 |
|
---|
| 165 | //mSceneObjects.clear(); // reset list of objects
|
---|
| 166 |
|
---|
| 167 | while (!ifstr.eof())
|
---|
| 168 | {
|
---|
| 169 | ifstr.getline(line, 256);
|
---|
| 170 | sscanf(line, "%s %f %f %f %f %f %f %f %f %f %f", objName,
|
---|
| 171 | &position.x, &position.y, &position.z,
|
---|
| 172 | &orientation.w, &orientation.x, &orientation.y, &orientation.z,
|
---|
| 173 | &mScale.x, &mScale.y, &mScale.z);
|
---|
| 174 |
|
---|
| 175 | /*Vector3 rot(orientation.getYaw().valueDegrees(),
|
---|
| 176 | orientation.getPitch().valueDegrees(),
|
---|
| 177 | orientation.getRoll().valueDegrees());*/
|
---|
| 178 |
|
---|
[121] | 179 | GenerateSceneObject(position, orientation, objName);
|
---|
[107] | 180 |
|
---|
[121] | 181 | //std::stringstream d; d << StringConverter::toString(position) << " " << StringConverter::toString(orientation);
|
---|
| 182 | //LogManager::getSingleton().logMessage(d.str());
|
---|
[107] | 183 | }
|
---|
| 184 | ifstr.close();
|
---|
| 185 |
|
---|
| 186 | return true;
|
---|
| 187 | }
|
---|
[130] | 188 |
|
---|
[107] | 189 | } // namespace Ogre |
---|