Yes, you are right. Probably camera management sets it to perspective. I created a small example in which I am trying to add zoom capabilities. I move the camera around with W,S,A,D keys and can zoom through SHIFT+W/SHIFT+S:
I wonder if it is possible to zoom without image cropping?
MinGrid.cpp (the interesting function is processUnbufferedInput):
Code: Select all
#include "minGrid.h"
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
using namespace Ogre;
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{
minGrid app;
try {
app.go();
} catch( Ogre::Exception& e ) {
MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
}
return 0;
}
minGrid::minGrid(void)
: mRoot(0),
mCamera(0),
mSceneMgr(0),
mWindow(0),
mResourcesCfg(Ogre::StringUtil::BLANK),
mPluginsCfg(Ogre::StringUtil::BLANK),
mShutDown(false),
mTrayMgr(0),
mInputManager(0),
mKeyboard(0),
mMouse(0),
mDetailsPanel(0)
{
m_panX = 0.1;
m_panY = 350;
m_panZ = 0.1;
_viewportGrid = 0;
}
minGrid::~minGrid(void)
{
if (mTrayMgr)
delete mTrayMgr;
if (_viewportGrid)
delete _viewportGrid;
//Remove ourself as a Window listener
Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
windowClosed(mWindow);
delete mRoot;
}
bool minGrid::processUnbufferedInput( const Ogre::FrameEvent& evt )
{
Ogre::Real mMove = 5; // The movement constant
if (mKeyboard->isKeyDown(OIS::KC_W)) // Forward
{
if (mKeyboard->isKeyDown(OIS::KC_LSHIFT) || mKeyboard->isKeyDown(OIS::KC_RSHIFT))
{
m_zoom -= 2;
}
else
{
m_panY -= mMove;
}
}
if (mKeyboard->isKeyDown(OIS::KC_S)) // Backward
{
if (mKeyboard->isKeyDown(OIS::KC_LSHIFT) || mKeyboard->isKeyDown(OIS::KC_RSHIFT))
{
m_zoom += 2;
}
else
{
m_panY += mMove;
}
}
if (mKeyboard->isKeyDown(OIS::KC_A)) // left
{
m_panX -= mMove;
}
if (mKeyboard->isKeyDown(OIS::KC_D)) // right
{
m_panX += mMove;
}
// Reset the camera
mCamera->setPosition(Ogre::Vector3::ZERO);
// Avoid weird clipping plane problems
if (m_zoom < 0.01f)
m_zoom = 0.01f;
mCamera->moveRelative(Ogre::Vector3(m_panX, m_panY, 350));
mCamera->setNearClipDistance(m_zoom);
return true;
}
void minGrid::createCamera( void )
{
mCamera = mSceneMgr->createCamera("PlayerCam");
Ogre::Quaternion orientation;
orientation.FromAxes(Ogre::Vector3::UNIT_X, Ogre::Vector3::NEGATIVE_UNIT_Z, Ogre::Vector3::UNIT_Y);
mCamera->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
mCamera->setOrientation(orientation);
mCamera->setPosition(m_panX, m_panY, m_panZ);
mCamera->setFOVy(Radian(Degree(160)));
m_zoom = mCamera->getNearClipDistance();
}
void minGrid::createScene(void)
{
Ogre::Entity* ninjaEnt = mSceneMgr->createEntity("Ninja", "ninja.mesh");
Ogre::SceneNode* ninjaNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
ninjaNode->attachObject(ninjaEnt);
// Set ambient light
mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
// Create a light
Ogre::Light* l = mSceneMgr->createLight("MainLight");
l->setPosition(20,80,50);
_viewportGrid = new ViewportGrid(mSceneMgr, mWindow->getViewport(0));
_viewportGrid->enable();
}
bool minGrid::configure(void)
{
// Show the configuration dialog and initialise the system
// You can skip this and use root.restoreConfig() to load configuration
// settings if you were sure there are valid ones saved in ogre.cfg
if(mRoot->restoreConfig() || mRoot->showConfigDialog())
{
// If returned true, user clicked OK so initialise
// Here we choose to let the system create a default rendering window by passing 'true'
mWindow = mRoot->initialise(true, "minGrid Render Window");
// Let's add a nice window icon
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
HWND hwnd;
mWindow->getCustomAttribute("WINDOW", (void*)&hwnd);
LONG iconID = (LONG)LoadIcon( GetModuleHandle(0), MAKEINTRESOURCE(IDI_APPICON) );
SetClassLong( hwnd, GCL_HICON, iconID );
#endif
return true;
}
else
{
return false;
}
}
void minGrid::chooseSceneManager(void)
{
// Get the SceneManager, in this case a generic one
mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
}
bool minGrid::frameRenderingQueued( const Ogre::FrameEvent& evt )
{
if (mWindow->isClosed())
return false;
if (mShutDown)
return false;
//Need to capture/update each device
mKeyboard->capture();
mTrayMgr->frameRenderingQueued(evt);
if (!mTrayMgr->isDialogVisible())
{
if (mDetailsPanel->isVisible()) // if details panel is visible, then update its contents
{
mDetailsPanel->setParamValue(0, Ogre::StringConverter::toString(mCamera->getDerivedPosition().x));
mDetailsPanel->setParamValue(1, Ogre::StringConverter::toString(mCamera->getDerivedPosition().y));
mDetailsPanel->setParamValue(2, Ogre::StringConverter::toString(mCamera->getDerivedPosition().z));
mDetailsPanel->setParamValue(4, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().w));
mDetailsPanel->setParamValue(5, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().x));
mDetailsPanel->setParamValue(6, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().y));
mDetailsPanel->setParamValue(7, Ogre::StringConverter::toString(mCamera->getDerivedOrientation().z));
}
}
processUnbufferedInput(evt);
return true;
}
void minGrid::go( void )
{
#ifdef _DEBUG
mResourcesCfg = "resources_d.cfg";
mPluginsCfg = "plugins_d.cfg";
#else
mResourcesCfg = "resources.cfg";
mPluginsCfg = "plugins.cfg";
#endif
if (!setup())
return;
mRoot->startRendering();
// clean up
destroyScene();
}
void minGrid::createFrameListener(void)
{
Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
OIS::ParamList pl;
size_t windowHnd = 0;
std::ostringstream windowHndStr;
mWindow->getCustomAttribute("WINDOW", &windowHnd);
windowHndStr << windowHnd;
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
mInputManager = OIS::InputManager::createInputSystem( pl );
mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));
mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));
mKeyboard->setEventCallback(this);
//Set initial mouse clipping size
windowResized(mWindow);
//Register as a Window listener
Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
mTrayMgr = new OgreBites::SdkTrayManager("InterfaceName", mWindow, mMouse, this);
mTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
mTrayMgr->hideCursor();
// create a params panel for displaying sample details
Ogre::StringVector items;
items.push_back("cam.pX");
items.push_back("cam.pY");
items.push_back("cam.pZ");
items.push_back("");
items.push_back("cam.oW");
items.push_back("cam.oX");
items.push_back("cam.oY");
items.push_back("cam.oZ");
items.push_back("");
items.push_back("Filtering");
items.push_back("Poly Mode");
mDetailsPanel = mTrayMgr->createParamsPanel(OgreBites::TL_NONE, "DetailsPanel", 200, items);
mDetailsPanel->setParamValue(9, "Bilinear");
mDetailsPanel->setParamValue(10, "Solid");
mDetailsPanel->hide();
mRoot->addFrameListener(this);
}
void minGrid::destroyScene(void)
{
}
void minGrid::createViewports(void)
{
// Create one viewport, entire window
Ogre::Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
mCamera->setAspectRatio(
Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
}
void minGrid::setupResources(void)
{
// Load resource paths from config file
Ogre::ConfigFile cf;
cf.load(mResourcesCfg);
// Go through all sections & settings in the file
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
Ogre::String secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName);
}
}
}
void minGrid::createResourceListener(void)
{
}
void minGrid::loadResources(void)
{
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
bool minGrid::setup(void)
{
mRoot = new Ogre::Root(mPluginsCfg);
setupResources();
bool carryOn = configure();
if (!carryOn) return false;
chooseSceneManager();
createCamera();
createViewports();
// Set default mipmap level (NB some APIs ignore this)
Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
// Create any resource listeners (for loading screens)
createResourceListener();
// Load resources
loadResources();
// Create the scene
createScene();
createFrameListener();
return true;
};
bool minGrid::keyPressed( const OIS::KeyEvent &arg )
{
if (mTrayMgr->isDialogVisible()) return true; // don't process any more keys if dialog is up
if (arg.key == OIS::KC_F) // toggle visibility of advanced frame stats
{
mTrayMgr->toggleAdvancedFrameStats();
}
else if (arg.key == OIS::KC_G) // toggle visibility of even rarer debugging details
{
if (mDetailsPanel->getTrayLocation() == OgreBites::TL_NONE)
{
mTrayMgr->moveWidgetToTray(mDetailsPanel, OgreBites::TL_TOPRIGHT, 0);
mDetailsPanel->show();
}
else
{
mTrayMgr->removeWidgetFromTray(mDetailsPanel);
mDetailsPanel->hide();
}
}
else if (arg.key == OIS::KC_T) // cycle polygon rendering mode
{
Ogre::String newVal;
Ogre::TextureFilterOptions tfo;
unsigned int aniso;
switch (mDetailsPanel->getParamValue(9).asUTF8()[0])
{
case 'B':
newVal = "Trilinear";
tfo = Ogre::TFO_TRILINEAR;
aniso = 1;
break;
case 'T':
newVal = "Anisotropic";
tfo = Ogre::TFO_ANISOTROPIC;
aniso = 8;
break;
case 'A':
newVal = "None";
tfo = Ogre::TFO_NONE;
aniso = 1;
break;
default:
newVal = "Bilinear";
tfo = Ogre::TFO_BILINEAR;
aniso = 1;
}
Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(tfo);
Ogre::MaterialManager::getSingleton().setDefaultAnisotropy(aniso);
mDetailsPanel->setParamValue(9, newVal);
}
else if (arg.key == OIS::KC_R) // cycle polygon rendering mode
{
Ogre::String newVal;
Ogre::PolygonMode pm;
switch (mCamera->getPolygonMode())
{
case Ogre::PM_SOLID:
newVal = "Wireframe";
pm = Ogre::PM_WIREFRAME;
break;
case Ogre::PM_WIREFRAME:
newVal = "Points";
pm = Ogre::PM_POINTS;
break;
default:
newVal = "Solid";
pm = Ogre::PM_SOLID;
}
mCamera->setPolygonMode(pm);
mDetailsPanel->setParamValue(10, newVal);
}
else if(arg.key == OIS::KC_F5) // refresh all textures
{
Ogre::TextureManager::getSingleton().reloadAll();
}
else if (arg.key == OIS::KC_SYSRQ) // take a screenshot
{
mWindow->writeContentsToTimestampedFile("screenshot", ".jpg");
}
else if (arg.key == OIS::KC_ESCAPE)
{
mShutDown = true;
}
return true;
}
bool minGrid::keyReleased( const OIS::KeyEvent &arg )
{
return true;
}
//Adjust mouse clipping area
void minGrid::windowResized(Ogre::RenderWindow* rw)
{
unsigned int width, height, depth;
int left, top;
rw->getMetrics(width, height, depth, left, top);
}
//Unattach OIS before window shutdown (very important under Linux)
void minGrid::windowClosed(Ogre::RenderWindow* rw)
{
//Only close for window that created OIS (the main window in these demos)
if( rw == mWindow )
{
if( mInputManager )
{
mInputManager->destroyInputObject( mMouse );
mInputManager->destroyInputObject( mKeyboard );
OIS::InputManager::destroyInputSystem(mInputManager);
mInputManager = 0;
}
}
}
MinGrid.h
Code: Select all
#ifndef __minGrid_h_
#define __minGrid_h_
#include "BaseApplication.h"
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#include "../res/resource.h"
#endif
#include <OgreCamera.h>
#include <OgreEntity.h>
#include <OgreLogManager.h>
#include <OgreRoot.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreConfigFile.h>
#include <OISEvents.h>
#include <OISInputManager.h>
#include <OISKeyboard.h>
#include <OISMouse.h>
#include <SdkTrays.h>
#include <SdkCameraMan.h>
#include "ViewportGrid.h"
class minGrid : public Ogre::FrameListener, public OIS::KeyListener, public Ogre::WindowEventListener, OgreBites::SdkTrayListener
{
public:
minGrid(void);
virtual ~minGrid(void);
virtual void go(void);
protected:
virtual bool setup();
virtual bool configure(void);
virtual void chooseSceneManager(void);
virtual void createCamera(void);
virtual void createFrameListener(void);
virtual void createScene(void);
virtual void destroyScene(void);
virtual void createViewports(void);
virtual void setupResources(void);
virtual void createResourceListener(void);
virtual void loadResources(void);
// Ogre::FrameListener
virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
bool processUnbufferedInput(const Ogre::FrameEvent& evt);
// OIS::KeyListener
virtual bool keyPressed( const OIS::KeyEvent &arg );
virtual bool keyReleased( const OIS::KeyEvent &arg );
// Ogre::WindowEventListener
//Adjust mouse clipping area
virtual void windowResized(Ogre::RenderWindow* rw);
//Unattach OIS before window shutdown (very important under Linux)
virtual void windowClosed(Ogre::RenderWindow* rw);
Ogre::Root *mRoot;
Ogre::Camera* mCamera;
Ogre::SceneManager* mSceneMgr;
Ogre::RenderWindow* mWindow;
Ogre::String mResourcesCfg;
Ogre::String mPluginsCfg;
//OIS Input devices
OIS::InputManager* mInputManager;
OIS::Mouse* mMouse;
OIS::Keyboard* mKeyboard;
// OgreBites
OgreBites::SdkTrayManager* mTrayMgr;
OgreBites::ParamsPanel* mDetailsPanel; // sample details panel
Ogre::ViewportGrid* _viewportGrid;
double m_zoom, m_panX, m_panY, m_panZ;
bool mShutDown;
};
#endif // #ifndef __minGrid_h_