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

Revision 2280, 6.3 KB checked in by mattausch, 17 years ago (diff)

removed dependency on ogre in gtpvisibility

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{
[2280]56        char name[300];
[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
[111]67        //ent->setCastShadows(false);
[160]68        node->attachObject(ent);
69        node->setScale(mScale);
70        node->setOrientation(orientation);
[107]71
[160]72        // store pointer to node and object
73        mSceneNodes.push_back(node);
74        mEntities.push_back(ent);
[175]75       
[160]76        return node;
[107]77}
78//-----------------------------------------------------------------------
[897]79SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position,
[107]80                                         const Vector3 &rotation, const String& objName)
81{
82        Matrix3 mat;
[112]83        mat.FromEulerAnglesYXZ(Degree(rotation.x), Degree(rotation.y),
84                                                   Degree(rotation.z));
[107]85
86        return GenerateSceneObject(position, Quaternion(mat), objName);
87}
88//-----------------------------------------------------------------------
89void SceneContentGenerator::SetMinAngle(Vector3 minAngle)
90{
91        mMinAngle = minAngle;
92}
93//-----------------------------------------------------------------------
94void SceneContentGenerator::SetMaxAngle(Vector3 maxAngle)
95{
96        mMaxAngle = maxAngle;
97}
98//-----------------------------------------------------------------------
99void SceneContentGenerator::SetMinPos(Vector3 minPos)
100{
101        mMinPos = minPos;
102}
103//-----------------------------------------------------------------------
104void SceneContentGenerator::SetMaxPos(Vector3 maxPos)
105{
106        mMaxPos = maxPos;
107}
108//-----------------------------------------------------------------------
109int SceneContentGenerator::GetObjectCount()
110{
[160]111        return (int)mSceneNodes.size();
[107]112}
113//-----------------------------------------------------------------------
114void SceneContentGenerator::SetScale(Vector3 scale)
115{
116        mScale = scale;
117}
118//-----------------------------------------------------------------------
119bool SceneContentGenerator::WriteObjects(const std::string &filename)
120{
121        std::ofstream ofstr(filename.c_str());
122        std::vector<SceneNode *>::const_iterator it, it_end;
123
[160]124        it_end = mSceneNodes.end();
[107]125
126        if(!ofstr.is_open())
127                return false;
128
129        char str[100];
130
[160]131        for(it = mSceneNodes.begin(); it < it_end; ++it)
[107]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//-----------------------------------------------------------------------
146bool 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
[121]156        if (!ifstr.is_open())
[107]157                return false;
158
[2278]159        while (1)
[107]160        {
161                ifstr.getline(line, 256);
[2278]162
163                if (ifstr.eof())
164                        break;
165
[107]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
[121]171                GenerateSceneObject(position, orientation, objName);
[107]172               
[121]173                //std::stringstream d; d << StringConverter::toString(position) << " " << StringConverter::toString(orientation);
174                //LogManager::getSingleton().logMessage(d.str());
[107]175        }
176        ifstr.close();
177
178        return true;
179}
[160]180//-----------------------------------------------------------------------
181SceneNodeList *SceneContentGenerator::GetGeneratedSceneNodes()
182{
183        return &mSceneNodes;
184}
185//-----------------------------------------------------------------------
186EntityList *SceneContentGenerator::GetGeneratedEntities()
187{
188        return &mEntities;
189}
190//-----------------------------------------------------------------------
191void 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               
[897]208                mSceneMgr->destroyEntity(ent);
[160]209        }
210}
[107]211} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.