OpenSceneGraph Offscreen Renderer
Efficient object rendering engine for offscreen rendering using OpenSceneGraph 3
The renderer contains python
and matlab
bindings. The renderer is easy to use and caches CAD models so that it does not load the CAD model every time it renders.
The C++ object is cached using Mexplus, and the matlab wrapper holds the C++ class instance until a user destroys the instance. The Renderer object remains on the memory, and it contains all the loaded CAD models and returns data to MATLAB or python directly. Thus it is optimal for on-the-fly rendering or visualization.
There are two modes for installation: one that require OSG installation, which is recommended, and one that works without OSG installation using prebuilt OSG libraries provided in this repo.
Example
IPython Notebook Demo
http://nbviewer.ipython.org/gist/chrischoy/19f7765c401973d28243
Python
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from PyRenderer import Renderer
viewport_size_x = 700
viewport_size_y = 700
azimuth = 0
elevation = 0
yaw = 0
distance_ratio = 1
field_of_view = 25
x = Renderer()
x.initialize(['mesh/2012-VW-beetle-turbo.3ds','mesh/Honda-Accord.3ds'],
viewport_size_x, viewport_size_y)
# Render
x.setViewpoint(azimuth, elevation, yaw, distance_ratio, field_of_view)
rendering, depth = x.render()
# Flip dimension
rendering = rendering.transpose((2,1,0))
depth = depth.transpose((1,0))
# image show
plt.imshow(rendering)
plt.show()
# depth show
plt.imshow(1-depth)
plt.show()
Matlab
% Add binary path
addpath('./bin');
% Initialize the Matlab object.
rendering_size_x = 700; rendering_size_y = 700; % pixels
azimuth = 90; elevation = 10; yaw = 0;
% Modify distance ratio to control distance from the object
% distance_ratio = 1 is the default
distance_ratio = 0; field_of_view = 25;
% Setup Renderer
renderer = Renderer();
renderer.initialize({'mesh/2012-VW-beetle-turbo.3ds', ...
'mesh/Honda-Accord.3ds'},...
rendering_size_x, rendering_size_y)
% If the output is only the rendering, it renders more efficiently
renderer.setModelIndex(1);
renderer.setViewpoint(azimuth,elevation,yaw,distance_ratio,field_of_view);
[rendering]= renderer.render();
subplot(221);
imagesc(rendering); axis equal; axis tight; axis off;
% Once you initialized the renderer, you can just set
% the viewpoint and render without loading CAD model again.
renderer.setModelIndex(2);
renderer.setViewpoint(azimuth,elevation,yaw,distance_ratio,field_of_view);
[rendering]= renderer.render();
subplot(222);
imagesc(rendering); axis equal; axis tight; axis off;
% If you give the second output, it renders depth too.
renderer.setModelIndex(1);
renderer.setViewpoint(45,20,0,2,25);
[rendering, depth]= renderer.render();
subplot(223);imagesc(rendering); axis equal; axis off;
subplot(224);imagesc(1-depth); axis equal; axis off; colormap hot;
% Return viewmatrix
renderer.getViewMatrix()
% Return projection matrix
renderer.getProjectionMatrix()
% You must clear the memory before you exit
renderer.delete(); clear renderer;
Output
Install : MATLAB Binding
Install : Standard (Linux/Mac)
Note
Prebuilt OSG library only works for Linux.
-
Install Open Scene Graph from https://github.com/openscenegraph/osg
git clone https://github.com/openscenegraph/osg cd osg make all sudo make install
-
Clone the OSGRenderer repo
git clone https://github.com/chrischoy/OSGRenderer.git
-
Go to the
OSGRenderer
folder and runcompile.m
Install : Prebuild (Linux Only)
-
Clone the OSGRenderer repo
git clone https://github.com/chrischoy/OSGRenderer.git
-
Add compile library path and runtime library path. Note that
LD_LIBRARY_PATH
is for application library search path andLIBRARY_PATH
is for compiler library search path. If you open new command line session, you must these lines every time unless you add the ld library path to.bashrc
cd OSGRenderer export LIBRARY_PATH=$LIBRARY_PATH:path/to/OSGRenderer/lib/osg/:path/to/OSGRenderer/lib/boost/ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:path/to/OSGRenderer/lib/osg:path/to/OSGRenderer/lib/boost/
-
Go to the
OSGRenderer
folder and runcompile.m
Other IDE (Eclipse)
- Use OpenSceneGraph https://github.com/openscenegraph/osg to download latest OSG.
- Eclipse Setting
-
Library to include
GL, GLU, osg, osgDB, osgGA, osgViewer, osgUtil, boost_program_options
-
If you installed OSG on
/usr/local/lib64
(which is default) go toRun Configuration
, add Environment variableLD_LIBRARY_PATH
and value/usr/local/lib64
-
Compile
g++ -o OSGRenderer OSGRenderer.cpp -lGL -losg -losgViewer -losgDB -losgGA -losgUtil -lboost_program_options -O3
Install : Python Binding
-
Install Boost http://www.boost.org
-
Modify Makefile variables
# Run numpy.get_include() in python to get the following path NUMPY_INCLUDE = /path/to/anaconda/lib/python2.7/site-packages/numpy/core/include # PYCONFIG_PATH = /usr/local/python2.7/ PYCONFIG_PATH = /path/to/anaconda/include/python2.7
-
make it
make
ISSUE
-
Update gcc version to < 4.8.
-
If you use MATLAB < 2012b, make sure to use matlab libstdc++ library when compiling OSG.
Input CAD format
List of available formats http://trac.openscenegraph.org/projects/osg//wiki/Support/UserGuides/Plugins
To get free models, use Google 3D Warehouse and use Sketchup to edit models. Rotate the model and export it to the format that you will use.
COLLADA (DAE) format support
Follow the direction of https://github.com/openscenegraph/osg/tree/master/src/osgPlugins/dae
Specifically, http://collada.org/mediawiki/index.php/DOM_guide:_Setting_up
-
download COLLADA from
svn co https://collada-dom.svn.sourceforge.net/svnroot/collada-dom/trunk colladadom
-
compile COLLADA
cd colladadom ccmake .
and configure and generate
Makefile
. andmake
-
download openscenegraph
cd /to/your/osg/directory/ git clone https://github.com/openscenegraph/osg
-
go to the downloaded
osg
andccmake
the foldercd osg ccmake .
and configure and make sure that COLLADA_DOM_ROOT, COLLADA_BOOST_FILESYSTEM_LIBRARY, COLLADA_DYNAMIC_LIBRARY, COLLADA_INCLUDE_DIR have been set correctly.
COLLADA_DOM_ROOT : COLLADA_BOOST_FILESYSTEM_LIBRARY : Boost library. something like /usr/local/lib/libboost_filesystem.dylib COLLADA_BOOST_SYSTEM_LIBRARY : Boost library. something like /usr/local/lib/libboost_system.dylib COLLADA_DYNAMIC_LIBRARY : /path/to/colladadom/dom/libcollada-dom2.4-dp.dylib COLLADA_INCLUDE_DIR : /path/to/colladadom/dom/include where dae.h resides
make
The precompiled library contains osgPlugin for collada format. (LINUX)
COLLADA MAC INSTALL (DOM 2.2 Install Fail on 10.10)
Install DOM2.2 export COLLADA_DIR=/path/to/collada/; cmake . make
ISSUE
Q: Warning: could not find plugin to read objects from file .
A: The reason that it fails to load is that the renderer fails to load appropriate shared library. In the prebuilt ./OSG/lib/osgPlugins-3.3.2, yoou can see several plugins. Add the plugin path to 'LD_L IBRARY_PATH`.
You must either install complete OSG package or download from http://trac.openscenegraph.org/projects/osg//wiki/Downloads/Dependencies
Q: Your MEX-file “path/OSGRenderer/Renderer_mexa64” is locked and must be unlocked before recompiling
The binary file is in use. Restart the matlab which will clear the memory.
TODO
- Windows installation
- Add dylib for prebuilt OS X
Written with StackEdit.