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

Revision 112, 5.7 KB checked in by mattausch, 19 years ago (diff)
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)
26//, mObjectCount(0)
27{
28}
29//-----------------------------------------------------------------------
30void 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//-----------------------------------------------------------------------
63SceneNode *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
74        //ent->setCastShadows(false);
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//-----------------------------------------------------------------------
85SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position,
86                                         const Vector3 &rotation, const String& objName)
87{
88        Matrix3 mat;
89        mat.FromEulerAnglesYXZ(Degree(rotation.x), Degree(rotation.y),
90                                                   Degree(rotation.z));
91
92        return GenerateSceneObject(position, Quaternion(mat), objName);
93}
94//-----------------------------------------------------------------------
95void SceneContentGenerator::SetMinAngle(Vector3 minAngle)
96{
97        mMinAngle = minAngle;
98}
99//-----------------------------------------------------------------------
100void SceneContentGenerator::SetMaxAngle(Vector3 maxAngle)
101{
102        mMaxAngle = maxAngle;
103}
104//-----------------------------------------------------------------------
105void SceneContentGenerator::SetMinPos(Vector3 minPos)
106{
107        mMinPos = minPos;
108}
109//-----------------------------------------------------------------------
110void SceneContentGenerator::SetMaxPos(Vector3 maxPos)
111{
112        mMaxPos = maxPos;
113}
114//-----------------------------------------------------------------------
115int SceneContentGenerator::GetObjectCount()
116{
117        return (int)mSceneObjects.size();
118}
119//-----------------------------------------------------------------------
120void SceneContentGenerator::SetScale(Vector3 scale)
121{
122        mScale = scale;
123}
124//-----------------------------------------------------------------------
125bool 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//-----------------------------------------------------------------------
152bool 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
162        if(!ifstr.is_open())
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
179                (void)GenerateSceneObject(position, orientation, objName);
180               
181                std::stringstream d;
182                d << StringConverter::toString(position) << " " << StringConverter::toString(orientation);
183                LogManager::getSingleton().logMessage(d.str());
184        }
185        ifstr.close();
186
187        return true;
188}
189} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.