Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Using SFML 2.1 for Window, Input, ... with Ogre3D Example(Working)  (Read 6536 times)

0 Members and 1 Guest are viewing this topic.

caracal

  • Newbie
  • *
  • Posts: 19
    • View Profile
Here is an example on how to use SFML v2.1 for Window, Input, ... for Ogre v1.9
Note: Currently this only works on linux,  changes to this line of code maybe required for other platforms. "misc["currentGLContext"] = Ogre::String("true");"

Performance appears to be very good.

I tried briefly to draw 2D Graphics via SFML but I wasn't able to get anything to show-up, I will have to look further into it. 

The SFML related code is actually very short and simple, I did however provide a full Ogre example with a frame-listener, etc. But 99% of the provided code in the example isn't needed.

Thanks to this Ogre/GLFW Example http://www.ogre3d.org/forums/viewtopic.php?f=2&t=69853 I was able to get this working.

I hope you find this useful.



// g++ main.cpp -o main `pkg-config --libs --cflags OGRE` -lsfml-window -lsfml-graphics -lsfml-system -lGL

// SFML v2.1 Includes
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>

// Ogre v1.9 Includes
#include <Ogre.h>
#include <OgreFrameListener.h>

using namespace Ogre;
     
//
class MyFrameListener : public Ogre::FrameListener {
public:
  bool frameStarted (const FrameEvent &evt);
  bool frameEnded   (const FrameEvent &evt);
  bool frameRenderingQueued  (const FrameEvent &evt);
};
     
//
bool MyFrameListener::frameStarted(const FrameEvent &evt) {
  //std::cout << "Frame Started" << std::endl;
  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()
{
  // create the window
  sf::RenderWindow window(sf::VideoMode(800, 600), "Ogre3D v1.9 and SFML v2.1", sf::Style::Default, sf::ContextSettings(32));
 
  // Create an instance of the OGRE Root Class
  Ogre::Root* ogreRoot = new Ogre::Root;
 
  // Configures the application
  if (!ogreRoot->restoreConfig())
    ogreRoot->showConfigDialog();
    ogreRoot->saveConfig();
 
  // Create Rendering System, but don't initialise it.
  ogreRoot->setRenderSystem(ogreRoot->getAvailableRenderers()[0]);
  ogreRoot->initialise(false);
 
  //
  Ogre::NameValuePairList misc;
  misc["currentGLContext"] = Ogre::String("true");
 
  // Create a render window | Note: Window Title and Size are not important here.
  Ogre::RenderWindow* ogreWindow = ogreRoot->createRenderWindow("Ogre Window", 800, 600, false, &misc);
  ogreWindow->setVisible(true);
 
  // Create Render Target
  Ogre::RenderTarget *ogreRenderTarget = ogreWindow;

  // Create Scene Manager
  Ogre::SceneManager* ogreSceneMgr = ogreRoot->createSceneManager(Ogre::ST_GENERIC);

  // Create a new camera
  Ogre::Camera* ogreCamera = ogreSceneMgr->createCamera("Camer0");
  ogreCamera->setPosition(Ogre::Vector3(0,0,10));
  ogreCamera->lookAt(Ogre::Vector3(0,0,-300));
  ogreCamera->setNearClipDistance(5);

  // Create OGRE Viewport and Set Background Colour
  Ogre::Viewport* vp = ogreWindow->addViewport(ogreCamera);
  vp->setBackgroundColour(Ogre::ColourValue(0,0,0));

  // Use the viewport to set the aspect ratio of the camera
  ogreCamera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
 
  // Add our model to our resources and index it
  Ogre::ResourceGroupManager::getSingleton().addResourceLocation("media/packs/Sinbad.zip", "Zip");
  Ogre::ResourceGroupManager::getSingleton().addResourceLocation("media/models/", "FileSystem");
  Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
 
  // Set Scene Ambient Light.
  ogreSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
 
  // Create a new light.
  Ogre::Light* light = ogreSceneMgr->createLight("Light0");
  light->setPosition(20.0f, 80.0f, 50.0f);

  // Create an instance of our model and add it to the scene
  Ogre::Entity* ent = ogreSceneMgr->createEntity("Sinbad.mesh");
  Ogre::SceneNode* entNode = ogreSceneMgr->createSceneNode("Character0");
  entNode->attachObject(ent);
  ogreSceneMgr->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();
  ogreRoot->addFrameListener(myListener);

  // run the main loop
  bool running = true;
  while (running)
  {
    //Ogre::WindowEventUtilities::messagePump();
   
    // handle events
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
      {
        // end the program
        running = false;
      }
      else if (event.type == sf::Event::Resized)
      {
        // adjust the viewport when the window is resized
        //glViewport(0, 0, event.size.width, event.size.height);
      }
    }
   
    // clear the buffers
    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    window.clear();
   
    // draw...
    ogreRoot->renderOneFrame();

    // end the current frame (internally swaps the front and back buffers)
    window.display();
  }
 
  // release resources...
  delete myListener;
  delete ogreRoot;
   
  return 0;
}
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Using SFML 2.1 for Window, Input, ... with Ogre3D Example(Working)
« Reply #1 on: November 12, 2013, 05:46:46 am »
You should put this on the SFML wiki. And maybe even tidy up your overall code style (comments, code indenting, commented code, ect)  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

caracal

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Using SFML 2.1 for Window, Input, ... with Ogre3D Example(Working)
« Reply #2 on: November 12, 2013, 06:29:13 am »
You should put this on the SFML wiki. And maybe even tidy up your overall code style (comments, code indenting, commented code, ect)  ;)

I plan on it, this was a quick post just to get the example online so it doesn't get lost.

I am going to modify the example with the following snippet I wrote in order to do away with the "Ogre Config Dialog"

#if defined OIS_LINUX_PLATFORM
  mRoot->loadPlugin("/usr/lib/OGRE/Plugin_ParticleFX");
  mRoot->loadPlugin("/usr/lib/OGRE/Plugin_CgProgramManager");
  mRoot->loadPlugin("/usr/lib/OGRE/Plugin_OctreeSceneManager");
  mRoot->loadPlugin("/usr/lib/OGRE/Plugin_PCZSceneManager");
  mRoot->loadPlugin("/usr/lib/OGRE/Plugin_OctreeZone");
  mRoot->loadPlugin("/usr/lib/OGRE/Plugin_BSPSceneManager");
  mRoot->loadPlugin("/usr/lib/OGRE/RenderSystem_GL");
  #endif

  Ogre::RenderSystemList::const_iterator renderers = mRoot->getAvailableRenderers().begin();

  while(renderers != mRoot->getAvailableRenderers().end())
  {
    Ogre::String rName = (*renderers)->getName();

    if (rName == "OpenGL Rendering Subsystem")
      break;

    renderers++;
  }

  Ogre::RenderSystem *renderSystem = *renderers;
  renderSystem->setConfigOption("Full Screen","No");
  renderSystem->setConfigOption("Video Mode","1024 x 768 @ 32-bit colour");
  renderSystem->setConfigOption("Display Frequency","50 Hz");
  renderSystem->setConfigOption("FSAA","16");
  renderSystem->setConfigOption("Fixed Pipeline Enabled","Yes");
  renderSystem->setConfigOption("RTT Preferred Mode","FBO");
  renderSystem->setConfigOption("VSync","No");
  renderSystem->setConfigOption("sRGB Gamma Conversion","No");

  mRoot->setRenderSystem(renderSystem);

  // Create a render window
  mWindow =  mRoot->initialise(true, "MyGame v0.0.1");
 

Cody

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Using SFML 2.1 for Window, Input, ... with Ogre3D Example(Working)
« Reply #3 on: December 01, 2013, 02:24:32 pm »
The example was quite useful to me, thanks.

Unfortunately the code you presented didn't work on Windows. The "currentGLContext" option seems to be ignored. This can be fixed by retrieving the context manually and set options accordingly. The code below works for me on Windows.

        Ogre::NameValuePairList misc;
#ifdef _WIN32
        unsigned long winHandle = reinterpret_cast<unsigned long>(window.getSystemHandle());
        unsigned long winGlContext = reinterpret_cast<unsigned long>(wglGetCurrentContext());

        misc["externalWindowHandle"] = StringConverter::toString(winHandle);
        misc["externalGLContext"] = StringConverter::toString(winGlContext);
        misc["externalGLControl"] = String("True");
#else
        misc["currentGLContext"] = String("True");
#endif

        Ogre::RenderWindow* ogreWindow = ogreRoot->createRenderWindow("Ogre Window", 800, 600, false, &misc);
 

Also, I found out how to handle the resize events correctly.

if (event.type == sf::Event::Resized) {
    ogreWindow->windowMovedOrResized();
    ogreCamera->setAspectRatio(Ogre::Real(event.size.width) / Ogre::Real(event.size.height));
}
 

 

anything