some cleanup

This commit is contained in:
Marc Zinnschlag 2012-02-06 10:41:13 +01:00
parent e1600d9a2a
commit 784c6fe0ff
2 changed files with 64 additions and 59 deletions

View file

@ -1,11 +1,11 @@
#include "objects.hpp" #include "objects.hpp"
#include <OgreSceneNode.h> #include <OgreSceneNode.h>
#include <components/nifogre/ogre_nif_loader.hpp> #include <components/nifogre/ogre_nif_loader.hpp>
using namespace Ogre;
using namespace MWRender; using namespace MWRender;
bool Objects::lightConst = false; bool Objects::lightConst = false;
float Objects::lightConstValue = 0.0f; float Objects::lightConstValue = 0.0f;
@ -30,14 +30,17 @@ void Objects::clearSceneNode (Ogre::SceneNode *node)
{ {
Ogre::MovableObject *object = node->getAttachedObject (i); Ogre::MovableObject *object = node->getAttachedObject (i);
node->detachObject (object); node->detachObject (object);
mRend.getScene()->destroyMovableObject (object); mRenderer.getScene()->destroyMovableObject (object);
} }
} }
void Objects::setMwRoot(Ogre::SceneNode* root){ void Objects::setMwRoot(Ogre::SceneNode* root)
{
mMwRoot = root; mMwRoot = root;
} }
void Objects::insertBegin (const MWWorld::Ptr& ptr, bool enabled, bool static_){
void Objects::insertBegin (const MWWorld::Ptr& ptr, bool enabled, bool static_)
{
Ogre::SceneNode* root = mMwRoot; Ogre::SceneNode* root = mMwRoot;
Ogre::SceneNode* cellnode; Ogre::SceneNode* cellnode;
if(mCellSceneNodes.find(ptr.getCell()) == mCellSceneNodes.end()) if(mCellSceneNodes.find(ptr.getCell()) == mCellSceneNodes.end())
@ -60,13 +63,13 @@ void Objects::insertBegin (const MWWorld::Ptr& ptr, bool enabled, bool static_){
f = ptr.getCellRef().pos.rot; f = ptr.getCellRef().pos.rot;
// Rotate around X axis // Rotate around X axis
Quaternion xr(Radian(-f[0]), Vector3::UNIT_X); Ogre::Quaternion xr(Ogre::Radian(-f[0]), Ogre::Vector3::UNIT_X);
// Rotate around Y axis // Rotate around Y axis
Quaternion yr(Radian(-f[1]), Vector3::UNIT_Y); Ogre::Quaternion yr(Ogre::Radian(-f[1]), Ogre::Vector3::UNIT_Y);
// Rotate around Z axis // Rotate around Z axis
Quaternion zr(Radian(-f[2]), Vector3::UNIT_Z); Ogre::Quaternion zr(Ogre::Radian(-f[2]), Ogre::Vector3::UNIT_Z);
// Rotates first around z, then y, then x // Rotates first around z, then y, then x
insert->setOrientation(xr*yr*zr); insert->setOrientation(xr*yr*zr);
@ -74,55 +77,55 @@ void Objects::insertBegin (const MWWorld::Ptr& ptr, bool enabled, bool static_){
if (!enabled) if (!enabled)
insert->setVisible (false); insert->setVisible (false);
ptr.getRefData().setBaseNode(insert); ptr.getRefData().setBaseNode(insert);
isStatic = static_; mIsStatic = static_;
} }
void Objects::insertMesh (const MWWorld::Ptr& ptr, const std::string& mesh){
void Objects::insertMesh (const MWWorld::Ptr& ptr, const std::string& mesh)
{
Ogre::SceneNode* insert = ptr.getRefData().getBaseNode(); Ogre::SceneNode* insert = ptr.getRefData().getBaseNode();
assert(insert); assert(insert);
NifOgre::NIFLoader::load(mesh); NifOgre::NIFLoader::load(mesh);
Entity *ent = mRend.getScene()->createEntity(mesh); Ogre::Entity *ent = mRenderer.getScene()->createEntity(mesh);
if(!isStatic) if(!mIsStatic)
{ {
insert->attachObject(ent); insert->attachObject(ent);
} }
else else
{ {
Ogre::StaticGeometry* sg = 0; Ogre::StaticGeometry* sg = 0;
if(mSG.find(ptr.getCell()) == mSG.end()) if(mStaticGeometry.find(ptr.getCell()) == mStaticGeometry.end())
{ {
uniqueID = uniqueID +1; uniqueID = uniqueID +1;
sg = mRend.getScene()->createStaticGeometry( "sg" + Ogre::StringConverter::toString(uniqueID)); sg = mRenderer.getScene()->createStaticGeometry( "sg" + Ogre::StringConverter::toString(uniqueID));
//Create the scenenode and put it in the map //Create the scenenode and put it in the map
mSG[ptr.getCell()] = sg; mStaticGeometry[ptr.getCell()] = sg;
} }
else else
{ {
sg = mSG[ptr.getCell()]; sg = mStaticGeometry[ptr.getCell()];
} }
sg->addEntity(ent,insert->_getDerivedPosition(),insert->_getDerivedOrientation(),insert->_getDerivedScale()); sg->addEntity(ent,insert->_getDerivedPosition(),insert->_getDerivedOrientation(),insert->_getDerivedScale());
sg->setRegionDimensions(Ogre::Vector3(100000,10000,100000)); sg->setRegionDimensions(Ogre::Vector3(100000,10000,100000));
mRenderer.getScene()->destroyEntity(ent);
mRend.getScene()->destroyEntity(ent);
} }
} }
void Objects::insertLight (const MWWorld::Ptr& ptr, float r, float g, float b, float radius){
Ogre::SceneNode* insert = mRend.getScene()->getSceneNode(ptr.getRefData().getHandle()); void Objects::insertLight (const MWWorld::Ptr& ptr, float r, float g, float b, float radius)
{
Ogre::SceneNode* insert = mRenderer.getScene()->getSceneNode(ptr.getRefData().getHandle());
assert(insert); assert(insert);
Ogre::Light *light = mRend.getScene()->createLight(); Ogre::Light *light = mRenderer.getScene()->createLight();
light->setDiffuseColour (r, g, b); light->setDiffuseColour (r, g, b);
float cval=0.0f, lval=0.0f, qval=0.0f; float cval=0.0f, lval=0.0f, qval=0.0f;
if(lightConst) if(lightConst)
cval = lightConstValue; cval = lightConstValue;
if(!lightOutQuadInLin) if(!lightOutQuadInLin)
{ {
if(lightLinear) if(lightLinear)
@ -159,7 +162,7 @@ bool Objects::deleteObject (const MWWorld::Ptr& ptr)
{ {
clearSceneNode (base); clearSceneNode (base);
base->removeAndDestroyAllChildren(); base->removeAndDestroyAllChildren();
mRend.getScene()->destroySceneNode (base); mRenderer.getScene()->destroySceneNode (base);
ptr.getRefData().setBaseNode (0); ptr.getRefData().setBaseNode (0);
return true; return true;
} }
@ -170,7 +173,8 @@ bool Objects::deleteObject (const MWWorld::Ptr& ptr)
return true; return true;
} }
void Objects::removeCell(MWWorld::Ptr::CellStore* store){ void Objects::removeCell(MWWorld::Ptr::CellStore* store)
{
if(mCellSceneNodes.find(store) != mCellSceneNodes.end()) if(mCellSceneNodes.find(store) != mCellSceneNodes.end())
{ {
Ogre::SceneNode* base = mCellSceneNodes[store]; Ogre::SceneNode* base = mCellSceneNodes[store];
@ -180,23 +184,24 @@ void Objects::removeCell(MWWorld::Ptr::CellStore* store){
base->removeAndDestroyAllChildren(); base->removeAndDestroyAllChildren();
mCellSceneNodes.erase(store); mCellSceneNodes.erase(store);
mRend.getScene()->destroySceneNode(base); mRenderer.getScene()->destroySceneNode(base);
base = 0; base = 0;
} }
if(mSG.find(store) != mSG.end()) if(mStaticGeometry.find(store) != mStaticGeometry.end())
{ {
Ogre::StaticGeometry* sg = mSG[store]; Ogre::StaticGeometry* sg = mStaticGeometry[store];
mSG.erase(store); mStaticGeometry.erase(store);
mRend.getScene()->destroyStaticGeometry (sg); mRenderer.getScene()->destroyStaticGeometry (sg);
sg = 0; sg = 0;
} }
} }
void Objects::buildStaticGeometry(ESMS::CellStore<MWWorld::RefData>& cell){ void Objects::buildStaticGeometry(ESMS::CellStore<MWWorld::RefData>& cell)
if(mSG.find(&cell) != mSG.end()) {
if(mStaticGeometry.find(&cell) != mStaticGeometry.end())
{ {
Ogre::StaticGeometry* sg = mSG[&cell]; Ogre::StaticGeometry* sg = mStaticGeometry[&cell];
sg->build(); sg->build();
} }
} }

View file

@ -11,11 +11,11 @@
namespace MWRender{ namespace MWRender{
class Objects{ class Objects{
OEngine::Render::OgreRenderer &mRend; OEngine::Render::OgreRenderer &mRenderer;
std::map<MWWorld::Ptr::CellStore *, Ogre::SceneNode *> mCellSceneNodes; std::map<MWWorld::Ptr::CellStore *, Ogre::SceneNode *> mCellSceneNodes;
std::map<MWWorld::Ptr::CellStore *, Ogre::StaticGeometry*> mSG; std::map<MWWorld::Ptr::CellStore *, Ogre::StaticGeometry*> mStaticGeometry;
Ogre::SceneNode* mMwRoot; Ogre::SceneNode* mMwRoot;
bool isStatic; bool mIsStatic;
static int uniqueID; static int uniqueID;
static bool lightConst; static bool lightConst;
static float lightConstValue; static float lightConstValue;
@ -36,7 +36,7 @@ class Objects{
///< Remove all movable objects from \a node. ///< Remove all movable objects from \a node.
public: public:
Objects(OEngine::Render::OgreRenderer& _rend): mRend(_rend){} Objects(OEngine::Render::OgreRenderer& renderer): mRenderer (renderer){}
~Objects(){} ~Objects(){}
void insertBegin (const MWWorld::Ptr& ptr, bool enabled, bool static_); void insertBegin (const MWWorld::Ptr& ptr, bool enabled, bool static_);
void insertMesh (const MWWorld::Ptr& ptr, const std::string& mesh); void insertMesh (const MWWorld::Ptr& ptr, const std::string& mesh);