Skip to content

Commit

Permalink
GRAPHICS: Add a new CameraManager for Ogre
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Mar 21, 2014
1 parent 3b0cb28 commit a077ab6
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/graphics/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ noinst_HEADERS = \
materialman.h \
cursor.h \
cursorman.h \
cameraman.h \
$(EMPTY)

libgraphics_la_SOURCES = \
Expand All @@ -28,6 +29,7 @@ libgraphics_la_SOURCES = \
materialman.cpp \
cursor.cpp \
cursorman.cpp \
cameraman.cpp \
$(EMPTY)

if DARWIN
Expand Down
141 changes: 141 additions & 0 deletions src/graphics/cameraman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names can be
* found in the AUTHORS file distributed with this source
* distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* The Infinity, Aurora, Odyssey, Eclipse and Lycium engines, Copyright (c) BioWare corp.
* The Electron engine, Copyright (c) Obsidian Entertainment and BioWare corp.
*/

/** @file graphics/cameraman.cpp
* A camera manager.
*/

#include <OgreRoot.h>
#include <OgreCamera.h>
#include <OgreViewport.h>
#include <OgreRenderWindow.h>

#include "graphics/cameraman.h"

DECLARE_SINGLETON(Graphics::CameraManager)

namespace Graphics {

CameraManager::CameraManager() : _camera(0) {
}

CameraManager::~CameraManager() {
deinit();
}

void CameraManager::init() {
Ogre::SceneManager *scene = Ogre::Root::getSingleton().getSceneManagerIterator().begin()->second;
if (!scene)
return;

_camera = scene->createCamera("camera");

_camera->setNearClipDistance(1.0);
_camera->setFarClipDistance(1000.0);
_camera->setFOVy(Ogre::Degree(60.0));
}

void CameraManager::deinit() {
Ogre::SceneManager *scene = Ogre::Root::getSingleton().getSceneManagerIterator().begin()->second;
if (!scene)
return;

if (_camera)
scene->destroyCamera(_camera);

_camera = 0;
}

Ogre::Viewport *CameraManager::createViewport(Ogre::RenderWindow *window) {
return window->addViewport(_camera);
}

void CameraManager::setAspectRatio(float aspect) {
_camera->setAspectRatio(aspect);
}

void CameraManager::reset() {
_camera->setPosition(0.0, 0.0, 0.0);
_camera->setOrientation(Ogre::Quaternion());
_camera->lookAt(0.0, 0.0, 0.0);
}

void CameraManager::getPosition(float &x, float &y, float &z) const {
const Ogre::Vector3 &pos = _camera->getPosition();

x = pos.x;
y = pos.y;
z = pos.z;
}

void CameraManager::getDirection(float &x, float &y, float &z) const {
const Ogre::Vector3 dir = _camera->getDirection();

x = dir.x;
y = dir.y;
z = dir.z;
}

void CameraManager::setPosition(float x, float y, float z) {
_camera->setPosition(x, y, z);
}

void CameraManager::setDirection(float x, float y, float z) {
_camera->setDirection(x, y, z);
}

void CameraManager::lookAt(float x, float y, float z) {
_camera->lookAt(x, y, z);
}

void CameraManager::setOrientation(float radian, float x, float y, float z) {
_camera->setOrientation(Ogre::Quaternion(Ogre::Radian(radian), Ogre::Vector3(x, y, z)));
}

void CameraManager::move(float x, float y, float z) {
_camera->move(Ogre::Vector3(x, y, z));
}

void CameraManager::moveRelative(float x, float y, float z) {
_camera->moveRelative(Ogre::Vector3(x, y, z));
}

void CameraManager::rotate(float radian, float x, float y, float z) {
_camera->rotate(Ogre::Quaternion(Ogre::Radian(radian), Ogre::Vector3(x, y, z)));
}

void CameraManager::roll(float radian) {
_camera->roll(Ogre::Radian(radian));
}

void CameraManager::yaw(float radian) {
_camera->yaw(Ogre::Radian(radian));
}

void CameraManager::pitch(float radian) {
_camera->pitch(Ogre::Radian(radian));
}

} // End of namespace Graphics
87 changes: 87 additions & 0 deletions src/graphics/cameraman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names can be
* found in the AUTHORS file distributed with this source
* distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* The Infinity, Aurora, Odyssey, Eclipse and Lycium engines, Copyright (c) BioWare corp.
* The Electron engine, Copyright (c) Obsidian Entertainment and BioWare corp.
*/

/** @file graphics/cameraman.h
* A camera manager.
*/

#ifndef GRAPHICS_CAMERAMAN_H
#define GRAPHICS_CAMERAMAN_H

#include "common/singleton.h"

namespace Ogre {
class RenderWindow;
class Viewport;
class Camera;
}

namespace Graphics {

class CameraManager : public Common::Singleton<CameraManager> {
public:
CameraManager();
~CameraManager();

void init();
void deinit();

/** Create a viewport for the camera in that window. */
Ogre::Viewport *createViewport(Ogre::RenderWindow *window);
/** Adjust the aspect ratio of the camera. */
void setAspectRatio(float aspect);

/** Reset the position and orientation of the camera. */
void reset();

void getPosition(float &x, float &y, float &z) const;
void getDirection(float &x, float &y, float &z) const;

void setPosition(float x, float y, float z);
void setDirection(float x, float y, float z);

void lookAt(float x, float y, float z);

void setOrientation(float radian, float x, float y, float z);

void move(float x, float y, float z);
void moveRelative(float x, float y, float z);

void rotate(float radian, float x, float y, float z);

void roll(float radian);
void yaw(float radian);
void pitch(float radian);

private:
Ogre::Camera *_camera; ///< The OGRE camera.
};

} // End of namespace Graphics

/** Shortcut for accessing the camera manager. */
#define CameraMan Graphics::CameraManager::instance()

#endif // GRAPHICS_CAMERAMAN_H
24 changes: 13 additions & 11 deletions src/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "common/configman.h"

#include "graphics/cursorman.h"
#include "graphics/cameraman.h"
#include "graphics/renderer.h"

#ifdef MACOSX
Expand Down Expand Up @@ -85,7 +86,7 @@ class OgreAnimator : public Ogre::FrameListener {

Renderer::Renderer(SDL_Window &screen, bool vsync, int fsaa) :
_logManager(0), _logger(0), _root(0), _overlaySystem(0), _dummyWindow(0),
_renderWindow(0), _sceneManager(0), _camera(0), _viewPort(0), _animator(0) {
_renderWindow(0), _sceneManager(0), _viewPort(0), _animator(0) {

try {
createLog();
Expand Down Expand Up @@ -120,7 +121,11 @@ Renderer::~Renderer() {
}

void Renderer::destroy() {
if (_renderWindow)
_renderWindow->removeAllViewports();

CursorMan.deinit();
CameraMan.deinit();

if (_root && _animator)
_root->removeFrameListener(_animator);
Expand All @@ -146,7 +151,6 @@ void Renderer::destroy() {
_dummyWindow = 0;
_renderWindow = 0;
_sceneManager = 0;
_camera = 0;
_viewPort = 0;
_animator = 0;
}
Expand Down Expand Up @@ -281,14 +285,11 @@ void Renderer::createScene() {
_sceneManager = _root->createSceneManager(Ogre::ST_GENERIC);
_sceneManager->addRenderQueueListener(_overlaySystem);

_camera = _sceneManager->createCamera("camera");
_viewPort = _renderWindow->addViewport(_camera);
CameraMan.init();

_camera->setAspectRatio(Ogre::Real(_viewPort->getActualWidth()) / Ogre::Real(_viewPort->getActualHeight()));
_viewPort = CameraMan.createViewport(_renderWindow);

_camera->setNearClipDistance(1.0);
_camera->setFarClipDistance(1000.0);
_camera->setFOVy(Ogre::Degree(60.0));
CameraMan.setAspectRatio(Ogre::Real(_viewPort->getActualWidth()) / Ogre::Real(_viewPort->getActualHeight()));

_animator = new OgreAnimator;
_root->addFrameListener(_animator);
Expand All @@ -313,8 +314,9 @@ bool Renderer::recreate(SDL_Window &screen, bool vsync, int fsaa) {
}

// Reattach camera
_viewPort = _renderWindow->addViewport(_camera);
_camera->setAspectRatio(Ogre::Real(_viewPort->getActualWidth()) / Ogre::Real(_viewPort->getActualHeight()));
_viewPort = CameraMan.createViewport(_renderWindow);

CameraMan.setAspectRatio(Ogre::Real(_viewPort->getActualWidth()) / Ogre::Real(_viewPort->getActualHeight()));

return true;
}
Expand All @@ -323,7 +325,7 @@ void Renderer::resized(int width, int height) {
_renderWindow->resize(width, height);
_renderWindow->windowMovedOrResized();

_camera->setAspectRatio(Ogre::Real(_viewPort->getActualWidth()) / Ogre::Real(_viewPort->getActualHeight()));
CameraMan.setAspectRatio(Ogre::Real(_viewPort->getActualWidth()) / Ogre::Real(_viewPort->getActualHeight()));
}

void Renderer::render() {
Expand Down
1 change: 0 additions & 1 deletion src/graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class Renderer {
Ogre::RenderWindow *_dummyWindow; ///< Fake dummy window holding the OGRE resources.
Ogre::RenderWindow *_renderWindow; ///< The OGRE render window.
Ogre::SceneManager *_sceneManager; ///< The OGRE scene manager.
Ogre::Camera *_camera; ///< The OGRE camera.
Ogre::Viewport *_viewPort; ///< The OGRE view port.
OgreAnimator *_animator; ///< The OGRE frame listener advancing animations.

Expand Down
3 changes: 3 additions & 0 deletions src/xoreos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "graphics/cursorman.h"
#include "graphics/textureman.h"
#include "graphics/materialman.h"
#include "graphics/cameraman.h"

#include "graphics/aurora/sceneman.h"

Expand Down Expand Up @@ -275,6 +276,8 @@ void deinit() {
Graphics::TextureManager::destroy();
Graphics::MaterialManager::destroy();

Graphics::CameraManager::destroy();

Aurora::TalkManager::destroy();
Aurora::TwoDARegistry::destroy();
Aurora::ResourceManager::destroy();
Expand Down

0 comments on commit a077ab6

Please sign in to comment.