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(Bundle savedInstanceState) {   
  7.          super .onCreate(savedInstanceState);   
  8.            
  9.         GLSurfaceView mGlSurfaceView =  new  GLSurfaceView( this );   
  10.            
  11.           
  12.         mGlSurfaceView.setRenderer( new  OpenGLRender());   
  13.         setContentView(mGlSurfaceView);   
  14.            
  15.     }   
  16. }  
  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(Bundle savedInstanceState) {  
  7.         super .onCreate(savedInstanceState);  
  8.           
  9.         GLSurfaceView mGlSurfaceView = new  GLSurfaceView( this );  
  10.           
  11.          
  12.         mGlSurfaceView.setRenderer(new  OpenGLRender());  
  13.         setContentView(mGlSurfaceView);  
  14.           
  15.     }  
  16. }  

我们这里的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(GL10 gl, EGLConfig config) {   
  8.          // TODO Auto-generated method stub   
  9.            
  10.         gl.glClearColor( 0 .9f,  0 .2f,  0 .2f,  1 .0f);   
  11.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);   
  12.     }   
  13.      @Override   
  14.      public   void  onSurfaceChanged(GL10 gl,  int  width,  int  height) {   
  15.         gl.glViewport( 0 0 , width, height);   
  16.     }   
  17.      @Override   
  18.      public   void  onDrawFrame(GL10 gl) {   
  19.          // TODO Auto-generated method stub   
  20.     }   
  21.        
  22. }  
  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(GL10 gl, EGLConfig config) {  
  8.         // TODO Auto-generated method stub   
  9.           
  10.         gl.glClearColor(0 .9f,  0 .2f,  0 .2f,  1 .0f);  
  11.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
  12.     }  
  13.     @Override   
  14.     public   void  onSurfaceChanged(GL10 gl,  int  width,  int  height) {  
  15.         gl.glViewport(0 0 , width, height);  
  16.     }  
  17.     @Override   
  18.     public   void  onDrawFrame(GL10 gl) {  
  19.         // TODO Auto-generated method stub   
  20.     }  
  21.       
  22. }  

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

还是黑黑的!

运行效果如下:

 

你可能感兴趣的:(android,OS)