Android OpenGL ES 开发教程(7):创建实例应用OpenGLDemos程序框架
来源:互联网 发布:苹果手机使用软件 编辑:程序博客网 时间:2024/05/17 22:42
有了前面关于Android OpenGL ES的介绍,可以开始创建示例程序OpenGLDemos。
使用Eclipse 创建一个Android项目
- Project Name: OpenGLDemos
- Build Target: Android 1.6 ( >1.5 即可)
- Application Name: Android OpenGL ES Demos
- Package Name: com.pstreets.opengl.demo
- Create Activity:AndroidOpenGLDemo
采用Android ApiDemos 类似的方法,AndroidOpenGLDemo为一ListActivity ,可以使用PackageManager 读取所有Category为guidebee.intent.category.opengl.SAMPLE_CODE 的Activity。Android ApiDemos示例解析(2): SimpleAdapter,ListActivity,PackageManager
创建一个OpenGLRenderer 实现 GLSurfaceView.Renderer接口:
- public class OpenGLRenderer implements Renderer {
- private final IOpenGLDemo openGLDemo;
- public OpenGLRenderer(IOpenGLDemo demo){
- openGLDemo=demo;
- }
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- // Set the background color to black ( rgba ).
- gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
- // Enable Smooth Shading, default not really needed.
- gl.glShadeModel(GL10.GL_SMOOTH);
- // Depth buffer setup.
- gl.glClearDepthf(1.0f);
- // Enables depth testing.
- gl.glEnable(GL10.GL_DEPTH_TEST);
- // The type of depth testing to do.
- gl.glDepthFunc(GL10.GL_LEQUAL);
- // Really nice perspective calculations.
- gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
- GL10.GL_NICEST);
- }
- public void onDrawFrame(GL10 gl) {
- if(openGLDemo!=null){
- openGLDemo.DrawScene(gl);
- }
- }
- public void onSurfaceChanged(GL10 gl, int width, int height) {
- // Sets the current view port to the new size.
- gl.glViewport(0, 0, width, height);
- // Select the projection matrix
- gl.glMatrixMode(GL10.GL_PROJECTION);
- // Reset the projection matrix
- gl.glLoadIdentity();
- // Calculate the aspect ratio of the window
- GLU.gluPerspective(gl, 45.0f,
- (float) width / (float) height,
- 0.1f, 100.0f);
- // Select the modelview matrix
- gl.glMatrixMode(GL10.GL_MODELVIEW);
- // Reset the modelview matrix
- gl.glLoadIdentity();
- }
- }
DrawScene 用于实际的GL绘图示例代码,其它的初始化工作基本就由GLSurfaceView 和OpenGLRenderer 完成,其中onSurfaceCreated 和 onSurfaceChanged 中的代码含义现在无需了解,后面会有具体介绍,只要知道它们是用来初始化GLSurfaceView就可以了。
最后使用一个简单的例子“Hello World”结束本篇,“Hello World” 使用红色背景刷新屏幕。
- public class HelloWorld extends Activity
- implements IOpenGLDemo{
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow()
- .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- mGLSurfaceView = new GLSurfaceView(this);
- mGLSurfaceView.setRenderer(new OpenGLRenderer(this));
- setContentView(mGLSurfaceView);
- }
- public void DrawScene(GL10 gl) {
- gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
- // Clears the screen and depth buffer.
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT
- | GL10.GL_DEPTH_BUFFER_BIT);
- }
- @Override
- protected void onResume() {
- // Ideally a game should implement
- // onResume() and onPause()
- // to take appropriate action when the
- //activity looses focus
- super.onResume();
- mGLSurfaceView.onResume();
- }
- @Override
- protected void onPause() {
- // Ideally a game should implement onResume()
- //and onPause()
- // to take appropriate action when the
- //activity looses focus
- super.onPause();
- mGLSurfaceView.onPause();
- }
- private GLSurfaceView mGLSurfaceView;
- }
其对应在AndroidManifest.xml中的定义如下:
<activity android:name=”.HelloWorld” android:label=”@string/activity_helloworld”>
<intent-filter>
< action android:name=”android.intent.action.MAIN” />
<category android:name=”guidebee.intent.category.opengl.SAMPLE_CODE” />
< /intent-filter>
< /activity>
- Android OpenGL ES 开发教程(7):创建实例应用OpenGLDemos程序框架
- Android OpenGL ES 开发教程(7):创建实例应用OpenGLDemos程序框架
- Android OpenGL ES 开发教程(7):创建实例应用OpenGLDemos程序框架
- Android OpenGL ES 开发教程(7):创建实例应用OpenGLDemos程序框架
- Android OpenGL ES(六):创建实例应用OpenGLDemos程序框架
- Android OpenGL ES 开发教程(2):关于OpenGL ES
- Android OpenGL ES 开发教程(3):OpenGL ES管道(Pipeline)
- Android OpenGL ES 开发教程(2):关于OpenGL ES
- Android OpenGL ES 开发教程(3):OpenGL ES管道(Pipeline)
- Android OpenGL ES 开发教程(2):关于OpenGL ES
- Android OpenGL ES 开发教程(3):OpenGL ES管道(Pipeline)
- Android开发之OpenGL+ES教程
- Android OpenGL ES 简明开发教程
- Android OpenGL ES 简明开发教程
- Android开发之OpenGL+ES教程
- Android OpenGL ES 简明开发教程
- Android OpenGL ES 开发教程(1):导言
- Android OpenGL ES 开发教程(6):GLSurfaceView
- android调用天气预报的webservice例子
- 让android日志更有用些
- 内核驱动小记(一)——Realtek 8168
- 高级字符驱动学习--阻塞型I/0
- Thrust快速入门教程(三)——迭代器与静态调度
- Android OpenGL ES 开发教程(7):创建实例应用OpenGLDemos程序框架
- linux c使用openssl/md5.h加密
- [问题分析]IIMS 系统开发中遇到的问题
- Android OpenGL ES 开发教程(8):基本几何图形定义
- 中语言特性介绍之前言
- 安全令牌
- javascript复习之--3.javascrpt变量和数据类型(上)
- Linux下如何用C实现MD5加密
- 中语言超酷特性 之 --- 线内汇编