1. 首先需要一个OSG for android的环境
(1)NDK 现在Eclipse 对NDK已经相当友好了,已经不需要另外cygwin的参与,具体可以参考
Android NDK开发篇(一):新版NDK环境搭建(免Cygwin,超级快)
(2).OSG for android的编译,参考 osg for android学习之一:windows下编译(亲测通过) 建议编译OpenGL ES2版本.
2.然后加载OSG自带的Example:osgAndroidExampleGLES2
(1)点击菜单键加载文件路径,输入/sdcard/osg/cow.osg(必须先往sdcard创建文件夹osg并把cow.osg放到该文件夹里边)
(2)接着经典的牛出现了:)
3.自带的example太多的代码,这样的代码无论对于NDK的初学者或OSG很不直观,所以本人重写了一个HelloWolrd for
osg的例子。例子很简单,就是简单加载一个四边形并附上颜色。
(1)java代码
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.example.helloosg;
import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView.Renderer; public class NDKRenderer implements Renderer{ NDKRenderer(){ @Override @Override |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.example.helloosg;
public class osgNativeLib { static { System.loadLibrary("osgNativeLib"); } /** |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.example.helloosg;
import android.app.Activity; public class MainActivity extends Activity { private GLSurfaceView mGLSurfaceView; @Override @Override @Override @Override |
(2)JNI代码
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
/ * Created on: 2014-10-19
* Author: VCC */ #include "OsgMainApp.h" OsgMainApp::OsgMainApp() { void OsgMainApp::initOsgWindow(int x, int y, int width, int height) { _viewer = new osgViewer::Viewer(); _root = new osg::Group(); _viewer->realize(); _manipulator = new osgGA::KeySwitchMatrixManipulator; _viewer->getViewerStats()->collectStats("scene", true); loadModels(); void OsgMainApp::draw() { void OsgMainApp::loadModels() { osg::Program * prog = new osg::Program; osg::ref_ptr<osg::Node> node = createNode(); _viewer->setSceneData(NULL); _viewer->getDatabasePager()->clear(); //创建一个四边形节点 //创建一个叶节点对象 //设置顶点数据 //创建纹理订点数据 //设置纹理坐标 //创建颜色数组 //设置颜色数组 //创建法线数组 //添加到叶子节点 return geode.get(); |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
/*
* OsgMainApp.h * * Created on: 2014-10-19 * Author: VCC */ #ifndef OSGMAINAPP_H_ //android log #include <string> //osg #include <osgText/Text> //osgDB //osg_viewer #include <osgGA/GUIEventAdapter> //Static plugins Macro #define LOG_TAG "osgNativeLib" static const char gVertexShader[] = class OsgMainApp { }; #endif /* OSGMAINAPP_H_ */ |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <jni.h>
#include <string.h> #include <osg/Node> #include <iostream> #include "OsgMainApp.h" OsgMainApp mainApp; extern "C"{ JNIEXPORT void JNICALL Java_com_example_helloosg_osgNativeLib_step(JNIEnv* env, jobject obj){ |
(3).mk文件
android.mk
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS) LOCAL_MODULE := osgNativeLib ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) ### Add all source file names to be included in lib separated by a whitespace LOCAL_C_INCLUDES:= $(OSG_ANDROID_DIR)/include LOCAL_LDLIBS := -lGLESv2 -lz -llog LOCAL_SRC_FILES := osgNativeLib.cpp OsgMainApp.cpp LOCAL_LDFLAGS := -L $(LIBDIR) \ include $(BUILD_SHARED_LIBRARY) |
application.mk
1
2 3 4 5 6 7 8 9 10 11 |
#ANDROID APPLICATION MAKEFILE
APP_BUILD_SCRIPT := $(call my-dir)/Android.mk #APP_PROJECT_PATH := $(call my-dir) APP_OPTIM := release APP_PLATFORM := android-8 |
运行结果
注:由于代码是基于OpenGL ES2,简单加载了一个四边形,并在片元着色器将四边形的颜色赋为浅蓝色,其实也可以通过
OSG将四边形的颜色或者纹理赋到四边形上,具体下篇将说明
https://blog.csdn.net/hai7song/article/details/40515465
参考:
https://blog.csdn.net/edgarliaohs/article/details/37877287
http://www.openscenegraph.com/index.php/documentation/platform-specifics/android/43-building-openscenegraph-for-android-3-0-2