diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index b2b534031..a7a694463 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -69,7 +69,8 @@ opencs_units (view/render ) opencs_units_noqt (view/render - navigation navigation1st navigationfree navigationorbit + navigation navigation1st navigationfree navigationorbit lighting lightingday lightingnight + lightingbright ) opencs_units_noqt (view/world diff --git a/apps/opencs/view/render/lighting.cpp b/apps/opencs/view/render/lighting.cpp new file mode 100644 index 000000000..d57570d69 --- /dev/null +++ b/apps/opencs/view/render/lighting.cpp @@ -0,0 +1,4 @@ + +#include "lighting.hpp" + +CSVRender::Lighting::~Lighting() {} \ No newline at end of file diff --git a/apps/opencs/view/render/lighting.hpp b/apps/opencs/view/render/lighting.hpp new file mode 100644 index 000000000..a1da9f7e3 --- /dev/null +++ b/apps/opencs/view/render/lighting.hpp @@ -0,0 +1,27 @@ +#ifndef OPENCS_VIEW_LIGHTING_H +#define OPENCS_VIEW_LIGHTING_H + +namespace Ogre +{ + class SceneManager; + class ColourValue; +} + +namespace CSVRender +{ + class Lighting + { + public: + + virtual ~Lighting(); + + virtual void activate (Ogre::SceneManager *sceneManager, + const Ogre::ColourValue *defaultAmbient = 0) = 0; + + virtual void deactivate() = 0; + + virtual void setDefaultAmbient (const Ogre::ColourValue& colour) = 0; + }; +} + +#endif diff --git a/apps/opencs/view/render/lightingbright.cpp b/apps/opencs/view/render/lightingbright.cpp new file mode 100644 index 000000000..ab845b924 --- /dev/null +++ b/apps/opencs/view/render/lightingbright.cpp @@ -0,0 +1,30 @@ + +#include "lightingbright.hpp" + +#include + +CSVRender::LightingBright::LightingBright() : mSceneManager (0), mLight (0) {} + +void CSVRender::LightingBright::activate (Ogre::SceneManager *sceneManager, + const Ogre::ColourValue *defaultAmbient) +{ + mSceneManager = sceneManager; + + mSceneManager->setAmbientLight (Ogre::ColourValue (1.0, 1.0, 1.0, 1)); + + mLight = mSceneManager->createLight(); + mLight->setType (Ogre::Light::LT_DIRECTIONAL); + mLight->setDirection (Ogre::Vector3 (0, 0, -1)); + mLight->setDiffuseColour (Ogre::ColourValue (1.0, 1.0, 1.0)); +} + +void CSVRender::LightingBright::deactivate() +{ + if (mLight) + { + mSceneManager->destroyLight (mLight); + mLight = 0; + } +} + +void CSVRender::LightingBright::setDefaultAmbient (const Ogre::ColourValue& colour) {} \ No newline at end of file diff --git a/apps/opencs/view/render/lightingbright.hpp b/apps/opencs/view/render/lightingbright.hpp new file mode 100644 index 000000000..bc01899cb --- /dev/null +++ b/apps/opencs/view/render/lightingbright.hpp @@ -0,0 +1,31 @@ +#ifndef OPENCS_VIEW_LIGHTING_BRIGHT_H +#define OPENCS_VIEW_LIGHTING_BRIGHT_H + +#include "lighting.hpp" + +namespace Ogre +{ + class Light; +} + +namespace CSVRender +{ + class LightingBright : public Lighting + { + Ogre::SceneManager *mSceneManager; + Ogre::Light *mLight; + + public: + + LightingBright(); + + virtual void activate (Ogre::SceneManager *sceneManager, + const Ogre::ColourValue *defaultAmbient = 0); + + virtual void deactivate(); + + virtual void setDefaultAmbient (const Ogre::ColourValue& colour); + }; +} + +#endif diff --git a/apps/opencs/view/render/lightingday.cpp b/apps/opencs/view/render/lightingday.cpp new file mode 100644 index 000000000..ab0257c0c --- /dev/null +++ b/apps/opencs/view/render/lightingday.cpp @@ -0,0 +1,36 @@ + +#include "lightingday.hpp" + +#include + +CSVRender::LightingDay::LightingDay() : mSceneManager (0), mLight (0) {} + +void CSVRender::LightingDay::activate (Ogre::SceneManager *sceneManager, + const Ogre::ColourValue *defaultAmbient) +{ + mSceneManager = sceneManager; + + if (defaultAmbient) + mSceneManager->setAmbientLight (*defaultAmbient); + else + mSceneManager->setAmbientLight (Ogre::ColourValue (0.7, 0.7, 0.7, 1)); + + mLight = mSceneManager->createLight(); + mLight->setType (Ogre::Light::LT_DIRECTIONAL); + mLight->setDirection (Ogre::Vector3 (0, 0, -1)); + mLight->setDiffuseColour (Ogre::ColourValue (1, 1, 1)); +} + +void CSVRender::LightingDay::deactivate() +{ + if (mLight) + { + mSceneManager->destroyLight (mLight); + mLight = 0; + } +} + +void CSVRender::LightingDay::setDefaultAmbient (const Ogre::ColourValue& colour) +{ + mSceneManager->setAmbientLight (colour); +} \ No newline at end of file diff --git a/apps/opencs/view/render/lightingday.hpp b/apps/opencs/view/render/lightingday.hpp new file mode 100644 index 000000000..8638146e2 --- /dev/null +++ b/apps/opencs/view/render/lightingday.hpp @@ -0,0 +1,31 @@ +#ifndef OPENCS_VIEW_LIGHTING_DAY_H +#define OPENCS_VIEW_LIGHTING_DAY_H + +#include "lighting.hpp" + +namespace Ogre +{ + class Light; +} + +namespace CSVRender +{ + class LightingDay : public Lighting + { + Ogre::SceneManager *mSceneManager; + Ogre::Light *mLight; + + public: + + LightingDay(); + + virtual void activate (Ogre::SceneManager *sceneManager, + const Ogre::ColourValue *defaultAmbient = 0); + + virtual void deactivate(); + + virtual void setDefaultAmbient (const Ogre::ColourValue& colour); + }; +} + +#endif diff --git a/apps/opencs/view/render/lightingnight.cpp b/apps/opencs/view/render/lightingnight.cpp new file mode 100644 index 000000000..516bb3f40 --- /dev/null +++ b/apps/opencs/view/render/lightingnight.cpp @@ -0,0 +1,36 @@ + +#include "lightingnight.hpp" + +#include + +CSVRender::LightingNight::LightingNight() : mSceneManager (0), mLight (0) {} + +void CSVRender::LightingNight::activate (Ogre::SceneManager *sceneManager, + const Ogre::ColourValue *defaultAmbient) +{ + mSceneManager = sceneManager; + + if (defaultAmbient) + mSceneManager->setAmbientLight (*defaultAmbient); + else + mSceneManager->setAmbientLight (Ogre::ColourValue (0.2, 0.2, 0.2, 1)); + + mLight = mSceneManager->createLight(); + mLight->setType (Ogre::Light::LT_DIRECTIONAL); + mLight->setDirection (Ogre::Vector3 (0, 0, -1)); + mLight->setDiffuseColour (Ogre::ColourValue (0.2, 0.2, 0.2)); +} + +void CSVRender::LightingNight::deactivate() +{ + if (mLight) + { + mSceneManager->destroyLight (mLight); + mLight = 0; + } +} + +void CSVRender::LightingNight::setDefaultAmbient (const Ogre::ColourValue& colour) +{ + mSceneManager->setAmbientLight (colour); +} \ No newline at end of file diff --git a/apps/opencs/view/render/lightingnight.hpp b/apps/opencs/view/render/lightingnight.hpp new file mode 100644 index 000000000..47d1d7ce8 --- /dev/null +++ b/apps/opencs/view/render/lightingnight.hpp @@ -0,0 +1,31 @@ +#ifndef OPENCS_VIEW_LIGHTING_NIGHT_H +#define OPENCS_VIEW_LIGHTING_NIGHT_H + +#include "lighting.hpp" + +namespace Ogre +{ + class Light; +} + +namespace CSVRender +{ + class LightingNight : public Lighting + { + Ogre::SceneManager *mSceneManager; + Ogre::Light *mLight; + + public: + + LightingNight(); + + virtual void activate (Ogre::SceneManager *sceneManager, + const Ogre::ColourValue *defaultAmbient = 0); + + virtual void deactivate(); + + virtual void setDefaultAmbient (const Ogre::ColourValue& colour); + }; +} + +#endif diff --git a/apps/opencs/view/render/scenewidget.cpp b/apps/opencs/view/render/scenewidget.cpp index 5a3c14b49..1aee421ed 100644 --- a/apps/opencs/view/render/scenewidget.cpp +++ b/apps/opencs/view/render/scenewidget.cpp @@ -11,7 +11,10 @@ #include #include +#include "../world/scenetoolmode.hpp" + #include "navigation.hpp" +#include "lighting.hpp" namespace CSVRender { @@ -19,11 +22,12 @@ namespace CSVRender : QWidget(parent) , mWindow(NULL) , mCamera(NULL) - , mSceneMgr(NULL), mNavigation (0), mUpdate (false) + , mSceneMgr(NULL), mNavigation (0), mLighting (0), mUpdate (false) , mKeyForward (false), mKeyBackward (false), mKeyLeft (false), mKeyRight (false) , mKeyRollLeft (false), mKeyRollRight (false) , mFast (false), mDragging (false), mMod1 (false) , mFastFactor (4) /// \todo make this configurable + , mDefaultAmbient (0, 0, 0, 0), mHasDefaultAmbient (false) { setAttribute(Qt::WA_PaintOnScreen); setAttribute(Qt::WA_NoSystemBackground); @@ -32,34 +36,45 @@ namespace CSVRender mSceneMgr = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC); - // Throw in a random color just to make sure multiple scenes work - Ogre::Real r = Ogre::Math::RangeRandom(0, 1); - Ogre::Real g = Ogre::Math::RangeRandom(0, 1); - Ogre::Real b = Ogre::Math::RangeRandom(0, 1); - mSceneMgr->setAmbientLight(Ogre::ColourValue(r,g,b,1)); - - Ogre::Light* l = mSceneMgr->createLight(); - l->setType (Ogre::Light::LT_DIRECTIONAL); - l->setDirection (Ogre::Vector3(-0.4, -0.7, 0.3)); - l->setDiffuseColour (Ogre::ColourValue(0.7,0.7,0.7)); + mSceneMgr->setAmbientLight (Ogre::ColourValue (0,0,0,1)); mCamera = mSceneMgr->createCamera("foo"); - mCamera->setPosition(300,0,000); - mCamera->lookAt(0,0,0); - mCamera->setNearClipDistance(0.1); - mCamera->setFarClipDistance(30000); + mCamera->setPosition (300, 0, 0); + mCamera->lookAt (0, 0, 0); + mCamera->setNearClipDistance (0.1); + mCamera->setFarClipDistance (30000); mCamera->roll (Ogre::Degree (90)); + setLighting (&mLightingDay); + QTimer *timer = new QTimer (this); connect (timer, SIGNAL (timeout()), this, SLOT (update())); timer->start (20); /// \todo make this configurable } - void SceneWidget::setAmbient (const Ogre::ColourValue& colour) + CSVWorld::SceneToolMode *SceneWidget::makeLightingSelector (CSVWorld::SceneToolbar *parent) { - mSceneMgr->setAmbientLight (colour); + CSVWorld::SceneToolMode *tool = new CSVWorld::SceneToolMode (parent); + + tool->addButton (":door.png", "day"); /// \todo replace icons + tool->addButton (":GMST.png", "night"); + tool->addButton (":Info.png", "bright"); + + connect (tool, SIGNAL (modeChanged (const std::string&)), + this, SLOT (selectLightingMode (const std::string&))); + + return tool; + } + + void SceneWidget::setDefaultAmbient (const Ogre::ColourValue& colour) + { + mDefaultAmbient = colour; + mHasDefaultAmbient = true; + + if (mLighting) + mLighting->setDefaultAmbient (colour); } void SceneWidget::updateOgreWindow() @@ -316,4 +331,23 @@ namespace CSVRender { return mFast ? mFastFactor : 1; } + + void SceneWidget::setLighting (Lighting *lighting) + { + if (mLighting) + mLighting->deactivate(); + + mLighting = lighting; + mLighting->activate (mSceneMgr, mHasDefaultAmbient ? &mDefaultAmbient : 0); + } + + void SceneWidget::selectLightingMode (const std::string& mode) + { + if (mode=="day") + setLighting (&mLightingDay); + else if (mode=="night") + setLighting (&mLightingNight); + else if (mode=="bright") + setLighting (&mLightingBright); + } } diff --git a/apps/opencs/view/render/scenewidget.hpp b/apps/opencs/view/render/scenewidget.hpp index 375c877d2..8df9cf347 100644 --- a/apps/opencs/view/render/scenewidget.hpp +++ b/apps/opencs/view/render/scenewidget.hpp @@ -3,17 +3,29 @@ #include +#include + +#include "lightingday.hpp" +#include "lightingnight.hpp" +#include "lightingbright.hpp" + namespace Ogre { class Camera; class SceneManager; class RenderWindow; - class ColourValue; +} + +namespace CSVWorld +{ + class SceneToolMode; + class SceneToolbar; } namespace CSVRender { class Navigation; + class Lighting; class SceneWidget : public QWidget { @@ -26,8 +38,9 @@ namespace CSVRender QPaintEngine* paintEngine() const; - void setAmbient (const Ogre::ColourValue& colour); - ///< \note The actual ambient colour may differ based on lighting settings. + CSVWorld::SceneToolMode *makeLightingSelector (CSVWorld::SceneToolbar *parent); + ///< \attention The created tool is not added to the toolbar (via addTool). Doing that + /// is the responsibility of the calling function. protected: @@ -38,6 +51,9 @@ namespace CSVRender void flagAsModified(); + void setDefaultAmbient (const Ogre::ColourValue& colour); + ///< \note The actual ambient colour may differ based on lighting settings. + private: void paintEvent(QPaintEvent* e); void resizeEvent(QResizeEvent* e); @@ -61,11 +77,15 @@ namespace CSVRender int getFastFactor() const; + void setLighting (Lighting *lighting); + ///< \attention The ownership of \a lighting is not transferred to *this. + Ogre::Camera* mCamera; Ogre::SceneManager* mSceneMgr; Ogre::RenderWindow* mWindow; Navigation *mNavigation; + Lighting *mLighting; bool mUpdate; bool mKeyForward; bool mKeyBackward; @@ -78,10 +98,17 @@ namespace CSVRender bool mMod1; QPoint mOldPos; int mFastFactor; + Ogre::ColourValue mDefaultAmbient; + bool mHasDefaultAmbient; + LightingDay mLightingDay; + LightingNight mLightingNight; + LightingBright mLightingBright; private slots: void update(); + + void selectLightingMode (const std::string& mode); }; } diff --git a/apps/opencs/view/render/unpagedworldspacewidget.cpp b/apps/opencs/view/render/unpagedworldspacewidget.cpp index c7edbe79b..fb74788cc 100644 --- a/apps/opencs/view/render/unpagedworldspacewidget.cpp +++ b/apps/opencs/view/render/unpagedworldspacewidget.cpp @@ -15,7 +15,7 @@ void CSVRender::UnpagedWorldspaceWidget::update() Ogre::ColourValue colour; colour.setAsABGR (record.get().mAmbi.mAmbient); - setAmbient (colour); + setDefaultAmbient (colour); /// \todo deal with mSunlight and mFog/mForDensity } diff --git a/apps/opencs/view/render/worldspacewidget.hpp b/apps/opencs/view/render/worldspacewidget.hpp index 2eccca3bf..7921c3560 100644 --- a/apps/opencs/view/render/worldspacewidget.hpp +++ b/apps/opencs/view/render/worldspacewidget.hpp @@ -28,7 +28,7 @@ namespace CSVRender WorldspaceWidget (QWidget *parent = 0); CSVWorld::SceneToolMode *makeNavigationSelector (CSVWorld::SceneToolbar *parent); - ///< \important The created tool is not added to the toolbar (via addTool). Doing that + ///< \attention The created tool is not added to the toolbar (via addTool). Doing that /// is the responsibility of the calling function. void selectDefaultNavigationMode(); diff --git a/apps/opencs/view/world/previewsubview.cpp b/apps/opencs/view/world/previewsubview.cpp index df9c3276c..ac9776d22 100644 --- a/apps/opencs/view/world/previewsubview.cpp +++ b/apps/opencs/view/world/previewsubview.cpp @@ -3,11 +3,10 @@ #include -#include "../render/scenewidget.hpp" +#include "../render/previewwidget.hpp" #include "scenetoolbar.hpp" - -#include "../render/previewwidget.hpp" +#include "scenetoolmode.hpp" CSVWorld::PreviewSubView::PreviewSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document) : SubView (id) @@ -31,6 +30,9 @@ CSVWorld::PreviewSubView::PreviewSubView (const CSMWorld::UniversalId& id, CSMDo SceneToolbar *toolbar = new SceneToolbar (48, this); + SceneToolMode *lightingTool = mScene->makeLightingSelector (toolbar); + toolbar->addTool (lightingTool); + layout->addWidget (toolbar, 0); layout->addWidget (mScene, 1); diff --git a/apps/opencs/view/world/scenesubview.cpp b/apps/opencs/view/world/scenesubview.cpp index 66e026604..10e8b4071 100644 --- a/apps/opencs/view/world/scenesubview.cpp +++ b/apps/opencs/view/world/scenesubview.cpp @@ -39,8 +39,11 @@ CSVWorld::SceneSubView::SceneSubView (const CSMWorld::UniversalId& id, CSMDoc::D else mScene = new CSVRender::UnpagedWorldspaceWidget (id.getId(), document, this); - SceneToolMode *tool = mScene->makeNavigationSelector (toolbar); - toolbar->addTool (tool); + SceneToolMode *navigationTool = mScene->makeNavigationSelector (toolbar); + toolbar->addTool (navigationTool); + + SceneToolMode *lightingTool = mScene->makeLightingSelector (toolbar); + toolbar->addTool (lightingTool); layout2->addWidget (toolbar, 0);