doum
6 天以前 2b287056e2f59518888d05a1bbc7e5a55fbd84d5
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
 */
package com.example.datalibrary.view;
 
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
 
import java.nio.ByteBuffer;
 
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
 
/*
 * 深度图渲染器
 */
public class ColorRenderer implements GLSurfaceView.Renderer {
    /**
     * GL图形绘制渲染器
     */
    private ColorViewImpl mColorViewImpl;
    /**
     * 接收的帧数据
     */
    private ByteBuffer mRgbBuffer;
 
    /**
     * 接收将要绘制的帧数据
     *
     * @param buffer
     */
    public void setRgbBuffer(ByteBuffer buffer) {
        mRgbBuffer = buffer;
    }
 
    /**
     * 仅调用一次,设置view的OpenGLES环境
     *
     * @param gl
     * @param config
     */
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // 设置背景色(用来清除缓冲的颜色为黑色)
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        // 创建GL图形绘制渲染器
        mColorViewImpl = new ColorViewImpl();
        if (!mColorViewImpl.isProgramBuilt()) {
            mColorViewImpl.buildProgram();
        }
        // 打开深度检测
        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
        // 打开背面剪裁
        GLES20.glEnable(GLES20.GL_CULL_FACE);
    }
 
    /**
     * 如果view的几和形状发生变化了就调用
     *
     * @param gl
     * @param width
     * @param height
     */
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // 刷新View的形状
        GLES20.glViewport(0, 0, width, height);
    }
 
    /**
     * 每次View被重绘时被调用
     *
     * @param gl
     */
    public void onDrawFrame(GL10 gl) {
        if (mColorViewImpl == null) {
            return;
        }
        // 清除缓冲
        // GLES20.GL_COLOR_BUFFER_BIT是颜色缓冲
        // GLES20.GL_DEPTH_BUFFER_BIT是深度缓冲
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
        if (mRgbBuffer != null) {
            mRgbBuffer.position(0);
            mColorViewImpl.buildTextures(mRgbBuffer, 640, 480);
        }
        mColorViewImpl.drawSelf();
    }
}