Precondition

  • OSG library has been built in x32 or x64
  • OSG Example data has been downloaded.
  • assuming OSGPATH is setting as D:\OSG
    • include - D:\OSG\include
    • lib - D:\OSG\lib
    • bin - D:\OSG\bin
    • data - D:\OSG\data
  • System environment has been set corroctly
    • PATH - add D:\OSG\bin to PATH
    • OSG_FILE_PATH - create this key with value D:\OSG\data
  • Qt Kit installed
    • MSVC2015 32bit or 64bit should be same with OSG library build

Overview

Normally you can find many instructions to say “Hello World” in OSG style.
It should look like following steps:

  1. Create a empty Qt Console Application.
  2. Add OSG lib to project properties
  3. Add codes to show a 3D model
  4. Compile and run application.

Create Project

Create an empty Qt Console Application project.

Create new project

File -> New File or Project…
select -> Application -> Qt Console Application -> Choose…
Build system -> qmake -> Next
Kit Selection -> MSVC2015 64bit -> Finish

create new project

application settings

Configure Project Properties

right click project -> Add libraries…
Type -> select External library -> Next
Details select D:\OSG\lib\osg.lib -> Next
Summary -> Finish
Follow the same process to add
osgViewer.lib
osgDB.lib
OpenThreads.lib

Build -> Run qmake

Important
It’s a very important procedure never forget it, is to run qmake again after adding library, or it will not be imported to the project.

project properties

project properties

project properties

Notice
pay more attention to using same x64 or x32 Platform configuration!

Add Codes

Normally codes will be like this:

#include <osgViewer/Viewer>
#include <osgdb/readfile>


int main(){

    osg::ref_ptr <osgViewer::Viewer> viewer = new osgViewer::Viewer;

    // set background color to dark
    viewer->getCamera()->setClearColor(osg::Vec4(0.2f, 0.2f,0.2f, 0.0f));

    // load osg model data which has been set under "OSG_FILE_PATH"
    viewer->setSceneData(osgDB::readNodeFile("glider.osg"));

    // set window start position and size
    viewer->setUpViewInWindow(400,200,800,600);
    viewer->realize();

    // run viewer
    int ret = viewer->run();
    return ret;
}

Alternatively, if we want to keep the same source code also can be built on Visual Studio, can add following code

#ifdef _DEBUG
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "OpenThreadsd.lib")
#pragma comment(lib, "osgd.lib")
#else
#pragma comment(lib, "osgViewer.lib")
#pragma comment(lib, "osgDB.lib")
#pragma comment(lib, "OpenThreads.lib")
#pragma comment(lib, "osg.lib")
#endif

#if defined(_WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64)
#include <windows.h>
#endif

#include <osgViewer/Viewer>
#include <osgdb/readfile>


int main(){

    osg::ref_ptr <osgViewer::Viewer> viewer = new osgViewer::Viewer;

    // set background color to dark
    viewer->getCamera()->setClearColor(osg::Vec4(0.2f, 0.2f,0.2f, 0.0f));

    // load osg model data which has been set under "OSG_FILE_PATH"
    viewer->setSceneData(osgDB::readNodeFile("glider.osg"));

    // set window start position and size
    viewer->setUpViewInWindow(400,200,800,600);
    viewer->realize();

    // run viewer
    int ret = viewer->run();
    return ret;
}

Compile and Run

A 3D model should be displayed on the screen correctly.

HelloOsg run

Leave a Comment