原文地址: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);