Example 4: Mixed OSG/SFML Rendering
This is what eXpl0it3r already hinted at: Drawing from both APIs. This can be somewhat tricky due to the shared OpenGL state. Nevertheless, it is possible and here is one way to go about it:
#if defined(_DEBUG)
#pragma comment(linker, "/SUBSYSTEM:\"console\"")
#else
#pragma comment(linker, "/SUBSYSTEM:\"windows\" /ENTRY:\"mainCRTStartup\"")
#endif
#if defined(_DEBUG)
#pragma comment(lib, "sfml-graphics-d.lib")
#pragma comment(lib, "sfml-window-d.lib")
#pragma comment(lib, "sfml-system-d.lib")
#else
#pragma comment(lib, "sfml-graphics.lib")
#pragma comment(lib, "sfml-window.lib")
#pragma comment(lib, "sfml-system.lib")
#endif
#if defined(_DEBUG)
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib")
#else
#pragma comment(lib, "osg.lib")
#pragma comment(lib, "osgDB.lib")
#pragma comment(lib, "osgGA.lib")
#pragma comment(lib, "osgViewer.lib")
#endif
#include <SFML/Graphics.hpp>
#pragma warning(push)
#pragma warning(disable: 4250)
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#pragma warning(pop)
#include "sf2osg.h"
int main(int argc, char** argv)
{
//osg::setNotifyLevel(osg::DEBUG_INFO);
sf::RenderWindow window;
window.setVerticalSyncEnabled(true);
sf::VideoMode mode = sf::VideoMode::getDesktopMode();
mode.width = 800;
mode.height = 600;
window.create(mode, "SFML+OSG Example");
osg::ref_ptr<osg::Node> root = osgDB::readNodeFile("cow.osg");
if (!root)
{
std::cerr << "Failed to load model." << std::endl;
return 1;
}
sf::Vector2u size = window.getSize();
sf::Font font;
if (!font.loadFromFile("verdana.ttf"))
{
std::cerr << "Failed to load font." << std::endl;
return 1;
}
sf::Text text("Hello SFML+OSG!", font, 24);
sf::FloatRect bounds = text.getLocalBounds();
text.setPosition((size.x-bounds.width)/2.0f, (size.y-bounds.height)/2.0f);
osgViewer::Viewer viewer;
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw = viewer.setUpViewerAsEmbeddedInWindow(0, 0, size.x, size.y);
viewer.setCameraManipulator(new osgGA::TrackballManipulator);
viewer.setSceneData(root.get());
viewer.realize();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
sf2osg::processEvent(*(gw->getEventQueue()), event);
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Escape)
{
window.close();
}
break;
case sf::Event::Resized:
window.setView(sf::View(sf::FloatRect(0.0f, 0.0f, float(event.size.width), float(event.size.height))));
gw->resized(0, 0, event.size.width, event.size.height);
break;
}
}
if (!window.isOpen()) break;
window.setActive();
viewer.frame();
window.pushGLStates();
window.draw(text);
window.popGLStates();
window.display();
}
return 0;
}
While it can be done, I am not convinced that it is actually a good idea. It may be better to just stick with one rendering API. If you're already using OSG, you might as well use it for all your drawing code.