- 1. 首先需要一个OSG for android的环境.
- (1).NDK 现在Eclipse 对NDK已经相当友好了,已经不需要另外cygwin的参与,具体可以参考
- (2).osg for android的编译,参考 建议编译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 代码:
package com.example.helloosg;import android.app.Activity;import android.opengl.GLSurfaceView;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;public class MainActivity extends Activity { private GLSurfaceView mGLSurfaceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLSurfaceView = new GLSurfaceView(this); mGLSurfaceView.setEGLContextClientVersion(2); NDKRenderer renderer = new NDKRenderer(); this.mGLSurfaceView.setRenderer(renderer); this.setContentView(mGLSurfaceView); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); mGLSurfaceView.onResume(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); mGLSurfaceView.onPause(); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}