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