1 下载源码进行编译
(大致参照https://vicrucann.github.io/tutorials/osg-linux-quick-install/)
git clone https://github.com/openscenegraph/OpenSceneGraph.git
git checkout -b 3.6 origin/OpenSceneGraph-3.6
mkdir build
cd build
cmake ..
make -j8
make install
这个时候根据你的make install的结果判断你的osg安装在什么位置,
我的是位于: /usr/local/include, /usr/local/lib, /usr/local/bin
将这三个路径添加到$PATH中,vim /etc/profile,在其尾部添加如下代码:
# set the osg env path:
export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"
export OPENTHREADS_INC_DIR="/usr/local/include"
export OPENTHREADS_LIB_DIR="/usr/local/lib"
export PATH="/usr/local/share:$OPENTHREADS_LIB_DIR:$OPENTHREADS_INC_DIR:$PATH"
验证库的位置:
[nash5 home]# ls /usr/local/include/ | grep osg
osg
osgAnimation
osgDB
osgFX
osgGA
osgManipulator
osgParticle
osgPresentation
osgShadow
osgSim
osgTerrain
osgText
osgUI
osgUtil
osgViewer
osgVolume
osgWidget
2 下载实例数据:
osg -> http://www.openscenegraph.org/index.php/download-section/data
3 测试:
osgviewer ../BASE_ENV/OpenSceneGraph-Data/cow.osg
使用如下代码测试:
CMakeLists.txt
cmake_minimum_required(VERSION 3.13) # CMake version check
project(osgWeb) # Create project "simple_example"
set(CMAKE_CXX_STANDARD 14) # Enable c++14 standar
# Add main.cpp file of project root directory as source file
set(SOURCE_FILES test.cpp)
# set the emscripten's dir and lib
include_directories(/home/nash5/BASE_ENV/emsdk/upstream/emscripten/system/include)
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
# Add executable target with source files listed in SOURCE_FILES variable
add_executable(test.out ${SOURCE_FILES})
target_link_libraries(test.out libosgDB.so libosgViewer.so)
test.cpp
#include <iostream>
#include <osgDB/ReadFile>
#include <osg/Node>
#include <osgViewer/Viewer>
int main()
{
std::cout << "Hello World!\n";
std::string modelPath = "/home/nash5/BASE_ENV/OpenSceneGraph-Data/cessna.osg";
osgViewer::Viewer viewer;
viewer.setSceneData(osgDB::readNodeFile(modelPath));
viewer.realize();
viewer.run();
std::cout << "Hello World!\n";
}
或者不使用CMakeLists.txt,直接使用g++编译:
g++ a.cpp -losgDB -losgViewer -o a.out
./a.out