I read several threads about this and they suggested using "externalWindowHandle" for Ogre to create render window.
I have succesfully implemented such code on MFC HWND, it is quite convient and straight forward.
But on iOS, I met some problem.
I found that, the "externalWindowHandle" requires a UIWindow pointer instead of UIView.
If I pass a UIView to externalWindowHandle, the program crash and error message is
Code: Select all
-[UIView setRootViewController:]: Unrecognized selector
So I did another experiment, create a second UIWinodow and init Ogre with this window. program runs alright, and 3D model is shown.
my code is quite simple, I did not use the template, and just a simple initOgre() function in the AppDelegate file
Code: Select all
#include "ogre.h"
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
Ogre::Root* m_ogreRoot;
Ogre::RenderWindow* m_ogreWindow; // required Ogre object, render windth
Ogre::SceneManager* m_sceneManager; // required Ogre object
Ogre::Camera* m_pCamera;
Ogre::Viewport* m_pViewport;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow *window2;
- (void) initOgre:(id)hwnd;
@synthesize window = _window;
@synthesize window2;
- (void)initOgre:(id)hwnd
{
Ogre::String rootpath = Ogre::macBundlePath() + "/";
m_ogreRoot = new Ogre::Root("", rootpath+"ogre.cfg");
Ogre::GLESPlugin* mGLESPlugin = new Ogre::GLESPlugin();
m_ogreRoot->installPlugin(mGLESPlugin);
// create window
m_ogreWindow = m_ogreRoot->initialise(false);
Ogre::String wndHandle = Ogre::StringConverter::toString((long)hwnd);
Ogre::NameValuePairList parms;
parms["externalWindowHandle"] = wndHandle;
m_ogreWindow = m_ogreRoot->createRenderWindow("Render", 200, 300, false, &parms);
// create scene
m_sceneManager = m_ogreRoot->createSceneManager(Ogre::ST_GENERIC);
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(rootpath+"Sinbad.zip","Zip");
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
Ogre::Entity* headEnt = m_sceneManager->createEntity("sinbad.mesh");
m_sceneManager->getRootSceneNode()->attachObject(headEnt);
m_pCamera = m_sceneManager->createCamera("Camera");
m_pCamera->setPosition(Ogre::Vector3(0, 0, 60));
m_pCamera->lookAt(Ogre::Vector3(0, 0, 0));
m_pCamera->setNearClipDistance(1);
m_pViewport = m_ogreWindow->addViewport(m_pCamera);
m_pViewport->setBackgroundColour(Ogre::ColourValue(0.8f, 0.7f, 0.6f, 1.0f));
m_pCamera->setAspectRatio(Ogre::Real(m_pViewport->getActualWidth()) / Ogre::Real(m_pViewport->getActualHeight()));
m_pViewport->setCamera(m_pCamera);
m_ogreWindow->setActive(true);
m_ogreWindow->update();
m_ogreRoot->renderOneFrame();
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
UIViewController* vc = [[UIViewController alloc] init];
self.window.rootViewController = vc;
//[self initOgre:vc.view];
window2 = [[UIWindow alloc] initWithFrame:CGRectMake(10,10,200,300)];
[self initOgre:window2];
return YES;
}