Skip to content

Instantly share code, notes, and snippets.

@zester
Created April 17, 2012 01:08
Show Gist options
  • Save zester/2402691 to your computer and use it in GitHub Desktop.
Save zester/2402691 to your computer and use it in GitHub Desktop.
SFML2 and OpenSceneGraph 3
// g++ main.cpp -o main -lsfml-window -lsfml-graphics -lsfml-system -losg -losgDB -losgGA -losgUtil
#include <SFML/Graphics.hpp>
#include <osgUtil/SceneView>
#include <osg/Node>
#include <osg/CameraNode>
#include <osg/Group>
#include <osgDB/ReadFile>
#include <osg/PositionAttitudeTransform>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML2 OpenSceneGraph Example1");
// Load a sprite to display
sf::Texture texture;
if (!texture.LoadFromFile("../data/background.jpg"))
return EXIT_FAILURE;
sf::Sprite sprite(texture);
// OSG Code Start
// View
osgUtil::SceneView* viewer = new osgUtil::SceneView();
viewer->setDefaults();
// Camera
osg::Camera* camera;
camera = viewer->getCamera();
camera->setViewport(20, 20, 640, 480);
camera->setClearColor(osg::Vec4(0, 0, 0, 0));
// Root Scene Node
osg::Group* root = new osg::Group();
// Transformation Object for our 3D Model
osg::PositionAttitudeTransform* meshTrans = new
osg::PositionAttitudeTransform();
meshTrans->setPosition(osg::Vec3d( 3, 0, -6));
// 3D Model
osg::Node* mesh = osgDB::readNodeFile("../data/ship.3ds");
root->addChild(meshTrans);
meshTrans->addChild(mesh);
// Set Root Scene Node to View and Init Vew
viewer->setSceneData(root);
viewer->init();
// OSG Code End
// Start the game loop
while (window.IsOpened())
{
// Process events
sf::Event event;
while (window.PollEvent(event))
{
// Close window : exit
if (event.Type == sf::Event::Closed)
window.Close();
}
window.Clear();
window.Draw(sprite);
// OSG Code Start
window.SaveGLStates();
viewer->update();
viewer->cull();
viewer->draw();
window.RestoreGLStates();
// OSG Code End
window.Display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment