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[300];
|
---|
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 | //ent->setCastShadows(false);
|
---|
68 | node->attachObject(ent);
|
---|
69 | node->setScale(mScale);
|
---|
70 | node->setOrientation(orientation);
|
---|
71 |
|
---|
72 | // store pointer to node and object
|
---|
73 | mSceneNodes.push_back(node);
|
---|
74 | mEntities.push_back(ent);
|
---|
75 |
|
---|
76 | return node;
|
---|
77 | }
|
---|
78 | //-----------------------------------------------------------------------
|
---|
79 | SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position,
|
---|
80 | const Vector3 &rotation, const String& objName)
|
---|
81 | {
|
---|
82 | Matrix3 mat;
|
---|
83 | mat.FromEulerAnglesYXZ(Degree(rotation.x), Degree(rotation.y),
|
---|
84 | Degree(rotation.z));
|
---|
85 |
|
---|
86 | return GenerateSceneObject(position, Quaternion(mat), objName);
|
---|
87 | }
|
---|
88 | //-----------------------------------------------------------------------
|
---|
89 | void SceneContentGenerator::SetMinAngle(Vector3 minAngle)
|
---|
90 | {
|
---|
91 | mMinAngle = minAngle;
|
---|
92 | }
|
---|
93 | //-----------------------------------------------------------------------
|
---|
94 | void SceneContentGenerator::SetMaxAngle(Vector3 maxAngle)
|
---|
95 | {
|
---|
96 | mMaxAngle = maxAngle;
|
---|
97 | }
|
---|
98 | //-----------------------------------------------------------------------
|
---|
99 | void SceneContentGenerator::SetMinPos(Vector3 minPos)
|
---|
100 | {
|
---|
101 | mMinPos = minPos;
|
---|
102 | }
|
---|
103 | //-----------------------------------------------------------------------
|
---|
104 | void SceneContentGenerator::SetMaxPos(Vector3 maxPos)
|
---|
105 | {
|
---|
106 | mMaxPos = maxPos;
|
---|
107 | }
|
---|
108 | //-----------------------------------------------------------------------
|
---|
109 | int SceneContentGenerator::GetObjectCount()
|
---|
110 | {
|
---|
111 | return (int)mSceneNodes.size();
|
---|
112 | }
|
---|
113 | //-----------------------------------------------------------------------
|
---|
114 | void SceneContentGenerator::SetScale(Vector3 scale)
|
---|
115 | {
|
---|
116 | mScale = scale;
|
---|
117 | }
|
---|
118 | //-----------------------------------------------------------------------
|
---|
119 | bool SceneContentGenerator::WriteObjects(const std::string &filename)
|
---|
120 | {
|
---|
121 | std::ofstream ofstr(filename.c_str());
|
---|
122 | std::vector<SceneNode *>::const_iterator it, it_end;
|
---|
123 |
|
---|
124 | it_end = mSceneNodes.end();
|
---|
125 |
|
---|
126 | if(!ofstr.is_open())
|
---|
127 | return false;
|
---|
128 |
|
---|
129 | char str[100];
|
---|
130 |
|
---|
131 | for(it = mSceneNodes.begin(); it < it_end; ++it)
|
---|
132 | {
|
---|
133 | SceneNode *node = (*it);
|
---|
134 | sscanf(node->getName().c_str(), "%s ", str); // write name of mesh
|
---|
135 |
|
---|
136 | ofstr << str << " "
|
---|
137 | << StringConverter::toString(node->getPosition()) << " "
|
---|
138 | << StringConverter::toString(node->getOrientation()) << " "
|
---|
139 | << StringConverter::toString(node->getScale()) << "\n";
|
---|
140 | }
|
---|
141 | ofstr.close();
|
---|
142 |
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 | //-----------------------------------------------------------------------
|
---|
146 | bool SceneContentGenerator::LoadObjects(const std::string &filename)
|
---|
147 | {
|
---|
148 | std::ifstream ifstr(filename.c_str());
|
---|
149 |
|
---|
150 | char line[256];
|
---|
151 |
|
---|
152 | Vector3 position;
|
---|
153 | Quaternion orientation;
|
---|
154 | char objName[100];
|
---|
155 |
|
---|
156 | if (!ifstr.is_open())
|
---|
157 | return false;
|
---|
158 |
|
---|
159 | while (1)
|
---|
160 | {
|
---|
161 | ifstr.getline(line, 256);
|
---|
162 |
|
---|
163 | if (ifstr.eof())
|
---|
164 | break;
|
---|
165 |
|
---|
166 | sscanf(line, "%s %f %f %f %f %f %f %f %f %f %f", objName,
|
---|
167 | &position.x, &position.y, &position.z,
|
---|
168 | &orientation.w, &orientation.x, &orientation.y, &orientation.z,
|
---|
169 | &mScale.x, &mScale.y, &mScale.z);
|
---|
170 |
|
---|
171 | GenerateSceneObject(position, orientation, objName);
|
---|
172 |
|
---|
173 | //std::stringstream d; d << StringConverter::toString(position) << " " << StringConverter::toString(orientation);
|
---|
174 | //LogManager::getSingleton().logMessage(d.str());
|
---|
175 | }
|
---|
176 | ifstr.close();
|
---|
177 |
|
---|
178 | return true;
|
---|
179 | }
|
---|
180 | //-----------------------------------------------------------------------
|
---|
181 | SceneNodeList *SceneContentGenerator::GetGeneratedSceneNodes()
|
---|
182 | {
|
---|
183 | return &mSceneNodes;
|
---|
184 | }
|
---|
185 | //-----------------------------------------------------------------------
|
---|
186 | EntityList *SceneContentGenerator::GetGeneratedEntities()
|
---|
187 | {
|
---|
188 | return &mEntities;
|
---|
189 | }
|
---|
190 | //-----------------------------------------------------------------------
|
---|
191 | void SceneContentGenerator::RemoveGeneratedObjects()
|
---|
192 | {
|
---|
193 | //-- destroy scene nodes and detach entities
|
---|
194 | while (!mSceneNodes.empty())
|
---|
195 | {
|
---|
196 | SceneNode *node = mSceneNodes.back();
|
---|
197 | mSceneNodes.pop_back();
|
---|
198 |
|
---|
199 | //node->detachAllObjects();
|
---|
200 | mSceneMgr->destroySceneNode(node->getName());
|
---|
201 | }
|
---|
202 | //-- remove and destroy entities
|
---|
203 | while (!mEntities.empty())
|
---|
204 | {
|
---|
205 | Entity *ent = mEntities.back();
|
---|
206 | mEntities.pop_back();
|
---|
207 |
|
---|
208 | mSceneMgr->destroyEntity(ent);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | } // namespace Ogre |
---|