GLSurfaceView实现Camera预览--glsurfaceview和onFrameAvailable的关系

215 篇文章 4 订阅

http://www.codezyw.com/archives/1402

 

前面有一篇探讨了如何在片段着色器中将YUV数据转换为RGB数据并显示,但采用samplerExternalOES将SurfaceTexture作为OpenGL外部纹理,需要使用GL_TEXTURE_EXTERNAL_OES作为纹理模板,通过SetPreviewTexture将Camera数据图像输出到SurfaceTexture,调用updateTexImage()时将对应纹理更新为最新的一帧,然后通知OpenGL绘制对应纹理。

1.创建纹理ID

[plain] view plain copy

 

  1. int[] texture = new int[1];
  2. GLES20.glGenTextures(1, texture, 0);
  3. GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]);
  4. GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
  5.             GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);
  6. GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
  7.             GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  8. GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
  9.             GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
  10. GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
  11.             GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
  12. return texture[0];

2.根据纹理id创建SurfaceTexture

[plain] view plain copy

 

  1. mSurfaceTexture = new SurfaceTexture(textureId);
  2. mSurfaceTexture.setOnFrameAvailableListener(listener);
  3. private OnFrameAvailableListener listener = new OnFrameAvailableListener() {
  4.  
  5.     @Override
  6.     public void onFrameAvailable(SurfaceTexture surfaceTexture) {
  7.         // TODO Auto-generated method stub
  8.         mGlSurfaceView.requestRender();
  9.     }
  10. };

3.更改Shader

顶点着色器与之前相比多了个textureTransform,用于接收SurfaceTexture变换矩阵,如果不需要也可以不盖被vertext shader。

片段着色器需要增加#extension GL_OES_EGL_image_external : require,因为第一步的纹理都绑定到GL_TEXTURE_EXTERNAL_OES

对应采样方式由Samlpe2D更改成samplerExternalOES。

[plain] view plain copy

 

  1. public static final String NO_FILTER_VERTEX_SHADER = “” +
  2.             “attribute vec4 position;\n” +
  3.            ” attribute vec4 inputTextureCoordinate;\n” +
  4.            ” \n” +
  5.             “uniform mat4 textureTransform;\n” +
  6.             “varying vec2 textureCoordinate;\n” +
  7.             ” \n” +
  8.            ” void main()\n” +
  9.             “{\n” +
  10.             “textureCoordinate = (textureTransform * inputTextureCoordinate).xy;\n” +
  11.             “gl_Position = position;\n” +
  12.             “}”;
  13.     public static final String NO_FILTER_FRAGMENT_SHADER = “” +
  14.             “#extension GL_OES_EGL_image_external : require\n”+
  15.             “precision mediump float;” +
  16.             “varying vec2 textureCoordinate;\n” +
  17.             “uniform samplerExternalOES inputImageTexture;\n” +
  18.             ” \n” +
  19.             “void main() {” +
  20.             ”  gl_FragColor = texture2D( inputImageTexture, textureCoordinate );\n” +
  21.             “}”;

4.绘制

与之前采用glTexImage2D绑定2D图像纹理不同,这里通过GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureid);绑定外部纹理即可。

如果使用了SurfaceTexture变换矩阵,可采用glUniformMatrix4fv传递给Shader

采用片段着色器方案的时间消耗主要在数据转移中,本方法的格式相关工作交给EGLImage处理,用到了Lock ANativeWindow。具体优劣尚未探讨,如有经验人士希望指点迷津。

 

 

 

 

 

GLSurfaceView是OpenGL中的一个类,也是可以预览Camera的,而且在预览Camera上有其独到之处。独到之处在哪?当使用Surfaceview无能为力、痛不欲生时就只有使用GLSurfaceView了,它能够真正做到让Camera的数据和显示分离,所以搞明白了这个,像Camera只开预览不显示这都是小菜,妥妥的。Android4.0的自带Camera源码是用SurfaceView预览的,但到了4.2就换成了GLSurfaceView来预览。如今到了4.4又用了自家的TextureView,所以从中可以窥探出新增TextureView的用意。

虽说Android4.2的Camera源码是用GLSurfaceView预览的,但是进行了大量的封装又封装的,由于是OpenGL小白,真是看的不知所云。俺滴要求不高,只想弄个可拍照的摸清GLSurfaceView在预览Camera上的使用流程。经过一番百度一无所获,后来翻出去Google一大圈也没发现可用的。倒是很多人都在用GLSurfaceView和Surfaceview同时预览Camera,Surfaceview用来预览数据,在上面又铺了一层GLSurfaceView绘制一些信息。无奈自己摸索,整出来的是能拍照也能得到数据,但是界面上不是一块白板就是一块黑板啥都不显示。后来在stackoverflow终于找到了一个可用的链接,哈哈,苍天啊,终于柳暗花明了!参考此链接,自己又改改摸索了一天才彻底搞定。之所以费这么多时间是不明白OpenGL ES2.0的绘制基本流程,跟简单的OpenGL的绘制还是稍有区别。下面上源码:

一、CameraGLSurfaceView.java 此类继承GLSurfaceView,并实现了两个接口

[java] view plain copy

 

print?

  1. <span style=“font-family:Comic Sans MS;font-size:18px;”>package org.yanzi.camera.preview;
  2.  
  3. import javax.microedition.khronos.egl.EGLConfig;
  4. import javax.microedition.khronos.opengles.GL10;
  5.  
  6. import org.yanzi.camera.CameraInterface;
  7.  
  8. import android.content.Context;
  9. import android.graphics.SurfaceTexture;
  10. import android.opengl.GLES11Ext;
  11. import android.opengl.GLES20;
  12. import android.opengl.GLSurfaceView;
  13. import android.opengl.GLSurfaceView.Renderer;
  14. import android.util.AttributeSet;
  15. import android.util.Log;
  16.  
  17. public class CameraGLSurfaceView extends GLSurfaceView implements Renderer, SurfaceTexture.OnFrameAvailableListener {
  18.     private static final String TAG = “yanzi”;
  19.     Context mContext;
  20.     SurfaceTexture mSurface;
  21.     int mTextureID = –1;
  22.     DirectDrawer mDirectDrawer;
  23.     public CameraGLSurfaceView(Context context, AttributeSet attrs) {
  24.         super(context, attrs);
  25.         // TODO Auto-generated constructor stub
  26.         mContext = context;
  27.         setEGLContextClientVersion(2);
  28.         setRenderer(this);
  29.         setRenderMode(RENDERMODE_WHEN_DIRTY);
  30.     }
  31.     @Override
  32.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  33.         // TODO Auto-generated method stub
  34.         Log.i(TAG, “onSurfaceCreated…”);
  35.         mTextureID = createTextureID();
  36.         mSurface = new SurfaceTexture(mTextureID);
  37.         mSurface.setOnFrameAvailableListener(this);
  38.         mDirectDrawer = new DirectDrawer(mTextureID);
  39.         CameraInterface.getInstance().doOpenCamera(null);
  40.  
  41.     }
  42.     @Override
  43.     public void onSurfaceChanged(GL10 gl, int width, int height) {
  44.         // TODO Auto-generated method stub
  45.         Log.i(TAG, “onSurfaceChanged…”);
  46.         GLES20.glViewport(0, 0, width, height);
  47.         if(!CameraInterface.getInstance().isPreviewing()){
  48.             CameraInterface.getInstance().doStartPreview(mSurface, 1.33f);
  49.         }
  50.  
  51.  
  52.     }
  53.     @Override
  54.     public void onDrawFrame(GL10 gl) {
  55.         // TODO Auto-generated method stub
  56.         Log.i(TAG, “onDrawFrame…”);
  57.         GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  58.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  59.         mSurface.updateTexImage();
  60.         float[] mtx = new float[16];
  61.         mSurface.getTransformMatrix(mtx);
  62.         mDirectDrawer.draw(mtx);
  63.     }
  64.  
  65.     @Override
  66.     public void onPause() {
  67.         // TODO Auto-generated method stub
  68.         super.onPause();
  69.         CameraInterface.getInstance().doStopCamera();
  70.     }
  71.     private int createTextureID()
  72.     {
  73.         int[] texture = new int[1];
  74.  
  75.         GLES20.glGenTextures(1, texture, 0);
  76.         GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]);
  77.         GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
  78.                 GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);
  79.         GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
  80.                 GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  81.         GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
  82.                 GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
  83.         GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
  84.                 GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
  85.  
  86.         return texture[0];
  87.     }
  88.     public SurfaceTexture _getSurfaceTexture(){
  89.         return mSurface;
  90.     }
  91.     @Override
  92.     public void onFrameAvailable(SurfaceTexture surfaceTexture) {
  93.         // TODO Auto-generated method stub
  94.         Log.i(TAG, “onFrameAvailable…”);
  95.         this.requestRender();
  96.     }
  97.  
  98. }
  99. </span>

关于这个类进行简单说明:

1、Renderer这个接口里有三个回调: onSurfaceCreated() onSurfaceChanged() onDrawFrame(),在onSurfaceCreated里设置了GLSurfaceView的版本: setEGLContextClientVersion(2); 如果没这个设置是啥都画不出来了,因为Android支持OpenGL ES1.1和2.0及最新的3.0,而且版本间差别很大。不告诉他版本他不知道用哪个版本的api渲染。在设置setRenderer(this);后,再设置它的模式为RENDERMODE_WHEN_DIRTY。这个也很关键,看api:

When renderMode is RENDERMODE_CONTINUOUSLY, the renderer is called repeatedly to re-render the scene. When renderMode is RENDERMODE_WHEN_DIRTY, the renderer only rendered when the surface is created, or when requestRender is called. Defaults to RENDERMODE_CONTINUOUSLY.

Using RENDERMODE_WHEN_DIRTY can improve battery life and overall system performance by allowing the GPU and CPU to idle when the view does not need to be updated.

大意是RENDERMODE_CONTINUOUSLY模式就会一直Render,如果设置成RENDERMODE_WHEN_DIRTY,就是当有数据时才rendered或者主动调用了GLSurfaceView的requestRender.默认是连续模式,很显然Camera适合脏模式,一秒30帧,当有数据来时再渲染。

2、正因是RENDERMODE_WHEN_DIRTY所以就要告诉GLSurfaceView什么时候Render,也就是啥时候进到onDrawFrame()这个函数里。SurfaceTexture.OnFrameAvailableListener这个接口就干了这么一件事,当有数据上来后会进到

public void onFrameAvailable(SurfaceTexture surfaceTexture) {
// TODO Auto-generated method stub
Log.i(TAG, “onFrameAvailable…”);
this.requestRender();
}

这里,然后执行requestRender()。

3、网上有一些OpenGL ES的示例是在Activity里实现了SurfaceTexture.OnFrameAvailableListener此接口,其实这个无所谓。无论是被谁实现,关键看在回调里干了什么事。

4、与TextureView里对比可知,TextureView预览时因为实现了SurfaceTextureListener会自动创建SurfaceTexture。但在GLSurfaceView里则要手动创建同时绑定一个纹理ID。

5、本文在onSurfaceCreated()里打开Camera,在onSurfaceChanged()里开启预览,默认1.33的比例。原因是相比前两种预览,此处SurfaceTexture创建需要一定时间。如果想要开预览时由Activity发起,则要GLSurfaceView利用Handler将创建的SurfaceTexture传递给Activity。

二、DirectDrawer.java 此类非常关键,负责将SurfaceTexture内容绘制到屏幕上

[java] view plain copy

 

print?

  1. <span style=“font-family:Comic Sans MS;font-size:18px;”>package org.yanzi.camera.preview;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.nio.FloatBuffer;
  6. import java.nio.ShortBuffer;
  7.  
  8. import android.opengl.GLES11Ext;
  9. import android.opengl.GLES20;
  10. import android.opengl.Matrix;
  11.  
  12. public class DirectDrawer {
  13.     private final String vertexShaderCode =
  14.             “attribute vec4 vPosition;” +
  15.             “attribute vec2 inputTextureCoordinate;” +
  16.             “varying vec2 textureCoordinate;” +
  17.             “void main()” +
  18.             “{“+
  19.                 “gl_Position = vPosition;”+
  20.                 “textureCoordinate = inputTextureCoordinate;” +
  21.             “}”;
  22.  
  23.     private final String fragmentShaderCode =
  24.             “#extension GL_OES_EGL_image_external : require\n”+
  25.             “precision mediump float;” +
  26.             “varying vec2 textureCoordinate;\n” +
  27.             “uniform samplerExternalOES s_texture;\n” +
  28.             “void main() {“ +
  29.             ”  gl_FragColor = texture2D( s_texture, textureCoordinate );\n” +
  30.             “}”;
  31.  
  32.     private FloatBuffer vertexBuffer, textureVerticesBuffer;
  33.     private ShortBuffer drawListBuffer;
  34.     private final int mProgram;
  35.     private int mPositionHandle;
  36.     private int mTextureCoordHandle;
  37.  
  38.     private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices
  39.  
  40.     // number of coordinates per vertex in this array
  41.     private static final int COORDS_PER_VERTEX = 2;
  42.  
  43.     private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
  44.  
  45.     static float squareCoords[] = {
  46.        –1.0f,  1.0f,
  47.        –1.0f, –1.0f,
  48.         1.0f, –1.0f,
  49.         1.0f,  1.0f,
  50.     };
  51.  
  52.     static float textureVertices[] = {
  53.         0.0f, 1.0f,
  54.         1.0f, 1.0f,
  55.         1.0f, 0.0f,
  56.         0.0f, 0.0f,
  57.     };
  58.  
  59.     private int texture;
  60.  
  61.     public DirectDrawer(int texture)
  62.     {
  63.         this.texture = texture;
  64.         // initialize vertex byte buffer for shape coordinates
  65.         ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4);
  66.         bb.order(ByteOrder.nativeOrder());
  67.         vertexBuffer = bb.asFloatBuffer();
  68.         vertexBuffer.put(squareCoords);
  69.         vertexBuffer.position(0);
  70.  
  71.         // initialize byte buffer for the draw list
  72.         ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
  73.         dlb.order(ByteOrder.nativeOrder());
  74.         drawListBuffer = dlb.asShortBuffer();
  75.         drawListBuffer.put(drawOrder);
  76.         drawListBuffer.position(0);
  77.  
  78.         ByteBuffer bb2 = ByteBuffer.allocateDirect(textureVertices.length * 4);
  79.         bb2.order(ByteOrder.nativeOrder());
  80.         textureVerticesBuffer = bb2.asFloatBuffer();
  81.         textureVerticesBuffer.put(textureVertices);
  82.         textureVerticesBuffer.position(0);
  83.  
  84.         int vertexShader    = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
  85.         int fragmentShader  = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
  86.  
  87.         mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
  88.         GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
  89.         GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
  90.         GLES20.glLinkProgram(mProgram);                  // creates OpenGL ES program executables
  91.     }
  92.  
  93.     public void draw(float[] mtx)
  94.     {
  95.         GLES20.glUseProgram(mProgram);
  96.  
  97.         GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  98.         GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture);
  99.  
  100.         // get handle to vertex shader’s vPosition member
  101.         mPositionHandle = GLES20.glGetAttribLocation(mProgram, “vPosition”);
  102.  
  103.         // Enable a handle to the triangle vertices
  104.         GLES20.glEnableVertexAttribArray(mPositionHandle);
  105.  
  106.         // Prepare the <insert shape here> coordinate data
  107.         GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
  108.  
  109.         mTextureCoordHandle = GLES20.glGetAttribLocation(mProgram, “inputTextureCoordinate”);
  110.         GLES20.glEnableVertexAttribArray(mTextureCoordHandle);
  111.  
  112. //        textureVerticesBuffer.clear();
  113. //        textureVerticesBuffer.put( transformTextureCoordinates( textureVertices, mtx ));
  114. //        textureVerticesBuffer.position(0);
  115.         GLES20.glVertexAttribPointer(mTextureCoordHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, textureVerticesBuffer);
  116.  
  117.         GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
  118.  
  119.         // Disable vertex array
  120.         GLES20.glDisableVertexAttribArray(mPositionHandle);
  121.         GLES20.glDisableVertexAttribArray(mTextureCoordHandle);
  122.     }
  123.  
  124.     private  int loadShader(int type, String shaderCode){
  125.  
  126.         // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
  127.         // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
  128.         int shader = GLES20.glCreateShader(type);
  129.  
  130.         // add the source code to the shader and compile it
  131.         GLES20.glShaderSource(shader, shaderCode);
  132.         GLES20.glCompileShader(shader);
  133.  
  134.         return shader;
  135.     }
  136.     private float[] transformTextureCoordinates( float[] coords, float[] matrix)
  137.     {
  138.        float[] result = new float[ coords.length ];
  139.        float[] vt = new float[4];
  140.  
  141.        for ( int i = 0 ; i < coords.length ; i += 2 ) {
  142.            float[] v = { coords[i], coords[i+1], 0 , 1  };
  143.            Matrix.multiplyMV(vt, 0, matrix, 0, v, 0);
  144.            result[i] = vt[0];
  145.            result[i+1] = vt[1];
  146.        }
  147.        return result;
  148.     }
  149. }
  150. </span>

三、有了上面两个类就完成95%的工作,可以将GLSurfaceView看成是有生命周期的。在onPause里进行关闭Camera,在Activity里复写两个方法:

[java] view plain copy

 

print?

  1. <span style=“font-family:Comic Sans MS;font-size:18px;”>    @Override
  2.     protected void onResume() {
  3.         // TODO Auto-generated method stub
  4.         super.onResume();
  5.         glSurfaceView.bringToFront();
  6.     }
  7.  
  8.     @Override
  9.     protected void onPause() {
  10.         // TODO Auto-generated method stub
  11.         super.onPause();
  12.         glSurfaceView.onPause();
  13.     }</span>

这个glSurfaceView.bringToFront();其实不写也中。在布局里写入自定义的GLSurfaceView就ok了:

[html] view plain copy

 

print?

  1. <span style=“font-family:Comic Sans MS;font-size:18px;”>    <FrameLayout
  2.         android:layout_width=“wrap_content”
  3.         android:layout_height=“wrap_content” >
  4.         <org.yanzi.camera.preview.CameraGLSurfaceView
  5.             android:id=“@+id/camera_textureview”
  6.             android:layout_width=“0dip”
  7.             android:layout_height=“0dip” />
  8.     </FrameLayout></span>

CameraActivity里只负责UI部分,CameraGLSurfaceView负责开Camera、预览,并调用DirectDrawer里的draw()进行绘制。其他代码就不上了。

注意事项:

1、在onDrawFrame()里,如果不调用mDirectDrawer.draw(mtx);是啥都显示不出来的!!!这是GLSurfaceView的特别之处。为啥呢?因为GLSurfaceView不是Android亲生的,而Surfaceview和TextureView是。所以得自己按照OpenGL ES的流程画。

2、究竟mDirectDrawer.draw(mtx)里在哪获取的Buffer目前杂家还么看太明白,貌似么有请求buffer,而是根据GLSurfaceView里创建的SurfaceTexture之前,生成的有个纹理ID。这个纹理ID一方面跟SurfaceTexture是绑定在一起的,另一方面跟DirectDrawer绑定,而SurfaceTexture作渲染载体。

3、参考链接里有,有人为了解决问题,给出了下面三段代码:

@Override
public void onDrawFrame(GL10 gl)
{
    float[] mtx = new float[16];
    mSurface.updateTexImage();
    mSurface.getTransformMatrix(mtx);    

    mDirectVideo.draw(mtx);
}
 private float[] transformTextureCoordinates( float[] coords, float[] matrix)
 {          
    float[] result = new float[ coords.length ];        
    float[] vt = new float[4];      

    for ( int i = 0 ; i < coords.length ; i += 2 ) {
        float[] v = { coords[i], coords[i+1], 0 , 1  };
        Matrix.multiplyMV(vt, 0, matrix, 0, v, 0);
        result[i] = vt[0];
        result[i+1] = vt[1];
    }
    return result;
 }
textureVerticesBuffer.clear();
textureVerticesBuffer.put( transformTextureCoordinates( textureVertices, mtx ));
textureVerticesBuffer.position(0);

我已经把代码都融入到了此demo,只不过在draw()方法里么有使用。原因是使用之后,得到的预览画面反而是变形的,而不用的话是ok的。上面的代码是得到SurfaceTexture的变换矩阵:mSurface.getTransformMatrix

然后将此矩阵传递给draw(),在draw的时候对textureVerticesBuffer作一个变化,然后再画。

下图是未加这个矩阵变换效果时:

 

下图为使用了变换矩阵,划片扭曲的还真说不上来咋扭曲的,但足以说明OpenGL ES在渲染效果上的强大,就是设置了个矩阵,不用一帧帧处理,就能得到不一样显示效果。

—————————–本文系原创,转载请注明作者yanzi1225627

版本号:PlayCamera_V3.0.0[2014-6-22].zip

CSDN下载链接:http://download.csdn.net/detail/yanzi1225627/7547263

百度云盘:

附个OpenGL ES简明教程:http://www.apkbus.com/android-20427-1-1.html

 

 

 

 

 

 

一、设置OpenGL ES视图

设置OpenGL视图并不难,Android上也较简单。我们一般只需要2个步骤。

GLSurfaceView

我们要为GLSurfaceView提供一个专门用于渲染的接口

public void  setRenderer(GLSurfaceView.Renderer renderer)

 

GLSurfaceView.Renderer

GLSurfaceView.Renderer是一个通用渲染接口。我们必须实现下面的三个抽象方法:

// 画面创建

public void onSurfaceCreated(GL10 gl, EGLConfig config)

// 画面绘制

public void onDrawFrame(GL10 gl)

// 画面改变

public void onSurfaceChanged(GL10 gl, int width, int height)

onSurfaceCreated

在这里我们主要进行一些初始化工作,比如对透视进行修正、设置清屏所用颜色等。

onDrawFrame

绘制当前画面

onSurfaceChanged

当设备水平或者垂直变化时调用此方法,设置新的显示比例

 

案例代码:

 

  1. public class OpenGLDemo extends Activity {
  2.     @Override
  3.     public void onCreate(Bundle savedInstanceState) {
  4.         GLSurfaceView view = new GLSurfaceView(this);
  5.         view.setRenderer(new OpenGLRenderer());
  6.         setContentView(view);
  7.     }
  8. }

复制代码

实现renderer需要更多的设置

  1.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  2.         // 黑色背景
  3.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  4.         // 启用阴影平滑(不是必须的)
  5.         gl.glShadeModel(GL10.GL_SMOOTH);
  6.         // 设置深度缓存
  7.         gl.glClearDepthf(1.0f);
  8.         // 启用深度测试
  9.         gl.glEnable(GL10.GL_DEPTH_TEST);
  10.         // 所作深度测试的类型
  11.         gl.glDepthFunc(GL10.GL_LEQUAL);
  12.         // 对透视进行修正
  13.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
  14.     }
  15.  
  16.     public void onDrawFrame(GL10 gl) {
  17.         // 清除屏幕和深度缓存
  18.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  19.     }
  20.  
  21.     public void onSurfaceChanged(GL10 gl, int width, int height) {
  22.         // 设置画面的大小
  23.         gl.glViewport(0, 0, width, height);
  24.         // 设置投影矩阵
  25.         gl.glMatrixMode(GL10.GL_PROJECTION);
  26.         // 重置投影矩阵
  27.         gl.glLoadIdentity();
  28.         // 设置画面比例
  29.         GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,100.0f);
  30.         // 选择模型观察矩阵
  31.         gl.glMatrixMode(GL10.GL_MODELVIEW);
  32.         // 重置模型观察矩阵
  33.         gl.glLoadIdentity();
  34.     }
  35. }

复制代码

只要加入这段代码到OpenGLDemo class里就可实现全屏this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
设置完视图后,即可编译运行,可以看到一个“漂亮”的黑屏 = =!

OpenGLDemo01.rar (48.38 KB, 下载次数: 259)

 

二、绘制多边形前面的教程都是关于设置GLSurfaceView.的,接下来的教程将教我们渲染出一个多边形。3D模型用较小的元素创建(点,边,面),他们可以被分别操作。
顶点

 

在Android中,我们通过float数组定义顶点,并将它放到字节型缓冲区内来获取更好的性能。下例的代码即为上图所示顶点。OpenGL ES的很多功能都必须手动的开启和关闭。

  1. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  2. // 设置顶点数据,3代表XYZ坐标系
  3. gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
  4. // 关闭顶点设置
  5. gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

复制代码

计算多边形面的时候,一定要注意正确的方向.。因为这将决定哪一面为正面哪一面为背面。 所以我们尽量保证整个项目都使用相同的环绕。gl.glFrontFace(GL10.GL_CCW);控制多边形的正面是如何决定的。在默认情况下,mode是GL_CCW。mode的值为:   GL_CCW 表示窗口坐标上投影多边形的顶点顺序为逆时针方向的表面为正面。   GL_CW 表示顶点顺序为顺时针方向的表面为正面。顶点的方向又称为环绕。gl.glEnable(GL10.GL_CULL_FACE);gl.glCullFace(GL10.GL_BACK);剔除多边形的背面,禁用多边形背面上的光照、阴影和颜色计算及操作。gl.glDisable(GL10.GL_CULL_FACE);
多边形

到了绘制面的时候了, 我们使用默认的逆时针环绕。下例代码将绘制上图多边形。

  1.     // 将坐标数组放入字节缓存中
  2.     // (1) 分配缓存,一个short为2个字节,所以要乘以2
  3.     ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
  4.     // (2) 设置字节处理规则
  5.     ibb.order(ByteOrder.nativeOrder());
  6.     // (3) 转换为short型字符
  7.     ShortBuffer indexBuffer = ibb.asShortBuffer();
  8.     // (4) 放入坐标数组
  9.     indexBuffer.put(indices);
  10.     // (5) 复位
  11.     indexBuffer.position(0);

复制代码

渲染是时候弄些玩意儿到屏幕上去了,绘制时我们将用到两个函数public abstract void glDrawArrays(int mode, int first, int count)通过我们构造的顶点缓存来绘制顶点public abstract void glDrawElements(int mode, int count, int type, Buffer indices)和glDrawArrays类似,但需要直接传入type(索引值的类型,如GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT),和indices(索引缓存)两者的共同点是,都必须知道他们需要画什么。怎样渲染图元,有不同方式,为了帮助调试,我们应该了解它们。
Mode:GL_POINTS绘制独立的点到屏幕

GL_LINE_STRIP连续的连线,第n个顶点与第n-1个顶点绘制一条直线

GL_LINE_LOOP和上面相同,但首尾相连

GL_LINES各对独立的线段

GL_TRIANGLES各个独立的三角形

GL_TRIANGLE_STRIP
绘制一系列的三角形,先是顶点 v0, v1, v2, 然后是 v2, v1, v3 (注意规律), 然后v2, v3, v4等。该规律确保所有的三角形都以相同的方向绘制。

GL_TRIANGLE_FAN和GL_TRIANGLE_STRIP类似, 但其先绘制 v0, v1, v2, 再是 v0, v2, v3, 然后 v0, v3, v4等。
我认为GL_TRIANGLES是使用最方便的,所以我们将先使用它。

  1. public class Square {
  2.     // 顶点坐标数组
  3.     private float vertices[] = { -1.0f, 1.0f, 0.0f, // 0, 左上
  4.         -1.0f, -1.0f, 0.0f, // 1, 左下
  5.         1.0f, -1.0f, 0.0f, // 2, 右下
  6.         1.0f, 1.0f, 0.0f, // 3, 右上
  7.     };
  8.     // 连接规则
  9.     private short[] indices = { 0, 1, 2, 0, 2, 3 };
  10.     // 顶点缓存
  11.     private FloatBuffer vertexBuffer;
  12.     // 索引缓存
  13.     private ShortBuffer indexBuffer;
  14.  
  15.  
  16.     public Square() {
  17.         // 一个float为4 bytes, 因此要乘以4
  18.         ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  19.         vbb.order(ByteOrder.nativeOrder());
  20.         vertexBuffer = vbb.asFloatBuffer();
  21.         vertexBuffer.put(vertices);
  22.         vertexBuffer.position(0);
  23.         // short类型同理
  24.         ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
  25.         ibb.order(ByteOrder.nativeOrder());
  26.         indexBuffer = ibb.asShortBuffer();
  27.         indexBuffer.put(indices);
  28.         indexBuffer.position(0);
  29.         }
  30.  
  31.     /**
  32.      * 绘制正方形到屏幕
  33.      *
  34.      * @param gl
  35.      */
  36.     public void draw(GL10 gl) {
  37.         // 逆时针环绕
  38.         gl.glFrontFace(GL10.GL_CCW);
  39.         // 开启剔除功能
  40.         gl.glEnable(GL10.GL_CULL_FACE);
  41.         // 剔除背面
  42.         gl.glCullFace(GL10.GL_BACK);
  43.         // 开启顶点缓存写入功能
  44.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  45.         // 设置顶点
  46.         // size:每个顶点有几个数指描述。
  47.         // type:数组中每个顶点的坐标类型。
  48.         // stride:数组中每个顶点间的间隔,步长(字节位移)。
  49.         // pointer:存储着每个顶点的坐标值。初始值为0
  50.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
  51.         gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
  52.         GL10.GL_UNSIGNED_SHORT, indexBuffer);
  53.         // 关闭各个功能
  54.         gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  55.         gl.glDisable(GL10.GL_CULL_FACE);
  56.     }
  57. }

复制代码

我们必须在OpenGLRenderer类中初始化square

  1. square = new Square();<!–EndFragment–>

复制代码

并在主绘制方法中调用square的绘制方法

  1. public void onDrawFrame(GL10 gl) {
  2.     // 清除屏幕和深度缓存
  3.     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  4.     // 绘制正方形
  5.     square.draw(gl);
  6. }

复制代码

如果你现在运行应用,我们又看到了华丽的黑屏,为什么?因为OpenGL ES渲染默认的当前位置为(0,0,0),窗口的定位也一样。而且OpenGL ES不渲染太靠近窗体定位的东西。解决方法就是移动绘制的位置。

  1. gl.glTranslatef(0, 0, -4);  <!–EndFragment–>

复制代码

再次运行应用你将看到该正方形已经被绘制,但是它好像离我们越来越远一样,最后消失了。OpenGL ES不会在画面之间复位绘制点,所以我们要自己完成。

  1. // 重置当前的模型观察矩阵
  2. gl.glLoadIdentity();<!–EndFragment–>

复制代码

现在,我们运行应用将会看到一个固定位置的正方形。

OpenGLDemo02.rar (64.87 KB, 下载次数: 347)

作者awen83发布于2016年11月17日分类TODO

文章导航

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 应用中使用 GLSurfaceViewCamera2 API 实现预览,可以参考以下步骤: 1. 在你的 Android 项目中添加 GLSurfaceView 控件,并在应用程序中初始化它。 2. 通过 Camera2 API 打开相机,并将相机输出连接到 GLSurfaceView 控件上。 3. 在 GLSurfaceView 控件中实现自定义的 Renderer,并在 Renderer 中实现图像渲染和处理逻辑。 4. 将渲染结果显示在 GLSurfaceView 控件上。 以下是一个简单的代码示例,演示如何使用 GLSurfaceViewCamera2 API 实现预览: ```java public class PreviewActivity extends AppCompatActivity { private CameraManager cameraManager; private CameraDevice cameraDevice; private CameraCaptureSession captureSession; private CaptureRequest.Builder previewRequestBuilder; private CaptureRequest previewRequest; private Size previewSize; private SurfaceTexture surfaceTexture; private GLSurfaceView glSurfaceView; private CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() { @Override public void onOpened(@NonNull CameraDevice camera) { cameraDevice = camera; createCameraPreviewSession(); } @Override public void onDisconnected(@NonNull CameraDevice camera) { cameraDevice.close(); cameraDevice = null; } @Override public void onError(@NonNull CameraDevice camera, int error) { cameraDevice.close(); cameraDevice = null; } }; private void openCamera() { cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { String cameraId = cameraManager.getCameraIdList()[0]; CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId); StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); previewSize = map.getOutputSizes(SurfaceTexture.class)[0]; surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight()); Surface previewSurface = new Surface(surfaceTexture); previewRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); previewRequestBuilder.addTarget(previewSurface); cameraDevice.createCaptureSession(Arrays.asList(previewSurface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession session) { captureSession = session; updatePreview(); } @Override public void onConfigureFailed(@NonNull CameraCaptureSession session) { } }, null); } catch (CameraAccessException e) { e.printStackTrace(); } } private void createCameraPreviewSession() { try { surfaceTexture = glSurfaceView.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight()); openCamera(); } catch (CameraAccessException e) { e.printStackTrace(); } } private void updatePreview() { previewRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); previewRequest = previewRequestBuilder.build(); try { captureSession.setRepeatingRequest(previewRequest, null, null); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); glSurfaceView = new GLSurfaceView(this); glSurfaceView.setEGLContextClientVersion(2); glSurfaceView.setRenderer(new PreviewRenderer()); setContentView(glSurfaceView); } @Override protected void onResume() { super.onResume(); if (glSurfaceView != null) { glSurfaceView.onResume(); } if (cameraDevice == null) { try { cameraManager.openCamera(cameraManager.getCameraIdList()[0], stateCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } } } @Override protected void onPause() { if (glSurfaceView != null) { glSurfaceView.onPause(); } if (cameraDevice != null) { cameraDevice.close(); cameraDevice = null; } super.onPause(); } private class PreviewRenderer implements GLSurfaceView.Renderer { private final float[] vertexData = { -1f, -1f, 1f, -1f, -1f, 1f, 1f, 1f }; private final float[] textureData = { 0f, 1f, 1f, 1f, 0f, 0f, 1f, 0f }; private int textureId; private int program; private int aPositionLocation; private int aTextureLocation; private int uTextureMatrixLocation; @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { textureId = createTexture(); program = createProgram(); aPositionLocation = glGetAttribLocation(program, "aPosition"); aTextureLocation = glGetAttribLocation(program, "aTextureCoord"); uTextureMatrixLocation = glGetUniformLocation(program, "uTextureMatrix"); glClearColor(0f, 0f, 0f, 0f); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { glViewport(0, 0, width, height); Matrix.scaleM(textureMatrix, 0, 1f, -1f, 1f); Matrix.translateM(textureMatrix, 0, 0f, -1f, 0f); Matrix.rotateM(textureMatrix, 0, 90f, 0f, 0f, 1f); } @Override public void onDrawFrame(GL10 gl) { glClear(GL_COLOR_BUFFER_BIT); glUseProgram(program); glEnableVertexAttribArray(aPositionLocation); glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT, false, 0, vertexBuffer); glEnableVertexAttribArray(aTextureLocation); glVertexAttribPointer(aTextureLocation, 2, GL_FLOAT, false, 0, textureBuffer); glUniformMatrix4fv(uTextureMatrixLocation, 1, false, textureMatrix, 0); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(aPositionLocation); glDisableVertexAttribArray(aTextureLocation); } private int createTexture() { int[] textures = new int[1]; glGenTextures(1, textures, 0); int textureId = textures[0]; glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureId); glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); return textureId; } private int createProgram() { String vertexShaderCode = "attribute vec4 aPosition;\n" + "attribute vec4 aTextureCoord;\n" + "uniform mat4 uTextureMatrix;\n" + "varying vec2 vTextureCoord;\n" + "void main() {\n" + " vTextureCoord = (uTextureMatrix * aTextureCoord).xy;\n" + " gl_Position = aPosition;\n" + "}"; String fragmentShaderCode = "#extension GL_OES_EGL_image_external : require\n" + "precision mediump float;\n" + "uniform samplerExternalOES uTexture;\n" + "varying vec2 vTextureCoord;\n" + "void main() {\n" + " gl_FragColor = texture2D(uTexture, vTextureCoord);\n" + "}"; int vertexShader = loadShader(GL_VERTEX_SHADER, vertexShaderCode); int fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentShaderCode); int program = glCreateProgram(); glAttachShader(program, vertexShader); glAttachShader(program, fragmentShader); glLinkProgram(program); glUseProgram(program); return program; } private int loadShader(int type, String code) { int shader = glCreateShader(type); glShaderSource(shader, code); glCompileShader(shader); return shader; } } } ``` 需要注意的是,这只是一个简单的示例,并且可能需要进行进一步的优化和改进,以满足你的实际需求和性能要求。同时,为了确保应用程序的稳定性,还需要进行充分的测试和错误处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值