1.GLSurfaceView
区别于SurfaceView,TextureSurfaceView,需自行出来图像画面的渲染
onSurfaceCreated(GL10 gl, EGLConfig config); 初始化SurefaceView,创建纹理操作。
onSurfaceChanged(GL10 gl, int width, int height);监听Surfaceview大小改变。
onDrawFrame(GL10 gl);进行图像处理及渲染draw工作。
2.FBO
FBO还需相关对象来辅佐,Shader;原纹理texture;输出纹理texture(fbo纹理);
public class Shader {
private int mProgram = 0;
private int mShaderVertex = 0;
private int mShaderFragment = 0;
// HashMap for storing uniform/attribute handles
private final HashMap mShaderHandleMap = new HashMap<>();
public Shader() {
}
public void setProgram(int vertexShader, int fragmentShader, Context context)
throws Exception {
String vertexSource = loadRawString(vertexShader, context);
String fragmentSource = loadRawString(fragmentShader, context);
mShaderVertex = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
mShaderFragment = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
int program = GLES20.glCreateProgram();
if (program != 0) {
GLES20.glAttachShader(program, mShaderVertex);
GLES20.glAttachShader(program, mShaderFragment);
GLES20.glLinkProgram(program);
int[] linkStatus = new int[1];
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
if (linkStatus[0] != GLES20.GL_TRUE) {
String error = GLES20.glGetProgramInfoLog(program);
deleteProgram();
throw new Exception(error);
}
}
mProgram = program;
mShaderHandleMap.clear();
}
public void useProgram() {
GLES20.glUseProgram(mProgram);
}
public void deleteProgram() {
GLES20.glDeleteShader(mShaderVertex);
GLES20.glDeleteShader(mShaderFragment);
GLES20.glDeleteProgram(mProgram);
mProgram = mShaderVertex = mShaderFragment = 0;
}
public int programHandle() {
return mProgram;
}
public int getHandle(String name) {
if (mShaderHandleMap.containsKey(name)) {
return mShaderHandleMap.get(name);
}
int handle = GLES20.glGetAttribLocation(mProgram, name);
if (handle == -1) {
handle = GLES20.glGetUniformLocation(mProgram, name);
}
if (handle == -1) {
Log.d("GLSL shader", "Could not get attrib location for " + name);
} else {
mShaderHandleMap.put(name, handle);
}
return handle;
}
public int[] getHandles(String... names) {
int[] res = new int[names.length];
for (int i = 0; i < names.length; ++i) {
res[i] = getHandle(names[i]);
}
return res;
}
private int loadShader(int shaderType, String source) throws Exception {
int shader = GLES20.glCreateShader(shaderType);
if (shader != 0) {
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
int[] compiled = new int[1];
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
if (compiled[0] == 0) {
String error = GLES20.glGetShaderInfoLog(shader);
GLES20.glDeleteShader(shader);
throw new Exception(error);
}
}
return shader;
}
private String loadRawString(int rawId, Context context) throws Exception {
InputStream is = context.getResources().openRawResource(rawId);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) != -1) {
baos.write(buf, 0, len);
}
return baos.toString();
}}
创建fbo
private int createFbo(int width, int height) {
int[] texture = new int[1];
int[] fbo = new int[1];
GLES20.glGenFramebuffers(1, fbo, 0);
GLES20.glGenTextures(1, texture, 0);
mFbo = fbo[0];
mDstTexture = texture[0];
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mDstTexture);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
// Bind the framebuffer
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFbo);
// Specify texture as color attachment
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mDstTexture, 0);
// Check for framebuffer complete
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
Log.e(LOG_TAG, "Failed to create framebuffer!!!");
}
return 0;
}
渲染器
public class TextureRenderer {
private static final String LOG_TAG = "TextureRenderer";
private final String mVertexShader =
"attribute vec4 position;\n" +
"attribute vec2 inputTextureCoordinate;\n" +
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uSTMatrix;\n" +
"varying vec2 textureCoordinate;\n" +
"void main()\n" +
"{\n" +
"gl_Position = uMVPMatrix * position;\n" +
"vec4 tex4 = vec4(inputTextureCoordinate.xy, 1.0, 1.0);\n" +
"textureCoordinate = (uSTMatrix * tex4).xy;\n" +
"}";
private final String mFragmentShaderOes =
"#extension GL_OES_EGL_image_external : require\n" +
"precision mediump float;\n" +
"varying vec2 textureCoordinate;\n" +
"uniform samplerExternalOES s_texture;\n" +
"void main() {\n" +
"gl_FragColor = texture2D(s_texture, textureCoordinate);\n" +
"}";
private final String mFragmentShaderRgba =
"precision mediump float;\n" +
"varying vec2 textureCoordinate;\n" +
"uniform sampler2D s_texture;\n" +
"void main() {\n" +
"gl_FragColor = texture2D(s_texture, textureCoordinate);\n" +
"}";
private FloatBuffer vertexBuffer, textureVerticesBuffer;
private ShortBuffer drawListBuffer;
private final int mProgram;
private boolean mOesTexture = true;
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 2;
static float squareVertices[] = {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f};
// in counterclockwise order:
float textureVertices[] = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,};
private short drawOrder[] = {0, 1, 2, 0, 2, 3}; // order to draw vertices
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
// uniforms
private int muMVPMatrixHandle;
private int muSTMatrixHandle;
private float[] mMVPMatrix = new float[16];
private float[] mSTMatrix = new float[16];
public TextureRenderer(boolean isOesTexture) {
mOesTexture = isOesTexture;
ByteBuffer bb = ByteBuffer.allocateDirect(squareVertices.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareVertices);
vertexBuffer.position(0);
ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
ByteBuffer bb2 = ByteBuffer.allocateDirect(textureVertices.length * 4);
bb2.order(ByteOrder.nativeOrder());
textureVerticesBuffer = bb2.asFloatBuffer();
textureVerticesBuffer.put(textureVertices);
textureVerticesBuffer.position(0);
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, mVertexShader);
int fragmentShader;
if (mOesTexture) {
fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, mFragmentShaderOes);
} else {
fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, mFragmentShaderRgba);
}
mProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(mProgram);
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
Matrix.setIdentityM(mMVPMatrix, 0);
Matrix.setIdentityM(mSTMatrix, 0);
}
public void rotate(int degrees) {
double theta = (double) degrees / 180 * Math.PI;
mMVPMatrix[0] = (float) Math.cos(theta);
mMVPMatrix[1] = -(float) Math.sin(theta);
mMVPMatrix[4] = (float) Math.sin(theta);
mMVPMatrix[5] = (float) Math.cos(theta);
}
public void draw(int texture) {
GLES20.glUseProgram(mProgram);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
if (mOesTexture)
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture);
else
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
int positionHandle = GLES20.glGetAttribLocation(mProgram, "position");
GLES20.glEnableVertexAttribArray(positionHandle);
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
int textureCoordHandle = GLES20.glGetAttribLocation(mProgram, "inputTextureCoordinate");
GLES20.glEnableVertexAttribArray(textureCoordHandle);
GLES20.glVertexAttribPointer(textureCoordHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, textureVerticesBuffer);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
// Disable vertex array
GLES20.glDisableVertexAttribArray(positionHandle);
GLES20.glDisableVertexAttribArray(textureCoordHandle);
GLES20.glUseProgram(0);
}
private void printMatrix(float[] matrix) {
// 4 x 4
for (int i = 0; i < 4; i++) {
Log.d(LOG_TAG, matrix[i * 4 + 0] + " " + matrix[i * 4 + 1] + " " + matrix[i * 4 + 2] + " " + matrix[i * 4 + 3]);
}
}
public void draw(int texture, float[] matrix) {
// Log.i(TAG, "Matrix is");
// printMatrix(matrix);
// mSTMatrix = matrix;
for (int i = 0; i < matrix.length; i++) {
mSTMatrix[i] = matrix[i];
}
// debug
draw(texture);
}
private int loadShader(int type, String shaderCode) {
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}