├── .gitignore ├── .idea ├── encodings.xml ├── gradle.xml ├── inspectionProfiles │ └── Project_Default.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── libs │ ├── arm64-v8a │ │ └── libdrawdemo.so │ ├── armeabi-v7a │ │ └── libdrawdemo.so │ ├── armeabi │ │ └── libdrawdemo.so │ ├── mips │ │ └── libdrawdemo.so │ ├── mips64 │ │ └── libdrawdemo.so │ ├── x86 │ │ └── libdrawdemo.so │ └── x86_64 │ │ └── libdrawdemo.so ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── andev │ │ │ └── androidshaderdemo │ │ │ ├── Constants.java │ │ │ ├── MainActivity.java │ │ │ ├── Section.java │ │ │ ├── activity │ │ │ ├── CameraActivity.java │ │ │ ├── EGLActivity.java │ │ │ ├── FilterActivity.java │ │ │ ├── LUTActivity.java │ │ │ ├── NativeActivity.java │ │ │ ├── SplitScreenOneActivity.java │ │ │ ├── SplitScreenTwoActivity.java │ │ │ └── WaterMarkActivity.java │ │ │ ├── camera │ │ │ ├── CameraEngine.java │ │ │ ├── CameraInfo.java │ │ │ └── CameraUtils.java │ │ │ ├── data │ │ │ └── VertexArray.java │ │ │ ├── filter │ │ │ ├── BaseFilter.java │ │ │ ├── CameraInputFilter.java │ │ │ ├── ImageFilter.java │ │ │ ├── SplitScreenFilter.java │ │ │ └── WatermarkFilter.java │ │ │ ├── gles │ │ │ ├── EglCore.java │ │ │ └── GlUtil.java │ │ │ ├── nativedemo │ │ │ └── NativeController.java │ │ │ ├── programs │ │ │ ├── AlphaTextureProgram.java │ │ │ ├── BaseProgram.java │ │ │ ├── BaseSampleProgram.java │ │ │ ├── BurrProgram.java │ │ │ ├── ColorShaderProgram.java │ │ │ ├── FlashWhiteProgram.java │ │ │ ├── GaussianProgram.java │ │ │ ├── GrayTextureProgram.java │ │ │ ├── LUTProgram.java │ │ │ ├── MultiTextureShaderProgram.java │ │ │ ├── NonmaximumSuppressionProgram.java │ │ │ ├── ScaleAnimationProgram.java │ │ │ ├── ShaderProgram.java │ │ │ ├── ShakeProgram.java │ │ │ ├── SharpShaderProgram.java │ │ │ ├── SimpleTextureOESProgram.java │ │ │ ├── SimpleTextureShaderProgram.java │ │ │ ├── SplitScreenProgram.java │ │ │ ├── TwoFilterOneProgram.java │ │ │ ├── TwoFilterTwoProgram.java │ │ │ └── WatermarkProgram.java │ │ │ ├── record │ │ │ ├── RecordActivity.java │ │ │ ├── codec │ │ │ │ ├── VideoCodec.java │ │ │ │ └── VideoConfig.java │ │ │ ├── filter │ │ │ │ ├── BaseImageFilter.java │ │ │ │ ├── GLUtils.java │ │ │ │ ├── ImageFilter.java │ │ │ │ ├── LogUtil.java │ │ │ │ ├── RendererInfo.java │ │ │ │ └── TextureProgram.java │ │ │ ├── render │ │ │ │ ├── OffScreenRender.java │ │ │ │ ├── OffSreenSurface.java │ │ │ │ ├── OnFrameAvailableCallback.java │ │ │ │ ├── OnRenderStateCallback.java │ │ │ │ └── PreviewRender.java │ │ │ ├── utils │ │ │ │ ├── StorageUtil.java │ │ │ │ └── VideoFrameData.java │ │ │ └── widget │ │ │ │ └── CameraSurfaceView.java │ │ │ ├── render │ │ │ ├── BaseRender.java │ │ │ ├── BaseSampleRender.java │ │ │ ├── BurrRender.java │ │ │ ├── CannyRender.java │ │ │ ├── EGLRender.java │ │ │ ├── FlashWhiteRender.java │ │ │ ├── LUTRender.java │ │ │ ├── MultiTextureRender.java │ │ │ ├── ScaleRender.java │ │ │ ├── ShakeRender.java │ │ │ ├── SharpRender.java │ │ │ ├── SimpleRender.java │ │ │ ├── SimpleTextureRender.java │ │ │ ├── SoulOutRender.java │ │ │ ├── TwoFilterRender.java │ │ │ └── WatermarkRender.java │ │ │ ├── util │ │ │ ├── LoggerConfig.java │ │ │ ├── Rotation.java │ │ │ ├── ShaderHelper.java │ │ │ ├── TextResourceReader.java │ │ │ ├── TextTextureHelper.java │ │ │ ├── TextureHelper.java │ │ │ └── TextureRotationUtil.java │ │ │ └── widget │ │ │ └── CameraView.java │ ├── jni │ │ ├── Android.mk │ │ ├── Application.mk │ │ ├── NativeController.cpp │ │ ├── com_andev_androidshaderdemo_nativedemo_NativeController.h │ │ ├── libcommon │ │ │ └── CommonTools.h │ │ └── librender │ │ │ ├── Android.mk │ │ │ ├── egl_core.cpp │ │ │ ├── egl_core.h │ │ │ ├── pic_preview_texture.cpp │ │ │ ├── pic_preview_texture.h │ │ │ ├── preview_controller.cpp │ │ │ ├── preview_controller.h │ │ │ ├── preview_render.cpp │ │ │ └── preview_render.h │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── baboon.png │ │ ├── edgeburn.png │ │ ├── face.png │ │ ├── fairy_tale.png │ │ ├── hefegradientmap.png │ │ ├── hefemap.png │ │ ├── hefemetal.png │ │ ├── hefesoftlight.png │ │ ├── ic_launcher_background.xml │ │ ├── lena.jpg │ │ ├── lena1.jpg │ │ ├── lookup_vertigo.png │ │ ├── test.png │ │ ├── test2.png │ │ ├── test3.png │ │ └── test6.jpg │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── camera_layout.xml │ │ ├── filter_layout.xml │ │ ├── java_egl_layout.xml │ │ ├── native_activity.xml │ │ ├── record_layout.xml │ │ └── split_screen_one_layout.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── raw │ │ ├── alpha_texture_fragment_shader.glsl │ │ ├── base_sample_fragment_shader.glsl │ │ ├── base_sample_vertex_shader.glsl │ │ ├── burr_fragment_shader.glsl │ │ ├── canny_sobel_step_fragment_shader.glsl │ │ ├── circle_mosaic_fragment_shader.glsl │ │ ├── flash_white_fragment_shader.glsl │ │ ├── gaussian_fragment_shader.glsl │ │ ├── gaussian_vertex_shader.glsl │ │ ├── gray_fragment_shader.glsl │ │ ├── lut_fragment_shader.glsl │ │ ├── mosaic_fragment_shader.glsl │ │ ├── multi_texture_fragment_shader │ │ ├── multi_texture_vertex_shader │ │ ├── nonmaximumsuppression_fragment_shader.glsl │ │ ├── scale_animation_vertex_shader.glsl │ │ ├── shake_fragment_shader.glsl │ │ ├── sharp_fragment_shader.glsl │ │ ├── simple_fragment_shader.glsl │ │ ├── simple_texture_fragment_shader │ │ ├── simple_texture_oes_fragment_shader.glsl │ │ ├── simple_texture_oes_vertex_shader.glsl │ │ ├── simple_texture_vertex_shader │ │ ├── simple_vertex_shader.glsl │ │ ├── sobel_fragment_shader.glsl │ │ ├── split_sreen_fragment_shader.glsl │ │ ├── two_filter_one_fragment_shader.glsl │ │ ├── two_filter_one_vertex_shader.glsl │ │ ├── two_filter_two_fragment_shader.glsl │ │ ├── two_filter_two_vertex_shader.glsl │ │ ├── watermark_program_shader.glsl │ │ └── weakpixelinclusion_fragment_shader.glsl │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── andev │ └── androidshaderdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── sreenshot ├── circle_mosaic.png ├── sharp.png └── two_filter_render.png /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidShaderDemo 2 | 3 | Android平台OpenGL SE Camera滤镜实现Demo 4 | 5 | 详细分析过程可参考 6 | 7 | [《Android平台OpenGL SE Camera滤镜实现》](https://www.jianshu.com/p/7d59b3c4f8d4) 8 | 9 | [《Android基于Shader的图像处理(1)-灰度和卷积》](https://www.jianshu.com/p/ead441c01c06) 10 | 11 | [《Android基于Shader的图像处理(2)-帧缓冲区对象FBO》](https://www.jianshu.com/p/94d9e8c056ce) 12 | 13 | [《Android基于Shader的图像处理(3)-马赛克》](https://www.jianshu.com/p/3797db1e2c33) 14 | 15 | [《Android基于Shader的图像处理(4)-Sobel边缘检测》](https://www.jianshu.com/p/7081419470ac) 16 | 17 | [《Android基于Shader的图像处理(5)-Canny边缘检测》](https://www.jianshu.com/p/bf18d3cf8bf7) 18 | 19 | [《Android基于Shader的图像处理(6)-Camera开发》](https://www.jianshu.com/p/36c04e889962) 20 | 21 | [《Android基于Shader的图像处理(7)-颜色表LUT》](https://www.jianshu.com/p/6f35c720c7bf) 22 | 23 | [《Android基于Shader的图像处理(8)-仿抖音缩放特效》](https://www.jianshu.com/p/2843b12f6238) 24 | 25 | [《Android基于Shader的图像处理(9)-仿抖音闪白特效》](https://www.jianshu.com/p/b0c28957ef2a) 26 | 27 | [《Android基于Shader的图像处理(10)-仿抖音毛刺特效》](https://www.jianshu.com/p/3cb9a38de1b6) 28 | 29 | [《Android基于Shader的图像处理(11)-仿抖音灵魂出窍》](https://www.jianshu.com/p/ae3bae47e1e6) 30 | 31 | [《Android基于Shader的图像处理(12)-仿抖音抖动特效》](https://www.jianshu.com/p/b43f3dbc32a2) 32 | 33 | [《Android基于Shader的图像处理(13)-相机分屏预览》](https://www.jianshu.com/p/bf75c84b7a79) 34 | 35 | [《Android基于Shader的图像处理(14)-仿抖音分屏预览》](https://www.jianshu.com/p/dd843de3c345) 36 | 37 | [《Android Native层 OpenGL ES开发》](https://www.jianshu.com/p/53ce56463c32) 38 | 39 | [《Android java层封装EGL》](https://www.jianshu.com/p/5376029fbd2e) 40 | 41 | 42 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.andev.androidshaderdemo" 7 | minSdkVersion 18 8 | targetSdkVersion 21 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | 19 | sourceSets { 20 | main { 21 | jni.srcDirs = [] 22 | } 23 | } 24 | } 25 | 26 | sourceSets { 27 | main { 28 | jniLibs.srcDirs = ['libs'] 29 | } 30 | } 31 | } 32 | 33 | dependencies { 34 | implementation fileTree(dir: 'libs', include: ['*.jar']) 35 | implementation 'com.android.support:appcompat-v7:26.1.0' 36 | implementation 'com.android.support:design:26.1.0' 37 | implementation 'com.android.support:appcompat-v7:26.1.0' 38 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 39 | testImplementation 'junit:junit:4.12' 40 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 41 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 42 | } 43 | -------------------------------------------------------------------------------- /app/libs/arm64-v8a/libdrawdemo.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/libs/arm64-v8a/libdrawdemo.so -------------------------------------------------------------------------------- /app/libs/armeabi-v7a/libdrawdemo.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/libs/armeabi-v7a/libdrawdemo.so -------------------------------------------------------------------------------- /app/libs/armeabi/libdrawdemo.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/libs/armeabi/libdrawdemo.so -------------------------------------------------------------------------------- /app/libs/mips/libdrawdemo.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/libs/mips/libdrawdemo.so -------------------------------------------------------------------------------- /app/libs/mips64/libdrawdemo.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/libs/mips64/libdrawdemo.so -------------------------------------------------------------------------------- /app/libs/x86/libdrawdemo.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/libs/x86/libdrawdemo.so -------------------------------------------------------------------------------- /app/libs/x86_64/libdrawdemo.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/libs/x86_64/libdrawdemo.so -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/Constants.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "OpenGL ES for Android", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information. 8 | ***/ 9 | package com.andev.androidshaderdemo; 10 | 11 | public class Constants { 12 | public static final int BYTES_PER_FLOAT = 4; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/Section.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo; 2 | 3 | 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | public class Section { 8 | List sectionList = new ArrayList<>(); 9 | 10 | public Section(){ 11 | sectionList.add("SimpleRender"); 12 | sectionList.add("SimpleTextureRender"); 13 | sectionList.add("MultiTextureRender"); 14 | sectionList.add("SharpRender"); 15 | sectionList.add("TwoFilterRender"); 16 | sectionList.add("Mosaic"); 17 | sectionList.add("Sobel Edge Detector"); 18 | sectionList.add("Canny Edge Detector"); 19 | sectionList.add("Camera"); 20 | sectionList.add("Native OpenGL ES"); 21 | sectionList.add("Java EGL"); 22 | sectionList.add("Color Lookup Table(LUT)"); 23 | sectionList.add("Scale Animation"); 24 | sectionList.add("Flash White"); 25 | sectionList.add("Burr"); 26 | sectionList.add("Soul out"); 27 | sectionList.add("Shake"); 28 | sectionList.add("Split Screen 1"); 29 | sectionList.add("Split Screen 2"); 30 | sectionList.add("Watermark"); 31 | sectionList.add("Record"); 32 | } 33 | 34 | List getSectionList(){ 35 | return sectionList; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/activity/CameraActivity.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.activity; 2 | 3 | 4 | import android.app.Activity; 5 | import android.os.Bundle; 6 | 7 | import com.andev.androidshaderdemo.R; 8 | 9 | public class CameraActivity extends Activity { 10 | 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.camera_layout); 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/activity/EGLActivity.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.activity; 2 | 3 | 4 | import android.app.Activity; 5 | import android.opengl.EGLSurface; 6 | import android.os.Bundle; 7 | import android.os.Handler; 8 | import android.os.HandlerThread; 9 | import android.view.SurfaceHolder; 10 | import android.view.SurfaceView; 11 | import android.widget.LinearLayout; 12 | 13 | import com.andev.androidshaderdemo.R; 14 | import com.andev.androidshaderdemo.gles.EglCore; 15 | import com.andev.androidshaderdemo.render.EGLRender; 16 | 17 | public class EGLActivity extends Activity implements SurfaceHolder.Callback{ 18 | 19 | private LinearLayout root; 20 | 21 | private SurfaceView surfaceView; 22 | 23 | private EglCore eglCore; 24 | private EGLSurface eglSurface; 25 | 26 | private EGLRender eglRender; 27 | 28 | private Handler renderHandler; 29 | 30 | @Override 31 | protected void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | setContentView(R.layout.java_egl_layout); 34 | 35 | initViews(); 36 | } 37 | 38 | private void initViews(){ 39 | root = (LinearLayout)findViewById(R.id.root); 40 | surfaceView = (SurfaceView)findViewById(R.id.surfaceView); 41 | surfaceView.getHolder().addCallback(this); 42 | } 43 | 44 | @Override 45 | public void surfaceCreated(final SurfaceHolder holder) { 46 | } 47 | 48 | @Override 49 | public void surfaceChanged(final SurfaceHolder holder, int format, final int width, final int height) { 50 | renderHandler = newHandlerThread("render"); 51 | renderHandler.post(new Runnable() { 52 | @Override 53 | public void run() { 54 | initEGL(); 55 | eglSurface = eglCore.createWindowSurface(holder.getSurface()); 56 | eglCore.makeCurrent(eglSurface); 57 | 58 | eglRender = new EGLRender(EGLActivity.this); 59 | eglRender.onSurfaceChanged(width, height); 60 | eglRender.onDraw(); 61 | 62 | eglCore.swapBuffers(eglSurface); 63 | } 64 | }); 65 | } 66 | 67 | @Override 68 | public void surfaceDestroyed(SurfaceHolder holder) { 69 | 70 | } 71 | 72 | private void initEGL(){ 73 | eglCore = new EglCore(null, EglCore.FLAG_RECORDABLE); 74 | } 75 | 76 | private Handler newHandlerThread(String tag) { 77 | HandlerThread thread = new HandlerThread(tag); 78 | thread.start(); 79 | return new Handler(thread.getLooper()); 80 | } 81 | 82 | @Override 83 | public void onDestroy(){ 84 | super.onDestroy(); 85 | 86 | renderHandler.post(new Runnable() { 87 | @Override 88 | public void run() { 89 | eglCore.release(); 90 | eglCore.releaseSurface(eglSurface); 91 | } 92 | }); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/activity/LUTActivity.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.activity; 2 | 3 | 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | 8 | import com.andev.androidshaderdemo.R; 9 | import com.andev.androidshaderdemo.render.LUTRender; 10 | 11 | public class LUTActivity extends AppCompatActivity { 12 | 13 | 14 | private GLSurfaceView glSurfaceView; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.filter_layout); 20 | 21 | glSurfaceView = (GLSurfaceView)findViewById(R.id.glsurfaceView); 22 | glSurfaceView.setEGLContextClientVersion(2); 23 | 24 | LUTRender lutRender = new LUTRender(this); 25 | glSurfaceView.setRenderer(lutRender); 26 | 27 | glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 28 | } 29 | 30 | @Override 31 | protected void onPause() { 32 | super.onPause(); 33 | glSurfaceView.onPause(); 34 | } 35 | 36 | @Override 37 | protected void onResume() { 38 | super.onResume(); 39 | glSurfaceView.onResume(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/activity/NativeActivity.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.activity; 2 | 3 | 4 | import android.app.Activity; 5 | import android.content.res.Resources; 6 | import android.graphics.Bitmap; 7 | import android.graphics.BitmapFactory; 8 | import android.os.Bundle; 9 | import android.view.SurfaceHolder; 10 | import android.view.SurfaceView; 11 | import android.widget.LinearLayout; 12 | 13 | import com.andev.androidshaderdemo.R; 14 | import com.andev.androidshaderdemo.nativedemo.NativeController; 15 | 16 | public class NativeActivity extends Activity implements SurfaceHolder.Callback{ 17 | private LinearLayout root; 18 | private SurfaceView surfaceView; 19 | 20 | private NativeController nativeController; 21 | 22 | static { 23 | System.loadLibrary("drawdemo"); 24 | } 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.native_activity); 30 | 31 | initViews(); 32 | } 33 | 34 | private void initViews(){ 35 | root = (LinearLayout)findViewById(R.id.root); 36 | surfaceView = new SurfaceView(this); 37 | surfaceView.getHolder().addCallback(this); 38 | 39 | root.addView(surfaceView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 40 | } 41 | 42 | @Override 43 | public void surfaceCreated(SurfaceHolder holder) { 44 | nativeController = new NativeController(); 45 | nativeController.init(); 46 | nativeController.setSurface(holder.getSurface()); 47 | } 48 | 49 | @Override 50 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 51 | nativeController.resetSize(width, height); 52 | 53 | Resources resources = getResources(); 54 | Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.face); 55 | nativeController.showBitmap(bitmap); 56 | } 57 | 58 | @Override 59 | public void surfaceDestroyed(SurfaceHolder holder) { 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/activity/SplitScreenTwoActivity.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.activity; 2 | 3 | 4 | import android.app.Activity; 5 | import android.os.Bundle; 6 | 7 | import com.andev.androidshaderdemo.R; 8 | import com.andev.androidshaderdemo.filter.SplitScreenFilter; 9 | import com.andev.androidshaderdemo.widget.CameraView; 10 | 11 | public class SplitScreenTwoActivity extends Activity implements CameraView.OnCameraStateListener{ 12 | private static final String TAG = "SplitScreenTwoActivity"; 13 | CameraView cameraView; 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.camera_layout); 19 | 20 | initViews(); 21 | } 22 | 23 | private void initViews(){ 24 | cameraView = (CameraView)findViewById(R.id.cameraView); 25 | cameraView.setOnCameraStateListener(this); 26 | } 27 | 28 | @Override 29 | public void onPreviewSurfaceCreated() { 30 | cameraView.setFilter(new SplitScreenFilter(this)); 31 | } 32 | 33 | 34 | @Override 35 | public void onStop() { 36 | super.onStop(); 37 | 38 | cameraView.stopPreview(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/activity/WaterMarkActivity.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.activity; 2 | 3 | 4 | import android.app.Activity; 5 | import android.os.Bundle; 6 | 7 | import com.andev.androidshaderdemo.R; 8 | import com.andev.androidshaderdemo.widget.CameraView; 9 | 10 | public class WaterMarkActivity extends Activity implements CameraView.OnCameraStateListener{ 11 | 12 | CameraView cameraView; 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.camera_layout); 18 | 19 | initViews(); 20 | } 21 | 22 | private void initViews(){ 23 | cameraView = (CameraView)findViewById(R.id.cameraView); 24 | cameraView.setOnCameraStateListener(this); 25 | } 26 | 27 | 28 | @Override 29 | public void onPreviewSurfaceCreated() { 30 | cameraView.openWatermark(); 31 | } 32 | 33 | @Override 34 | public void onStop() { 35 | super.onStop(); 36 | 37 | cameraView.stopPreview(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/camera/CameraInfo.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.camera; 2 | 3 | public class CameraInfo { 4 | 5 | public int previewWidth; 6 | 7 | public int previewHeight; 8 | 9 | public int orientation; 10 | 11 | public boolean isFront; 12 | 13 | public int pictureWidth; 14 | 15 | public int pictureHeight; 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/camera/CameraUtils.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.camera; 2 | 3 | import android.hardware.Camera; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * Created by why8222 on 2016/2/25. 9 | */ 10 | public class CameraUtils { 11 | 12 | public static Camera.Size getLargePictureSize(Camera camera){ 13 | if(camera != null){ 14 | List sizes = camera.getParameters().getSupportedPictureSizes(); 15 | Camera.Size temp = sizes.get(0); 16 | for(int i = 1;i < sizes.size();i ++){ 17 | float scale = (float)(sizes.get(i).height) / sizes.get(i).width; 18 | if(temp.width < sizes.get(i).width && scale < 0.6f && scale > 0.5f) 19 | temp = sizes.get(i); 20 | } 21 | return temp; 22 | } 23 | return null; 24 | } 25 | 26 | public static Camera.Size getLargePreviewSize(Camera camera){ 27 | if(camera != null){ 28 | List sizes = camera.getParameters().getSupportedPreviewSizes(); 29 | Camera.Size temp = sizes.get(0); 30 | for(int i = 1;i < sizes.size();i ++){ 31 | if(temp.width < sizes.get(i).width) 32 | temp = sizes.get(i); 33 | } 34 | return temp; 35 | } 36 | return null; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/data/VertexArray.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "OpenGL ES for Android", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information. 8 | ***/ 9 | package com.andev.androidshaderdemo.data; 10 | 11 | import java.nio.ByteBuffer; 12 | import java.nio.ByteOrder; 13 | import java.nio.FloatBuffer; 14 | 15 | import static android.opengl.GLES20.GL_FLOAT; 16 | import static android.opengl.GLES20.glEnableVertexAttribArray; 17 | import static android.opengl.GLES20.glVertexAttribPointer; 18 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 19 | 20 | 21 | public class VertexArray { 22 | private final FloatBuffer floatBuffer; 23 | 24 | public VertexArray(float[] vertexData) { 25 | floatBuffer = ByteBuffer 26 | .allocateDirect(vertexData.length * BYTES_PER_FLOAT) 27 | .order(ByteOrder.nativeOrder()) 28 | .asFloatBuffer() 29 | .put(vertexData); 30 | } 31 | 32 | public void setVertexAttribPointer(int dataOffset, int attributeLocation, 33 | int componentCount, int stride) { 34 | floatBuffer.position(dataOffset); 35 | glVertexAttribPointer(attributeLocation, componentCount, GL_FLOAT, 36 | false, stride, floatBuffer); 37 | glEnableVertexAttribArray(attributeLocation); 38 | 39 | floatBuffer.position(0); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/filter/BaseFilter.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.filter; 2 | 3 | 4 | 5 | public abstract class BaseFilter { 6 | public abstract void onDrawFrame(final int textureId); 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/filter/ImageFilter.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.filter; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | 7 | import com.andev.androidshaderdemo.R; 8 | import com.andev.androidshaderdemo.data.VertexArray; 9 | import com.andev.androidshaderdemo.programs.GrayTextureProgram; 10 | import com.andev.androidshaderdemo.util.TextureHelper; 11 | 12 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 13 | import static android.opengl.GLES20.glClear; 14 | import static android.opengl.GLES20.glClearColor; 15 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 16 | 17 | public class ImageFilter extends BaseFilter{ 18 | 19 | private static final int POSITION_COMPONENT_COUNT = 2; 20 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 21 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 22 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 23 | 24 | public static final float CUBE[] = { 25 | -1.0f, -1.0f, 0f, 0f, 26 | 1.0f, -1.0f, 1f, 0f, 27 | -1.0f, 1.0f, 0f, 1f, 28 | 1.0f, 1.0f, 1f, 1f, 29 | }; 30 | 31 | Context context; 32 | VertexArray vertexArray; 33 | GrayTextureProgram grayTextureProgram; 34 | 35 | private int texture; 36 | 37 | public ImageFilter(Context context){ 38 | this.context = context; 39 | vertexArray = new VertexArray(CUBE); 40 | grayTextureProgram = new GrayTextureProgram(context); 41 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 42 | } 43 | 44 | @Override 45 | public void onDrawFrame(final int textureId) { 46 | glClearColor (0.0f, 0.0f, 0.0f, 0.0f); 47 | glClear(GL_COLOR_BUFFER_BIT); 48 | 49 | grayTextureProgram.useProgram(); 50 | grayTextureProgram.setUniforms(textureId); 51 | 52 | vertexArray.setVertexAttribPointer( 53 | 0, 54 | grayTextureProgram.getPositionAttributeLocation(), 55 | POSITION_COMPONENT_COUNT, 56 | STRIDE); 57 | 58 | vertexArray.setVertexAttribPointer( 59 | POSITION_COMPONENT_COUNT, 60 | grayTextureProgram.getTextureCoordinatesAttributeLocation(), 61 | TEXTURE_COORDINATES_COMPONENT_COUNT, 62 | STRIDE); 63 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 64 | } 65 | 66 | //for test 67 | public void onDrawFrame() { 68 | glClear(GL_COLOR_BUFFER_BIT); 69 | 70 | grayTextureProgram.useProgram(); 71 | grayTextureProgram.setUniforms(texture); 72 | 73 | vertexArray.setVertexAttribPointer( 74 | 0, 75 | grayTextureProgram.getPositionAttributeLocation(), 76 | POSITION_COMPONENT_COUNT, 77 | STRIDE); 78 | 79 | vertexArray.setVertexAttribPointer( 80 | POSITION_COMPONENT_COUNT, 81 | grayTextureProgram.getTextureCoordinatesAttributeLocation(), 82 | TEXTURE_COORDINATES_COMPONENT_COUNT, 83 | STRIDE); 84 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/filter/SplitScreenFilter.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.filter; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | 7 | import com.andev.androidshaderdemo.data.VertexArray; 8 | import com.andev.androidshaderdemo.programs.SplitScreenProgram; 9 | 10 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 11 | import static android.opengl.GLES20.glClear; 12 | import static android.opengl.GLES20.glClearColor; 13 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 14 | 15 | public class SplitScreenFilter extends BaseFilter { 16 | 17 | private static final int POSITION_COMPONENT_COUNT = 2; 18 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 19 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 20 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 21 | 22 | public static final float CUBE[] = { 23 | -1.0f, -1.0f, 0f, 0f, 24 | 1.0f, -1.0f, 1f, 0f, 25 | -1.0f, 1.0f, 0f, 1f, 26 | 1.0f, 1.0f, 1f, 1f, 27 | }; 28 | 29 | Context context; 30 | VertexArray vertexArray; 31 | SplitScreenProgram spliteSreenProgram; 32 | 33 | 34 | public SplitScreenFilter(Context context) { 35 | this.context = context; 36 | vertexArray = new VertexArray(CUBE); 37 | spliteSreenProgram = new SplitScreenProgram(context); 38 | } 39 | 40 | @Override 41 | public void onDrawFrame(final int textureId) { 42 | glClearColor (0.0f, 1.0f, 0.0f, 0.0f); 43 | glClear(GL_COLOR_BUFFER_BIT); 44 | 45 | spliteSreenProgram.useProgram(); 46 | spliteSreenProgram.setUniforms(textureId); 47 | 48 | vertexArray.setVertexAttribPointer( 49 | 0, 50 | spliteSreenProgram.getPositionAttributeLocation(), 51 | POSITION_COMPONENT_COUNT, 52 | STRIDE); 53 | 54 | vertexArray.setVertexAttribPointer( 55 | POSITION_COMPONENT_COUNT, 56 | spliteSreenProgram.getTextureCoordinatesAttributeLocation(), 57 | TEXTURE_COORDINATES_COMPONENT_COUNT, 58 | STRIDE); 59 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 60 | } 61 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/filter/WatermarkFilter.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.filter; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | 7 | import com.andev.androidshaderdemo.data.VertexArray; 8 | import com.andev.androidshaderdemo.programs.WatermarkProgram; 9 | import com.andev.androidshaderdemo.util.TextTextureHelper; 10 | 11 | import static android.opengl.GLES20.GL_BLEND; 12 | import static android.opengl.GLES20.GL_DST_ALPHA; 13 | import static android.opengl.GLES20.GL_SRC_ALPHA; 14 | import static android.opengl.GLES20.glBlendFunc; 15 | import static android.opengl.GLES20.glEnable; 16 | import static android.opengl.GLES20.glViewport; 17 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 18 | 19 | public class WatermarkFilter{ 20 | private static final int POSITION_COMPONENT_COUNT = 2; 21 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 22 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 23 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 24 | 25 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 26 | -1.0f, -1.0f, 0f, 1f - 0f, 27 | 1.0f, -1.0f, 1f, 1f -0f, 28 | -1.0f, 1.0f, 0f, 1f -1f, 29 | 1.0f, 1.0f, 1f, 1f -1f, 30 | }; 31 | 32 | private int width; 33 | private int height; 34 | private String text; 35 | private int textSize; 36 | 37 | private int texture; 38 | 39 | Context context; 40 | VertexArray vertexArray; 41 | WatermarkProgram watermarkProgram; 42 | 43 | public WatermarkFilter(Context context, int width, int height, String text, int textSize){ 44 | this.width = width; 45 | this.height = height; 46 | this.text = text; 47 | this.textSize = textSize; 48 | 49 | this.context = context; 50 | vertexArray = new VertexArray(CUBE); 51 | watermarkProgram = new WatermarkProgram(context); 52 | 53 | texture = TextTextureHelper.createTexture(text, width, height, textSize); 54 | //texture = TextureHelper.loadTexture(context, R.drawable.lena); 55 | } 56 | 57 | 58 | public void onDrawFrame() { 59 | glViewport(0, 0, width, height); 60 | 61 | 62 | glEnable(GL_BLEND); 63 | glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA); 64 | 65 | watermarkProgram.useProgram(); 66 | watermarkProgram.setUniforms(texture); 67 | 68 | vertexArray.setVertexAttribPointer( 69 | 0, 70 | watermarkProgram.getPositionAttributeLocation(), 71 | POSITION_COMPONENT_COUNT, 72 | STRIDE); 73 | 74 | vertexArray.setVertexAttribPointer( 75 | POSITION_COMPONENT_COUNT, 76 | watermarkProgram.getTextureCoordinatesAttributeLocation(), 77 | TEXTURE_COORDINATES_COMPONENT_COUNT, 78 | STRIDE); 79 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 80 | 81 | GLES20.glDisable(GLES20.GL_BLEND); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/nativedemo/NativeController.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.nativedemo; 2 | 3 | 4 | import android.graphics.Bitmap; 5 | import android.view.Surface; 6 | 7 | public class NativeController { 8 | public native void init(); 9 | 10 | public native void setSurface(Surface surface); 11 | 12 | public native void resetSize(int width, int height); 13 | 14 | public native void showBitmap(Bitmap bitmap); 15 | 16 | public native void stop(); 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/AlphaTextureProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1f; 15 | import static android.opengl.GLES20.glUniform1i; 16 | 17 | public class AlphaTextureProgram extends ShaderProgram{ 18 | private final int uTextureUnitLocation; 19 | 20 | private final int aPositionLocation; 21 | private final int uMvpMatrixLocation; 22 | private final int aTextureCoordinatesLocation; 23 | private final int uAlphaLocation; 24 | 25 | 26 | public AlphaTextureProgram(Context context){ 27 | super(context, R.raw.scale_animation_vertex_shader, R.raw.alpha_texture_fragment_shader); 28 | 29 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 30 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 31 | uMvpMatrixLocation = glGetUniformLocation(program, U_MVPMATRIX); 32 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 33 | uAlphaLocation = glGetUniformLocation(program, U_ALPHA); 34 | } 35 | 36 | public void setUniforms(int textureId, float alpha) { 37 | // Set the active texture unit to texture unit 0. 38 | glActiveTexture(GL_TEXTURE0); 39 | 40 | // Bind the texture to this unit. 41 | glBindTexture(GL_TEXTURE_2D, textureId); 42 | 43 | // Tell the texture uniform sampler to use this texture in the shader by 44 | // telling it to read from texture unit 0. 45 | glUniform1i(uTextureUnitLocation, 0); 46 | 47 | glUniform1f(uAlphaLocation, alpha); 48 | } 49 | 50 | public int getPositionAttributeLocation() { 51 | return aPositionLocation; 52 | } 53 | 54 | public int getTextureCoordinatesAttributeLocation() { 55 | return aTextureCoordinatesLocation; 56 | } 57 | 58 | public int getMvpMatrixLocation() { 59 | return uMvpMatrixLocation; 60 | } 61 | 62 | public int getAlphaLocation() { 63 | return uAlphaLocation; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/BaseProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1f; 15 | import static android.opengl.GLES20.glUniform1i; 16 | 17 | public class BaseProgram extends ShaderProgram{ 18 | private final int uTextureUnitLocation; 19 | 20 | private final int aPositionLocation; 21 | private final int aTextureCoordinatesLocation; 22 | 23 | private final int texelWidthOffsetLocation; 24 | private final int texelHeightOffsetLocation; 25 | 26 | public BaseProgram(Context context){ 27 | super(context, R.raw.simple_texture_vertex_shader, R.raw.simple_texture_fragment_shader); 28 | 29 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 30 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 31 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 32 | 33 | texelWidthOffsetLocation = glGetUniformLocation(program, TexelWidthOffset); 34 | texelHeightOffsetLocation = glGetUniformLocation(program, TexelHeightOffset); 35 | } 36 | 37 | public BaseProgram(Context context, int vertexShader, int fragmentShader){ 38 | super(context, vertexShader, fragmentShader); 39 | 40 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 41 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 42 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 43 | 44 | texelWidthOffsetLocation = glGetUniformLocation(program, TexelWidthOffset); 45 | texelHeightOffsetLocation = glGetUniformLocation(program, TexelHeightOffset); 46 | } 47 | 48 | public void setUniforms(int textureId, float widthFactor, float heightFactor) { 49 | // Set the active texture unit to texture unit 0. 50 | glActiveTexture(GL_TEXTURE0); 51 | 52 | // Bind the texture to this unit. 53 | glBindTexture(GL_TEXTURE_2D, textureId); 54 | 55 | // Tell the texture uniform sampler to use this texture in the shader by 56 | // telling it to read from texture unit 0. 57 | glUniform1i(uTextureUnitLocation, 0); 58 | 59 | glUniform1f(texelWidthOffsetLocation, widthFactor); 60 | glUniform1f(texelHeightOffsetLocation, heightFactor); 61 | } 62 | 63 | public int getPositionAttributeLocation() { 64 | return aPositionLocation; 65 | } 66 | 67 | public int getTextureCoordinatesAttributeLocation() { 68 | return aTextureCoordinatesLocation; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/BaseSampleProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1f; 15 | 16 | public class BaseSampleProgram extends ShaderProgram { 17 | private final int uTextureUnitLocation; 18 | private final int uStrength; 19 | private final int uWidthFactor; 20 | private final int uHeightFactor; 21 | 22 | private final int aPositionLocation; 23 | private final int aTextureCoordinatesLocation; 24 | 25 | public BaseSampleProgram(Context context) { 26 | super(context, R.raw.base_sample_vertex_shader, R.raw.base_sample_fragment_shader); 27 | 28 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 29 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 30 | 31 | uStrength = glGetUniformLocation(program, U_STRENGTH); 32 | uWidthFactor = glGetUniformLocation(program, U_WIDTH_FACTOR); 33 | uHeightFactor = glGetUniformLocation(program, U_HEIGHT_FACTOR); 34 | 35 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 36 | } 37 | 38 | public BaseSampleProgram(Context context, int vertexShader, int fragmentShader){ 39 | super(context, vertexShader, fragmentShader); 40 | 41 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 42 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 43 | 44 | uStrength = glGetUniformLocation(program, U_STRENGTH); 45 | uWidthFactor = glGetUniformLocation(program, U_WIDTH_FACTOR); 46 | uHeightFactor = glGetUniformLocation(program, U_HEIGHT_FACTOR); 47 | 48 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 49 | } 50 | 51 | public void setUniforms(int textureId, float widthFactor, float heightFactor) { 52 | // Set the active texture unit to texture unit 0. 53 | glActiveTexture(GL_TEXTURE0); 54 | 55 | // Bind the texture to this unit. 56 | glBindTexture(GL_TEXTURE_2D, textureId); 57 | 58 | glUniform1f(uWidthFactor, widthFactor); 59 | glUniform1f(uHeightFactor, heightFactor); 60 | 61 | glUniform1f(uStrength, widthFactor); 62 | } 63 | 64 | public int getPositionAttributeLocation() { 65 | return aPositionLocation; 66 | } 67 | 68 | public int getTextureCoordinatesAttributeLocation() { 69 | return aTextureCoordinatesLocation; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/BurrProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1i; 15 | 16 | public class BurrProgram extends ShaderProgram{ 17 | private final int uTextureUnitLocation; 18 | 19 | private final int aPositionLocation; 20 | private final int uBurrParamLocation; 21 | private final int aTextureCoordinatesLocation; 22 | 23 | 24 | public BurrProgram(Context context){ 25 | super(context, R.raw.simple_texture_vertex_shader, R.raw.burr_fragment_shader); 26 | 27 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 28 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 29 | uBurrParamLocation = glGetUniformLocation(program, U_BURRPARAM); 30 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 31 | } 32 | 33 | public void setUniforms(int textureId) { 34 | // Set the active texture unit to texture unit 0. 35 | glActiveTexture(GL_TEXTURE0); 36 | 37 | // Bind the texture to this unit. 38 | glBindTexture(GL_TEXTURE_2D, textureId); 39 | 40 | // Tell the texture uniform sampler to use this texture in the shader by 41 | // telling it to read from texture unit 0. 42 | glUniform1i(uTextureUnitLocation, 0); 43 | } 44 | 45 | public int getPositionAttributeLocation() { 46 | return aPositionLocation; 47 | } 48 | 49 | public int getTextureCoordinatesAttributeLocation() { 50 | return aTextureCoordinatesLocation; 51 | } 52 | 53 | 54 | public int getBurrParamLocation(){ 55 | return uBurrParamLocation; 56 | } 57 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/ColorShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.glGetAttribLocation; 9 | 10 | public class ColorShaderProgram extends ShaderProgram{ 11 | private final int aPositionLocation; 12 | private final int aColorLocation; 13 | 14 | public ColorShaderProgram(Context context){ 15 | super(context, R.raw.simple_vertex_shader, R.raw.simple_fragment_shader); 16 | 17 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 18 | aColorLocation = glGetAttribLocation(program, A_COLOR); 19 | } 20 | 21 | public int getPositionAttributeLocation() { 22 | return aPositionLocation; 23 | } 24 | 25 | public int getColorAttributeLocation() { 26 | return aColorLocation; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/FlashWhiteProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1i; 15 | 16 | public class FlashWhiteProgram extends ShaderProgram{ 17 | private final int uTextureUnitLocation; 18 | 19 | private final int aPositionLocation; 20 | private final int uFlashLocation; 21 | private final int aTextureCoordinatesLocation; 22 | 23 | 24 | public FlashWhiteProgram(Context context){ 25 | super(context, R.raw.simple_texture_vertex_shader, R.raw.flash_white_fragment_shader); 26 | 27 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 28 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 29 | uFlashLocation = glGetUniformLocation(program, U_FLASHPARAM); 30 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 31 | } 32 | 33 | public void setUniforms(int textureId) { 34 | // Set the active texture unit to texture unit 0. 35 | glActiveTexture(GL_TEXTURE0); 36 | 37 | // Bind the texture to this unit. 38 | glBindTexture(GL_TEXTURE_2D, textureId); 39 | 40 | // Tell the texture uniform sampler to use this texture in the shader by 41 | // telling it to read from texture unit 0. 42 | glUniform1i(uTextureUnitLocation, 0); 43 | } 44 | 45 | public int getPositionAttributeLocation() { 46 | return aPositionLocation; 47 | } 48 | 49 | public int getTextureCoordinatesAttributeLocation() { 50 | return aTextureCoordinatesLocation; 51 | } 52 | 53 | public int getFlashAttributeLocation() { 54 | return uFlashLocation; 55 | } 56 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/GaussianProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1f; 15 | 16 | public class GaussianProgram extends ShaderProgram{ 17 | private final int []uTextureUnitLocation; 18 | 19 | private final int texelWidthOffsetLocation; 20 | private final int texelHeightOffsetLocation; 21 | 22 | private final int aPositionLocation; 23 | private final int aTextureCoordinatesLocation; 24 | 25 | public GaussianProgram(Context context) { 26 | super(context, R.raw.gaussian_vertex_shader, R.raw.gaussian_fragment_shader); 27 | 28 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 29 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 30 | 31 | 32 | texelWidthOffsetLocation = glGetUniformLocation(program, TexelWidthOffset); 33 | texelHeightOffsetLocation = glGetUniformLocation(program, TexelHeightOffset); 34 | 35 | uTextureUnitLocation = new int[1]; 36 | 37 | for(int i = 0; i < uTextureUnitLocation.length; i++){ 38 | uTextureUnitLocation[i] = glGetUniformLocation(program, U_TEXTURE_UNIT + i); 39 | } 40 | } 41 | 42 | public void setUniforms(int textureId, float widthFactor, float heightFactor) { 43 | // Set the active texture unit to texture unit 0. 44 | glActiveTexture(GL_TEXTURE0); 45 | 46 | // Bind the texture to this unit. 47 | glBindTexture(GL_TEXTURE_2D, textureId); 48 | 49 | glUniform1f(texelWidthOffsetLocation, widthFactor); 50 | glUniform1f(texelHeightOffsetLocation, heightFactor); 51 | } 52 | 53 | 54 | public int getPositionAttributeLocation() { 55 | return aPositionLocation; 56 | } 57 | 58 | public int getTextureCoordinatesAttributeLocation() { 59 | return aTextureCoordinatesLocation; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/GrayTextureProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1i; 15 | 16 | public class GrayTextureProgram extends ShaderProgram { 17 | private final int uTextureUnitLocation; 18 | 19 | private final int aPositionLocation; 20 | private final int aTextureCoordinatesLocation; 21 | 22 | 23 | public GrayTextureProgram(Context context) { 24 | super(context, R.raw.simple_texture_vertex_shader, R.raw.gray_fragment_shader); 25 | 26 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 27 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 28 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 29 | } 30 | 31 | public void setUniforms(int textureId) { 32 | // Set the active texture unit to texture unit 0. 33 | glActiveTexture(GL_TEXTURE0); 34 | 35 | // Bind the texture to this unit. 36 | glBindTexture(GL_TEXTURE_2D, textureId); 37 | 38 | // Tell the texture uniform sampler to use this texture in the shader by 39 | // telling it to read from texture unit 0. 40 | glUniform1i(uTextureUnitLocation, 0); 41 | } 42 | 43 | public int getPositionAttributeLocation() { 44 | return aPositionLocation; 45 | } 46 | 47 | public int getTextureCoordinatesAttributeLocation() { 48 | return aTextureCoordinatesLocation; 49 | } 50 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/LUTProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | import android.content.Context; 4 | 5 | import com.andev.androidshaderdemo.R; 6 | 7 | import static android.opengl.GLES20.GL_TEXTURE0; 8 | import static android.opengl.GLES20.GL_TEXTURE_2D; 9 | import static android.opengl.GLES20.glActiveTexture; 10 | import static android.opengl.GLES20.glBindTexture; 11 | import static android.opengl.GLES20.glGetAttribLocation; 12 | import static android.opengl.GLES20.glGetUniformLocation; 13 | import static android.opengl.GLES20.glUniform1i; 14 | 15 | public class LUTProgram extends ShaderProgram{ 16 | private final int []uTextureUnitLocation; 17 | 18 | private final int aPositionLocation; 19 | private final int aTextureCoordinatesLocation; 20 | 21 | public LUTProgram(Context context) { 22 | super(context, R.raw.multi_texture_vertex_shader, R.raw.lut_fragment_shader); 23 | 24 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 25 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 26 | 27 | uTextureUnitLocation = new int[2]; 28 | 29 | for(int i = 0; i < uTextureUnitLocation.length; i++){ 30 | uTextureUnitLocation[i] = glGetUniformLocation(program, U_TEXTURE_UNIT + i); 31 | } 32 | } 33 | 34 | public void setUniforms(int [] textureIDs) { 35 | for(int i = 0; i < textureIDs.length; i++){ 36 | glActiveTexture(GL_TEXTURE0 + i); 37 | glBindTexture(GL_TEXTURE_2D, textureIDs[i]); 38 | glUniform1i(uTextureUnitLocation[i], i); 39 | } 40 | } 41 | 42 | public int getPositionAttributeLocation() { 43 | return aPositionLocation; 44 | } 45 | 46 | public int getTextureCoordinatesAttributeLocation() { 47 | return aTextureCoordinatesLocation; 48 | } 49 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/MultiTextureShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1f; 15 | import static android.opengl.GLES20.glUniform1i; 16 | 17 | public class MultiTextureShaderProgram extends ShaderProgram{ 18 | private final int []uTextureUnitLocation; 19 | private final int uStrength; 20 | 21 | 22 | private final int aPositionLocation; 23 | private final int aTextureCoordinatesLocation; 24 | 25 | public MultiTextureShaderProgram(Context context) { 26 | super(context, R.raw.multi_texture_vertex_shader, R.raw.multi_texture_fragment_shader); 27 | 28 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 29 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 30 | 31 | uStrength = glGetUniformLocation(program, U_STRENGTH); 32 | 33 | uTextureUnitLocation = new int[6]; 34 | 35 | for(int i = 0; i < uTextureUnitLocation.length; i++){ 36 | uTextureUnitLocation[i] = glGetUniformLocation(program, U_TEXTURE_UNIT + i); 37 | } 38 | } 39 | 40 | public void setUniforms(int [] textureIDs, float strength) { 41 | for(int i = 0; i < textureIDs.length; i++){ 42 | glActiveTexture(GL_TEXTURE0 + i); 43 | glBindTexture(GL_TEXTURE_2D, textureIDs[i]); 44 | glUniform1i(uTextureUnitLocation[i], i); 45 | } 46 | 47 | glUniform1f(uStrength, strength); 48 | } 49 | 50 | public int getPositionAttributeLocation() { 51 | return aPositionLocation; 52 | } 53 | 54 | public int getTextureCoordinatesAttributeLocation() { 55 | return aTextureCoordinatesLocation; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/NonmaximumSuppressionProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1f; 15 | 16 | public class NonmaximumSuppressionProgram extends ShaderProgram{ 17 | private final int []uTextureUnitLocation; 18 | 19 | private final int texelWidthOffsetLocation; 20 | private final int texelHeightOffsetLocation; 21 | 22 | private final int aPositionLocation; 23 | private final int aTextureCoordinatesLocation; 24 | 25 | public NonmaximumSuppressionProgram(Context context) { 26 | super(context, R.raw.simple_texture_vertex_shader, R.raw.nonmaximumsuppression_fragment_shader); 27 | 28 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 29 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 30 | 31 | 32 | texelWidthOffsetLocation = glGetUniformLocation(program, TexelWidthOffset); 33 | texelHeightOffsetLocation = glGetUniformLocation(program, TexelHeightOffset); 34 | 35 | uTextureUnitLocation = new int[1]; 36 | 37 | for(int i = 0; i < uTextureUnitLocation.length; i++){ 38 | uTextureUnitLocation[i] = glGetUniformLocation(program, U_TEXTURE_UNIT + i); 39 | } 40 | } 41 | 42 | public void setUniforms(int textureId, float widthFactor, float heightFactor) { 43 | // Set the active texture unit to texture unit 0. 44 | glActiveTexture(GL_TEXTURE0); 45 | 46 | // Bind the texture to this unit. 47 | glBindTexture(GL_TEXTURE_2D, textureId); 48 | 49 | glUniform1f(texelWidthOffsetLocation, widthFactor); 50 | glUniform1f(texelHeightOffsetLocation, heightFactor); 51 | } 52 | 53 | 54 | public int getPositionAttributeLocation() { 55 | return aPositionLocation; 56 | } 57 | 58 | public int getTextureCoordinatesAttributeLocation() { 59 | return aTextureCoordinatesLocation; 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/ScaleAnimationProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1i; 15 | 16 | public class ScaleAnimationProgram extends ShaderProgram{ 17 | private final int uTextureUnitLocation; 18 | 19 | private final int aPositionLocation; 20 | private final int uMvpMatrixLocation; 21 | private final int aTextureCoordinatesLocation; 22 | 23 | 24 | public ScaleAnimationProgram(Context context){ 25 | super(context, R.raw.scale_animation_vertex_shader, R.raw.simple_texture_fragment_shader); 26 | 27 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 28 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 29 | uMvpMatrixLocation = glGetUniformLocation(program, U_MVPMATRIX); 30 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 31 | } 32 | 33 | public void setUniforms(int textureId) { 34 | // Set the active texture unit to texture unit 0. 35 | glActiveTexture(GL_TEXTURE0); 36 | 37 | // Bind the texture to this unit. 38 | glBindTexture(GL_TEXTURE_2D, textureId); 39 | 40 | // Tell the texture uniform sampler to use this texture in the shader by 41 | // telling it to read from texture unit 0. 42 | glUniform1i(uTextureUnitLocation, 0); 43 | } 44 | 45 | public int getPositionAttributeLocation() { 46 | return aPositionLocation; 47 | } 48 | 49 | public int getTextureCoordinatesAttributeLocation() { 50 | return aTextureCoordinatesLocation; 51 | } 52 | 53 | public int getMvpMatrixLocation() { 54 | return uMvpMatrixLocation; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/ShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | import com.andev.androidshaderdemo.util.ShaderHelper; 6 | import com.andev.androidshaderdemo.util.TextResourceReader; 7 | 8 | import static android.opengl.GLES20.glUseProgram; 9 | 10 | public class ShaderProgram { 11 | 12 | // Uniform constants 13 | protected static final String U_MATRIX = "u_Matrix"; 14 | protected static final String U_TEXTURE_UNIT = "u_TextureUnit"; 15 | protected static final String U_TEXTURE_OES_UNIT = "u_TextureOESUnit"; 16 | protected static final String U_STRENGTH = "u_Strength"; 17 | protected static final String U_WIDTH_FACTOR = "u_imageWidthFactor"; 18 | protected static final String U_HEIGHT_FACTOR = "u_imageHeightFactor"; 19 | protected static final String U_TEXTURE_TRANSFORM = "u_textureTransform"; 20 | protected static final String U_MVPMATRIX = "uMvpMatrix"; 21 | protected static final String U_FLASHPARAM = "uflashParam"; 22 | protected static final String U_BURRPARAM = "uBurrParam"; 23 | protected static final String U_ALPHA = "u_alpha"; 24 | protected static final String U_OFFSET = "u_Offset"; 25 | 26 | protected static final String TexelWidthOffset = "texelWidthOffset"; 27 | protected static final String TexelHeightOffset = "texelHeightOffset"; 28 | 29 | // Attribute constants 30 | protected static final String A_POSITION = "a_Position"; 31 | protected static final String A_COLOR = "a_Color"; 32 | protected static final String A_TEXTURE_COORDINATES = "a_TextureCoordinates"; 33 | 34 | // Shader program 35 | protected final int program; 36 | 37 | protected ShaderProgram(Context context, int vertexShaderResourceId, 38 | int fragmentShaderResourceId) { 39 | // Compile the shaders and link the program. 40 | program = ShaderHelper.buildProgram( 41 | TextResourceReader.readTextFileFromResource( 42 | context, vertexShaderResourceId), 43 | TextResourceReader.readTextFileFromResource( 44 | context, fragmentShaderResourceId)); 45 | } 46 | 47 | public void useProgram() { 48 | // Set the current OpenGL shader program to this program. 49 | glUseProgram(program); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/ShakeProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1f; 15 | import static android.opengl.GLES20.glUniform1i; 16 | 17 | public class ShakeProgram extends ShaderProgram{ 18 | private final int uTextureUnitLocation; 19 | 20 | private final int aPositionLocation; 21 | private final int uMvpMatrixLocation; 22 | private final int aTextureCoordinatesLocation; 23 | private final int uOffsetLocation; 24 | 25 | public ShakeProgram(Context context){ 26 | super(context, R.raw.scale_animation_vertex_shader, R.raw.shake_fragment_shader); 27 | 28 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 29 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 30 | uMvpMatrixLocation = glGetUniformLocation(program, U_MVPMATRIX); 31 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 32 | uOffsetLocation = glGetUniformLocation(program, U_OFFSET); 33 | } 34 | 35 | public void setUniforms(int textureId, float offset) { 36 | // Set the active texture unit to texture unit 0. 37 | glActiveTexture(GL_TEXTURE0); 38 | 39 | // Bind the texture to this unit. 40 | glBindTexture(GL_TEXTURE_2D, textureId); 41 | 42 | // Tell the texture uniform sampler to use this texture in the shader by 43 | // telling it to read from texture unit 0. 44 | glUniform1i(uTextureUnitLocation, 0); 45 | 46 | glUniform1f(uOffsetLocation, offset); 47 | } 48 | 49 | public int getPositionAttributeLocation() { 50 | return aPositionLocation; 51 | } 52 | 53 | public int getTextureCoordinatesAttributeLocation() { 54 | return aTextureCoordinatesLocation; 55 | } 56 | 57 | public int getMvpMatrixLocation() { 58 | return uMvpMatrixLocation; 59 | } 60 | 61 | public int getOffsetLocation() { 62 | return uOffsetLocation; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/SharpShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1f; 15 | import static android.opengl.GLES20.glUniform1i; 16 | 17 | public class SharpShaderProgram extends ShaderProgram{ 18 | private final int []uTextureUnitLocation; 19 | private final int uStrength; 20 | private final int uWidthFactor; 21 | private final int uHeightFactor; 22 | 23 | private final int aPositionLocation; 24 | private final int aTextureCoordinatesLocation; 25 | 26 | public SharpShaderProgram(Context context) { 27 | super(context, R.raw.base_sample_vertex_shader, R.raw.sharp_fragment_shader); 28 | 29 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 30 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 31 | 32 | uStrength = glGetUniformLocation(program, U_STRENGTH); 33 | uWidthFactor = glGetUniformLocation(program, U_WIDTH_FACTOR); 34 | uHeightFactor = glGetUniformLocation(program, U_HEIGHT_FACTOR); 35 | 36 | uTextureUnitLocation = new int[1]; 37 | 38 | for(int i = 0; i < uTextureUnitLocation.length; i++){ 39 | uTextureUnitLocation[i] = glGetUniformLocation(program, U_TEXTURE_UNIT + i); 40 | } 41 | } 42 | 43 | public void setUniforms(int [] textureIDs, float widthFactor, float heightFactor) { 44 | for(int i = 0; i < textureIDs.length; i++){ 45 | glActiveTexture(GL_TEXTURE0 + i); 46 | glBindTexture(GL_TEXTURE_2D, textureIDs[i]); 47 | glUniform1i(uTextureUnitLocation[i], i); 48 | } 49 | 50 | glUniform1f(uWidthFactor, widthFactor); 51 | glUniform1f(uHeightFactor, heightFactor); 52 | 53 | glUniform1f(uStrength, widthFactor); 54 | } 55 | 56 | public int getPositionAttributeLocation() { 57 | return aPositionLocation; 58 | } 59 | 60 | public int getTextureCoordinatesAttributeLocation() { 61 | return aTextureCoordinatesLocation; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/SimpleTextureOESProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES11Ext; 6 | 7 | import com.andev.androidshaderdemo.R; 8 | 9 | import static android.opengl.GLES20.GL_TEXTURE0; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1i; 15 | import static android.opengl.GLES20.glUniformMatrix4fv; 16 | 17 | public class SimpleTextureOESProgram extends ShaderProgram { 18 | 19 | private final int uTextureUnitLocation; 20 | private final int uTextureTransformLocation; 21 | 22 | private final int aPositionLocation; 23 | private final int aTextureCoordinatesLocation; 24 | 25 | 26 | public SimpleTextureOESProgram(Context context) { 27 | super(context, R.raw.simple_texture_oes_vertex_shader, R.raw.simple_texture_oes_fragment_shader); 28 | 29 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_OES_UNIT); 30 | uTextureTransformLocation = glGetUniformLocation(program, U_TEXTURE_TRANSFORM); 31 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 32 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 33 | } 34 | 35 | public void setUniforms(int textureId, float[] textureTransformMatrix) { 36 | // Set the active texture unit to texture unit 0. 37 | glActiveTexture(GL_TEXTURE0); 38 | 39 | // Bind the texture to this unit. 40 | glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId); 41 | 42 | // Tell the texture uniform sampler to use this texture in the shader by 43 | // telling it to read from texture unit 0. 44 | glUniform1i(uTextureUnitLocation, 0); 45 | 46 | glUniformMatrix4fv(uTextureTransformLocation, 1, false, textureTransformMatrix, 0); 47 | } 48 | 49 | public int getPositionAttributeLocation() { 50 | return aPositionLocation; 51 | } 52 | 53 | public int getTextureCoordinatesAttributeLocation() { 54 | return aTextureCoordinatesLocation; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/SimpleTextureShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1i; 15 | 16 | public class SimpleTextureShaderProgram extends ShaderProgram{ 17 | private final int uTextureUnitLocation; 18 | 19 | private final int aPositionLocation; 20 | private final int aTextureCoordinatesLocation; 21 | 22 | 23 | public SimpleTextureShaderProgram(Context context){ 24 | super(context, R.raw.simple_texture_vertex_shader, R.raw.simple_texture_fragment_shader); 25 | 26 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 27 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 28 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 29 | } 30 | 31 | public void setUniforms(int textureId) { 32 | // Set the active texture unit to texture unit 0. 33 | glActiveTexture(GL_TEXTURE0); 34 | 35 | // Bind the texture to this unit. 36 | glBindTexture(GL_TEXTURE_2D, textureId); 37 | 38 | // Tell the texture uniform sampler to use this texture in the shader by 39 | // telling it to read from texture unit 0. 40 | glUniform1i(uTextureUnitLocation, 0); 41 | } 42 | 43 | public int getPositionAttributeLocation() { 44 | return aPositionLocation; 45 | } 46 | 47 | public int getTextureCoordinatesAttributeLocation() { 48 | return aTextureCoordinatesLocation; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/SplitScreenProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1i; 15 | 16 | public class SplitScreenProgram extends ShaderProgram { 17 | private final int uTextureUnitLocation; 18 | 19 | private final int aPositionLocation; 20 | private final int aTextureCoordinatesLocation; 21 | 22 | 23 | public SplitScreenProgram(Context context) { 24 | super(context, R.raw.simple_texture_vertex_shader, R.raw.split_sreen_fragment_shader); 25 | 26 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 27 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 28 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 29 | } 30 | 31 | public void setUniforms(int textureId) { 32 | // Set the active texture unit to texture unit 0. 33 | glActiveTexture(GL_TEXTURE0); 34 | 35 | // Bind the texture to this unit. 36 | glBindTexture(GL_TEXTURE_2D, textureId); 37 | 38 | // Tell the texture uniform sampler to use this texture in the shader by 39 | // telling it to read from texture unit 0. 40 | glUniform1i(uTextureUnitLocation, 0); 41 | } 42 | 43 | public int getPositionAttributeLocation() { 44 | return aPositionLocation; 45 | } 46 | 47 | public int getTextureCoordinatesAttributeLocation() { 48 | return aTextureCoordinatesLocation; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/TwoFilterOneProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | import com.andev.androidshaderdemo.R; 6 | 7 | import static android.opengl.GLES20.GL_TEXTURE0; 8 | import static android.opengl.GLES20.GL_TEXTURE_2D; 9 | import static android.opengl.GLES20.glActiveTexture; 10 | import static android.opengl.GLES20.glBindTexture; 11 | import static android.opengl.GLES20.glGetAttribLocation; 12 | import static android.opengl.GLES20.glGetUniformLocation; 13 | import static android.opengl.GLES20.glUniform1f; 14 | import static android.opengl.GLES20.glUniform1i; 15 | 16 | public class TwoFilterOneProgram extends ShaderProgram{ 17 | private final int []uTextureUnitLocation; 18 | 19 | private final int texelWidthOffsetLocation; 20 | private final int texelHeightOffsetLocation; 21 | 22 | private final int aPositionLocation; 23 | private final int aTextureCoordinatesLocation; 24 | 25 | public TwoFilterOneProgram(Context context) { 26 | super(context, R.raw.two_filter_one_vertex_shader, R.raw.two_filter_one_fragment_shader); 27 | 28 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 29 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 30 | 31 | 32 | texelWidthOffsetLocation = glGetUniformLocation(program, TexelWidthOffset); 33 | texelHeightOffsetLocation = glGetUniformLocation(program, TexelHeightOffset); 34 | 35 | uTextureUnitLocation = new int[1]; 36 | 37 | for(int i = 0; i < uTextureUnitLocation.length; i++){ 38 | uTextureUnitLocation[i] = glGetUniformLocation(program, U_TEXTURE_UNIT + i); 39 | } 40 | } 41 | 42 | public void setUniforms(int [] textureIDs, float widthFactor, float heightFactor) { 43 | for(int i = 0; i < textureIDs.length; i++){ 44 | glActiveTexture(GL_TEXTURE0 + i); 45 | glBindTexture(GL_TEXTURE_2D, textureIDs[i]); 46 | glUniform1i(uTextureUnitLocation[i], i); 47 | } 48 | 49 | glUniform1f(texelWidthOffsetLocation, widthFactor); 50 | glUniform1f(texelHeightOffsetLocation, heightFactor); 51 | } 52 | 53 | public int getPositionAttributeLocation() { 54 | return aPositionLocation; 55 | } 56 | 57 | public int getTextureCoordinatesAttributeLocation() { 58 | return aTextureCoordinatesLocation; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/TwoFilterTwoProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.andev.androidshaderdemo.R; 7 | 8 | import static android.opengl.GLES20.GL_TEXTURE0; 9 | import static android.opengl.GLES20.GL_TEXTURE_2D; 10 | import static android.opengl.GLES20.glActiveTexture; 11 | import static android.opengl.GLES20.glBindTexture; 12 | import static android.opengl.GLES20.glGetAttribLocation; 13 | import static android.opengl.GLES20.glGetUniformLocation; 14 | import static android.opengl.GLES20.glUniform1f; 15 | import static android.opengl.GLES20.glUniform1i; 16 | 17 | public class TwoFilterTwoProgram extends ShaderProgram{ 18 | private final int []uTextureUnitLocation; 19 | 20 | private final int texelWidthOffsetLocation; 21 | private final int texelHeightOffsetLocation; 22 | 23 | private final int aPositionLocation; 24 | private final int aTextureCoordinatesLocation; 25 | 26 | public TwoFilterTwoProgram(Context context) { 27 | super(context, R.raw.two_filter_two_vertex_shader, R.raw.two_filter_two_fragment_shader); 28 | 29 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 30 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 31 | 32 | 33 | texelWidthOffsetLocation = glGetUniformLocation(program, TexelWidthOffset); 34 | texelHeightOffsetLocation = glGetUniformLocation(program, TexelHeightOffset); 35 | 36 | uTextureUnitLocation = new int[2]; 37 | 38 | for(int i = 0; i < uTextureUnitLocation.length; i++){ 39 | uTextureUnitLocation[i] = glGetUniformLocation(program, U_TEXTURE_UNIT + i); 40 | } 41 | } 42 | 43 | public void setUniforms(int [] textureIDs, float widthFactor, float heightFactor) { 44 | for(int i = 0; i < textureIDs.length; i++){ 45 | glActiveTexture(GL_TEXTURE0 + i); 46 | glBindTexture(GL_TEXTURE_2D, textureIDs[i]); 47 | glUniform1i(uTextureUnitLocation[i], i); 48 | } 49 | 50 | glUniform1f(texelWidthOffsetLocation, widthFactor); 51 | glUniform1f(texelHeightOffsetLocation, heightFactor); 52 | } 53 | 54 | public int getPositionAttributeLocation() { 55 | return aPositionLocation; 56 | } 57 | 58 | public int getTextureCoordinatesAttributeLocation() { 59 | return aTextureCoordinatesLocation; 60 | } 61 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/programs/WatermarkProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.programs; 2 | 3 | import android.content.Context; 4 | 5 | import com.andev.androidshaderdemo.R; 6 | 7 | import static android.opengl.GLES20.GL_TEXTURE0; 8 | import static android.opengl.GLES20.GL_TEXTURE_2D; 9 | import static android.opengl.GLES20.glActiveTexture; 10 | import static android.opengl.GLES20.glBindTexture; 11 | import static android.opengl.GLES20.glGetAttribLocation; 12 | import static android.opengl.GLES20.glGetUniformLocation; 13 | import static android.opengl.GLES20.glUniform1i; 14 | 15 | public class WatermarkProgram extends ShaderProgram{ 16 | private final int uTextureUnitLocation; 17 | 18 | private final int aPositionLocation; 19 | private final int aTextureCoordinatesLocation; 20 | 21 | 22 | public WatermarkProgram(Context context){ 23 | super(context, R.raw.simple_texture_vertex_shader, R.raw.watermark_program_shader); 24 | 25 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 26 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 27 | aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES); 28 | } 29 | 30 | public void setUniforms(int textureId) { 31 | // Set the active texture unit to texture unit 0. 32 | glActiveTexture(GL_TEXTURE0); 33 | 34 | // Bind the texture to this unit. 35 | glBindTexture(GL_TEXTURE_2D, textureId); 36 | 37 | // Tell the texture uniform sampler to use this texture in the shader by 38 | // telling it to read from texture unit 0. 39 | glUniform1i(uTextureUnitLocation, 0); 40 | } 41 | 42 | public int getPositionAttributeLocation() { 43 | return aPositionLocation; 44 | } 45 | 46 | public int getTextureCoordinatesAttributeLocation() { 47 | return aTextureCoordinatesLocation; 48 | } 49 | 50 | } 51 | 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/codec/VideoConfig.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.codec; 2 | 3 | 4 | import android.opengl.EGLContext; 5 | 6 | public class VideoConfig { 7 | private EGLContext mGLContext; 8 | 9 | private int mVideoWidth; 10 | 11 | private int mVideoHeight; 12 | 13 | private int mBitRate; 14 | 15 | private String fileName; 16 | 17 | private float factor; 18 | 19 | private long maxDuration; 20 | 21 | public VideoConfig(String fileName, EGLContext context, int width, int height, int bitRate) { 22 | this.mGLContext = context; 23 | this.mVideoWidth = width; 24 | this.mVideoHeight = height; 25 | this.mBitRate = bitRate; 26 | this.fileName = fileName; 27 | } 28 | 29 | public String getFileName() { 30 | return this.fileName; 31 | } 32 | 33 | public float getFactor() { 34 | return factor; 35 | } 36 | 37 | public void setFactor(float factor) { 38 | this.factor = factor; 39 | } 40 | 41 | public EGLContext getGLContext() { 42 | return mGLContext; 43 | } 44 | 45 | public int getVideoWidth() { 46 | return mVideoWidth; 47 | } 48 | 49 | public int getVideoHeight() { 50 | return mVideoHeight; 51 | } 52 | 53 | public int getBitRate() { 54 | return mBitRate; 55 | } 56 | 57 | public long getMaxDuration() { 58 | return maxDuration; 59 | } 60 | 61 | public void setMaxDuration(long maxDuration) { 62 | this.maxDuration = maxDuration; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/filter/BaseImageFilter.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.filter; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | 7 | import com.andev.androidshaderdemo.R; 8 | import com.andev.androidshaderdemo.data.VertexArray; 9 | import com.andev.androidshaderdemo.programs.GrayTextureProgram; 10 | import com.andev.androidshaderdemo.util.TextureHelper; 11 | 12 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 13 | import static android.opengl.GLES20.glClear; 14 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 15 | 16 | public class BaseImageFilter { 17 | private static final int POSITION_COMPONENT_COUNT = 2; 18 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 19 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 20 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 21 | 22 | public static final float CUBE[] = { 23 | -1.0f, -1.0f, 0f, 0f, 24 | 1.0f, -1.0f, 1f, 0f, 25 | -1.0f, 1.0f, 0f, 1f, 26 | 1.0f, 1.0f, 1f, 1f, 27 | }; 28 | 29 | Context context; 30 | VertexArray vertexArray; 31 | GrayTextureProgram grayTextureProgram; 32 | 33 | private int texture; 34 | 35 | public BaseImageFilter(Context context){ 36 | this.context = context; 37 | vertexArray = new VertexArray(CUBE); 38 | grayTextureProgram = new GrayTextureProgram(context); 39 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 40 | } 41 | 42 | public void onDrawFrame(final int textureId) { 43 | glClear(GL_COLOR_BUFFER_BIT); 44 | 45 | grayTextureProgram.useProgram(); 46 | grayTextureProgram.setUniforms(textureId); 47 | 48 | vertexArray.setVertexAttribPointer( 49 | 0, 50 | grayTextureProgram.getPositionAttributeLocation(), 51 | POSITION_COMPONENT_COUNT, 52 | STRIDE); 53 | 54 | vertexArray.setVertexAttribPointer( 55 | POSITION_COMPONENT_COUNT, 56 | grayTextureProgram.getTextureCoordinatesAttributeLocation(), 57 | TEXTURE_COORDINATES_COMPONENT_COUNT, 58 | STRIDE); 59 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 60 | } 61 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/filter/LogUtil.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.filter; 2 | 3 | import android.util.Log; 4 | 5 | /** 6 | * Created by 薛贤俊 on 2018/3/1. 7 | */ 8 | 9 | public class LogUtil { 10 | 11 | private static final String TAG = "DouyinDemo"; 12 | 13 | public static void e(String tag, String msg, Throwable e) { 14 | Log.e(tag, msg, e); 15 | } 16 | 17 | public static void e(String tag, Throwable e) { 18 | Log.e(tag, "", e); 19 | } 20 | 21 | public static void e(String msg) { 22 | e(TAG, msg); 23 | } 24 | 25 | public static void e(Throwable e) { 26 | e(TAG, e); 27 | } 28 | 29 | public static void d(String tag, String msg) { 30 | Log.d(tag, msg); 31 | } 32 | 33 | public static void d(String msg) { 34 | d(TAG, msg); 35 | } 36 | 37 | public static void e(String tag, String msg) { 38 | Log.e(tag, msg); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/filter/RendererInfo.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.filter; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.nio.ByteOrder; 5 | import java.nio.FloatBuffer; 6 | 7 | /** 8 | * Created by 薛贤俊 on 2018/3/7. 9 | */ 10 | 11 | public class RendererInfo { 12 | 13 | private static final float FULL_RECTANGLE_COORDS[] = { 14 | -1.0f, -1.0f, // 0 bottom left 15 | 1.0f, -1.0f, // 1 bottom right 16 | -1.0f, 1.0f, // 2 top left 17 | 1.0f, 1.0f, // 3 top right 18 | }; 19 | private static final float FULL_RECTANGLE_TEX_COORDS[] = { 20 | 0.0f, 0.0f, // 0 bottom left 21 | 1.0f, 0.0f, // 1 bottom right 22 | 0.0f, 1.0f, // 2 top left 23 | 1.0f, 1.0f // 3 top right 24 | }; 25 | 26 | private FloatBuffer mVertexBuffer; 27 | 28 | private FloatBuffer mTexBuffer; 29 | 30 | public RendererInfo() { 31 | mVertexBuffer = ByteBuffer.allocateDirect(FULL_RECTANGLE_COORDS.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); 32 | mVertexBuffer.position(0); 33 | mVertexBuffer.put(FULL_RECTANGLE_COORDS); 34 | 35 | mTexBuffer = ByteBuffer.allocateDirect(FULL_RECTANGLE_TEX_COORDS.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); 36 | mTexBuffer.position(0); 37 | mTexBuffer.put(FULL_RECTANGLE_TEX_COORDS); 38 | } 39 | 40 | public FloatBuffer getVertexBuffer() { 41 | return mVertexBuffer; 42 | } 43 | 44 | public FloatBuffer getTextureBuffer() { 45 | return mTexBuffer; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/filter/TextureProgram.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.filter; 2 | 3 | import static android.opengl.GLES20.glUseProgram; 4 | 5 | /** 6 | * Created by 薛贤俊 on 2018/3/7. 7 | */ 8 | 9 | public class TextureProgram { 10 | 11 | private int mProgramId; 12 | 13 | public TextureProgram(String vertexCode, String fragmentCode) { 14 | mProgramId = GLUtils.buildProgram(vertexCode, fragmentCode); 15 | } 16 | 17 | public void useProgram() { 18 | glUseProgram(mProgramId); 19 | } 20 | 21 | public int getProgramId() { 22 | return mProgramId; 23 | } 24 | 25 | public void enableAttrs() { 26 | 27 | } 28 | 29 | public void disableAttrs() { 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/render/OffScreenRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.render; 2 | 3 | 4 | import android.opengl.EGLContext; 5 | import android.view.Surface; 6 | 7 | import com.andev.androidshaderdemo.filter.CameraInputFilter; 8 | import com.andev.androidshaderdemo.gles.EglCore; 9 | import com.andev.androidshaderdemo.record.filter.ImageFilter; 10 | 11 | public class OffScreenRender { 12 | private static final String TAG = "OffScreenRender"; 13 | private EglCore eglCore; 14 | private OffSreenSurface offSreenSurface; 15 | 16 | ImageFilter imageFilter; 17 | 18 | public OffScreenRender(EGLContext eglContext, Surface surface) { 19 | eglCore = new EglCore(eglContext, EglCore.FLAG_RECORDABLE); 20 | 21 | offSreenSurface = new OffSreenSurface(eglCore, surface); 22 | offSreenSurface.makeCurrent(); 23 | } 24 | 25 | public void release() { 26 | if (offSreenSurface == null) { 27 | return; 28 | } 29 | 30 | offSreenSurface.release(); 31 | eglCore.release(); 32 | offSreenSurface = null; 33 | eglCore = null; 34 | } 35 | 36 | public void draw(CameraInputFilter cameraInputFilter, float[] matrix, int textureId, long time) { 37 | offSreenSurface.makeCurrent(); 38 | // if (imageFilter != filter && imageFilter != null) { 39 | // imageFilter.release(); 40 | // } 41 | // imageFilter = filter; 42 | // filter.init(); 43 | // filter.draw(textureId, matrix,0,0); 44 | 45 | cameraInputFilter.onDrawFrame(textureId); 46 | offSreenSurface.setPresentationTime(time); 47 | 48 | offSreenSurface.swapBuffers(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/render/OffSreenSurface.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.render; 2 | 3 | 4 | import android.opengl.EGL14; 5 | import android.opengl.EGLSurface; 6 | import android.view.Surface; 7 | 8 | import com.andev.androidshaderdemo.gles.EglCore; 9 | 10 | public class OffSreenSurface { 11 | private EglCore eglCore; 12 | private EGLSurface eglSurface; 13 | private Surface surface; 14 | 15 | 16 | public OffSreenSurface(EglCore eglCore, Surface surface) { 17 | this.eglCore = eglCore; 18 | this.surface = surface; 19 | eglSurface = eglCore.createWindowSurface(surface); 20 | } 21 | 22 | 23 | public void makeCurrent() { 24 | eglCore.makeCurrent(eglSurface); 25 | } 26 | 27 | public void setPresentationTime(long nanoSeconds) { 28 | eglCore.setPresentationTime(eglSurface, nanoSeconds); 29 | } 30 | 31 | 32 | public boolean swapBuffers() { 33 | boolean result = eglCore.swapBuffers(eglSurface); 34 | 35 | return result; 36 | } 37 | 38 | public void release() { 39 | eglCore.releaseSurface(eglSurface); 40 | eglSurface = EGL14.EGL_NO_SURFACE; 41 | 42 | if (surface != null) { 43 | surface.release(); 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/render/OnFrameAvailableCallback.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.render; 2 | 3 | 4 | import com.andev.androidshaderdemo.record.utils.VideoFrameData; 5 | 6 | public interface OnFrameAvailableCallback { 7 | void onFrameAvailableCallback(VideoFrameData frameData); 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/render/OnRenderStateCallback.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.render; 2 | 3 | 4 | import android.graphics.SurfaceTexture; 5 | import android.opengl.EGLContext; 6 | 7 | import javax.microedition.khronos.opengles.GL10; 8 | 9 | public interface OnRenderStateCallback { 10 | void onSurfaceCreatedCallback(GL10 gl, EGLContext eglContext, SurfaceTexture surfaceTexture); 11 | void onSurfaceChangedCallback(GL10 gl, int width, int height); 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/render/PreviewRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.render; 2 | 3 | 4 | import android.graphics.SurfaceTexture; 5 | import android.opengl.EGL14; 6 | import android.opengl.GLES20; 7 | import android.opengl.GLSurfaceView; 8 | import android.util.Log; 9 | 10 | import com.andev.androidshaderdemo.filter.CameraInputFilter; 11 | import com.andev.androidshaderdemo.record.filter.ImageFilter; 12 | import com.andev.androidshaderdemo.record.utils.VideoFrameData; 13 | import com.andev.androidshaderdemo.util.TextureHelper; 14 | 15 | import javax.microedition.khronos.egl.EGLConfig; 16 | import javax.microedition.khronos.opengles.GL10; 17 | 18 | import static android.opengl.GLES20.glViewport; 19 | 20 | public class PreviewRender implements GLSurfaceView.Renderer{ 21 | private SurfaceTexture surfaceTexture; 22 | private int textureId = TextureHelper.NO_TEXTURE; 23 | private CameraInputFilter cameraInputFilter; 24 | private ImageFilter mFilter; 25 | 26 | private OnRenderStateCallback onRenderStateCallback; 27 | 28 | private OnFrameAvailableCallback onFrameAvailableCallback; 29 | 30 | public void setOnRenderStateCallback(OnRenderStateCallback onRenderStateCallback){ 31 | this.onRenderStateCallback = onRenderStateCallback; 32 | } 33 | 34 | public void setOnFrameAvailableCallback(OnFrameAvailableCallback onFrameAvailableCallback){ 35 | this.onFrameAvailableCallback = onFrameAvailableCallback; 36 | } 37 | 38 | @Override 39 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 40 | initSurfaceTexture(); 41 | if(onRenderStateCallback != null){ 42 | onRenderStateCallback.onSurfaceCreatedCallback(gl, EGL14.eglGetCurrentContext(), surfaceTexture); 43 | } 44 | 45 | mFilter = new ImageFilter(); 46 | } 47 | 48 | @Override 49 | public void onSurfaceChanged(GL10 gl, int width, int height) { 50 | glViewport(0, 0, width, height); 51 | if(onRenderStateCallback != null){ 52 | onRenderStateCallback.onSurfaceChangedCallback(gl, width, height); 53 | } 54 | 55 | if(cameraInputFilter != null){ 56 | cameraInputFilter.onDisplaySizeChanged(width, height); 57 | } 58 | } 59 | 60 | @Override 61 | public void onDrawFrame(GL10 gl) { 62 | GLES20.glClearColor(0.0f, 1.0f, 0.0f, 0.0f); 63 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 64 | 65 | Log.d("onDrawFrame", "Thread : " + Thread.currentThread().getName()); 66 | 67 | if(surfaceTexture == null) 68 | return; 69 | surfaceTexture.updateTexImage(); 70 | float[] mtx = new float[16]; 71 | surfaceTexture.getTransformMatrix(mtx); 72 | 73 | 74 | 75 | if(onFrameAvailableCallback != null){ 76 | onFrameAvailableCallback.onFrameAvailableCallback(new VideoFrameData(cameraInputFilter, 77 | mtx, surfaceTexture.getTimestamp(), textureId)); 78 | } 79 | 80 | // mFilter.init(); 81 | // surfaceTexture.getTransformMatrix(mtx); 82 | // mFilter.draw(textureId, mtx, 0, 0); 83 | 84 | if(cameraInputFilter != null){ 85 | cameraInputFilter.setTextureTransformMatrix(mtx); 86 | cameraInputFilter.onDrawFrame(textureId); 87 | } 88 | 89 | } 90 | 91 | private void initSurfaceTexture(){ 92 | if (textureId == TextureHelper.NO_TEXTURE) { 93 | textureId = TextureHelper.getExternalOESTextureID(); 94 | if (textureId != TextureHelper.NO_TEXTURE) { 95 | surfaceTexture = new SurfaceTexture(textureId); 96 | } 97 | } 98 | } 99 | 100 | public void setCameraInputFilter(CameraInputFilter cameraInputFilter){ 101 | this.cameraInputFilter = cameraInputFilter; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/utils/StorageUtil.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.utils; 2 | 3 | import android.os.Environment; 4 | 5 | import java.io.File; 6 | 7 | public class StorageUtil { 8 | private static final String APP_DIRECTORY_NAME = "DYDemo"; 9 | 10 | private static boolean sMounted = false; 11 | 12 | private static String APP_DIRECTORY = null; 13 | 14 | public static String getExternalStoragePath() { 15 | if (APP_DIRECTORY != null) { 16 | return APP_DIRECTORY; 17 | } 18 | sMounted = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 19 | if (sMounted) { 20 | APP_DIRECTORY = Environment.getExternalStorageDirectory().getPath(); 21 | File file = new File(APP_DIRECTORY); 22 | if (file.exists() && !file.isDirectory()) { 23 | file.delete(); 24 | } 25 | if(!file.exists()){ 26 | file.mkdirs(); 27 | } 28 | } 29 | return APP_DIRECTORY; 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/record/utils/VideoFrameData.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.record.utils; 2 | 3 | import com.andev.androidshaderdemo.filter.CameraInputFilter; 4 | 5 | public class VideoFrameData { 6 | private CameraInputFilter mFilter; 7 | private float[] mMatrix; 8 | private long mTimeStamp; 9 | private int mTextureId; 10 | 11 | public VideoFrameData(CameraInputFilter filter, float[] matrix, long timestamp, int textureId) { 12 | this.mFilter = filter; 13 | this.mMatrix = matrix; 14 | this.mTimeStamp = timestamp; 15 | this.mTextureId = textureId; 16 | } 17 | 18 | public CameraInputFilter getFilter() { 19 | return mFilter; 20 | } 21 | 22 | public float[] getMatrix() { 23 | return mMatrix; 24 | } 25 | 26 | public long getTimestamp() { 27 | return mTimeStamp; 28 | } 29 | 30 | public int getTextureId() { 31 | return mTextureId; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/BaseRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | 8 | import com.andev.androidshaderdemo.R; 9 | import com.andev.androidshaderdemo.data.VertexArray; 10 | import com.andev.androidshaderdemo.programs.BaseProgram; 11 | import com.andev.androidshaderdemo.util.TextureHelper; 12 | 13 | import javax.microedition.khronos.egl.EGLConfig; 14 | import javax.microedition.khronos.opengles.GL10; 15 | 16 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 17 | import static android.opengl.GLES20.glClear; 18 | import static android.opengl.GLES20.glClearColor; 19 | import static android.opengl.GLES20.glViewport; 20 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 21 | 22 | public class BaseRender implements GLSurfaceView.Renderer{ 23 | 24 | private static final int POSITION_COMPONENT_COUNT = 2; 25 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 26 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 27 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 28 | 29 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 30 | -1.0f, -1.0f, 0f, 1f - 0f, 31 | 1.0f, -1.0f, 1f, 1f -0f, 32 | -1.0f, 1.0f, 0f, 1f -1f, 33 | 1.0f, 1.0f, 1f, 1f -1f, 34 | }; 35 | 36 | Context context; 37 | VertexArray vertexArray; 38 | BaseProgram baseProgram; 39 | private int texture; 40 | 41 | private int width; 42 | private int height; 43 | 44 | private int vertexShader; 45 | private int fragmentShader; 46 | 47 | public BaseRender(Context context){ 48 | this.context = context; 49 | vertexArray = new VertexArray(CUBE); 50 | } 51 | 52 | public BaseRender(Context context, int vertexShader, int fragmentShader){ 53 | this.context = context; 54 | vertexArray = new VertexArray(CUBE); 55 | this.vertexShader = vertexShader; 56 | this.fragmentShader = fragmentShader; 57 | } 58 | 59 | @Override 60 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 61 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 62 | 63 | baseProgram = new BaseProgram(context, vertexShader, fragmentShader); 64 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 65 | } 66 | 67 | @Override 68 | public void onSurfaceChanged(GL10 gl, int width, int height) { 69 | glViewport(0, 0, width, height); 70 | this.width = width; 71 | this.height = height; 72 | } 73 | 74 | @Override 75 | public void onDrawFrame(GL10 gl) { 76 | glClear(GL_COLOR_BUFFER_BIT); 77 | 78 | baseProgram.useProgram(); 79 | baseProgram.setUniforms(texture, 1.0f / width, 1.0f / height); 80 | 81 | vertexArray.setVertexAttribPointer( 82 | 0, 83 | baseProgram.getPositionAttributeLocation(), 84 | POSITION_COMPONENT_COUNT, 85 | STRIDE); 86 | 87 | vertexArray.setVertexAttribPointer( 88 | POSITION_COMPONENT_COUNT, 89 | baseProgram.getTextureCoordinatesAttributeLocation(), 90 | TEXTURE_COORDINATES_COMPONENT_COUNT, 91 | STRIDE); 92 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/BaseSampleRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | 8 | import com.andev.androidshaderdemo.R; 9 | import com.andev.androidshaderdemo.data.VertexArray; 10 | import com.andev.androidshaderdemo.programs.BaseSampleProgram; 11 | import com.andev.androidshaderdemo.util.TextureHelper; 12 | 13 | import javax.microedition.khronos.egl.EGLConfig; 14 | import javax.microedition.khronos.opengles.GL10; 15 | 16 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 17 | import static android.opengl.GLES20.glClear; 18 | import static android.opengl.GLES20.glClearColor; 19 | import static android.opengl.GLES20.glViewport; 20 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 21 | 22 | public class BaseSampleRender implements GLSurfaceView.Renderer{ 23 | 24 | private static final int POSITION_COMPONENT_COUNT = 2; 25 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 26 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 27 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 28 | 29 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 30 | -1.0f, -1.0f, 0f, 1f - 0f, 31 | 1.0f, -1.0f, 1f, 1f -0f, 32 | -1.0f, 1.0f, 0f, 1f -1f, 33 | 1.0f, 1.0f, 1f, 1f -1f, 34 | }; 35 | 36 | Context context; 37 | VertexArray vertexArray; 38 | BaseSampleProgram baseSampleProgram; 39 | 40 | private int texture; 41 | 42 | private int width; 43 | private int height; 44 | 45 | private int vertexShader; 46 | private int fragmentShader; 47 | 48 | public BaseSampleRender(Context context){ 49 | this.context = context; 50 | vertexArray = new VertexArray(CUBE); 51 | } 52 | 53 | public BaseSampleRender(Context context, int vertexShader, int fragmentShader){ 54 | this.context = context; 55 | vertexArray = new VertexArray(CUBE); 56 | this.vertexShader = vertexShader; 57 | this.fragmentShader = fragmentShader; 58 | } 59 | 60 | @Override 61 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 62 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 63 | 64 | baseSampleProgram = new BaseSampleProgram(context, vertexShader, fragmentShader); 65 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 66 | } 67 | 68 | @Override 69 | public void onSurfaceChanged(GL10 gl, int width, int height) { 70 | glViewport(0, 0, width, height); 71 | 72 | this.width = width; 73 | this.height = height; 74 | } 75 | 76 | @Override 77 | public void onDrawFrame(GL10 gl) { 78 | glClear(GL_COLOR_BUFFER_BIT); 79 | 80 | baseSampleProgram.useProgram(); 81 | baseSampleProgram.setUniforms(texture, 1.0f / width, 1.0f / height); 82 | 83 | vertexArray.setVertexAttribPointer( 84 | 0, 85 | baseSampleProgram.getPositionAttributeLocation(), 86 | POSITION_COMPONENT_COUNT, 87 | STRIDE); 88 | 89 | vertexArray.setVertexAttribPointer( 90 | POSITION_COMPONENT_COUNT, 91 | baseSampleProgram.getTextureCoordinatesAttributeLocation(), 92 | TEXTURE_COORDINATES_COMPONENT_COUNT, 93 | STRIDE); 94 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 95 | 96 | } 97 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/BurrRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | 8 | import com.andev.androidshaderdemo.R; 9 | import com.andev.androidshaderdemo.data.VertexArray; 10 | import com.andev.androidshaderdemo.programs.BurrProgram; 11 | import com.andev.androidshaderdemo.util.TextureHelper; 12 | 13 | import javax.microedition.khronos.egl.EGLConfig; 14 | import javax.microedition.khronos.opengles.GL10; 15 | 16 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 17 | import static android.opengl.GLES20.glClear; 18 | import static android.opengl.GLES20.glClearColor; 19 | import static android.opengl.GLES20.glUniform1f; 20 | import static android.opengl.GLES20.glViewport; 21 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 22 | 23 | public class BurrRender implements GLSurfaceView.Renderer{ 24 | private static final int POSITION_COMPONENT_COUNT = 2; 25 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 26 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 27 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 28 | 29 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 30 | -1.0f, -1.0f, 0f, 1f - 0f, 31 | 1.0f, -1.0f, 1f, 1f -0f, 32 | -1.0f, 1.0f, 0f, 1f -1f, 33 | 1.0f, 1.0f, 1f, 1f -1f, 34 | }; 35 | 36 | private final int MaxFrame = 36; 37 | private final int MiddleFrame = 18; 38 | private int curFrame; 39 | 40 | Context context; 41 | VertexArray vertexArray; 42 | BurrProgram burrProgram; 43 | private int texture; 44 | 45 | public BurrRender(Context context){ 46 | this.context = context; 47 | vertexArray = new VertexArray(CUBE); 48 | } 49 | 50 | @Override 51 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 52 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 53 | 54 | burrProgram = new BurrProgram(context); 55 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 56 | } 57 | 58 | @Override 59 | public void onSurfaceChanged(GL10 gl, int width, int height) { 60 | glViewport(0, 0, width, height); 61 | } 62 | 63 | @Override 64 | public void onDrawFrame(GL10 gl) { 65 | glClear(GL_COLOR_BUFFER_BIT); 66 | 67 | burrProgram.useProgram(); 68 | prepareDraw(); 69 | burrProgram.setUniforms(texture); 70 | 71 | vertexArray.setVertexAttribPointer( 72 | 0, 73 | burrProgram.getPositionAttributeLocation(), 74 | POSITION_COMPONENT_COUNT, 75 | STRIDE); 76 | 77 | vertexArray.setVertexAttribPointer( 78 | POSITION_COMPONENT_COUNT, 79 | burrProgram.getTextureCoordinatesAttributeLocation(), 80 | TEXTURE_COORDINATES_COMPONENT_COUNT, 81 | STRIDE); 82 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 83 | } 84 | 85 | private void prepareDraw(){ 86 | float progress; 87 | if (curFrame <= MiddleFrame) { 88 | progress = curFrame * 1.0f / MiddleFrame; 89 | } else { 90 | progress = 2f - curFrame * 1.0f / MiddleFrame; 91 | } 92 | 93 | glUniform1f(burrProgram.getBurrParamLocation(), progress); 94 | 95 | curFrame++; 96 | if(curFrame > MaxFrame){ 97 | curFrame = 0; 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/EGLRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.util.Log; 7 | 8 | import com.andev.androidshaderdemo.R; 9 | import com.andev.androidshaderdemo.data.VertexArray; 10 | import com.andev.androidshaderdemo.programs.SimpleTextureShaderProgram; 11 | import com.andev.androidshaderdemo.util.TextureHelper; 12 | 13 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 14 | import static android.opengl.GLES20.glClear; 15 | import static android.opengl.GLES20.glClearColor; 16 | import static android.opengl.GLES20.glViewport; 17 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 18 | 19 | public class EGLRender { 20 | private static final int POSITION_COMPONENT_COUNT = 2; 21 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 22 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 23 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 24 | 25 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 26 | -1.0f, -1.0f, 0f, 1f - 0f, 27 | 1.0f, -1.0f, 1f, 1f -0f, 28 | -1.0f, 1.0f, 0f, 1f -1f, 29 | 1.0f, 1.0f, 1f, 1f -1f, 30 | }; 31 | 32 | Context context; 33 | VertexArray vertexArray; 34 | SimpleTextureShaderProgram simpleTextureShaderProgram; 35 | private int texture; 36 | 37 | public EGLRender(Context context) { 38 | this.context = context; 39 | 40 | vertexArray = new VertexArray(CUBE); 41 | simpleTextureShaderProgram = new SimpleTextureShaderProgram(context); 42 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 43 | } 44 | 45 | public void onSurfaceChanged(int width, int height) { 46 | glClearColor(0.0f, 1.0f, 0.0f, 0.0f); 47 | glViewport(0, 0, width, height); 48 | 49 | Log.d("EGLRender", "onSurfaceChanged width:" + width + " height : " + height); 50 | } 51 | 52 | 53 | public void onDraw(){ 54 | glClear(GL_COLOR_BUFFER_BIT); 55 | 56 | simpleTextureShaderProgram.useProgram(); 57 | simpleTextureShaderProgram.setUniforms(texture); 58 | 59 | vertexArray.setVertexAttribPointer( 60 | 0, 61 | simpleTextureShaderProgram.getPositionAttributeLocation(), 62 | POSITION_COMPONENT_COUNT, 63 | STRIDE); 64 | 65 | vertexArray.setVertexAttribPointer( 66 | POSITION_COMPONENT_COUNT, 67 | simpleTextureShaderProgram.getTextureCoordinatesAttributeLocation(), 68 | TEXTURE_COORDINATES_COMPONENT_COUNT, 69 | STRIDE); 70 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 71 | 72 | Log.d("EGLRender", "EGLRender onDraw"); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/FlashWhiteRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | 8 | import com.andev.androidshaderdemo.R; 9 | import com.andev.androidshaderdemo.data.VertexArray; 10 | import com.andev.androidshaderdemo.programs.FlashWhiteProgram; 11 | import com.andev.androidshaderdemo.util.TextureHelper; 12 | 13 | import javax.microedition.khronos.egl.EGLConfig; 14 | import javax.microedition.khronos.opengles.GL10; 15 | 16 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 17 | import static android.opengl.GLES20.glClear; 18 | import static android.opengl.GLES20.glClearColor; 19 | import static android.opengl.GLES20.glUniform1f; 20 | import static android.opengl.GLES20.glViewport; 21 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 22 | 23 | public class FlashWhiteRender implements GLSurfaceView.Renderer{ 24 | private static final int POSITION_COMPONENT_COUNT = 2; 25 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 26 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 27 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 28 | 29 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 30 | -1.0f, -1.0f, 0f, 1f - 0f, 31 | 1.0f, -1.0f, 1f, 1f -0f, 32 | -1.0f, 1.0f, 0f, 1f -1f, 33 | 1.0f, 1.0f, 1f, 1f -1f, 34 | }; 35 | 36 | 37 | private final int MaxFrame = 16; 38 | private final int MiddleFrame = 8; 39 | private int curFrame; 40 | 41 | Context context; 42 | VertexArray vertexArray; 43 | FlashWhiteProgram flashWhiteProgram; 44 | private int texture; 45 | 46 | public FlashWhiteRender(Context context){ 47 | this.context = context; 48 | vertexArray = new VertexArray(CUBE); 49 | } 50 | 51 | @Override 52 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 53 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 54 | 55 | flashWhiteProgram = new FlashWhiteProgram(context); 56 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 57 | } 58 | 59 | @Override 60 | public void onSurfaceChanged(GL10 gl, int width, int height) { 61 | glViewport(0, 0, width, height); 62 | } 63 | 64 | @Override 65 | public void onDrawFrame(GL10 gl) { 66 | glClear(GL_COLOR_BUFFER_BIT); 67 | 68 | flashWhiteProgram.useProgram(); 69 | prepareDraw(); 70 | flashWhiteProgram.setUniforms(texture); 71 | 72 | vertexArray.setVertexAttribPointer( 73 | 0, 74 | flashWhiteProgram.getPositionAttributeLocation(), 75 | POSITION_COMPONENT_COUNT, 76 | STRIDE); 77 | 78 | vertexArray.setVertexAttribPointer( 79 | POSITION_COMPONENT_COUNT, 80 | flashWhiteProgram.getTextureCoordinatesAttributeLocation(), 81 | TEXTURE_COORDINATES_COMPONENT_COUNT, 82 | STRIDE); 83 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 84 | } 85 | 86 | private void prepareDraw(){ 87 | float progress; 88 | if (curFrame <= MiddleFrame) { 89 | progress = curFrame * 1.0f / MiddleFrame; 90 | } else { 91 | progress = 2f - curFrame * 1.0f / MiddleFrame; 92 | } 93 | 94 | glUniform1f(flashWhiteProgram.getFlashAttributeLocation(), progress); 95 | 96 | curFrame++; 97 | if(curFrame > MaxFrame){ 98 | curFrame = 0; 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/LUTRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLES20; 5 | import android.opengl.GLSurfaceView; 6 | 7 | import com.andev.androidshaderdemo.R; 8 | import com.andev.androidshaderdemo.data.VertexArray; 9 | import com.andev.androidshaderdemo.programs.LUTProgram; 10 | import com.andev.androidshaderdemo.util.TextureHelper; 11 | 12 | import javax.microedition.khronos.egl.EGLConfig; 13 | import javax.microedition.khronos.opengles.GL10; 14 | 15 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 16 | import static android.opengl.GLES20.glClear; 17 | import static android.opengl.GLES20.glClearColor; 18 | import static android.opengl.GLES20.glViewport; 19 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 20 | 21 | 22 | public class LUTRender implements GLSurfaceView.Renderer{ 23 | private static final String TAG = "LUTRender"; 24 | private static final int POSITION_COMPONENT_COUNT = 2; 25 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 26 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 27 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 28 | 29 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 30 | -1.0f, -1.0f, 0f, 1f - 0f, 31 | 1.0f, -1.0f, 1f, 1f -0f, 32 | -1.0f, 1.0f, 0f, 1f -1f, 33 | 1.0f, 1.0f, 1f, 1f -1f, 34 | }; 35 | 36 | Context context; 37 | VertexArray vertexArray; 38 | LUTProgram lutProgram; 39 | private int[] textureIDs; 40 | private int originTexture; 41 | private int lutTexture; 42 | 43 | public LUTRender(Context context){ 44 | this.context = context; 45 | vertexArray = new VertexArray(CUBE); 46 | } 47 | 48 | @Override 49 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 50 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 51 | 52 | lutProgram = new LUTProgram(context); 53 | originTexture = TextureHelper.loadTexture(context, R.drawable.lena); 54 | lutTexture = TextureHelper.loadLutTexture(context, R.drawable.fairy_tale); 55 | 56 | textureIDs = new int[]{originTexture, lutTexture}; 57 | } 58 | 59 | @Override 60 | public void onSurfaceChanged(GL10 gl, int width, int height) { 61 | glViewport(0, 0, width, height); 62 | } 63 | 64 | @Override 65 | public void onDrawFrame(GL10 gl) { 66 | glClear(GL_COLOR_BUFFER_BIT); 67 | 68 | lutProgram.useProgram(); 69 | lutProgram.setUniforms(textureIDs); 70 | 71 | vertexArray.setVertexAttribPointer( 72 | 0, 73 | lutProgram.getPositionAttributeLocation(), 74 | POSITION_COMPONENT_COUNT, 75 | STRIDE); 76 | 77 | vertexArray.setVertexAttribPointer( 78 | POSITION_COMPONENT_COUNT, 79 | lutProgram.getTextureCoordinatesAttributeLocation(), 80 | TEXTURE_COORDINATES_COMPONENT_COUNT, 81 | STRIDE); 82 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 83 | } 84 | } -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/MultiTextureRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | import com.andev.androidshaderdemo.R; 8 | import com.andev.androidshaderdemo.data.VertexArray; 9 | import com.andev.androidshaderdemo.programs.MultiTextureShaderProgram; 10 | import com.andev.androidshaderdemo.util.TextureHelper; 11 | 12 | import javax.microedition.khronos.egl.EGLConfig; 13 | import javax.microedition.khronos.opengles.GL10; 14 | 15 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 16 | import static android.opengl.GLES20.glClear; 17 | import static android.opengl.GLES20.glClearColor; 18 | import static android.opengl.GLES20.glViewport; 19 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 20 | 21 | public class MultiTextureRender implements GLSurfaceView.Renderer{ 22 | 23 | private static final int POSITION_COMPONENT_COUNT = 2; 24 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 25 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 26 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 27 | 28 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 29 | -1.0f, -1.0f, 0f, 1f - 0f, 30 | 1.0f, -1.0f, 1f, 1f -0f, 31 | -1.0f, 1.0f, 0f, 1f -1f, 32 | 1.0f, 1.0f, 1f, 1f -1f, 33 | }; 34 | 35 | Context context; 36 | VertexArray vertexArray; 37 | MultiTextureShaderProgram multiTextureShaderProgram; 38 | private int originTexture; 39 | private int edgeTexture; 40 | private int hefeMapTexture; 41 | private int hefemetalTexture; 42 | private int hefesoftlightTexture; 43 | private int hefegradientmapTexture; 44 | private int[] textureIDs; 45 | 46 | 47 | public MultiTextureRender(Context context){ 48 | this.context = context; 49 | vertexArray = new VertexArray(CUBE); 50 | } 51 | 52 | @Override 53 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 54 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 55 | 56 | multiTextureShaderProgram = new MultiTextureShaderProgram(context); 57 | originTexture = TextureHelper.loadTexture(context, R.drawable.lena); 58 | edgeTexture = TextureHelper.loadTexture(context, R.drawable.edgeburn); 59 | hefeMapTexture = TextureHelper.loadTexture(context, R.drawable.hefemap); 60 | hefemetalTexture = TextureHelper.loadTexture(context, R.drawable.hefemetal); 61 | hefesoftlightTexture = TextureHelper.loadTexture(context, R.drawable.hefesoftlight); 62 | hefegradientmapTexture = TextureHelper.loadTexture(context, R.drawable.hefegradientmap); 63 | 64 | textureIDs = new int[]{originTexture, edgeTexture, hefeMapTexture, 65 | hefemetalTexture, hefesoftlightTexture,hefegradientmapTexture}; 66 | } 67 | 68 | @Override 69 | public void onSurfaceChanged(GL10 gl, int width, int height) { 70 | glViewport(0, 0, width, height); 71 | } 72 | 73 | @Override 74 | public void onDrawFrame(GL10 gl) { 75 | glClear(GL_COLOR_BUFFER_BIT); 76 | 77 | multiTextureShaderProgram.useProgram(); 78 | multiTextureShaderProgram.setUniforms(textureIDs, 0.5f); 79 | 80 | vertexArray.setVertexAttribPointer( 81 | 0, 82 | multiTextureShaderProgram.getPositionAttributeLocation(), 83 | POSITION_COMPONENT_COUNT, 84 | STRIDE); 85 | 86 | vertexArray.setVertexAttribPointer( 87 | POSITION_COMPONENT_COUNT, 88 | multiTextureShaderProgram.getTextureCoordinatesAttributeLocation(), 89 | TEXTURE_COORDINATES_COMPONENT_COUNT, 90 | STRIDE); 91 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 92 | 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/ScaleRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | import android.opengl.Matrix; 8 | 9 | import com.andev.androidshaderdemo.R; 10 | import com.andev.androidshaderdemo.data.VertexArray; 11 | import com.andev.androidshaderdemo.programs.ScaleAnimationProgram; 12 | import com.andev.androidshaderdemo.util.TextureHelper; 13 | 14 | import javax.microedition.khronos.egl.EGLConfig; 15 | import javax.microedition.khronos.opengles.GL10; 16 | 17 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 18 | import static android.opengl.GLES20.glClear; 19 | import static android.opengl.GLES20.glClearColor; 20 | import static android.opengl.GLES20.glUniformMatrix4fv; 21 | import static android.opengl.GLES20.glViewport; 22 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 23 | 24 | public class ScaleRender implements GLSurfaceView.Renderer{ 25 | private static final int POSITION_COMPONENT_COUNT = 2; 26 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 27 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 28 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 29 | 30 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 31 | -1.0f, -1.0f, 0f, 1f - 0f, 32 | 1.0f, -1.0f, 1f, 1f -0f, 33 | -1.0f, 1.0f, 0f, 1f -1f, 34 | 1.0f, 1.0f, 1f, 1f -1f, 35 | }; 36 | 37 | private float[] scaleMatrix = new float[16]; 38 | private float scale; 39 | private final int MaxFrame = 16; 40 | private final int MiddleFrame = 8; 41 | private int curFrame; 42 | 43 | Context context; 44 | VertexArray vertexArray; 45 | ScaleAnimationProgram scaleAnimationProgram; 46 | private int texture; 47 | 48 | public ScaleRender(Context context){ 49 | this.context = context; 50 | vertexArray = new VertexArray(CUBE); 51 | } 52 | 53 | @Override 54 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 55 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 56 | 57 | scaleAnimationProgram = new ScaleAnimationProgram(context); 58 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 59 | } 60 | 61 | @Override 62 | public void onSurfaceChanged(GL10 gl, int width, int height) { 63 | glViewport(0, 0, width, height); 64 | } 65 | 66 | @Override 67 | public void onDrawFrame(GL10 gl) { 68 | glClear(GL_COLOR_BUFFER_BIT); 69 | 70 | scaleAnimationProgram.useProgram(); 71 | prepareDraw(); 72 | scaleAnimationProgram.setUniforms(texture); 73 | 74 | vertexArray.setVertexAttribPointer( 75 | 0, 76 | scaleAnimationProgram.getPositionAttributeLocation(), 77 | POSITION_COMPONENT_COUNT, 78 | STRIDE); 79 | 80 | vertexArray.setVertexAttribPointer( 81 | POSITION_COMPONENT_COUNT, 82 | scaleAnimationProgram.getTextureCoordinatesAttributeLocation(), 83 | TEXTURE_COORDINATES_COMPONENT_COUNT, 84 | STRIDE); 85 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 86 | } 87 | 88 | private void prepareDraw(){ 89 | Matrix.setIdentityM(scaleMatrix, 0); 90 | 91 | float progress; 92 | if (curFrame <= MiddleFrame) { 93 | progress = curFrame * 1.0f / MiddleFrame; 94 | } else { 95 | progress = 2f - curFrame * 1.0f / MiddleFrame; 96 | } 97 | float scale = 1f + progress * 0.3f; 98 | Matrix.scaleM(scaleMatrix, 0, scale, scale, scale); 99 | glUniformMatrix4fv(scaleAnimationProgram.getMvpMatrixLocation(), 1, false, scaleMatrix, 0); 100 | 101 | curFrame++; 102 | if(curFrame > MaxFrame){ 103 | curFrame = 0; 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/ShakeRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | import android.opengl.Matrix; 8 | 9 | import com.andev.androidshaderdemo.R; 10 | import com.andev.androidshaderdemo.data.VertexArray; 11 | import com.andev.androidshaderdemo.programs.ShakeProgram; 12 | import com.andev.androidshaderdemo.util.TextureHelper; 13 | 14 | import javax.microedition.khronos.egl.EGLConfig; 15 | import javax.microedition.khronos.opengles.GL10; 16 | 17 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 18 | import static android.opengl.GLES20.glClear; 19 | import static android.opengl.GLES20.glClearColor; 20 | import static android.opengl.GLES20.glUniformMatrix4fv; 21 | import static android.opengl.GLES20.glViewport; 22 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 23 | 24 | public class ShakeRender implements GLSurfaceView.Renderer{ 25 | private static final int POSITION_COMPONENT_COUNT = 2; 26 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 27 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 28 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 29 | 30 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 31 | -1.0f, -1.0f, 0f, 1f - 0f, 32 | 1.0f, -1.0f, 1f, 1f -0f, 33 | -1.0f, 1.0f, 0f, 1f -1f, 34 | 1.0f, 1.0f, 1f, 1f -1f, 35 | }; 36 | 37 | private float[] scaleMatrix = new float[16]; 38 | private float scale; 39 | private final int MaxFrame = 16; 40 | private final int MiddleFrame = 8; 41 | private int curFrame; 42 | 43 | Context context; 44 | VertexArray vertexArray; 45 | ShakeProgram shakeProgram; 46 | private int texture; 47 | 48 | public ShakeRender(Context context){ 49 | this.context = context; 50 | vertexArray = new VertexArray(CUBE); 51 | } 52 | 53 | @Override 54 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 55 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 56 | 57 | shakeProgram = new ShakeProgram(context); 58 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 59 | } 60 | 61 | @Override 62 | public void onSurfaceChanged(GL10 gl, int width, int height) { 63 | glViewport(0, 0, width, height); 64 | } 65 | 66 | @Override 67 | public void onDrawFrame(GL10 gl) { 68 | glClear(GL_COLOR_BUFFER_BIT); 69 | 70 | float progress; 71 | if (curFrame <= MaxFrame) { 72 | progress = curFrame * 1.0f / MaxFrame; 73 | } else { 74 | curFrame = 0; 75 | progress = 0; 76 | } 77 | 78 | float scale = 1f + progress * 0.3f; 79 | float offset = progress * 0.01f; 80 | curFrame++; 81 | 82 | drawLayer(scale, offset); 83 | } 84 | 85 | private void drawLayer(float scale, float offset){ 86 | shakeProgram.useProgram(); 87 | 88 | Matrix.setIdentityM(scaleMatrix, 0); 89 | 90 | Matrix.scaleM(scaleMatrix, 0, scale, scale, scale); 91 | glUniformMatrix4fv(shakeProgram.getMvpMatrixLocation(), 1, false, scaleMatrix, 0); 92 | 93 | shakeProgram.setUniforms(texture, offset); 94 | 95 | draw(); 96 | } 97 | 98 | private void draw(){ 99 | vertexArray.setVertexAttribPointer( 100 | 0, 101 | shakeProgram.getPositionAttributeLocation(), 102 | POSITION_COMPONENT_COUNT, 103 | STRIDE); 104 | 105 | vertexArray.setVertexAttribPointer( 106 | POSITION_COMPONENT_COUNT, 107 | shakeProgram.getTextureCoordinatesAttributeLocation(), 108 | TEXTURE_COORDINATES_COMPONENT_COUNT, 109 | STRIDE); 110 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/SharpRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | import com.andev.androidshaderdemo.R; 8 | import com.andev.androidshaderdemo.data.VertexArray; 9 | import com.andev.androidshaderdemo.programs.SharpShaderProgram; 10 | import com.andev.androidshaderdemo.util.TextureHelper; 11 | 12 | import javax.microedition.khronos.egl.EGLConfig; 13 | import javax.microedition.khronos.opengles.GL10; 14 | 15 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 16 | import static android.opengl.GLES20.glClear; 17 | import static android.opengl.GLES20.glClearColor; 18 | import static android.opengl.GLES20.glViewport; 19 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 20 | 21 | 22 | public class SharpRender implements GLSurfaceView.Renderer{ 23 | 24 | private static final int POSITION_COMPONENT_COUNT = 2; 25 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 26 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 27 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 28 | 29 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 30 | -1.0f, -1.0f, 0f, 1f - 0f, 31 | 1.0f, -1.0f, 1f, 1f -0f, 32 | -1.0f, 1.0f, 0f, 1f -1f, 33 | 1.0f, 1.0f, 1f, 1f -1f, 34 | }; 35 | 36 | Context context; 37 | VertexArray vertexArray; 38 | SharpShaderProgram sharpShaderProgram; 39 | private int originTexture; 40 | 41 | private int[] textureIDs; 42 | 43 | private int width; 44 | private int height; 45 | 46 | public SharpRender(Context context){ 47 | this.context = context; 48 | vertexArray = new VertexArray(CUBE); 49 | } 50 | 51 | @Override 52 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 53 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 54 | 55 | sharpShaderProgram = new SharpShaderProgram(context); 56 | originTexture = TextureHelper.loadTexture(context, R.drawable.lena); 57 | 58 | textureIDs = new int[]{originTexture}; 59 | } 60 | 61 | @Override 62 | public void onSurfaceChanged(GL10 gl, int width, int height) { 63 | glViewport(0, 0, width, height); 64 | 65 | this.width = width; 66 | this.height = height; 67 | } 68 | 69 | @Override 70 | public void onDrawFrame(GL10 gl) { 71 | glClear(GL_COLOR_BUFFER_BIT); 72 | 73 | sharpShaderProgram.useProgram(); 74 | sharpShaderProgram.setUniforms(textureIDs, 1.0f / width, 1.0f / height); 75 | 76 | vertexArray.setVertexAttribPointer( 77 | 0, 78 | sharpShaderProgram.getPositionAttributeLocation(), 79 | POSITION_COMPONENT_COUNT, 80 | STRIDE); 81 | 82 | vertexArray.setVertexAttribPointer( 83 | POSITION_COMPONENT_COUNT, 84 | sharpShaderProgram.getTextureCoordinatesAttributeLocation(), 85 | TEXTURE_COORDINATES_COMPONENT_COUNT, 86 | STRIDE); 87 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/SimpleRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | import com.andev.androidshaderdemo.data.VertexArray; 8 | import com.andev.androidshaderdemo.programs.ColorShaderProgram; 9 | 10 | import javax.microedition.khronos.egl.EGLConfig; 11 | import javax.microedition.khronos.opengles.GL10; 12 | 13 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 14 | import static android.opengl.GLES20.glClear; 15 | import static android.opengl.GLES20.glClearColor; 16 | import static android.opengl.GLES20.glViewport; 17 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 18 | 19 | 20 | public class SimpleRender implements GLSurfaceView.Renderer{ 21 | private static final int POSITION_COMPONENT_COUNT = 2; 22 | private static final int COLOR_COMPONENT_COUNT = 3; 23 | private static final int STRIDE = 24 | (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) 25 | * BYTES_PER_FLOAT; 26 | 27 | public static final float CUBE[] = { 28 | -1.0f, -1.0f, 1f, 0f, 0f, 29 | 1.0f, -1.0f, 1f, 0f, 0f, 30 | -1.0f, 1.0f, 1f, 0f, 0f, 31 | 1.0f, 1.0f, 1f, 0f, 0f, 32 | }; 33 | 34 | Context context; 35 | VertexArray vertexArray; 36 | 37 | ColorShaderProgram colorProgram; 38 | 39 | public SimpleRender(Context context) { 40 | this.context = context; 41 | 42 | vertexArray = new VertexArray(CUBE); 43 | } 44 | 45 | 46 | @Override 47 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 48 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 49 | 50 | colorProgram = new ColorShaderProgram(context); 51 | } 52 | 53 | @Override 54 | public void onSurfaceChanged(GL10 gl, int width, int height) { 55 | glViewport(0, 0, width, height); 56 | } 57 | 58 | @Override 59 | public void onDrawFrame(GL10 gl) { 60 | glClear(GL_COLOR_BUFFER_BIT); 61 | 62 | colorProgram.useProgram(); 63 | vertexArray.setVertexAttribPointer( 64 | 0, 65 | colorProgram.getPositionAttributeLocation(), 66 | POSITION_COMPONENT_COUNT, 67 | STRIDE); 68 | 69 | vertexArray.setVertexAttribPointer( 70 | POSITION_COMPONENT_COUNT, 71 | colorProgram.getColorAttributeLocation(), 72 | COLOR_COMPONENT_COUNT, 73 | STRIDE); 74 | 75 | //GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6); 76 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/SimpleTextureRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | import com.andev.androidshaderdemo.R; 8 | import com.andev.androidshaderdemo.data.VertexArray; 9 | import com.andev.androidshaderdemo.programs.SimpleTextureShaderProgram; 10 | import com.andev.androidshaderdemo.util.TextureHelper; 11 | 12 | import javax.microedition.khronos.egl.EGLConfig; 13 | import javax.microedition.khronos.opengles.GL10; 14 | 15 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 16 | import static android.opengl.GLES20.glClear; 17 | import static android.opengl.GLES20.glClearColor; 18 | import static android.opengl.GLES20.glViewport; 19 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 20 | 21 | 22 | public class SimpleTextureRender implements GLSurfaceView.Renderer{ 23 | private static final int POSITION_COMPONENT_COUNT = 2; 24 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 25 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 26 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 27 | 28 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 29 | -1.0f, -1.0f, 0f, 1f - 0f, 30 | 1.0f, -1.0f, 1f, 1f -0f, 31 | -1.0f, 1.0f, 0f, 1f -1f, 32 | 1.0f, 1.0f, 1f, 1f -1f, 33 | }; 34 | 35 | Context context; 36 | VertexArray vertexArray; 37 | SimpleTextureShaderProgram simpleTextureShaderProgram; 38 | private int texture; 39 | 40 | public SimpleTextureRender(Context context){ 41 | this.context = context; 42 | vertexArray = new VertexArray(CUBE); 43 | } 44 | 45 | @Override 46 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 47 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 48 | 49 | simpleTextureShaderProgram = new SimpleTextureShaderProgram(context); 50 | texture = TextureHelper.loadTexture(context, R.drawable.lena); 51 | } 52 | 53 | @Override 54 | public void onSurfaceChanged(GL10 gl, int width, int height) { 55 | glViewport(0, 0, width, height); 56 | } 57 | 58 | @Override 59 | public void onDrawFrame(GL10 gl) { 60 | glClear(GL_COLOR_BUFFER_BIT); 61 | 62 | simpleTextureShaderProgram.useProgram(); 63 | simpleTextureShaderProgram.setUniforms(texture); 64 | 65 | vertexArray.setVertexAttribPointer( 66 | 0, 67 | simpleTextureShaderProgram.getPositionAttributeLocation(), 68 | POSITION_COMPONENT_COUNT, 69 | STRIDE); 70 | 71 | vertexArray.setVertexAttribPointer( 72 | POSITION_COMPONENT_COUNT, 73 | simpleTextureShaderProgram.getTextureCoordinatesAttributeLocation(), 74 | TEXTURE_COORDINATES_COMPONENT_COUNT, 75 | STRIDE); 76 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/render/WatermarkRender.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.render; 2 | 3 | 4 | import android.content.Context; 5 | import android.opengl.GLES20; 6 | import android.opengl.GLSurfaceView; 7 | 8 | import com.andev.androidshaderdemo.data.VertexArray; 9 | import com.andev.androidshaderdemo.programs.WatermarkProgram; 10 | import com.andev.androidshaderdemo.util.TextTextureHelper; 11 | 12 | import javax.microedition.khronos.egl.EGLConfig; 13 | import javax.microedition.khronos.opengles.GL10; 14 | 15 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; 16 | import static android.opengl.GLES20.glClear; 17 | import static android.opengl.GLES20.glClearColor; 18 | import static android.opengl.GLES20.glViewport; 19 | import static com.andev.androidshaderdemo.Constants.BYTES_PER_FLOAT; 20 | 21 | 22 | public class WatermarkRender implements GLSurfaceView.Renderer{ 23 | private static final int POSITION_COMPONENT_COUNT = 2; 24 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 25 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 26 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT; 27 | 28 | public static final float CUBE[] = {//翻转顶点信息中的纹理坐标,统一用1去减 29 | -1.0f, -1.0f, 0f, 1f - 0f, 30 | 1.0f, -1.0f, 1f, 1f -0f, 31 | -1.0f, 1.0f, 0f, 1f -1f, 32 | 1.0f, 1.0f, 1f, 1f -1f, 33 | }; 34 | 35 | Context context; 36 | VertexArray vertexArray; 37 | WatermarkProgram watermarkProgram; 38 | private int texture; 39 | 40 | public WatermarkRender(Context context){ 41 | this.context = context; 42 | vertexArray = new VertexArray(CUBE); 43 | } 44 | 45 | @Override 46 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 47 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 48 | 49 | watermarkProgram = new WatermarkProgram(context); 50 | texture = TextTextureHelper.createTexture("Android Shader Demo", 220, 100, 30); 51 | } 52 | 53 | @Override 54 | public void onSurfaceChanged(GL10 gl, int width, int height) { 55 | glViewport(0, 0, width, height); 56 | } 57 | 58 | @Override 59 | public void onDrawFrame(GL10 gl) { 60 | glClear(GL_COLOR_BUFFER_BIT); 61 | 62 | glViewport(0, 0, 400, 100); 63 | 64 | watermarkProgram.useProgram(); 65 | watermarkProgram.setUniforms(texture); 66 | 67 | vertexArray.setVertexAttribPointer( 68 | 0, 69 | watermarkProgram.getPositionAttributeLocation(), 70 | POSITION_COMPONENT_COUNT, 71 | STRIDE); 72 | 73 | vertexArray.setVertexAttribPointer( 74 | POSITION_COMPONENT_COUNT, 75 | watermarkProgram.getTextureCoordinatesAttributeLocation(), 76 | TEXTURE_COORDINATES_COMPONENT_COUNT, 77 | STRIDE); 78 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/util/LoggerConfig.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "OpenGL ES for Android", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information. 8 | ***/ 9 | package com.andev.androidshaderdemo.util; 10 | 11 | public class LoggerConfig { 12 | public static final boolean ON = true; 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/util/Rotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 CyberAgent 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.andev.androidshaderdemo.util; 18 | 19 | public enum Rotation { 20 | NORMAL, ROTATION_90, ROTATION_180, ROTATION_270; 21 | 22 | /** 23 | * Retrieves the int representation of the Rotation. 24 | * 25 | * @return 0, 90, 180 or 270 26 | */ 27 | public int asInt() { 28 | switch (this) { 29 | case NORMAL: return 0; 30 | case ROTATION_90: return 90; 31 | case ROTATION_180: return 180; 32 | case ROTATION_270: return 270; 33 | default: throw new IllegalStateException("Unknown Rotation!"); 34 | } 35 | } 36 | 37 | /** 38 | * Create a Rotation from an integer. Needs to be either 0, 90, 180 or 270. 39 | * 40 | * @param rotation 0, 90, 180 or 270 41 | * @return Rotation object 42 | */ 43 | public static Rotation fromInt(int rotation) { 44 | switch (rotation) { 45 | case 0: return NORMAL; 46 | case 90: return ROTATION_90; 47 | case 180: return ROTATION_180; 48 | case 270: return ROTATION_270; 49 | case 360: return NORMAL; 50 | default: throw new IllegalStateException( 51 | rotation + " is an unknown rotation. Needs to be either 0, 90, 180 or 270!"); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/util/TextResourceReader.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.util; 2 | 3 | 4 | import android.content.Context; 5 | import android.content.res.Resources; 6 | 7 | import java.io.BufferedReader; 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.io.InputStreamReader; 11 | 12 | public class TextResourceReader { 13 | 14 | public static String readTextFileFromResource(Context context, 15 | int resourceId) { 16 | StringBuilder body = new StringBuilder(); 17 | 18 | try { 19 | InputStream inputStream = 20 | context.getResources().openRawResource(resourceId); 21 | InputStreamReader inputStreamReader = 22 | new InputStreamReader(inputStream); 23 | BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 24 | 25 | String nextLine; 26 | 27 | while ((nextLine = bufferedReader.readLine()) != null) { 28 | body.append(nextLine); 29 | body.append('\n'); 30 | } 31 | } catch (IOException e) { 32 | throw new RuntimeException( 33 | "Could not open resource: " + resourceId, e); 34 | } catch (Resources.NotFoundException nfe) { 35 | throw new RuntimeException("Resource not found: " + resourceId, nfe); 36 | } 37 | 38 | return body.toString(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/util/TextTextureHelper.java: -------------------------------------------------------------------------------- 1 | package com.andev.androidshaderdemo.util; 2 | 3 | 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.Paint; 8 | import android.opengl.GLES20; 9 | import android.util.Log; 10 | 11 | import static android.opengl.GLES20.GL_TEXTURE_2D; 12 | import static android.opengl.GLES20.glBindTexture; 13 | import static android.opengl.GLES20.glGenTextures; 14 | import static android.opengl.GLUtils.texImage2D; 15 | 16 | public class TextTextureHelper { 17 | private static final String TAG = "TextTextureHelper"; 18 | 19 | public static Bitmap createBitmap(String text, int width, int height, int textSize){ 20 | Bitmap bitmap; 21 | 22 | bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 23 | Canvas canvas = new Canvas(bitmap); 24 | 25 | Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);//消除锯齿 26 | paint.setColor(Color.argb(255, 255, 255,255)); 27 | 28 | // canvas.drawRect(0, 0, width, height, paint); 29 | // canvas.drawBitmap(bitmap, 0, 0, paint); 30 | 31 | 32 | paint.setShadowLayer(1, 0, 1, Color.DKGRAY); 33 | paint.setTextSize(textSize); 34 | paint.setTextAlign(Paint.Align.CENTER); 35 | 36 | canvas.drawText(text, width / 2,height/2 , paint); 37 | 38 | return bitmap; 39 | } 40 | 41 | public static int createTexture(String text, int width, int height, int textSize){ 42 | Bitmap bitmap = createBitmap(text, width, height, textSize); 43 | 44 | if (bitmap == null) { 45 | Log.w(TAG, "TextTextureHelper createTexture fail."); 46 | } 47 | 48 | final int[] textureObjectIds = new int[1]; 49 | glGenTextures(1, textureObjectIds, 0); 50 | 51 | glBindTexture(GL_TEXTURE_2D, textureObjectIds[0]); 52 | // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 53 | // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 54 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, 55 | GLES20.GL_CLAMP_TO_EDGE); 56 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, 57 | GLES20.GL_CLAMP_TO_EDGE); 58 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, 59 | GLES20.GL_NEAREST); 60 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, 61 | GLES20.GL_NEAREST); 62 | // Load the bitmap into the bound texture. 63 | texImage2D(GL_TEXTURE_2D, 0, bitmap, 0); 64 | 65 | bitmap.recycle(); 66 | 67 | // Unbind from the texture. 68 | glBindTexture(GL_TEXTURE_2D, 0); 69 | 70 | return textureObjectIds[0]; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/java/com/andev/androidshaderdemo/util/TextureRotationUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 CyberAgent 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.andev.androidshaderdemo.util; 18 | 19 | public class TextureRotationUtil { 20 | 21 | public static final float TEXTURE_NO_ROTATION[] = { 22 | 0.0f, 1.0f, 23 | 1.0f, 1.0f, 24 | 0.0f, 0.0f, 25 | 1.0f, 0.0f, 26 | }; 27 | 28 | public static final float TEXTURE_ROTATED_90[] = { 29 | 1.0f, 1.0f, 30 | 1.0f, 0.0f, 31 | 0.0f, 1.0f, 32 | 0.0f, 0.0f, 33 | }; 34 | public static final float TEXTURE_ROTATED_180[] = { 35 | 1.0f, 0.0f, 36 | 0.0f, 0.0f, 37 | 1.0f, 1.0f, 38 | 0.0f, 1.0f, 39 | }; 40 | public static final float TEXTURE_ROTATED_270[] = { 41 | 0.0f, 0.0f, 42 | 0.0f, 1.0f, 43 | 1.0f, 0.0f, 44 | 1.0f, 1.0f, 45 | }; 46 | 47 | public static final float CUBE[] = { 48 | -1.0f, -1.0f, 49 | 1.0f, -1.0f, 50 | -1.0f, 1.0f, 51 | 1.0f, 1.0f, 52 | }; 53 | 54 | private TextureRotationUtil() {} 55 | 56 | public static float[] getRotation(final Rotation rotation, final boolean flipHorizontal, 57 | final boolean flipVertical) { 58 | float[] rotatedTex; 59 | switch (rotation) { 60 | case ROTATION_90: 61 | rotatedTex = TEXTURE_ROTATED_90; 62 | break; 63 | case ROTATION_180: 64 | rotatedTex = TEXTURE_ROTATED_180; 65 | break; 66 | case ROTATION_270: 67 | rotatedTex = TEXTURE_ROTATED_270; 68 | break; 69 | case NORMAL: 70 | default: 71 | rotatedTex = TEXTURE_NO_ROTATION; 72 | break; 73 | } 74 | if (flipHorizontal) { 75 | rotatedTex = new float[]{ 76 | flip(rotatedTex[0]), rotatedTex[1], 77 | flip(rotatedTex[2]), rotatedTex[3], 78 | flip(rotatedTex[4]), rotatedTex[5], 79 | flip(rotatedTex[6]), rotatedTex[7], 80 | }; 81 | } 82 | if (flipVertical) { 83 | rotatedTex = new float[]{ 84 | rotatedTex[0], flip(rotatedTex[1]), 85 | rotatedTex[2], flip(rotatedTex[3]), 86 | rotatedTex[4], flip(rotatedTex[5]), 87 | rotatedTex[6], flip(rotatedTex[7]), 88 | }; 89 | } 90 | return rotatedTex; 91 | } 92 | 93 | 94 | private static float flip(final float i) { 95 | if (i == 0.0f) { 96 | return 1.0f; 97 | } 98 | return 0.0f; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /app/src/main/jni/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_CFLAGS += -D__STDC_CONSTANT_MACROS 5 | 6 | LOCAL_SRC_FILES = \ 7 | ./NativeController.cpp 8 | 9 | LOCAL_STATIC_LIBRARIES := librender 10 | 11 | LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 12 | LOCAL_LDLIBS += -lz 13 | LOCAL_LDLIBS += -landroid 14 | LOCAL_LDLIBS += -ljnigraphics 15 | # Link with OpenGL ES 16 | LOCAL_LDLIBS += -lGLESv2 17 | LOCAL_LDLIBS += -lEGL 18 | 19 | LOCAL_MODULE := libdrawdemo 20 | include $(BUILD_SHARED_LIBRARY) 21 | include $(call all-makefiles-under,$(LOCAL_PATH)) -------------------------------------------------------------------------------- /app/src/main/jni/Application.mk: -------------------------------------------------------------------------------- 1 | APP_ABI := all 2 | APP_STL := gnustl_static 3 | APP_CPPFLAGS := -std=gnu++11 -fexceptions -D__STDC_LIMIT_MACROS 4 | NDK_TOOLCHAIN_VERSION = 4.9 5 | APP_PLATFORM := android-14 -------------------------------------------------------------------------------- /app/src/main/jni/NativeController.cpp: -------------------------------------------------------------------------------- 1 | #include "com_andev_androidshaderdemo_nativedemo_NativeController.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include "./librender/preview_controller.h" 7 | 8 | #define LOG_TAG "NativeController" 9 | 10 | static ANativeWindow *window = 0; 11 | static PicPreviewController *controller = 0; 12 | 13 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_init 14 | (JNIEnv * env, jobject obj){ 15 | controller = new PicPreviewController(); 16 | controller->initialize(); 17 | } 18 | 19 | 20 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_setSurface 21 | (JNIEnv * env, jobject obj, jobject surface){ 22 | if (surface != 0 && NULL != controller) { 23 | window = ANativeWindow_fromSurface(env, surface); 24 | controller->setWindow(window); 25 | } else if (window != 0) { 26 | ANativeWindow_release(window); 27 | window = 0; 28 | } 29 | } 30 | 31 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_resetSize 32 | (JNIEnv * env, jobject obj, jint width, jint height){ 33 | if (NULL != controller) { 34 | controller->resetSize(width, height); 35 | } 36 | } 37 | 38 | 39 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_showBitmap 40 | (JNIEnv * env, jobject obj, jobject bitmap){ 41 | 42 | int ret = 0; 43 | AndroidBitmapInfo bitmapInfo; 44 | void *pixels = NULL; 45 | 46 | int imgWidth = 0; 47 | int imgHeight = 0; 48 | 49 | if ((ret = AndroidBitmap_getInfo(env, bitmap, &bitmapInfo)) < 0) { 50 | return; 51 | } 52 | 53 | LOGI("showBitmap width %d, height %d, format %d", bitmapInfo.width, bitmapInfo.height, bitmapInfo.format); 54 | 55 | imgWidth = bitmapInfo.width; 56 | imgHeight = bitmapInfo.height; 57 | 58 | if (bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { 59 | LOGI("Java_com_example_hellojni_HelloJni_showBitmap invalid rgb format"); 60 | return; 61 | } 62 | 63 | if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) { 64 | LOGI("AndroidBitmap_lockPixels() failed ! error=%d", ret); 65 | } 66 | 67 | controller->loadTexture((byte*)pixels, imgWidth, imgHeight); 68 | controller->draw(); 69 | 70 | AndroidBitmap_unlockPixels(env, bitmap); 71 | } 72 | 73 | 74 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_stop 75 | (JNIEnv * env, jobject ob){ 76 | 77 | } 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /app/src/main/jni/com_andev_androidshaderdemo_nativedemo_NativeController.h: -------------------------------------------------------------------------------- 1 | /* DO NOT EDIT THIS FILE - it is machine generated */ 2 | #include 3 | /* Header for class com_andev_androidshaderdemo_nativedemo_NativeController */ 4 | 5 | #ifndef _Included_com_andev_androidshaderdemo_nativedemo_NativeController 6 | #define _Included_com_andev_androidshaderdemo_nativedemo_NativeController 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | /* 11 | * Class: com_andev_androidshaderdemo_nativedemo_NativeController 12 | * Method: init 13 | * Signature: ()V 14 | */ 15 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_init 16 | (JNIEnv *, jobject); 17 | 18 | /* 19 | * Class: com_andev_androidshaderdemo_nativedemo_NativeController 20 | * Method: setSurface 21 | * Signature: (Landroid/view/Surface;)V 22 | */ 23 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_setSurface 24 | (JNIEnv *, jobject, jobject); 25 | 26 | /* 27 | * Class: com_andev_androidshaderdemo_nativedemo_NativeController 28 | * Method: resetSize 29 | * Signature: (II)V 30 | */ 31 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_resetSize 32 | (JNIEnv *, jobject, jint, jint); 33 | 34 | /* 35 | * Class: com_andev_androidshaderdemo_nativedemo_NativeController 36 | * Method: showBitmap 37 | * Signature: (Landroid/graphics/Bitmap;)V 38 | */ 39 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_showBitmap 40 | (JNIEnv *, jobject, jobject); 41 | 42 | /* 43 | * Class: com_andev_androidshaderdemo_nativedemo_NativeController 44 | * Method: stop 45 | * Signature: ()V 46 | */ 47 | JNIEXPORT void JNICALL Java_com_andev_androidshaderdemo_nativedemo_NativeController_stop 48 | (JNIEnv *, jobject); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | #endif 54 | -------------------------------------------------------------------------------- /app/src/main/jni/librender/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_ARM_MODE := arm 5 | 6 | LOCAL_CFLAGS := -DHAVE_CONFIG_H -DFPM_ARM -ffast-math -O3 7 | 8 | LOCAL_SRC_FILES += \ 9 | ./egl_core.cpp \ 10 | ./preview_controller.cpp \ 11 | ./pic_preview_texture.cpp \ 12 | ./preview_render.cpp \ 13 | 14 | LOCAL_MODULE := librender 15 | include $(BUILD_STATIC_LIBRARY) -------------------------------------------------------------------------------- /app/src/main/jni/librender/egl_core.cpp: -------------------------------------------------------------------------------- 1 | #include "egl_core.h" 2 | 3 | #define LOG_TAG "EGLCore" 4 | 5 | EGLCore::EGLCore() { 6 | display = EGL_NO_DISPLAY; 7 | context = EGL_NO_CONTEXT; 8 | } 9 | 10 | EGLCore::~EGLCore() { 11 | } 12 | 13 | void EGLCore::release() { 14 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 15 | LOGI("after eglMakeCurrent..."); 16 | eglDestroyContext(display, context); 17 | LOGI("after eglDestroyContext..."); 18 | display = EGL_NO_DISPLAY; 19 | context = EGL_NO_CONTEXT; 20 | } 21 | 22 | void EGLCore::releaseSurface(EGLSurface eglSurface) { 23 | eglDestroySurface(display, eglSurface); 24 | eglSurface = EGL_NO_SURFACE; 25 | } 26 | 27 | EGLContext EGLCore::getContext(){ 28 | LOGI("return EGLCore getContext..."); 29 | return context; 30 | } 31 | 32 | EGLDisplay EGLCore::getDisplay(){ 33 | return display; 34 | } 35 | 36 | EGLConfig EGLCore::getConfig(){ 37 | return config; 38 | } 39 | 40 | EGLSurface EGLCore::createWindowSurface(ANativeWindow* _window) { 41 | EGLSurface surface = NULL; 42 | EGLint format; 43 | if (!eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format)) { 44 | LOGE("eglGetConfigAttrib() returned error %d", eglGetError()); 45 | release(); 46 | return surface; 47 | } 48 | ANativeWindow_setBuffersGeometry(_window, 0, 0, format); 49 | if (!(surface = eglCreateWindowSurface(display, config, _window, 0))) { 50 | LOGE("eglCreateWindowSurface() returned error %d", eglGetError()); 51 | } 52 | return surface; 53 | } 54 | 55 | bool EGLCore::swapBuffers(EGLSurface eglSurface) { 56 | return eglSwapBuffers(display, eglSurface); 57 | } 58 | 59 | bool EGLCore::makeCurrent(EGLSurface eglSurface) { 60 | return eglMakeCurrent(display, eglSurface, eglSurface, context); 61 | } 62 | 63 | void EGLCore::doneCurrent() { 64 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 65 | } 66 | 67 | bool EGLCore::init() { 68 | return this->init(NULL); 69 | } 70 | 71 | bool EGLCore::init(EGLContext sharedContext) { 72 | EGLint numConfigs; 73 | EGLint width; 74 | EGLint height; 75 | 76 | const EGLint attribs[] = { EGL_BUFFER_SIZE, 32, EGL_ALPHA_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 77 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; 78 | 79 | if ((display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY) { 80 | LOGE("eglGetDisplay() returned error %d", eglGetError()); 81 | return false; 82 | } 83 | if (!eglInitialize(display, 0, 0)) { 84 | LOGE("eglInitialize() returned error %d", eglGetError()); 85 | return false; 86 | } 87 | 88 | if (!eglChooseConfig(display, attribs, &config, 1, &numConfigs)) { 89 | LOGE("eglChooseConfig() returned error %d", eglGetError()); 90 | release(); 91 | return false; 92 | } 93 | 94 | EGLint eglContextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; 95 | if (!(context = eglCreateContext(display, config, NULL == sharedContext ? EGL_NO_CONTEXT : sharedContext, eglContextAttributes))) { 96 | LOGE("eglCreateContext() returned error %d", eglGetError()); 97 | release(); 98 | return false; 99 | } 100 | 101 | return true; 102 | } 103 | 104 | 105 | -------------------------------------------------------------------------------- /app/src/main/jni/librender/egl_core.h: -------------------------------------------------------------------------------- 1 | #ifndef ANDROIDSHADERDEMO_EGL_CORE_H 2 | #define ANDROIDSHADERDEMO_EGL_CORE_H 3 | 4 | #include "./../libcommon/CommonTools.h" 5 | #include 6 | #include 7 | #include 8 | 9 | class EGLCore { 10 | public: 11 | EGLCore(); 12 | virtual ~EGLCore(); 13 | 14 | bool init(); 15 | 16 | bool init(EGLContext sharedContext); 17 | 18 | EGLSurface createWindowSurface(ANativeWindow* _window); 19 | 20 | bool makeCurrent(EGLSurface eglSurface); 21 | 22 | void doneCurrent(); 23 | 24 | bool swapBuffers(EGLSurface eglSurface); 25 | 26 | void releaseSurface(EGLSurface eglSurface); 27 | void release(); 28 | 29 | EGLContext getContext(); 30 | EGLDisplay getDisplay(); 31 | EGLConfig getConfig(); 32 | 33 | private: 34 | EGLDisplay display; 35 | EGLConfig config; 36 | EGLContext context; 37 | }; 38 | 39 | 40 | 41 | 42 | #endif //ANDROIDSHADERDEMO_EGL_CORE_H 43 | -------------------------------------------------------------------------------- /app/src/main/jni/librender/pic_preview_texture.cpp: -------------------------------------------------------------------------------- 1 | #include "./pic_preview_texture.h" 2 | 3 | #define LOG_TAG "PicPreviewTexture" 4 | 5 | PicPreviewTexture::PicPreviewTexture() { 6 | } 7 | 8 | PicPreviewTexture::~PicPreviewTexture() { 9 | } 10 | 11 | bool PicPreviewTexture::createTexture() { 12 | LOGI("enter PicPreviewTexture::createTexture"); 13 | texture = 0; 14 | int ret = initTexture(); 15 | if (ret < 0) { 16 | LOGI("init texture failed..."); 17 | this->dealloc(); 18 | return false; 19 | } 20 | return true; 21 | } 22 | 23 | void PicPreviewTexture::updateTexImage(byte* pixels, int frameWidth, int frameHeight) { 24 | if (pixels) { 25 | glActiveTexture(GL_TEXTURE0); 26 | glBindTexture(GL_TEXTURE_2D, texture); 27 | if (checkGlError("glBindTexture")) { 28 | return; 29 | } 30 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, frameWidth, frameHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 31 | } 32 | } 33 | 34 | bool PicPreviewTexture::bindTexture(GLint uniformSampler) { 35 | glActiveTexture(GL_TEXTURE0); 36 | glBindTexture(GL_TEXTURE_2D, texture); 37 | if (checkGlError("glBindTexture")) { 38 | return false; 39 | } 40 | glUniform1i(uniformSampler, 0); 41 | return true; 42 | } 43 | 44 | int PicPreviewTexture::initTexture() { 45 | glGenTextures(1, &texture); 46 | glBindTexture(GL_TEXTURE_2D, texture); 47 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 48 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 49 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 50 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 51 | return 1; 52 | } 53 | 54 | bool PicPreviewTexture::checkGlError(const char* op) { 55 | GLint error; 56 | for (error = glGetError(); error; error = glGetError()) { 57 | LOGI("error::after %s() glError (0x%x)\n", op, error); 58 | return true; 59 | } 60 | return false; 61 | } 62 | 63 | void PicPreviewTexture::dealloc() { 64 | LOGI("enter PicPreviewTexture::dealloc"); 65 | if (texture) { 66 | glDeleteTextures(1, &texture); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/jni/librender/pic_preview_texture.h: -------------------------------------------------------------------------------- 1 | #ifndef PIC_PREVIEW_TEXTURE_H 2 | #define PIC_PREVIEW_TEXTURE_H 3 | #include "./../libcommon/CommonTools.h" 4 | #include 5 | #include 6 | 7 | class PicPreviewTexture { 8 | private: 9 | GLuint texture; 10 | 11 | int initTexture(); 12 | 13 | bool checkGlError(const char* op); 14 | public: 15 | PicPreviewTexture(); 16 | virtual ~PicPreviewTexture(); 17 | 18 | bool createTexture(); 19 | void updateTexImage(byte* pixels, int width, int height); 20 | bool bindTexture(GLint uniformSampler); 21 | void dealloc(); 22 | }; 23 | 24 | #endif //PIC_PREVIEW_TEXTURE_H 25 | -------------------------------------------------------------------------------- /app/src/main/jni/librender/preview_controller.cpp: -------------------------------------------------------------------------------- 1 | #include "preview_controller.h" 2 | 3 | 4 | #define LOG_TAG "PreviewController" 5 | 6 | PicPreviewController::PicPreviewController() : 7 | previewSurface(0), eglCore(0) { 8 | LOGI("VideoDutePlayerController instance created"); 9 | renderer = new PicPreviewRender(); 10 | this->screenWidth = 720; 11 | this->screenHeight = 720; 12 | } 13 | 14 | PicPreviewController::~PicPreviewController() { 15 | LOGI("VideoDutePlayerController instance destroyed"); 16 | } 17 | 18 | void PicPreviewController::setWindow(ANativeWindow *window) { 19 | _window = window; 20 | 21 | previewSurface = eglCore->createWindowSurface(_window); 22 | eglCore->makeCurrent(previewSurface); 23 | LOGI("Initializing window Success"); 24 | } 25 | 26 | void PicPreviewController::resetSize(int width, int height) { 27 | LOGI("VideoDutePlayerController::resetSize width:%d; height:%d", width, height); 28 | this->screenWidth = width; 29 | this->screenHeight = height; 30 | renderer->resetRenderSize(0, 0, width, height); 31 | } 32 | 33 | void PicPreviewController::initialize() { 34 | eglCore = new EGLCore(); 35 | eglCore->init(); 36 | LOGI("Initializing context Success"); 37 | } 38 | 39 | void PicPreviewController::loadTexture(byte* pixels, int frameWidth, int frameHeight) { 40 | picPreviewTexture = new PicPreviewTexture(); 41 | bool createTexFlag = picPreviewTexture->createTexture(); 42 | if(!createTexFlag){ 43 | LOGI("createTexFlag is failed..."); 44 | destroy(); 45 | return; 46 | } 47 | 48 | picPreviewTexture->updateTexImage(pixels, frameWidth, frameHeight); 49 | 50 | bool isRendererInitialized = renderer->init(screenWidth, screenHeight, picPreviewTexture); 51 | if (!isRendererInitialized) { 52 | LOGI("Renderer failed on initialized..."); 53 | return; 54 | } 55 | LOGI("loadTexture Success"); 56 | } 57 | 58 | void PicPreviewController::draw() { 59 | drawFrame(); 60 | } 61 | 62 | void PicPreviewController::destroy() { 63 | LOGI("dealloc renderer ..."); 64 | 65 | if(eglCore){ 66 | eglCore->releaseSurface(previewSurface); 67 | eglCore->release(); 68 | eglCore = NULL; 69 | } 70 | return; 71 | } 72 | 73 | void PicPreviewController::drawFrame() { 74 | renderer->render(); 75 | 76 | if (!eglCore->swapBuffers(previewSurface)) { 77 | LOGE("eglSwapBuffers() returned error %d", eglGetError()); 78 | } 79 | } 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /app/src/main/jni/librender/preview_controller.h: -------------------------------------------------------------------------------- 1 | #ifndef ANDROIDSHADERDEMO_PREVIEW_CONTROLLER_H 2 | #define ANDROIDSHADERDEMO_PREVIEW_CONTROLLER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "egl_core.h" 8 | #include "preview_render.h" 9 | #include "pic_preview_texture.h" 10 | 11 | class PicPreviewController { 12 | public: 13 | PicPreviewController(); 14 | virtual ~PicPreviewController(); 15 | 16 | void initialize(); 17 | void setWindow(ANativeWindow* window); 18 | void resetSize(int width, int height); 19 | void loadTexture(byte* pixels, int frameWidth, int frameHeight); 20 | void draw(); 21 | private: 22 | PicPreviewRender* renderer; 23 | PicPreviewTexture* picPreviewTexture; 24 | 25 | int screenWidth; 26 | int screenHeight; 27 | // android window, supported by NDK r5 and newer 28 | ANativeWindow* _window; 29 | 30 | EGLCore* eglCore; 31 | EGLSurface previewSurface; 32 | 33 | void drawFrame(); 34 | void destroy(); 35 | 36 | }; 37 | 38 | 39 | #endif //ANDROIDSHADERDEMO_PREVIEW_CONTROLLER_H 40 | -------------------------------------------------------------------------------- /app/src/main/jni/librender/preview_render.h: -------------------------------------------------------------------------------- 1 | #ifndef ANDROIDSHADERDEMO_PREVIEW_RENDER_H 2 | #define ANDROIDSHADERDEMO_PREVIEW_RENDER_H 3 | 4 | #include "./../libcommon/CommonTools.h" 5 | #include 6 | #include 7 | #include "pic_preview_texture.h" 8 | 9 | //Shader.frag文件内容 10 | static const char* PIC_PREVIEW_FRAG_SHADER_2 = 11 | "varying highp vec2 v_texcoord;\n" 12 | "uniform sampler2D yuvTexSampler;\n" 13 | "void main() {\n" 14 | " gl_FragColor = texture2D(yuvTexSampler, v_texcoord);\n" 15 | "}\n"; 16 | 17 | //Shader.vert文件内容 18 | static const char* PIC_PREVIEW_VERTEX_SHADER_2 = 19 | "attribute vec4 position; \n" 20 | "attribute vec2 texcoord; \n" 21 | "varying vec2 v_texcoord; \n" 22 | "void main(void) \n" 23 | "{ \n" 24 | " gl_Position = position; \n" 25 | " v_texcoord = texcoord; \n" 26 | "} \n"; 27 | 28 | class PicPreviewRender { 29 | protected: 30 | GLint _backingLeft; 31 | GLint _backingTop; 32 | GLint _backingWidth; 33 | GLint _backingHeight; 34 | 35 | GLuint vertShader; 36 | GLuint fragShader; 37 | 38 | PicPreviewTexture* picPreviewTexture; 39 | GLint uniformSampler; 40 | GLuint program; 41 | 42 | int useProgram(); 43 | int initShaders(); 44 | GLuint compileShader(GLenum type, const char *sources); 45 | bool checkGlError(const char* op); 46 | 47 | public: 48 | PicPreviewRender(); 49 | virtual ~PicPreviewRender(); 50 | virtual bool init(int width, int height, PicPreviewTexture* picPreviewTexture); 51 | virtual void render(); 52 | virtual void dealloc(); 53 | void resetRenderSize(int left, int top, int width, int height); 54 | }; 55 | 56 | 57 | #endif //ANDROIDSHADERDEMO_PREVIEW_RENDER_H 58 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/baboon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/baboon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/edgeburn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/edgeburn.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/face.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/face.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/fairy_tale.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/fairy_tale.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/hefegradientmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/hefegradientmap.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/hefemap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/hefemap.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/hefemetal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/hefemetal.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/hefesoftlight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/hefesoftlight.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/lena.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/lena.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/lena1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/lena1.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/lookup_vertigo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/lookup_vertigo.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/test.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/test2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/test2.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/test3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/test3.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/test6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andev009/AndroidShaderDemo/c86f19cdd06f5446744978f507fcb0caca54a897/app/src/main/res/drawable/test6.jpg -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/layout/camera_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/filter_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 18 | 19 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/java_egl_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/native_activity.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/layout/record_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 13 | 14 |