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