From 855ca8ee74eb0241264238998029c6f3fea4c7df Mon Sep 17 00:00:00 2001 From: Nicolay Korslund Date: Sat, 5 Jun 2010 20:37:01 +0200 Subject: [PATCH] Added camera and scene, started adding input. MISSING FindOIS.cmake! --- CMakeLists.txt | 4 +- game/main.cpp | 11 ++ game/render/mwscene.cpp | 50 +++++++++ game/render/mwscene.hpp | 27 +++++ input/oismanager.cpp | 83 +++++++++++++++ input/oismanager.hpp | 20 ++++ old_d_version/ogre/cpp_interface.cpp | 147 --------------------------- old_d_version/ogre/cpp_ogre.cpp | 22 ---- 8 files changed, 194 insertions(+), 170 deletions(-) create mode 100644 game/render/mwscene.cpp create mode 100644 game/render/mwscene.hpp create mode 100644 input/oismanager.cpp create mode 100644 input/oismanager.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bbe009090..f9d349d1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,9 @@ set(NIF nif/nif_file.cpp nifogre/ogre_nif_loader.cpp) set(TOOLS tools/stringops.cpp tools/fileops.cpp) set(MANGLE_VFS mangle/vfs/servers/ogre_vfs.cpp) set(OGRE ogre/renderer.cpp) +set(INPUT input/oismanager.cpp) set(GAME game/main.cpp game/esm_store/store.cpp game/cell_store.cpp) +set(GAMEREND game/render/mwscene.cpp) # Platform specific if (WIN32) @@ -28,5 +30,5 @@ include_directories("." ${OGRE_INCLUDE_DIR} ${Boost_INCLUDE_DIR} ${PLATFORM_INCL link_directories(${Boost_LIBRARY_DIRS} ${OGRE_LIB_DIR}) # Main executable -add_executable(openmw ${BSA} ${TOOLS} ${OGRE} ${GAME}) +add_executable(openmw ${BSA} ${TOOLS} ${OGRE} ${INPUT} ${GAME} ${GAMEREND}) target_link_libraries(openmw ${OGRE_LIBRARIES}) diff --git a/game/main.cpp b/game/main.cpp index 97048e372..81f0f2201 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -2,9 +2,11 @@ #include "cell_store.hpp" #include "render/cell.hpp" +#include "render/mwscene.hpp" #include "bsa/bsa_archive.hpp" #include "ogre/renderer.hpp" #include "tools/fileops.hpp" +#include "input/oismanager.hpp" using namespace std; @@ -40,6 +42,15 @@ void maintest() ogre.createWindow("OpenMW"); + Render::MWScene scene; + scene.setup(&ogre); + + Input::OISManager input; + input.setup(&ogre); + + // Add the frame listener + //root->addFrameListener(&mFrameListener); + cout << "\nThat's all for now!\n"; } diff --git a/game/render/mwscene.cpp b/game/render/mwscene.cpp new file mode 100644 index 000000000..b916e940c --- /dev/null +++ b/game/render/mwscene.cpp @@ -0,0 +1,50 @@ +#include "mwscene.hpp" + +#include + +using namespace Render; +using namespace Ogre; + +void MWScene::setup(OgreRenderer *_rend) +{ + rend = _rend; + assert(rend); + + Root *root = rend->getRoot(); + RenderWindow *window = rend->getWindow(); + + // Get the SceneManager, in this case a generic one + sceneMgr = root->createSceneManager(ST_GENERIC); + + // Create the camera + camera = sceneMgr->createCamera("PlayerCam"); + + camera->setNearClipDistance(5); + + // Create one viewport, entire window + vp = window->addViewport(camera); + // Give the backround a healthy shade of green + vp->setBackgroundColour(ColourValue(0,0.1,0)); + + // Alter the camera aspect ratio to match the viewport + camera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight())); + camera->setFOVy(Degree(55)); + + // Set default mipmap level (NB some APIs ignore this) + TextureManager::getSingleton().setDefaultNumMipmaps(5); + + // Load resources + ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); + + // Turn the entire scene (represented by the 'root' node) -90 + // degrees around the x axis. This makes Z go upwards, and Y go into + // the screen (when x is to the right.) This is the orientation that + // Morrowind uses, and it automagically makes everything work as it + // should. + SceneNode *rt = sceneMgr->getRootSceneNode(); + mwRoot = rt->createChildSceneNode(); + mwRoot->pitch(Degree(-90)); + + // For testing + sceneMgr->setAmbientLight(ColourValue(1,1,1)); +} diff --git a/game/render/mwscene.hpp b/game/render/mwscene.hpp new file mode 100644 index 000000000..9eaba76f7 --- /dev/null +++ b/game/render/mwscene.hpp @@ -0,0 +1,27 @@ +#ifndef _GAME_RENDER_MWSCENE_H +#define _GAME_RENDER_MWSCENE_H + +#include "ogre/renderer.hpp" + +namespace Render +{ + /** Class responsible for Morrowind-specific interfaces to OGRE. + */ + class MWScene + { + OgreRenderer *rend; + Ogre::SceneManager *sceneMgr; + Ogre::Camera *camera; + Ogre::Viewport *vp; + + // Root node for all objects added to the scene. This is rotated so + // that the OGRE coordinate system matches that used internally in + // Morrowind. + Ogre::SceneNode *mwRoot; + + public: + void setup(OgreRenderer *_rend); + }; +} + +#endif diff --git a/input/oismanager.cpp b/input/oismanager.cpp new file mode 100644 index 000000000..37593c29d --- /dev/null +++ b/input/oismanager.cpp @@ -0,0 +1,83 @@ +#include "oismanager.hpp" +#include +#include +#include + +using namespace Input; +using namespace Ogre; +using namespace OIS; + +#include +using namespace std; + +void OISManager::setup(Render::OgreRenderer *rend) +{ + assert(rend); + + RenderWindow *window = rend->getWindow(); + assert(window); + + size_t windowHnd; + window->getCustomAttribute("WINDOW", &windowHnd); + + std::ostringstream windowHndStr; + ParamList pl; + + windowHndStr << windowHnd; + pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); + + // Non-exclusive mouse and keyboard input in debug mode + if(true) + { +#if defined OIS_WIN32_PLATFORM + pl.insert(std::make_pair(std::string("w32_mouse"), + std::string("DISCL_FOREGROUND" ))); + pl.insert(std::make_pair(std::string("w32_mouse"), + std::string("DISCL_NONEXCLUSIVE"))); + pl.insert(std::make_pair(std::string("w32_keyboard"), + std::string("DISCL_FOREGROUND"))); + pl.insert(std::make_pair(std::string("w32_keyboard"), + std::string("DISCL_NONEXCLUSIVE"))); +#elif defined OIS_LINUX_PLATFORM + pl.insert(std::make_pair(std::string("x11_mouse_grab"), + std::string("false"))); + pl.insert(std::make_pair(std::string("x11_mouse_hide"), + std::string("false"))); + pl.insert(std::make_pair(std::string("x11_keyboard_grab"), + std::string("false"))); + pl.insert(std::make_pair(std::string("XAutoRepeatOn"), + std::string("true"))); +#endif + } + + inputMgr = InputManager::createInputSystem( pl ); + + // Create all devices + keyboard = static_cast(inputMgr->createInputObject + ( OISKeyboard, true )); + mouse = static_cast(inputMgr->createInputObject + ( OISMouse, true )); + + // Set mouse region + const MouseState &ms = mouse->getMouseState(); + ms.width = window->getWidth(); + ms.height = window->getHeight(); + + /* + // Register the input listener + keyboard -> setEventCallback( &mInput ); + mouse -> setEventCallback( &mInput ); + */ +} + +void OISManager::cleanup() +{ + if(inputMgr == NULL) return; + + // Kill the input systems. This will reset input options such as key + // repetition. + inputMgr->destroyInputObject(keyboard); + inputMgr->destroyInputObject(mouse); + InputManager::destroyInputSystem(inputMgr); + inputMgr = NULL; +} diff --git a/input/oismanager.hpp b/input/oismanager.hpp new file mode 100644 index 000000000..56cac6f94 --- /dev/null +++ b/input/oismanager.hpp @@ -0,0 +1,20 @@ +#ifndef _INPUT_OISMANAGER_H +#define _INPUT_OISMANAGER_H + +#include "ogre/renderer.hpp" +#include + +namespace Input +{ + class OISManager + { + OIS::InputManager *inputMgr; + OIS::Mouse *mouse; + OIS::Keyboard *keyboard; + + public: + void setup(Render::OgreRenderer *rend); + void cleanup(); + }; +} +#endif diff --git a/old_d_version/ogre/cpp_interface.cpp b/old_d_version/ogre/cpp_interface.cpp index b82f2277a..aeb556750 100644 --- a/old_d_version/ogre/cpp_interface.cpp +++ b/old_d_version/ogre/cpp_interface.cpp @@ -48,146 +48,6 @@ extern "C" // E X P O R T E D F U N C T I O N S //----------------------------------------------------------------------- -extern "C" void ogre_cleanup() -{ - // Kill the input systems. This will reset input options such as key - // repetition. - mInputManager->destroyInputObject(mKeyboard); - mInputManager->destroyInputObject(mMouse); - OIS::InputManager::destroyInputSystem(mInputManager); - - // Code killing ogre has been ported already -} - -// Initialize window. This will create and show the actual window. -extern "C" void ogre_initWindow() -{ - TRACE("ogre_initWindow"); - - // Initialize OGRE. - mWindow = mRoot->initialise(true, "OpenMW", ""); - - // Set up the input system - - using namespace OIS; - - size_t windowHnd; - mWindow->getCustomAttribute("WINDOW", &windowHnd); - - std::ostringstream windowHndStr; - ParamList pl; - - windowHndStr << windowHnd; - pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); - - // Non-exclusive mouse and keyboard input in debug mode - if(g_isDebug) - { -#if defined OIS_WIN32_PLATFORM - pl.insert(std::make_pair(std::string("w32_mouse"), - std::string("DISCL_FOREGROUND" ))); - pl.insert(std::make_pair(std::string("w32_mouse"), - std::string("DISCL_NONEXCLUSIVE"))); - pl.insert(std::make_pair(std::string("w32_keyboard"), - std::string("DISCL_FOREGROUND"))); - pl.insert(std::make_pair(std::string("w32_keyboard"), - std::string("DISCL_NONEXCLUSIVE"))); -#elif defined OIS_LINUX_PLATFORM - pl.insert(std::make_pair(std::string("x11_mouse_grab"), - std::string("false"))); - pl.insert(std::make_pair(std::string("x11_mouse_hide"), - std::string("false"))); - pl.insert(std::make_pair(std::string("x11_keyboard_grab"), - std::string("false"))); - pl.insert(std::make_pair(std::string("XAutoRepeatOn"), - std::string("true"))); -#endif - } - - mInputManager = InputManager::createInputSystem( pl ); - - const bool bufferedKeys = true; - const bool bufferedMouse = true; - - // Create all devices - mKeyboard = static_cast(mInputManager->createInputObject - ( OISKeyboard, bufferedKeys )); - mMouse = static_cast(mInputManager->createInputObject - ( OISMouse, bufferedMouse )); - - // Set mouse region - const MouseState &ms = mMouse->getMouseState(); - ms.width = mWindow->getWidth(); - ms.height = mWindow->getHeight(); - - // Register the input listener - mKeyboard -> setEventCallback( &mInput ); - mMouse -> setEventCallback( &mInput ); -} - -// Make a scene -extern "C" void ogre_makeScene() -{ - // Get the SceneManager, in this case a generic one - mSceneMgr = mRoot->createSceneManager(ST_GENERIC); - - // Create the camera - mCamera = mSceneMgr->createCamera("PlayerCam"); - - mCamera->setNearClipDistance(5); - - // Create one viewport, entire window - vp = mWindow->addViewport(mCamera); - // Give the backround a healthy shade of green - vp->setBackgroundColour(ColourValue(0,0.1,0)); - - // Alter the camera aspect ratio to match the viewport - mCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight())); - - mCamera->setFOVy(Degree(55)); - - // Set default mipmap level (NB some APIs ignore this) - TextureManager::getSingleton().setDefaultNumMipmaps(5); - - // Load resources - ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); - - // Add the frame listener - mRoot->addFrameListener(&mFrameListener); - - // Turn the entire scene (represented by the 'root' node) -90 - // degrees around the x axis. This makes Z go upwards, and Y go into - // the screen (when x is to the right.) This is the orientation that - // Morrowind uses, and it automagically makes everything work as it - // should. - SceneNode *rt = mSceneMgr->getRootSceneNode(); - mwRoot = rt->createChildSceneNode(); - mwRoot->pitch(Degree(-90)); - - /* - g_light = mSceneMgr->createLight("carry"); - g_light->setDiffuseColour(1,0.7,0.3); - g_light->setAttenuation(2000, 0, 0.008, 0); - */ -} - -/* -// Toggle carryable light -extern "C" void ogre_toggleCarryLight() -{ - if(g_spotOn == 0) - { - g_light->setVisible(true); - g_spotOn = 1; - } - else - { - g_light->setVisible(false); - g_spotOn = 0; - } -} -*/ - // Toggle ambient light extern "C" void ogre_toggleLight() { @@ -212,13 +72,6 @@ extern "C" void ogre_toggleLight() } } -// Create a sky dome. Currently disabled since we aren't including the -// Ogre example data (which has the sky material.) -extern "C" void ogre_makeSky() -{ - //mSceneMgr->setSkyDome( true, "Examples/CloudySky", 5, 8 ); -} - extern "C" Light* ogre_attachLight(char *name, SceneNode* base, float r, float g, float b, float radius) diff --git a/old_d_version/ogre/cpp_ogre.cpp b/old_d_version/ogre/cpp_ogre.cpp index a2ae56c28..9b38dd458 100644 --- a/old_d_version/ogre/cpp_ogre.cpp +++ b/old_d_version/ogre/cpp_ogre.cpp @@ -31,35 +31,18 @@ #include #include -#include - #include #include "../util/dbg.h" using namespace Ogre; -RenderWindow* mWindow; -Root *mRoot; -SceneManager *mSceneMgr; -Camera *mCamera; -Viewport *vp; - ColourValue g_ambient; int g_lightOn = 0; -/* -int g_spotOn = 0; -Light *g_light; -*/ - // Set to nonzero if debug mode is enabled int g_isDebug = 0; -OIS::InputManager *mInputManager; -OIS::Mouse *mMouse; -OIS::Keyboard *mKeyboard; - // The global GUI object MyGUI::Gui *mGUI; @@ -68,11 +51,6 @@ MyGUI::Gui *mGUI; // input into MyGUI. int32_t guiMode = 0; -// Root node for all objects added to the scene. This is rotated so -// that the OGRE coordinate system matches that used internally in -// Morrowind. -SceneNode *mwRoot; - // Include the other parts of the code, and make one big happy object // file. This is extremely against the grain of C++ "recomended // practice", but I don't care.