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

Author Topic: SFML2 & OpenSceneGraph (Here is a Code Example)  (Read 9578 times)

0 Members and 1 Guest are viewing this topic.

caracal

  • Newbie
  • *
  • Posts: 19
    • View Profile
SFML2 & OpenSceneGraph (Here is a Code Example)
« on: January 01, 2012, 11:57:58 am »
I see the same question asked over and over again how do you
get 3d graphics in sfml? Well here is a simple example.

All you need is SFML2 and OpenSceneGraph v3

Code: [Select]


// 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();
}
}


Screenshot



Hope that helps

caracal

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: SFML2 & OpenSceneGraph (Here is a Code Example)
« Reply #1 on: April 28, 2012, 05:02:27 am »
Here is an update that works with the new SFML2 api style.

// 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()
{
        // Create Game Window with Title
        sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML2 OpenSceneGraph Example1");
       
        // Create & Load Splashscreen Image
        sf::Texture texture;
        if (!texture.loadFromFile("data/background.png"))
                return EXIT_FAILURE;
        sf::Sprite sprite(texture);


        // Create 3D Viewport
        osgUtil::SceneView* viewer = new osgUtil::SceneView();
        viewer->setDefaults();
 
        // Create Camera
        osg::Camera* camera;
        camera = viewer->getCamera();
        camera->setViewport(0, 0, 640, 480); // Set 3D Viewport Position and Size
        camera->setClearColor(osg::Vec4(0, 0, 0, 0)); // Set Scene Background Color

        // Create 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));

        // Create 3D Model, Apply Transformation
        osg::Node* mesh = osgDB::readNodeFile("data/untitled.3ds");
        root->addChild(meshTrans); // Add Trasformation to Scene
        meshTrans->addChild(mesh); // Add 3D Model to Transformation
         
        // Set Root Scene Node to View and Init Vew
        viewer->setSceneData(root);
        viewer->init();


        // Start the Game Loop
        while (window.isOpen())
        {
                // 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);
       
                window.pushGLStates();
                viewer->update();
                viewer->cull();
                viewer->draw();
                window.popGLStates();

                window.display();
        }
}
 
« Last Edit: April 28, 2012, 09:03:17 am by Laurent »

MonkeyKnight

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: SFML2 & OpenSceneGraph (Here is a Code Example)
« Reply #2 on: May 02, 2012, 06:18:10 pm »
Stellar.

Is there any way you could upload your assets?

I can just as easily google and download a random .3ds, but seeings as yours is proven to work I would rather yours. If you don't mind, of course. :D


caracal

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: SFML2 & OpenSceneGraph (Here is a Code Example)
« Reply #3 on: May 05, 2012, 06:23:34 pm »
Stellar.

Is there any way you could upload your assets?

I can just as easily google and download a random .3ds, but seeings as yours is proven to work I would rather yours. If you don't mind, of course. :D

Ohhh sorry I didn't realize anyone had posted on this topic.  I will look into getting an example online but to be truthful I am having a little trouble with this example my self.

1. With Lights and Materials added no models are showing up.
2. I cant seam to get models textures to show.
3. Model Animation is an issue also.

I am still working on it but I have some good news, I have osgOcean(Ocean Simulator), osgPPU(HDR, Bloom, ... Effects, osgBullet(Physics) and osgRocket(LibRocket) and Googles 2D Graphics Library (Skia) all working out of the box. Once i solve the above issue then we will have pretty much a  complete 3D Game Engine in SFML2.
« Last Edit: May 05, 2012, 06:33:29 pm by caracal »

eugustus

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: SFML2 & OpenSceneGraph (Here is a Code Example)
« Reply #4 on: December 31, 2016, 07:33:06 pm »
Here is an update that works with the new SFML2 api style.

Hi guys, this not works but this works great.

This how integrate with SFGUI https://gist.github.com/colesnicov/bca684b939d19e2d1ac74d9a881b35b1


 

anything