import android.graphics.SurfaceTexture;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.opengl.Matrix;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
public class TextureRender {
private static final String VERTEX_SHADER_CODE =
"uniform mat4 uMVPMatrix;\n" +
"attribute vec2 aPosition;\n" +
"attribute vec2 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"void main() {\n" +
" vTextureCoord = aTextureCoord;\n" +
" gl_Position = uMVPMatrix * vec4(aPosition, 0.0, 1.0);\n" +
"}\n";
private static final String FRAGMENT_SHADER_CODE =
"#extension GL_OES_EGL_image_external : require\n" +
"precision mediump float;\n" +
"varying vec2 vTextureCoord;\n" +
"uniform samplerExternalOES sTexture;\n" +
"void main() {\n" +
" gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +
"}\n";
private final float[] mMVPMatrix = new float[16];
private final float[] mSTMatrix = new float[16];
private int mProgram;
private int mTextureID = -12;
private int muMVPMatrixHandle;
private int maPositionHandle;
private int maTextureHandle;
private int muSTMatrixHandle;
public TextureRender() {
// 加载定点着色器和片段着色器
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_CODE);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_CODE);
// 创建程序
mProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glLinkProgram(mProgram);
}
public void drawFrame(SurfaceTexture st) {
// 使用程序
GLES20.glUseProgram(mProgram);
// 获取变换矩阵处理句柄
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
// 获取外部纹理
if (mTextureID == -12) {
int[] textures = new int[1];
GLES20.glGenTextures(1, textur
评论已关闭