Monday, March 13, 2017

How to build OpenSceneGraph with Code::Blocks



This building process was implemented in OpenSuse 42.2, CodeBlocks 16.01, CMake 3.5.2, OpenSceneGraph-3.0.1



Step 1. Preparation


Building the OSG requires CMake, due to the extensive use of CMake directives in the Makefiles. I used CMake-GUI to make easy setting up OSG option. You may download cmake from https://cmake.org

Next preparation, download the OSG source from OpenSceneGraph website. In this project, I used version OpenSceneGraph-3.0.1


Extract the OpenSceneGraph source into the correct folder for example
  /home/addies/build/osg

 then you will get the result as below
  /home/addies/build/osg/OpenSceneGraph-3.0.1

Create new folder osgBuild
 cd /home/addies/build
 mkdir osgBuild

Final result
  /home/addies/build/osg/osgBuild
 /home/addies/build/osg/OpenSceneGraph-3.0.1


[you can skip this step if you have already installed OpenGL development. For Ubuntu and Windows have been installed normally. In my case, I need to install it]

Please make sure that your OpenSuse has been installed OpenGL. Check your correspondens openGL library then install it
  zypper se mesa | grep devel

searching result like this


 Install OpenGL lib with zypper
  sudo zypper install Mesa-libGL-devel



Step 2. Configure and Generate OSG

Please open your CMake-GUI


put "Where is the source code" with path /home/addies/build/osg/OpenSceneGraph-3.0.1 and next put "Where to build the binaries" with path /home/addies/build/osg/osgBuild then click button Configure, cmake will ask you to select "Specify the generator for this project". Please select CodeBlocks-Unix Makefiles then click finish.




CMake will report with Configuring Done and click Generate. CMake will generate all configured file in your build folder /home/addies/build/osg/osgBuild.



Step 3. Compile OSG with CodeBlocks

Please open your CodeBlocks and open OpenSceneGraph.cbp project from your build folder /home/addies/build/osg/osgBuild and Build (or press Ctrl + F9).

Result in /home/addies/build/osg/osgBuild/bin folder


Result in /home/addies/build/osg/osgBuild/lib folder



Finally, let test the binaries



Step 4. Hello World OSG with CodeBlocks

Please create new project by clicking File --> New --> Project --> Console Application --> Next --> C++ and just put in your project name as HelloWorld. Copy code as below into your main.cpp file

/**
   Hello World to display capsule geometry
*/
#include <iostream>
#include <osgViewer/Viewer>
#include <osg/ShapeDrawable>

using namespace std;

int main()
{
    osgViewer::Viewer viewer;
    osg::ref_ptr<osg::Group> root (new osg::Group);
    osg::ref_ptr<osg::Geode> myshapegeode (new osg::Geode);

    osg::ref_ptr<osg::Capsule> myCapsule (new osg::Capsule(osg::Vec3f(),1,2));
    osg::ref_ptr<osg::ShapeDrawable> capsuledrawable (new
             osg::ShapeDrawable(myCapsule.get()));
    myshapegeode->addDrawable(capsuledrawable.get());
  
    root->addChild(myshapegeode.get());
    viewer.setSceneData( root.get() );
  
    return (viewer.run());
}

 Before compiling the source, let us setup the CodeBlocks global variable. Select menu Settings --> Global variables. Click new button to create Current Variable and set the name as OSG. Next,  fill in Built-in fields as shown as below then close the window.




Next setting Build Options by right click your project and select Build Options .

in tab Linker settings, add
   libOpenThreads, libosg, libosgDB, libosgGA, libosgUtil, libosgViewer 


in Tab Search directories --> Compiler, add $(#OSG.include)


in Tab Search directories --> Linker add $(#OSG.lib)


Then Ok. That's all. You can build the project. If you're running successfully then you will get Capsule Geometry shown as below.






Hope you enjoy this tutorial






Other Topics:




No comments:

Post a Comment