Skip to content

Instantly share code, notes, and snippets.

@zester
Created September 24, 2013 09:52
Show Gist options
  • Save zester/6682632 to your computer and use it in GitHub Desktop.
Save zester/6682632 to your computer and use it in GitHub Desktop.
// g++ main.cpp -o main `pkg-config --libs --cflags OGRE OIS`
#include <Ogre.h>
#include <OgreFrameListener.h>
#include <OIS.h>
#include <iostream>
using namespace Ogre;
//
class MyFrameListener : public FrameListener {
private:
OIS::InputManager* mInputManager;
OIS::Keyboard* mKeyboard;
OIS::Mouse* mMouse;
Ogre::SceneNode* mNode;
Ogre::Camera *mCamera;
float mMovementspeed;
public:
MyFrameListener(Ogre::SceneNode*,RenderWindow*, Ogre::Camera*);
~MyFrameListener();
bool frameStarted (const FrameEvent &evt);
bool frameEnded (const FrameEvent &evt);
bool frameRenderingQueued (const FrameEvent &evt);
};
//
MyFrameListener::MyFrameListener(Ogre::SceneNode *node, RenderWindow *mWindow, Ogre::Camera *camera)
{
//
mNode = node;
mCamera = camera;
mMovementspeed = 5.0f;
//
Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
//
size_t windowHnd = 0;
mWindow->getCustomAttribute("WINDOW", &windowHnd);
//
std::stringstream windowHndStr;
windowHndStr << windowHnd;
//
OIS::ParamList pl;
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));
}
//
MyFrameListener::~MyFrameListener()
{
//
mInputManager->destroyInputObject(mKeyboard);
mInputManager->destroyInputObject(mMouse);
OIS::InputManager::destroyInputSystem(mInputManager);
}
//
bool MyFrameListener::frameStarted(const FrameEvent &evt) {
//std::cout << "Frame Started" << std::endl;
//
Ogre::Vector3 translate(0,0,0);
//
mKeyboard->capture();
mMouse->capture();
//
if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
{
return false;
}
//
if(mKeyboard->isKeyDown(OIS::KC_W))
{
translate += Ogre::Vector3(0,0,-10);
}
//
if(mKeyboard->isKeyDown(OIS::KC_S))
{
translate += Ogre::Vector3(0,0,10);
}
//
if(mKeyboard->isKeyDown(OIS::KC_A))
{
translate += Ogre::Vector3(-10,0,0);
}
//
if(mKeyboard->isKeyDown(OIS::KC_D))
{
translate += Ogre::Vector3(10,0,0);
}
//
float rotX = mMouse->getMouseState().X.rel * evt.timeSinceLastFrame* -1;
float rotY = mMouse->getMouseState().Y.rel * evt.timeSinceLastFrame * -1;
mCamera->yaw(Ogre::Radian(rotX));
mCamera->pitch(Ogre::Radian(rotY));
//
mCamera->moveRelative(translate * evt.timeSinceLastFrame * mMovementspeed);
return true;
}
//
bool MyFrameListener::frameEnded(const FrameEvent &evt) {
//std::cout << "Frame Ended" << std::endl;
return true;
}
//
bool MyFrameListener::frameRenderingQueued(const FrameEvent &evt) {
//std::cout << "Frame Queued" << std::endl;
return true;
}
int main(void)
{
// Create an instance of the OGRE Root Class
Root* root = new Root;
// Configures the application
if (!root->restoreConfig())
root->showConfigDialog();
root->saveConfig();
// Create a render window
RenderWindow* window = root->initialise(true, "Tutorial 1");
// Create a new scene manager.
SceneManager* sceneManager = root->createSceneManager(ST_GENERIC);
sceneManager->setAmbientLight(Ogre::ColourValue(0.0, 0.0, 0.0));
// Create a new camera
Camera* camera = sceneManager->createCamera("Camera");
camera->setPosition(Ogre::Vector3(0,0,15));
camera->lookAt(Ogre::Vector3(0,0,0));
camera->setNearClipDistance(5);
// Add our model to our resources and index it
ResourceGroupManager::getSingleton().addResourceLocation("Media/packs/Sinbad.zip", "Zip");
ResourceGroupManager::getSingleton().addResourceLocation("Media/", "FileSystem");
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
//
Light* light1 = sceneManager->createLight("Light1");
light1->setType(Ogre::Light::LT_POINT);
// Set Light Color
light1->setDiffuseColour(1.0f, 1.0f, 1.0f);
// Set Light Reflective Color
light1->setSpecularColour(1.0f, 0.0f, 0.0f);
// Set Light (Range, Brightness, Fade Speed, Rapid Fade Speed)
light1->setAttenuation(10, 0.5, 0.045, 0.0);
//
Entity* lightEnt = sceneManager->createEntity("LightEntity", "models/sphere.mesh");
SceneNode* lightNode = sceneManager->createSceneNode("LightNode");
lightNode->attachObject(lightEnt);
lightNode->attachObject(light1);
lightNode->setScale(0.01f, 0.01f, 0.01f);
lightNode->setPosition(0, 4, 10);
sceneManager->getRootSceneNode()->addChild(lightNode);
// Using the camera create a viewport and set the background color to black
Viewport* viewport = window->addViewport(camera);
viewport->setBackgroundColour(Ogre::ColourValue(0.0,0.0,0.0));
// Use the viewport to set the aspect ratio of the camera
camera->setAspectRatio(Real(viewport->getActualWidth()) /
Real(viewport->getActualHeight()));
// Create an instance of our model and add it to the scene
Entity* ent = sceneManager->createEntity("Sinbad.mesh");
SceneNode* entNode = sceneManager->createSceneNode("Character");
entNode->attachObject(ent);
sceneManager->getRootSceneNode()->addChild(entNode);
entNode->setPosition(0,0,0);
// Create an instance of the MyFrameListener Class and add it to the root object
MyFrameListener* myListener = new MyFrameListener(entNode, window, camera);
root->addFrameListener(myListener);
// Tell root to start rendering
root->startRendering();
// Cleanup
delete myListener;
delete root;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment