`
kerlubasola
  • 浏览: 678724 次
文章分类
社区版块
存档分类
最新评论

Android OpenGL学习笔记

 
阅读更多

大家好,今天我讲一下Android OpenGL,这个系列是我的学习笔记,希望对大家有所帮助!这一节将给大家简洁的介绍一下术语,以及第一个Android OpenGL程序.

首先让我看们看一下术语:

Vertex (顶点)
A vertex is a point in 3D space and is the building block for many objects. In OpenGL you can specify as few as two coordinates (X,Y) and as many as four (X,Y,Z,W). The w-axis is optional, the default value is set to 1.0. The z-axis is also optional, the default value is set to 0. In this series, we will use the three main coordinates X, Y, and Z, since the W is generally used as a placeholder. The plural of vertex is vertices (mainly important for non native speakers, because it may create confusion). All objects are drawn using vertices as their points, so a point will refer to a vertex.

Triangle (三角)
A triangle requires three points to be created. So in OpenGL, we use three vertices to create one.

Polygon (多边形)
A polygon is an object which has at least three connected points. Therefor a triangle is also a polygon.

Primitives (基本实体)
A primitive is a three-dimensional object created using either triangles or polygons. A bit ironic: A detailed model with 50.000 vertices is also a primitive like a low detailed model with 500 vertices.

下面就是我们的第一个Android OpenGL程序:

  1. package com.android.tutor;
  2. import android.app.Activity;
  3. import android.opengl.GLSurfaceView;
  4. import android.os.Bundle;
  5. public class OpenGl_Lesson1 extends Activity{
  6. public void onCreate(BundlesavedInstanceState){
  7. super .onCreate(savedInstanceState);
  8. GLSurfaceViewmGlSurfaceView=new GLSurfaceView( this );
  9. mGlSurfaceView.setRenderer(new OpenGLRender());
  10. setContentView(mGlSurfaceView);
  11. }
  12. }

我们这里的View是用的GLSurfaceView,但是它要setRenderer()一下,就像我们Activity里面的setContentView()方法一样!

这里的OpenGLRender是我重新写的类,它继承于GLSurfaceView.Renderer,我们要实现其种的三个方法:

onSurfaceCreated(),onSurfaceChanged(),onDrawFrame()。代码如下:

  1. package com.android.tutor;
  2. import javax.microedition.khronos.egl.EGLConfig;
  3. import javax.microedition.khronos.opengles.GL10;
  4. import android.opengl.GLSurfaceView.Renderer;
  5. public class OpenGLRender implements Renderer{
  6. @Override
  7. public void onSurfaceCreated(GL10gl,EGLConfigconfig){
  8. //TODOAuto-generatedmethodstub
  9. gl.glClearColor(0 .9f, 0 .2f, 0 .2f, 1 .0f);
  10. gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  11. }
  12. @Override
  13. public void onSurfaceChanged(GL10gl, int width, int height){
  14. gl.glViewport(0 , 0 ,width,height);
  15. }
  16. @Override
  17. public void onDrawFrame(GL10gl){
  18. //TODOAuto-generatedmethodstub
  19. }
  20. }

第10行就是相当于我们设置画布颜色RGBA(红绿蓝透),第11行这里是清除深度颜色缓存,不加上一句,第10句不起作用,整个View的颜色

还是黑黑的!

运行效果如下:

前面讲了一些Android OpenGl 的概念问题,这一节讲给大家讲一下如何画一个三角现出来.

一、新建一个Android工程命名为:OpenGL_Lesson2.

二、修改OpenGL_Lesson2.java代码如下:

  1. package com.android.tutor;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class OpenGL_Lesson2 extends Activity{
  5. private OpenGlViewmOpenGlView;
  6. public void onCreate(BundlesavedInstanceState){
  7. super .onCreate(savedInstanceState);
  8. mOpenGlView=new OpenGlView( this );
  9. setContentView(mOpenGlView);
  10. }
  11. }

三、新建一个类OpenGlView.java继承与GLSurfaceView,代码如下:

  1. package com.android.tutor;
  2. import android.content.Context;
  3. import android.opengl.GLSurfaceView;
  4. public class OpenGlView extends GLSurfaceView{
  5. private OpenGlRendermOpenGlRender;
  6. public OpenGlView(Contextcontext){
  7. super (context);
  8. mOpenGlRender=new OpenGlRender();
  9. setRenderer(mOpenGlRender);
  10. }
  11. }

四、新建一个OpenGlRender.java类继承与Renderer,代码如下:

  1. package com.android.tutor;
  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.nio.FloatBuffer;
  5. import java.nio.ShortBuffer;
  6. import javax.microedition.khronos.egl.EGLConfig;
  7. import javax.microedition.khronos.opengles.GL10;
  8. import android.opengl.GLSurfaceView.Renderer;
  9. public class OpenGlRender implements Renderer{
  10. private float _red=0f;
  11. private float _green=25f;
  12. private float _blue=200f;
  13. private ShortBuffer_indexBuffer;
  14. private FloatBuffer_vertexBuffer;
  15. private short []_indicesArray={ 0 , 1 , 2 };
  16. private int _nrOfVertices= 3 ;
  17. @Override
  18. public void onSurfaceCreated(GL10gl,EGLConfigconfig){
  19. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  20. initTriangle();
  21. }
  22. @Override
  23. public void onSurfaceChanged(GL10gl, int width, int height){
  24. gl.glViewport(0 , 0 ,width,height);
  25. }
  26. @Override
  27. public void onDrawFrame(GL10gl){
  28. gl.glClearColor(_red,_green,_blue,1 .0f);
  29. gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  30. gl.glColor4f(0 .5f,0f,0f, 0 .5f);
  31. gl.glVertexPointer(_nrOfVertices,GL10.GL_FLOAT,0 ,_vertexBuffer);
  32. gl.glDrawElements(GL10.GL_TRIANGLES,_nrOfVertices,GL10.GL_UNSIGNED_SHORT,_indexBuffer);
  33. }
  34. private void initTriangle(){
  35. //floathas4bytes
  36. ByteBuffervbb=ByteBuffer.allocateDirect(_nrOfVertices*3 * 4 );
  37. vbb.order(ByteOrder.nativeOrder());
  38. _vertexBuffer=vbb.asFloatBuffer();
  39. //shorthas4bytes
  40. ByteBufferibb=ByteBuffer.allocateDirect(_nrOfVertices*2 );
  41. ibb.order(ByteOrder.nativeOrder());
  42. _indexBuffer=ibb.asShortBuffer();
  43. float []coords={
  44. -0 .5f,- 0 .5f,0f, //(x1,y1,z1)
  45. 0 .5f,- 0 .5f,0f, //(x2,y2,z2)
  46. 0f,0 .5f,0f //(x3,y3,z3)
  47. };
  48. _vertexBuffer.put(coords);
  49. _indexBuffer.put(_indicesArray);
  50. _vertexBuffer.position(0 );
  51. _indexBuffer.position(0 );
  52. }
  53. }

五、运行之,效果如下:

今天晚上就先不做详细解释了!改日在加上!谢谢~

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics