source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreSceneContentGenerator.cpp @ 897

Revision 897, 6.4 KB checked in by mattausch, 18 years ago (diff)

updated to ogre 1.2

RevLine 
[107]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
[160]14/*************************************************************/
15/*           SceneContentGenerator implementation            */
16/*************************************************************/
[107]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)),
[160]25mScale(0.1, 0.1, 0.1)//, mObjectCount(0)
[107]26{
27}
[897]28//-----------------------------------------------------------------------
29void 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        {
[107]38                // Setup the ray scene query
[897]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
[175]43                Vector3 position = Vector3(Math::RangeRandom(mMinPos.x, mMaxPos.x),
44                                                                   Math::RangeRandom(mMinPos.y, mMaxPos.y),
45                                                                   Math::RangeRandom(mMinPos.z, mMaxPos.z));
[897]46
47                // failed to generate new object
48                if (!GenerateSceneObject(position, rotation, objName))
49                        ++ failed_attempts;
50        }
[107]51}
52//-----------------------------------------------------------------------
[897]53SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position,
[107]54                                         const Quaternion &orientation, const String& objName)
55{
56        char name[25];
[159]57
[107]58        sprintf(name, "%s Entity%d", objName.c_str(), GetObjectCount());
59               
60        Entity *ent = mSceneMgr->createEntity(name, objName + ".mesh");
[415]61       
[175]62        ent->setCastShadows(true);
63
[160]64        SceneNode *node = mSceneMgr->getRootSceneNode()->
[107]65                createChildSceneNode(String(name) + "Node", position);
66
[415]67        //std::stringstream d; d << "new node: " << name << "Node";
68        //Ogre::LogManager::getSingleton().logMessage(d.str());
69
[111]70        //ent->setCastShadows(false);
[160]71        node->attachObject(ent);
72        node->setScale(mScale);
73        node->setOrientation(orientation);
[107]74
[160]75        // store pointer to node and object
76        mSceneNodes.push_back(node);
77        mEntities.push_back(ent);
[175]78       
[160]79        return node;
[107]80}
81//-----------------------------------------------------------------------
[897]82SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position,
[107]83                                         const Vector3 &rotation, const String& objName)
84{
85        Matrix3 mat;
[112]86        mat.FromEulerAnglesYXZ(Degree(rotation.x), Degree(rotation.y),
87                                                   Degree(rotation.z));
[107]88
89        return GenerateSceneObject(position, Quaternion(mat), objName);
90}
91//-----------------------------------------------------------------------
92void SceneContentGenerator::SetMinAngle(Vector3 minAngle)
93{
94        mMinAngle = minAngle;
95}
96//-----------------------------------------------------------------------
97void SceneContentGenerator::SetMaxAngle(Vector3 maxAngle)
98{
99        mMaxAngle = maxAngle;
100}
101//-----------------------------------------------------------------------
102void SceneContentGenerator::SetMinPos(Vector3 minPos)
103{
104        mMinPos = minPos;
105}
106//-----------------------------------------------------------------------
107void SceneContentGenerator::SetMaxPos(Vector3 maxPos)
108{
109        mMaxPos = maxPos;
110}
111//-----------------------------------------------------------------------
112int SceneContentGenerator::GetObjectCount()
113{
[160]114        return (int)mSceneNodes.size();
[107]115}
116//-----------------------------------------------------------------------
117void SceneContentGenerator::SetScale(Vector3 scale)
118{
119        mScale = scale;
120}
121//-----------------------------------------------------------------------
122bool SceneContentGenerator::WriteObjects(const std::string &filename)
123{
124        std::ofstream ofstr(filename.c_str());
125        std::vector<SceneNode *>::const_iterator it, it_end;
126
[160]127        it_end = mSceneNodes.end();
[107]128
129        if(!ofstr.is_open())
130                return false;
131
132        char str[100];
133
[160]134        for(it = mSceneNodes.begin(); it < it_end; ++it)
[107]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//-----------------------------------------------------------------------
149bool 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
[121]159        if (!ifstr.is_open())
[107]160                return false;
161
[160]162        //mSceneNodes.clear(); // reset list of objects
[107]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
[121]172                GenerateSceneObject(position, orientation, objName);
[107]173               
[121]174                //std::stringstream d; d << StringConverter::toString(position) << " " << StringConverter::toString(orientation);
175                //LogManager::getSingleton().logMessage(d.str());
[107]176        }
177        ifstr.close();
178
179        return true;
180}
[160]181//-----------------------------------------------------------------------
182SceneNodeList *SceneContentGenerator::GetGeneratedSceneNodes()
183{
184        return &mSceneNodes;
185}
186//-----------------------------------------------------------------------
187EntityList *SceneContentGenerator::GetGeneratedEntities()
188{
189        return &mEntities;
190}
191//-----------------------------------------------------------------------
192void 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               
[897]209                mSceneMgr->destroyEntity(ent);
[160]210        }
211}
[107]212} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.