This tutorial aims to show how quickly and easily you can get OGRE running on Mac OS X in your own interface.
The very bare bones basics to get OGRE running.
#import "OgreController.h"
#import "Ogre/Ogre.h"
using namespace Ogre;
@implementation OgreController
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
std::string mResourcePath = [[[NSBundle mainBundle] resourcePath] cString];
// Create a new root object with the correct paths
Root *mRoot = new Root(mResourcePath + "/plugins.cfg", mResourcePath + "/ogre.cfg", mResourcePath + "/Ogre.log");
mRoot->setRenderSystem(mRoot->getAvailableRenderers()->front());
// use pbuffers not frame buffers because of driver problems
mRoot->getRenderSystem()->setConfigOption("RTT Preferred Mode", "PBuffer");
// Show a configure box and exit if user clicked cancel
if(!mRoot->showConfigDialog())
return;
// Initialise
RenderWindow* mWindow = mRoot->initialise(true);
SceneManager *mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "my scene manager");
// Create the camera, node & attach camera
Camera *mCamera = mSceneMgr->createCamera("PlayerCam");
SceneNode* camNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
camNode->attachObject(mCamera);
mWindow->addViewport(mCamera);
}
@end
A black screen is not very exciting. OGRE can do much more, we provide a start by adding some simple resources and creating something to look at.
// Add resource locations -- looking at folders recursively
ResourceGroupManager::getSingleton().addResourceLocation(mResourcePath, std::string("FileSystem"), ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, false);
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
// Create a light
mSceneMgr->setAmbientLight(ColourValue(0, 0, 0));
mSceneMgr->createLight("MainLight");
// Add a object, give it it's own node
SceneNode *objectNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
Entity *knot = mSceneMgr->createEntity("knot", "knot.mesh");
objectNode->attachObject(knot);
objectNode->setPosition(Vector3(0, 0, -500));
At the moment the 3D view only redraws as you resize the window. To make it move, we add a timer that causes some animation and updates the window.
// create a timer that causes OGRE to render at 50fps
[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(renderFrame) userInfo:NULL repeats:YES];
- (void)renderFrame
{
Ogre::Root::getSingleton().renderOneFrame();
objectNode->rotate(Vector3(0, 1, 0), Radian(0.01));
}
OGRE can fit seamlessly in your own interface, inbetween all sorts of controls with multiple views.
// Show a configure box and exit if user clicked cancel
if(!mRoot->showConfigDialog())
return;
// Initialise
RenderWindow* mWindow = mRoot->initialise(true);
// Initialise without an automatically created window
mRoot->initialise(false);
// Ask for a new window passing in the ogreView in our nib file
NameValuePairList misc;
misc["externalWindowHandle"] = StringConverter::toString((size_t)ogreView);
mRoot->createRenderWindow("ogre window", 0, 0, false, &misc);
RenderWindow *mWindow = [ogreView ogreWindow];
NSTimer *renderTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(renderFrame) userInfo:NULL repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:renderTimer forMode:NSEventTrackingRunLoopMode];