原文地址:http://blog.csdn.net/wonegequ1/article/details/46501397
寫作目的:osg for Android編譯所需安裝軟件及步驟都比較多,方便以后查閱以記錄,如對他人有些許幫助,那再好不過。所需軟件已在我360雲盤分享
1、jdk安裝,我用的版本是jdk1.7 。 http://yunpan.cn/cQfzNgLtxMYLV (提取碼:f405)
jdk安裝沒什么好說的,一直下一步,裝完后,添加JAVA_HOME環境變量到安裝目錄,CLASSPATH把必須的jar庫路徑加上,將Java的bin目錄路徑加到環境變量中。我的為
JAVA_HOME:C:\Program Files (x86)\Java\jdk1.7.0_13
CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\toos.jar
path中加入%JAVA_HOME%/bin;


2、下載Eclipse,我已裝好ADT插件,直接在雲盤下載,直接解壓即可使用,http://yunpan.cn/cQfiLH8QAwumr (提取碼:8fe1)
3、Android ndk 下載,http://yunpan.cn/cQy6uyANhiXYN (提取碼:4e49)
4、Android sdk 下載安裝,由於google被牆,sdk老是下載不了,可以用我分享的http://yunpan.cn/cQy6FFBfjShYF (提取碼:ce73)
5、在Eclipse設置Android sdk的路徑
在Windows菜單下點擊Preference菜單項,在SDK Location中填入sdk路徑

6、cygwin安裝,http://yunpan.cn/cQfIUtmiXFcVV (提取碼:1857)
安裝編譯C++的必須庫(都在Devel下):cmake,make,gcc-core,gcc-g++
(1)

(2)選擇安裝目錄

(3)選擇緩存目錄,以后如果再一次安裝,設置設置到該目錄,可不用每次下載

(4)選擇下一步,選擇下載站點

(5)安裝必須庫



(6)選擇下一步,開始安裝庫,完畢
7、下載3rdpartyforandroid(http://yunpan.cn/cQfhUmzw5W8Jf (提取碼:a1b8)),osg3.0.1(http://yunpan.cn/cQfhzCHSWTmEg (提取碼:ff67))
將3rdparty解壓放到osg目錄,在osg目錄中新建兩個文件夾,一個是生成目錄build、一個是生成庫存放的目錄bin

8、開始重頭戲,osg編譯(osg3.0.1版本)
(1)打開cygwin終端
設置SDK、NDK路徑
export ANDROID_SDK=路徑
export ANDROID_NDK=路徑

(2)cd 到build目錄

(3)cmake,我選擇編譯GLES2.0版本,
輸入命令cmake .. -DOSG_BUILD_PLATFORM_ANDROID=ON -DDYNAMIC_OPENTHREADS=OFF -DDYNAMIC_OPENSCENEGRAPH=OFF -DOSG_GL_DISPLAYLISTS_AVAILABLE=OFF -DOSG_GL_MATRICES_AVAILABLE=OFF -DOSG_GL_VERTEX_FUNCS_AVAILABLE=OFF -DOSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE=OFF -DOSG_GL_FIXED_FUNCTION_AVAILABLE=OFF -DOSG_CPP_EXCEPTIONS_AVAILABLE=OFF -DOSG_GL1_AVAILABLE=OFF -DOSG_GL2_AVAILABLE=OFF -DOSG_GL3_AVAILABLE=OFF -DOSG_GLES1_AVAILABLE=OFF -DOSG_GLES2_AVAILABLE=ON -DJ=4 -DANDROID_STL="gnustl_static" -DCMAKE_INSTALL_PREFIX=生成庫放置目錄

(4)cmake配置完畢,開始編譯,輸入make命令

(5)等1個多小時后,編譯完畢,輸入make install命令,將會把生成的庫放到設置的bin目錄下,編譯完畢

9、osg android Demo展示,將osg例子程序osgAndroidExamplesGLES2.0導入Eclipse中。
(1)問題1,Eclipse出現6個類似The method colorChange(int) of type osgViewer must override a superclass method osgViewer.java錯誤,這是java1.7的問題,去掉前面的@Override

(2)配置ndk編譯c/c++源碼
- 右擊項目點擊Properties,彈出Properities對話框,點擊Builders—》點擊New...—》點擊Program

-losgdb_tiff \
-losgdb_png \
-losgdb_jpeg
//"precision mediump float; \n"
//"precision mediump vec4; \n"
"varying vec4 color; \n"
"const vec3 lightPos =vec3(0.0, 0.0, 10.0); \n"
"varying vec2 texCoord;\n"
"varying vec4 vertexColor;\n"
"const vec4 cessnaColor =vec4(0.8, 0.8, 0.8, 1.0); \n"
"const vec4 lightAmbient =vec4(0.1, 0.1, 0.1, 1.0); \n"
"const vec4 lightDiffuse =vec4(0.4, 0.4, 0.4, 1.0); \n"
"const vec4 lightSpecular =vec4(0.8, 0.8, 0.8, 1.0); \n"
"void DirectionalLight(in vec3 normal, \n"
" in vec3 ecPos, \n"
" inout vec4 ambient, \n"
" inout vec4 diffuse, \n"
" inout vec4 specular) \n"
"{ \n"
" float nDotVP; \n"
" vec3 L = normalize(gl_ModelViewMatrix*vec4(lightPos, 0.0)).xyz; \n"
" nDotVP = max(0.0, dot(normal, L)); \n"
" \n"
" if (nDotVP > 0.0) { \n"
" vec3 E = normalize(-ecPos); \n"
" vec3 R = normalize(reflect( L, normal )); \n"
" specular = pow(max(dot(R, E), 0.0), 16.0) * lightSpecular; \n"
" } \n"
" ambient = lightAmbient; \n"
" diffuse = lightDiffuse * nDotVP; \n"
"} \n"
"void main() { \n"
" vec4 ambiCol = vec4(0.0); \n"
" vec4 diffCol = vec4(0.0); \n"
" vec4 specCol = vec4(0.0); \n"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
" vec3 normal = normalize(gl_NormalMatrix * gl_Normal); \n"
" vec4 ecPos = gl_ModelViewMatrix * gl_Vertex; \n"
" DirectionalLight(normal, ecPos.xyz, ambiCol, diffCol, specCol); \n"
" color = cessnaColor * (ambiCol + diffCol + specCol); \n"
" texCoord = gl_MultiTexCoord0.xy;\n"
" vertexColor=gl_Color;\n"
"} \n";
static const char gFragmentShaderSimple[] =
"precision mediump float; \n"
"varying mediump vec4 color; \n"
"varying vec2 texCoord;\n"
"varying vec4 vertexColor;\n"
//"uniform vec3 mycolor;\n"
"uniform sampler2D mytexture;\n"
"void main() { \n"
//"vec3 acolor;\n"
//"acolor=vec3(texture2D(1,texCoord));\n"
//" gl_FragColor = vec4(mycolor,1.0); \n"
" gl_FragColor = texture2D(mytexture,texCoord); \n"
//" gl_FragColor = vertexColor; \n"
"} ";
osg::Shader * fshader = new osg::Shader(osg::Shader::FRAGMENT, gFragmentShaderSimple );
osg::Program * prog = new osg::Program;
prog->addShader ( vshader );
prog->addShader ( fshader );
osg::ref_ptr<osg::Uniform> mytexture = new osg::Uniform("mytexture",0);
_root->getOrCreateStateSet()->addUniform(mytexture);

