source: trunk/VUT/Ogre/src/OgreSceneContentGenerator.cpp @ 160

Revision 160, 6.4 KB checked in by mattausch, 19 years ago (diff)

added animation

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