Advertisement
Guest User

Minimal Ogre/WinRT Application (not working)

a guest
Mar 27th, 2012
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.61 KB | None | 0 0
  1. OgreTest::OgreTest() :
  2.   ogreRoot(new Ogre::Root()) {
  3.  
  4.   // WinRt apps only have one render system they can pick from...  
  5.   pickFirstAvailableRenderSystem();
  6.  
  7.   this->ogreRoot->initialise(false);
  8. }
  9.  
  10. // --------------------------------------------------------------------------------------------- //
  11.  
  12. void OgreTest::Initialize(CoreApplicationView ^applicationView) {
  13.   // ...omitted WinRT suspend/resume event subscribing stuff...
  14. }
  15.  
  16. // --------------------------------------------------------------------------------------------- //
  17.  
  18. void OgreTest::SetWindow(CoreWindow ^window) {
  19.   this->window = window;
  20.  
  21.   setupOgreRenderWindow();
  22. }
  23.  
  24. // --------------------------------------------------------------------------------------------- //
  25.  
  26. void OgreTest::Load(Platform::String ^entryPoint) {
  27.   this->ogreSceneManager = this->ogreRoot->createSceneManager(Ogre::ST_GENERIC);
  28.  
  29.   this->ogreCamera = this->ogreSceneManager->createCamera("DefaultCamera");
  30.   this->ogreCamera->setPosition(Ogre::Vector3(0.0f, 100.0f, 250.0f));
  31.   this->ogreCamera->lookAt(Ogre::Vector3(0.0f, 50.0f, 0.0f));
  32.  
  33.   this->ogreRenderWindow->removeAllViewports();
  34.   Ogre::Viewport *viewport = this->ogreRenderWindow->addViewport(this->ogreCamera);
  35.   viewport->setBackgroundColour(
  36.     Ogre::ColourValue(20.0f / 255.0f, 106.0f / 255.0f, 181.0f / 255.0f)
  37.   );
  38.  
  39.   // This thing generates shader at runtime. It's currently not clear whether
  40.   // D3DCompile() will be supported on ARM (in the Windows 8 Consumer Preview,
  41.   // the ARM DLL is missing), so it might be wise to not rely on it.
  42.   initializeRuntimeShaderSystem();
  43.  
  44.   // Only load what's necessary here. Metro applications must not become
  45.   // unresponsive for more than 50 ms at a time, so maybe load the resources necessary
  46.   // to display a loading screen here and do the rest asynchronously!
  47.   loadResources();
  48.  
  49.   // Create the example scene
  50.   createScene();
  51. }
  52.  
  53. // --------------------------------------------------------------------------------------------- //
  54.  
  55. void OgreTest::Run() {
  56.   BasicTimer ^timer = ref new BasicTimer();
  57.  
  58.   // App main loop
  59.   while(!this->windowClosed && !this->ogreRoot->endRenderingQueued()) {
  60.     timer->Update();
  61.     this->ogreRoot->renderOneFrame(timer->Delta);
  62.  
  63.     // Good old message pump :)
  64.     CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(
  65.       CoreProcessEventsOption::ProcessAllIfPresent
  66.     );
  67.   }
  68. }
  69.  
  70. // --------------------------------------------------------------------------------------------- //
  71.  
  72. void OgreTest::Uninitialize() {
  73. }
  74.  
  75. // --------------------------------------------------------------------------------------------- //
  76.  
  77. void OgreTest::pickFirstAvailableRenderSystem() {
  78.   assert(this->ogreRoot != nullptr);
  79.  
  80.   const Ogre::RenderSystemList &renderSystems = this->ogreRoot->getAvailableRenderers();
  81.   for(
  82.     Ogre::RenderSystemList::const_iterator iterator = renderSystems.begin();
  83.     iterator != renderSystems.end();
  84.     ++iterator
  85.   ) {
  86.     this->ogreRoot->setRenderSystem(*iterator);
  87.     return;
  88.   }
  89.  
  90.   throw std::runtime_error("No render systems registered");
  91. }
  92.  
  93. // --------------------------------------------------------------------------------------------- //
  94.  
  95. void OgreTest::setupOgreRenderWindow() {
  96.   Windows::Foundation::Rect bounds = this->window->Bounds;
  97.  
  98.   // You cannot create windows in metro (not even a message box), instead, Windows
  99.   // hands you a "window" - a full-screen view, actually - that you can use. Thus,
  100.   // we don't proactively create a render window but just set up our render window
  101.   // into the one provided by Windows.
  102.  
  103.   Ogre::NameValuePairList parameters;
  104.   parameters["externalWindowHandle"] = Ogre::StringConverter::toString(
  105.     (unsigned long)reinterpret_cast<void *>(this->window)
  106.   );
  107.  
  108.   this->ogreRenderWindow = this->ogreRoot->createRenderWindow(
  109.     "OgreTest Rendering Window",
  110.     static_cast<unsigned int>(bounds.Width), static_cast<unsigned int>(bounds.Height),
  111.     false, // fullscreen
  112.     &parameters
  113.   );
  114. }
  115.  
  116. // --------------------------------------------------------------------------------------------- //
  117.  
  118. void OgreTest::initializeRuntimeShaderSystem() {
  119.   if(!Ogre::RTShader::ShaderGenerator::initialize()) {
  120.     throw std::runtime_error("Could not initialize runtime shader system");
  121.   }
  122.  
  123.   this->ogreShaderGenerator = Ogre::RTShader::ShaderGenerator::getSingletonPtr();
  124.  
  125.   Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  126.     "Resources/RTShaderLib", "FileSystem"
  127.   );
  128.   this->ogreShaderGenerator->addSceneManager(this->ogreSceneManager);
  129.  
  130.   // TODO: Enabling this on WinRT doesn't work yet. It fails to write the test file.
  131.   //this->ogreShaderGenerator->setShaderCachePath("Resources/RTShaderCache");
  132. }
  133.  
  134. // --------------------------------------------------------------------------------------------- //
  135.  
  136. void OgreTest::loadResources() {
  137.   Ogre::ConfigFile resourcesCfg;
  138.   resourcesCfg.load("resources.cfg");
  139.  
  140.   // Go through all sections & settings in the file
  141.   Ogre::ConfigFile::SectionIterator sectionIterator = resourcesCfg.getSectionIterator();
  142.   while (sectionIterator.hasMoreElements()) {
  143.     Ogre::String sectionName = sectionIterator.peekNextKey();
  144.     Ogre::ConfigFile::SettingsMultiMap *settings = sectionIterator.getNext();
  145.  
  146.     for(
  147.       Ogre::ConfigFile::SettingsMultiMap::iterator iterator = settings->begin();
  148.       iterator != settings->end();
  149.       ++iterator
  150.     ) {
  151.       Ogre::String typeName = iterator->first;
  152.       Ogre::String archiveName = iterator->second;
  153.      
  154.       bool recursive = true;
  155.       Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
  156.         archiveName, typeName, sectionName, recursive
  157.       );
  158.     }
  159.   }
  160.  
  161.   Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
  162. }
  163.  
  164. // --------------------------------------------------------------------------------------------- //
  165.  
  166. void OgreTest::createScene() {
  167.  
  168.   // TODO: If you attemp to render the example mesh, the renderer will explode complaining:
  169.   //
  170.   //   "Attempted to render to a D3D11 device without both vertex and fragment shaders there
  171.   //   is no fixed pipeline in d3d11 - use the RTSS or write custom shaders."
  172.   //
  173.   // To see the actual error message, hit Ctrl+D,E to bring up the exception menu
  174.   // and tick the checkboxes in the "Thrown" column for C++ exceptions.
  175.  
  176.   this->ogreSceneManager->setAmbientLight(Ogre::ColourValue::White);
  177.   //Ogre::Entity *entity = this->ogreSceneManager->createEntity("Head", "ogrehead.mesh");
  178.   //Ogre::SceneNode *node = this->ogreSceneManager->getRootSceneNode()->createChildSceneNode("HeadNode");
  179.   //node->attachObject(entity);
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement