@ -61,6 +61,8 @@ public class GPUImageFilter {
private int [ ] mGLFboId ;
private int [ ] mGLFboTexId ;
private IntBuffer mGLFboBuffer ;
private float [ ] TEX_COORD ;
private int curCameraid ;
public GPUImageFilter ( ) {
this ( MagicFilterType . NONE ) ;
@ -83,6 +85,7 @@ public class GPUImageFilter {
public void init ( Context context ) {
mContext = context ;
// curCameraid = cameraid;
onInit ( ) ;
onInitialized ( ) ;
}
@ -119,36 +122,25 @@ public class GPUImageFilter {
}
private void loadSamplerShader ( ) {
mGLProgId = OpenGLUtils . loadProgram ( OpenGLUtils . readShaderFromRawResource ( getContext ( ) , mVertexShaderId ) ,
OpenGLUtils . readShaderFromRawResource ( getContext ( ) , mFragmentShaderId ) ) ;
mGLProgId = OpenGLUtils . loadProgram ( OpenGLUtils . readShaderFromRawResource ( getContext ( ) , mVertexShaderId ) , OpenGLUtils . readShaderFromRawResource ( getContext ( ) , mFragmentShaderId ) ) ;
mGLPositionIndex = GLES20 . glGetAttribLocation ( mGLProgId , "position" ) ;
mGLTextureCoordinateIndex = GLES20 . glGetAttribLocation ( mGLProgId , "inputTextureCoordinate" ) ;
mGLTextureCoordinateIndex = GLES20 . glGetAttribLocation ( mGLProgId , "inputTextureCoordinate" ) ;
mGLTextureTransformIndex = GLES20 . glGetUniformLocation ( mGLProgId , "textureTransform" ) ;
mGLInputImageTextureIndex = GLES20 . glGetUniformLocation ( mGLProgId , "inputImageTexture" ) ;
}
private void initVbo ( ) {
final float VEX_CUBE [ ] = {
- 1.0f , - 1.0f , // Bottom left.
1.0f , - 1.0f , // Bottom right.
- 1.0f , 1.0f , // Top left.
1.0f , 1.0f , // Top right.
//初始化 顶点着色器
- 1.0f , - 1.0f , // Bottom left.
1.0f , - 1.0f , // Bottom right.
- 1.0f , 1.0f , // Top left.
1.0f , 1.0f , // Top right.
} ;
final float TEX_COORD [ ] = {
0.0f , 0.0f , // Bottom left.
1.0f , 0.0f , // Bottom right.
0.0f , 1.0f , // Top left.
1.0f , 1.0f // Top right.
} ;
mGLCubeBuffer = ByteBuffer . allocateDirect ( VEX_CUBE . length * 4 )
. order ( ByteOrder . nativeOrder ( ) ) . asFloatBuffer ( ) ;
setOrtation ( curCameraid ) ;
mGLCubeBuffer = ByteBuffer . allocateDirect ( VEX_CUBE . length * 4 ) . order ( ByteOrder . nativeOrder ( ) ) . asFloatBuffer ( ) ;
mGLCubeBuffer . put ( VEX_CUBE ) . position ( 0 ) ;
mGLTextureBuffer = ByteBuffer . allocateDirect ( TEX_COORD . length * 4 )
. order ( ByteOrder . nativeOrder ( ) ) . asFloatBuffer ( ) ;
mGLTextureBuffer . put ( TEX_COORD ) . position ( 0 ) ;
mGLCubeId = new int [ 1 ] ;
mGLTextureCoordinateId = new int [ 1 ] ;
@ -157,9 +149,7 @@ public class GPUImageFilter {
GLES20 . glBindBuffer ( GLES20 . GL_ARRAY_BUFFER , mGLCubeId [ 0 ] ) ;
GLES20 . glBufferData ( GLES20 . GL_ARRAY_BUFFER , mGLCubeBuffer . capacity ( ) * 4 , mGLCubeBuffer , GLES20 . GL_STATIC_DRAW ) ;
GLES20 . glGenBuffers ( 1 , mGLTextureCoordinateId , 0 ) ;
GLES20 . glBindBuffer ( GLES20 . GL_ARRAY_BUFFER , mGLTextureCoordinateId [ 0 ] ) ;
GLES20 . glBufferData ( GLES20 . GL_ARRAY_BUFFER , mGLTextureBuffer . capacity ( ) * 4 , mGLTextureBuffer , GLES20 . GL_STATIC_DRAW ) ;
initTextures ( ) ;
}
private void destoryVbo ( ) {
@ -255,6 +245,8 @@ public class GPUImageFilter {
GLES20 . glEnableVertexAttribArray ( mGLPositionIndex ) ;
GLES20 . glVertexAttribPointer ( mGLPositionIndex , 2 , GLES20 . GL_FLOAT , false , 4 * 2 , 0 ) ;
initTextures ( ) ;
GLES20 . glBindBuffer ( GLES20 . GL_ARRAY_BUFFER , mGLTextureCoordinateId [ 0 ] ) ;
GLES20 . glEnableVertexAttribArray ( mGLTextureCoordinateIndex ) ;
GLES20 . glVertexAttribPointer ( mGLTextureCoordinateIndex , 2 , GLES20 . GL_FLOAT , false , 4 * 2 , 0 ) ;
@ -288,16 +280,28 @@ public class GPUImageFilter {
return mGLFboTexId [ 0 ] ;
}
protected void onDrawArraysPre ( ) { }
//初始化纹理坐标
private void initTextures ( ) {
mGLTextureBuffer = ByteBuffer . allocateDirect ( TEX_COORD . length * 4 ) . order ( ByteOrder . nativeOrder ( ) ) . asFloatBuffer ( ) ;
mGLTextureBuffer . put ( TEX_COORD ) . position ( 0 ) ;
GLES20 . glGenBuffers ( 1 , mGLTextureCoordinateId , 0 ) ;
GLES20 . glBindBuffer ( GLES20 . GL_ARRAY_BUFFER , mGLTextureCoordinateId [ 0 ] ) ;
GLES20 . glBufferData ( GLES20 . GL_ARRAY_BUFFER , mGLTextureBuffer . capacity ( ) * 4 , mGLTextureBuffer , GLES20 . GL_STATIC_DRAW ) ;
}
protected void onDrawArraysPre ( ) {
}
protected void onDrawArraysAfter ( ) {
}
protected void onDrawArraysAfter ( ) { }
private void runPendingOnDrawTasks ( ) {
while ( ! mRunOnDraw . isEmpty ( ) ) {
mRunOnDraw . removeFirst ( ) . run ( ) ;
}
}
public int getProgram ( ) {
return mGLProgId ;
}
@ -313,8 +317,8 @@ public class GPUImageFilter {
protected MagicFilterType getFilterType ( ) {
return mType ;
}
public void setTextureTransformMatrix ( float [ ] mtx ) {
public void setTextureTransformMatrix ( float [ ] mtx ) {
mGLTextureTransformMatrix = mtx ;
}
@ -410,5 +414,52 @@ public class GPUImageFilter {
mRunOnDraw . addLast ( runnable ) ;
}
}
//设置纹理旋转角度
public void setOrtation ( int i ) {
//纹理坐标
TEX_COORD = new float [ ] { 0.0f , 0.0f , // Bottom left.
1.0f , 0.0f , // Bottom right.
0.0f , 1.0f , // Top left.
1.0f , 1.0f // Top right.
} ;
final float TEX_COORD_270 [ ] = { 0.0f , 0.0f , // Bottom left.
1.0f , 0.0f , // Bottom right.
0.0f , 1.0f , // Top left.
1.0f , 1.0f // Top right.
} ;
final float TEX_COORD0 [ ] = {
//1号摄像头 正
0.0f , 1.0f , // Bottom left.
0.0f , 0.0f , // Bottom right.
1.0f , 1.0f , // Top left.
1.0f , 0.0f // Top right.
} ;
final float TEX_COORD_90 [ ] = {
//二号摄像头正
1.0f , 1.0f , // Bottom left.
0.0f , 1.0f , // Bottom right.
1.0f , 0.0f , // Top left.
0.0f , 0.0f // Top right.
} ;
final float TEX_COORD_180 [ ] = {
//三号摄像头正
1.0f , 0.0f , // Bottom left.
1.0f , 1.0f , // Bottom right.
0.0f , 0.0f , // Top left.
0.0f , 1.0f // Top right.
} ;
if ( i = = 0 ) {
TEX_COORD = TEX_COORD0 ;
} else if ( i = = 1 ) {
TEX_COORD = TEX_COORD_90 ;
} else if ( i = = 2 ) {
TEX_COORD = TEX_COORD_180 ;
} else if ( i = = 3 ) {
TEX_COORD = TEX_COORD_270 ;
}
}
}