提交 b931d26a 编写于 作者: G guoshuyu

增加水印效果 (2017-09-29)

上级 f3969743
package com.example.gsyvideoplayer.effect;
import android.opengl.GLSurfaceView;
import com.shuyu.gsyvideoplayer.GSYVideoGLView.ShaderInterface;
/**
* 合成两个渲染图画面
*/
public class BitmapIconEffect implements ShaderInterface {
public BitmapIconEffect() {
}
@Override
public String getShader(GLSurfaceView mGlSurfaceView) {
String shader =
"#extension GL_OES_EGL_image_external : require\n"
+ "precision mediump float;\n"
+ "varying vec2 vTextureCoord;\n"
+ "uniform samplerExternalOES sTexture;\n"
+ "uniform sampler2D sTexture2;\n"
+ "void main() {\n"
+ " vec4 c1 = texture2D(sTexture2, vTextureCoord);\n"
+ " gl_FragColor = c1;\n"
+ "}\n";
return shader;
}
}
\ No newline at end of file
......@@ -3,13 +3,20 @@ package com.example.gsyvideoplayer.effect;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.SurfaceTexture;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.opengl.Matrix;
import com.shuyu.gsyvideoplayer.R;
import com.example.gsyvideoplayer.R;
import com.shuyu.gsyvideoplayer.listener.GSYVideoShotListener;
import com.shuyu.gsyvideoplayer.render.GSYVideoGLViewSimpleRender;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
......@@ -20,47 +27,150 @@ import javax.microedition.khronos.opengles.GL10;
@SuppressLint("ViewConstructor")
public class GSYVideoGLViewCustomRender extends GSYVideoGLViewSimpleRender {
private Bitmap mBitmap, mBitmap2;
private static final int FLOAT_SIZE_BYTES = 4;
private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0;
private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3;
private final float[] mTriangleVerticesData = {
// X, Y, Z, U, V
-1.0f, -1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
-1.0f, 0.0f, 1.0f,
0.0f, -1.0f, 1.0f,
0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f,};
private float[] mMVPMatrix = new float[16];
private float[] mSTMatrix = new float[16];
private int muMVPMatrixHandle;
private int muSTMatrixHandle;
private int maPositionHandle;
private int maTextureHandle;
private int curProgram;
private int mTexturesBitmap[] = new int[1];
private FloatBuffer mTriangleVertices;
private Bitmap mBitmap;
//水印圖
private BitmapEffect bitmapEffect = new BitmapEffect();
private BitmapIconEffect bitmapEffect = new BitmapIconEffect();
public GSYVideoGLViewCustomRender() {
super();
mTriangleVertices = ByteBuffer
.allocateDirect(
mTriangleVerticesData.length * FLOAT_SIZE_BYTES)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mTriangleVertices.put(mTriangleVerticesData).position(0);
Matrix.setIdentityM(mSTMatrix, 0);
Matrix.setIdentityM(mMVPMatrix, 0);
}
@Override
protected void bindDrawFrameTexture() {
super.bindDrawFrameTexture();
int mFilterInputTextureUniform2 = GLES20.glGetUniformLocation(mProgram, "sTexture2");
public void onDrawFrame(GL10 glUnused) {
super.onDrawFrame(glUnused);
curProgram = createProgram(getVertexShader(), bitmapEffect.getShader(mSurfaceView));
GLES20.glUseProgram(curProgram);
checkGlError("glUseProgram");
int mFilterInputTextureUniform2 = GLES20.glGetUniformLocation(curProgram, "sTexture2");
GLES20.glActiveTexture(GLES20.GL_TEXTURE3);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexturesBitmap[0]);
GLES20.glUniform1i(mFilterInputTextureUniform2, mTexturesBitmap[0]);
}
@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT,
false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
mTriangleVertices);
checkGlError("glVertexAttribPointer maPosition");
GLES20.glEnableVertexAttribArray(maPositionHandle);
checkGlError("glEnableVertexAttribArray maPositionHandle");
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT,
false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES,
mTriangleVertices);
checkGlError("glVertexAttribPointer maTextureHandle");
GLES20.glEnableVertexAttribArray(maTextureHandle);
checkGlError("glEnableVertexAttribArray maTextureHandle");
GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);
@Override
public void onDrawFrame(GL10 glUnused) {
super.onDrawFrame(glUnused);
float[] transform = new float[16];
Matrix.setRotateM(transform, 0, 360 * 60 / 100, 0.0f, 0.0f, 1.0f);
Matrix.scaleM(transform, 0, 50f / mSurfaceView.getWidth(), 50f / mSurfaceView.getWidth(), 1);
Matrix.scaleM(transform, 0, 200f / mSurfaceView.getWidth(), 200f / mSurfaceView.getWidth(), 1);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, transform, 0);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
//GLES20.glBlendColor(1, 1, 1, 0.5f);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
checkGlError("glDrawArrays");
GLES20.glFinish();
GLES20.glDisable(GLES20.GL_BLEND);
}
@Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
super.onSurfaceCreated(glUnused, config);
curProgram = createProgram(getVertexShader(), bitmapEffect.getShader(mSurfaceView));
if (curProgram == 0) {
return;
}
maPositionHandle = GLES20
.glGetAttribLocation(curProgram, "aPosition");
checkGlError("glGetAttribLocation aPosition");
if (maPositionHandle == -1) {
throw new RuntimeException(
"Could not get attrib location for aPosition");
}
maTextureHandle = GLES20.glGetAttribLocation(curProgram,
"aTextureCoord");
checkGlError("glGetAttribLocation aTextureCoord");
if (maTextureHandle == -1) {
throw new RuntimeException(
"Could not get attrib location for aTextureCoord");
}
muMVPMatrixHandle = GLES20.glGetUniformLocation(curProgram,
"uMVPMatrix");
checkGlError("glGetUniformLocation uMVPMatrix");
if (muMVPMatrixHandle == -1) {
throw new RuntimeException(
"Could not get attrib location for uMVPMatrix");
}
muSTMatrixHandle = GLES20.glGetUniformLocation(curProgram,
"uSTMatrix");
checkGlError("glGetUniformLocation uSTMatrix");
if (muSTMatrixHandle == -1) {
throw new RuntimeException(
"Could not get attrib location for uSTMatrix");
}
mBitmap = BitmapFactory.decodeResource(mSurfaceView.getResources(), R.drawable.unlock);
mBitmap2 = BitmapFactory.decodeResource(mSurfaceView.getResources(), R.drawable.video_enlarge);
GLES20.glGenTextures(1, mTexturesBitmap, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexturesBitmap[0]);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
......@@ -75,20 +185,9 @@ public class GSYVideoGLViewCustomRender extends GSYVideoGLViewSimpleRender {
mBitmap.recycle();
}
@Override
protected String getVertexShader() {
return super.getVertexShader();
}
@Override
protected String getFragmentShader() {
return bitmapEffect.getShader(mSurfaceView);
}
@Override
public void releaseAll() {
super.releaseAll();
}
}
......
......@@ -35,7 +35,7 @@ public class GSYVideoGLViewSimpleRender extends GSYVideoGLViewBaseRender {
private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3;
protected static final int GL_TEXTURE_EXTERNAL_OES = 0x8D65;
private static final int GL_TEXTURE_EXTERNAL_OES = 0x8D65;
private final float[] mTriangleVerticesData = {
// X, Y, Z, U, V
......@@ -57,15 +57,15 @@ public class GSYVideoGLViewSimpleRender extends GSYVideoGLViewBaseRender {
+ " vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n"
+ "}\n";
protected float[] mMVPMatrix = new float[16];
private float[] mMVPMatrix = new float[16];
private float[] mSTMatrix = new float[16];
protected int mProgram;
private int mProgram;
protected int mTextureID[] = new int[2];
private int mTextureID[] = new int[2];
protected int muMVPMatrixHandle;
private int muMVPMatrixHandle;
private int muSTMatrixHandle;
......@@ -246,6 +246,10 @@ public class GSYVideoGLViewSimpleRender extends GSYVideoGLViewBaseRender {
}
public int getProgram() {
return mProgram;
}
protected String getVertexShader() {
return mVertexShader;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册