Chinaunix首页 | 论坛 | 博客
  • 博客访问: 175992
  • 博文数量: 38
  • 博客积分: 638
  • 博客等级: 下士
  • 技术积分: 395
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-07 17:16
文章分类

全部博文(38)

文章存档

2013年(1)

2012年(5)

2011年(32)

分类: 嵌入式

2011-12-16 20:32:05

写出一个OpenGL ES应用的通用框架。

创建一个新的Android 项目:OpenGLESTutorial, 在项目在添加两个类TutorialPartI.java 和OpenGLRenderer.java.

具体代码如下:

TutorialPartI.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class TutorialPartI extends Activity {
 // Called when the activity is first created.
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 this.requestWindowFeature(Window.FEATURE_NO_TITLE); // (NEW)
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 WindowManager.LayoutParams.FLAG_FULLSCREEN); // (NEW)
 
 GLSurfaceView view = new GLSurfaceView(this);
 view.setRenderer(new OpenGLRenderer());
 setContentView(view);
 }
}

OpenGLRenderer.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
30
31
32
33
34
35
36
37
38
39
40
41
public class OpenGLRenderer implements Renderer {
 
 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
 // Set the background color to black ( rgba ).
 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // OpenGL docs.
 // Enable Smooth Shading, default not really needed.
 gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs.
 // Depth buffer setup.
 gl.glClearDepthf(1.0f);// OpenGL docs.
 // Enables depth testing.
 gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs.
 // The type of depth testing to do.
 gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs.
 // Really nice perspective calculations.
 gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs.
 GL10.GL_NICEST);
 }
 
 public void onDrawFrame(GL10 gl) {
 // Clears the screen and depth buffer.
 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs.
 GL10.GL_DEPTH_BUFFER_BIT);
 }
 
 public void onSurfaceChanged(GL10 gl, int width, int height) {
 // Sets the current view port to the new size.
 gl.glViewport(0, 0, width, height);// OpenGL docs.
 // Select the projection matrix
 gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs.
 // Reset the projection matrix
 gl.glLoadIdentity();// OpenGL docs.
 // 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);// OpenGL docs.
 // Reset the modelview matrix
 gl.glLoadIdentity();// OpenGL docs.
 }
}

编译后运行,屏幕显示一个黑色的全屏。这两个类定义了Android OpenGL ES应用的最基本的类和方法,可以看作是OpenGL ES的”Hello ,world”应用,后面将逐渐丰富这个例子来画出3D图型

阅读(1770) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~