Skip to content

Commit

Permalink
GRAPHICS: Allow for several Ogre::SceneManager
Browse files Browse the repository at this point in the history
One for the world, one for the GUI
  • Loading branch information
DrMcCoy committed Jan 28, 2014
1 parent bdce641 commit d906a51
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 55 deletions.
17 changes: 8 additions & 9 deletions src/graphics/aurora/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace Graphics {

namespace Aurora {

Cube::Cube(const Common::UString &texture) : _entity(0) {
Cube::Cube(const Common::UString &texture, const Common::UString &scene) : Renderable(scene), _entity(0) {
Common::UString name = Common::generateIDRandomString();

try {
Expand Down Expand Up @@ -105,7 +105,7 @@ Cube::Cube(const Common::UString &texture) : _entity(0) {

// Create entity, put the requested material on it and attach it to a scene node

_entity = getOgreSceneManager().createEntity(mesh);
_entity = getOgreSceneManager(_scene).createEntity(mesh);
_entity->setQueryFlags(kSelectableNone);

_entity->getUserObjectBindings().setUserAny("renderable", Ogre::Any((Renderable *) this));
Expand All @@ -114,8 +114,7 @@ Cube::Cube(const Common::UString &texture) : _entity(0) {

_entity->setMaterial(material);

//_rootNode = getOgreSceneManager().createSceneNode(name.c_str());
_rootNode = getOgreSceneManager().getRootSceneNode()->createChildSceneNode(name.c_str());
_rootNode = getOgreSceneManager(_scene).getRootSceneNode()->createChildSceneNode(name.c_str());

_rootNode->attachObject(_entity);
_rootNode->setVisible(false);
Expand All @@ -125,7 +124,7 @@ Cube::Cube(const Common::UString &texture) : _entity(0) {
float animLength = 4.0f;
int animSubDivisions = 4;

Ogre::Animation *anim = getOgreSceneManager().createAnimation(name.c_str(), animLength);
Ogre::Animation *anim = getOgreSceneManager(_scene).createAnimation(name.c_str(), animLength);

Ogre::NodeAnimationTrack *trackX = anim->createNodeTrack(0, _rootNode);
Ogre::NodeAnimationTrack *trackY = anim->createNodeTrack(1, _rootNode);
Expand All @@ -144,7 +143,7 @@ Cube::Cube(const Common::UString &texture) : _entity(0) {
trackZ->createNodeKeyFrame(i * timePerSub)->setRotation(rotZ);
}

Ogre::AnimationState *animState = getOgreSceneManager().createAnimationState(name.c_str());
Ogre::AnimationState *animState = getOgreSceneManager(_scene).createAnimationState(name.c_str());
animState->setEnabled(false);

} catch (std::exception &e) {
Expand All @@ -158,7 +157,7 @@ Cube::~Cube() {

setVisible(false);

Ogre::SceneManager &scene = getOgreSceneManager();
Ogre::SceneManager &scene = getOgreSceneManager(_scene);

destroyAnimation(_rootNode->getName().c_str());

Expand All @@ -174,11 +173,11 @@ Cube::~Cube() {

void Cube::startRotate() {
_rootNode->setInitialState();
getOgreSceneManager().getAnimationState(_rootNode->getName())->setEnabled(true);
getOgreSceneManager(_scene).getAnimationState(_rootNode->getName())->setEnabled(true);
}

void Cube::stopRotate() {
getOgreSceneManager().getAnimationState(_rootNode->getName())->setEnabled(false);
getOgreSceneManager(_scene).getAnimationState(_rootNode->getName())->setEnabled(false);
}

void Cube::setSelectable(bool selectable) {
Expand Down
8 changes: 3 additions & 5 deletions src/graphics/aurora/cube.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,21 @@
#ifndef GRAPHICS_AURORA_CUBE_H
#define GRAPHICS_AURORA_CUBE_H

#include "common/ustring.h"

#include "graphics/renderable.h"

namespace Ogre {
class Entity;
}

namespace Common {
class UString;
}

namespace Graphics {

namespace Aurora {

class Cube : public Renderable {
public:
Cube(const Common::UString &texture);
Cube(const Common::UString &texture, const Common::UString &scene = "world");
~Cube();

void startRotate();
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/aurora/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Model::State::State(const Common::UString &n) : name(n), animation(0), animation
}


Model::Model() : _currentState(0) {
Model::Model(const Common::UString &scene) : Renderable(scene), _currentState(0) {
}

Model::~Model() {
Expand All @@ -75,7 +75,7 @@ Model::~Model() {
for (ModelMap::iterator c = _childModels.begin(); c != _childModels.end(); ++c)
delete c->second;

Ogre::SceneManager &scene = getOgreSceneManager();
Ogre::SceneManager &scene = getOgreSceneManager(_scene);

for (StateMap::iterator s = _states.begin(); s != _states.end(); ++s)
destroyAnimation(s->second->animation);
Expand Down Expand Up @@ -304,7 +304,7 @@ Ogre::Entity *Model::createEntity(const VertexDeclaration &vertexDecl, const Ogr

mesh->load();

Ogre::Entity *entity = getOgreSceneManager().createEntity(mesh);
Ogre::Entity *entity = getOgreSceneManager(_scene).createEntity(mesh);
entity->setQueryFlags(kSelectableNone);

entity->getUserObjectBindings().setUserAny("renderable", Ogre::Any((Renderable *) this));
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/aurora/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace Aurora {

class Model : public Renderable {
public:
Model();
Model(const Common::UString &scene = "world");
~Model();

/** Return the name of every state in this model. */
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/aurora/model_kotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ void Model_KotOR::ParserContext::newNode() {
}


Model_KotOR::Model_KotOR(const Common::UString &name, bool kotor2, const Common::UString &texture) :
_fileName(name) {
Model_KotOR::Model_KotOR(const Common::UString &name, bool kotor2, const Common::UString &texture, const Common::UString &scene) :
Model(scene), _fileName(name) {

ParserContext ctx(name, kotor2, texture);

Expand Down Expand Up @@ -220,7 +220,7 @@ void Model_KotOR::load(ParserContext &ctx) {

readStrings(*ctx.mdl, nameOffsets, ctx.offModelData, ctx.names);

_rootNode = getOgreSceneManager().getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode = getOgreSceneManager(_scene).getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode->setVisible(false);

_states.insert(std::make_pair("", new State));
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/aurora/model_kotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace Aurora {

class Model_KotOR : public Model {
public:
Model_KotOR(const Common::UString &name, bool kotor2, const Common::UString &texture = "");
Model_KotOR(const Common::UString &name, bool kotor2, const Common::UString &texture = "", const Common::UString &scene = "world");
~Model_KotOR();

private:
Expand Down
12 changes: 6 additions & 6 deletions src/graphics/aurora/model_nwn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ Model_NWN::MeshASCII::MeshASCII() : vCount(0), tCount(0), faceCount(0) {
}


Model_NWN::Model_NWN(const Common::UString &name, const Common::UString &texture) :
_fileName(name) {
Model_NWN::Model_NWN(const Common::UString &name, const Common::UString &texture, const Common::UString &scene) :
Model(scene), _fileName(name) {

ParserContext ctx(name, texture);

Expand Down Expand Up @@ -273,7 +273,7 @@ void Model_NWN::loadBinary(ParserContext &ctx) {

_superModelName.readFixedASCII(*ctx.mdl, 64);

_rootNode = getOgreSceneManager().getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode = getOgreSceneManager(_scene).getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode->setVisible(false);

_states.insert(std::make_pair("", new State));
Expand All @@ -293,7 +293,7 @@ void Model_NWN::loadBinary(ParserContext &ctx) {
}

void Model_NWN::loadASCII(ParserContext &ctx) {
_rootNode = getOgreSceneManager().getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode = getOgreSceneManager(_scene).getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode->setVisible(false);

_states.insert(std::make_pair("", new State));
Expand Down Expand Up @@ -831,7 +831,7 @@ void Model_NWN::readBinaryAnim(ParserContext &ctx, uint32 offset) {
float animLength = ctx.mdl->readIEEEFloatLE();
float transTime = ctx.mdl->readIEEEFloatLE();

ctx.state->animation = getOgreSceneManager().createAnimation(Common::generateIDRandomString().c_str(), animLength);
ctx.state->animation = getOgreSceneManager(_scene).createAnimation(Common::generateIDRandomString().c_str(), animLength);

Common::UString animRoot;
animRoot.readFixedASCII(*ctx.mdl, 64);
Expand All @@ -842,7 +842,7 @@ void Model_NWN::readBinaryAnim(ParserContext &ctx, uint32 offset) {
ctx.mdl->seek(ctx.offModelData + nodeHeadPointer);
loadBinaryNode(ctx, _rootNode);

ctx.state->animationState = getOgreSceneManager().createAnimationState(ctx.state->animation->getName());
ctx.state->animationState = getOgreSceneManager(_scene).createAnimationState(ctx.state->animation->getName());
ctx.state->animationState->setEnabled(false);
}

Expand Down
2 changes: 1 addition & 1 deletion src/graphics/aurora/model_nwn.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace Aurora {

class Model_NWN : public Model {
public:
Model_NWN(const Common::UString &name, const Common::UString &texture = "");
Model_NWN(const Common::UString &name, const Common::UString &texture = "", const Common::UString &scene = "world");
~Model_NWN();

private:
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/aurora/model_nwn2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void Model_NWN2::ParserContext::newNode() {
}


Model_NWN2::Model_NWN2(const Common::UString &name) : _fileName(name) {
Model_NWN2::Model_NWN2(const Common::UString &name, const Common::UString &scene) : Model(scene), _fileName(name) {
ParserContext ctx(name);

load(ctx);
Expand Down Expand Up @@ -117,7 +117,7 @@ void Model_NWN2::load(ParserContext &ctx) {
packetKey->offset = ctx.mdb->readUint32LE();
}

_rootNode = getOgreSceneManager().getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode = getOgreSceneManager(_scene).getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode->setVisible(false);

_states.insert(std::make_pair("", new State));
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/aurora/model_nwn2.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace Aurora {

class Model_NWN2 : public Model {
public:
Model_NWN2(const Common::UString &name);
Model_NWN2(const Common::UString &name, const Common::UString &scene = "world");
~Model_NWN2();

private:
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/aurora/model_witcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void Model_Witcher::ParserContext::newNode() {
}


Model_Witcher::Model_Witcher(const Common::UString &name) : _fileName(name) {
Model_Witcher::Model_Witcher(const Common::UString &name, const Common::UString &scene) : Model(scene), _fileName(name) {
ParserContext ctx(name);

load(ctx);
Expand Down Expand Up @@ -179,7 +179,7 @@ void Model_Witcher::load(ParserContext &ctx) {

ctx.mdb->skip(16);

_rootNode = getOgreSceneManager().getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode = getOgreSceneManager(_scene).getRootSceneNode()->createChildSceneNode(Common::generateIDRandomString().c_str());
_rootNode->setVisible(false);

_states.insert(std::make_pair("", new State));
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/aurora/model_witcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace Aurora {

class Model_Witcher : public Model {
public:
Model_Witcher(const Common::UString &name);
Model_Witcher(const Common::UString &name, const Common::UString &scene = "world");
~Model_Witcher();

private:
Expand Down
20 changes: 10 additions & 10 deletions src/graphics/aurora/sceneman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ void SceneManager::destroy(Renderable *r) {
delete r;
}

Cube *SceneManager::createCube(const Common::UString &texture) {
Cube *SceneManager::createCube(const Common::UString &texture, const Common::UString &scene) {
if (!Common::isMainThread()) {
Events::MainThreadFunctor<Cube *> functor(boost::bind(&SceneManager::createCube, this, texture));
Events::MainThreadFunctor<Cube *> functor(boost::bind(&SceneManager::createCube, this, texture, scene));

return RequestMan.callInMainThread(functor);
}
Expand All @@ -96,7 +96,7 @@ Cube *SceneManager::createCube(const Common::UString &texture) {
try {

LOCK_FRAME();
cube = new Cube(texture);
cube = new Cube(texture, scene);

} catch (Common::Exception &e) {
e.add("Failed to create rotating cube with texture \"%s\"", texture.c_str());
Expand All @@ -106,12 +106,12 @@ Cube *SceneManager::createCube(const Common::UString &texture) {
return cube;
}

Model *SceneManager::createModel(const Common::UString &model, const Common::UString &texture) {
Model *SceneManager::createModel(const Common::UString &model, const Common::UString &texture, const Common::UString &scene) {
if (model.empty())
return 0;

if (!Common::isMainThread()) {
Events::MainThreadFunctor<Model *> functor(boost::bind(&SceneManager::createModel, this, model, texture));
Events::MainThreadFunctor<Model *> functor(boost::bind(&SceneManager::createModel, this, model, texture, scene));

return RequestMan.callInMainThread(functor);
}
Expand All @@ -122,15 +122,15 @@ Model *SceneManager::createModel(const Common::UString &model, const Common::USt
LOCK_FRAME();

if (_modelType == kModelTypeNWN)
modelInstance = new Model_NWN(model, texture);
modelInstance = new Model_NWN(model, texture, scene);
else if (_modelType == kModelTypeKotOR)
modelInstance = new Model_KotOR(model, false, texture);
modelInstance = new Model_KotOR(model, false, texture, scene);
else if (_modelType == kModelTypeKotOR2)
modelInstance = new Model_KotOR(model, true, texture);
modelInstance = new Model_KotOR(model, true, texture, scene);
else if (_modelType == kModelTypeNWN2)
modelInstance = new Model_NWN2(model);
modelInstance = new Model_NWN2(model, scene);
else if (_modelType == kModelTypeTheWitcher)
modelInstance = new Model_Witcher(model);
modelInstance = new Model_Witcher(model, scene);
else
throw Common::Exception("No valid model type registered");

Expand Down
4 changes: 2 additions & 2 deletions src/graphics/aurora/sceneman.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class SceneManager : public Common::Singleton<SceneManager> {
void destroy(Renderable *r);

/** Create a simple cube with a rotation animation. */
Cube *createCube(const Common::UString &texture);
Cube *createCube(const Common::UString &texture, const Common::UString &scene = "world");
/** Create a complex Aurora model. */
Model *createModel(const Common::UString &model, const Common::UString &texture = "");
Model *createModel(const Common::UString &model, const Common::UString &texture = "", const Common::UString &scene = "world");

/** Return the nearest renderable at these screen coordinates. */
Renderable *getRenderableAt(int x, int y, SelectableType type, float &distance);
Expand Down
10 changes: 5 additions & 5 deletions src/graphics/renderable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

namespace Graphics {

Renderable::Renderable() : _rootNode(0), _visible(false) {
Renderable::Renderable(const Common::UString &scene) : _scene(scene), _rootNode(0), _visible(false) {
_basePosition[0] = 0.0;
_basePosition[1] = 0.0;
_basePosition[2] = 0.0;
Expand Down Expand Up @@ -188,10 +188,10 @@ void Renderable::showBoundingBox(bool show) {
}

void Renderable::destroyAnimation(const Common::UString &name) {
if (getOgreSceneManager().hasAnimationState(name.c_str()))
getOgreSceneManager().destroyAnimationState(name.c_str());
if (getOgreSceneManager().hasAnimation(name.c_str()))
getOgreSceneManager().destroyAnimation(name.c_str());
if (getOgreSceneManager(_scene).hasAnimationState(name.c_str()))
getOgreSceneManager(_scene).destroyAnimationState(name.c_str());
if (getOgreSceneManager(_scene).hasAnimation(name.c_str()))
getOgreSceneManager(_scene).destroyAnimation(name.c_str());
}

void Renderable::destroyAnimation(Ogre::Animation *anim) {
Expand Down
4 changes: 3 additions & 1 deletion src/graphics/renderable.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Graphics {

class Renderable {
public:
Renderable();
Renderable(const Common::UString &scene = "world");
virtual ~Renderable();

Ogre::SceneNode *getRootNode();
Expand Down Expand Up @@ -87,6 +87,8 @@ class Renderable {
virtual void setSelectable(bool selectable) = 0;

protected:
Common::UString _scene;

Ogre::SceneNode *_rootNode;

bool _visible;
Expand Down
5 changes: 3 additions & 2 deletions src/graphics/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@

#include "common/types.h"
#include "common/util.h"
#include "common/ustring.h"
#include "common/maths.h"

namespace Graphics {

static inline Ogre::SceneManager &getOgreSceneManager() {
Ogre::SceneManager *scene = Ogre::Root::getSingleton().getSceneManager("world");
static inline Ogre::SceneManager &getOgreSceneManager(const Common::UString &name = "world") {
Ogre::SceneManager *scene = Ogre::Root::getSingleton().getSceneManager(name.c_str());
assert(scene);
return *scene;
}
Expand Down

0 comments on commit d906a51

Please sign in to comment.