diff --git a/src/game/area.cpp b/src/game/area.cpp index c6f4a8a4..7c4dc0aa 100644 --- a/src/game/area.cpp +++ b/src/game/area.cpp @@ -66,10 +66,11 @@ static const char kPartyLeaderTag[] = "party-leader"; static const char kPartyMember1Tag[] = "party-member-1"; static const char kPartyMember2Tag[] = "party-member-2"; -Area::Area(uint32_t id, GameVersion version, ObjectFactory *objectFactory) : +Area::Area(uint32_t id, GameVersion version, ObjectFactory *objectFactory, SceneGraph *sceneGraph) : Object(id, ObjectType::Area), _version(version), _objectFactory(objectFactory), + _sceneGraph(sceneGraph), _navMesh(new NavMesh()) { assert(_objectFactory); @@ -87,17 +88,16 @@ void Area::load(const string &name, const GffStruct &are, const GffStruct &git) void Area::loadLYT() { ResourceManager &resources = Resources; - SceneGraph &scene = TheSceneGraph; LytFile lyt; lyt.load(wrap(resources.find(_name, ResourceType::AreaLayout))); for (auto &lytRoom : lyt.rooms()) { - shared_ptr model(new ModelSceneNode(resources.findModel(lytRoom.name))); + shared_ptr model(new ModelSceneNode(_sceneGraph, resources.findModel(lytRoom.name))); model->setLocalTransform(glm::translate(glm::mat4(1.0f), lytRoom.position)); model->animate("animloop1", kAnimationLoop); - scene.addRoot(model); + _sceneGraph->addRoot(model); shared_ptr walkmesh(resources.findWalkmesh(lytRoom.name, ResourceType::Walkmesh)); unique_ptr room(new Room(lytRoom.name, lytRoom.position, model, walkmesh)); @@ -134,10 +134,10 @@ void Area::loadPTH() { } pointZ.insert(make_pair(i, z)); - shared_ptr aabb(new CubeSceneNode(0.5f)); + shared_ptr aabb(new CubeSceneNode(_sceneGraph, 0.5f)); aabb->setLocalTransform(glm::translate(glm::mat4(1.0f), glm::vec3(point.x, point.y, z + 0.25f))); - TheSceneGraph.addRoot(aabb); + _sceneGraph->addRoot(aabb); } _navMesh->load(paths, pointZ); @@ -167,7 +167,8 @@ void Area::loadAmbientColor(const GffStruct &are) { (ambientColorValue >> 16) & 0xff); ambientColor /= 255.0f; - TheSceneGraph.setAmbientLightColor(ambientColor); + + _sceneGraph->setAmbientLightColor(ambientColor); } void Area::loadScripts(const GffStruct &are) { @@ -248,7 +249,7 @@ void Area::add(const shared_ptr &object) { shared_ptr sceneNode(object->model()); if (sceneNode) { - TheSceneGraph.addRoot(sceneNode); + _sceneGraph->addRoot(sceneNode); } determineObjectRoom(*object); } @@ -427,7 +428,7 @@ void Area::update(const UpdateContext &updateCtx) { updateSelection(); - TheSceneGraph.prepare(updateCtx.cameraPosition); + _sceneGraph->prepare(updateCtx.cameraPosition); } void Area::updateDelayedCommands() { diff --git a/src/game/area.h b/src/game/area.h index 52e19d8d..199f8d8d 100644 --- a/src/game/area.h +++ b/src/game/area.h @@ -22,6 +22,7 @@ #include "../gui/types.h" #include "../net/types.h" #include "../render/camera/camera.h" +#include "../render/scene/scenegraph.h" #include "../render/types.h" #include "../resources/types.h" #include "../script/variable.h" @@ -47,7 +48,8 @@ public: Area( uint32_t id, resources::GameVersion version, - ObjectFactory *objectFactory); + ObjectFactory *objectFactory, + render::SceneGraph *sceneGraph); void load(const std::string &name, const resources::GffStruct &are, const resources::GffStruct &git); void loadParty(const PartyConfiguration &party, const glm::vec3 &position, float heading); @@ -96,6 +98,7 @@ public: protected: ObjectFactory *_objectFactory { nullptr }; + render::SceneGraph *_sceneGraph { nullptr }; bool _scriptsEnabled { true }; std::function _onPlayerChanged; diff --git a/src/game/game.cpp b/src/game/game.cpp index 6c6dd1ca..e0080cd4 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -72,7 +72,7 @@ void Game::initGameVersion() { } void Game::initObjectFactory() { - _objectFactory = make_unique(_version, _options); + _objectFactory = make_unique(_version, &_sceneGraph, _options); } int Game::run() { @@ -157,7 +157,7 @@ void Game::loadNextModule() { if (_module) { _module->saveTo(_state); } - TheSceneGraph.clear(); + _sceneGraph.clear(); loadModule(_nextModule, _state.party, _nextEntry); _nextModule.clear(); @@ -221,7 +221,7 @@ void Game::drawWorld() { case Screen::InGame: case Screen::Dialog: case Screen::Container: - TheSceneGraph.render(); + _sceneGraph.render(); break; default: break; diff --git a/src/game/game.h b/src/game/game.h index 7d8a1c77..549ffc88 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -20,6 +20,7 @@ #include "../audio/soundinstance.h" #include "../audio/types.h" #include "../gui/gui.h" +#include "../render/scene/scenegraph.h" #include "../render/window.h" #include "../resources/types.h" @@ -87,6 +88,7 @@ protected: Options _options; resources::GameVersion _version { resources::GameVersion::KotOR }; std::unique_ptr _objectFactory; + render::SceneGraph _sceneGraph; std::shared_ptr _module; std::string _nextModule; Screen _screen { Screen::MainMenu }; diff --git a/src/game/gui/mainmenu.cpp b/src/game/gui/mainmenu.cpp index 2ae8379e..db13ebe8 100644 --- a/src/game/gui/mainmenu.cpp +++ b/src/game/gui/mainmenu.cpp @@ -81,7 +81,7 @@ void MainMenu::load(GameVersion version) { transform = glm::scale(transform, glm::vec3(scale)); Control::Scene3D scene; - scene.model = make_shared(Resources.findModel("mainmenu")); + scene.model = make_shared(nullptr, Resources.findModel("mainmenu")); scene.transform = move(transform); control.setScene3D(scene); diff --git a/src/game/multiplayer/area.cpp b/src/game/multiplayer/area.cpp index bbd41e12..44dbccaa 100644 --- a/src/game/multiplayer/area.cpp +++ b/src/game/multiplayer/area.cpp @@ -34,9 +34,10 @@ MultiplayerArea::MultiplayerArea( GameVersion version, MultiplayerMode mode, ObjectFactory *objectFactory, + SceneGraph *sceneGraph, IMultiplayerCallbacks *callbacks ) : - Area(id, version, objectFactory), _callbacks(callbacks) { + Area(id, version, objectFactory, sceneGraph), _callbacks(callbacks) { _scriptsEnabled = mode == MultiplayerMode::Server; } @@ -75,7 +76,7 @@ void MultiplayerArea::execute(const Command &cmd) { } } void MultiplayerArea::executeLoadCreature(const Command &cmd) { - shared_ptr creature(new MultiplayerCreature(cmd.objectId(), _objectFactory, _callbacks)); + shared_ptr creature(new MultiplayerCreature(cmd.objectId(), _objectFactory, _sceneGraph, _callbacks)); creature->setTag(cmd.tag()); for (auto &item : cmd.equipment()) { diff --git a/src/game/multiplayer/area.h b/src/game/multiplayer/area.h index e85b5e65..de1756a4 100644 --- a/src/game/multiplayer/area.h +++ b/src/game/multiplayer/area.h @@ -33,6 +33,7 @@ public: resources::GameVersion version, MultiplayerMode mode, ObjectFactory *objectFactory, + render::SceneGraph *sceneGraph, IMultiplayerCallbacks *callbacks); void execute(const Command &cmd); diff --git a/src/game/multiplayer/creature.cpp b/src/game/multiplayer/creature.cpp index ed34fd93..cfd03f6f 100644 --- a/src/game/multiplayer/creature.cpp +++ b/src/game/multiplayer/creature.cpp @@ -21,12 +21,14 @@ using namespace std; +using namespace reone::render; + namespace reone { namespace game { -MultiplayerCreature::MultiplayerCreature(uint32_t id, ObjectFactory *objectFactory, IMultiplayerCallbacks *callbacks) : - Creature(id, objectFactory), _callbacks(callbacks) { +MultiplayerCreature::MultiplayerCreature(uint32_t id, ObjectFactory *objectFactory, SceneGraph *sceneGraph, IMultiplayerCallbacks *callbacks) : + Creature(id, objectFactory, sceneGraph), _callbacks(callbacks) { } void MultiplayerCreature::setClientTag(const string &clientTag) { diff --git a/src/game/multiplayer/creature.h b/src/game/multiplayer/creature.h index c208d924..7145afea 100644 --- a/src/game/multiplayer/creature.h +++ b/src/game/multiplayer/creature.h @@ -27,7 +27,11 @@ class IMultiplayerCallbacks; class MultiplayerCreature : public Creature { public: - MultiplayerCreature(uint32_t id, ObjectFactory *objectFactory, IMultiplayerCallbacks *callbacks); + MultiplayerCreature( + uint32_t id, + ObjectFactory *objectFactory, + render::SceneGraph *sceneGraph, + IMultiplayerCallbacks *callbacks); void setClientTag(const std::string &clientTag); diff --git a/src/game/multiplayer/door.cpp b/src/game/multiplayer/door.cpp index c571ce18..e919a4fe 100644 --- a/src/game/multiplayer/door.cpp +++ b/src/game/multiplayer/door.cpp @@ -21,11 +21,14 @@ using namespace std; +using namespace reone::render; + namespace reone { namespace game { -MultiplayerDoor::MultiplayerDoor(uint32_t id, IMultiplayerCallbacks *callbacks) : Door(id), _callbacks(callbacks) { +MultiplayerDoor::MultiplayerDoor(uint32_t id, SceneGraph *sceneGraph, IMultiplayerCallbacks *callbacks) : + Door(id, sceneGraph), _callbacks(callbacks) { } void MultiplayerDoor::open(const shared_ptr &trigerrer) { diff --git a/src/game/multiplayer/door.h b/src/game/multiplayer/door.h index fcd723c7..0b9d351b 100644 --- a/src/game/multiplayer/door.h +++ b/src/game/multiplayer/door.h @@ -27,7 +27,7 @@ class IMultiplayerCallbacks; class MultiplayerDoor : public Door { public: - MultiplayerDoor(uint32_t id, IMultiplayerCallbacks *callbacks); + MultiplayerDoor(uint32_t id, render::SceneGraph *sceneGraph, IMultiplayerCallbacks *callbacks); void open(const std::shared_ptr &trigerrer) override; diff --git a/src/game/multiplayer/game.cpp b/src/game/multiplayer/game.cpp index 594511ae..75a3a757 100644 --- a/src/game/multiplayer/game.cpp +++ b/src/game/multiplayer/game.cpp @@ -42,7 +42,7 @@ MultiplayerGame::MultiplayerGame(MultiplayerMode mode, const fs::path &path, con } void MultiplayerGame::initObjectFactory() { - _objectFactory = unique_ptr(new MultiplayerObjectFactory(_version, _mode, this, _options)); + _objectFactory = unique_ptr(new MultiplayerObjectFactory(_version, _mode, &_sceneGraph, this, _options)); } void MultiplayerGame::configure() { diff --git a/src/game/multiplayer/objectfactory.cpp b/src/game/multiplayer/objectfactory.cpp index f4691f4e..111c8f49 100644 --- a/src/game/multiplayer/objectfactory.cpp +++ b/src/game/multiplayer/objectfactory.cpp @@ -33,22 +33,23 @@ namespace game { MultiplayerObjectFactory::MultiplayerObjectFactory( GameVersion version, MultiplayerMode mode, + render::SceneGraph *sceneGraph, IMultiplayerCallbacks *callbacks, const Options &opts ) : - ObjectFactory(version, opts), _mode(mode), _callbacks(callbacks) { + ObjectFactory(version, sceneGraph, opts), _mode(mode), _callbacks(callbacks) { } unique_ptr MultiplayerObjectFactory::newArea() { - return make_unique(_counter++, _version, _mode, this, _callbacks); + return make_unique(_counter++, _version, _mode, this, _sceneGraph, _callbacks); } unique_ptr MultiplayerObjectFactory::newCreature() { - return make_unique(_counter++, this, _callbacks); + return make_unique(_counter++, this, _sceneGraph, _callbacks); } unique_ptr MultiplayerObjectFactory::newDoor() { - return make_unique(_counter++, _callbacks); + return make_unique(_counter++, _sceneGraph, _callbacks); } } // namespace game diff --git a/src/game/multiplayer/objectfactory.h b/src/game/multiplayer/objectfactory.h index ce6635af..a5b02aa3 100644 --- a/src/game/multiplayer/objectfactory.h +++ b/src/game/multiplayer/objectfactory.h @@ -30,6 +30,7 @@ public: MultiplayerObjectFactory( resources::GameVersion version, MultiplayerMode mode, + render::SceneGraph *sceneGraph, IMultiplayerCallbacks *callbacks, const Options &opts); diff --git a/src/game/object/creature.cpp b/src/game/object/creature.cpp index 1a49d9e9..ad118e73 100644 --- a/src/game/object/creature.cpp +++ b/src/game/object/creature.cpp @@ -63,8 +63,8 @@ Creature::Action::Action(ActionType type, const shared_ptr &object, floa Creature::Action::Action(ActionType type, const ExecutionContext &ctx) : type(type), context(ctx) { } -Creature::Creature(uint32_t id, ObjectFactory *objectFactory) : - SpatialObject(id, ObjectType::Creature), _objectFactory(objectFactory) { +Creature::Creature(uint32_t id, ObjectFactory *objectFactory, SceneGraph *sceneGraph) : + SpatialObject(id, ObjectType::Creature, sceneGraph), _objectFactory(objectFactory) { assert(_objectFactory); _drawDistance = 2048.0f; @@ -130,7 +130,7 @@ void Creature::updateAppearance() { // Body if (!_model) { - _model = make_unique(Resources.findModel(bodyModelName)); + _model = make_unique(_sceneGraph, Resources.findModel(bodyModelName)); _model->setLightingEnabled(true); } else { _model->setModel(Resources.findModel(bodyModelName)); diff --git a/src/game/object/creature.h b/src/game/object/creature.h index c2282e63..6b001bb7 100644 --- a/src/game/object/creature.h +++ b/src/game/object/creature.h @@ -71,7 +71,7 @@ public: int pointIdx { 0 }; }; - Creature(uint32_t id, ObjectFactory *objectFactory); + Creature(uint32_t id, ObjectFactory *objectFactory, render::SceneGraph *sceneGraph); void load(const resources::GffStruct &gffs); void load(const CreatureConfiguration &config); diff --git a/src/game/object/door.cpp b/src/game/object/door.cpp index 3a4dc1ca..15d44d24 100644 --- a/src/game/object/door.cpp +++ b/src/game/object/door.cpp @@ -34,7 +34,7 @@ namespace reone { namespace game { -Door::Door(uint32_t id) : SpatialObject(id, ObjectType::Door) { +Door::Door(uint32_t id, SceneGraph *sceneGraph) : SpatialObject(id, ObjectType::Door, sceneGraph) { _drawDistance = FLT_MAX; _fadeDistance = 0.25f * _drawDistance; _selectable = true; @@ -75,7 +75,7 @@ void Door::loadBlueprint(const string &resRef) { string model(table->getString(_blueprint->genericType(), "modelname")); boost::to_lower(model); - _model = make_unique(resources.findModel(model)); + _model = make_unique(_sceneGraph, resources.findModel(model)); _walkmesh = resources.findWalkmesh(model + "0", ResourceType::DoorWalkmesh); } diff --git a/src/game/object/door.h b/src/game/object/door.h index 82ff745c..f90dadfc 100644 --- a/src/game/object/door.h +++ b/src/game/object/door.h @@ -29,7 +29,7 @@ namespace game { class Door : public SpatialObject { public: - Door(uint32_t id); + Door(uint32_t id, render::SceneGraph *sceneGraph); void load(const resources::GffStruct &gffs); virtual void open(const std::shared_ptr &triggerrer); diff --git a/src/game/object/factory.cpp b/src/game/object/factory.cpp index 2dc72446..6cc8c8d2 100644 --- a/src/game/object/factory.cpp +++ b/src/game/object/factory.cpp @@ -19,14 +19,15 @@ using namespace std; +using namespace reone::render; using namespace reone::resources; namespace reone { namespace game { -ObjectFactory::ObjectFactory(GameVersion version, const Options &opts) : - _version(version), _options(opts) { +ObjectFactory::ObjectFactory(GameVersion version, SceneGraph *sceneGraph, const Options &opts) : + _version(version), _sceneGraph(sceneGraph), _options(opts) { } unique_ptr ObjectFactory::newModule() { @@ -34,27 +35,27 @@ unique_ptr ObjectFactory::newModule() { } unique_ptr ObjectFactory::newArea() { - return make_unique(_counter++, _version, this); + return make_unique(_counter++, _version, this, _sceneGraph); } unique_ptr ObjectFactory::newCreature() { - return make_unique(_counter++, this); + return make_unique(_counter++, this, _sceneGraph); } unique_ptr ObjectFactory::newPlaceable() { - return make_unique(_counter++, this); + return make_unique(_counter++, this, _sceneGraph); } unique_ptr ObjectFactory::newDoor() { - return make_unique(_counter++); + return make_unique(_counter++, _sceneGraph); } unique_ptr ObjectFactory::newWaypoint() { - return make_unique(_counter++); + return make_unique(_counter++, _sceneGraph); } unique_ptr ObjectFactory::newTrigger() { - return make_unique(_counter++); + return make_unique(_counter++, _sceneGraph); } unique_ptr ObjectFactory::newItem() { diff --git a/src/game/object/factory.h b/src/game/object/factory.h index 47480611..11dd404a 100644 --- a/src/game/object/factory.h +++ b/src/game/object/factory.h @@ -20,6 +20,7 @@ #include #include +#include "../../render/scene/scenegraph.h" #include "../../resources/types.h" #include "../area.h" @@ -38,7 +39,7 @@ namespace game { class ObjectFactory { public: - ObjectFactory(resources::GameVersion version, const Options &opts); + ObjectFactory(resources::GameVersion version, render::SceneGraph *sceneGraph, const Options &opts); virtual std::unique_ptr newModule(); virtual std::unique_ptr newArea(); @@ -51,6 +52,7 @@ public: protected: resources::GameVersion _version { resources::GameVersion::KotOR }; + render::SceneGraph *_sceneGraph { nullptr }; Options _options; uint32_t _counter { 2 }; // ids 0 and 1 are reserved diff --git a/src/game/object/placeable.cpp b/src/game/object/placeable.cpp index 6c7f4b6c..d8583a7a 100644 --- a/src/game/object/placeable.cpp +++ b/src/game/object/placeable.cpp @@ -36,8 +36,8 @@ namespace reone { namespace game { -Placeable::Placeable(uint32_t id, ObjectFactory *objectFactory) : - SpatialObject(id, ObjectType::Placeable), +Placeable::Placeable(uint32_t id, ObjectFactory *objectFactory, SceneGraph *sceneGraph) : + SpatialObject(id, ObjectType::Placeable, sceneGraph), _objectFactory(objectFactory) { assert(_objectFactory); @@ -69,7 +69,7 @@ void Placeable::loadBlueprint(const string &resRef) { string model(table->getString(_blueprint->appearance(), "modelname")); boost::to_lower(model); - _model = make_unique(Resources.findModel(model)); + _model = make_unique(_sceneGraph, Resources.findModel(model)); _model->setLightingEnabled(true); _walkmesh = Resources.findWalkmesh(model, ResourceType::PlaceableWalkmesh); diff --git a/src/game/object/placeable.h b/src/game/object/placeable.h index 08061c0a..9f8d4b72 100644 --- a/src/game/object/placeable.h +++ b/src/game/object/placeable.h @@ -33,7 +33,7 @@ class ObjectFactory; class Placeable : public SpatialObject { public: - Placeable(uint32_t id, ObjectFactory *objectFactory); + Placeable(uint32_t id, ObjectFactory *objectFactory, render::SceneGraph *sceneGraph); void load(const resources::GffStruct &gffs); diff --git a/src/game/object/spatial.cpp b/src/game/object/spatial.cpp index 2da9ba36..27e06ca0 100644 --- a/src/game/object/spatial.cpp +++ b/src/game/object/spatial.cpp @@ -32,7 +32,8 @@ namespace reone { namespace game { -SpatialObject::SpatialObject(uint32_t id, ObjectType type) : Object(id, type) { +SpatialObject::SpatialObject(uint32_t id, ObjectType type, SceneGraph *sceneGraph) : + Object(id, type), _sceneGraph(sceneGraph) { } float SpatialObject::distanceTo(const glm::vec2 &point) const { diff --git a/src/game/object/spatial.h b/src/game/object/spatial.h index 00d4d31d..e4854d43 100644 --- a/src/game/object/spatial.h +++ b/src/game/object/spatial.h @@ -72,6 +72,7 @@ public: // END Animation protected: + render::SceneGraph *_sceneGraph { nullptr }; glm::vec3 _position { 0.0f }; float _heading { 0.0f }; glm::mat4 _transform { 1.0f }; @@ -83,7 +84,7 @@ protected: std::vector> _items; bool _selectable { false }; - SpatialObject(uint32_t id, ObjectType type); + SpatialObject(uint32_t id, ObjectType type, render::SceneGraph *sceneGraph); virtual void updateTransform(); }; diff --git a/src/game/object/trigger.cpp b/src/game/object/trigger.cpp index 395b371e..cc8c8134 100644 --- a/src/game/object/trigger.cpp +++ b/src/game/object/trigger.cpp @@ -23,13 +23,14 @@ using namespace std; +using namespace reone::render; using namespace reone::resources; namespace reone { namespace game { -Trigger::Trigger(uint32_t id) : SpatialObject(id, ObjectType::Trigger) { +Trigger::Trigger(uint32_t id, SceneGraph *sceneGraph) : SpatialObject(id, ObjectType::Trigger, sceneGraph) { } void Trigger::load(const GffStruct &gffs) { diff --git a/src/game/object/trigger.h b/src/game/object/trigger.h index 202b35ab..1b9a92a4 100644 --- a/src/game/object/trigger.h +++ b/src/game/object/trigger.h @@ -27,7 +27,7 @@ namespace game { class Trigger : public SpatialObject { public: - Trigger(uint32_t id); + Trigger(uint32_t id, render::SceneGraph *sceneGraph); void load(const resources::GffStruct &gffs); diff --git a/src/game/object/waypoint.cpp b/src/game/object/waypoint.cpp index ac26ab54..808c376b 100644 --- a/src/game/object/waypoint.cpp +++ b/src/game/object/waypoint.cpp @@ -21,13 +21,14 @@ #include "glm/glm.hpp" +using namespace reone::render; using namespace reone::resources; namespace reone { namespace game { -Waypoint::Waypoint(uint32_t id) : SpatialObject(id, ObjectType::Waypoint) { +Waypoint::Waypoint(uint32_t id, SceneGraph *sceneGraph) : SpatialObject(id, ObjectType::Waypoint, sceneGraph) { } void Waypoint::load(const GffStruct &gffs) { diff --git a/src/game/object/waypoint.h b/src/game/object/waypoint.h index 1e40a630..270d5954 100644 --- a/src/game/object/waypoint.h +++ b/src/game/object/waypoint.h @@ -27,7 +27,7 @@ namespace game { class Waypoint : public SpatialObject { public: - Waypoint(uint32_t id); + Waypoint(uint32_t id, render::SceneGraph *sceneGraph); void load(const resources::GffStruct &gffs); }; diff --git a/src/render/scene/aabbnode.cpp b/src/render/scene/aabbnode.cpp index 9e5b140a..278e8994 100644 --- a/src/render/scene/aabbnode.cpp +++ b/src/render/scene/aabbnode.cpp @@ -27,7 +27,7 @@ namespace reone { namespace render { -AABBSceneNode::AABBSceneNode(const AABB &aabb) : _aabb(aabb) { +AABBSceneNode::AABBSceneNode(SceneGraph *sceneGraph, const AABB &aabb) : SceneNode(sceneGraph), _aabb(aabb) { } void AABBSceneNode::render() const { diff --git a/src/render/scene/aabbnode.h b/src/render/scene/aabbnode.h index 37da3647..315eb1b5 100644 --- a/src/render/scene/aabbnode.h +++ b/src/render/scene/aabbnode.h @@ -27,7 +27,7 @@ namespace render { class AABBSceneNode : public SceneNode { public: - AABBSceneNode(const AABB &abbb); + AABBSceneNode(SceneGraph *sceneGraph, const AABB &abbb); void render() const override; diff --git a/src/render/scene/cubenode.cpp b/src/render/scene/cubenode.cpp index 252e3120..410705e6 100644 --- a/src/render/scene/cubenode.cpp +++ b/src/render/scene/cubenode.cpp @@ -27,7 +27,7 @@ namespace reone { namespace render { -CubeSceneNode::CubeSceneNode(float size) : _size(size) { +CubeSceneNode::CubeSceneNode(SceneGraph *sceneGraph, float size) : SceneNode(sceneGraph), _size(size) { } void CubeSceneNode::render() const { diff --git a/src/render/scene/cubenode.h b/src/render/scene/cubenode.h index b6fc4820..5660c2d1 100644 --- a/src/render/scene/cubenode.h +++ b/src/render/scene/cubenode.h @@ -25,7 +25,7 @@ namespace render { class CubeSceneNode : public SceneNode { public: - CubeSceneNode(float size); + CubeSceneNode(SceneGraph *sceneGraph, float size); void render() const override; diff --git a/src/render/scene/lightnode.cpp b/src/render/scene/lightnode.cpp index 70676136..0d981565 100644 --- a/src/render/scene/lightnode.cpp +++ b/src/render/scene/lightnode.cpp @@ -25,12 +25,12 @@ namespace reone { namespace render { -LightSceneNode::LightSceneNode(const ModelNode *modelNode) : _modelNode(modelNode) { +LightSceneNode::LightSceneNode(SceneGraph *sceneGraph, const ModelNode *modelNode) : SceneNode(sceneGraph), _modelNode(modelNode) { assert(_modelNode); } void LightSceneNode::fillSceneGraph() { - TheSceneGraph.addLight(this); + _sceneGraph->addLight(this); } const ModelNode &LightSceneNode::modelNode() const { diff --git a/src/render/scene/lightnode.h b/src/render/scene/lightnode.h index 3e4e633c..6fe6b859 100644 --- a/src/render/scene/lightnode.h +++ b/src/render/scene/lightnode.h @@ -27,7 +27,7 @@ class ModelNode; class LightSceneNode : public SceneNode { public: - LightSceneNode(const ModelNode *modelNode); + LightSceneNode(SceneGraph *sceneGraph, const ModelNode *modelNode); void fillSceneGraph() override; diff --git a/src/render/scene/meshnode.cpp b/src/render/scene/meshnode.cpp index c94334ab..ea438f2a 100644 --- a/src/render/scene/meshnode.cpp +++ b/src/render/scene/meshnode.cpp @@ -31,16 +31,17 @@ namespace reone { namespace render { -MeshSceneNode::MeshSceneNode(const ModelSceneNode *model, const ModelNode *modelNode) : _model(model), _modelNode(modelNode) { +MeshSceneNode::MeshSceneNode(SceneGraph *sceneGraph, const ModelSceneNode *model, const ModelNode *modelNode) : + SceneNode(sceneGraph), _model(model), _modelNode(modelNode) { + assert(_model && _modelNode); } void MeshSceneNode::fillSceneGraph() { - SceneGraph &scene = TheSceneGraph; if (isTransparent()) { - scene.addTransparentMesh(this); + _sceneGraph->addTransparentMesh(this); } else { - scene.addOpaqueMesh(this); + _sceneGraph->addOpaqueMesh(this); } SceneNode::fillSceneGraph(); } @@ -128,7 +129,7 @@ void MeshSceneNode::render() const { shaders.setUniform("lightingEnabled", true); shaders.setUniform("lightCount", lightCount); - shaders.setUniform("ambientLightColor", TheSceneGraph.ambientLightColor()); + shaders.setUniform("ambientLightColor", _sceneGraph->ambientLightColor()); for (int i = 0; i < lightCount; ++i) { LightSceneNode *light = lights[i]; diff --git a/src/render/scene/meshnode.h b/src/render/scene/meshnode.h index 4d88a86b..0f6c61da 100644 --- a/src/render/scene/meshnode.h +++ b/src/render/scene/meshnode.h @@ -32,7 +32,7 @@ class ModelSceneNode; class MeshSceneNode : public SceneNode { public: - MeshSceneNode(const ModelSceneNode *model, const ModelNode *modelNode); + MeshSceneNode(SceneGraph *sceneGraph, const ModelSceneNode *model, const ModelNode *modelNode); void fillSceneGraph() override; void updateDistanceToCamera(const glm::vec3 &cameraPosition); diff --git a/src/render/scene/modelnode.cpp b/src/render/scene/modelnode.cpp index 8c9b079c..52fd844e 100644 --- a/src/render/scene/modelnode.cpp +++ b/src/render/scene/modelnode.cpp @@ -40,7 +40,7 @@ namespace reone { namespace render { -ModelSceneNode::ModelSceneNode(const shared_ptr &model) : _model(model) { +ModelSceneNode::ModelSceneNode(SceneGraph *sceneGraph, const shared_ptr &model) : SceneNode(sceneGraph), _model(model) { assert(_model); initChildren(); } @@ -57,11 +57,11 @@ void ModelSceneNode::initChildren() { nodes.push(child.get()); } if (shouldRender(*node)) { - shared_ptr mesh(new MeshSceneNode(this, node)); + shared_ptr mesh(new MeshSceneNode(_sceneGraph, this, node)); mesh->setParent(this); mesh->setLocalTransform(node->absoluteTransform()); - shared_ptr aabb(new AABBSceneNode(node->mesh()->aabb())); + shared_ptr aabb(new AABBSceneNode(_sceneGraph, node->mesh()->aabb())); mesh->addChild(aabb); _meshes.insert(make_pair(node->nodeNumber(), mesh)); @@ -69,7 +69,7 @@ void ModelSceneNode::initChildren() { shared_ptr modelLight(node->light()); if (modelLight) { - shared_ptr light(new LightSceneNode(node)); + shared_ptr light(new LightSceneNode(_sceneGraph, node)); light->setParent(this); light->setLocalTransform(node->absoluteTransform()); @@ -144,7 +144,7 @@ void ModelSceneNode::attach(const string &parentNode, const shared_ptr &m } if (model) { - shared_ptr child(new ModelSceneNode(model)); + shared_ptr child(new ModelSceneNode(_sceneGraph, model)); child->setLocalTransform(parent->absoluteTransform()); child->setLightingEnabled(_lightingEnabled); addChild(child); @@ -319,7 +319,7 @@ void ModelSceneNode::updateLighting() { _lightsAffectedBy.clear(); glm::vec3 center(_absoluteTransform * glm::vec4(_model->aabb().center(), 1.0f)); - TheSceneGraph.getLightsAt(center, _lightsAffectedBy); + _sceneGraph->getLightsAt(center, _lightsAffectedBy); _lightingDirty = false; for (auto &attached : _attachedModels) { diff --git a/src/render/scene/modelnode.h b/src/render/scene/modelnode.h index ec3beb0e..d01274d7 100644 --- a/src/render/scene/modelnode.h +++ b/src/render/scene/modelnode.h @@ -49,7 +49,7 @@ public: std::unordered_map boneTransforms; }; - ModelSceneNode(const std::shared_ptr &model); + ModelSceneNode(SceneGraph *sceneGraph, const std::shared_ptr &model); void attach(const std::string &parentNode, const std::shared_ptr &model); void update(float dt); diff --git a/src/render/scene/scenegraph.cpp b/src/render/scene/scenegraph.cpp index 3773c35f..72eac876 100644 --- a/src/render/scene/scenegraph.cpp +++ b/src/render/scene/scenegraph.cpp @@ -30,11 +30,6 @@ namespace render { static const int kMaxLightCount = 8; -SceneGraph &SceneGraph::instance() { - static SceneGraph graph; - return graph; -} - void SceneGraph::clear() { _opaqueMeshes.clear(); _transparentMeshes.clear(); diff --git a/src/render/scene/scenegraph.h b/src/render/scene/scenegraph.h index c192db53..87a8f686 100644 --- a/src/render/scene/scenegraph.h +++ b/src/render/scene/scenegraph.h @@ -31,7 +31,7 @@ namespace render { class SceneGraph { public: - static SceneGraph &instance(); + SceneGraph() = default; void clear(); void addRoot(const std::shared_ptr &node); @@ -54,13 +54,10 @@ private: std::vector _lights; glm::vec3 _ambientLightColor { 1.0f }; - SceneGraph() = default; SceneGraph(const SceneGraph &) = delete; SceneGraph &operator=(const SceneGraph &) = delete; }; -#define TheSceneGraph render::SceneGraph::instance() - } // namespace render } // namespace reone diff --git a/src/render/scene/scenenode.cpp b/src/render/scene/scenenode.cpp index c07e741e..219050cf 100644 --- a/src/render/scene/scenenode.cpp +++ b/src/render/scene/scenenode.cpp @@ -29,6 +29,9 @@ namespace reone { namespace render { +SceneNode::SceneNode(SceneGraph *sceneGraph) : _sceneGraph(sceneGraph) { +} + void SceneNode::addChild(const shared_ptr &node) { assert(node); node->setParent(this); diff --git a/src/render/scene/scenenode.h b/src/render/scene/scenenode.h index 04e33af5..0e174e96 100644 --- a/src/render/scene/scenenode.h +++ b/src/render/scene/scenenode.h @@ -47,12 +47,13 @@ public: virtual void setLocalTransform(const glm::mat4 &transform); protected: + SceneGraph *_sceneGraph { nullptr }; const SceneNode *_parent { nullptr }; glm::mat4 _localTransform { 1.0f }; glm::mat4 _absoluteTransform { 1.0f }; std::vector> _children; - SceneNode() = default; + SceneNode(SceneGraph *sceneGraph); virtual void updateAbsoluteTransform();