├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── assets
│ │ │ ├── vr
│ │ │ │ ├── 360sp.jpg
│ │ │ │ ├── skysphere.frag
│ │ │ │ └── skysphere.vert
│ │ │ ├── 3dres
│ │ │ │ ├── eye1.png
│ │ │ │ ├── mouth1.png
│ │ │ │ ├── pikagen.png
│ │ │ │ ├── Char_Patrick.png
│ │ │ │ ├── obj.frag
│ │ │ │ ├── pikachu.mtl
│ │ │ │ ├── patrick.mtl
│ │ │ │ ├── obj2.frag
│ │ │ │ ├── obj.vert
│ │ │ │ └── obj2.vert
│ │ │ ├── etczip
│ │ │ │ └── cc.zip
│ │ │ ├── lookup
│ │ │ │ ├── purity.png
│ │ │ │ ├── amatorka.png
│ │ │ │ ├── highkey.png
│ │ │ │ ├── lookup.vert
│ │ │ │ └── lookup.frag
│ │ │ ├── texture
│ │ │ │ └── fengj.png
│ │ │ ├── vary
│ │ │ │ ├── fragment.sh
│ │ │ │ └── vertex.sh
│ │ │ ├── fshader
│ │ │ │ ├── Cone.sh
│ │ │ │ ├── Texture.sh
│ │ │ │ ├── BallWithLight.sh
│ │ │ │ └── Beauty.sh
│ │ │ ├── vshader
│ │ │ │ ├── SingleColor.sh
│ │ │ │ ├── ImageDefault.sh
│ │ │ │ ├── Texture.sh
│ │ │ │ ├── Cone.sh
│ │ │ │ ├── Ball.sh
│ │ │ │ └── BallWithLight.sh
│ │ │ ├── light
│ │ │ │ ├── light.frag
│ │ │ │ └── light.vert
│ │ │ ├── shader
│ │ │ │ ├── base_fragment.sh
│ │ │ │ ├── pkm_mul.vert
│ │ │ │ ├── base_vertex.sh
│ │ │ │ ├── oes
│ │ │ │ │ ├── default_vertex.sh
│ │ │ │ │ └── default_fragment.sh
│ │ │ │ ├── oes_base_fragment.sh
│ │ │ │ ├── color
│ │ │ │ │ └── gray_fragment.frag
│ │ │ │ ├── pkm_mul.frag
│ │ │ │ ├── oes_base_vertex.sh
│ │ │ │ └── beauty
│ │ │ │ │ ├── beauty.vert
│ │ │ │ │ └── beauty.frag
│ │ │ ├── filter
│ │ │ │ ├── default_fragment.sh
│ │ │ │ ├── default_vertex.sh
│ │ │ │ ├── fugu_fragment.sh
│ │ │ │ ├── half_color_vertex.sh
│ │ │ │ ├── color_fragment.sh
│ │ │ │ └── half_color_fragment.sh
│ │ │ └── camera
│ │ │ │ ├── camera_fragment.sh
│ │ │ │ └── camera_vertex.sh
│ │ ├── ic_launcher-web.png
│ │ ├── res
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── bg.jpg
│ │ │ │ ├── src.png
│ │ │ │ ├── logo.png
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── btn_camera_all.png
│ │ │ │ └── btn_camera_all_click.png
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── layout
│ │ │ │ ├── activity_vary.xml
│ │ │ │ ├── glview.xml
│ │ │ │ ├── titlebar.xml
│ │ │ │ ├── activity_opengl.xml
│ │ │ │ ├── activity_camera.xml
│ │ │ │ ├── activity_obj.xml
│ │ │ │ ├── activity_picture.xml
│ │ │ │ ├── item_button.xml
│ │ │ │ ├── item_text.xml
│ │ │ │ ├── activity_list.xml
│ │ │ │ ├── item_choose.xml
│ │ │ │ ├── activity_ani.xml
│ │ │ │ ├── activity_egl_back_env.xml
│ │ │ │ ├── activity_zip.xml
│ │ │ │ ├── activity_fbo.xml
│ │ │ │ ├── activity_fglview.xml
│ │ │ │ ├── activity_camera2.xml
│ │ │ │ ├── activity_camera3.xml
│ │ │ │ ├── activity_light.xml
│ │ │ │ ├── activity_blend.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable
│ │ │ │ ├── btn_shutter_background.xml
│ │ │ │ └── btn_bg.xml
│ │ │ ├── values
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── styles.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── strings.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── menu
│ │ │ │ └── menu_filter.xml
│ │ ├── java
│ │ │ └── edu
│ │ │ │ └── wuwang
│ │ │ │ └── opengl
│ │ │ │ ├── camera
│ │ │ │ ├── FrameCallback.java
│ │ │ │ ├── Renderer.java
│ │ │ │ ├── IAiyaCamera.java
│ │ │ │ ├── ICamera.java
│ │ │ │ ├── Camera3Activity.java
│ │ │ │ ├── CameraActivity.java
│ │ │ │ ├── CameraView.java
│ │ │ │ ├── TextureFilter.java
│ │ │ │ └── CameraDrawer.java
│ │ │ │ ├── egl
│ │ │ │ ├── GlError.java
│ │ │ │ ├── GLES20BackEnv.java
│ │ │ │ └── EGLBackEnvActivity.java
│ │ │ │ ├── BaseActivity.java
│ │ │ │ ├── etc
│ │ │ │ ├── StateChangeListener.java
│ │ │ │ ├── ZipActivity.java
│ │ │ │ └── ZipAniView.java
│ │ │ │ ├── filter
│ │ │ │ ├── NoFilter.java
│ │ │ │ ├── GrayFilter.java
│ │ │ │ ├── OesFilter.java
│ │ │ │ ├── CameraFilter.java
│ │ │ │ ├── LookupFilter.java
│ │ │ │ ├── Beauty.java
│ │ │ │ └── WaterMarkFilter.java
│ │ │ │ ├── render
│ │ │ │ ├── Shape.java
│ │ │ │ ├── FGLView.java
│ │ │ │ ├── FGLRender.java
│ │ │ │ ├── FGLViewActivity.java
│ │ │ │ ├── Cone.java
│ │ │ │ ├── Triangle.java
│ │ │ │ ├── Cylinder.java
│ │ │ │ ├── BallWithLight.java
│ │ │ │ ├── Ball.java
│ │ │ │ └── TriangleWithCamera.java
│ │ │ │ ├── blend
│ │ │ │ └── SquareRelativeLayout.java
│ │ │ │ ├── obj
│ │ │ │ ├── MtlInfo.java
│ │ │ │ ├── ObjLoadActivity.java
│ │ │ │ ├── ObjLoadActivity2.java
│ │ │ │ ├── Obj3D.java
│ │ │ │ ├── ObjFilter.java
│ │ │ │ └── ObjFilter2.java
│ │ │ │ ├── vary
│ │ │ │ ├── VaryActivity.java
│ │ │ │ ├── VaryRender.java
│ │ │ │ └── Cube.java
│ │ │ │ ├── image
│ │ │ │ ├── filter
│ │ │ │ │ ├── ContrastColorFilter.java
│ │ │ │ │ └── ColorFilter.java
│ │ │ │ ├── SGLView.java
│ │ │ │ ├── SGLRender.java
│ │ │ │ └── SGLViewActivity.java
│ │ │ │ ├── vr
│ │ │ │ ├── MatrixHelper.java
│ │ │ │ └── VrContextActivity.java
│ │ │ │ ├── utils
│ │ │ │ ├── PermissionUtils.java
│ │ │ │ ├── VaryTools.java
│ │ │ │ ├── EasyGlUtils.java
│ │ │ │ └── ShaderUtils.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ └── ChooseActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── edu
│ │ │ └── wuwang
│ │ │ └── opengl
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── edu
│ │ └── wuwang
│ │ └── opengl
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── ken.md
├── .idea
├── copyright
│ └── profiles_settings.xml
├── vcs.xml
├── runConfigurations.xml
├── modules.xml
├── gradle.xml
├── compiler.xml
├── misc.xml
└── encodings.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── README.md
└── gradlew.bat
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/ken.md:
--------------------------------------------------------------------------------
1 | 1. ETC1 将Alpha与RGB合并为一张图片时候,图片拉升滤波采样会导致边界颜色错误
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/assets/vr/360sp.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/vr/360sp.jpg
--------------------------------------------------------------------------------
/app/src/main/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/ic_launcher-web.png
--------------------------------------------------------------------------------
/app/src/main/assets/3dres/eye1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/3dres/eye1.png
--------------------------------------------------------------------------------
/app/src/main/assets/etczip/cc.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/etczip/cc.zip
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/assets/3dres/mouth1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/3dres/mouth1.png
--------------------------------------------------------------------------------
/app/src/main/assets/3dres/pikagen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/3dres/pikagen.png
--------------------------------------------------------------------------------
/app/src/main/assets/lookup/purity.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/lookup/purity.png
--------------------------------------------------------------------------------
/app/src/main/assets/texture/fengj.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/texture/fengj.png
--------------------------------------------------------------------------------
/app/src/main/assets/vary/fragment.sh:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 | varying vec4 vColor;
3 | void main() {
4 | gl_FragColor =vColor;
5 | }
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/bg.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-xhdpi/bg.jpg
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/src.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-xhdpi/src.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/app/src/main/assets/fshader/Cone.sh:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 | varying vec4 vColor;
3 |
4 | void main(){
5 | gl_FragColor=vColor;
6 | }
--------------------------------------------------------------------------------
/app/src/main/assets/lookup/amatorka.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/lookup/amatorka.png
--------------------------------------------------------------------------------
/app/src/main/assets/lookup/highkey.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/lookup/highkey.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-xhdpi/logo.png
--------------------------------------------------------------------------------
/app/src/main/assets/3dres/Char_Patrick.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/assets/3dres/Char_Patrick.png
--------------------------------------------------------------------------------
/app/src/main/assets/vshader/SingleColor.sh:
--------------------------------------------------------------------------------
1 | uniform mat4 vMatrix;
2 | attribute vec4 vPosition;
3 |
4 | void main(){
5 | gl_Position=vPosition;
6 | }
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/btn_camera_all.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-xhdpi/btn_camera_all.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/btn_camera_all_click.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/doggycoder/AndroidOpenGLDemo/HEAD/app/src/main/res/mipmap-xhdpi/btn_camera_all_click.png
--------------------------------------------------------------------------------
/app/src/main/assets/light/light.frag:
--------------------------------------------------------------------------------
1 | precision highp float;
2 |
3 | //uniform sampler2D uTexture;
4 | varying vec4 vColor;
5 |
6 | void main(){
7 | gl_FragColor=vColor;
8 | }
--------------------------------------------------------------------------------
/app/src/main/assets/fshader/Texture.sh:
--------------------------------------------------------------------------------
1 | precision lowp float;
2 |
3 | varying vec2 vCoord;
4 | uniform sampler2D vTexture;
5 | void main()
6 | {
7 | gl_FragColor = texture2D(vTexture, vCoord);
8 | }
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/assets/vshader/ImageDefault.sh:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | attribute vec2 aCoord;
3 |
4 | varying vec2 vCoord;
5 |
6 | void main(){
7 | gl_Position = vPosition;
8 | vCoord = aCoord;
9 | }
--------------------------------------------------------------------------------
/app/src/main/assets/vary/vertex.sh:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | uniform mat4 vMatrix;
3 | varying vec4 vColor;
4 | attribute vec4 aColor;
5 | void main() {
6 | gl_Position = vMatrix*vPosition;
7 | vColor=aColor;
8 | }
--------------------------------------------------------------------------------
/app/src/main/assets/shader/base_fragment.sh:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 | varying vec2 textureCoordinate;
3 | uniform sampler2D vTexture;
4 | void main() {
5 | gl_FragColor = texture2D( vTexture, textureCoordinate );
6 | }
--------------------------------------------------------------------------------
/app/src/main/assets/vshader/Texture.sh:
--------------------------------------------------------------------------------
1 | uniform mat4 vMatrix;
2 |
3 | attribute vec4 vPosition;
4 | attribute vec2 aCoord;
5 | varying vec2 vCoord;
6 | void main(){
7 | gl_Position = vPosition;
8 | vCoord = aCoord;
9 | }
--------------------------------------------------------------------------------
/app/src/main/assets/vr/skysphere.frag:
--------------------------------------------------------------------------------
1 | precision highp float;
2 |
3 | uniform sampler2D uTexture;
4 | varying vec2 vCoordinate;
5 |
6 | void main(){
7 | vec4 color=texture2D(uTexture,vCoordinate);
8 | gl_FragColor=color;
9 | }
--------------------------------------------------------------------------------
/app/src/main/assets/shader/pkm_mul.vert:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | attribute vec2 vCoord;
3 | varying vec2 aCoord;
4 | uniform mat4 vMatrix;
5 |
6 | void main(){
7 | aCoord = vCoord;
8 | gl_Position = vMatrix*vPosition;
9 | }
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/camera/FrameCallback.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.camera;
2 |
3 | /**
4 | * Description:
5 | */
6 | public interface FrameCallback {
7 |
8 | void onFrame(byte[] bytes, long time);
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/assets/filter/default_fragment.sh:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 |
3 | uniform sampler2D vTexture;
4 | uniform int vIsHalf;
5 |
6 | varying vec2 aCoordinate;
7 |
8 | void main(){
9 | gl_FragColor=texture2D(vTexture,aCoordinate);
10 | }
--------------------------------------------------------------------------------
/app/src/main/assets/fshader/BallWithLight.sh:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 | varying vec4 vDiffuse;//接收从顶点着色器过来的散射光分量
3 | void main(){
4 | vec4 finalColor=vec4(1.0);
5 | //给此片元颜色值
6 | gl_FragColor=finalColor*vDiffuse+finalColor*vec4(0.15,0.15,0.15,1.0);
7 | }
--------------------------------------------------------------------------------
/app/src/main/assets/filter/default_vertex.sh:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | attribute vec2 vCoordinate;
3 | uniform mat4 vMatrix;
4 |
5 | varying vec2 aCoordinate;
6 |
7 | void main(){
8 | gl_Position=vMatrix*vPosition;
9 | aCoordinate=vCoordinate;
10 | }
--------------------------------------------------------------------------------
/app/src/main/assets/lookup/lookup.vert:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | attribute vec2 vCoord;
3 | uniform mat4 vMatrix;
4 |
5 | varying vec2 textureCoordinate;
6 |
7 | void main(){
8 | gl_Position = vMatrix*vPosition;
9 | textureCoordinate = vCoord;
10 | }
--------------------------------------------------------------------------------
/app/src/main/assets/shader/base_vertex.sh:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | attribute vec2 vCoord;
3 | uniform mat4 vMatrix;
4 |
5 | varying vec2 textureCoordinate;
6 |
7 | void main(){
8 | gl_Position = vMatrix*vPosition;
9 | textureCoordinate = vCoord;
10 | }
--------------------------------------------------------------------------------
/app/src/main/assets/shader/oes/default_vertex.sh:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | attribute vec2 vCoordinate;
3 | uniform mat4 vMatrix;
4 |
5 | varying vec2 aCoordinate;
6 |
7 | void main(){
8 | gl_Position=vMatrix*vPosition;
9 | aCoordinate=vCoordinate;
10 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_vary.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/assets/camera/camera_fragment.sh:
--------------------------------------------------------------------------------
1 | #extension GL_OES_EGL_image_external : require
2 | precision mediump float;
3 | varying vec2 textureCoordinate;
4 | uniform samplerExternalOES s_texture;
5 | void main() {
6 | gl_FragColor = texture2D( s_texture, textureCoordinate );
7 | }
--------------------------------------------------------------------------------
/app/src/main/assets/shader/oes_base_fragment.sh:
--------------------------------------------------------------------------------
1 | #extension GL_OES_EGL_image_external : require
2 | precision mediump float;
3 | varying vec2 textureCoordinate;
4 | uniform samplerExternalOES vTexture;
5 | void main() {
6 | gl_FragColor = texture2D( vTexture, textureCoordinate );
7 | }
--------------------------------------------------------------------------------
/app/src/main/assets/shader/oes/default_fragment.sh:
--------------------------------------------------------------------------------
1 | #extension GL_OES_EGL_image_external : require
2 | precision mediump float;
3 | varying vec2 textureCoordinate;
4 | uniform samplerExternalOES vTexture;
5 | void main() {
6 | gl_FragColor = texture2D( vTexture, textureCoordinate );
7 | }
--------------------------------------------------------------------------------
/app/src/main/assets/camera/camera_vertex.sh:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | uniform mat4 vMatrix;
3 | attribute vec2 inputTextureCoordinate;
4 | varying vec2 textureCoordinate;
5 |
6 | void main(){
7 | gl_Position = vMatrix*vPosition;
8 | textureCoordinate = inputTextureCoordinate;
9 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/assets/shader/color/gray_fragment.frag:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 | varying vec2 textureCoordinate;
3 | uniform sampler2D vTexture;
4 | void main() {
5 | vec4 color=texture2D( vTexture, textureCoordinate);
6 | float rgb=color.g;
7 | vec4 c=vec4(rgb,rgb,rgb,color.a);
8 | gl_FragColor = c;
9 | }
--------------------------------------------------------------------------------
/app/src/main/assets/shader/pkm_mul.frag:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 | varying vec2 aCoord;
3 | uniform sampler2D vTexture;
4 | uniform sampler2D vTextureAlpha;
5 |
6 | void main() {
7 | vec4 color=texture2D( vTexture, aCoord);
8 | color.a=texture2D(vTextureAlpha,aCoord).r;
9 | gl_FragColor = color;
10 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/glview.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
--------------------------------------------------------------------------------
/app/src/main/assets/3dres/obj.frag:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 | varying vec2 textureCoordinate;
3 | uniform sampler2D vTexture;
4 | varying vec4 vDiffuse;//接收从顶点着色器过来的散射光分量
5 | void main() {
6 | vec4 finalColor=vec4(1.0);
7 | //给此片元颜色值
8 | gl_FragColor=finalColor*vDiffuse+finalColor*vec4(0.15,0.15,0.15,1.0);
9 | }
--------------------------------------------------------------------------------
/app/src/main/assets/shader/oes_base_vertex.sh:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | attribute vec2 vCoord;
3 | uniform mat4 vMatrix;
4 | uniform mat4 vCoordMatrix;
5 | varying vec2 textureCoordinate;
6 |
7 | void main(){
8 | gl_Position = vMatrix*vPosition;
9 | textureCoordinate = (vCoordMatrix*vec4(vCoord,0,1)).xy;
10 | }
--------------------------------------------------------------------------------
/app/src/main/assets/vshader/Cone.sh:
--------------------------------------------------------------------------------
1 | uniform mat4 vMatrix;
2 | varying vec4 vColor;
3 | attribute vec4 vPosition;
4 |
5 | void main(){
6 | gl_Position=vMatrix*vPosition;
7 | if(vPosition.z!=0.0){
8 | vColor=vec4(0.0,0.0,0.0,1.0);
9 | }else{
10 | vColor=vec4(0.9,0.9,0.9,1.0);
11 | }
12 | }
--------------------------------------------------------------------------------
/app/src/main/assets/filter/fugu_fragment.sh:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 |
3 | uniform sampler2D vTexture;
4 |
5 | varying vec2 aCoordinate;
6 |
7 | void main(){
8 | vec4 nColor=texture2D(vTexture,aCoordinate);
9 | float c=nColor.r*0.299+nColor.g*0.587+nColor.b*0.114;
10 | gl_FragColor=vec4(c,c,c,nColor.a);
11 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/btn_shutter_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/assets/vshader/Ball.sh:
--------------------------------------------------------------------------------
1 | uniform mat4 vMatrix;
2 | varying vec4 vColor;
3 | attribute vec4 vPosition;
4 |
5 | void main(){
6 | gl_Position=vMatrix*vPosition;
7 | float color;
8 | if(vPosition.z>0.0){
9 | color=vPosition.z;
10 | }else{
11 | color=-vPosition.z;
12 | }
13 | vColor=vec4(color,color,color,1.0);
14 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 | 10dp
7 | 16sp
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/assets/filter/half_color_vertex.sh:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | attribute vec2 vCoordinate;
3 | uniform mat4 vMatrix;
4 |
5 | varying vec2 aCoordinate;
6 | varying vec4 aPos;
7 | varying vec4 gPosition;
8 |
9 | void main(){
10 | gl_Position=vMatrix*vPosition;
11 | aPos=vPosition;
12 | aCoordinate=vCoordinate;
13 | gPosition=vMatrix*vPosition;
14 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/btn_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/assets/vr/skysphere.vert:
--------------------------------------------------------------------------------
1 | uniform mat4 uProjMatrix;
2 | uniform mat4 uViewMatrix;
3 | uniform mat4 uModelMatrix;
4 | uniform mat4 uRotateMatrix;
5 |
6 | attribute vec3 aPosition;
7 | attribute vec2 aCoordinate;
8 |
9 | varying vec2 vCoordinate;
10 |
11 | void main(){
12 | gl_Position=uProjMatrix*uRotateMatrix*uViewMatrix*uModelMatrix*vec4(aPosition,1);
13 | vCoordinate=aCoordinate;
14 | }
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/test/java/edu/wuwang/opengl/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/titlebar.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/camera/Renderer.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Renderer.java
4 | *
5 | * Created by Wuwang on 2017/3/3
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.camera;
9 |
10 | import android.opengl.GLSurfaceView;
11 |
12 | /**
13 | * Description:
14 | */
15 | public interface Renderer extends GLSurfaceView.Renderer {
16 |
17 | void onDestroy();
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_opengl.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_camera.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
9 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/edu/wuwang/opengl/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_obj.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_picture.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_button.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
10 |
--------------------------------------------------------------------------------
/app/src/main/assets/3dres/pikachu.mtl:
--------------------------------------------------------------------------------
1 | # Material file for pikachu.obj
2 |
3 | newmtl eye
4 | Ns 0
5 | d 1
6 | illum 2
7 | Kd 0.8 0.8 0.8
8 | Ks 0.0 0.0 0.0
9 | Ka 0.2 0.2 0.2
10 | map_Kd eye1.png
11 |
12 | newmtl mouth
13 | Ns 0
14 | d 1
15 | illum 2
16 | Kd 0.8 0.8 0.8
17 | Ks 0.0 0.0 0.0
18 | Ka 0.2 0.2 0.2
19 | map_Kd mouth1.png
20 |
21 | newmtl pikagen
22 | Ns 0
23 | d 1
24 | illum 2
25 | Kd 0.8 0.8 0.8
26 | Ks 0.0 0.0 0.0
27 | Ka 0.2 0.2 0.2
28 | map_Kd pikagen.png
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/egl/GlError.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.egl;
2 |
3 | public enum GlError {
4 |
5 | OK(0,"ok"),
6 | ConfigErr(101,"config not support");
7 | int code;
8 | String msg;
9 | GlError(int code, String msg){
10 | this.code=code;
11 | this.msg=msg;
12 | }
13 |
14 | public int value(){
15 | return code;
16 | }
17 |
18 | @Override
19 | public String toString() {
20 | return msg;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/assets/3dres/patrick.mtl:
--------------------------------------------------------------------------------
1 | # Material file for patrick.obj
2 |
3 | newmtl patty
4 | Ns 0
5 | d 1
6 | illum 2
7 | Kd 0.8 0.8 0.8
8 | Ks 0.0 0.0 0.0
9 | Ka 0.2 0.2 0.2
10 | map_Kd Patty_Wagon.png
11 |
12 | newmtl pat
13 | Ns 0
14 | d 1
15 | illum 2
16 | Kd 0.8 0.8 0.8
17 | Ks 0.0 0.0 0.0
18 | Ka 0.2 0.2 0.2
19 | map_Kd Char_Patrick.png
20 |
21 | newmtl sb
22 | Ns 0
23 | d 1
24 | illum 2
25 | Kd 0.8 0.8 0.8
26 | Ks 0.0 0.0 0.0
27 | Ka 0.2 0.2 0.2
28 | map_Kd Spongebob_SquarePants.png
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #0688c4
4 | #FF444444
5 | #DD0688c4
6 | #FF4081
7 | #FF111111
8 | #FFFFFFFF
9 |
10 | #FFFFFFFF
11 | #FF2B2B2B
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.app.AppCompatActivity;
6 |
7 | /**
8 | * Created by wuwang on 2016/10/30
9 | */
10 |
11 | public class BaseActivity extends AppCompatActivity {
12 |
13 | @Override
14 | protected void onCreate(@Nullable Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | setTitle("OpenGL Demo");
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/etc/StateChangeListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * StateCallback.java
4 | *
5 | * Created by Wuwang on 2016/11/30
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.etc;
9 |
10 | /**
11 | * Description:
12 | */
13 | public interface StateChangeListener {
14 |
15 | int START=1;
16 | int STOP=2;
17 | int PLAYING=3;
18 | int INIT=4;
19 | int PAUSE=5;
20 | int RESUME=6;
21 |
22 | void onStateChanged(int lastState, int nowState);
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_text.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
11 |
--------------------------------------------------------------------------------
/app/src/main/assets/3dres/obj2.frag:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 | varying vec2 textureCoordinate;
3 | uniform sampler2D vTexture;
4 | varying vec4 vDiffuse; //接收从顶点着色器过来的散射光分量
5 | varying vec4 vAmbient; //接收传递给片元着色器的环境光分量
6 | varying vec4 vSpecular; //接收传递给片元着色器的镜面光分量
7 | void main() {
8 | vec4 finalColor=texture2D(vTexture,textureCoordinate);
9 | // //给此片元颜色值
10 | // gl_FragColor=finalColor;
11 | //vec4 finalColor=vec4(1.0);
12 | //给此片元颜色值
13 | gl_FragColor=finalColor*vAmbient+finalColor*vSpecular+finalColor*vDiffuse;
14 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_choose.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
12 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_ani.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
11 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | OpenGL
3 | 切换
4 |
5 | 绘制形体
6 | 图片处理
7 | 图形变换
8 | 相机
9 | 压缩纹理动画
10 | FBO使用
11 | EGL后台处理图片
12 | 3D Obj模型
13 | Obj+mtl模型
14 |
15 | 原图
16 | 全部处理
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_egl_back_env.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
9 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_zip.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
15 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Android\android-sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/filter/NoFilter.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * NoFilter.java
4 | *
5 | * Created by Wuwang on 2016/11/19
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.filter;
9 |
10 | import android.content.res.Resources;
11 |
12 | /**
13 | * Description:
14 | */
15 | public class NoFilter extends AFilter {
16 |
17 | public NoFilter(Resources res) {
18 | super(res);
19 | }
20 |
21 | @Override
22 | protected void onCreate() {
23 | createProgramByAssetsFile("shader/base_vertex.sh",
24 | "shader/base_fragment.sh");
25 | }
26 |
27 | @Override
28 | protected void onSizeChanged(int width, int height) {
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/filter/GrayFilter.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * GrayFilter.java
4 | *
5 | * Created by Wuwang on 2016/12/14
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.filter;
9 |
10 | import android.content.res.Resources;
11 |
12 | /**
13 | * Description:
14 | */
15 | public class GrayFilter extends AFilter {
16 |
17 | public GrayFilter(Resources mRes) {
18 | super(mRes);
19 | }
20 |
21 | @Override
22 | protected void onCreate() {
23 | createProgramByAssetsFile("shader/base_vertex.sh",
24 | "shader/color/gray_fragment.frag");
25 | }
26 |
27 | @Override
28 | protected void onSizeChanged(int width, int height) {
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_filter.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "24.0.1"
6 |
7 | defaultConfig {
8 | applicationId "edu.wuwang.opengl"
9 | minSdkVersion 15
10 | targetSdkVersion 24
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:24.2.0'
26 | compile 'com.android.support:recyclerview-v7:24.2.0'
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/render/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Shape.java
4 | *
5 | * Created by Wuwang on 2016/9/30
6 | */
7 | package edu.wuwang.opengl.render;
8 |
9 | import android.opengl.GLES20;
10 | import android.opengl.GLSurfaceView;
11 | import android.view.View;
12 |
13 | /**
14 | * Description:
15 | */
16 | public abstract class Shape implements GLSurfaceView.Renderer {
17 |
18 | protected View mView;
19 |
20 | public Shape(View mView){
21 | this.mView=mView;
22 | }
23 |
24 | public int loadShader(int type, String shaderCode){
25 | //根据type创建顶点着色器或者片元着色器
26 | int shader = GLES20.glCreateShader(type);
27 | //将资源加入到着色器中,并编译
28 | GLES20.glShaderSource(shader, shaderCode);
29 | GLES20.glCompileShader(shader);
30 | return shader;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_fbo.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
12 |
18 |
--------------------------------------------------------------------------------
/app/src/main/assets/filter/color_fragment.sh:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 |
3 | uniform sampler2D vTexture;
4 | uniform int vChangeType;
5 | uniform vec3 vChangeColor;
6 |
7 | varying vec2 aCoordinate;
8 |
9 | void modifyColor(vec4 color){
10 | color.r=max(min(color.r,1.0),0.0);
11 | color.g=max(min(color.g,1.0),0.0);
12 | color.b=max(min(color.b,1.0),0.0);
13 | color.a=max(min(color.a,1.0),0.0);
14 | }
15 |
16 | void main(){
17 | vec4 nColor=texture2D(vTexture,aCoordinate);
18 | if(vChangeType==1){
19 | float c=nColor.r*vChangeColor.r+nColor.g*vChangeColor.g+nColor.b*vChangeColor.b;
20 | gl_FragColor=vec4(c,c,c,nColor.a);
21 | }else if(vChangeType==2){
22 | vec4 deltaColor=nColor+vec4(vChangeColor,0.0);
23 | modifyColor(deltaColor);
24 | gl_FragColor=deltaColor;
25 | }else{
26 | gl_FragColor=nColor;
27 | }
28 | }
--------------------------------------------------------------------------------
/app/src/main/assets/3dres/obj.vert:
--------------------------------------------------------------------------------
1 | attribute vec3 vPosition;
2 | attribute vec2 vCoord;
3 | uniform mat4 vMatrix;
4 |
5 | varying vec2 textureCoordinate;
6 |
7 | attribute vec3 vNormal; //法向量
8 | varying vec4 vDiffuse; //用于传递给片元着色器的散射光最终强度
9 |
10 |
11 | //返回散射光强度
12 | vec4 pointLight(vec3 normal,vec3 lightLocation,vec4 lightDiffuse){
13 | //变换后的法向量
14 | vec3 newTarget=normalize((vMatrix*vec4(normal+vPosition,1)).xyz-(vMatrix*vec4(vPosition,1)).xyz);
15 | //表面点与光源的方向向量
16 | vec3 vp=normalize(lightLocation-(vMatrix*vec4(vPosition,1)).xyz);
17 | return lightDiffuse*max(0.0,dot(newTarget,vp));
18 | }
19 |
20 | void main(){
21 | gl_Position = vMatrix*vec4(vPosition,1);
22 | textureCoordinate = vCoord;
23 |
24 | vec4 at=vec4(1.0,1.0,1.0,1.0); //光照强度
25 | vec3 pos=vec3(50.0,200.0,50.0); //光照位置
26 | vDiffuse=pointLight(vNormal,pos,at);
27 | }
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/blend/SquareRelativeLayout.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.blend;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.widget.RelativeLayout;
6 |
7 | /**
8 | * Created by aiya on 2017/8/2.
9 | */
10 |
11 | public class SquareRelativeLayout extends RelativeLayout {
12 | public SquareRelativeLayout(Context context) {
13 | super(context);
14 | }
15 |
16 | public SquareRelativeLayout(Context context, AttributeSet attrs) {
17 | super(context, attrs);
18 | }
19 |
20 | public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
21 | super(context, attrs, defStyleAttr);
22 | }
23 |
24 | @Override
25 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
26 | super.onMeasure(widthMeasureSpec, widthMeasureSpec);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_fglview.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
10 |
16 |
17 |
21 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/obj/MtlInfo.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.obj;
2 |
3 | /**
4 | * Created by wuwang on 2017/2/22
5 | */
6 |
7 | public class MtlInfo {
8 | public String newmtl;
9 | public float[] Ka=new float[3]; //阴影色
10 | public float[] Kd=new float[3]; //固有色
11 | public float[] Ks=new float[3]; //高光色
12 | public float[] Ke=new float[3]; //
13 | public float Ns; //shininess
14 | public String map_Kd; //固有纹理贴图
15 | public String map_Ks; //高光纹理贴图
16 | public String map_Ka; //阴影纹理贴图
17 |
18 | //denotes the illumination model used by the material.
19 | // illum = 1 indicates a flat material with no specular highlights,
20 | // so the value of Ks is not used.
21 | // illum = 2 denotes the presence of specular highlights,
22 | // and so a specification for Ks is required.
23 | public int illum;
24 | }
25 |
--------------------------------------------------------------------------------
/app/src/main/assets/vshader/BallWithLight.sh:
--------------------------------------------------------------------------------
1 | uniform mat4 vMatrix; //总变换矩阵
2 | uniform mat4 uMMatrix; //变换矩阵
3 | uniform vec3 uLightLocation; //光源位置
4 | uniform vec3 uCamera; //摄像机位置
5 | attribute vec3 vPosition; //顶点位置
6 | attribute vec3 vNormal; //法向量
7 | varying vec4 vDiffuse; //用于传递给片元着色器的散射光最终强度
8 |
9 |
10 | //返回散射光强度
11 | vec4 pointLight(vec3 normal,vec3 lightLocation,vec4 lightDiffuse){
12 | //变换后的法向量
13 | vec3 newTarget=normalize((vMatrix*vec4(normal+vPosition,1)).xyz-(vMatrix*vec4(vPosition,1)).xyz);
14 | //表面点与光源的方向向量
15 | vec3 vp=normalize(lightLocation-(vMatrix*vec4(vPosition,1)).xyz);
16 | return lightDiffuse*max(0.0,dot(newTarget,vp));
17 | }
18 |
19 | void main(){
20 | gl_Position = vMatrix * vec4(vPosition,1); //根据总变换矩阵计算此次绘制此顶点位置
21 |
22 | vec4 at=vec4(1.0,1.0,1.0,1.0); //光照强度
23 | vec3 pos=vec3(100.0,80.0,80.0); //光照位置
24 | vDiffuse=pointLight(normalize(vPosition),pos,at);
25 | }
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/camera/IAiyaCamera.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * IAiyaCamera.java
4 | *
5 | * Created by Wuwang on 2016/11/22
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.camera;
9 |
10 | import android.graphics.Point;
11 | import android.graphics.SurfaceTexture;
12 |
13 | /**
14 | * Description:
15 | */
16 | public interface IAiyaCamera {
17 |
18 | void open(int cameraId);
19 |
20 | void setPreviewTexture(SurfaceTexture texture);
21 |
22 | void setConfig(Config config);
23 |
24 | void setOnPreviewFrameCallback(PreviewFrameCallback callback);
25 |
26 | void preview();
27 |
28 | Point getPreviewSize();
29 |
30 | Point getPictureSize();
31 |
32 | boolean close();
33 |
34 | class Config{
35 | float rate; //宽高比
36 | int minPreviewWidth;
37 | int minPictureWidth;
38 | }
39 |
40 | interface PreviewFrameCallback{
41 | void onPreviewFrame(byte[] bytes, int width, int height);
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/vary/VaryActivity.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.vary;
2 |
3 | import android.opengl.GLSurfaceView;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 |
7 | import edu.wuwang.opengl.BaseActivity;
8 | import edu.wuwang.opengl.R;
9 |
10 | /**
11 | * Created by wuwang on 2016/10/30
12 | */
13 |
14 | public class VaryActivity extends BaseActivity {
15 |
16 | private GLSurfaceView mGLView;
17 | private VaryRender render;
18 |
19 | @Override
20 | protected void onCreate(@Nullable Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_opengl);
23 | initGL();
24 | }
25 |
26 | public void initGL(){
27 | mGLView= (GLSurfaceView) findViewById(R.id.mGLView);
28 | mGLView.setEGLContextClientVersion(2);
29 | mGLView.setRenderer(render=new VaryRender(getResources()));
30 | mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_camera2.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
8 |
11 |
12 |
22 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/image/filter/ContrastColorFilter.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.image.filter;
2 |
3 | import android.content.Context;
4 | import android.opengl.GLES20;
5 |
6 | /**
7 | * Created by wuwang on 2016/10/22
8 | */
9 | public class ContrastColorFilter extends AFilter {
10 |
11 | private ColorFilter.Filter filter;
12 |
13 | private int hChangeType;
14 | private int hChangeColor;
15 |
16 | public ContrastColorFilter(Context context, ColorFilter.Filter filter) {
17 | super(context, "filter/half_color_vertex.sh", "filter/half_color_fragment.sh");
18 | this.filter=filter;
19 | }
20 |
21 | @Override
22 | public void onDrawSet() {
23 | GLES20.glUniform1i(hChangeType,filter.getType());
24 | GLES20.glUniform3fv(hChangeColor,1,filter.data(),0);
25 | }
26 |
27 | @Override
28 | public void onDrawCreatedSet(int mProgram) {
29 | hChangeType=GLES20.glGetUniformLocation(mProgram,"vChangeType");
30 | hChangeColor=GLES20.glGetUniformLocation(mProgram,"vChangeColor");
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/vr/MatrixHelper.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.vr;
2 |
3 | /**
4 | * Created by aiya on 2017/5/22.
5 | */
6 |
7 | public class MatrixHelper {
8 |
9 | public static void perspectiveM(float[] m, int offset,
10 | float fovy, float aspect, float zNear, float zFar) {
11 | float f = 1.0f / (float) Math.tan(fovy * (Math.PI / 360.0));
12 | float rangeReciprocal = 1.0f / (zNear - zFar);
13 |
14 | m[offset + 0] = f / aspect;
15 | m[offset + 1] = 0.0f;
16 | m[offset + 2] = 0.0f;
17 | m[offset + 3] = 0.0f;
18 |
19 | m[offset + 4] = 0.0f;
20 | m[offset + 5] = f;
21 | m[offset + 6] = 0.0f;
22 | m[offset + 7] = 0.0f;
23 |
24 | m[offset + 8] = 0.0f;
25 | m[offset + 9] = 0.0f;
26 | m[offset + 10] = (zFar + zNear) * rangeReciprocal;
27 | m[offset + 11] = -1.0f;
28 |
29 | m[offset + 12] = 0.0f;
30 | m[offset + 13] = 0.0f;
31 | m[offset + 14] = 2.0f * zFar * zNear * rangeReciprocal;
32 | m[offset + 15] = 0.0f;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/render/FGLView.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * FGLView.java
4 | *
5 | * Created by Wuwang on 2016/9/29
6 | */
7 | package edu.wuwang.opengl.render;
8 |
9 | import android.content.Context;
10 | import android.opengl.GLSurfaceView;
11 | import android.util.AttributeSet;
12 |
13 | import edu.wuwang.opengl.render.FGLRender;
14 | import edu.wuwang.opengl.render.Shape;
15 |
16 | /**
17 | * Description:
18 | */
19 | public class FGLView extends GLSurfaceView {
20 |
21 | private FGLRender renderer;
22 |
23 | public FGLView(Context context) {
24 | this(context,null);
25 | }
26 |
27 | public FGLView(Context context, AttributeSet attrs) {
28 | super(context, attrs);
29 | init();
30 | }
31 |
32 | private void init(){
33 | setEGLContextClientVersion(2);
34 | setRenderer(renderer=new FGLRender(this));
35 | setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
36 | }
37 |
38 | public void setShape(Class extends Shape> clazz){
39 | try {
40 | renderer.setShape(clazz);
41 | } catch (Exception e) {
42 | e.printStackTrace();
43 | }
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/camera/ICamera.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * ICamera.java
4 | *
5 | * Created by Wuwang on 2016/11/10
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.camera;
9 |
10 | import android.graphics.Point;
11 | import android.graphics.SurfaceTexture;
12 |
13 | /**
14 | * Description:
15 | */
16 | public interface ICamera {
17 |
18 | boolean open(int cameraId);
19 | void setConfig(Config config);
20 | boolean preview();
21 | boolean switchTo(int cameraId);
22 | void takePhoto(TakePhotoCallback callback);
23 | boolean close();
24 | void setPreviewTexture(SurfaceTexture texture);
25 |
26 | Point getPreviewSize();
27 | Point getPictureSize();
28 |
29 | void setOnPreviewFrameCallback(PreviewFrameCallback callback);
30 |
31 | class Config{
32 | float rate; //宽高比
33 | int minPreviewWidth;
34 | int minPictureWidth;
35 | }
36 |
37 | interface TakePhotoCallback{
38 | void onTakePhoto(byte[] bytes, int width, int height);
39 | }
40 |
41 | interface PreviewFrameCallback{
42 | void onPreviewFrame(byte[] bytes, int width, int height);
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_camera3.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
8 |
11 |
12 |
17 |
27 |
--------------------------------------------------------------------------------
/app/src/main/assets/lookup/lookup.frag:
--------------------------------------------------------------------------------
1 | varying highp vec2 textureCoordinate;
2 |
3 | uniform sampler2D vTexture;
4 | uniform sampler2D maskTexture;
5 |
6 | uniform highp float intensity;
7 | //lookup table 为大小为512*512,个数为8*8的格子表
8 | void main(){
9 | highp vec4 textureColor = texture2D(vTexture, textureCoordinate);
10 | //根据当前颜色的蓝色通道查找所在格子
11 | highp float blueColor = textureColor.b * 63.0;
12 | highp vec2 quad1;
13 | quad1.y = floor(floor(blueColor) / 8.0);
14 | quad1.x = floor(blueColor) - (quad1.y * 8.0);
15 |
16 | highp vec2 quad2;
17 | quad2.y = floor(ceil(blueColor) / 8.0);
18 | quad2.x = ceil(blueColor) - (quad2.y * 8.0);
19 | highp vec2 texPos1;
20 | texPos1.x = (quad1.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
21 | texPos1.y = (quad1.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);
22 | highp vec2 texPos2;
23 | texPos2.x = (quad2.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
24 | texPos2.y = (quad2.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);
25 | lowp vec4 newColor1 = texture2D(maskTexture, texPos1);
26 | lowp vec4 newColor2 = texture2D(maskTexture, texPos2);
27 | lowp vec4 newColor = mix(newColor1, newColor2, fract(blueColor));
28 | gl_FragColor = mix(textureColor, vec4(newColor.rgb, textureColor.w), intensity);
29 | }
--------------------------------------------------------------------------------
/app/src/main/assets/shader/beauty/beauty.vert:
--------------------------------------------------------------------------------
1 | attribute vec4 vPosition;
2 | attribute vec2 vCoord;
3 | varying vec2 textureCoordinate;
4 | varying vec2 blurCoord1s[14];
5 | const highp float mWidth=720.0;
6 | const highp float mHeight=1280.0;
7 | uniform mat4 vMatrix;
8 | void main( )
9 | {
10 | gl_Position = vMatrix*vPosition;
11 | textureCoordinate = vCoord;
12 |
13 | highp float mul_x = 2.0 / mWidth;
14 | highp float mul_y = 2.0 / mHeight;
15 |
16 | // 14个采样点
17 | blurCoord1s[0] = vCoord + vec2( 0.0 * mul_x, -10.0 * mul_y );
18 | blurCoord1s[1] = vCoord + vec2( 8.0 * mul_x, -5.0 * mul_y );
19 | blurCoord1s[2] = vCoord + vec2( 8.0 * mul_x, 5.0 * mul_y );
20 | blurCoord1s[3] = vCoord + vec2( 0.0 * mul_x, 10.0 * mul_y );
21 | blurCoord1s[4] = vCoord + vec2( -8.0 * mul_x, 5.0 * mul_y );
22 | blurCoord1s[5] = vCoord + vec2( -8.0 * mul_x, -5.0 * mul_y );
23 | blurCoord1s[6] = vCoord + vec2( 0.0 * mul_x, -6.0 * mul_y );
24 | blurCoord1s[7] = vCoord + vec2( -4.0 * mul_x, -4.0 * mul_y );
25 | blurCoord1s[8] = vCoord + vec2( -6.0 * mul_x, 0.0 * mul_y );
26 | blurCoord1s[9] = vCoord + vec2( -4.0 * mul_x, 4.0 * mul_y );
27 | blurCoord1s[10] = vCoord + vec2( 0.0 * mul_x, 6.0 * mul_y );
28 | blurCoord1s[11] = vCoord + vec2( 4.0 * mul_x, 4.0 * mul_y );
29 | blurCoord1s[12] = vCoord + vec2( 6.0 * mul_x, 0.0 * mul_y );
30 | blurCoord1s[13] = vCoord + vec2( 4.0 * mul_x, -4.0 * mul_y );
31 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_light.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
8 |
14 |
20 |
26 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/image/SGLView.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * SGLView.java
4 | *
5 | * Created by Wuwang on 2016/10/15
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.image;
9 |
10 | import android.content.Context;
11 | import android.graphics.BitmapFactory;
12 | import android.opengl.GLSurfaceView;
13 | import android.util.AttributeSet;
14 |
15 | import java.io.IOException;
16 |
17 | import edu.wuwang.opengl.image.filter.AFilter;
18 | import edu.wuwang.opengl.image.filter.ColorFilter;
19 |
20 | /**
21 | * Description:
22 | */
23 | public class SGLView extends GLSurfaceView {
24 |
25 | private SGLRender render;
26 |
27 | public SGLView(Context context) {
28 | this(context,null);
29 | }
30 |
31 | public SGLView(Context context, AttributeSet attrs) {
32 | super(context, attrs);
33 | init();
34 | }
35 |
36 | private void init(){
37 | setEGLContextClientVersion(2);
38 | render=new SGLRender(this);
39 | setRenderer(render);
40 | setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
41 |
42 | try {
43 | render.setImage(BitmapFactory.decodeStream(getResources().getAssets().open("texture/fengj.png")));
44 | requestRender();
45 | } catch (IOException e) {
46 | e.printStackTrace();
47 | }
48 | }
49 |
50 | public SGLRender getRender(){
51 | return render;
52 | }
53 |
54 | public void setFilter(AFilter filter){
55 | render.setFilter(filter);
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/utils/PermissionUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * PermissionUtils.java
4 | *
5 | * Created by Wuwang on 2016/11/14
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.utils;
9 |
10 | import android.Manifest;
11 | import android.app.Activity;
12 | import android.content.pm.PackageManager;
13 | import android.os.Build;
14 | import android.support.v4.app.ActivityCompat;
15 |
16 | /**
17 | * Description:
18 | */
19 | public class PermissionUtils {
20 |
21 |
22 | public static void askPermission(Activity context, String[] permissions,int req, Runnable
23 | runnable){
24 | if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
25 | int result= ActivityCompat.checkSelfPermission(context,permissions[0]);
26 | if(result== PackageManager.PERMISSION_GRANTED){
27 | runnable.run();
28 | }else{
29 | ActivityCompat.requestPermissions(context,new String[]{Manifest.permission.CAMERA
30 | ,Manifest.permission.WRITE_EXTERNAL_STORAGE},req);
31 | }
32 | }else{
33 | runnable.run();
34 | }
35 | }
36 |
37 | public static void onRequestPermissionsResult(boolean isReq,int[] grantResults,Runnable
38 | okRun,Runnable deniRun){
39 | if(isReq){
40 | if (grantResults.length > 0&&grantResults[0]==PackageManager.PERMISSION_GRANTED) {
41 | okRun.run();
42 | } else {
43 | deniRun.run();
44 | }
45 | }
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/filter/OesFilter.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * CameraFilter.java
4 | *
5 | * Created by Wuwang on 2016/11/19
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.filter;
9 |
10 | import java.util.Arrays;
11 |
12 | import android.content.res.Resources;
13 | import android.opengl.GLES11Ext;
14 | import android.opengl.GLES20;
15 |
16 | /**
17 | * Description:
18 | */
19 | public class OesFilter extends AFilter{
20 |
21 | private int mHCoordMatrix;
22 | private float[] mCoordMatrix= Arrays.copyOf(OM,16);
23 |
24 | public OesFilter(Resources mRes) {
25 | super(mRes);
26 | }
27 |
28 | @Override
29 | protected void onCreate() {
30 | createProgramByAssetsFile("shader/oes_base_vertex.sh","shader/oes_base_fragment.sh");
31 | mHCoordMatrix=GLES20.glGetUniformLocation(mProgram,"vCoordMatrix");
32 | }
33 |
34 | public void setCoordMatrix(float[] matrix){
35 | this.mCoordMatrix=matrix;
36 | }
37 |
38 | @Override
39 | protected void onSetExpandData() {
40 | super.onSetExpandData();
41 | GLES20.glUniformMatrix4fv(mHCoordMatrix,1,false,mCoordMatrix,0);
42 | }
43 |
44 | @Override
45 | protected void onBindTexture() {
46 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0+getTextureType());
47 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,getTextureId());
48 | GLES20.glUniform1i(mHTexture,getTextureType());
49 | }
50 |
51 | @Override
52 | protected void onSizeChanged(int width, int height) {
53 |
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_blend.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
10 |
14 |
15 |
20 |
25 |
30 |
31 |
38 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 说明
2 | 这是一个Android OpenGLES2.0 从零开始的Demo程序,仅做个人学习OpenGLES的记录,并提供给后来初学者参考。若有相关问题,也非常乐意与大家一起讨论共同进步。
3 | 关于Android OpenGLES2.0学习的博客,也相当于这个Demo的讲解,在CSDN平台上的[链接地址](http://blog.csdn.net/junzia/article/category/6462864)。
4 |
5 | # 更新说明
6 | ## 当前内容
7 | 1. 三角形。
8 | 2. 等腰直角三角形。
9 | 3. 彩色三角形。
10 | 4. 正方形的绘制。
11 | 5. 正方体的绘制。
12 | 6. 圆锥的绘制。
13 | 7. 圆柱的绘制
14 | 8. 球体的绘制
15 | 9. 显示图片(纹理贴图初步)
16 | 10. 图片简单的色彩处理
17 | 11. 图片模糊
18 | 12. 放大镜效果
19 | 13. 平移旋转缩放变换
20 | 14. 相机GL预览
21 | 15. 压缩纹理动画
22 | 16. FBO使用
23 | 17. 3D模型《帽子》加载
24 | 18. EGL方式后台处理图像示例
25 | 19. 3D《皮卡丘》模型及纹理加载,光照处理
26 | 20. Camera1API/Camera2API预览到SurfaceView和TextureView
27 | 21. Camera上增加动画
28 | 22. VR 天空盒(球形)效果
29 | 23. 美颜
30 | 24. Blend示例
31 | 25. 光照
32 |
33 | ## 20180326更新
34 | 1. 增加冯氏光照模型的光照示例
35 |
36 | ## 20170803更新
37 | 1. 增加Blend示例
38 |
39 | ## 20170614更新
40 | 1. 增加LookupFilter
41 | 2. 增加美颜及滤镜示例
42 |
43 | ## 20170523更新
44 | 1. 球形天空盒实现
45 | 2. VR效果
46 |
47 | ## 20170324更新
48 | 1. 增加Camera上播放动画的示例
49 |
50 | ## 20170309更新
51 | 1. 增加Texture控制类,支持所有图像流的OpenGLES方式处理
52 | 2. 增加相机示例,5.0前使用Camera1 API预览,5.0后使用Camera2预览
53 |
54 | ## 20170227更新
55 | 1. 增加相对复杂些的3D模型加载
56 | 2. 为3D模型增加贴图
57 | 3. 给3D模型增加散射光、镜面光及环境光
58 |
59 | ## 20170218更新
60 | 1. 增加EGL方式后台处理图像示例
61 |
62 | ## 20170110更新
63 | 1. 增加3D模型加载
64 | 2. 为3D模型增加散射光照(只是为了便于呈现立体效果)
65 |
66 | ## 20161224更新
67 | 1. 修复压缩纹理动画播放错乱问题
68 | 2. 增加FBO使用
69 |
70 | ## 20161114更新
71 | 1. 增加平移旋转缩放变换示例
72 | 2. 增加相机预览示例
73 |
74 | ## 20161030更新
75 | 1. 增加图片模糊处理
76 | 2. 增加放大镜效果处理
77 |
78 | ## 20161023更新
79 | 1. 增加光照球体(不全)
80 | 2. 增加显示纹理图片
81 | 3. 修改demo入口
82 | 4. 增加图片黑白、冷色调、暖色调处理
83 | 5. 允许色彩处理方式切换
84 |
85 | ## 20161016更新
86 | 1. 修复圆锥绘制的bug
87 | 2. 增加圆柱绘制
88 | 3. 增加球体的绘制
89 |
90 | ## 20161014更新
91 | 1. 将shader分离到assets文件夹中。
92 | 2. 增加圆锥绘制。
93 | 3. 允许绘制图形切换,便于更好的展示。
94 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/etc/ZipActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * ZipActivity.java
4 | *
5 | * Created by Wuwang on 2016/12/8
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.etc;
9 |
10 | import android.os.Bundle;
11 | import android.support.annotation.Nullable;
12 | import android.support.v7.app.AppCompatActivity;
13 | import android.view.View;
14 |
15 | import edu.wuwang.opengl.R;
16 | import edu.wuwang.opengl.utils.Gl2Utils;
17 |
18 | /**
19 | * Description:
20 | */
21 | public class ZipActivity extends AppCompatActivity {
22 |
23 | private ZipAniView mAniView;
24 | private String nowMenu="assets/etczip/cc.zip";
25 |
26 | @Override
27 | protected void onCreate(@Nullable Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | setContentView(R.layout.activity_zip);
30 | mAniView= (ZipAniView)findViewById(R.id.mAni);
31 | mAniView.setScaleType(Gl2Utils.TYPE_CENTERINSIDE);
32 | mAniView.setOnClickListener(new View.OnClickListener() {
33 | @Override
34 | public void onClick(View v) {
35 | if(!mAniView.isPlay()){
36 | mAniView.setAnimation(nowMenu,50);
37 | mAniView.start();
38 | }
39 | }
40 | });
41 | mAniView.setStateChangeListener(new StateChangeListener() {
42 | @Override
43 | public void onStateChanged(int lastState, int nowState) {
44 | if(nowState==STOP){
45 | if(!mAniView.isPlay()){
46 | mAniView.setAnimation(nowMenu,50);
47 | mAniView.start();
48 | }
49 | }
50 | }
51 | });
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/camera/Camera3Activity.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.camera;
2 |
3 | import android.support.v7.widget.AppCompatSeekBar;
4 | import android.util.Log;
5 | import android.widget.SeekBar;
6 | import edu.wuwang.opengl.R;
7 | import edu.wuwang.opengl.filter.Beauty;
8 | import edu.wuwang.opengl.filter.LookupFilter;
9 |
10 | /**
11 | * Created by aiya on 2017/6/8.
12 | */
13 |
14 | public class Camera3Activity extends Camera2Activity {
15 |
16 | private AppCompatSeekBar mSeek;
17 | private LookupFilter mLookupFilter;
18 | private Beauty mBeautyFilter;
19 |
20 | @Override
21 | protected void setContentView() {
22 | setContentView(R.layout.activity_camera3);
23 | mSeek=(AppCompatSeekBar) findViewById(R.id.mSeek);
24 | mSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
25 | @Override
26 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
27 | Log.e("wuwang","process:"+progress);
28 | mLookupFilter.setIntensity(progress/100f);
29 | mBeautyFilter.setFlag(progress/20+1);
30 | }
31 |
32 | @Override
33 | public void onStartTrackingTouch(SeekBar seekBar) {
34 |
35 | }
36 |
37 | @Override
38 | public void onStopTrackingTouch(SeekBar seekBar) {
39 |
40 | }
41 | });
42 | }
43 |
44 | @Override
45 | protected void onFilterSet(TextureController controller) {
46 | mLookupFilter=new LookupFilter(getResources());
47 | mLookupFilter.setMaskImage("lookup/purity.png");
48 | mLookupFilter.setIntensity(0.0f);
49 | controller.addFilter(mLookupFilter);
50 | mBeautyFilter=new Beauty(getResources());
51 | controller.addFilter(mBeautyFilter);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/image/filter/ColorFilter.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * NoFilter.java
4 | *
5 | * Created by Wuwang on 2016/10/17
6 | */
7 | package edu.wuwang.opengl.image.filter;
8 |
9 | import android.content.Context;
10 | import android.opengl.GLES20;
11 |
12 | /**
13 | * Description:
14 | */
15 | public class ColorFilter extends AFilter {
16 |
17 | private Filter filter;
18 |
19 | private int hChangeType;
20 | private int hChangeColor;
21 |
22 | public ColorFilter(Context context,Filter filter) {
23 | super(context, "filter/default_vertex.sh", "filter/color_fragment.sh");
24 | this.filter=filter;
25 | }
26 |
27 | @Override
28 | public void onDrawSet() {
29 | GLES20.glUniform1i(hChangeType,filter.getType());
30 | GLES20.glUniform3fv(hChangeColor,1,filter.data(),0);
31 | }
32 |
33 | @Override
34 | public void onDrawCreatedSet(int mProgram) {
35 | hChangeType=GLES20.glGetUniformLocation(mProgram,"vChangeType");
36 | hChangeColor=GLES20.glGetUniformLocation(mProgram,"vChangeColor");
37 | }
38 |
39 | public enum Filter{
40 |
41 | NONE(0,new float[]{0.0f,0.0f,0.0f}),
42 | GRAY(1,new float[]{0.299f,0.587f,0.114f}),
43 | COOL(2,new float[]{0.0f,0.0f,0.1f}),
44 | WARM(2,new float[]{0.1f,0.1f,0.0f}),
45 | BLUR(3,new float[]{0.006f,0.004f,0.002f}),
46 | MAGN(4,new float[]{0.0f,0.0f,0.4f});
47 |
48 |
49 | private int vChangeType;
50 | private float[] data;
51 |
52 | Filter(int vChangeType,float[] data){
53 | this.vChangeType=vChangeType;
54 | this.data=data;
55 | }
56 |
57 | public int getType(){
58 | return vChangeType;
59 | }
60 |
61 | public float[] data(){
62 | return data;
63 | }
64 |
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/render/FGLRender.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * FGLRender.java
4 | *
5 | * Created by Wuwang on 2016/9/29
6 | */
7 | package edu.wuwang.opengl.render;
8 |
9 | import android.opengl.GLES20;
10 | import android.opengl.GLSurfaceView;
11 | import android.util.Log;
12 | import android.view.View;
13 |
14 | import java.lang.reflect.Constructor;
15 |
16 | import javax.microedition.khronos.egl.EGLConfig;
17 | import javax.microedition.khronos.opengles.GL10;
18 |
19 | /**
20 | * Description:
21 | */
22 | public class FGLRender extends Shape {
23 |
24 | private Shape shape;
25 | private Class extends Shape> clazz=Cube.class;
26 |
27 | public FGLRender(View mView) {
28 | super(mView);
29 | }
30 |
31 | public void setShape(Class extends Shape> shape){
32 | this.clazz=shape;
33 | }
34 |
35 | @Override
36 | public void onSurfaceCreated(GL10 gl, EGLConfig config) {
37 | GLES20.glClearColor(0.5f,0.5f,0.5f,1.0f);
38 | Log.e("wuwang","onSurfaceCreated");
39 | try {
40 | Constructor constructor=clazz.getDeclaredConstructor(View.class);
41 | constructor.setAccessible(true);
42 | shape= (Shape) constructor.newInstance(mView);
43 | } catch (Exception e) {
44 | e.printStackTrace();
45 | shape=new Cube(mView);
46 | }
47 | shape.onSurfaceCreated(gl,config);
48 | }
49 |
50 | @Override
51 | public void onSurfaceChanged(GL10 gl, int width, int height) {
52 | Log.e("wuwang","onSurfaceChanged");
53 | GLES20.glViewport(0,0,width,height);
54 |
55 | shape.onSurfaceChanged(gl, width, height);
56 | }
57 |
58 | @Override
59 | public void onDrawFrame(GL10 gl) {
60 | Log.e("wuwang","onDrawFrame");
61 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
62 | shape.onDrawFrame(gl);
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/filter/CameraFilter.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * AiyaFilter.java
4 | *
5 | * Created by Wuwang on 2016/11/19
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.filter;
9 |
10 | import android.content.res.Resources;
11 | import android.hardware.Camera;
12 |
13 | /**
14 | * Description:
15 | */
16 | public class CameraFilter extends OesFilter {
17 |
18 |
19 |
20 | public CameraFilter(Resources mRes) {
21 | super(mRes);
22 | }
23 |
24 | @Override
25 | protected void initBuffer() {
26 | super.initBuffer();
27 | movie();
28 | }
29 |
30 | @Override
31 | public void setFlag(int flag) {
32 | super.setFlag(flag);
33 | if(getFlag()== Camera.CameraInfo.CAMERA_FACING_FRONT){ //前置摄像头
34 | cameraFront();
35 | }else if(getFlag()==Camera.CameraInfo.CAMERA_FACING_BACK){ //后置摄像头
36 | cameraBack();
37 | }
38 | }
39 |
40 | private void cameraFront(){
41 | float[] coord=new float[]{
42 | 1.0f, 0.0f,
43 | 0.0f, 0.0f,
44 | 1.0f, 1.0f,
45 | 0.0f, 1.0f,
46 | };
47 | mTexBuffer.clear();
48 | mTexBuffer.put(coord);
49 | mTexBuffer.position(0);
50 | }
51 |
52 | private void cameraBack(){
53 | float[] coord=new float[]{
54 | 1.0f, 0.0f,
55 | 0.0f, 0.0f,
56 | 1.0f, 1.0f,
57 | 0.0f, 1.0f,
58 | };
59 | mTexBuffer.clear();
60 | mTexBuffer.put(coord);
61 | mTexBuffer.position(0);
62 | }
63 |
64 | private void movie(){
65 | float[] coord=new float[]{
66 | 0.0f, 0.0f,
67 | 0.0f, 1.0f,
68 | 1.0f, 0.0f,
69 | 1.0f, 1.0f,
70 | };
71 | mTexBuffer.clear();
72 | mTexBuffer.put(coord);
73 | mTexBuffer.position(0);
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/assets/light/light.vert:
--------------------------------------------------------------------------------
1 | attribute vec4 aPosition;
2 | attribute vec2 aCoord;
3 | attribute vec3 aNormal;
4 | uniform mat4 uMatrix;
5 | uniform vec4 uBaseColor;
6 | uniform vec3 uLightColor;
7 | uniform float uAmbientStrength;
8 | uniform float uDiffuseStrength;
9 | uniform float uSpecularStrength;
10 | uniform vec3 uLightPosition;
11 | varying vec4 vColor;
12 |
13 | //在片元着色器中计算光照会获得更好更真实的光照效果,但是会比较耗性能
14 |
15 | //环境光的计算
16 | vec4 ambientColor(){
17 | vec3 ambient = uAmbientStrength * uLightColor;
18 | return vec4(ambient,1.0);
19 | }
20 |
21 | //漫反射的计算
22 | vec4 diffuseColor(){
23 | //模型变换后的位置
24 | vec3 fragPos=(uMatrix*aPosition).xyz;
25 | //光照方向
26 | vec3 direction=normalize(uLightPosition-fragPos);
27 | //模型变换后的法线向量
28 | vec3 normal=normalize(mat3(uMatrix)*aNormal);
29 | //max(cos(入射角),0)
30 | float diff = max(dot(normal,direction), 0.0);
31 | //材质的漫反射系数*max(cos(入射角),0)*光照颜色
32 | vec3 diffuse=uDiffuseStrength * diff * uLightColor;
33 | return vec4(diffuse,1.0);
34 | }
35 |
36 | //镜面光计算,镜面光计算有两种方式,一种是冯氏模型,一种是Blinn改进的冯氏模型
37 | //这里使用的是改进的冯氏模型,基于Half-Vector的计算方式
38 | vec4 specularColor(){
39 | //模型变换后的位置
40 | vec3 fragPos=(uMatrix*aPosition).xyz;
41 | //光照方向
42 | vec3 lightDirection=normalize(uLightPosition-fragPos);
43 | //模型变换后的法线向量
44 | vec3 normal=normalize(mat3(uMatrix)*aNormal);
45 | //观察方向,这里将观察点固定在(0,0,uLightPosition.z)处
46 | vec3 viewDirection=normalize(vec3(0,0,uLightPosition.z)-fragPos);
47 | //观察向量与光照向量的半向量
48 | vec3 hafVector=normalize(lightDirection+viewDirection);
49 | //max(0,cos(半向量与法向量的夹角)^粗糙度
50 | float diff=pow(max(dot(normal,hafVector),0.0),4.0);
51 | //材质的镜面反射系数*max(0,cos(反射向量与观察向量夹角)^粗糙度*光照颜色
52 | //材质的镜面反射系数*max(0,cos(半向量与法向量的夹角)^粗糙度*光照颜色
53 | vec3 specular=uSpecularStrength*diff*uLightColor;
54 | return vec4(specular,1.0);
55 | }
56 |
57 | void main(){
58 | gl_Position=uMatrix*aPosition;
59 | vColor=(ambientColor() + diffuseColor() + specularColor())* uBaseColor;
60 | }
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/render/FGLViewActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * FGLViewActivity.java
4 | *
5 | * Created by Wuwang on 2016/9/30
6 | */
7 | package edu.wuwang.opengl.render;
8 |
9 | import android.content.Intent;
10 | import android.os.Bundle;
11 | import android.support.annotation.Nullable;
12 | import android.support.v7.app.AppCompatActivity;
13 | import android.view.View;
14 | import android.widget.Button;
15 |
16 | import edu.wuwang.opengl.ChooseActivity;
17 | import edu.wuwang.opengl.R;
18 |
19 | /**
20 | * Description:
21 | */
22 | public class FGLViewActivity extends AppCompatActivity implements View.OnClickListener {
23 |
24 | private static final int REQ_CHOOSE=0x0101;
25 |
26 | private Button mChange;
27 | private FGLView mGLView;
28 |
29 |
30 | @Override
31 | protected void onCreate(@Nullable Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_fglview);
34 | init();
35 | }
36 |
37 | private void init(){
38 | mChange= (Button) findViewById(R.id.mChange);
39 | mGLView= (FGLView) findViewById(R.id.mGLView);
40 | }
41 |
42 | public void onClick(View view){
43 | switch (view.getId()){
44 | case R.id.mChange:
45 | Intent intent=new Intent(this,ChooseActivity.class);
46 | startActivityForResult(intent,REQ_CHOOSE);
47 | break;
48 | }
49 | }
50 |
51 | @Override
52 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
53 | super.onActivityResult(requestCode, resultCode, data);
54 | if(resultCode==RESULT_OK){
55 | mGLView.setShape((Class extends Shape>) data.getSerializableExtra("name"));
56 | }
57 | }
58 |
59 | @Override
60 | protected void onResume() {
61 | super.onResume();
62 | mGLView.onResume();
63 | }
64 |
65 | @Override
66 | protected void onPause() {
67 | super.onPause();
68 | mGLView.onPause();
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/utils/VaryTools.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.utils;
2 |
3 | import android.opengl.Matrix;
4 |
5 | import java.util.Arrays;
6 | import java.util.Stack;
7 |
8 | /**
9 | * Created by wuwang on 2016/10/30
10 | */
11 |
12 | public class VaryTools {
13 |
14 | private float[] mMatrixCamera=new float[16]; //相机矩阵
15 | private float[] mMatrixProjection=new float[16]; //投影矩阵
16 | private float[] mMatrixCurrent= //原始矩阵
17 | {1,0,0,0,
18 | 0,1,0,0,
19 | 0,0,1,0,
20 | 0,0,0,1};
21 |
22 | private Stack mStack; //变换矩阵堆栈
23 |
24 | public VaryTools(){
25 | mStack=new Stack<>();
26 | }
27 |
28 | //保护现场
29 | public void pushMatrix(){
30 | mStack.push(Arrays.copyOf(mMatrixCurrent,16));
31 | }
32 |
33 | //恢复现场
34 | public void popMatrix(){
35 | mMatrixCurrent=mStack.pop();
36 | }
37 |
38 | public void clearStack(){
39 | mStack.clear();
40 | }
41 |
42 | //平移变换
43 | public void translate(float x,float y,float z){
44 | Matrix.translateM(mMatrixCurrent,0,x,y,z);
45 | }
46 |
47 | //旋转变换
48 | public void rotate(float angle,float x,float y,float z){
49 | Matrix.rotateM(mMatrixCurrent,0,angle,x,y,z);
50 | }
51 |
52 | //缩放变换
53 | public void scale(float x,float y,float z){
54 | Matrix.scaleM(mMatrixCurrent,0,x,y,z);
55 | }
56 |
57 | //设置相机
58 | public void setCamera(float ex,float ey,float ez,float cx,float cy,float cz,float ux,float uy,float uz){
59 | Matrix.setLookAtM(mMatrixCamera,0,ex,ey,ez,cx,cy,cz,ux,uy,uz);
60 | }
61 |
62 | public void frustum(float left,float right,float bottom,float top,float near,float far){
63 | Matrix.frustumM(mMatrixProjection,0,left,right,bottom,top,near,far);
64 | }
65 |
66 | public void ortho(float left,float right,float bottom,float top,float near,float far){
67 | Matrix.orthoM(mMatrixProjection,0,left,right,bottom,top,near,far);
68 | }
69 |
70 | public float[] getFinalMatrix(){
71 | float[] ans=new float[16];
72 | Matrix.multiplyMM(ans,0,mMatrixCamera,0,mMatrixCurrent,0);
73 | Matrix.multiplyMM(ans,0,mMatrixProjection,0,ans,0);
74 | return ans;
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/etc/ZipAniView.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * ZipAniView.java
4 | *
5 | * Created by Wuwang on 2016/12/8
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.etc;
9 |
10 | import javax.microedition.khronos.egl.EGLConfig;
11 | import javax.microedition.khronos.opengles.GL10;
12 |
13 | import android.content.Context;
14 | import android.graphics.PixelFormat;
15 | import android.opengl.GLSurfaceView;
16 | import android.util.AttributeSet;
17 |
18 | /**
19 | * Description:
20 | */
21 | public class ZipAniView extends GLSurfaceView implements GLSurfaceView.Renderer {
22 |
23 | private ZipMulDrawer mDrawer;
24 |
25 | public ZipAniView(Context context) {
26 | this(context,null);
27 | }
28 |
29 | public ZipAniView(Context context, AttributeSet attrs) {
30 | super(context, attrs);
31 | init();
32 | }
33 |
34 | private void init(){
35 | setEGLContextClientVersion(2);
36 | setZOrderOnTop(true);
37 | getHolder().setFormat(PixelFormat.TRANSLUCENT);
38 | setEGLConfigChooser(8,8,8,8,16,0);
39 | setRenderer(this);
40 | setRenderMode(RENDERMODE_WHEN_DIRTY);
41 | mDrawer=new ZipMulDrawer(getResources());
42 | }
43 |
44 | public void setScaleType(int type){
45 | if(mDrawer!=null){
46 | mDrawer.setInt(ZipMulDrawer.TYPE,type);
47 | }
48 | }
49 |
50 | public void setAnimation(String path,int timeStep){
51 | mDrawer.setAnimation(this,path,timeStep);
52 | }
53 |
54 | public void start(){
55 | mDrawer.start();
56 | }
57 |
58 | public void stop(){
59 | mDrawer.stop();
60 | }
61 |
62 | public boolean isPlay(){
63 | return mDrawer.isPlay();
64 | }
65 |
66 | public void setStateChangeListener(StateChangeListener listener){
67 | mDrawer.setStateChangeListener(listener);
68 | }
69 |
70 | @Override
71 | public void onSurfaceCreated(GL10 gl, EGLConfig config) {
72 | mDrawer.create();
73 | }
74 |
75 | @Override
76 | public void onSurfaceChanged(GL10 gl, int width, int height) {
77 | mDrawer.onSizeChanged(width,height);
78 | }
79 |
80 | @Override
81 | public void onDrawFrame(GL10 gl) {
82 | mDrawer.draw();
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/filter/LookupFilter.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.filter;
2 |
3 | import android.content.res.Resources;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.opengl.GLES20;
7 | import android.opengl.GLUtils;
8 | import edu.wuwang.opengl.utils.EasyGlUtils;
9 | import java.io.IOException;
10 |
11 | public class LookupFilter extends AFilter {
12 |
13 | private int mHMaskImage;
14 | private int mHIntensity;
15 |
16 | private float intensity;
17 |
18 | private int[] mastTextures=new int[1];
19 | private Bitmap mBitmap;
20 |
21 | public LookupFilter(Resources mRes) {
22 | super(mRes);
23 | }
24 |
25 | @Override
26 | protected void onCreate() {
27 | createProgramByAssetsFile("lookup/lookup.vert","lookup/lookup.frag");
28 | mHMaskImage=GLES20.glGetUniformLocation(mProgram,"maskTexture");
29 | mHIntensity=GLES20.glGetUniformLocation(mProgram,"intensity");
30 | EasyGlUtils.genTexturesWithParameter(1,mastTextures,0, GLES20.GL_RGBA,512,512);
31 | }
32 |
33 | public void setIntensity(float value){
34 | this.intensity=value;
35 | }
36 |
37 | public void setMaskImage(String mask){
38 | try {
39 | mBitmap=BitmapFactory.decodeStream(mRes.getAssets().open(mask));
40 | } catch (IOException e) {
41 | e.printStackTrace();
42 | }
43 | }
44 |
45 | public void setMaskImage(Bitmap bitmap){
46 | this.mBitmap=bitmap;
47 | }
48 |
49 | @Override
50 | protected void onSizeChanged(int width, int height) {
51 |
52 | }
53 |
54 | @Override
55 | protected void onBindTexture() {
56 | super.onBindTexture();
57 | }
58 |
59 | @Override
60 | protected void onSetExpandData() {
61 | super.onSetExpandData();
62 | GLES20.glUniform1f(mHIntensity,intensity);
63 | if(mastTextures[0]!=0){
64 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0+getTextureType()+1);
65 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,mastTextures[0]);
66 | if(mBitmap!=null&&!mBitmap.isRecycled()){
67 | GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0,mBitmap,0);
68 | mBitmap.recycle();
69 | }
70 | GLES20.glUniform1i(mHMaskImage,getTextureType()+1);
71 | }
72 |
73 |
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/image/SGLRender.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * SGLRender.java
4 | *
5 | * Created by Wuwang on 2016/10/15
6 | */
7 | package edu.wuwang.opengl.image;
8 |
9 | import android.graphics.Bitmap;
10 | import android.opengl.GLSurfaceView;
11 | import android.view.View;
12 |
13 |
14 | import javax.microedition.khronos.egl.EGLConfig;
15 | import javax.microedition.khronos.opengles.GL10;
16 |
17 | import edu.wuwang.opengl.image.filter.AFilter;
18 | import edu.wuwang.opengl.image.filter.ColorFilter;
19 | import edu.wuwang.opengl.image.filter.ContrastColorFilter;
20 |
21 | /**
22 | * Description:
23 | */
24 | public class SGLRender implements GLSurfaceView.Renderer {
25 |
26 | private AFilter mFilter;
27 | private Bitmap bitmap;
28 | private int width,height;
29 | private boolean refreshFlag=false;
30 | private EGLConfig config;
31 |
32 | public SGLRender(View mView){
33 | mFilter=new ContrastColorFilter(mView.getContext(), ColorFilter.Filter.NONE);
34 | }
35 |
36 | public void setFilter(AFilter filter){
37 | refreshFlag=true;
38 | mFilter=filter;
39 | if(bitmap!=null){
40 | mFilter.setBitmap(bitmap);
41 | }
42 | }
43 |
44 | public void setImageBuffer(int[] buffer,int width,int height){
45 | bitmap= Bitmap.createBitmap(buffer,width,height, Bitmap.Config.RGB_565);
46 | mFilter.setBitmap(bitmap);
47 | }
48 |
49 | public void refresh(){
50 | refreshFlag=true;
51 | }
52 |
53 | public AFilter getFilter(){
54 | return mFilter;
55 | }
56 |
57 | public void setImage(Bitmap bitmap){
58 | this.bitmap=bitmap;
59 | mFilter.setBitmap(bitmap);
60 | }
61 |
62 | @Override
63 | public void onSurfaceCreated(GL10 gl, EGLConfig config) {
64 | this.config=config;
65 | mFilter.onSurfaceCreated(gl, config);
66 | }
67 |
68 | @Override
69 | public void onSurfaceChanged(GL10 gl, int width, int height) {
70 | this.width=width;
71 | this.height=height;
72 | mFilter.onSurfaceChanged(gl, width, height);
73 | }
74 |
75 | @Override
76 | public void onDrawFrame(GL10 gl) {
77 | if(refreshFlag&&width!=0&&height!=0){
78 | mFilter.onSurfaceCreated(gl, config);
79 | mFilter.onSurfaceChanged(gl,width,height);
80 | refreshFlag=false;
81 | }
82 | mFilter.onDrawFrame(gl);
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/obj/ObjLoadActivity.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.obj;
2 |
3 | import android.opengl.GLSurfaceView;
4 | import android.opengl.Matrix;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 |
8 | import java.io.IOException;
9 |
10 | import javax.microedition.khronos.egl.EGLConfig;
11 | import javax.microedition.khronos.opengles.GL10;
12 |
13 | import edu.wuwang.opengl.BaseActivity;
14 | import edu.wuwang.opengl.R;
15 | import edu.wuwang.opengl.utils.Gl2Utils;
16 |
17 | /**
18 | * Created by wuwang on 2017/1/7
19 | */
20 |
21 | public class ObjLoadActivity extends BaseActivity {
22 |
23 | private GLSurfaceView mGLView;
24 | private ObjFilter mFilter;
25 | private Obj3D obj;
26 |
27 | @Override
28 | protected void onCreate(@Nullable Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | setContentView(R.layout.activity_obj);
31 | mGLView= (GLSurfaceView) findViewById(R.id.mGLView);
32 | mGLView.setEGLContextClientVersion(2);
33 | mFilter=new ObjFilter(getResources());
34 | obj=new Obj3D();
35 | try {
36 | ObjReader.read(getAssets().open("3dres/hat.obj"),obj);
37 | mFilter.setObj3D(obj);
38 | } catch (IOException e) {
39 | e.printStackTrace();
40 | }
41 | mGLView.setRenderer(new GLSurfaceView.Renderer() {
42 | @Override
43 | public void onSurfaceCreated(GL10 gl, EGLConfig config) {
44 | mFilter.create();
45 | }
46 |
47 | @Override
48 | public void onSurfaceChanged(GL10 gl, int width, int height) {
49 | mFilter.onSizeChanged(width, height);
50 | float[] matrix=Gl2Utils.getOriginalMatrix();
51 | Matrix.scaleM(matrix,0,0.2f,0.2f*width/height,0.2f);
52 | mFilter.setMatrix(matrix);
53 | }
54 |
55 | @Override
56 | public void onDrawFrame(GL10 gl) {
57 | Matrix.rotateM(mFilter.getMatrix(),0,0.3f,0,1,0);
58 | mFilter.draw();
59 | }
60 | });
61 | mGLView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
62 | }
63 |
64 |
65 | @Override
66 | protected void onResume() {
67 | super.onResume();
68 | mGLView.onResume();
69 | }
70 |
71 | @Override
72 | protected void onPause() {
73 | super.onPause();
74 | mGLView.onPause();
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/vary/VaryRender.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.vary;
2 |
3 | import android.content.res.Resources;
4 | import android.opengl.GLES20;
5 | import android.opengl.GLSurfaceView;
6 |
7 | import javax.microedition.khronos.egl.EGLConfig;
8 | import javax.microedition.khronos.opengles.GL10;
9 |
10 | import edu.wuwang.opengl.utils.VaryTools;
11 |
12 | /**
13 | * Created by wuwang on 2016/10/30
14 | */
15 |
16 | public class VaryRender implements GLSurfaceView.Renderer {
17 |
18 | private VaryTools tools;
19 | private Cube cube;
20 |
21 | public VaryRender(Resources res){
22 | tools=new VaryTools();
23 | cube=new Cube(res);
24 | }
25 |
26 | @Override
27 | public void onSurfaceCreated(GL10 gl, EGLConfig config) {
28 | GLES20.glClearColor(1.0f,1.0f,1.0f,1.0f);
29 | //开启深度测试
30 | GLES20.glEnable(GLES20.GL_DEPTH_TEST);
31 | cube.create();
32 | }
33 |
34 | @Override
35 | public void onSurfaceChanged(GL10 gl, int width, int height) {
36 | GLES20.glViewport(0,0,width,height);
37 | float rate=width/(float)height;
38 | tools.ortho(-rate*6,rate*6,-6,6,3,20);
39 | tools.setCamera(0,0,10,0,0,0,0,1,0);
40 | }
41 |
42 | @Override
43 | public void onDrawFrame(GL10 gl) {
44 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT| GLES20.GL_DEPTH_BUFFER_BIT);
45 | cube.setMatrix(tools.getFinalMatrix());
46 | cube.drawSelf();
47 |
48 | //y轴正方形平移
49 | tools.pushMatrix();
50 | tools.translate(0,3,0);
51 | cube.setMatrix(tools.getFinalMatrix());
52 | cube.drawSelf();
53 | tools.popMatrix();
54 |
55 | //y轴负方向平移,然后按xyz->(0,0,0)到(1,1,1)旋转30度
56 | tools.pushMatrix();
57 | tools.translate(0,-3,0);
58 | tools.rotate(30f,1,1,1);
59 | cube.setMatrix(tools.getFinalMatrix());
60 | cube.drawSelf();
61 | tools.popMatrix();
62 |
63 | //x轴负方向平移,然后按xyz->(0,0,0)到(1,-1,1)旋转120度,在放大到0.5倍
64 | tools.pushMatrix();
65 | tools.translate(-3,0,0);
66 | tools.scale(0.5f,0.5f,0.5f);
67 |
68 | //在以上变换的基础上再进行变换
69 | tools.pushMatrix();
70 | tools.translate(12,0,0);
71 | tools.scale(1.0f,2.0f,1.0f);
72 | tools.rotate(30f,1,2,1);
73 | cube.setMatrix(tools.getFinalMatrix());
74 | cube.drawSelf();
75 | tools.popMatrix();
76 |
77 | //接着被中断的地方执行
78 | tools.rotate(30f,-1,-1,1);
79 | cube.setMatrix(tools.getFinalMatrix());
80 | cube.drawSelf();
81 | tools.popMatrix();
82 | }
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/utils/EasyGlUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * TextureUtils.java
4 | *
5 | * Created by Wuwang on 2016/12/23
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.utils;
9 |
10 | import android.graphics.Bitmap;
11 | import android.opengl.GLES20;
12 |
13 | /**
14 | * Description:
15 | */
16 | public enum EasyGlUtils {
17 | ;
18 | EasyGlUtils(){
19 |
20 | }
21 |
22 | public static void useTexParameter(){
23 | //设置缩小过滤为使用纹理中坐标最接近的一个像素的颜色作为需要绘制的像素颜色
24 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
25 | //设置放大过滤为使用纹理中坐标最接近的若干个颜色,通过加权平均算法得到需要绘制的像素颜色
26 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
27 | //设置环绕方向S,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
28 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
29 | //设置环绕方向T,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
30 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
31 | }
32 |
33 | public static void useTexParameter(int gl_wrap_s,int gl_wrap_t,int gl_min_filter,
34 | int gl_mag_filter){
35 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,gl_wrap_s);
36 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,gl_wrap_t);
37 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,gl_min_filter);
38 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,gl_mag_filter);
39 | }
40 |
41 | public static void genTexturesWithParameter(int size,int[] textures,int start,
42 | int gl_format,int width,int height){
43 | GLES20.glGenTextures(size, textures, start);
44 | for (int i = 0; i < size; i++) {
45 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[i]);
46 | GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0,gl_format, width, height,
47 | 0, gl_format, GLES20.GL_UNSIGNED_BYTE, null);
48 | useTexParameter();
49 | }
50 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,0);
51 | }
52 |
53 | public static void bindFrameTexture(int frameBufferId,int textureId){
54 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBufferId);
55 | GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
56 | GLES20.GL_TEXTURE_2D, textureId, 0);
57 | }
58 |
59 | public static void unBindFrameBuffer(){
60 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,0);
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
11 |
12 |
18 |
24 |
30 |
36 |
42 |
48 |
54 |
60 |
66 |
67 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/filter/Beauty.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Beauty.java
4 | *
5 | * Created by Wuwang on 2016/11/18
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.filter;
9 |
10 | import android.content.res.Resources;
11 | import android.opengl.GLES20;
12 |
13 | /**
14 | * Description:
15 | */
16 | public class Beauty extends AFilter {
17 |
18 | private int gHaaCoef;
19 | private int gHmixCoef;
20 | private int gHiternum;
21 | private int gHWidth;
22 | private int gHHeight;
23 |
24 | private float aaCoef;
25 | private float mixCoef;
26 | private int iternum;
27 |
28 | private int mWidth=720;
29 | private int mHeight=1280;
30 |
31 |
32 | public Beauty(Resources res) {
33 | super(res);
34 | setFlag(0);
35 | }
36 |
37 | @Override
38 | protected void onCreate() {
39 | createProgramByAssetsFile("shader/beauty/beauty.vert", "shader/beauty/beauty.frag");
40 | gHaaCoef=GLES20.glGetUniformLocation(mProgram,"aaCoef");
41 | gHmixCoef=GLES20.glGetUniformLocation(mProgram,"mixCoef");
42 | gHiternum=GLES20.glGetUniformLocation(mProgram,"iternum");
43 | gHWidth=GLES20.glGetUniformLocation(mProgram,"mWidth");
44 | gHHeight=GLES20.glGetUniformLocation(mProgram,"mHeight");
45 | }
46 |
47 | @Override
48 | public void setFlag(int flag) {
49 | super.setFlag(flag);
50 | switch (flag){
51 | case 1:
52 | a(1,0.19f,0.54f);
53 | break;
54 | case 2:
55 | a(2,0.29f,0.54f);
56 | break;
57 | case 3:
58 | a(3,0.17f,0.39f);
59 | break;
60 | case 4:
61 | a(3,0.25f,0.54f);
62 | break;
63 | case 5:
64 | a(4,0.13f,0.54f);
65 | break;
66 | case 6:
67 | a(4,0.19f,0.69f);
68 | break;
69 | default:
70 | a(0,0f,0f);
71 | break;
72 | }
73 | }
74 |
75 | private void a(int a,float b,float c){
76 | this.iternum=a;
77 | this.aaCoef=b;
78 | this.mixCoef=c;
79 | }
80 |
81 | @Override
82 | protected void onSizeChanged(int width, int height) {
83 | this.mWidth=width;
84 | this.mHeight=height;
85 | }
86 |
87 | @Override
88 | protected void onSetExpandData() {
89 | super.onSetExpandData();
90 | GLES20.glUniform1i(gHWidth,mWidth);
91 | GLES20.glUniform1i(gHHeight,mHeight);
92 | GLES20.glUniform1f(gHaaCoef,aaCoef);
93 | GLES20.glUniform1f(gHmixCoef,mixCoef);
94 | GLES20.glUniform1i(gHiternum,iternum);
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/camera/CameraActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * CameraActivity.java
4 | *
5 | * Created by Wuwang on 2016/11/14
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.camera;
9 |
10 | import android.Manifest;
11 | import android.os.Bundle;
12 | import android.support.annotation.NonNull;
13 | import android.support.annotation.Nullable;
14 | import android.view.Menu;
15 | import android.view.MenuItem;
16 | import android.widget.Toast;
17 |
18 | import edu.wuwang.opengl.BaseActivity;
19 | import edu.wuwang.opengl.R;
20 | import edu.wuwang.opengl.utils.PermissionUtils;
21 |
22 | /**
23 | * Description:
24 | */
25 | public class CameraActivity extends BaseActivity {
26 |
27 | private CameraView mCameraView;
28 |
29 | @Override
30 | protected void onCreate(@Nullable Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | PermissionUtils.askPermission(this,new String[]{Manifest.permission.CAMERA,Manifest
33 | .permission.WRITE_EXTERNAL_STORAGE},10,initViewRunnable);
34 | }
35 |
36 | private Runnable initViewRunnable=new Runnable() {
37 | @Override
38 | public void run() {
39 | setContentView(R.layout.activity_camera);
40 | mCameraView= (CameraView)findViewById(R.id.mCameraView);
41 | }
42 | };
43 |
44 | @Override
45 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
46 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
47 | PermissionUtils.onRequestPermissionsResult(requestCode == 10, grantResults, initViewRunnable,
48 | new Runnable() {
49 | @Override
50 | public void run() {
51 | Toast.makeText(CameraActivity.this, "没有获得必要的权限", Toast.LENGTH_SHORT).show();
52 | finish();
53 | }
54 | });
55 | }
56 |
57 | @Override
58 | protected void onResume() {
59 | super.onResume();
60 | mCameraView.onResume();
61 | }
62 |
63 | @Override
64 | protected void onPause() {
65 | super.onPause();
66 | mCameraView.onPause();
67 | }
68 |
69 | @Override
70 | public boolean onCreateOptionsMenu(Menu menu) {
71 | menu.add("切换摄像头").setTitle("切换摄像头").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
72 | return super.onCreateOptionsMenu(menu);
73 | }
74 |
75 | @Override
76 | public boolean onOptionsItemSelected(MenuItem item) {
77 | String name=item.getTitle().toString();
78 | if(name.equals("切换摄像头")){
79 | mCameraView.switchCamera();
80 | }
81 | return super.onOptionsItemSelected(item);
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/app/src/main/assets/filter/half_color_fragment.sh:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 |
3 | uniform sampler2D vTexture;
4 | uniform int vChangeType;
5 | uniform vec3 vChangeColor;
6 | uniform int vIsHalf;
7 | uniform float uXY;
8 |
9 | varying vec4 gPosition;
10 |
11 | varying vec2 aCoordinate;
12 | varying vec4 aPos;
13 |
14 | void modifyColor(vec4 color){
15 | color.r=max(min(color.r,1.0),0.0);
16 | color.g=max(min(color.g,1.0),0.0);
17 | color.b=max(min(color.b,1.0),0.0);
18 | color.a=max(min(color.a,1.0),0.0);
19 | }
20 |
21 | void main(){
22 | vec4 nColor=texture2D(vTexture,aCoordinate);
23 | if(aPos.x>0.0||vIsHalf==0){
24 | if(vChangeType==1){
25 | float c=nColor.r*vChangeColor.r+nColor.g*vChangeColor.g+nColor.b*vChangeColor.b;
26 | gl_FragColor=vec4(c,c,c,nColor.a);
27 | }else if(vChangeType==2){
28 | vec4 deltaColor=nColor+vec4(vChangeColor,0.0);
29 | modifyColor(deltaColor);
30 | gl_FragColor=deltaColor;
31 | }else if(vChangeType==3){
32 | nColor+=texture2D(vTexture,vec2(aCoordinate.x-vChangeColor.r,aCoordinate.y-vChangeColor.r));
33 | nColor+=texture2D(vTexture,vec2(aCoordinate.x-vChangeColor.r,aCoordinate.y+vChangeColor.r));
34 | nColor+=texture2D(vTexture,vec2(aCoordinate.x+vChangeColor.r,aCoordinate.y-vChangeColor.r));
35 | nColor+=texture2D(vTexture,vec2(aCoordinate.x+vChangeColor.r,aCoordinate.y+vChangeColor.r));
36 | nColor+=texture2D(vTexture,vec2(aCoordinate.x-vChangeColor.g,aCoordinate.y-vChangeColor.g));
37 | nColor+=texture2D(vTexture,vec2(aCoordinate.x-vChangeColor.g,aCoordinate.y+vChangeColor.g));
38 | nColor+=texture2D(vTexture,vec2(aCoordinate.x+vChangeColor.g,aCoordinate.y-vChangeColor.g));
39 | nColor+=texture2D(vTexture,vec2(aCoordinate.x+vChangeColor.g,aCoordinate.y+vChangeColor.g));
40 | nColor+=texture2D(vTexture,vec2(aCoordinate.x-vChangeColor.b,aCoordinate.y-vChangeColor.b));
41 | nColor+=texture2D(vTexture,vec2(aCoordinate.x-vChangeColor.b,aCoordinate.y+vChangeColor.b));
42 | nColor+=texture2D(vTexture,vec2(aCoordinate.x+vChangeColor.b,aCoordinate.y-vChangeColor.b));
43 | nColor+=texture2D(vTexture,vec2(aCoordinate.x+vChangeColor.b,aCoordinate.y+vChangeColor.b));
44 | nColor/=13.0;
45 | gl_FragColor=nColor;
46 | }else if(vChangeType==4){
47 | float dis=distance(vec2(gPosition.x,gPosition.y/uXY),vec2(vChangeColor.r,vChangeColor.g));
48 | if(dis
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
20 |
22 |
23 |
24 |
25 |
26 |
27 |
29 |
30 |
32 |
33 |
35 |
36 |
38 |
39 |
41 |
43 |
45 |
47 |
49 |
51 |
53 |
55 |
57 |
59 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/obj/ObjLoadActivity2.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.obj;
2 |
3 | import android.opengl.GLES20;
4 | import android.opengl.GLSurfaceView;
5 | import android.opengl.Matrix;
6 | import android.os.Bundle;
7 | import android.support.annotation.Nullable;
8 | import android.support.v7.app.AppCompatActivity;
9 |
10 | import java.io.IOException;
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | import javax.microedition.khronos.egl.EGLConfig;
15 | import javax.microedition.khronos.opengles.GL10;
16 |
17 | import edu.wuwang.opengl.R;
18 | import edu.wuwang.opengl.utils.Gl2Utils;
19 |
20 | /**
21 | * Created by wuwang on 2017/2/23
22 | */
23 |
24 | public class ObjLoadActivity2 extends AppCompatActivity {
25 |
26 | private GLSurfaceView mGLView;
27 | private List filters;
28 |
29 |
30 | @Override
31 | protected void onCreate(@Nullable Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_obj);
34 | mGLView= (GLSurfaceView) findViewById(R.id.mGLView);
35 | mGLView.setEGLContextClientVersion(2);
36 | List model=ObjReader.readMultiObj(this,"assets/3dres/pikachu.obj");
37 | filters=new ArrayList<>();
38 | for (int i=0;i tempVert;
23 | private ArrayList tempVertNorl;
24 | public ArrayList tempVertTexture;
25 |
26 | public int textureSMode;
27 | public int textureTMode;
28 |
29 | public void addVert(float d){
30 | if(tempVert==null){
31 | tempVert=new ArrayList<>();
32 | }
33 | tempVert.add(d);
34 | }
35 |
36 | public void addVertTexture(float d){
37 | if(tempVertTexture==null){
38 | tempVertTexture=new ArrayList<>();
39 | }
40 | tempVertTexture.add(d);
41 | }
42 |
43 | public void addVertNorl(float d){
44 | if(tempVertNorl==null){
45 | tempVertNorl=new ArrayList<>();
46 | }
47 | tempVertNorl.add(d);
48 | }
49 |
50 | public void dataLock(){
51 | if(tempVert!=null){
52 | setVert(tempVert);
53 | tempVert.clear();
54 | tempVert=null;
55 | }
56 | if(tempVertTexture!=null){
57 | setVertTexture(tempVertTexture);
58 | tempVertTexture.clear();
59 | tempVertTexture=null;
60 | }
61 | if(tempVertNorl!=null){
62 | setVertNorl(tempVertNorl);
63 | tempVertNorl.clear();
64 | tempVertNorl=null;
65 | }
66 | }
67 |
68 | public void setVert(ArrayList data){
69 | int size=data.size();
70 | ByteBuffer buffer=ByteBuffer.allocateDirect(size*4);
71 | buffer.order(ByteOrder.nativeOrder());
72 | vert=buffer.asFloatBuffer();
73 | for (int i=0;i data){
81 | int size=data.size();
82 | ByteBuffer buffer=ByteBuffer.allocateDirect(size*4);
83 | buffer.order(ByteOrder.nativeOrder());
84 | vertNorl=buffer.asFloatBuffer();
85 | for (int i=0;i data){
92 | int size=data.size();
93 | ByteBuffer buffer=ByteBuffer.allocateDirect(size*4);
94 | buffer.order(ByteOrder.nativeOrder());
95 | vertTexture=buffer.asFloatBuffer();
96 | for (int i=0;i sensors=mSensorManager.getSensorList(Sensor.TYPE_ALL);
38 | //todo 判断是否存在rotation vector sensor
39 | mRotation=mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
40 |
41 | mGLView=(GLSurfaceView) findViewById(R.id.mGLView);
42 | mGLView.setEGLContextClientVersion(2);
43 | mGLView.setRenderer(this);
44 | mGLView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
45 |
46 | mSkySphere=new SkySphere(this.getApplicationContext(),"vr/360sp.jpg");
47 | }
48 |
49 | @Override
50 | protected void onResume() {
51 | super.onResume();
52 | mSensorManager.registerListener(this,mRotation,SensorManager.SENSOR_DELAY_GAME);
53 | mGLView.onResume();
54 | }
55 |
56 | @Override
57 | protected void onPause() {
58 | super.onPause();
59 | mSensorManager.unregisterListener(this);
60 | mGLView.onPause();
61 | }
62 |
63 | @Override
64 | public void onSurfaceCreated(GL10 gl, EGLConfig config) {
65 | mSkySphere.create();
66 | GLES20.glEnable(GLES20.GL_DEPTH_TEST);
67 | GLES20.glEnable(GLES20.GL_CULL_FACE);
68 | GLES20.glCullFace(GLES20.GL_FRONT);
69 | }
70 |
71 | @Override
72 | public void onSurfaceChanged(GL10 gl, int width, int height) {
73 | mSkySphere.setSize(width, height);
74 | GLES20.glViewport(0,0,width,height);
75 | }
76 |
77 | @Override
78 | public void onDrawFrame(GL10 gl) {
79 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
80 | GLES20.glClearColor(1,1,1,1);
81 | mSkySphere.draw();
82 | }
83 |
84 | @Override
85 | public void onSensorChanged(SensorEvent event) {
86 | SensorManager.getRotationMatrixFromVector(matrix,event.values);
87 | mSkySphere.setMatrix(matrix);
88 | }
89 |
90 | @Override
91 | public void onAccuracyChanged(Sensor sensor, int accuracy) {
92 |
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/utils/ShaderUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * ShaderUtils.java
4 | *
5 | * Created by Wuwang on 2016/10/8
6 | */
7 | package edu.wuwang.opengl.utils;
8 |
9 | import android.content.Context;
10 | import android.content.res.Resources;
11 | import android.opengl.GLES20;
12 | import android.util.Log;
13 |
14 | import java.io.InputStream;
15 |
16 | /**
17 | * Description:
18 | */
19 | public class ShaderUtils {
20 |
21 | private static final String TAG="ShaderUtils";
22 |
23 | private ShaderUtils(){
24 | }
25 |
26 | public static void checkGLError(String op){
27 | Log.e("wuwang",op);
28 | }
29 |
30 | public static int loadShader(int shaderType,String source){
31 | int shader= GLES20.glCreateShader(shaderType);
32 | if(0!=shader){
33 | GLES20.glShaderSource(shader,source);
34 | GLES20.glCompileShader(shader);
35 | int[] compiled=new int[1];
36 | GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS,compiled,0);
37 | if(compiled[0]==0){
38 | Log.e(TAG,"Could not compile shader:"+shaderType);
39 | Log.e(TAG,"GLES20 Error:"+ GLES20.glGetShaderInfoLog(shader));
40 | GLES20.glDeleteShader(shader);
41 | shader=0;
42 | }
43 | }
44 | return shader;
45 | }
46 |
47 | public static int loadShader(Resources res,int shaderType,String resName){
48 | return loadShader(shaderType,loadFromAssetsFile(resName,res));
49 | }
50 |
51 | public static int createProgram(String vertexSource, String fragmentSource){
52 | int vertex=loadShader(GLES20.GL_VERTEX_SHADER,vertexSource);
53 | if(vertex==0)return 0;
54 | int fragment=loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentSource);
55 | if(fragment==0)return 0;
56 | int program= GLES20.glCreateProgram();
57 | if(program!=0){
58 | GLES20.glAttachShader(program,vertex);
59 | checkGLError("Attach Vertex Shader");
60 | GLES20.glAttachShader(program,fragment);
61 | checkGLError("Attach Fragment Shader");
62 | GLES20.glLinkProgram(program);
63 | int[] linkStatus=new int[1];
64 | GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS,linkStatus,0);
65 | if(linkStatus[0]!= GLES20.GL_TRUE){
66 | Log.e(TAG,"Could not link program:"+ GLES20.glGetProgramInfoLog(program));
67 | GLES20.glDeleteProgram(program);
68 | program=0;
69 | }
70 | }
71 | return program;
72 | }
73 |
74 | public static int createProgram(Resources res,String vertexRes,String fragmentRes){
75 | return createProgram(loadFromAssetsFile(vertexRes,res),loadFromAssetsFile(fragmentRes,res));
76 | }
77 |
78 | public static String loadFromAssetsFile(String fname, Resources res){
79 | StringBuilder result=new StringBuilder();
80 | try{
81 | InputStream is=res.getAssets().open(fname);
82 | int ch;
83 | byte[] buffer=new byte[1024];
84 | while (-1!=(ch=is.read(buffer))){
85 | result.append(new String(buffer,0,ch));
86 | }
87 | }catch (Exception e){
88 | return null;
89 | }
90 | return result.toString().replaceAll("\\r\\n","\n");
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/filter/WaterMarkFilter.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * WaterMarkFilter.java
4 | *
5 | * Created by Wuwang on 2016/12/15
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.filter;
9 |
10 | import android.content.res.Resources;
11 | import android.graphics.Bitmap;
12 | import android.opengl.GLES20;
13 | import android.opengl.GLUtils;
14 |
15 | import edu.wuwang.opengl.utils.MatrixUtils;
16 |
17 | /**
18 | * 水印Filter示例
19 | */
20 | public class WaterMarkFilter extends NoFilter {
21 |
22 | private Bitmap mBitmap;
23 | private NoFilter mFilter;
24 | private int width,height;
25 |
26 | private int x,y,w,h;
27 |
28 | public WaterMarkFilter(Resources mRes) {
29 | super(mRes);
30 | mFilter=new NoFilter(mRes){
31 | @Override
32 | protected void onClear() {
33 |
34 | }
35 | };
36 | }
37 |
38 | @Override
39 | protected void onCreate() {
40 | super.onCreate();
41 | mFilter.create();
42 | createTexture();
43 | }
44 |
45 | @Override
46 | protected void onClear() {
47 | super.onClear();
48 | }
49 |
50 | @Override
51 | public void draw() {
52 | super.draw();
53 | GLES20.glViewport(x,y,w==0?mBitmap.getWidth():w,h==0?mBitmap.getHeight():h);
54 | GLES20.glDisable(GLES20.GL_DEPTH_TEST);
55 | GLES20.glEnable(GLES20.GL_BLEND);
56 | GLES20.glBlendFunc(GLES20.GL_SRC_COLOR,GLES20.GL_DST_ALPHA);
57 | mFilter.draw();
58 | GLES20.glDisable(GLES20.GL_BLEND);
59 | GLES20.glViewport(0,0,width,height);
60 | }
61 |
62 | @Override
63 | protected void onSizeChanged(int width, int height) {
64 | this.width=width;
65 | this.height=height;
66 | mFilter.setSize(width,height);
67 | }
68 |
69 | public void setWaterMark(Bitmap bitmap){
70 | if(this.mBitmap!=null){
71 | this.mBitmap.recycle();
72 | }
73 | this.mBitmap=bitmap;
74 | }
75 |
76 | private int[] textures=new int[1];
77 | private void createTexture(){
78 | if(mBitmap!=null){
79 | //生成纹理
80 | GLES20.glGenTextures(1,textures,0);
81 | //生成纹理
82 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,textures[0]);
83 | //设置缩小过滤为使用纹理中坐标最接近的一个像素的颜色作为需要绘制的像素颜色
84 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
85 | //设置放大过滤为使用纹理中坐标最接近的若干个颜色,通过加权平均算法得到需要绘制的像素颜色
86 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
87 | //设置环绕方向S,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
88 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
89 | //设置环绕方向T,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
90 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
91 | GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, mBitmap, 0);
92 | MatrixUtils.flip(mFilter.getMatrix(),false,true);
93 |
94 | mFilter.setTextureId(textures[0]);
95 | }
96 | }
97 |
98 | public void setPosition(int x,int y,int width,int height){
99 | this.x=x;
100 | this.y=y;
101 | this.w=width;
102 | this.h=height;
103 | }
104 |
105 | }
106 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/obj/ObjFilter.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.obj;
2 |
3 | import android.content.res.Resources;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.opengl.GLES20;
7 | import android.opengl.GLUtils;
8 |
9 | import java.io.IOException;
10 |
11 | import edu.wuwang.opengl.filter.AFilter;
12 | import edu.wuwang.opengl.utils.Gl2Utils;
13 |
14 | /**
15 | * Created by wuwang on 2017/1/8
16 | */
17 |
18 | public class ObjFilter extends AFilter {
19 |
20 | private int vertCount;
21 |
22 | private int mHNormal;
23 | private Obj3D obj;
24 |
25 | private int textureId;
26 |
27 | public ObjFilter(Resources mRes) {
28 | super(mRes);
29 | }
30 |
31 | public void setObj3D(Obj3D obj){
32 | this.obj=obj;
33 | }
34 |
35 | @Override
36 | protected void initBuffer() {
37 | super.initBuffer();
38 | }
39 |
40 | @Override
41 | protected void onCreate() {
42 | createProgramByAssetsFile("3dres/obj.vert","3dres/obj.frag");
43 | mHNormal=GLES20.glGetAttribLocation(mProgram,"vNormal");
44 | //打开深度检测
45 | GLES20.glEnable(GLES20.GL_DEPTH_TEST);
46 | if(obj.vertTexture!=null){
47 | try {
48 | textureId=createTexture(BitmapFactory.decodeStream(mRes.getAssets().open("3dres/"+obj.mtl.map_Kd)));
49 | setTextureId(textureId);
50 | } catch (IOException e) {
51 | e.printStackTrace();
52 | }
53 | }
54 | }
55 |
56 | @Override
57 | protected void onClear() {
58 | super.onClear();
59 | }
60 |
61 | @Override
62 | protected void onDraw() {
63 | GLES20.glEnableVertexAttribArray(mHPosition);
64 | GLES20.glVertexAttribPointer(mHPosition,3, GLES20.GL_FLOAT, false, 3*4,obj.vert);
65 | GLES20.glEnableVertexAttribArray(mHNormal);
66 | GLES20.glVertexAttribPointer(mHNormal,3, GLES20.GL_FLOAT, false, 3*4,obj.vertNorl);
67 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,obj.vertCount);
68 | GLES20.glDisableVertexAttribArray(mHPosition);
69 | GLES20.glDisableVertexAttribArray(mHNormal);
70 | // GLES20.glDisableVertexAttribArray(mHCoord);
71 | }
72 |
73 | @Override
74 | protected void onSizeChanged(int width, int height) {
75 | GLES20.glViewport(0,0,width,height);
76 | }
77 |
78 | private int createTexture(Bitmap bitmap){
79 | int[] texture=new int[1];
80 | if(bitmap!=null&&!bitmap.isRecycled()){
81 | //生成纹理
82 | GLES20.glGenTextures(1,texture,0);
83 | //生成纹理
84 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,texture[0]);
85 | //设置缩小过滤为使用纹理中坐标最接近的一个像素的颜色作为需要绘制的像素颜色
86 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
87 | //设置放大过滤为使用纹理中坐标最接近的若干个颜色,通过加权平均算法得到需要绘制的像素颜色
88 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
89 | //设置环绕方向S,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
90 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
91 | //设置环绕方向T,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
92 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
93 | //根据以上指定的参数,生成一个2D纹理
94 | GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
95 | return texture[0];
96 | }
97 | return 0;
98 | }
99 |
100 | }
101 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/camera/TextureFilter.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * TrackFilter.java
4 | *
5 | * Created by Wuwang on 2016/12/21
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.camera;
9 |
10 | import java.nio.ByteBuffer;
11 |
12 | import android.content.res.Resources;
13 | import android.graphics.SurfaceTexture;
14 | import android.opengl.GLES11Ext;
15 | import android.opengl.GLES20;
16 | import android.util.Log;
17 |
18 | import edu.wuwang.opengl.filter.AFilter;
19 | import edu.wuwang.opengl.filter.CameraFilter;
20 | import edu.wuwang.opengl.utils.EasyGlUtils;
21 |
22 | /**
23 | * Description:
24 | */
25 | public class TextureFilter extends AFilter {
26 |
27 | private CameraFilter mFilter;
28 | private int width=0;
29 | private int height=0;
30 |
31 | private int[] fFrame = new int[1];
32 | private int[] fTexture = new int[1];
33 | private int[] mCameraTexture=new int[1];
34 |
35 | private SurfaceTexture mSurfaceTexture;
36 | private float[] mCoordOM=new float[16];
37 |
38 | //获取Track数据
39 | private ByteBuffer tBuffer;
40 |
41 | public TextureFilter(Resources mRes) {
42 | super(mRes);
43 | mFilter=new CameraFilter(mRes);
44 | }
45 |
46 | public void setCoordMatrix(float[] matrix){
47 | mFilter.setCoordMatrix(matrix);
48 | }
49 |
50 | public SurfaceTexture getTexture(){
51 | return mSurfaceTexture;
52 | }
53 |
54 | @Override
55 | public void setFlag(int flag) {
56 | mFilter.setFlag(flag);
57 | }
58 |
59 | @Override
60 | protected void initBuffer() {
61 |
62 | }
63 |
64 | @Override
65 | public void setMatrix(float[] matrix) {
66 | mFilter.setMatrix(matrix);
67 | }
68 |
69 | @Override
70 | public int getOutputTexture() {
71 | return fTexture[0];
72 | }
73 |
74 | @Override
75 | public void draw() {
76 | boolean a=GLES20.glIsEnabled(GLES20.GL_DEPTH_TEST);
77 | if(a){
78 | GLES20.glDisable(GLES20.GL_DEPTH_TEST);
79 | }
80 | if(mSurfaceTexture!=null){
81 | mSurfaceTexture.updateTexImage();
82 | mSurfaceTexture.getTransformMatrix(mCoordOM);
83 | mFilter.setCoordMatrix(mCoordOM);
84 | }
85 | EasyGlUtils.bindFrameTexture(fFrame[0],fTexture[0]);
86 | GLES20.glViewport(0,0,width,height);
87 | mFilter.setTextureId(mCameraTexture[0]);
88 | mFilter.draw();
89 | Log.e("wuwang","textureFilter draw");
90 | EasyGlUtils.unBindFrameBuffer();
91 |
92 | if(a){
93 | GLES20.glEnable(GLES20.GL_DEPTH_TEST);
94 | }
95 | }
96 |
97 | @Override
98 | protected void onCreate() {
99 | mFilter.create();
100 | createOesTexture();
101 | mSurfaceTexture=new SurfaceTexture(mCameraTexture[0]);
102 | }
103 |
104 | @Override
105 | protected void onSizeChanged(int width, int height) {
106 | mFilter.setSize(width, height);
107 | if(this.width!=width||this.height!=height){
108 | this.width=width;
109 | this.height=height;
110 | //创建FrameBuffer和Texture
111 | deleteFrameBuffer();
112 | GLES20.glGenFramebuffers(1, fFrame, 0);
113 | EasyGlUtils.genTexturesWithParameter(1, fTexture,0,GLES20.GL_RGBA,width,height);
114 | }
115 | }
116 |
117 | private void deleteFrameBuffer() {
118 | GLES20.glDeleteFramebuffers(1, fFrame, 0);
119 | GLES20.glDeleteTextures(1, fTexture,0);
120 | }
121 |
122 | private void createOesTexture(){
123 | GLES20.glGenTextures(1,mCameraTexture,0);
124 | }
125 |
126 | }
127 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/camera/CameraDrawer.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * CameraDrawer.java
4 | *
5 | * Created by Wuwang on 2016/11/5
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.camera;
9 |
10 | import java.nio.ByteBuffer;
11 | import java.nio.ByteOrder;
12 | import java.nio.FloatBuffer;
13 | import java.nio.IntBuffer;
14 | import java.util.Arrays;
15 | import javax.microedition.khronos.egl.EGLConfig;
16 | import javax.microedition.khronos.opengles.GL10;
17 |
18 | import android.content.res.Resources;
19 | import android.graphics.Bitmap;
20 | import android.graphics.SurfaceTexture;
21 | import android.opengl.GLES11Ext;
22 | import android.opengl.GLES20;
23 | import android.opengl.GLSurfaceView;
24 | import android.opengl.Matrix;
25 | import android.util.Log;
26 |
27 | import edu.wuwang.opengl.filter.AFilter;
28 | import edu.wuwang.opengl.filter.OesFilter;
29 | import edu.wuwang.opengl.utils.Gl2Utils;
30 | import edu.wuwang.opengl.utils.ShaderUtils;
31 |
32 | /**
33 | * Description:
34 | */
35 | public class CameraDrawer implements GLSurfaceView.Renderer {
36 |
37 | private float[] matrix=new float[16];
38 | private SurfaceTexture surfaceTexture;
39 | private int width,height;
40 | private int dataWidth,dataHeight;
41 | private AFilter mOesFilter;
42 | private int cameraId=1;
43 |
44 | public CameraDrawer(Resources res){
45 | mOesFilter=new OesFilter(res);
46 | }
47 | public void setDataSize(int dataWidth,int dataHeight){
48 | this.dataWidth=dataWidth;
49 | this.dataHeight=dataHeight;
50 | calculateMatrix();
51 | }
52 |
53 | public void setViewSize(int width,int height){
54 | this.width=width;
55 | this.height=height;
56 | calculateMatrix();
57 | }
58 |
59 | private void calculateMatrix(){
60 | Gl2Utils.getShowMatrix(matrix,this.dataWidth,this.dataHeight,this.width,this.height);
61 | if(cameraId==1){
62 | Gl2Utils.flip(matrix,true,false);
63 | Gl2Utils.rotate(matrix,90);
64 | }else{
65 | Gl2Utils.rotate(matrix,270);
66 | }
67 | mOesFilter.setMatrix(matrix);
68 | }
69 |
70 | public SurfaceTexture getSurfaceTexture(){
71 | return surfaceTexture;
72 | }
73 |
74 | public void setCameraId(int id){
75 | this.cameraId=id;
76 | calculateMatrix();
77 | }
78 |
79 | @Override
80 | public void onSurfaceCreated(GL10 gl, EGLConfig config) {
81 | int texture = createTextureID();
82 | surfaceTexture=new SurfaceTexture(texture);
83 | mOesFilter.create();
84 | mOesFilter.setTextureId(texture);
85 | }
86 |
87 | @Override
88 | public void onSurfaceChanged(GL10 gl, int width, int height) {
89 | setViewSize(width,height);
90 | }
91 |
92 | @Override
93 | public void onDrawFrame(GL10 gl) {
94 | if(surfaceTexture!=null){
95 | surfaceTexture.updateTexImage();
96 | }
97 | mOesFilter.draw();
98 | }
99 |
100 | private int createTextureID(){
101 | int[] texture = new int[1];
102 | GLES20.glGenTextures(1, texture, 0);
103 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]);
104 | GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
105 | GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);
106 | GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
107 | GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
108 | GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
109 | GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
110 | GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
111 | GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
112 | return texture[0];
113 | }
114 |
115 | }
116 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/render/Cone.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Cone.java
4 | *
5 | * Created by Wuwang on 2016/10/14
6 | */
7 | package edu.wuwang.opengl.render;
8 |
9 | import android.opengl.GLES20;
10 | import android.opengl.Matrix;
11 | import android.util.Log;
12 | import android.view.View;
13 |
14 | import java.nio.ByteBuffer;
15 | import java.nio.ByteOrder;
16 | import java.nio.FloatBuffer;
17 | import java.util.ArrayList;
18 |
19 | import javax.microedition.khronos.egl.EGLConfig;
20 | import javax.microedition.khronos.opengles.GL10;
21 |
22 | import edu.wuwang.opengl.utils.ShaderUtils;
23 |
24 | /**
25 | * Description:圆锥
26 | */
27 | public class Cone extends Shape {
28 |
29 | private int mProgram;
30 |
31 | private Oval oval;
32 | private FloatBuffer vertexBuffer;
33 |
34 | private float[] mViewMatrix=new float[16];
35 | private float[] mProjectMatrix=new float[16];
36 | private float[] mMVPMatrix=new float[16];
37 |
38 | private int n=360; //切割份数
39 | private float height=2.0f; //圆锥高度
40 | private float radius=1.0f; //圆锥底面半径
41 | private float[] colors={1.0f,1.0f,1.0f,1.0f};
42 |
43 | private int vSize;
44 |
45 | public Cone(View mView){
46 | super(mView);
47 | oval=new Oval(mView);
48 | ArrayList pos=new ArrayList<>();
49 | pos.add(0.0f);
50 | pos.add(0.0f);
51 | pos.add(height);
52 | float angDegSpan=360f/n;
53 | for(float i=0;i<360+angDegSpan;i+=angDegSpan){
54 | pos.add((float) (radius*Math.sin(i*Math.PI/180f)));
55 | pos.add((float)(radius*Math.cos(i*Math.PI/180f)));
56 | pos.add(0.0f);
57 | }
58 | float[] d=new float[pos.size()];
59 | for (int i=0;i pos=new ArrayList<>();
43 | float angDegSpan=360f/n;
44 | for(float i=0;i<360+angDegSpan;i+=angDegSpan){
45 | pos.add((float) (radius*Math.sin(i*Math.PI/180f)));
46 | pos.add((float)(radius*Math.cos(i*Math.PI/180f)));
47 | pos.add(height);
48 | pos.add((float) (radius*Math.sin(i*Math.PI/180f)));
49 | pos.add((float)(radius*Math.cos(i*Math.PI/180f)));
50 | pos.add(0.0f);
51 | }
52 | float[] d=new float[pos.size()];
53 | for (int i=0;i data=new ArrayList<>();
48 | float r1,r2;
49 | float h1,h2;
50 | float sin,cos;
51 | for(float i=-90;i<90+step;i+=step){
52 | r1 = (float)Math.cos(i * Math.PI / 180.0);
53 | r2 = (float)Math.cos((i + step) * Math.PI / 180.0);
54 | h1 = (float)Math.sin(i * Math.PI / 180.0);
55 | h2 = (float)Math.sin((i + step) * Math.PI / 180.0);
56 | // 固定纬度, 360 度旋转遍历一条纬线
57 | float step2=step*2;
58 | for (float j = 0.0f; j <360.0f+step; j +=step2 ) {
59 | cos = (float) Math.cos(j * Math.PI / 180.0);
60 | sin = -(float) Math.sin(j * Math.PI / 180.0);
61 |
62 | data.add(r2 * cos);
63 | data.add(h2);
64 | data.add(r2 * sin);
65 | data.add(r1 * cos);
66 | data.add(h1);
67 | data.add(r1 * sin);
68 | }
69 | }
70 | float[] f=new float[data.size()];
71 | for(int i=0;i data;
33 |
34 | @Override
35 | protected void onCreate(Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 | setContentView(R.layout.activity_main);
38 | mList= (RecyclerView)findViewById(R.id.mList);
39 | mList.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
40 | data=new ArrayList<>();
41 | add("绘制形体",FGLViewActivity.class);
42 | add("图片处理",SGLViewActivity.class);
43 | add("图形变换",VaryActivity.class);
44 | add("相机",CameraActivity.class);
45 | add("相机2 动画",Camera2Activity.class);
46 | add("相机3 美颜",Camera3Activity.class);
47 | add("压缩纹理动画",ZipActivity.class);
48 | add("FBO使用",FBOActivity.class);
49 | add("EGL后台处理",EGLBackEnvActivity.class);
50 | add("3D obj模型",ObjLoadActivity.class);
51 | add("obj+mtl模型",ObjLoadActivity2.class);
52 | add("VR效果",VrContextActivity.class);
53 | add("颜色混合",BlendActivity.class);
54 | add("光照",LightActivity.class);
55 | mList.setAdapter(new MenuAdapter());
56 | }
57 |
58 | private void add(String name,Class> clazz){
59 | MenuBean bean=new MenuBean();
60 | bean.name=name;
61 | bean.clazz=clazz;
62 | data.add(bean);
63 | }
64 |
65 | private class MenuBean{
66 |
67 | String name;
68 | Class> clazz;
69 |
70 | }
71 |
72 | private class MenuAdapter extends RecyclerView.Adapter{
73 |
74 |
75 | @Override
76 | public MenuHolder onCreateViewHolder(ViewGroup parent, int viewType) {
77 | return new MenuHolder(getLayoutInflater().inflate(R.layout.item_button,parent,false));
78 | }
79 |
80 | @Override
81 | public void onBindViewHolder(MenuHolder holder, int position) {
82 | holder.setPosition(position);
83 | }
84 |
85 | @Override
86 | public int getItemCount() {
87 | return data.size();
88 | }
89 |
90 | class MenuHolder extends RecyclerView.ViewHolder{
91 |
92 | private Button mBtn;
93 |
94 | MenuHolder(View itemView) {
95 | super(itemView);
96 | mBtn= (Button)itemView.findViewById(R.id.mBtn);
97 | mBtn.setOnClickListener(MainActivity.this);
98 | }
99 |
100 | public void setPosition(int position){
101 | MenuBean bean=data.get(position);
102 | mBtn.setText(bean.name);
103 | mBtn.setTag(position);
104 | }
105 | }
106 |
107 | }
108 |
109 | @Override
110 | public void onClick(View view){
111 | int position= (int)view.getTag();
112 | MenuBean bean=data.get(position);
113 | startActivity(new Intent(this,bean.clazz));
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/render/Ball.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.render;
2 |
3 | import android.opengl.GLES20;
4 | import android.opengl.Matrix;
5 | import android.util.Log;
6 | import android.view.View;
7 |
8 | import java.nio.ByteBuffer;
9 | import java.nio.ByteOrder;
10 | import java.nio.FloatBuffer;
11 | import java.util.ArrayList;
12 |
13 | import javax.microedition.khronos.egl.EGLConfig;
14 | import javax.microedition.khronos.opengles.GL10;
15 |
16 | import edu.wuwang.opengl.utils.ShaderUtils;
17 |
18 | /**
19 | * Created by wuwang on 2016/10/15
20 | */
21 | public class Ball extends Shape {
22 |
23 | private float step=5f;
24 | private FloatBuffer vertexBuffer;
25 | private int vSize;
26 |
27 | private int mProgram;
28 | private float[] mViewMatrix=new float[16];
29 | private float[] mProjectMatrix=new float[16];
30 | private float[] mMVPMatrix=new float[16];
31 |
32 | public Ball(View mView) {
33 | super(mView);
34 | float[] dataPos=createBallPos();
35 | ByteBuffer buffer=ByteBuffer.allocateDirect(dataPos.length*4);
36 | buffer.order(ByteOrder.nativeOrder());
37 | vertexBuffer=buffer.asFloatBuffer();
38 | vertexBuffer.put(dataPos);
39 | vertexBuffer.position(0);
40 | vSize=dataPos.length/3;
41 | }
42 |
43 | private float[] createBallPos(){
44 | //球以(0,0,0)为中心,以R为半径,则球上任意一点的坐标为
45 | // ( R * cos(a) * sin(b),y0 = R * sin(a),R * cos(a) * cos(b))
46 | // 其中,a为圆心到点的线段与xz平面的夹角,b为圆心到点的线段在xz平面的投影与z轴的夹角
47 | ArrayList data=new ArrayList<>();
48 | float r1,r2;
49 | float h1,h2;
50 | float sin,cos;
51 | for(float i=-90;i<90+step;i+=step){
52 | r1 = (float)Math.cos(i * Math.PI / 180.0);
53 | r2 = (float)Math.cos((i + step) * Math.PI / 180.0);
54 | h1 = (float)Math.sin(i * Math.PI / 180.0);
55 | h2 = (float)Math.sin((i + step) * Math.PI / 180.0);
56 | // 固定纬度, 360 度旋转遍历一条纬线
57 | float step2=step*2;
58 | for (float j = 0.0f; j <360.0f+step; j +=step2 ) {
59 | cos = (float) Math.cos(j * Math.PI / 180.0);
60 | sin = -(float) Math.sin(j * Math.PI / 180.0);
61 |
62 | data.add(r2 * cos);
63 | data.add(h2);
64 | data.add(r2 * sin);
65 | data.add(r1 * cos);
66 | data.add(h1);
67 | data.add(r1 * sin);
68 | }
69 | }
70 | float[] f=new float[data.size()];
71 | for(int i=0;i mData;
43 |
44 | @Override
45 | protected void onCreate(Bundle savedInstanceState) {
46 | super.onCreate(savedInstanceState);
47 | context=this;
48 | setContentView(R.layout.activity_list);
49 | init();
50 | }
51 |
52 | private void init(){
53 | initData();
54 | mList= (ListView) findViewById(R.id.mList);
55 | mList.setAdapter(new Adapter());
56 | mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
57 | @Override
58 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
59 | Intent intent=new Intent();
60 | intent.putExtra("name",mData.get(position).clazz);
61 | setResult(RESULT_OK,intent);
62 | finish();
63 | }
64 | });
65 | }
66 |
67 | private void initData(){
68 | mData=new ArrayList<>();
69 | add("三角形", Triangle.class);
70 | add("正三角形", TriangleWithCamera.class);
71 | add("彩色三角形", TriangleColorFull.class);
72 | add("正方形", Square.class);
73 | add("圆形", Oval.class);
74 | add("正方体", Cube.class);
75 | add("圆锥", Cone.class);
76 | add("圆柱", Cylinder.class);
77 | add("球体", Ball.class);
78 | add("带光源的球体",BallWithLight.class);
79 | }
80 |
81 | private void add(String showName,Class> clazz){
82 | Data data=new Data();
83 | data.clazz=clazz;
84 | data.showName=showName;
85 | mData.add(data);
86 | }
87 |
88 |
89 | private class Data{
90 | String showName;
91 | Class> clazz;
92 | }
93 |
94 | private class Adapter extends BaseAdapter{
95 |
96 | @Override
97 | public int getCount() {
98 | return mData.size();
99 | }
100 |
101 | @Override
102 | public Object getItem(int position) {
103 | return mData.get(position);
104 | }
105 |
106 | @Override
107 | public long getItemId(int position) {
108 | return position;
109 | }
110 |
111 | @Override
112 | public View getView(int position, View convertView, ViewGroup parent) {
113 | if(convertView==null){
114 | convertView= LayoutInflater.from(context).inflate(R.layout.item_choose,parent,false);
115 | convertView.setTag(new ViewHolder(convertView));
116 | }
117 | ViewHolder holder= (ViewHolder) convertView.getTag();
118 | holder.setData(mData.get(position));
119 | return convertView;
120 | }
121 |
122 | private class ViewHolder{
123 | private TextView mName;
124 | public ViewHolder(View parent){
125 | mName= (TextView) parent.findViewById(R.id.mName);
126 | }
127 | public void setData(Data data){
128 | mName.setText(data.showName);
129 | }
130 | }
131 | }
132 |
133 |
134 | }
135 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/obj/ObjFilter2.java:
--------------------------------------------------------------------------------
1 | package edu.wuwang.opengl.obj;
2 |
3 | import android.content.res.Resources;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.opengl.GLES20;
7 | import android.opengl.GLUtils;
8 | import android.util.Log;
9 |
10 | import java.io.IOException;
11 |
12 | import edu.wuwang.opengl.filter.AFilter;
13 |
14 | /**
15 | * Created by wuwang on 2017/1/8
16 | */
17 |
18 | public class ObjFilter2 extends AFilter {
19 |
20 | private int vertCount;
21 |
22 | private int mHNormal;
23 | private int mHKa;
24 | private int mHKd;
25 | private int mHKs;
26 | private Obj3D obj;
27 |
28 | private int textureId;
29 |
30 | public ObjFilter2(Resources mRes) {
31 | super(mRes);
32 | }
33 |
34 | public void setObj3D(Obj3D obj){
35 | this.obj=obj;
36 | }
37 |
38 | @Override
39 | protected void initBuffer() {
40 | super.initBuffer();
41 | }
42 |
43 | @Override
44 | protected void onCreate() {
45 | createProgramByAssetsFile("3dres/obj2.vert","3dres/obj2.frag");
46 | mHNormal=GLES20.glGetAttribLocation(mProgram,"vNormal");
47 | mHKa=GLES20.glGetUniformLocation(mProgram,"vKa");
48 | mHKd=GLES20.glGetUniformLocation(mProgram,"vKd");
49 | mHKs=GLES20.glGetUniformLocation(mProgram,"vKs");
50 | //打开深度检测
51 | GLES20.glEnable(GLES20.GL_DEPTH_TEST);
52 | if(obj!=null&&obj.mtl!=null){
53 | try {
54 | Log.e("obj","texture-->"+"3dres/"+obj.mtl.map_Kd);
55 | textureId=createTexture(BitmapFactory.decodeStream(mRes.getAssets().open("3dres/"+obj.mtl.map_Kd)));
56 | setTextureId(textureId);
57 | } catch (IOException e) {
58 | e.printStackTrace();
59 | }
60 | }
61 | }
62 |
63 | @Override
64 | protected void onClear() {
65 | // super.onClear();
66 | }
67 |
68 | @Override
69 | protected void onSetExpandData() {
70 | super.onSetExpandData();
71 | if(obj!=null&&obj.mtl!=null){
72 | GLES20.glUniform3fv(mHKa,1,obj.mtl.Ka,0);
73 | GLES20.glUniform3fv(mHKd,1,obj.mtl.Kd,0);
74 | GLES20.glUniform3fv(mHKs,1,obj.mtl.Ks,0);
75 | }
76 | }
77 |
78 | @Override
79 | protected void onDraw() {
80 | GLES20.glEnableVertexAttribArray(mHPosition);
81 | GLES20.glVertexAttribPointer(mHPosition,3, GLES20.GL_FLOAT, false,0,obj.vert);
82 | GLES20.glEnableVertexAttribArray(mHNormal);
83 | GLES20.glVertexAttribPointer(mHNormal,3, GLES20.GL_FLOAT, false, 0,obj.vertNorl);
84 | GLES20.glEnableVertexAttribArray(mHCoord);
85 | GLES20.glVertexAttribPointer(mHCoord,2,GLES20.GL_FLOAT,false,0,obj.vertTexture);
86 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES,0,obj.vertCount);
87 | GLES20.glDisableVertexAttribArray(mHPosition);
88 | GLES20.glDisableVertexAttribArray(mHNormal);
89 | GLES20.glDisableVertexAttribArray(mHCoord);
90 | }
91 |
92 | @Override
93 | protected void onSizeChanged(int width, int height) {
94 | GLES20.glViewport(0,0,width,height);
95 | }
96 |
97 | private int createTexture(Bitmap bitmap){
98 | int[] texture=new int[1];
99 | if(bitmap!=null&&!bitmap.isRecycled()){
100 | //生成纹理
101 | GLES20.glGenTextures(1,texture,0);
102 | //生成纹理
103 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,texture[0]);
104 | //设置缩小过滤为使用纹理中坐标最接近的一个像素的颜色作为需要绘制的像素颜色
105 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
106 | //设置放大过滤为使用纹理中坐标最接近的若干个颜色,通过加权平均算法得到需要绘制的像素颜色
107 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_LINEAR);
108 | //设置环绕方向S,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
109 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,GLES20.GL_CLAMP_TO_EDGE);
110 | //设置环绕方向T,截取纹理坐标到[1/2n,1-1/2n]。将导致永远不会与border融合
111 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,GLES20.GL_CLAMP_TO_EDGE);
112 | //根据以上指定的参数,生成一个2D纹理
113 | GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
114 | return texture[0];
115 | }
116 | return 0;
117 | }
118 |
119 | }
120 |
--------------------------------------------------------------------------------
/app/src/main/assets/fshader/Beauty.sh:
--------------------------------------------------------------------------------
1 | precision mediump float;
2 |
3 | varying mediump vec2 vCoord;
4 |
5 | uniform sampler2D vTexture;
6 | uniform vec2 pStepOffset;
7 | uniform mediump float pParams;
8 |
9 | const highp vec3 W = vec3(0.299,0.587,0.114);
10 | vec2 blurCoordinates[24];
11 |
12 | float hardLight(float color)
13 | {
14 | if(color <= 0.5)
15 | color = color * color * 2.0;
16 | else
17 | color = 1.0 - ((1.0 - color)*(1.0 - color) * 2.0);
18 | return color;
19 | }
20 |
21 | void main(){
22 |
23 | vec3 centralColor = texture2D(vTexture, vCoord).rgb;
24 | blurCoordinates[0] = vCoord.xy + pStepOffset * vec2(0.0, -10.0);
25 | blurCoordinates[1] = vCoord.xy + pStepOffset * vec2(0.0, 10.0);
26 | blurCoordinates[2] = vCoord.xy + pStepOffset * vec2(-10.0, 0.0);
27 | blurCoordinates[3] = vCoord.xy + pStepOffset * vec2(10.0, 0.0);
28 | blurCoordinates[4] = vCoord.xy + pStepOffset * vec2(5.0, -8.0);
29 | blurCoordinates[5] = vCoord.xy + pStepOffset * vec2(5.0, 8.0);
30 | blurCoordinates[6] = vCoord.xy + pStepOffset * vec2(-5.0, 8.0);
31 | blurCoordinates[7] = vCoord.xy + pStepOffset * vec2(-5.0, -8.0);
32 | blurCoordinates[8] = vCoord.xy + pStepOffset * vec2(8.0, -5.0);
33 | blurCoordinates[9] = vCoord.xy + pStepOffset * vec2(8.0, 5.0);
34 | blurCoordinates[10] = vCoord.xy + pStepOffset * vec2(-8.0, 5.0);
35 | blurCoordinates[11] = vCoord.xy + pStepOffset * vec2(-8.0, -5.0);
36 | blurCoordinates[12] = vCoord.xy + pStepOffset * vec2(0.0, -6.0);
37 | blurCoordinates[13] = vCoord.xy + pStepOffset * vec2(0.0, 6.0);
38 | blurCoordinates[14] = vCoord.xy + pStepOffset * vec2(6.0, 0.0);
39 | blurCoordinates[15] = vCoord.xy + pStepOffset * vec2(-6.0, 0.0);
40 | blurCoordinates[16] = vCoord.xy + pStepOffset * vec2(-4.0, -4.0);
41 | blurCoordinates[17] = vCoord.xy + pStepOffset * vec2(-4.0, 4.0);
42 | blurCoordinates[18] = vCoord.xy + pStepOffset * vec2(4.0, -4.0);
43 | blurCoordinates[19] = vCoord.xy + pStepOffset * vec2(4.0, 4.0);
44 | blurCoordinates[20] = vCoord.xy + pStepOffset * vec2(-2.0, -2.0);
45 | blurCoordinates[21] = vCoord.xy + pStepOffset * vec2(-2.0, 2.0);
46 | blurCoordinates[22] = vCoord.xy + pStepOffset * vec2(2.0, -2.0);
47 | blurCoordinates[23] = vCoord.xy + pStepOffset * vec2(2.0, 2.0);
48 |
49 | float sampleColor = centralColor.g * 22.0;
50 | sampleColor += texture2D(vTexture, blurCoordinates[0]).g;
51 | sampleColor += texture2D(vTexture, blurCoordinates[1]).g;
52 | sampleColor += texture2D(vTexture, blurCoordinates[2]).g;
53 | sampleColor += texture2D(vTexture, blurCoordinates[3]).g;
54 | sampleColor += texture2D(vTexture, blurCoordinates[4]).g;
55 | sampleColor += texture2D(vTexture, blurCoordinates[5]).g;
56 | sampleColor += texture2D(vTexture, blurCoordinates[6]).g;
57 | sampleColor += texture2D(vTexture, blurCoordinates[7]).g;
58 | sampleColor += texture2D(vTexture, blurCoordinates[8]).g;
59 | sampleColor += texture2D(vTexture, blurCoordinates[9]).g;
60 | sampleColor += texture2D(vTexture, blurCoordinates[10]).g;
61 | sampleColor += texture2D(vTexture, blurCoordinates[11]).g;
62 | sampleColor += texture2D(vTexture, blurCoordinates[12]).g * 2.0;
63 | sampleColor += texture2D(vTexture, blurCoordinates[13]).g * 2.0;
64 | sampleColor += texture2D(vTexture, blurCoordinates[14]).g * 2.0;
65 | sampleColor += texture2D(vTexture, blurCoordinates[15]).g * 2.0;
66 | sampleColor += texture2D(vTexture, blurCoordinates[16]).g * 2.0;
67 | sampleColor += texture2D(vTexture, blurCoordinates[17]).g * 2.0;
68 | sampleColor += texture2D(vTexture, blurCoordinates[18]).g * 2.0;
69 | sampleColor += texture2D(vTexture, blurCoordinates[19]).g * 2.0;
70 | sampleColor += texture2D(vTexture, blurCoordinates[20]).g * 3.0;
71 | sampleColor += texture2D(vTexture, blurCoordinates[21]).g * 3.0;
72 | sampleColor += texture2D(vTexture, blurCoordinates[22]).g * 3.0;
73 | sampleColor += texture2D(vTexture, blurCoordinates[23]).g * 3.0;
74 |
75 | sampleColor = sampleColor / 62.0;
76 |
77 | float highPass = centralColor.g - sampleColor + 0.5;
78 |
79 | for(int i = 0; i < 5;i++)
80 | {
81 | highPass = hardLight(highPass);
82 | }
83 | float luminance = dot(centralColor, W);
84 |
85 | float alpha = pow(luminance, pParams);
86 |
87 | vec3 smoothColor = centralColor + (centralColor-vec3(highPass))*alpha*0.1;
88 |
89 | gl_FragColor = vec4(mix(smoothColor.rgb, max(smoothColor, centralColor), alpha), 1.0);
90 | }
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/render/TriangleWithCamera.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Triangle.java
4 | *
5 | * Created by Wuwang on 2016/9/30
6 | */
7 | package edu.wuwang.opengl.render;
8 |
9 | import android.opengl.GLES20;
10 | import android.opengl.Matrix;
11 | import android.view.View;
12 |
13 | import java.nio.ByteBuffer;
14 | import java.nio.ByteOrder;
15 | import java.nio.FloatBuffer;
16 |
17 | import javax.microedition.khronos.egl.EGLConfig;
18 | import javax.microedition.khronos.opengles.GL10;
19 |
20 | /**
21 | * Description:
22 | */
23 | public class TriangleWithCamera extends Shape {
24 |
25 | private FloatBuffer vertexBuffer;
26 | private final String vertexShaderCode =
27 | "attribute vec4 vPosition;" +
28 | "uniform mat4 vMatrix;"+
29 | "void main() {" +
30 | " gl_Position = vMatrix*vPosition;" +
31 | "}";
32 |
33 | private final String fragmentShaderCode =
34 | "precision mediump float;" +
35 | "uniform vec4 vColor;" +
36 | "void main() {" +
37 | " gl_FragColor = vColor;" +
38 | "}";
39 |
40 | private int mProgram;
41 |
42 | static final int COORDS_PER_VERTEX = 3;
43 | static float triangleCoords[] = {
44 | 0.5f, 0.5f, 0.0f, // top
45 | -0.5f, -0.5f, 0.0f, // bottom left
46 | 0.5f, -0.5f, 0.0f // bottom right
47 | };
48 |
49 | private int mPositionHandle;
50 | private int mColorHandle;
51 |
52 | private float[] mViewMatrix=new float[16];
53 | private float[] mProjectMatrix=new float[16];
54 | private float[] mMVPMatrix=new float[16];
55 |
56 | //顶点个数
57 | private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
58 | //顶点之间的偏移量
59 | private final int vertexStride = COORDS_PER_VERTEX * 4; // 每个顶点四个字节
60 |
61 | private int mMatrixHandler;
62 |
63 | //设置颜色,依次为红绿蓝和透明通道
64 | float color[] = { 1.0f, 1.0f, 1.0f, 1.0f };
65 |
66 | public TriangleWithCamera(View mView) {
67 | super(mView);
68 | ByteBuffer bb = ByteBuffer.allocateDirect(
69 | triangleCoords.length * 4);
70 | bb.order(ByteOrder.nativeOrder());
71 |
72 | vertexBuffer = bb.asFloatBuffer();
73 | vertexBuffer.put(triangleCoords);
74 | vertexBuffer.position(0);
75 | int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,
76 | vertexShaderCode);
77 | int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
78 | fragmentShaderCode);
79 |
80 | //创建一个空的OpenGLES程序
81 | mProgram = GLES20.glCreateProgram();
82 | //将顶点着色器加入到程序
83 | GLES20.glAttachShader(mProgram, vertexShader);
84 | //将片元着色器加入到程序中
85 | GLES20.glAttachShader(mProgram, fragmentShader);
86 | //连接到着色器程序
87 | GLES20.glLinkProgram(mProgram);
88 | }
89 |
90 | @Override
91 | public void onSurfaceCreated(GL10 gl, EGLConfig config) {
92 |
93 | }
94 |
95 | @Override
96 | public void onSurfaceChanged(GL10 gl, int width, int height) {
97 | //计算宽高比
98 | float ratio=(float)width/height;
99 | //设置透视投影
100 | Matrix.frustumM(mProjectMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
101 | //设置相机位置
102 | Matrix.setLookAtM(mViewMatrix, 0, 0, 0, 7.0f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
103 | //计算变换矩阵
104 | Matrix.multiplyMM(mMVPMatrix,0,mProjectMatrix,0,mViewMatrix,0);
105 | }
106 |
107 | @Override
108 | public void onDrawFrame(GL10 gl) {
109 | //将程序加入到OpenGLES2.0环境
110 | GLES20.glUseProgram(mProgram);
111 | //获取变换矩阵vMatrix成员句柄
112 | mMatrixHandler= GLES20.glGetUniformLocation(mProgram,"vMatrix");
113 | //指定vMatrix的值
114 | GLES20.glUniformMatrix4fv(mMatrixHandler,1,false,mMVPMatrix,0);
115 | //获取顶点着色器的vPosition成员句柄
116 | mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
117 | //启用三角形顶点的句柄
118 | GLES20.glEnableVertexAttribArray(mPositionHandle);
119 | //准备三角形的坐标数据
120 | GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
121 | GLES20.GL_FLOAT, false,
122 | vertexStride, vertexBuffer);
123 | //获取片元着色器的vColor成员的句柄
124 | mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
125 | //设置绘制三角形的颜色
126 | GLES20.glUniform4fv(mColorHandle, 1, color, 0);
127 | //绘制三角形
128 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
129 | //禁止顶点数组的句柄
130 | GLES20.glDisableVertexAttribArray(mPositionHandle);
131 | }
132 |
133 | }
134 |
--------------------------------------------------------------------------------
/app/src/main/java/edu/wuwang/opengl/egl/EGLBackEnvActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * EGLBackEnvActivity.java
4 | *
5 | * Created by Wuwang on 2017/2/18
6 | * Copyright © 2016年 深圳哎吖科技. All rights reserved.
7 | */
8 | package edu.wuwang.opengl.egl;
9 |
10 | import java.io.BufferedOutputStream;
11 | import java.io.File;
12 | import java.io.FileOutputStream;
13 | import java.io.IOException;
14 | import java.nio.ByteBuffer;
15 |
16 | import android.app.Activity;
17 | import android.content.Intent;
18 | import android.database.Cursor;
19 | import android.graphics.Bitmap;
20 | import android.graphics.BitmapFactory;
21 | import android.net.Uri;
22 | import android.opengl.GLES20;
23 | import android.opengl.GLSurfaceView;
24 | import android.opengl.GLUtils;
25 | import android.os.Bundle;
26 | import android.provider.MediaStore;
27 | import android.support.annotation.Nullable;
28 | import android.util.Log;
29 | import android.view.View;
30 | import android.widget.ImageView;
31 | import android.widget.Toast;
32 |
33 | import edu.wuwang.opengl.BaseActivity;
34 | import edu.wuwang.opengl.R;
35 | import edu.wuwang.opengl.fbo.FBOActivity;
36 | import edu.wuwang.opengl.fbo.FBORender;
37 | import edu.wuwang.opengl.filter.AFilter;
38 | import edu.wuwang.opengl.filter.GrayFilter;
39 | import edu.wuwang.opengl.utils.Gl2Utils;
40 |
41 | /**
42 | * Description:
43 | */
44 | public class EGLBackEnvActivity extends BaseActivity {
45 |
46 | private ImageView mImage;
47 |
48 | private int mBmpWidth,mBmpHeight;
49 | private String mImgPath;
50 | private GLES20BackEnv mBackEnv;
51 |
52 | @Override
53 | protected void onCreate(@Nullable Bundle savedInstanceState) {
54 | super.onCreate(savedInstanceState);
55 | setContentView(R.layout.activity_egl_back_env);
56 | mImage= (ImageView)findViewById(R.id.mImage);
57 | }
58 |
59 | public void onClick(View view){
60 | //调用相册
61 | Intent intent = new Intent(Intent.ACTION_PICK,
62 | android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
63 | startActivityForResult(intent, 1);
64 | }
65 |
66 | @Override
67 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
68 | super.onActivityResult(requestCode, resultCode, data);
69 | if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
70 | Uri selectedImage = data.getData();
71 | String[] filePathColumns = {MediaStore.Images.Media.DATA};
72 | Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
73 | c.moveToFirst();
74 | int columnIndex = c.getColumnIndex(filePathColumns[0]);
75 | mImgPath = c.getString(columnIndex);
76 | Log.e("wuwang","img->"+mImgPath);
77 | Bitmap bmp= BitmapFactory.decodeFile(mImgPath);
78 | mBmpWidth=bmp.getWidth();
79 | mBmpHeight=bmp.getHeight();
80 | mBackEnv=new GLES20BackEnv(mBmpWidth,mBmpHeight);
81 | mBackEnv.setThreadOwner(getMainLooper().getThread().getName());
82 | mBackEnv.setFilter(new GrayFilter(getResources()));
83 | mBackEnv.setInput(bmp);
84 | saveBitmap(mBackEnv.getBitmap());
85 | c.close();
86 | }
87 | }
88 |
89 |
90 | //图片保存
91 | public void saveBitmap(final Bitmap b){
92 | String path = mImgPath.substring(0,mImgPath.lastIndexOf("/")+1);
93 | File folder=new File(path);
94 | if(!folder.exists()&&!folder.mkdirs()){
95 | runOnUiThread(new Runnable() {
96 | @Override
97 | public void run() {
98 | Toast.makeText(EGLBackEnvActivity.this, "无法保存照片", Toast.LENGTH_SHORT).show();
99 | }
100 | });
101 | return;
102 | }
103 | long dataTake = System.currentTimeMillis();
104 | final String jpegName=path+ dataTake +".jpg";
105 | try {
106 | FileOutputStream fout = new FileOutputStream(jpegName);
107 | BufferedOutputStream bos = new BufferedOutputStream(fout);
108 | b.compress(Bitmap.CompressFormat.JPEG, 100, bos);
109 | bos.flush();
110 | bos.close();
111 | } catch (IOException e) {
112 | // TODO Auto-generated catch block
113 | e.printStackTrace();
114 | }
115 | runOnUiThread(new Runnable() {
116 | @Override
117 | public void run() {
118 | Toast.makeText(EGLBackEnvActivity.this, "保存成功->"+jpegName, Toast.LENGTH_SHORT).show();
119 | mImage.setImageBitmap(b);
120 | }
121 | });
122 |
123 | }
124 |
125 | }
126 |
--------------------------------------------------------------------------------