├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── app-release.apk ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── example │ │ ├── BasicRender.java │ │ ├── ViewThouchListener.java │ │ ├── object │ │ ├── Geometry.java │ │ ├── Skybox.java │ │ ├── Table.java │ │ ├── ThirdMesh.java │ │ ├── Triangle.java │ │ └── Vertices3.java │ │ ├── opengldemo │ │ ├── AntiAliasing │ │ │ ├── AntiAliasingDemoActivity.java │ │ │ └── AntiAliasinglRender.java │ │ ├── GaussianBlur │ │ │ ├── GaosiUtil.java │ │ │ ├── GaussianBlurDemoActivity.java │ │ │ ├── GaussianBlurRender.java │ │ │ └── GussianBlurShaderProgram.java │ │ ├── MainActivity.java │ │ ├── bumpMapping │ │ │ ├── BumpMappingDemoActivity.java │ │ │ └── BumpMappingtRender.java │ │ ├── directLight │ │ │ ├── DirectLightDemoActivity.java │ │ │ ├── DirectLightRender.java │ │ │ └── DirectLightShaderProgram.java │ │ ├── egl │ │ │ ├── EGLHelper.java │ │ │ ├── EGLPBHelper.java │ │ │ ├── EglTestActivity.java │ │ │ ├── EventListener.java │ │ │ ├── OpenglView.java │ │ │ └── PbRender.java │ │ ├── embossed │ │ │ ├── EmbossedDemoActivity.java │ │ │ └── EmbossedRender.java │ │ ├── fbo │ │ │ ├── FboDemoActivity.java │ │ │ └── FboRender.java │ │ ├── fishEye │ │ │ ├── FishEyeDemoActivity.java │ │ │ └── FishEyeRender.java │ │ ├── normalmapping │ │ │ ├── MyGLSurfaceView.java │ │ │ ├── MyRenderer.java │ │ │ ├── NormalMappingActivity.java │ │ │ ├── Square.java │ │ │ └── Triangle.java │ │ ├── pointLight │ │ │ ├── PointLightDemoActivity.java │ │ │ ├── PointLightRender.java │ │ │ └── PointLightShaderProgram.java │ │ ├── processTexture │ │ │ ├── ProcessTextureDemoActivity.java │ │ │ ├── ProcessTextureRender.java │ │ │ └── ProcessTextureShaderProgram.java │ │ ├── rayPickup │ │ │ ├── RayPickupDemoActivity.java │ │ │ └── RayPickupLightRender.java │ │ ├── reflectLight │ │ │ ├── CommonShaderProgram.java │ │ │ ├── ReflectLightDemoActivity.java │ │ │ ├── ReflectLightRender.java │ │ │ └── SkyboxShaderProgam.java │ │ ├── ripple │ │ │ ├── RippleDemoActivity.java │ │ │ ├── RippleRender.java │ │ │ └── RippleShaderProgram.java │ │ ├── rotate │ │ │ ├── RotateDemoActivity.java │ │ │ └── RotateRender.java │ │ ├── shadowMapping │ │ │ ├── Cube.java │ │ │ ├── Plane.java │ │ │ ├── ShadowMappingtDemoActivity.java │ │ │ ├── ShadowsGLSurfaceView.java │ │ │ ├── ShadowsRenderer.java │ │ │ └── common │ │ │ │ ├── FPSCounter.java │ │ │ │ ├── RenderConstants.java │ │ │ │ └── RenderProgram.java │ │ ├── skybox │ │ │ ├── SkyBoxDemoActivity.java │ │ │ └── SkyBoxRender.java │ │ ├── sobelOperator │ │ │ ├── SobelDemoActivity.java │ │ │ └── SobelRender.java │ │ ├── twirl │ │ │ ├── TwirlDemoActivity.java │ │ │ ├── TwirlRender.java │ │ │ └── TwirlShaderProgram.java │ │ └── wobble │ │ │ ├── WobbleDemoActivity.java │ │ │ ├── WobbleRender.java │ │ │ └── WobbleShaderProgram.java │ │ └── shaderUtil │ │ ├── BasicShaderProgram.java │ │ ├── Constants.java │ │ ├── IndexBuffer.java │ │ ├── MatrixHelper.java │ │ ├── ObjLoader.java │ │ ├── RayPickupUtil.java │ │ ├── ShaderHelper.java │ │ ├── ShaderProgram.java │ │ ├── SkyboxShaderProgram.java │ │ ├── TextResourceReader.java │ │ ├── TextureHelper.java │ │ ├── TextureShaderProgram.java │ │ ├── VertexArray.java │ │ └── VertexBuffer.java │ └── res │ ├── drawable-xxhdpi │ ├── air_hockey_surface.png │ ├── back.png │ ├── bottom.png │ ├── cat.jpg │ ├── crate.png │ ├── face.png │ ├── front.png │ ├── ic_launcher.png │ ├── left.png │ ├── right.png │ └── top.png │ ├── drawable │ ├── bricks.JPG │ ├── bricks_norm.JPG │ ├── ic_launcher_background.xml │ ├── metal.JPG │ ├── metal_norm.JPG │ ├── normal.jpg │ ├── normal2.jpg │ ├── normal3.png │ ├── normal4.png │ ├── normal_empty.JPG │ ├── rocks.JPG │ ├── rocks_norm.JPG │ ├── stone.png │ ├── stone_normal.png │ ├── texture.jpg │ └── white.JPG │ ├── layout │ ├── activity_main.xml │ ├── activity_main2.xml │ └── egl_test_layout.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── raw │ ├── anti_aliasing_fragment.glsl │ ├── ball.obj │ ├── box.obj │ ├── bumpmapping_fragment.glsl │ ├── bumpmapping_vertex.glsl │ ├── cube.obj │ ├── depth_tex_f_depth_map.glsl │ ├── depth_tex_f_with_simple_shadow.glsl │ ├── depth_tex_v_depth_map.glsl │ ├── depth_tex_v_with_shadow.glsl │ ├── direct_light_fragment.glsl │ ├── direct_light_vertex.glsl │ ├── embossed_fragment.glsl │ ├── fisheye_fragment.glsl │ ├── horitzontal_gussian_blure_fargment.glsl │ ├── monkey.obj │ ├── pixel_shader_texture_normals_distance.glsl │ ├── plane.obj │ ├── point_light_fragment.glsl │ ├── point_light_vertex.glsl │ ├── process_texture_fragment.glsl │ ├── reflect_light_fragment.glsl │ ├── reflect_light_vertext.glsl │ ├── ripple_fragment_shader.glsl │ ├── ripple_vertex_shader.glsl │ ├── rotate_fragment_shader.glsl │ ├── rotate_vertex_shader.glsl │ ├── simple_fragment_shader.glsl │ ├── simple_vertex_shader.glsl │ ├── skybox_fragment_shader.glsl │ ├── skybox_vertex_shader.glsl │ ├── sobel_fragment_shader.glsl │ ├── sphere.obj │ ├── test_fragment_shader.glsl │ ├── twirl_fragment.glsl │ ├── verital_gussian_blur_fragment.glsl │ ├── vertex_shader.glsl │ ├── water.obj │ ├── water_test.obj │ └── wobble_vertex_shader.glsl │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.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 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 34 | 36 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #### AndroidOpengl 2 | #### 简单的android opengl实现案例,涉及矩阵复合操作,物体变形,纹理动画,光照,射线拾取,天空盒,fbo等技术。在com.example.opengldemo包中又分多个子包,各个功能如下: 3 | #### rotate包中,实现三个物体相互围绕运动。 4 | #### wobble包中,实现一个物体顶点变化的动画。 5 | #### ripple包中,实现一个带水纹效果的相机。 6 | #### processTexture包中,实现一个简单的过程纹理。 7 | #### directLight包中,实现一个方向光的效果。 8 | #### pointLight包中,实现一个点光的漫反射效果。 9 | #### reflectLight包中,实现一个点光的镜面反射的效果。 10 | #### bumpMapping包中,实现一个凹凸纹理(不是normal mapping实现,是通过一个简单的算法扰乱法线)。 11 | #### rayPickup包中,实现射线拾取的功能。 12 | #### skybox包中,实现天空盒全景的效果,天空盒中的物体有环境反射和折射的效果。 13 | #### fbo包中,实现一个简单的fbo的效果。 14 | #### sobelOperator包中,实现一个简单的索贝尔边缘检测效果 15 | #### GaussianBlur包中,实现一个高斯模糊的效果(还有很大的改进空间) 16 | #### embossed包中,实现一个简单的浮雕效果 17 | #### fishEye包中,实现一个简单的鱼眼效果 18 | #### twril包中,实现一个漩涡扭曲的效果 19 | #### shadowMapping包中,实现一个光照下阴影的效果 20 | #### normalmapping包中,实现一个normalmapping的效果 21 | #### AntiAliasing,实现一个FXAA的反走样效果 22 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app-release.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/app-release.apk -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "decryptzip.flame.ynet.com.opengldemo" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | 15 | aaptOptions { 16 | cruncherEnabled = false 17 | useNewCruncher = false 18 | 19 | } 20 | 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | } 28 | 29 | dependencies { 30 | compile fileTree(dir: 'libs', include: ['*.jar']) 31 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 32 | exclude group: 'com.android.support', module: 'support-annotations' 33 | }) 34 | compile 'com.android.support:appcompat-v7:25.2.0' 35 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 36 | testCompile 'junit:junit:4.12' 37 | } 38 | -------------------------------------------------------------------------------- /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:\work_space\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 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 24 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/BasicRender.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import android.opengl.GLSurfaceView; 4 | 5 | import javax.microedition.khronos.egl.EGLConfig; 6 | import javax.microedition.khronos.opengles.GL10; 7 | 8 | /** 9 | * Created by L on 2017/8/9. 10 | */ 11 | 12 | public abstract class BasicRender implements GLSurfaceView.Renderer { 13 | @Override 14 | public abstract void onSurfaceCreated(GL10 gl, EGLConfig config); 15 | 16 | @Override 17 | public abstract void onSurfaceChanged(GL10 gl, int width, int height); 18 | 19 | @Override 20 | public abstract void onDrawFrame(GL10 gl); 21 | 22 | public abstract void rotate(float xAngle, float yAngle); 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/ViewThouchListener.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import android.opengl.GLSurfaceView; 4 | import android.view.MotionEvent; 5 | import android.view.View; 6 | 7 | /** 8 | * Created by L on 2017/8/9. 9 | */ 10 | 11 | public class ViewThouchListener implements View.OnTouchListener { 12 | private GLSurfaceView view; 13 | private BasicRender render; 14 | 15 | private float lastX = 0; 16 | private float lastY = 0; 17 | 18 | public ViewThouchListener(GLSurfaceView view, BasicRender render){ 19 | this.view = view; 20 | this.render = render; 21 | } 22 | 23 | @Override 24 | public boolean onTouch(View v, final MotionEvent event) { 25 | if (event.getAction() == MotionEvent.ACTION_DOWN) { 26 | lastX = 0; 27 | lastY = 0; 28 | } 29 | 30 | view.queueEvent(new Runnable() { 31 | 32 | @Override 33 | public void run() { 34 | float currentX = event.getX(); 35 | float currentY = event.getY(); 36 | 37 | float yAngle = 0; 38 | float xAngle = 0; 39 | yAngle = (currentX - lastX) * 0.5f; 40 | xAngle = (currentY - lastY) * 0.5f; 41 | if (lastY != 0 && lastX != 0) { 42 | render.rotate(xAngle, yAngle); 43 | } 44 | lastX = currentX; 45 | lastY = currentY; 46 | } 47 | }); 48 | 49 | return true; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/object/Skybox.java: -------------------------------------------------------------------------------- 1 | package com.example.object; 2 | 3 | import android.opengl.GLES20; 4 | 5 | import com.example.shaderUtil.SkyboxShaderProgram; 6 | import com.example.shaderUtil.VertexArray; 7 | 8 | import java.nio.ByteBuffer; 9 | 10 | /** 11 | * Created by L on 2017/8/2. 12 | */ 13 | 14 | public class Skybox { 15 | private static final int POSITION_COMPONENT_COUNT = 3; 16 | private VertexArray vertexArray; 17 | private ByteBuffer indexArray; 18 | 19 | public Skybox() { 20 | vertexArray = new VertexArray(new float[]{ 21 | -1, 1, 1, 22 | 1, 1, 1, 23 | -1, -1, 1, 24 | 1, -1, 1, 25 | -1, 1, -1, 26 | 1, 1, -1, 27 | -1, -1, -1, 28 | 1, -1, -1 29 | }); 30 | 31 | indexArray = ByteBuffer.allocateDirect(6 * 6).put(new byte[]{ 32 | // Front 33 | 1, 3, 0, 34 | 0, 3, 2, 35 | 36 | // Back 37 | 4, 6, 5, 38 | 5, 6, 7, 39 | 40 | // Left 41 | 0, 2, 4, 42 | 4, 2, 6, 43 | 44 | // Right 45 | 5, 7, 1, 46 | 1, 7, 3, 47 | 48 | // Top 49 | 5, 1, 4, 50 | 4, 1, 0, 51 | 52 | // Bottom 53 | 6, 2, 7, 54 | 7, 2, 3 55 | }); 56 | indexArray.position(0); 57 | } 58 | 59 | public void bindData(SkyboxShaderProgram skyboxProgram) { 60 | vertexArray.setVertexAttribPointer(0, skyboxProgram.getPositionAttributeLocation(), POSITION_COMPONENT_COUNT, 0); 61 | } 62 | 63 | public void draw(){ 64 | GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_BYTE, indexArray); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/object/Table.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "OpenGL ES for Android", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information. 8 | ***/ 9 | package com.example.object; 10 | import static android.opengl.GLES20.glDrawArrays; 11 | 12 | import android.opengl.GLES20; 13 | 14 | import com.example.shaderUtil.Constants; 15 | import com.example.shaderUtil.TextureShaderProgram; 16 | import com.example.shaderUtil.VertexArray; 17 | 18 | public class Table { 19 | private static final int POSITION_COMPONENT_COUNT = 2; 20 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 21 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 22 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * Constants.BYTES_PER_FLOAT; 23 | 24 | private static final float[] VERTEX_DATA = { 25 | // Order of coordinates: X, Y, S, T 26 | 27 | // Triangle Fan 28 | 0f, 0f, 0.5f, 0.5f, 29 | -0.5f, -0.8f, 0f, 0.9f, 30 | 0.5f, -0.8f, 1f, 0.9f, 31 | 2.1f, 2.4f, 1f, 0.1f, 32 | 1.8f, 2.7f, 0f, 0.1f, 33 | 1.1f, -1.2f, 0f, 0.9f }; 34 | 35 | private final VertexArray vertexArray; 36 | 37 | public Table() { 38 | vertexArray = new VertexArray(VERTEX_DATA); 39 | } 40 | 41 | public Table(float[] vertexData){ 42 | vertexArray = new VertexArray(vertexData); 43 | } 44 | 45 | public void bindData(TextureShaderProgram textureProgram) { 46 | vertexArray.setVertexAttribPointer( 47 | 0, 48 | textureProgram.getPositionAttributeLocation(), 49 | POSITION_COMPONENT_COUNT, 50 | STRIDE); 51 | 52 | vertexArray.setVertexAttribPointer( 53 | POSITION_COMPONENT_COUNT, 54 | textureProgram.getTextureCoordinatesAttributeLocation(), 55 | TEXTURE_COORDINATES_COMPONENT_COUNT, 56 | STRIDE); 57 | } 58 | 59 | public void draw() { 60 | glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); 61 | // glDrawArrays(GL_TRIANGLE_FAN, 0, 6); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/object/ThirdMesh.java: -------------------------------------------------------------------------------- 1 | package com.example.object; 2 | 3 | import static android.opengl.GLES20.glDrawArrays; 4 | 5 | import android.opengl.GLES20; 6 | 7 | import com.example.shaderUtil.Constants; 8 | import com.example.shaderUtil.TextureShaderProgram; 9 | import com.example.shaderUtil.VertexArray; 10 | 11 | public class ThirdMesh { 12 | 13 | private static final int POSITION_COMPONENT_COUNT = 2; 14 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 15 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 16 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * Constants.BYTES_PER_FLOAT; 17 | 18 | private static final float[] VERTEX_DATA = { 19 | // Order of coordinates: X, Y, S, T 20 | 21 | // Triangle Fan 22 | // 0f, 0f, 0.5f, 0.5f, 23 | // -0.5f, -0.8f, 0f, 0.9f, 24 | // 0.5f, -0.8f, 1f, 0.9f, 25 | -0.2f, -0.5f, 0.9f, 0f, 26 | -0.9f, -0.8f, -0.1f, 0f, 27 | -0.2f, -0.3f, -0.1f, 0.8f }; 28 | 29 | private final VertexArray vertexArray; 30 | 31 | public ThirdMesh() { 32 | vertexArray = new VertexArray(VERTEX_DATA); 33 | } 34 | 35 | public void bindData(TextureShaderProgram textureProgram) { 36 | vertexArray.setVertexAttribPointer( 37 | 0, 38 | textureProgram.getPositionAttributeLocation(), 39 | POSITION_COMPONENT_COUNT, 40 | STRIDE); 41 | 42 | vertexArray.setVertexAttribPointer( 43 | POSITION_COMPONENT_COUNT, 44 | textureProgram.getTextureCoordinatesAttributeLocation(), 45 | TEXTURE_COORDINATES_COMPONENT_COUNT, 46 | STRIDE); 47 | } 48 | 49 | public void draw() { 50 | glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); 51 | // glDrawArrays(GL_TRIANGLE_FAN, 0, 6); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/object/Triangle.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "OpenGL ES for Android", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information. 8 | ***/ 9 | package com.example.object; 10 | import static android.opengl.GLES20.GL_TRIANGLE_FAN; 11 | import static android.opengl.GLES20.glDrawArrays; 12 | 13 | import android.opengl.GLES20; 14 | 15 | import com.example.shaderUtil.BasicShaderProgram; 16 | import com.example.shaderUtil.Constants; 17 | import com.example.shaderUtil.TextureShaderProgram; 18 | import com.example.shaderUtil.VertexArray; 19 | 20 | public class Triangle { 21 | private static final int POSITION_COMPONENT_COUNT = 2; 22 | private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2; 23 | private static final int STRIDE = (POSITION_COMPONENT_COUNT 24 | + TEXTURE_COORDINATES_COMPONENT_COUNT) * Constants.BYTES_PER_FLOAT; 25 | 26 | private float[] VERTEX_DATA = { 27 | // Order of coordinates: X, Y, S, T 28 | 29 | // Triangle Fan 30 | // 0f, 0f, 0.5f, 0.5f, 31 | // -0.5f, -0.8f, 0f, 0.9f, 32 | // 0.5f, -0.8f, 1f, 0.9f, 33 | -0.1f, -0.4f, 1f, 0.1f, 34 | -0.8f, -0.7f, 0f, 0.1f, 35 | -0.1f, -0.2f, 0f, 0.9f }; 36 | 37 | private final VertexArray vertexArray; 38 | 39 | // public Triangle() { 40 | // vertexArray = new VertexArray(VERTEX_DATA); 41 | // } 42 | 43 | public Triangle(float[] triangleData){ 44 | this.VERTEX_DATA = triangleData; 45 | vertexArray = new VertexArray(triangleData); 46 | } 47 | 48 | public void bindData(BasicShaderProgram textureProgram) { 49 | vertexArray.setVertexAttribPointer( 50 | 0, 51 | textureProgram.getPositionLocation(), 52 | POSITION_COMPONENT_COUNT, 53 | STRIDE); 54 | 55 | vertexArray.setVertexAttribPointer( 56 | POSITION_COMPONENT_COUNT, 57 | textureProgram.getTextureCoordinatesLocation(), 58 | TEXTURE_COORDINATES_COMPONENT_COUNT, 59 | STRIDE); 60 | } 61 | 62 | public void draw() { 63 | // glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); 64 | glDrawArrays(GL_TRIANGLE_FAN, 0, VERTEX_DATA.length / 4); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/AntiAliasing/AntiAliasingDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.AntiAliasing; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | 7 | import com.example.ViewThouchListener; 8 | 9 | public class AntiAliasingDemoActivity extends Activity { 10 | private GLSurfaceView view; 11 | private AntiAliasinglRender render; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | 17 | initView(); 18 | setContentView(view); 19 | } 20 | 21 | private void initView() { 22 | view = new GLSurfaceView(this); 23 | 24 | view.setEGLContextClientVersion(2); 25 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 26 | render = new AntiAliasinglRender(this); 27 | view.setRenderer(render); 28 | 29 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 30 | view.setOnTouchListener(touchListener); 31 | } 32 | 33 | @Override 34 | protected void onPause() { 35 | super.onPause(); 36 | view.onPause(); 37 | } 38 | 39 | @Override 40 | protected void onResume() { 41 | super.onResume(); 42 | view.onResume(); 43 | } 44 | 45 | @Override 46 | protected void onDestroy() { 47 | super.onDestroy(); 48 | render.destroy(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/GaussianBlur/GaosiUtil.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.GaussianBlur; 2 | 3 | //import android.util.Log; 4 | 5 | public class GaosiUtil { 6 | //二维高斯算法具体实现 7 | static float sum=0; 8 | public static float[][] get2DKernalData(int n, float sigma) { 9 | int size = 2*n +1; 10 | float sigma22 = 2*sigma*sigma; 11 | float sigma22PI = (float)Math.PI * sigma22; 12 | float[][] kernalData = new float[size][size]; 13 | 14 | 15 | int row = 0; 16 | for(int i=-n; i<=n; i++) { 17 | int column = 0; 18 | for(int j=-n; j<=n; j++) { 19 | float xDistance = i*i; 20 | float yDistance = j*j; 21 | kernalData[row][column] = (float)Math.exp(-(xDistance + yDistance)/sigma22)/sigma22PI; 22 | column++; 23 | } 24 | row++; 25 | } 26 | System.out.println("二维高斯结果"); 27 | for(int i=0; i>>down move event: "+index++); 104 | } 105 | 106 | } 107 | 108 | private class TouchListener implements EventListener { 109 | 110 | @Override 111 | public void event() { 112 | Log.i("", "------>>>touch move event"+index++); 113 | } 114 | 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/egl/PbRender.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.egl; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.graphics.Bitmap; 6 | import android.opengl.EGLContext; 7 | import android.opengl.GLES20; 8 | import android.os.Handler; 9 | import android.os.Looper; 10 | import android.os.Message; 11 | import android.util.Log; 12 | import android.view.SurfaceHolder; 13 | import android.widget.ImageView; 14 | 15 | import java.nio.IntBuffer; 16 | 17 | import javax.microedition.khronos.opengles.GL10; 18 | 19 | public class PbRender implements Runnable { 20 | private SurfaceHolder surfaceHolder; 21 | private Context context; 22 | private EGLContext shareContext; 23 | 24 | private EGLPBHelper eglpbHelper; 25 | private boolean isRun; 26 | private ImageView imageView; 27 | 28 | private Handler handler = new Handler(Looper.getMainLooper()){ 29 | @Override 30 | public void handleMessage(Message msg) { 31 | super.handleMessage(msg); 32 | Bitmap bitmap = (Bitmap) msg.obj; 33 | imageView.setImageBitmap(bitmap); 34 | } 35 | }; 36 | 37 | public PbRender(SurfaceHolder surfaceHolder, Context context, 38 | EGLContext shareContext, ImageView imageView){ 39 | this.surfaceHolder = surfaceHolder; 40 | this.context = context; 41 | this.shareContext = shareContext; 42 | this.imageView = imageView; 43 | } 44 | 45 | public void startRender(){ 46 | isRun = true; 47 | 48 | Thread pbThread = new Thread(this); 49 | pbThread.start(); 50 | } 51 | 52 | public void stopRender(){ 53 | isRun = false; 54 | } 55 | 56 | 57 | @Override 58 | public void run() { 59 | Activity activity = (Activity)context; 60 | eglpbHelper = new EGLPBHelper(surfaceHolder, activity); 61 | 62 | eglpbHelper.initEgl(shareContext); 63 | eglpbHelper.onCreate(); 64 | 65 | while (isRun) { 66 | eglpbHelper.onFrame(); 67 | eglpbHelper.swap(); 68 | 69 | IntBuffer ib = IntBuffer.allocate(480 * 800); 70 | GLES20.glReadPixels(0, 0, 480, 800, GL10.GL_RGBA, 71 | GL10.GL_UNSIGNED_BYTE, ib); 72 | Bitmap bitmap = frameToBitmap(480, 800, ib); 73 | 74 | Message msg = new Message(); 75 | msg.obj = bitmap; 76 | handler.sendMessage(msg); 77 | Log.i("", ""); 78 | } 79 | eglpbHelper.destroyEgl(); 80 | } 81 | 82 | private static Bitmap frameToBitmap(int width, int height, IntBuffer ib) { 83 | int pixs[] = ib.array(); 84 | for (int y = 0; y < height / 2; y++) { 85 | for (int x = 0; x < width; x++) { 86 | int pos1 = y * width + x; 87 | int pos2 = (height - 1 - y) * width + x; 88 | 89 | int tmp = pixs[pos1]; 90 | pixs[pos1] = (pixs[pos2] & 0xFF00FF00) | ((pixs[pos2] >> 16) & 0xff) | ((pixs[pos2] << 16) & 0x00ff0000); // ABGR->ARGB 91 | pixs[pos2] = (tmp & 0xFF00FF00) | ((tmp >> 16) & 0xff) | ((tmp << 16) & 0x00ff0000); 92 | } 93 | } 94 | if (height % 2 == 1) { 95 | for (int x = 0; x < width; x++) { 96 | int pos = (height / 2 + 1) * width + x; 97 | pixs[pos] = (pixs[pos] & 0xFF00FF00) | ((pixs[pos] >> 16) & 0xff) | ((pixs[pos] << 16) & 0x00ff0000); 98 | } 99 | } 100 | 101 | return Bitmap.createBitmap(pixs, width, height, Bitmap.Config.ARGB_8888); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/embossed/EmbossedDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.embossed; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | 7 | import com.example.ViewThouchListener; 8 | 9 | public class EmbossedDemoActivity extends Activity { 10 | private GLSurfaceView view; 11 | private EmbossedRender render; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | 17 | initView(); 18 | setContentView(view); 19 | } 20 | 21 | private void initView() { 22 | view = new GLSurfaceView(this); 23 | 24 | view.setEGLContextClientVersion(2); 25 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 26 | render = new EmbossedRender(this); 27 | view.setRenderer(render); 28 | 29 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 30 | view.setOnTouchListener(touchListener); 31 | } 32 | 33 | @Override 34 | protected void onPause() { 35 | super.onPause(); 36 | view.onPause(); 37 | } 38 | 39 | @Override 40 | protected void onResume() { 41 | super.onResume(); 42 | view.onResume(); 43 | } 44 | 45 | @Override 46 | protected void onDestroy() { 47 | super.onDestroy(); 48 | render.release(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/fbo/FboDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.fbo; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.view.View.OnTouchListener; 9 | 10 | import com.example.ViewThouchListener; 11 | 12 | public class FboDemoActivity extends Activity { 13 | private GLSurfaceView view; 14 | private FboRender render; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | 20 | initView(); 21 | setContentView(view); 22 | } 23 | 24 | private void initView() { 25 | view = new GLSurfaceView(this); 26 | 27 | view.setEGLContextClientVersion(2); 28 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 29 | render = new FboRender(this); 30 | view.setRenderer(render); 31 | 32 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 33 | view.setOnTouchListener(touchListener); 34 | } 35 | 36 | @Override 37 | protected void onPause() { 38 | super.onPause(); 39 | view.onPause(); 40 | } 41 | 42 | @Override 43 | protected void onResume() { 44 | super.onResume(); 45 | view.onResume(); 46 | } 47 | 48 | @Override 49 | protected void onDestroy(){ 50 | super.onDestroy(); 51 | render.release(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/fishEye/FishEyeDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.fishEye; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | 7 | import com.example.ViewThouchListener; 8 | 9 | public class FishEyeDemoActivity extends Activity { 10 | private GLSurfaceView view; 11 | private FishEyeRender render; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | 17 | initView(); 18 | setContentView(view); 19 | } 20 | 21 | private void initView() { 22 | view = new GLSurfaceView(this); 23 | 24 | view.setEGLContextClientVersion(2); 25 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 26 | render = new FishEyeRender(this); 27 | view.setRenderer(render); 28 | 29 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 30 | view.setOnTouchListener(touchListener); 31 | } 32 | 33 | @Override 34 | protected void onPause() { 35 | super.onPause(); 36 | view.onPause(); 37 | } 38 | 39 | @Override 40 | protected void onResume() { 41 | super.onResume(); 42 | view.onResume(); 43 | } 44 | 45 | @Override 46 | protected void onDestroy() { 47 | super.onDestroy(); 48 | render.release(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/normalmapping/MyGLSurfaceView.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.normalmapping; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLSurfaceView; 5 | 6 | /** 7 | * Created by egslava on 18/03/2017. 8 | */ 9 | 10 | public class MyGLSurfaceView extends GLSurfaceView { 11 | 12 | public final MyRenderer mRenderer; 13 | 14 | public MyGLSurfaceView(Context context) { 15 | super(context); 16 | setEGLContextClientVersion(2); 17 | 18 | mRenderer = new MyRenderer(context); 19 | setRenderer(mRenderer); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/normalmapping/MyRenderer.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.normalmapping; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.BitmapFactory; 6 | import android.opengl.GLES20; 7 | import android.opengl.GLSurfaceView; 8 | import android.opengl.GLUtils; 9 | import android.util.Log; 10 | import android.widget.Toast; 11 | 12 | import com.example.opengldemo.R; 13 | 14 | import java.io.BufferedReader; 15 | import java.io.IOException; 16 | import java.io.InputStream; 17 | import java.io.InputStreamReader; 18 | 19 | import javax.microedition.khronos.egl.EGLConfig; 20 | import javax.microedition.khronos.opengles.GL10; 21 | 22 | /** 23 | * Created by egslava on 18/03/2017. 24 | */ 25 | 26 | public class MyRenderer implements GLSurfaceView.Renderer { 27 | 28 | public static final String TAG = "MyRenderer"; 29 | private final String mRawTextResourceVertexShader; 30 | private final String mRawTextResourcePixelShader; 31 | private Context mContext; 32 | private int mTexture; 33 | public Triangle triangle; 34 | private Square square; 35 | private int mNormal; 36 | 37 | public MyRenderer(Context context) { 38 | mRawTextResourceVertexShader = getRawTextResource(context, 39 | R.raw.vertex_shader); 40 | mRawTextResourcePixelShader = getRawTextResource(context, 41 | R.raw.pixel_shader_texture_normals_distance); 42 | 43 | 44 | mContext = context; 45 | } 46 | 47 | private String getRawTextResource(Context context, int resource) { 48 | final InputStream inputStream = context.getResources().openRawResource(resource); 49 | final BufferedReader bufferedReaderVertexShader = new BufferedReader(new InputStreamReader(inputStream)); 50 | 51 | StringBuilder total = new StringBuilder(); 52 | try { 53 | String readLine; 54 | do { 55 | readLine = bufferedReaderVertexShader.readLine(); 56 | if (readLine != null){ 57 | total.append( readLine ); 58 | total.append( "\n"); 59 | } 60 | }while (readLine != null); 61 | 62 | Log.d(TAG, "getRawTextResource: " + total.toString()); 63 | return total.toString(); 64 | 65 | } catch (IOException e) { 66 | e.printStackTrace(); 67 | Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();; 68 | return null; 69 | } 70 | } 71 | 72 | @Override 73 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 74 | GLES20.glClearColor(0, 0, 0, 0); 75 | Log.d(TAG, "onSurfaceCreated: "); 76 | 77 | mTexture = loadTexture(mContext, R.drawable.bricks); 78 | mNormal = loadTexture(mContext, R.drawable.bricks_norm); 79 | triangle = new Triangle(mRawTextResourceVertexShader, mRawTextResourcePixelShader, mTexture, mNormal); 80 | square = new Square(); 81 | } 82 | 83 | @Override 84 | public void onSurfaceChanged(GL10 gl, int width, int height) { 85 | GLES20.glViewport(0, 0, width, height); 86 | Log.d(TAG, "onSurfaceChanged: "); 87 | } 88 | 89 | float time; 90 | float d = 0.005f; 91 | final float D = 0.005f; 92 | 93 | @Override 94 | public void onDrawFrame(GL10 gl) { 95 | if (time >= 1){ 96 | d = -D; 97 | } 98 | if (time <= 0){ 99 | d = D; 100 | } 101 | 102 | time += d; 103 | 104 | GLES20.glClearColor(time, time, time, 1.0f); 105 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 106 | 107 | triangle.draw(); 108 | } 109 | 110 | public static int loadShader(int type, String shaderCode){ 111 | 112 | int shader = GLES20.glCreateShader(type); 113 | GLES20.glShaderSource(shader, shaderCode); 114 | GLES20.glCompileShader(shader); 115 | return shader; 116 | 117 | } 118 | 119 | int loadTexture(Context context, int res){ 120 | int textures[] = {-1}; 121 | GLES20.glGenTextures(1, textures, 0); 122 | int texture = textures[0]; 123 | 124 | Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), res); 125 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture); 126 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); 127 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); 128 | GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0 ); 129 | 130 | bitmap.recycle(); 131 | 132 | return texture; 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/normalmapping/NormalMappingActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.normalmapping; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.util.Log; 6 | import android.view.Menu; 7 | import android.view.MotionEvent; 8 | import android.view.View; 9 | 10 | public class NormalMappingActivity extends Activity { 11 | 12 | private static final String TAG = NormalMappingActivity.class.getSimpleName(); 13 | private MyGLSurfaceView myGLSurfaceView; 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | myGLSurfaceView = new MyGLSurfaceView(this); 19 | setContentView(myGLSurfaceView); 20 | } 21 | 22 | @Override 23 | public boolean onCreateOptionsMenu(Menu menu) { 24 | return super.onCreateOptionsMenu(menu); 25 | } 26 | 27 | @Override 28 | protected void onResume() { 29 | super.onResume(); 30 | 31 | goFullScreen(); 32 | } 33 | 34 | private void goFullScreen() { 35 | View decorView = getWindow().getDecorView(); 36 | // decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE 37 | // | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 38 | // | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 39 | // | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 40 | // | View.SYSTEM_UI_FLAG_FULLSCREEN 41 | // | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 42 | } 43 | 44 | @Override 45 | public boolean onTouchEvent(MotionEvent event) { 46 | super.onTouchEvent(event); 47 | int widthPixels = getResources().getDisplayMetrics().widthPixels; 48 | int heightPixels = getResources().getDisplayMetrics().heightPixels; 49 | float x = event.getX() / (float) widthPixels; 50 | float y = event.getY() / (float) heightPixels; 51 | 52 | 53 | myGLSurfaceView.mRenderer.triangle.vLightPosition.set(2.0f*(x - 0.5f), (-y + 0.5f) * 2.0f); // going to a monitor coords 54 | Log.d(TAG, "onTouchEvent, x: " + x + ", y: " + y); 55 | return true; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/normalmapping/Square.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.normalmapping; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.nio.ByteOrder; 5 | import java.nio.FloatBuffer; 6 | import java.nio.ShortBuffer; 7 | 8 | /** 9 | * Created by egslava on 18/03/2017. 10 | */ 11 | 12 | /** 13 | * @deprecated isn't used 14 | */ 15 | public class Square { 16 | 17 | static final float[] vertices = { 18 | -0.5f, 0.5f, 0.0f, // top left 19 | -0.5f, -0.5f, 0.0f, // bottom left 20 | 0.5f, -0.5f, 0.0f, // bottom right 21 | 0.5f, 0.5f, 0.0f // top right 22 | }; 23 | private final FloatBuffer vertexBuffer; 24 | private final ShortBuffer indexBuffer; 25 | short indices[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices 26 | 27 | 28 | public Square() { 29 | 30 | ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4); 31 | bb.order(ByteOrder.nativeOrder()); 32 | 33 | vertexBuffer = bb.asFloatBuffer(); 34 | vertexBuffer.put(vertices); 35 | vertexBuffer.position(0); 36 | 37 | final ByteBuffer bb2 = ByteBuffer.allocateDirect(indices.length * 4); 38 | indexBuffer = bb2.asShortBuffer(); 39 | indexBuffer.put(indices); 40 | indexBuffer.position(0); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/normalmapping/Triangle.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.normalmapping; 2 | 3 | import android.graphics.PointF; 4 | import android.opengl.GLES20; 5 | 6 | import java.nio.ByteBuffer; 7 | import java.nio.ByteOrder; 8 | import java.nio.FloatBuffer; 9 | 10 | /** 11 | * Created by egslava on 18/03/2017. 12 | */ 13 | 14 | public class Triangle { 15 | 16 | 17 | private final String mVertexShaderCode; 18 | 19 | private final String mFragmentShaderCode; 20 | private final int mNormal; 21 | private int mTexture; 22 | 23 | static final float coordinates[] = { 24 | -1.0f, 3.0f, 0.0f, // top 25 | -1.0f, -1.0f, 0.0f, // bottom left 26 | 3.0f, -1.0f, 0.0f // bottom right 27 | }; 28 | 29 | static final float tex_coordinates[] = { 30 | 0.0f, 2.0f, 0.0f, // top 31 | 0.0f, 0.0f, 0.0f, // bottom left 32 | 6.0f, 0.0f, 0.0f // bottom right 33 | }; 34 | 35 | static final int numVertex = 3; 36 | 37 | float colors[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f }; 38 | private FloatBuffer mVertexBuffer; 39 | private FloatBuffer mTextureBuffer; 40 | private int mVertexShader; 41 | private int mPixelShader; 42 | private int mProgram; 43 | 44 | PointF vLightPosition = new PointF(0.0f, 0.0f); 45 | 46 | public Triangle(String mVertexShaderCode, String mFragmentShaderCode, int texture, int normal){ 47 | this.mVertexShaderCode = mVertexShaderCode; 48 | this.mFragmentShaderCode = mFragmentShaderCode; 49 | mTexture = texture; 50 | mNormal = normal; 51 | 52 | 53 | // vertex buffer 54 | ByteBuffer bb = ByteBuffer.allocateDirect(coordinates.length * 4); 55 | bb.order(ByteOrder.nativeOrder()); 56 | mVertexBuffer = bb.asFloatBuffer(); 57 | mVertexBuffer.put(coordinates); 58 | mVertexBuffer.position(0); 59 | 60 | 61 | 62 | // texture buffer 63 | ByteBuffer tb = ByteBuffer.allocateDirect(tex_coordinates.length * 4); 64 | tb.order(ByteOrder.nativeOrder()); 65 | mTextureBuffer = tb.asFloatBuffer(); 66 | mTextureBuffer.put(tex_coordinates); 67 | mTextureBuffer.position(0); 68 | 69 | 70 | 71 | // loading shaders 72 | mVertexShader = MyRenderer.loadShader(GLES20.GL_VERTEX_SHADER, this.mVertexShaderCode); 73 | mPixelShader = MyRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, this.mFragmentShaderCode); 74 | 75 | mProgram = GLES20.glCreateProgram(); 76 | GLES20.glAttachShader(mProgram, mVertexShader); 77 | GLES20.glAttachShader(mProgram, mPixelShader); 78 | GLES20.glLinkProgram(mProgram); 79 | } 80 | 81 | public void draw(){ 82 | GLES20.glUseProgram( mProgram ); 83 | 84 | // vertex shader 85 | final int vPosition = GLES20.glGetAttribLocation(mProgram, "vPosition"); 86 | GLES20.glEnableVertexAttribArray(vPosition); 87 | GLES20.glVertexAttribPointer(vPosition, 3, GLES20.GL_FLOAT, false, 4 * 3, mVertexBuffer); 88 | 89 | int u_texture = GLES20.glGetUniformLocation(mProgram, "u_Texture"); 90 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 91 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexture); 92 | GLES20.glUniform1i(u_texture, 0); 93 | 94 | int u_Normal = GLES20.glGetUniformLocation(mProgram, "u_Normal"); 95 | GLES20.glActiveTexture(GLES20.GL_TEXTURE1); 96 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mNormal); 97 | GLES20.glUniform1i(u_Normal, 1); 98 | 99 | final int vTexture = GLES20.glGetAttribLocation(mProgram, "vTexture"); 100 | GLES20.glEnableVertexAttribArray(vTexture); 101 | GLES20.glVertexAttribPointer(vTexture, 3, GLES20.GL_FLOAT, false, 4 * 3, mTextureBuffer); 102 | 103 | final int vColor = GLES20.glGetUniformLocation(mProgram, "vColor"); 104 | GLES20.glUniform4fv(vColor, 1, colors, 0); 105 | 106 | final int uLightPosition = GLES20.glGetUniformLocation(mProgram, "vLightPosition"); 107 | GLES20.glUniform2f(uLightPosition, vLightPosition.x, vLightPosition.y); 108 | 109 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); 110 | 111 | GLES20.glDisableVertexAttribArray(vPosition); 112 | 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/pointLight/PointLightDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.pointLight; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.view.View.OnTouchListener; 9 | 10 | import com.example.ViewThouchListener; 11 | 12 | public class PointLightDemoActivity extends Activity { 13 | private GLSurfaceView view; 14 | private PointLightRender render; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | 20 | initView(); 21 | setContentView(view); 22 | } 23 | 24 | private void initView() { 25 | view = new GLSurfaceView(this); 26 | 27 | view.setEGLContextClientVersion(2); 28 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 29 | render = new PointLightRender(this); 30 | view.setRenderer(render); 31 | 32 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 33 | view.setOnTouchListener(touchListener); 34 | } 35 | 36 | @Override 37 | protected void onPause() { 38 | super.onPause(); 39 | view.onPause(); 40 | } 41 | 42 | @Override 43 | protected void onResume() { 44 | super.onResume(); 45 | view.onResume(); 46 | } 47 | 48 | @Override 49 | protected void onDestroy() { 50 | super.onDestroy(); 51 | render.release(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/pointLight/PointLightRender.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.pointLight; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.Activity; 5 | import android.graphics.Bitmap; 6 | import android.opengl.GLES20; 7 | import android.opengl.GLSurfaceView.Renderer; 8 | import android.opengl.Matrix; 9 | 10 | import com.example.BasicRender; 11 | import com.example.opengldemo.R; 12 | import com.example.shaderUtil.MatrixHelper; 13 | import com.example.shaderUtil.ObjLoader; 14 | import com.example.shaderUtil.TextureHelper; 15 | import com.example.shaderUtil.TextureShaderProgram; 16 | import com.example.object.Vertices3; 17 | 18 | import java.util.Random; 19 | 20 | import javax.microedition.khronos.egl.EGLConfig; 21 | import javax.microedition.khronos.opengles.GL10; 22 | 23 | @SuppressLint("NewApi") 24 | public class PointLightRender extends BasicRender{ 25 | private Activity activity; 26 | private int tableTexture; 27 | 28 | private float[] projectionMatrix = new float[16]; 29 | private float[] viewMatrix = new float[16]; 30 | private final float[] modelMatrix = new float[16]; 31 | private float[] viewProjectMatrix = new float[16]; 32 | private float[] modelViewMatrix = new float[16]; 33 | private float[] it_modelViewMatrix = new float[16]; 34 | private float[] tempMatrix = new float[16]; 35 | 36 | private PointLightShaderProgram textureShader; 37 | private Vertices3 mesh; 38 | private float xAngle = 0; 39 | private float yAngle = 0; 40 | 41 | public PointLightRender(Activity activity) { 42 | this.activity = activity; 43 | } 44 | 45 | @Override 46 | public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) { 47 | GLES20.glClearColor(1F, 1F, 1F, 0F); 48 | 49 | GLES20.glEnable(GLES20.GL_DEPTH_TEST); 50 | 51 | tableTexture = TextureHelper.loadTexture(activity, R.drawable.air_hockey_surface); 52 | 53 | textureShader = new PointLightShaderProgram(activity, 54 | R.raw.point_light_vertex, R.raw.point_light_fragment); 55 | 56 | mesh = ObjLoader.load(activity, R.raw.ball); 57 | } 58 | 59 | @Override 60 | public void onSurfaceChanged(GL10 arg0, int width, int height) { 61 | GLES20.glViewport(0, 0, width, height); 62 | 63 | float fov = 120; 64 | float aspect = ((float) width / (float) height); 65 | float near = 0.1f; 66 | float far = 200f; 67 | MatrixHelper.perspectiveM(projectionMatrix, fov, aspect, near, far); 68 | 69 | Matrix.setLookAtM(viewMatrix, 0, 70 | 0f, 0f, 1f, 71 | 0f, 0.5f, -1f, 72 | 0f, 1f, 0f); 73 | 74 | Matrix.translateM(viewMatrix, 0, 0, 2, 0); 75 | } 76 | Random random = new Random(100); 77 | @Override 78 | public void onDrawFrame(GL10 arg0) { 79 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 80 | 81 | drawMesh(); 82 | } 83 | 84 | private void drawMesh(){ 85 | float[] matrix = getMeshMatrix(); 86 | textureShader.useProgram(); 87 | textureShader.setUniform(matrix, tableTexture); 88 | 89 | float[] pointLight = {1.0f, -1.0f, 2.0f, 1.0f}; 90 | Matrix.multiplyMV(pointLight, 0, viewMatrix, 0, pointLight, 0); 91 | textureShader.setPointLightUniform(pointLight); 92 | textureShader.setLightMatrix(modelViewMatrix, it_modelViewMatrix); 93 | 94 | int normalLocation = textureShader.getNormalCoordinatesLocation(); 95 | mesh.bind(textureShader.getPositionLocation(), 96 | textureShader.getTextureCoordinatesLocation(), 97 | normalLocation); 98 | mesh.draw(); 99 | } 100 | 101 | private float[] getMeshMatrix() { 102 | Matrix.multiplyMM(viewProjectMatrix, 0, projectionMatrix, 0, viewMatrix, 0); 103 | Matrix.setIdentityM(modelMatrix, 0); 104 | 105 | Matrix.translateM(modelMatrix, 0, 0f, -2f, -100f); 106 | Matrix.rotateM(modelMatrix, 0, xAngle, 1f, 0f, 0f); 107 | Matrix.rotateM(modelMatrix, 0, yAngle, 0f, 1f, 0f); 108 | float[] temp = new float[16]; 109 | float[] resultMatrix = new float[16]; 110 | Matrix.multiplyMM(temp, 0, viewProjectMatrix, 0, modelMatrix, 0); 111 | System.arraycopy(temp, 0, resultMatrix, 0, temp.length); 112 | // xAngle += 0.5; 113 | 114 | Matrix.multiplyMM(modelViewMatrix, 0, viewMatrix, 0, modelMatrix, 0); 115 | Matrix.invertM(tempMatrix, 0, modelViewMatrix, 0); 116 | Matrix.transposeM(it_modelViewMatrix, 0, tempMatrix, 0); 117 | 118 | return resultMatrix; 119 | } 120 | 121 | public void rotate(float xAngle, float yAngle) { 122 | this.xAngle += xAngle; 123 | this.yAngle += yAngle; 124 | } 125 | 126 | public void release(){ 127 | int[] textureArray = {tableTexture}; 128 | GLES20.glDeleteTextures(1, textureArray, 0); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/pointLight/PointLightShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.pointLight; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLES20; 5 | 6 | import com.example.shaderUtil.BasicShaderProgram; 7 | 8 | /** 9 | * Created by L on 2017/8/9. 10 | */ 11 | 12 | public class PointLightShaderProgram extends BasicShaderProgram { 13 | private float textureMove; 14 | private final int textureMoveLocation; 15 | private final int uLightPointLocation; 16 | private final int uMVMatrixLocation; 17 | private final int uIT_MVMatrixLocation; 18 | 19 | private final int aNormalCoordinatesLocation; 20 | 21 | public PointLightShaderProgram(Context context, int vertexShaderResourceId, int fragmentShaderResourceId) { 22 | super(context, vertexShaderResourceId, fragmentShaderResourceId); 23 | 24 | textureMoveLocation = GLES20.glGetUniformLocation(program, "textureMove"); 25 | uLightPointLocation = GLES20.glGetUniformLocation(program, U_POINT_LIGHT); 26 | uMVMatrixLocation = GLES20.glGetUniformLocation(program, U_MVMATRIX); 27 | uIT_MVMatrixLocation = GLES20.glGetUniformLocation(program, U_IT_MVMATRIX); 28 | 29 | aNormalCoordinatesLocation = GLES20.glGetAttribLocation(program, A_NORMAL_COORDINATES); 30 | } 31 | 32 | @Override 33 | public void setUniform(float[] matrix, int textureId) { 34 | super.setUniform(matrix, textureId); 35 | 36 | GLES20.glUniform1f(textureMoveLocation, textureMove); 37 | } 38 | 39 | public void setPointLightUniform(float[] lightPoint){ 40 | GLES20.glUniform4fv(uLightPointLocation, 1, lightPoint, 0); 41 | } 42 | 43 | public void setLightMatrix(float mvMatrix[], float it_mvMatrix[]){ 44 | if(mvMatrix != null) { 45 | GLES20.glUniformMatrix4fv(uMVMatrixLocation, 1, false, mvMatrix, 0); 46 | } 47 | GLES20.glUniformMatrix4fv(uIT_MVMatrixLocation, 1, false, it_mvMatrix, 0); 48 | } 49 | 50 | public int getNormalCoordinatesLocation() { 51 | return aNormalCoordinatesLocation; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/processTexture/ProcessTextureDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.processTexture; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | 7 | import com.example.ViewThouchListener; 8 | 9 | public class ProcessTextureDemoActivity extends Activity { 10 | private GLSurfaceView view; 11 | private ProcessTextureRender render; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | 17 | initView(); 18 | setContentView(view); 19 | } 20 | 21 | private void initView() { 22 | view = new GLSurfaceView(this); 23 | 24 | view.setEGLContextClientVersion(2); 25 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 26 | render = new ProcessTextureRender(this); 27 | view.setRenderer(render); 28 | 29 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 30 | view.setOnTouchListener(touchListener); 31 | 32 | } 33 | 34 | @Override 35 | protected void onPause() { 36 | super.onPause(); 37 | view.onPause(); 38 | } 39 | 40 | @Override 41 | protected void onResume() { 42 | super.onResume(); 43 | view.onResume(); 44 | } 45 | 46 | @Override 47 | protected void onDestroy() { 48 | super.onDestroy(); 49 | render.release(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/processTexture/ProcessTextureRender.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.processTexture; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.Activity; 5 | import android.opengl.GLES20; 6 | import android.opengl.Matrix; 7 | 8 | import com.example.BasicRender; 9 | import com.example.object.Vertices3; 10 | import com.example.opengldemo.R; 11 | import com.example.shaderUtil.MatrixHelper; 12 | import com.example.shaderUtil.ObjLoader; 13 | import com.example.shaderUtil.TextureHelper; 14 | 15 | import javax.microedition.khronos.egl.EGLConfig; 16 | import javax.microedition.khronos.opengles.GL10; 17 | 18 | @SuppressLint("NewApi") 19 | public class ProcessTextureRender extends BasicRender { 20 | private Activity activity; 21 | private int meshTexture; 22 | 23 | private float[] projectionMatrix = new float[16]; 24 | private float[] viewMatrix = new float[16]; 25 | private final float[] modelMatrix = new float[16]; 26 | private float[] viewProjectMatrix = new float[16]; 27 | 28 | private ProcessTextureShaderProgram textureShader; 29 | private Vertices3 mesh; 30 | private float xAngle = 0; 31 | private float yAngle = 0; 32 | 33 | public ProcessTextureRender(Activity activity) { 34 | this.activity = activity; 35 | } 36 | 37 | @Override 38 | public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) { 39 | GLES20.glClearColor(1F, 1F, 1F, 0F); 40 | 41 | GLES20.glEnable(GLES20.GL_DEPTH_TEST); 42 | GLES20.glDisable(GLES20.GL_CULL_FACE); 43 | 44 | meshTexture = TextureHelper.loadTexture(activity, R.drawable.face); 45 | 46 | textureShader = new ProcessTextureShaderProgram(activity, 47 | R.raw.rotate_vertex_shader, R.raw.process_texture_fragment); 48 | 49 | mesh = ObjLoader.load(activity, R.raw.monkey); 50 | } 51 | 52 | @Override 53 | public void onSurfaceChanged(GL10 arg0, int width, int height) { 54 | GLES20.glViewport(0, 0, width, height); 55 | 56 | float fov = 120; 57 | float aspect = ((float) width / (float) height); 58 | float near = 0.1f; 59 | float far = 200f; 60 | MatrixHelper.perspectiveM(projectionMatrix, fov, aspect, near, far); 61 | 62 | Matrix.setLookAtM(viewMatrix, 0, 63 | 0f, 0f, 1f, 64 | 0f, 0.5f, -1f, 65 | 0f, 1f, 0f); 66 | 67 | Matrix.translateM(viewMatrix, 0, 0, 0, -2); 68 | } 69 | 70 | @Override 71 | public void onDrawFrame(GL10 arg0) { 72 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 73 | 74 | drawMonkey(); 75 | } 76 | 77 | private void drawMonkey(){ 78 | float[] matrix = getMeshMatrix(); 79 | textureShader.useProgram(); 80 | textureShader.setUniform(matrix, meshTexture); 81 | 82 | mesh.bind(textureShader.getPositionLocation(), 83 | textureShader.getTextureCoordinatesLocation()); 84 | mesh.draw(); 85 | } 86 | 87 | private float[] getMeshMatrix() { 88 | Matrix.setIdentityM(modelMatrix, 0); 89 | 90 | Matrix.translateM(modelMatrix, 0, 0f, -2f, -5); 91 | Matrix.rotateM(modelMatrix, 0, xAngle, 1f, 0f, 0f); 92 | Matrix.rotateM(modelMatrix, 0, yAngle, 0f, 1f, 0f); 93 | float[] temp = new float[16]; 94 | float[] resultMatrix = new float[16]; 95 | Matrix.multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0); 96 | System.arraycopy(temp, 0, resultMatrix, 0, temp.length); 97 | // xAngle += 0.5; 98 | 99 | return resultMatrix; 100 | } 101 | 102 | public void rotate(float xAngle, float yAngle) { 103 | this.xAngle += xAngle; 104 | this.yAngle += yAngle; 105 | } 106 | 107 | public void release(){ 108 | int[] textureArray = {meshTexture}; 109 | GLES20.glDeleteTextures(1, textureArray, 0); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/processTexture/ProcessTextureShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.processTexture; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLES20; 5 | 6 | import com.example.shaderUtil.BasicShaderProgram; 7 | 8 | /** 9 | * Created by L on 2017/8/8. 10 | */ 11 | 12 | public class ProcessTextureShaderProgram extends BasicShaderProgram { 13 | private final int uRadianAngleLocation; 14 | private float radianAngle; 15 | 16 | public ProcessTextureShaderProgram(Context context, int vertexShaderResourceId, int fragmentShaderResourceId) { 17 | super(context, vertexShaderResourceId, fragmentShaderResourceId); 18 | uRadianAngleLocation = GLES20.glGetUniformLocation(program, "radianAngle"); 19 | } 20 | 21 | @Override 22 | public void setUniform(float[] matrix, int textureId) { 23 | super.setUniform(matrix, textureId); 24 | 25 | radianAngle += 0.05; 26 | GLES20.glUniform1f(uRadianAngleLocation, radianAngle); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/rayPickup/RayPickupDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.rayPickup; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.view.View.OnTouchListener; 9 | 10 | public class RayPickupDemoActivity extends Activity { 11 | private GLSurfaceView view; 12 | private RayPickupLightRender render; 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | 18 | initView(); 19 | setContentView(view); 20 | } 21 | 22 | private void initView() { 23 | view = new GLSurfaceView(this); 24 | 25 | ViewThouchListener touchListener = new ViewThouchListener(); 26 | view.setOnTouchListener(touchListener); 27 | 28 | view.setEGLContextClientVersion(2); 29 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 30 | render = new RayPickupLightRender(this); 31 | view.setRenderer(render); 32 | } 33 | 34 | @Override 35 | protected void onPause() { 36 | super.onPause(); 37 | view.onPause(); 38 | } 39 | 40 | @Override 41 | protected void onResume() { 42 | super.onResume(); 43 | view.onResume(); 44 | } 45 | 46 | private class ViewThouchListener implements OnTouchListener { 47 | private float lastX = 0; 48 | private float lastY = 0; 49 | 50 | @Override 51 | public boolean onTouch(View v, final MotionEvent event) { 52 | if(event == null){ 53 | return false; 54 | } 55 | 56 | final float normalizedX = (event.getX() / (float) v.getWidth()) * 2 - 1; 57 | final float normalizedY = -((event.getY() / (float) v.getHeight()) * 2 - 1); 58 | if (event.getAction() == MotionEvent.ACTION_DOWN) { 59 | view.queueEvent(new Runnable() { 60 | @Override 61 | public void run() { 62 | render.handleTouchPress(normalizedX, normalizedY, true); 63 | } 64 | }); 65 | }else if(event.getAction() == MotionEvent.ACTION_MOVE){ 66 | render.handleTouchPress(normalizedX, normalizedY, false); 67 | } 68 | return true; 69 | } 70 | } 71 | 72 | @Override 73 | protected void onDestroy() { 74 | super.onDestroy(); 75 | render.release(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/reflectLight/CommonShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.reflectLight; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLES20; 5 | 6 | import com.example.shaderUtil.BasicShaderProgram; 7 | 8 | /** 9 | * Created by L on 2017/8/9. 10 | */ 11 | public class CommonShaderProgram extends BasicShaderProgram { 12 | private float textureMove; 13 | private final int textureMoveLocation; 14 | private final int uLightPointLocation; 15 | private final int uMVMatrixLocation; 16 | private final int uIT_MVMatrixLocation; 17 | 18 | private final int aNormalCoordinatesLocation; 19 | 20 | public CommonShaderProgram(Context context, int vertexShaderResourceId, int fragmentShaderResourceId) { 21 | super(context, vertexShaderResourceId, fragmentShaderResourceId); 22 | 23 | textureMoveLocation = GLES20.glGetUniformLocation(program, "textureMove"); 24 | uLightPointLocation = GLES20.glGetUniformLocation(program, U_POINT_LIGHT); 25 | uMVMatrixLocation = GLES20.glGetUniformLocation(program, U_MVMATRIX); 26 | uIT_MVMatrixLocation = GLES20.glGetUniformLocation(program, U_IT_MVMATRIX); 27 | 28 | aNormalCoordinatesLocation = GLES20.glGetAttribLocation(program, A_NORMAL_COORDINATES); 29 | } 30 | 31 | @Override 32 | public void setUniform(float[] matrix, int textureId) { 33 | super.setUniform(matrix, textureId); 34 | 35 | GLES20.glUniform1f(textureMoveLocation, textureMove); 36 | } 37 | 38 | public void setPointLightUniform(float[] lightPoint){ 39 | GLES20.glUniform4fv(uLightPointLocation, 1, lightPoint, 0); 40 | } 41 | 42 | public void setLightMatrix(float mvMatrix[], float it_mvMatrix[]){ 43 | if(mvMatrix != null) { 44 | GLES20.glUniformMatrix4fv(uMVMatrixLocation, 1, false, mvMatrix, 0); 45 | } 46 | GLES20.glUniformMatrix4fv(uIT_MVMatrixLocation, 1, false, it_mvMatrix, 0); 47 | } 48 | 49 | public int getNormalCoordinatesLocation() { 50 | return aNormalCoordinatesLocation; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/reflectLight/ReflectLightDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.reflectLight; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.view.View.OnTouchListener; 9 | 10 | import com.example.ViewThouchListener; 11 | 12 | public class ReflectLightDemoActivity extends Activity { 13 | private GLSurfaceView view; 14 | private ReflectLightRender render; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | 20 | initView(); 21 | setContentView(view); 22 | } 23 | 24 | private void initView() { 25 | view = new GLSurfaceView(this); 26 | 27 | view.setEGLContextClientVersion(2); 28 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 29 | render = new ReflectLightRender(this); 30 | view.setRenderer(render); 31 | 32 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 33 | view.setOnTouchListener(touchListener); 34 | } 35 | 36 | @Override 37 | protected void onPause() { 38 | super.onPause(); 39 | view.onPause(); 40 | } 41 | 42 | @Override 43 | protected void onResume() { 44 | super.onResume(); 45 | view.onResume(); 46 | } 47 | 48 | @Override 49 | protected void onDestroy() { 50 | super.onDestroy(); 51 | render.release(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/reflectLight/ReflectLightRender.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.reflectLight; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.Activity; 5 | import android.graphics.Bitmap; 6 | import android.opengl.GLES20; 7 | import android.opengl.GLSurfaceView.Renderer; 8 | import android.opengl.Matrix; 9 | 10 | import com.example.BasicRender; 11 | import com.example.opengldemo.R; 12 | import com.example.opengldemo.pointLight.PointLightShaderProgram; 13 | import com.example.shaderUtil.MatrixHelper; 14 | import com.example.shaderUtil.ObjLoader; 15 | import com.example.shaderUtil.TextureHelper; 16 | import com.example.shaderUtil.TextureShaderProgram; 17 | import com.example.object.Vertices3; 18 | 19 | import javax.microedition.khronos.egl.EGLConfig; 20 | import javax.microedition.khronos.opengles.GL10; 21 | 22 | @SuppressLint("NewApi") 23 | public class ReflectLightRender extends BasicRender { 24 | private Activity activity; 25 | private int tableTexture; 26 | 27 | private float[] projectionMatrix = new float[16]; 28 | private float[] viewMatrix = new float[16]; 29 | private final float[] modelMatrix = new float[16]; 30 | private float[] viewProjectMatrix = new float[16]; 31 | private float[] modelViewMatrix = new float[16]; 32 | private float[] it_modelViewMatrix = new float[16]; 33 | private float[] tempMatrix = new float[16]; 34 | 35 | private PointLightShaderProgram textureShader; 36 | private Vertices3 mesh; 37 | private float xAngle = 0; 38 | private float yAngle = 0; 39 | 40 | public ReflectLightRender(Activity activity) { 41 | this.activity = activity; 42 | } 43 | 44 | @Override 45 | public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) { 46 | GLES20.glClearColor(1F, 1F, 1F, 0F); 47 | 48 | GLES20.glEnable(GLES20.GL_DEPTH_TEST); 49 | 50 | tableTexture = TextureHelper.loadTexture(activity, R.drawable.air_hockey_surface); 51 | 52 | textureShader = new PointLightShaderProgram(activity, 53 | R.raw.reflect_light_vertext, R.raw.reflect_light_fragment); 54 | 55 | mesh = ObjLoader.load(activity, R.raw.cube); 56 | } 57 | 58 | @Override 59 | public void onSurfaceChanged(GL10 arg0, int width, int height) { 60 | GLES20.glViewport(0, 0, width, height); 61 | 62 | float fov = 120; 63 | float aspect = ((float) width / (float) height); 64 | float near = 0.1f; 65 | float far = 200f; 66 | MatrixHelper.perspectiveM(projectionMatrix, fov, aspect, near, far); 67 | 68 | Matrix.setLookAtM(viewMatrix, 0, 69 | 0f, 0f, 1f, 70 | 0f, 0.5f, -1f, 71 | 0f, 1f, 0f); 72 | 73 | Matrix.translateM(viewMatrix, 0, 0, 2, 0); 74 | } 75 | 76 | @Override 77 | public void onDrawFrame(GL10 arg0) { 78 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 79 | 80 | drawMesh(); 81 | } 82 | 83 | private void drawMesh(){ 84 | float[] matrix = getMeshMatrix(); 85 | textureShader.useProgram(); 86 | textureShader.setUniform(matrix, tableTexture); 87 | 88 | float[] pointLight = {2f, 2f, 1.0f, 1.0f}; 89 | Matrix.multiplyMV(pointLight, 0, viewMatrix, 0, pointLight, 0); 90 | textureShader.setPointLightUniform(pointLight); 91 | textureShader.setLightMatrix(modelViewMatrix, it_modelViewMatrix); 92 | 93 | int normalLocation = textureShader.getNormalCoordinatesLocation(); 94 | mesh.bind(textureShader.getPositionLocation(), 95 | textureShader.getTextureCoordinatesLocation(), 96 | normalLocation); 97 | mesh.draw(); 98 | } 99 | 100 | private float[] getMeshMatrix() { 101 | Matrix.multiplyMM(viewProjectMatrix, 0, projectionMatrix, 0, viewMatrix, 0); 102 | Matrix.setIdentityM(modelMatrix, 0); 103 | 104 | Matrix.translateM(modelMatrix, 0, 0f, -2f, -2.5f); 105 | Matrix.rotateM(modelMatrix, 0, xAngle, 1f, 0f, 0f); 106 | Matrix.rotateM(modelMatrix, 0, yAngle, 0f, 1f, 0f); 107 | float[] temp = new float[16]; 108 | float[] resultMatrix = new float[16]; 109 | Matrix.multiplyMM(temp, 0, viewProjectMatrix, 0, modelMatrix, 0); 110 | System.arraycopy(temp, 0, resultMatrix, 0, temp.length); 111 | // xAngle += 0.5; 112 | 113 | Matrix.multiplyMM(modelViewMatrix, 0, viewMatrix, 0, modelMatrix, 0); 114 | Matrix.invertM(tempMatrix, 0, modelViewMatrix, 0); 115 | Matrix.transposeM(it_modelViewMatrix, 0, tempMatrix, 0); 116 | 117 | return resultMatrix; 118 | } 119 | 120 | public void rotate(float xAngle, float yAngle) { 121 | this.xAngle += xAngle; 122 | this.yAngle += yAngle; 123 | } 124 | 125 | public void release(){ 126 | int[] textureArray = {tableTexture}; 127 | GLES20.glDeleteTextures(1, textureArray, 0); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/reflectLight/SkyboxShaderProgam.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.reflectLight; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLES20; 5 | 6 | public class SkyboxShaderProgam extends CommonShaderProgram { 7 | private final int skyCubeLoation; 8 | private final int matrixForSkyLocation; 9 | 10 | public SkyboxShaderProgam(Context context, int vertexShaderResourceId, int fragmentShaderResourceId) { 11 | super(context, vertexShaderResourceId, fragmentShaderResourceId); 12 | 13 | skyCubeLoation = GLES20.glGetUniformLocation(program, "skyCube"); 14 | matrixForSkyLocation= GLES20.glGetUniformLocation(program, "matrixForSky"); 15 | } 16 | 17 | public void setSkyCube(int skyCube, float[] skyMatrix){ 18 | GLES20.glActiveTexture(GLES20.GL_TEXTURE1); 19 | GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, skyCube); 20 | GLES20.glUniform1i(skyCubeLoation, 1); 21 | 22 | GLES20.glUniformMatrix4fv(matrixForSkyLocation, 1, false, skyMatrix, 0); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/ripple/RippleDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.ripple; 2 | 3 | import android.Manifest; 4 | import android.app.Activity; 5 | import android.content.pm.PackageManager; 6 | import android.opengl.GLSurfaceView; 7 | import android.os.Build; 8 | import android.os.Bundle; 9 | import android.support.annotation.NonNull; 10 | import android.support.v4.app.ActivityCompat; 11 | import android.support.v4.content.ContextCompat; 12 | import android.util.Log; 13 | import android.view.MotionEvent; 14 | import android.view.View; 15 | import android.view.View.OnTouchListener; 16 | import android.widget.Toast; 17 | 18 | public class RippleDemoActivity extends Activity { 19 | private GLSurfaceView view; 20 | private RippleRender render; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | this.applyCamerapermission(); 26 | } 27 | 28 | private void initView() { 29 | view = new GLSurfaceView(this); 30 | 31 | view.setEGLContextClientVersion(2); 32 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 33 | render = new RippleRender(this, view); 34 | view.setRenderer(render); 35 | 36 | // ViewThouchListener touchListener = new ViewThouchListener(view, render); 37 | view.setOnTouchListener(new ViewThouchListener()); 38 | } 39 | 40 | @Override 41 | protected void onPause() { 42 | super.onPause(); 43 | if(view != null) { 44 | try{ 45 | view.onPause(); 46 | }catch (Exception e){ 47 | 48 | } 49 | 50 | } 51 | } 52 | 53 | @Override 54 | protected void onResume() { 55 | super.onResume(); 56 | if(view != null) { 57 | try{ 58 | view.onResume(); 59 | }catch (Exception e){ 60 | 61 | } 62 | 63 | } 64 | } 65 | 66 | @Override 67 | protected void onDestroy() { 68 | super.onDestroy(); 69 | if(view != null) { 70 | try{ 71 | render.releaseCamera(); 72 | }catch (Exception e){ 73 | 74 | } 75 | 76 | } 77 | } 78 | 79 | private class ViewThouchListener implements OnTouchListener { 80 | private float lastX = 0; 81 | private float lastY = 0; 82 | 83 | @Override 84 | public boolean onTouch(View v, final MotionEvent event) { 85 | if(event == null){ 86 | return false; 87 | } 88 | 89 | final float normalizedX = (event.getX() / (float) v.getWidth()) * 2 - 1; 90 | final float normalizedY = -((event.getY() / (float) v.getHeight()) * 2 - 1); 91 | Log.i("intersectPoint", "----touch point: "+normalizedX+" ,"+normalizedY); 92 | if (event.getAction() == MotionEvent.ACTION_DOWN) { 93 | view.queueEvent(new Runnable() { 94 | @Override 95 | public void run() { 96 | render.handleTouchPress(normalizedX ,normalizedY); 97 | } 98 | }); 99 | } 100 | // else if(event.getAction() == MotionEvent.ACTION_MOVE){ 101 | // render.handleTouchPress(normalizedX, normalizedY, false); 102 | // } 103 | return true; 104 | } 105 | } 106 | 107 | 108 | public void applyCamerapermission(){ 109 | if(Build.VERSION.SDK_INT>=23){ 110 | //检查是否已经给了权限 111 | int checkpermission= ContextCompat.checkSelfPermission(this.getApplicationContext(), 112 | Manifest.permission.CAMERA); 113 | if(checkpermission!= PackageManager.PERMISSION_GRANTED){//没有给权限 114 | Log.e("permission","动态申请"); 115 | //参数分别是当前活动,权限字符串数组,requestcode 116 | ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, 1); 117 | }else { 118 | initView(); 119 | setContentView(view); 120 | } 121 | }else { 122 | initView(); 123 | setContentView(view); 124 | } 125 | } 126 | 127 | @Override 128 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 129 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 130 | //grantResults数组与权限字符串数组对应,里面存放权限申请结果 131 | if(grantResults[0]== PackageManager.PERMISSION_GRANTED){ 132 | try{ 133 | initView(); 134 | setContentView(view); 135 | }catch (Exception e){ 136 | e.printStackTrace(); 137 | Toast.makeText(this, "请开启摄像头权限", Toast.LENGTH_LONG).show(); 138 | finish(); 139 | } 140 | // Toast.makeText(this,"已授权",Toast.LENGTH_SHORT).show(); 141 | }else{ 142 | Toast.makeText(this, "请开启摄像头权限", Toast.LENGTH_LONG).show(); 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/ripple/RippleShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.ripple; 2 | 3 | import com.example.shaderUtil.BasicShaderProgram; 4 | 5 | import android.content.Context; 6 | import android.opengl.GLES11Ext; 7 | import android.opengl.GLES20; 8 | import android.os.Handler; 9 | import android.os.Looper; 10 | import android.support.annotation.MainThread; 11 | 12 | import java.util.Random; 13 | 14 | /** 15 | * Created by L on 2017/8/8. 16 | */ 17 | 18 | public class RippleShaderProgram extends BasicShaderProgram implements Runnable{ 19 | private final int aTextureUnitLocation; 20 | private final int uTimeLocation; 21 | private final int textureCoordinateYLocation; 22 | private final int uCustomPointLocation; 23 | private final int uModelMatrixLocation; 24 | private final int uItModelViewMatrixLocation; 25 | private final int uCameraTexmatLocation; 26 | private float time; 27 | private float textureCoordinateY; 28 | private int tableTexture; 29 | 30 | private volatile boolean isWave = false; 31 | private Random random = new Random(); 32 | // private float randomX, randomZ; 33 | private float[] customPoint = new float[2]; 34 | 35 | public RippleShaderProgram(Context context, int vertexShaderResourceId, int fragmentShaderResourceId, int tableTexture) { 36 | super(context, vertexShaderResourceId, fragmentShaderResourceId); 37 | 38 | aTextureUnitLocation = GLES20.glGetUniformLocation(program, "a_TextureUnit"); 39 | uTimeLocation = GLES20.glGetUniformLocation(program, "time"); 40 | textureCoordinateYLocation = GLES20.glGetUniformLocation(program, "textureCoordinateY"); 41 | this.tableTexture = tableTexture; 42 | uCustomPointLocation = GLES20.glGetUniformLocation(program, "customPoint"); 43 | uModelMatrixLocation = GLES20.glGetUniformLocation(program, "u_modelMatrix"); 44 | uItModelViewMatrixLocation = GLES20.glGetUniformLocation(program, "it_Matrix"); 45 | uCameraTexmatLocation = GLES20.glGetUniformLocation(program, "cameraTexmat"); 46 | } 47 | 48 | Thread loadTask = null; 49 | 50 | @Override 51 | public void setUniform(float[] matrix, int textureId) { 52 | super.setUniform(matrix, textureId); 53 | 54 | if(loadTask == null) { 55 | customPoint[0] = random.nextInt(20)-10; 56 | customPoint[1] = random.nextInt(20)-10; 57 | GLES20.glUniform2fv(uCustomPointLocation, 1, customPoint, 0); 58 | 59 | loadTask = new Thread(this); 60 | loadTask.start(); 61 | } 62 | 63 | GLES20.glActiveTexture(GLES20.GL_TEXTURE1); 64 | // GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tableTexture); 65 | GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, tableTexture); 66 | GLES20.glUniform1i(aTextureUnitLocation, 1); 67 | 68 | 69 | // if(time < 15){ 70 | if(isWave) { 71 | time += 0.0015f; 72 | GLES20.glUniform1f(uTimeLocation, time); 73 | isWave = false; 74 | }else if(!isWave && time > 1){ 75 | // time = -1.0f; 76 | // GLES20.glUniform1f(uTimeLocation, time); 77 | 78 | customPoint[0] = random.nextInt(20)-10; 79 | customPoint[1] = random.nextInt(20)-10; 80 | GLES20.glUniform2fv(uCustomPointLocation, 1, customPoint, 0); 81 | time = 0; 82 | 83 | } 84 | // }else { 85 | // time = 0; 86 | // GLES20.glUniform1f(uTimeLocation, time); 87 | // } 88 | 89 | // textureCoordinateY += 0.005; 90 | GLES20.glUniform1f(textureCoordinateYLocation, textureCoordinateY); 91 | } 92 | 93 | public void setItMVmatrix(float[] itMVmatrix){ 94 | GLES20.glUniformMatrix4fv(uItModelViewMatrixLocation, 1, false, itMVmatrix, 0); 95 | } 96 | 97 | public void setCameraTexmat(float[] cameraTexmat){ 98 | GLES20.glUniformMatrix4fv(uCameraTexmatLocation, 1, false, cameraTexmat, 0); 99 | } 100 | 101 | private void setCustomPoint( float[] customPoint, float[] modelMatrix){ 102 | if(customPoint == null){ 103 | return; 104 | } 105 | 106 | try{//java.lang.IllegalArgumentException: length - offset < count*3 < needed 107 | GLES20.glUniform3fv(uCustomPointLocation, 1, customPoint, 0); 108 | // uModelMatrixLocation 109 | GLES20.glUniform3fv(uModelMatrixLocation, 1, modelMatrix, 0); 110 | }catch (Exception e){ 111 | e.printStackTrace(); 112 | } 113 | 114 | } 115 | 116 | int waveCount = 0; 117 | 118 | @Override 119 | public void run() { 120 | while (true){ 121 | if(time <= 1){ 122 | try { 123 | Thread.sleep(5); 124 | } catch (InterruptedException e) { 125 | e.printStackTrace(); 126 | } 127 | isWave = true; 128 | waveCount++; 129 | } 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/rotate/RotateDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.rotate; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.view.MotionEvent; 8 | import android.view.View; 9 | import android.view.View.OnTouchListener; 10 | 11 | public class RotateDemoActivity extends Activity { 12 | private GLSurfaceView view; 13 | private RotateRender render; 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | 19 | initView(); 20 | setContentView(view); 21 | } 22 | 23 | private void initView() { 24 | view = new GLSurfaceView(this); 25 | view.setEGLContextClientVersion(2); 26 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 27 | render = new RotateRender(this); 28 | view.setRenderer(render); 29 | } 30 | 31 | @Override 32 | protected void onPause() { 33 | super.onPause(); 34 | view.onPause(); 35 | } 36 | 37 | @Override 38 | protected void onResume() { 39 | super.onResume(); 40 | view.onResume(); 41 | } 42 | 43 | @Override 44 | protected void onDestroy() { 45 | super.onDestroy(); 46 | render.destroy(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/shadowMapping/Plane.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.shadowMapping; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.nio.ByteOrder; 5 | import java.nio.FloatBuffer; 6 | 7 | import com.example.opengldemo.shadowMapping.common.RenderConstants; 8 | 9 | import android.opengl.GLES20; 10 | 11 | public class Plane { 12 | private final FloatBuffer planePosition; 13 | private final FloatBuffer planeNormal; 14 | private final FloatBuffer planeColor; 15 | 16 | //TODO: remove 17 | int translateY = 0; 18 | int translateZ = 0; 19 | 20 | float[] planePositionData = { 21 | // X, Y, Z, 22 | -25.0f, -5.0f, -25.0f + translateZ, 23 | -25.0f, -5.0f, 25.0f + translateZ, 24 | 25.0f, -5.0f, -25.0f + translateZ, 25 | -25.0f, -5.0f, 25.0f + translateZ, 26 | 25.0f, -5.0f, 25.0f + translateZ, 27 | 25.0f, -5.0f, -25.0f + translateZ 28 | }; 29 | 30 | float[] planeNormalData = { 31 | // nX, nY, nZ 32 | 0.0f, 1.0f, 0.0f, 33 | 0.0f, 1.0f, 0.0f, 34 | 0.0f, 1.0f, 0.0f, 35 | 0.0f, 1.0f, 0.0f, 36 | 0.0f, 1.0f, 0.0f, 37 | 0.0f, 1.0f, 0.0f 38 | }; 39 | 40 | float[] planeColorData = { 41 | // R, G, B, A 42 | 0.5f, 0.5f, 0.5f, 1.0f, 43 | 0.5f, 0.5f, 0.5f, 1.0f, 44 | 0.5f, 0.5f, 0.5f, 1.0f, 45 | 0.5f, 0.5f, 0.5f, 1.0f, 46 | 0.5f, 0.5f, 0.5f, 1.0f, 47 | 0.5f, 0.5f, 0.5f, 1.0f 48 | }; 49 | 50 | public Plane() { 51 | // Buffer initialization 52 | ByteBuffer bPos = ByteBuffer.allocateDirect(planePositionData.length * RenderConstants.FLOAT_SIZE_IN_BYTES); 53 | bPos.order(ByteOrder.nativeOrder()); 54 | planePosition = bPos.asFloatBuffer(); 55 | 56 | ByteBuffer bNormal = ByteBuffer.allocateDirect(planeNormalData.length * RenderConstants.FLOAT_SIZE_IN_BYTES); 57 | bNormal.order(ByteOrder.nativeOrder()); 58 | planeNormal = bNormal.asFloatBuffer(); 59 | 60 | ByteBuffer bColor = ByteBuffer.allocateDirect(planeColorData.length * RenderConstants.FLOAT_SIZE_IN_BYTES); 61 | bColor.order(ByteOrder.nativeOrder()); 62 | planeColor = bColor.asFloatBuffer(); 63 | 64 | planePosition.put(planePositionData).position(0); 65 | planeNormal.put(planeNormalData).position(0); 66 | planeColor.put(planeColorData).position(0); 67 | } 68 | 69 | public void render(int positionAttribute, int normalAttribute, int colorAttribute, boolean onlyPosition) { 70 | 71 | // Pass position information to shader 72 | planePosition.position(0); 73 | GLES20.glVertexAttribPointer(positionAttribute, 3, GLES20.GL_FLOAT, false, 74 | 0, planePosition); 75 | 76 | GLES20.glEnableVertexAttribArray(positionAttribute); 77 | 78 | if (!onlyPosition) 79 | { 80 | // Pass normal information to shader 81 | planeNormal.position(0); 82 | GLES20.glVertexAttribPointer(normalAttribute, 3, GLES20.GL_FLOAT, false, 83 | 0, planeNormal); 84 | 85 | GLES20.glEnableVertexAttribArray(normalAttribute); 86 | 87 | // Pass color information to shader 88 | planeColor.position(0); 89 | GLES20.glVertexAttribPointer(colorAttribute, 4, GLES20.GL_FLOAT, false, 90 | 0, planeColor); 91 | 92 | GLES20.glEnableVertexAttribArray(colorAttribute); 93 | } 94 | 95 | // Draw the plane 96 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6); 97 | } 98 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/shadowMapping/ShadowMappingtDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.shadowMapping; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | 6 | public class ShadowMappingtDemoActivity extends Activity { 7 | 8 | private ShadowsGLSurfaceView mGLView; 9 | private ShadowsRenderer renderer; 10 | /** 11 | * Type of shadow bias to reduce unnecessary shadows 12 | * - constant bias 13 | * - bias value is variable according to slope 14 | */ 15 | private float mBiasType = 0.0f; 16 | /** 17 | * Type of shadow algorithm 18 | * - simple shadow (shadow value is only two state (yes/no) so aliasing is visible, no blur effect is possible) 19 | * - Percentage Closer Filtering (PCF) 20 | */ 21 | private float mShadowType = 0.0f; 22 | /** 23 | * Shadow map size: 24 | * - displayWidth * SHADOW_MAP_RATIO 25 | * - displayHeight * SHADOW_MAP_RATIO 26 | */ 27 | private float mShadowMapRatio = 1; 28 | 29 | @Override 30 | public void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | 33 | // Create a GLSurfaceView instance and set it 34 | // as the ContentView for this Activity 35 | mGLView = new ShadowsGLSurfaceView(this); 36 | 37 | // Create an OpenGL ES 2.0 context. 38 | mGLView.setEGLContextClientVersion(2); 39 | mGLView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 40 | 41 | renderer = new ShadowsRenderer(this); 42 | mGLView.setRenderer(renderer); 43 | 44 | setContentView(mGLView); 45 | 46 | // Toast.makeText(this, R.string.user_hint, Toast.LENGTH_SHORT).show(); 47 | } 48 | 49 | @Override 50 | protected void onPause() { 51 | super.onPause(); 52 | mGLView.onPause(); 53 | } 54 | 55 | @Override 56 | protected void onResume() { 57 | super.onResume(); 58 | mGLView.onResume(); 59 | } 60 | 61 | public float getmBiasType() { 62 | return mBiasType; 63 | } 64 | 65 | private void setmBiasType(float mBiasType) { 66 | this.mBiasType = mBiasType; 67 | } 68 | 69 | public float getmShadowType() { 70 | return mShadowType; 71 | } 72 | 73 | private void setmShadowType(float mShadowType) { 74 | this.mShadowType = mShadowType; 75 | } 76 | 77 | public float getmShadowMapRatio() { 78 | return mShadowMapRatio; 79 | } 80 | 81 | private void setmShadowMapRatio(float mShadowMapRatio) { 82 | this.mShadowMapRatio = mShadowMapRatio; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/shadowMapping/ShadowsGLSurfaceView.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.shadowMapping; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLSurfaceView; 5 | import android.view.MotionEvent; 6 | 7 | public class ShadowsGLSurfaceView extends GLSurfaceView { 8 | private ShadowsRenderer mRenderer; 9 | 10 | public ShadowsGLSurfaceView(Context context) { 11 | super(context); 12 | 13 | // Render the view only when there is a change in the drawing data 14 | //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 15 | } 16 | 17 | private final float TOUCH_SCALE_FACTOR = 180.0f / 320; 18 | private float mPreviousX; 19 | private float mPreviousY; 20 | 21 | @Override 22 | public boolean onTouchEvent(MotionEvent e) { 23 | // MotionEvent reports input details from the touch screen 24 | // and other input controls. In this case, you are only 25 | // interested in events where the touch position changed. 26 | 27 | float x = e.getX(); 28 | float y = e.getY(); 29 | 30 | switch (e.getAction()) { 31 | case MotionEvent.ACTION_MOVE: 32 | 33 | float dx = x - mPreviousX; 34 | float dy = y - mPreviousY; 35 | 36 | mRenderer.setRotationX( 37 | mRenderer.getRotationX() + 38 | (dx * TOUCH_SCALE_FACTOR)); // = 180.0f / 320 39 | 40 | mRenderer.setRotationY( 41 | mRenderer.getRotationY() + 42 | (dy * TOUCH_SCALE_FACTOR)); // = 180.0f / 320 43 | 44 | requestRender(); 45 | } 46 | 47 | mPreviousX = x; 48 | mPreviousY = y; 49 | return true; 50 | } 51 | 52 | public void setRenderer(ShadowsRenderer renderer) { 53 | mRenderer = renderer; 54 | super.setRenderer(renderer); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/shadowMapping/common/FPSCounter.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.shadowMapping.common; 2 | 3 | import android.util.Log; 4 | 5 | public class FPSCounter { 6 | long startTime = System.nanoTime(); 7 | int frames = 0; 8 | 9 | public void logFrame() { 10 | frames++; 11 | if(System.nanoTime() - startTime >= 1000000000) { 12 | Log.i("FPSCounter", "fps: " + frames); 13 | frames = 0; 14 | startTime = System.nanoTime(); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/shadowMapping/common/RenderConstants.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.shadowMapping.common; 2 | 3 | public class RenderConstants { 4 | 5 | /** Identifiers for our uniforms and attributes inside the shaders. */ 6 | public static final String MVP_MATRIX_UNIFORM = "uMVPMatrix"; 7 | public static final String MV_MATRIX_UNIFORM = "uMVMatrix"; 8 | public static final String NORMAL_MATRIX_UNIFORM = "uNormalMatrix"; 9 | public static final String LIGHT_POSITION_UNIFORM = "uLightPos"; 10 | public static final String POSITION_ATTRIBUTE = "aPosition"; 11 | public static final String NORMAL_ATTRIBUTE = "aNormal"; 12 | public static final String COLOR_ATTRIBUTE = "aColor"; 13 | public static final String TEX_COORDINATE = "aTexCoordinate"; 14 | 15 | public static final String SHADOW_TEXTURE = "uShadowTexture"; 16 | public static final String SHADOW_PROJ_MATRIX = "uShadowProjMatrix"; 17 | public static final String SHADOW_X_PIXEL_OFFSET = "uxPixelOffset"; 18 | public static final String SHADOW_Y_PIXEL_OFFSET = "uyPixelOffset"; 19 | 20 | public static final String SHADOW_POSITION_ATTRIBUTE = "aShadowPosition"; 21 | 22 | public static final String TEXTURE_UNIFORM = "uTexture"; 23 | 24 | /** Additional constants. */ 25 | public static final int FLOAT_SIZE_IN_BYTES = 4; 26 | public static final int SHORT_SIZE_IN_BYTES = 2; 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/shadowMapping/common/RenderProgram.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Represents a shader object 3 | * from Shayan Javed 4 | * http://blog.shayanjaved.com/2011/03/13/shaders-android/ 5 | * 6 | * modified! 7 | */ 8 | 9 | package com.example.opengldemo.shadowMapping.common; 10 | 11 | import java.io.BufferedReader; 12 | import java.io.InputStream; 13 | import java.io.InputStreamReader; 14 | 15 | import android.content.Context; 16 | import android.opengl.GLES20; 17 | import android.util.Log; 18 | 19 | public class RenderProgram { 20 | /************************ 21 | * PROPERTIES 22 | **********************/ 23 | private static final String TAG = "RenderProgram"; 24 | 25 | // program/vertex/fragment handles 26 | private int mProgram, mVertexShader, mPixelShader; 27 | 28 | // The shaders 29 | private String mVertexS, mFragmentS; 30 | 31 | /************************ 32 | * CONSTRUCTOR(S) 33 | *************************/ 34 | // Takes in Strings directly 35 | public RenderProgram(String vertexS, String fragmentS) { 36 | setup(vertexS, fragmentS); 37 | } 38 | 39 | // Takes in ids for files to be read 40 | public RenderProgram(int vID, int fID, Context context) { 41 | StringBuffer vs = new StringBuffer(); 42 | StringBuffer fs = new StringBuffer(); 43 | 44 | // read the files 45 | try { 46 | // Read the file from the resource 47 | //Log.d("loadFile", "Trying to read vs"); 48 | // Read VS first 49 | InputStream inputStream = context.getResources().openRawResource(vID); 50 | // setup Bufferedreader 51 | BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); 52 | 53 | String read = in.readLine(); 54 | while (read != null) { 55 | vs.append(read + "\n"); 56 | read = in.readLine(); 57 | } 58 | 59 | vs.deleteCharAt(vs.length() - 1); 60 | 61 | // Now read FS 62 | inputStream = context.getResources().openRawResource(fID); 63 | // setup Bufferedreader 64 | in = new BufferedReader(new InputStreamReader(inputStream)); 65 | 66 | read = in.readLine(); 67 | while (read != null) { 68 | fs.append(read + "\n"); 69 | read = in.readLine(); 70 | } 71 | 72 | fs.deleteCharAt(fs.length() - 1); 73 | } catch (Exception e) { 74 | Log.d(TAG, "Could not read shader: " + e.getLocalizedMessage()); 75 | } 76 | 77 | 78 | // Setup everything 79 | setup(vs.toString(), fs.toString()); 80 | } 81 | 82 | 83 | /************************** 84 | * OTHER METHODS 85 | *************************/ 86 | 87 | /** 88 | * Sets up everything 89 | * @param vs the vertex shader 90 | * @param fs the fragment shader 91 | */ 92 | private void setup(String vs, String fs) { 93 | this.mVertexS = vs; 94 | this.mFragmentS = fs; 95 | 96 | // create the program 97 | if (createProgram() != 1) { 98 | throw new RuntimeException("Error at creating shaders"); 99 | }; 100 | } 101 | 102 | /** 103 | * Creates a shader program. 104 | * @param vertexSource 105 | * @param fragmentSource 106 | * @return returns 1 if creation successful, 0 if not 107 | */ 108 | private int createProgram() { 109 | // Vertex shader 110 | mVertexShader = loadShader(GLES20.GL_VERTEX_SHADER, mVertexS); 111 | if (mVertexShader == 0) { 112 | return 0; 113 | } 114 | 115 | // pixel shader 116 | mPixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, mFragmentS); 117 | if (mPixelShader == 0) { 118 | return 0; 119 | } 120 | 121 | // Create the program 122 | mProgram = GLES20.glCreateProgram(); 123 | if (mProgram != 0) { 124 | GLES20.glAttachShader(mProgram, mVertexShader); 125 | //checkGlError("glAttachShader VS " + this.toString()); 126 | GLES20.glAttachShader(mProgram, mPixelShader); 127 | //checkGlError("glAttachShader PS"); 128 | GLES20.glLinkProgram(mProgram); 129 | int[] linkStatus = new int[1]; 130 | GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, linkStatus, 0); 131 | if (linkStatus[0] != GLES20.GL_TRUE) { 132 | Log.e(TAG, "Could not link _program: "); 133 | Log.e(TAG, GLES20.glGetProgramInfoLog(mProgram)); 134 | GLES20.glDeleteProgram(mProgram); 135 | mProgram = 0; 136 | return 0; 137 | } 138 | } 139 | else 140 | Log.d("CreateProgram", "Could not create program"); 141 | 142 | return 1; 143 | } 144 | 145 | /** 146 | * Loads a shader (either vertex or pixel) given the source 147 | * @param shaderType VERTEX or PIXEL 148 | * @param source The string data representing the shader code 149 | * @return handle for shader 150 | */ 151 | private int loadShader(int shaderType, String source) { 152 | int shader = GLES20.glCreateShader(shaderType); 153 | if (shader != 0) { 154 | GLES20.glShaderSource(shader, source); 155 | GLES20.glCompileShader(shader); 156 | int[] compiled = new int[1]; 157 | GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); 158 | if (compiled[0] == 0) { 159 | Log.e(TAG, "Could not compile shader " + shaderType + ":"); 160 | Log.e(TAG, GLES20.glGetShaderInfoLog(shader)); 161 | GLES20.glDeleteShader(shader); 162 | shader = 0; 163 | } 164 | } 165 | return shader; 166 | } 167 | 168 | /*************************** 169 | * GET/SET 170 | *************************/ 171 | public int getProgram() { 172 | return mProgram; 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/skybox/SkyBoxDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.skybox; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | import android.view.MotionEvent; 7 | import android.view.View; 8 | import android.view.View.OnTouchListener; 9 | 10 | import com.example.ViewThouchListener; 11 | 12 | public class SkyBoxDemoActivity extends Activity { 13 | private GLSurfaceView view; 14 | private SkyBoxRender render; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | 20 | initView(); 21 | setContentView(view); 22 | } 23 | 24 | private void initView() { 25 | view = new GLSurfaceView(this); 26 | 27 | view.setEGLContextClientVersion(2); 28 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 29 | render = new SkyBoxRender(this); 30 | view.setRenderer(render); 31 | 32 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 33 | view.setOnTouchListener(touchListener); 34 | } 35 | 36 | @Override 37 | protected void onPause() { 38 | super.onPause(); 39 | view.onPause(); 40 | } 41 | 42 | @Override 43 | protected void onResume() { 44 | super.onResume(); 45 | view.onResume(); 46 | } 47 | 48 | @Override 49 | protected void onDestroy() { 50 | super.onDestroy(); 51 | render.release(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/sobelOperator/SobelDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.sobelOperator; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | 7 | import com.example.ViewThouchListener; 8 | 9 | public class SobelDemoActivity extends Activity { 10 | private GLSurfaceView view; 11 | private SobelRender render; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | 17 | initView(); 18 | setContentView(view); 19 | } 20 | 21 | private void initView() { 22 | view = new GLSurfaceView(this); 23 | 24 | view.setEGLContextClientVersion(2); 25 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 26 | render = new SobelRender(this); 27 | view.setRenderer(render); 28 | 29 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 30 | view.setOnTouchListener(touchListener); 31 | } 32 | 33 | @Override 34 | protected void onPause() { 35 | super.onPause(); 36 | view.onPause(); 37 | } 38 | 39 | @Override 40 | protected void onResume() { 41 | super.onResume(); 42 | view.onResume(); 43 | } 44 | 45 | @Override 46 | protected void onDestroy() { 47 | super.onDestroy(); 48 | render.destroy(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/twirl/TwirlDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.twirl; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | 7 | import com.example.ViewThouchListener; 8 | 9 | public class TwirlDemoActivity extends Activity { 10 | private GLSurfaceView view; 11 | private TwirlRender render; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | 17 | initView(); 18 | setContentView(view); 19 | } 20 | 21 | private void initView() { 22 | view = new GLSurfaceView(this); 23 | 24 | view.setEGLContextClientVersion(2); 25 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 26 | render = new TwirlRender(this); 27 | render.setDrawFbo(true); 28 | view.setRenderer(render); 29 | 30 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 31 | view.setOnTouchListener(touchListener); 32 | } 33 | 34 | @Override 35 | protected void onPause() { 36 | super.onPause(); 37 | view.onPause(); 38 | } 39 | 40 | @Override 41 | protected void onResume() { 42 | super.onResume(); 43 | view.onResume(); 44 | } 45 | 46 | @Override 47 | protected void onDestroy() { 48 | super.onDestroy(); 49 | render.destroy(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/twirl/TwirlShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.twirl; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLES20; 5 | 6 | import com.example.shaderUtil.BasicShaderProgram; 7 | 8 | /** 9 | * Created by L on 2017/10/6. 10 | */ 11 | 12 | public class TwirlShaderProgram extends BasicShaderProgram { 13 | protected final int uAngleLocation; 14 | 15 | public TwirlShaderProgram(Context context, int vertexShaderResourceId, int fragmentShaderResourceId) { 16 | super(context, vertexShaderResourceId, fragmentShaderResourceId); 17 | uAngleLocation = GLES20.glGetUniformLocation(program, "angle"); 18 | } 19 | 20 | public void setAngle(float angle){ 21 | GLES20.glUniform1f(uAngleLocation, angle); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/wobble/WobbleDemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.wobble; 2 | 3 | import android.app.Activity; 4 | import android.opengl.GLSurfaceView; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.view.MotionEvent; 8 | import android.view.View; 9 | import android.view.View.OnTouchListener; 10 | 11 | import com.example.ViewThouchListener; 12 | 13 | public class WobbleDemoActivity extends Activity { 14 | private GLSurfaceView view; 15 | private WobbleRender render; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | 21 | initView(); 22 | setContentView(view); 23 | } 24 | 25 | private void initView() { 26 | view = new GLSurfaceView(this); 27 | 28 | view.setEGLContextClientVersion(2); 29 | view.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 30 | render = new WobbleRender(this); 31 | view.setRenderer(render); 32 | 33 | ViewThouchListener touchListener = new ViewThouchListener(view, render); 34 | view.setOnTouchListener(touchListener); 35 | } 36 | 37 | @Override 38 | protected void onPause() { 39 | super.onPause(); 40 | view.onPause(); 41 | } 42 | 43 | @Override 44 | protected void onResume() { 45 | super.onResume(); 46 | view.onResume(); 47 | } 48 | 49 | @Override 50 | protected void onDestroy() { 51 | super.onDestroy(); 52 | render.release(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/wobble/WobbleRender.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.wobble; 2 | 3 | import javax.microedition.khronos.egl.EGLConfig; 4 | import javax.microedition.khronos.opengles.GL10; 5 | 6 | import com.example.BasicRender; 7 | import com.example.opengldemo.R; 8 | import com.example.shaderUtil.MatrixHelper; 9 | import com.example.shaderUtil.ObjLoader; 10 | import com.example.shaderUtil.TextureHelper; 11 | import com.example.shaderUtil.TextureShaderProgram; 12 | import com.example.object.Vertices3; 13 | 14 | import android.annotation.SuppressLint; 15 | import android.app.Activity; 16 | import android.graphics.Bitmap; 17 | import android.opengl.GLES20; 18 | import android.opengl.GLSurfaceView.Renderer; 19 | import android.opengl.Matrix; 20 | 21 | @SuppressLint("NewApi") 22 | public class WobbleRender extends BasicRender { 23 | private Activity activity; 24 | private int meshTexture; 25 | 26 | private float[] projectionMatrix = new float[16]; 27 | private float[] viewMatrix = new float[16]; 28 | private final float[] modelMatrix = new float[16]; 29 | private float[] viewProjectMatrix = new float[16]; 30 | 31 | private WobbleShaderProgram textureShader; 32 | private Vertices3 mesh; 33 | private float xAngle = 0; 34 | private float yAngle = 0; 35 | 36 | Bitmap bitmap[] = new Bitmap[5]; 37 | 38 | public WobbleRender(Activity activity) { 39 | this.activity = activity; 40 | } 41 | 42 | @Override 43 | public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) { 44 | GLES20.glClearColor(1F, 1F, 1F, 0F); 45 | 46 | GLES20.glEnable(GLES20.GL_DEPTH_TEST); 47 | // GLES20.glEnable(GLES20.GL_CULL_FACE); 48 | 49 | meshTexture = TextureHelper.loadTexture(activity, R.drawable.face); 50 | 51 | textureShader = new WobbleShaderProgram(activity, 52 | R.raw.wobble_vertex_shader, R.raw.rotate_fragment_shader); 53 | mesh = ObjLoader.load(activity, R.raw.monkey); 54 | } 55 | 56 | @Override 57 | public void onSurfaceChanged(GL10 arg0, int width, int height) { 58 | 59 | GLES20.glViewport(0, 0, width, height); 60 | 61 | float fov = 120; 62 | float aspect = ((float) width / (float) height); 63 | float near = 0.1f; 64 | float far = 200f; 65 | MatrixHelper.perspectiveM(projectionMatrix, fov, aspect, near, far); 66 | 67 | Matrix.setLookAtM(viewMatrix, 0, 68 | 0f, 0f, 1f, 69 | 0f, 0.5f, -1f, 70 | 0f, 1f, 0f); 71 | 72 | Matrix.translateM(viewMatrix, 0, 0, 0, -2); 73 | } 74 | 75 | @Override 76 | public void onDrawFrame(GL10 arg0) { 77 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 78 | 79 | drawWobble(); 80 | } 81 | 82 | private void drawWobble(){ 83 | float[] matrix = getWobbleMatrix(); 84 | textureShader.useProgram(); 85 | textureShader.setUniform(matrix, meshTexture); 86 | 87 | mesh.bind(textureShader.getPositionLocation(), 88 | textureShader.getTextureCoordinatesLocation()); 89 | mesh.draw(); 90 | } 91 | 92 | private float[] getWobbleMatrix() { 93 | Matrix.setIdentityM(modelMatrix, 0); 94 | 95 | Matrix.translateM(modelMatrix, 0, 0f, -2f, -5); 96 | Matrix.rotateM(modelMatrix, 0, xAngle, 1f, 0f, 0f); 97 | Matrix.rotateM(modelMatrix, 0, yAngle, 0f, 1f, 0f); 98 | float[] temp = new float[16]; 99 | float[] resultMatrix = new float[16]; 100 | Matrix.multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0); 101 | System.arraycopy(temp, 0, resultMatrix, 0, temp.length); 102 | // xAngle += 0.5; 103 | 104 | return resultMatrix; 105 | } 106 | 107 | public void rotate(float xAngle, float yAngle) { 108 | this.xAngle += xAngle; 109 | this.yAngle += yAngle; 110 | } 111 | 112 | public void release(){ 113 | int[] textureArray = {meshTexture}; 114 | GLES20.glDeleteTextures(1, textureArray, 0); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/opengldemo/wobble/WobbleShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.example.opengldemo.wobble; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLES20; 5 | 6 | import com.example.shaderUtil.BasicShaderProgram; 7 | 8 | /** 9 | * Created by L on 2017/8/7. 10 | */ 11 | 12 | public class WobbleShaderProgram extends BasicShaderProgram { 13 | private final int uTimeLocation; 14 | private float time; 15 | 16 | public WobbleShaderProgram(Context context, int vertexShaderResourceId, int fragmentShaderResourceId) { 17 | super(context, vertexShaderResourceId, fragmentShaderResourceId); 18 | uTimeLocation = GLES20.glGetUniformLocation(program, "time"); 19 | } 20 | 21 | @Override 22 | public void setUniform(float[] matrix, int textureId) { 23 | super.setUniform(matrix, textureId); 24 | 25 | time += 0.05f; 26 | GLES20.glUniform1f(uTimeLocation, time); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/BasicShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.example.shaderUtil; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLES20; 5 | 6 | /** 7 | * Created by L on 2017/8/7. 8 | */ 9 | 10 | public class BasicShaderProgram extends ShaderProgram { 11 | protected final int uMatrixLocation; 12 | protected final int uTextureUnitLocation; 13 | 14 | protected final int aPositionLocation; 15 | protected final int aTextureCoordinatesLocation; 16 | 17 | public BasicShaderProgram(Context context, int vertexShaderResourceId, int fragmentShaderResourceId) { 18 | super(context, vertexShaderResourceId, fragmentShaderResourceId); 19 | 20 | uMatrixLocation = GLES20.glGetUniformLocation(program, U_MATRIX); 21 | uTextureUnitLocation = GLES20.glGetUniformLocation(program, U_TEXTURE_UNIT); 22 | 23 | aPositionLocation = GLES20.glGetAttribLocation(program, A_POSITION); 24 | aTextureCoordinatesLocation = GLES20.glGetAttribLocation(program, A_TEXTURE_COORDINATES); 25 | } 26 | 27 | public void setUniform(float[] matrix, int textureId){ 28 | GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0); 29 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 30 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId); 31 | GLES20.glUniform1i(uTextureUnitLocation, 0); 32 | } 33 | 34 | public int getPositionLocation() { 35 | return aPositionLocation; 36 | } 37 | 38 | public int getTextureCoordinatesLocation() { 39 | return aTextureCoordinatesLocation; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/Constants.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "OpenGL ES for Android", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information. 8 | ***/ 9 | package com.example.shaderUtil; 10 | 11 | public class Constants { 12 | public static final int BYTES_PER_FLOAT = 4; 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/IndexBuffer.java: -------------------------------------------------------------------------------- 1 | package com.example.shaderUtil; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.nio.ByteOrder; 5 | import java.nio.ShortBuffer; 6 | 7 | import android.annotation.SuppressLint; 8 | import android.opengl.GLES20; 9 | 10 | public class IndexBuffer { 11 | 12 | private int bufferID; 13 | 14 | public IndexBuffer(short indexData[]){ 15 | int buffers[] = new int[1]; 16 | GLES20.glGenBuffers(buffers.length, buffers, 0); 17 | 18 | this.bufferID = buffers[0]; 19 | GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, bufferID); 20 | int bytesPerShort = 2; 21 | ShortBuffer vertexArray = ByteBuffer.allocateDirect(indexData.length * bytesPerShort).order(ByteOrder.nativeOrder()).asShortBuffer().put(indexData); 22 | vertexArray.position(0); 23 | GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, vertexArray.capacity() * bytesPerShort, vertexArray, GLES20.GL_STATIC_DRAW); 24 | GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); 25 | } 26 | 27 | @SuppressLint("NewApi") 28 | public void setVertexAttribPosition(int dataOffset, int attributeLocation, int componentCount, int stride){ 29 | GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, bufferID); 30 | GLES20.glVertexAttribPointer(attributeLocation, componentCount, GLES20.GL_FLOAT, false, stride, dataOffset); 31 | GLES20.glEnableVertexAttribArray(attributeLocation); 32 | GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); 33 | } 34 | 35 | public int getBufferID() { 36 | return bufferID; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/MatrixHelper.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "OpenGL ES for Android", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information. 8 | ***/ 9 | package com.example.shaderUtil; 10 | 11 | public class MatrixHelper { 12 | public static void perspectiveM(float[] m, float yFovInDegrees, float aspect, 13 | float n, float f) { 14 | final float angleInRadians = (float) (yFovInDegrees * Math.PI / 180.0); 15 | 16 | final float a = (float) (1.0 / Math.tan(angleInRadians / 2.0)); 17 | m[0] = a / aspect; 18 | m[1] = 0f; 19 | m[2] = 0f; 20 | m[3] = 0f; 21 | 22 | m[4] = 0f; 23 | m[5] = a; 24 | m[6] = 0f; 25 | m[7] = 0f; 26 | 27 | m[8] = 0f; 28 | m[9] = 0f; 29 | m[10] = -((f + n) / (f - n)); 30 | m[11] = -1f; 31 | 32 | m[12] = 0f; 33 | m[13] = 0f; 34 | m[14] = -((2f * f * n) / (f - n)); 35 | m[15] = 0f; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/RayPickupUtil.java: -------------------------------------------------------------------------------- 1 | package com.example.shaderUtil; 2 | 3 | import android.opengl.Matrix; 4 | 5 | import com.example.object.Geometry; 6 | 7 | public class RayPickupUtil { 8 | 9 | public static Geometry.Ray generateRay(float normalizedX, float normalizedY, float[] invertedViewProjectMatrix){ 10 | float[] nearPointNdc = {normalizedX, normalizedY, -1, 1}; 11 | float[] farPointNdc = {normalizedX, normalizedY, 1, 1}; 12 | 13 | float[] nearPointWorld = new float[4]; 14 | float[] farPointWorld = new float[4]; 15 | 16 | Matrix.multiplyMV(nearPointWorld, 0, invertedViewProjectMatrix, 0, nearPointNdc, 0); 17 | Matrix.multiplyMV(farPointWorld, 0, invertedViewProjectMatrix, 0, farPointNdc, 0); 18 | 19 | divideByW(nearPointWorld); 20 | divideByW(farPointWorld); 21 | 22 | Geometry.Point nearPointRay = new Geometry.Point(nearPointWorld[0], nearPointWorld[1], nearPointWorld[2]); 23 | Geometry.Point farPointRay = new Geometry.Point(farPointWorld[0], farPointWorld[1], farPointWorld[2]); 24 | 25 | Geometry.Ray ray = new Geometry.Ray(nearPointRay, Geometry.vectorBetween(nearPointRay, farPointRay)); 26 | 27 | return ray; 28 | } 29 | 30 | private static void divideByW(float[] vector){ 31 | vector[0] /= vector[3]; 32 | vector[1] /= vector[3]; 33 | vector[2] /= vector[3]; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/ShaderHelper.java: -------------------------------------------------------------------------------- 1 | package com.example.shaderUtil; 2 | 3 | import android.opengl.GLES20; 4 | import android.util.Log; 5 | 6 | public class ShaderHelper { 7 | public static int compileVertexShader(String shaderCode){ 8 | int shaderID = compileShader(GLES20.GL_VERTEX_SHADER, shaderCode); 9 | return shaderID; 10 | } 11 | 12 | public static int compileFragmentShader(String shaderCode){ 13 | int shaderID = compileShader(GLES20.GL_FRAGMENT_SHADER, shaderCode); 14 | return shaderID; 15 | } 16 | 17 | public static int compileShader(int type, String shaderCode){ 18 | int shaderID = GLES20.glCreateShader(type); 19 | if(shaderID == 0){ 20 | return 0; 21 | } 22 | 23 | GLES20.glShaderSource(shaderID, shaderCode); 24 | GLES20.glCompileShader(shaderID); 25 | 26 | int[] compileStatus = new int[1]; 27 | GLES20.glGetShaderiv(shaderID, GLES20.GL_COMPILE_STATUS, compileStatus, 0); 28 | 29 | String shaderInfo = GLES20.glGetShaderInfoLog(shaderID); 30 | 31 | 32 | if(compileStatus[0] == 0){ 33 | GLES20.glDeleteShader(shaderID); 34 | return 0; 35 | } 36 | 37 | 38 | 39 | return shaderID; 40 | } 41 | 42 | public static int linkProgram(int vertexShaderID, int fragmentShaderID){ 43 | int programID = GLES20.glCreateProgram(); 44 | if(programID == 0){ 45 | return 0; 46 | } 47 | 48 | GLES20.glAttachShader(programID, vertexShaderID); 49 | GLES20.glAttachShader(programID, fragmentShaderID); 50 | 51 | GLES20.glLinkProgram(programID); 52 | int[] linkStatus = new int[1]; 53 | GLES20.glGetProgramiv(programID, GLES20.GL_LINK_STATUS, linkStatus, 0); 54 | if(linkStatus[0] == 0){ 55 | GLES20.glDeleteProgram(programID); 56 | return 0; 57 | } 58 | 59 | return programID; 60 | } 61 | 62 | public static boolean validateProgram(int program){ 63 | GLES20.glValidateProgram(program); 64 | int[] validateStatus = new int[1]; 65 | GLES20.glGetProgramiv(program, GLES20.GL_VALIDATE_STATUS, validateStatus, 0); 66 | 67 | return (validateStatus[0] != 0); 68 | } 69 | 70 | public static int buildProgram(String vertexShaderSource, 71 | String fragmentShaderSource) { 72 | int program; 73 | 74 | int vertexShader = compileVertexShader(vertexShaderSource); 75 | int fragmentShader = compileFragmentShader(fragmentShaderSource); 76 | 77 | program = linkProgram(vertexShader, fragmentShader); 78 | 79 | 80 | return program; 81 | } 82 | 83 | public static void useProgram(int program){ 84 | GLES20.glUseProgram(program); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/ShaderProgram.java: -------------------------------------------------------------------------------- 1 | package com.example.shaderUtil; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLES20; 5 | 6 | abstract class ShaderProgram { 7 | protected static final String U_MATRIX = "u_Matrix"; 8 | protected static final String U_TEXTURE_UNIT = "u_TextureUnit"; 9 | protected static final String U_POINT_LIGHT = "u_light_point"; 10 | protected static final String U_MVMATRIX = "u_MVMatrix"; 11 | protected static final String U_IT_MVMATRIX = "u_IT_MVMATRIX"; 12 | 13 | protected static final String A_POSITION = "a_Position"; 14 | protected static final String A_COLOR = "u_Color";//"a_Color"; 15 | protected static final String A_TEXTURE_COORDINATES = "a_TextureCoordinates"; 16 | protected static final String A_NORMAL_COORDINATES = "a_normalCoordinates"; // 17 | 18 | protected final int program; 19 | protected ShaderProgram(Context context, int vertexShaderResourceId, 20 | int fragmentShaderResourceId) { 21 | program = ShaderHelper.buildProgram( 22 | TextResourceReader.readTextFileFromResource( 23 | context, vertexShaderResourceId), 24 | TextResourceReader.readTextFileFromResource( 25 | context, fragmentShaderResourceId)); 26 | } 27 | 28 | public void useProgram() { 29 | GLES20.glUseProgram(program); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/SkyboxShaderProgram.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "OpenGL ES for Android", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information. 8 | ***/ 9 | package com.example.shaderUtil; 10 | 11 | import static android.opengl.GLES20.GL_TEXTURE0; 12 | import static android.opengl.GLES20.GL_TEXTURE_CUBE_MAP; 13 | import static android.opengl.GLES20.glActiveTexture; 14 | import static android.opengl.GLES20.glBindTexture; 15 | import static android.opengl.GLES20.glGetAttribLocation; 16 | import static android.opengl.GLES20.glGetUniformLocation; 17 | import static android.opengl.GLES20.glUniform1i; 18 | import static android.opengl.GLES20.glUniformMatrix4fv; 19 | import android.content.Context; 20 | 21 | import com.example.opengldemo.R; 22 | 23 | public class SkyboxShaderProgram extends ShaderProgram { 24 | private final int uMatrixLocation; 25 | private final int uTextureUnitLocation; 26 | private final int aPositionLocation; 27 | 28 | public SkyboxShaderProgram(Context context) { 29 | super(context, R.raw.skybox_vertex_shader, 30 | R.raw.skybox_fragment_shader); 31 | 32 | uMatrixLocation = glGetUniformLocation(program, U_MATRIX); 33 | uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT); 34 | aPositionLocation = glGetAttribLocation(program, A_POSITION); 35 | } 36 | 37 | public void setUniforms(float[] matrix, int textureId) { 38 | glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0); 39 | 40 | glActiveTexture(GL_TEXTURE0); 41 | glBindTexture(GL_TEXTURE_CUBE_MAP, textureId); 42 | glUniform1i(uTextureUnitLocation, 0); 43 | } 44 | 45 | public int getPositionAttributeLocation() { 46 | return aPositionLocation; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/TextResourceReader.java: -------------------------------------------------------------------------------- 1 | package com.example.shaderUtil; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | 8 | import android.content.Context; 9 | 10 | public class TextResourceReader { 11 | public static String readTextFileFromResource(Context context, int resourceID){ 12 | StringBuilder body = new StringBuilder(); 13 | 14 | try { 15 | InputStream inputStream = context.getResources().openRawResource(resourceID); 16 | InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 17 | BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 18 | String nextLine = null; 19 | 20 | while( (nextLine = bufferedReader.readLine()) != null ){ 21 | body.append(nextLine); 22 | body.append("\n"); 23 | } 24 | } catch (IOException e) { 25 | // TODO Auto-generated catch block 26 | e.printStackTrace(); 27 | } 28 | return body.toString(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/shaderUtil/VertexArray.java: -------------------------------------------------------------------------------- 1 | /*** 2 | * Excerpted from "OpenGL ES for Android", 3 | * published by The Pragmatic Bookshelf. 4 | * Copyrights apply to this code. It may not be used to create training material, 5 | * courses, books, articles, and the like. Contact us if you are in doubt. 6 | * We make no guarantees that this code is fit for any purpose. 7 | * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information. 8 | ***/ 9 | package com.example.shaderUtil; 10 | 11 | import static android.opengl.GLES20.GL_FLOAT; 12 | import static android.opengl.GLES20.glEnableVertexAttribArray; 13 | import static android.opengl.GLES20.glVertexAttribPointer; 14 | 15 | import java.nio.ByteBuffer; 16 | import java.nio.ByteOrder; 17 | import java.nio.FloatBuffer; 18 | 19 | public class VertexArray { 20 | private final FloatBuffer floatBuffer; 21 | 22 | public VertexArray(float[] vertexData) { 23 | floatBuffer = ByteBuffer 24 | .allocateDirect(vertexData.length * Constants.BYTES_PER_FLOAT) 25 | .order(ByteOrder.nativeOrder()) 26 | .asFloatBuffer() 27 | .put(vertexData); 28 | } 29 | 30 | public void setVertexAttribPointer(int dataOffset, int attributeLocation, 31 | int componentCount, int stride) { 32 | floatBuffer.position(dataOffset); 33 | glVertexAttribPointer(attributeLocation, componentCount, GL_FLOAT, 34 | false, stride, floatBuffer); 35 | glEnableVertexAttribArray(attributeLocation); 36 | 37 | floatBuffer.position(0); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/air_hockey_surface.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/air_hockey_surface.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/back.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/bottom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/bottom.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/cat.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/crate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/crate.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/face.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/face.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/front.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/front.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/left.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/right.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/top.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable-xxhdpi/top.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/bricks.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/bricks.JPG -------------------------------------------------------------------------------- /app/src/main/res/drawable/bricks_norm.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/bricks_norm.JPG -------------------------------------------------------------------------------- /app/src/main/res/drawable/metal.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/metal.JPG -------------------------------------------------------------------------------- /app/src/main/res/drawable/metal_norm.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/metal_norm.JPG -------------------------------------------------------------------------------- /app/src/main/res/drawable/normal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/normal.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/normal2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/normal2.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/normal3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/normal3.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/normal4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/normal4.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/normal_empty.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/normal_empty.JPG -------------------------------------------------------------------------------- /app/src/main/res/drawable/rocks.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/rocks.JPG -------------------------------------------------------------------------------- /app/src/main/res/drawable/rocks_norm.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/rocks_norm.JPG -------------------------------------------------------------------------------- /app/src/main/res/drawable/stone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/stone.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/stone_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/stone_normal.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/texture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/texture.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/white.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/drawable/white.JPG -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main2.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/layout/egl_test_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/raw/anti_aliasing_fragment.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | 4 | varying vec2 v_TextureCoordinates; 5 | uniform sampler2D u_TextureUnit; 6 | vec3 lum = vec3(0.2126, 0.7152, 0.0722); 7 | float p00,p10,p20,p01,p21,p02,p12,p22,x,y,px,py,distance; 8 | vec2 FBS = vec2(256.0, 256.0); 9 | 10 | float fxaaLuma(vec3 rgb){ 11 | return rgb.y * (0.587/0.299) + rgb.x; 12 | } 13 | 14 | void main() { 15 | float FXAA_SPANA_MAX = 8.0; 16 | float FXAA_REDUCE_MUL = 1.0/8.0; 17 | float FXAA_REDUCE_MIN = 1.0/128.0; 18 | 19 | vec3 rgbNW = texture2D(u_TextureUnit, v_TextureCoordinates + (vec2(-1.0, -1.0)/FBS)).xyz; 20 | vec3 rgbNE = texture2D(u_TextureUnit, v_TextureCoordinates + (vec2(1.0, -1.0)/FBS)).xyz; 21 | vec3 rgbSW = texture2D(u_TextureUnit, v_TextureCoordinates + (vec2(-1.0, 1.0)/FBS)).xyz; 22 | vec3 rgbSE = texture2D(u_TextureUnit, v_TextureCoordinates + (vec2(1.0, 1.0)/FBS)).xyz; 23 | vec3 rgbM = texture2D(u_TextureUnit, v_TextureCoordinates).xyz; 24 | 25 | float lumaNW = fxaaLuma(rgbNW); 26 | float lumaNE = fxaaLuma(rgbNE); 27 | float lumaSW = fxaaLuma(rgbSW); 28 | float lumaSE = fxaaLuma(rgbSE); 29 | float lumaM = fxaaLuma(rgbM); 30 | 31 | vec2 dir; 32 | dir.x = ((lumaNW + lumaNE) - (lumaSW + lumaSE)); 33 | dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); 34 | 35 | float dirReduce = max((lumaNW + lumaNE + lumaSE + lumaSW) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); 36 | float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); 37 | dir = min(vec2(FXAA_SPANA_MAX, FXAA_SPANA_MAX), max(vec2(-FXAA_SPANA_MAX, -FXAA_SPANA_MAX), dir * rcpDirMin)) / FBS; 38 | 39 | vec3 rgbA = 0.5 * (texture2D(u_TextureUnit, v_TextureCoordinates.xy + dir * (1.0 / 3.0 - 0.5)).xyz 40 | + texture2D(u_TextureUnit, v_TextureCoordinates.xy + dir * (2.0 / 3.0 - 0.5)).xyz); 41 | 42 | vec3 rgbB = rgbA * 0.5 + (1.0 / 4.0) * (texture2D(u_TextureUnit, v_TextureCoordinates.xy + dir * (0.0 / 3.0 - 0.5)).xyz 43 | + texture2D(u_TextureUnit, v_TextureCoordinates.xy + dir * (3.0 / 3.0 - 0.5)).xyz); 44 | 45 | float lumaB = fxaaLuma(rgbB); 46 | float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); 47 | float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); 48 | if((lumaB < lumaM) || (lumaB > lumaMax)){ 49 | gl_FragColor = vec4(rgbA, 1.0); 50 | }else { 51 | gl_FragColor = vec4(rgbB, 1.0); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/res/raw/bumpmapping_fragment.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | varying vec2 v_TextureCoordinates; 4 | uniform sampler2D u_TextureUnit; 5 | varying vec4 objectCoord; 6 | varying vec3 v_normalCoordinates; 7 | uniform vec4 u_light_point; 8 | uniform float textureMove; 9 | 10 | vec4 LightColor=vec4(0.0, 1.0, 1.0 , 1.0); 11 | uniform mat4 u_MVMatrix; 12 | uniform mat4 u_IT_MVMATRIX; 13 | 14 | void main() 15 | { 16 | vec4 eyeSpacePosition = u_MVMatrix * objectCoord; //vec4(objectCoord, 1); 17 | vec3 eyeSpaceNormal=vec3(u_IT_MVMATRIX * vec4(v_normalCoordinates, 0.0)); // -0.5, 0.4, 0.2 18 | 19 | eyeSpaceNormal = normalize(eyeSpaceNormal); 20 | 21 | vec3 toPointLight = vec3(u_light_point) - vec3(eyeSpacePosition); 22 | float distance = length(vec3(toPointLight))/4.0; 23 | toPointLight = normalize(toPointLight); 24 | float diffcuse = max(0.0, dot(eyeSpaceNormal, toPointLight)); 25 | vec4 TextureWood = texture2D(u_TextureUnit, v_TextureCoordinates+textureMove); 26 | 27 | gl_FragColor = (TextureWood * LightColor * diffcuse); 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/res/raw/bumpmapping_vertex.glsl: -------------------------------------------------------------------------------- 1 | attribute vec4 a_Position; 2 | attribute vec2 a_TextureCoordinates; 3 | attribute vec3 a_normalCoordinates; 4 | 5 | uniform mat4 u_Matrix; 6 | varying vec2 v_TextureCoordinates; 7 | varying vec4 objectCoord; 8 | varying vec3 v_normalCoordinates; 9 | 10 | void main() 11 | { 12 | v_TextureCoordinates = a_TextureCoordinates; 13 | objectCoord = a_Position; 14 | vec4 VertexCoord = a_Position; 15 | 16 | v_normalCoordinates = a_normalCoordinates; 17 | 18 | float a = 1080.0; 19 | float b = 300.0; 20 | float x = objectCoord.x; 21 | float y = objectCoord.y; 22 | float z = objectCoord.z; 23 | v_normalCoordinates.x = v_normalCoordinates.x + a*sin(b*x); 24 | v_normalCoordinates.y = v_normalCoordinates.y + a*sin(b*y); 25 | v_normalCoordinates.z = v_normalCoordinates.z + a*sin(b*z); 26 | 27 | gl_Position = u_Matrix * VertexCoord; 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/res/raw/cube.obj: -------------------------------------------------------------------------------- 1 | # Exported from Wings 3D 1.2 2 | mtllib cube.mtl 3 | o cube1 4 | #8 vertices, 12 faces 5 | v -0.50000000 -0.50000000 0.50000000 6 | v -0.50000000 0.50000000 0.50000000 7 | v 0.50000000 0.50000000 0.50000000 8 | v 0.50000000 -0.50000000 0.50000000 9 | v -0.50000000 -0.50000000 -0.50000000 10 | v -0.50000000 0.50000000 -0.50000000 11 | v 0.50000000 0.50000000 -0.50000000 12 | v 0.50000000 -0.50000000 -0.50000000 13 | vt 0.0000000e+0 1.1102230e-16 14 | vt 0.0000000e+0 2.2204460e-16 15 | vt 0.0000000e+0 1.00000000 16 | vt 2.2204460e-16 2.2204460e-16 17 | vt 2.2204460e-16 4.4408921e-16 18 | vt 2.2204460e-16 1.00000000 19 | vt 4.4408921e-16 0.0000000e+0 20 | vt 4.4408921e-16 2.2204460e-16 21 | vt 4.4408921e-16 1.00000000 22 | vt 1.00000000 0.0000000e+0 23 | vt 1.00000000 1.1102230e-16 24 | vt 1.00000000 2.2204460e-16 25 | vt 1.00000000 4.4408921e-16 26 | vt 1.00000000 1.00000000 27 | vn 0.0000000e+0 -1.00000000 0.0000000e+0 28 | vn 0.0000000e+0 0.0000000e+0 1.00000000 29 | vn -1.00000000 0.0000000e+0 0.0000000e+0 30 | vn 0.0000000e+0 1.00000000 0.0000000e+0 31 | vn 0.0000000e+0 0.0000000e+0 1.00000000 32 | vn 0.0000000e+0 1.00000000 0.0000000e+0 33 | vn 0.0000000e+0 0.0000000e+0 1.00000000 34 | vn -1.00000000 0.0000000e+0 0.0000000e+0 35 | vn -1.00000000 0.0000000e+0 0.0000000e+0 36 | vn 1.00000000 0.0000000e+0 0.0000000e+0 37 | vn 0.0000000e+0 1.00000000 0.0000000e+0 38 | vn 0.0000000e+0 0.0000000e+0 1.00000000 39 | vn 0.0000000e+0 -1.00000000 0.0000000e+0 40 | vn 1.00000000 0.0000000e+0 0.0000000e+0 41 | vn 0.0000000e+0 0.0000000e+0 1.00000000 42 | vn 0.0000000e+0 -1.00000000 0.0000000e+0 43 | vn 0.0000000e+0 0.0000000e+0 1.00000000 44 | vn 1.00000000 0.0000000e+0 0.0000000e+0 45 | vn 0.0000000e+0 -1.00000000 0.0000000e+0 46 | vn 0.0000000e+0 0.0000000e+0 -1.00000000 47 | vn 0.0000000e+0 -1.00000000 0.0000000e+0 48 | vn 0.0000000e+0 0.0000000e+0 -1.00000000 49 | vn -1.00000000 0.0000000e+0 0.0000000e+0 50 | vn -1.00000000 0.0000000e+0 0.0000000e+0 51 | vn 0.0000000e+0 1.00000000 0.0000000e+0 52 | vn 0.0000000e+0 0.0000000e+0 -1.00000000 53 | vn -1.00000000 0.0000000e+0 0.0000000e+0 54 | vn 0.0000000e+0 1.00000000 0.0000000e+0 55 | vn 1.00000000 0.0000000e+0 0.0000000e+0 56 | vn 0.0000000e+0 0.0000000e+0 -1.00000000 57 | vn 0.0000000e+0 1.00000000 0.0000000e+0 58 | vn 0.0000000e+0 0.0000000e+0 -1.00000000 59 | vn 1.00000000 0.0000000e+0 0.0000000e+0 60 | vn 0.0000000e+0 0.0000000e+0 -1.00000000 61 | vn 0.0000000e+0 -1.00000000 0.0000000e+0 62 | vn 1.00000000 0.0000000e+0 0.0000000e+0 63 | g cube1_cube1_auv 64 | usemtl cube1_auv 65 | s 1 66 | f 1/8/1 5/9/19 4/12/13 67 | f 3/5/11 7/6/31 2/12/6 68 | f 4/11/18 8/2/36 7/3/33 69 | f 6/9/26 7/14/32 5/7/22 70 | s 2 71 | f 2/14/7 4/4/17 3/6/12 72 | f 2/3/8 5/11/23 1/1/3 73 | f 2/12/4 7/6/28 6/14/25 74 | f 5/9/21 8/14/35 4/12/16 75 | s 3 76 | f 1/13/2 4/4/15 2/14/5 77 | f 2/3/9 6/14/27 5/11/24 78 | f 4/11/14 7/3/29 3/14/10 79 | f 7/14/30 8/10/34 5/7/20 80 | -------------------------------------------------------------------------------- /app/src/main/res/raw/depth_tex_f_depth_map.glsl: -------------------------------------------------------------------------------- 1 | // Pixel shader to generate the Depth Map 2 | // Used for shadow mapping - generates depth map from the light's viewpoint 3 | precision highp float; 4 | 5 | void main() { 6 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/depth_tex_f_with_simple_shadow.glsl: -------------------------------------------------------------------------------- 1 | // Based on http://blog.shayanjaved.com/2011/03/13/shaders-android/ 2 | // from Shayan Javed 3 | // And dEngine source from Fabien Sanglard 4 | 5 | //precision highp float; 6 | precision mediump float; 7 | 8 | // The position of the light in eye space. 9 | uniform vec3 uLightPos; 10 | 11 | // Texture variables: depth texture 12 | uniform sampler2D uShadowTexture; 13 | 14 | // This define the value to move one pixel left or right 15 | uniform float uxPixelOffset; 16 | // This define the value to move one pixel up or down 17 | //uniform float uyPixelOffset; 18 | 19 | // from vertex shader - values get interpolated 20 | varying vec3 vPosition; 21 | varying vec4 vColor; 22 | varying vec3 vNormal; 23 | 24 | // shadow coordinates 25 | varying vec4 vShadowCoord; 26 | 27 | //Simple shadow mapping 28 | float shadowSimple() 29 | { 30 | vec4 shadowMapPosition = vShadowCoord / vShadowCoord.w; 31 | 32 | float distanceFromLight = texture2D(uShadowTexture, shadowMapPosition.xy).z; 33 | 34 | //add bias to reduce shadow acne (error margin) 35 | float bias = 0.0005; 36 | 37 | //1.0 = not in shadow (fragmant is closer to light than the value stored in shadow map) 38 | //0.0 = in shadow 39 | return float(distanceFromLight > shadowMapPosition.z - bias); 40 | } 41 | 42 | void main() 43 | { 44 | vec3 lightVec = uLightPos - vPosition; 45 | lightVec = normalize(lightVec); 46 | 47 | // Phong shading with diffuse and ambient component 48 | float diffuseComponent = max(0.0,dot(lightVec, vNormal) ); 49 | float ambientComponent = 0.3; 50 | 51 | // Shadow 52 | float shadow = 1.0; 53 | //此处用于对深度进行比较,然后查找出距离光影比较近对片段 54 | //if (diffuseComponent < 0.01) 55 | //{ 56 | // shadow = 1.0; 57 | //} 58 | //else 59 | //{ 60 | //if the fragment is not behind light view frustum 61 | //if (vShadowCoord.w > 0.0) { 62 | 63 | shadow = shadowSimple(); 64 | 65 | //scale 0.0-1.0 to 0.2-1.0 66 | //otherways everything in shadow would be black 67 | shadow = (shadow * 0.8) + 0.2; 68 | //} 69 | //} 70 | 71 | // Final output color with shadow and lighting 72 | //gl_FragColor = (vColor * (diffuseComponent + ambientComponent * shadow)); 73 | gl_FragColor = (vColor * (diffuseComponent + ambientComponent) * shadow); 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/res/raw/depth_tex_v_depth_map.glsl: -------------------------------------------------------------------------------- 1 | // Vertex shader to generate the Depth Map 2 | // Used for shadow mapping - generates depth map from the light's viewpoint 3 | precision highp float; 4 | 5 | // model-view projection matrix 6 | uniform mat4 uMVPMatrix; 7 | 8 | // position of the vertices 9 | attribute vec4 aShadowPosition; 10 | 11 | void main() { 12 | gl_Position = uMVPMatrix * aShadowPosition; 13 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/depth_tex_v_with_shadow.glsl: -------------------------------------------------------------------------------- 1 | // Based on http://blog.shayanjaved.com/2011/03/13/shaders-android/ 2 | // from Shayan Javed 3 | 4 | uniform mat4 uMVPMatrix; 5 | uniform mat4 uMVMatrix; 6 | uniform mat4 uNormalMatrix; 7 | 8 | // the shadow projection matrix 9 | uniform mat4 uShadowProjMatrix; 10 | 11 | // position and normal of the vertices 12 | attribute vec4 aPosition; 13 | attribute vec4 aColor; 14 | attribute vec3 aNormal; 15 | 16 | // to pass on 17 | varying vec3 vPosition; 18 | varying vec4 vColor; 19 | varying vec3 vNormal; 20 | varying vec4 vShadowCoord; 21 | 22 | 23 | void main() { 24 | // the vertex position in camera space 25 | vPosition = vec3(uMVMatrix * aPosition); 26 | 27 | // the vertex color 28 | vColor = aColor; 29 | 30 | // the vertex normal coordinate in camera space 31 | vNormal = vec3(uNormalMatrix * vec4(aNormal, 0.0)); 32 | 33 | vShadowCoord = uShadowProjMatrix * aPosition; 34 | 35 | gl_Position = uMVPMatrix * aPosition; 36 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/direct_light_fragment.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | varying vec2 v_TextureCoordinates; 4 | uniform sampler2D u_TextureUnit; 5 | varying vec3 v_normalCoordinates; 6 | 7 | vec4 LightColor=vec4(0.0, 1.0, 1.0 , 1.0); 8 | uniform vec3 lightVector;// = vec3(0.1, 0.2, 0.3); 9 | uniform mat4 u_IT_MVMATRIX; 10 | 11 | void main() 12 | { 13 | vec3 eyeSpaceNormal=normalize(vec3(u_IT_MVMATRIX * vec4(v_normalCoordinates, 0.0))); 14 | float diffcuse = max(dot(eyeSpaceNormal, lightVector), 0.0); 15 | vec4 TextureWood = texture2D(u_TextureUnit, v_TextureCoordinates); 16 | 17 | gl_FragColor = TextureWood * LightColor * diffcuse; 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/res/raw/direct_light_vertex.glsl: -------------------------------------------------------------------------------- 1 | //#version 120 2 | 3 | attribute vec4 a_Position; 4 | attribute vec2 a_TextureCoordinates; 5 | attribute vec3 a_normalCoordinates; 6 | uniform mat4 u_Matrix; 7 | varying vec2 v_TextureCoordinates; 8 | varying vec3 v_normalCoordinates; 9 | 10 | void main() 11 | { 12 | v_TextureCoordinates = a_TextureCoordinates; 13 | v_normalCoordinates = a_normalCoordinates; 14 | gl_Position = u_Matrix * a_Position; 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/res/raw/embossed_fragment.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | 4 | varying vec2 v_TextureCoordinates; 5 | uniform sampler2D u_TextureUnit; 6 | 7 | void main() 8 | { 9 | float x = 1.0/256.0; 10 | float y = 1.0/256.0; 11 | 12 | vec3 p00 = texture2D(u_TextureUnit, v_TextureCoordinates).rgb; 13 | vec3 p01 = texture2D(u_TextureUnit, v_TextureCoordinates + vec2(0.0, y)).rgb; 14 | 15 | vec3 diff = p00 - p01; 16 | 17 | float maxNum = diff.r; 18 | if(abs(diff.g) > abs(maxNum)){ 19 | maxNum = diff.g; 20 | } 21 | if(abs(diff.b) > abs(maxNum)){ 22 | maxNum = diff.b; 23 | } 24 | 25 | float gray = clamp(maxNum + 0.2, 0.0, 1.0); 26 | gl_FragColor = vec4(gray, gray, gray, 1.0); 27 | 28 | // vec4 TextureFragile = texture2D(u_TextureUnit, v_TextureCoordinates); 29 | // gl_FragColor = TextureFragile; 30 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/fisheye_fragment.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | 4 | varying vec2 v_TextureCoordinates; 5 | uniform sampler2D u_TextureUnit; 6 | vec2 xy, uv; 7 | 8 | vec2 barrelDistorition(vec2 p){ 9 | float theta = atan(p.y, p.x); 10 | float radius = sqrt(p.x*p.x + p.y*p.y); 11 | radius = pow(radius, 0.955); 12 | p.x = radius * cos(theta); 13 | p.y = radius * sin(theta); 14 | return p + 0.5; 15 | } 16 | 17 | void main() 18 | { 19 | xy = v_TextureCoordinates - vec2(0.5); 20 | float distance = sqrt(xy.x * xy.x + xy.y * xy.y); 21 | if(distance > 0.35){ 22 | vec4 TextureFragile = vec4(0.0, 0.0, 0.0, 0.0); 23 | gl_FragColor = TextureFragile; 24 | }else{ 25 | uv = barrelDistorition(xy); 26 | vec4 TextureFragile = texture2D(u_TextureUnit, uv); 27 | gl_FragColor = TextureFragile; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/horitzontal_gussian_blure_fargment.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | varying vec2 v_TextureCoordinates; 4 | uniform sampler2D u_TextureUnit; 5 | float step = 10.0; 6 | uniform float[11] weightArray; 7 | 8 | void main() 9 | { 10 | float rowLength = step + 1.0; 11 | vec4 sum = texture2D(u_TextureUnit, v_TextureCoordinates); 12 | float unit = 0.01; 13 | for(float i=1.0; i 0.5){//这样做是为了在Y轴以rectSize为单位,交替的时候,在x轴上增加0.5 18 | position.x += 0.5; 19 | } 20 | position = fract(position); 21 | useBrick = step(position, BrickPercent); 22 | vec4 tempColor = vec4(1.0, 0.0, 1.0, 1.0); 23 | vec4 baseColor = vec4(0.0, 1.0, 0.0, 0.0); 24 | 25 | return mix(baseColor, tempColor, useBrick.x * useBrick.y); 26 | } 27 | 28 | vec4 getHollowOutColor(mat3 testRotation, vec3 VertexCoord){ //镂空效果 29 | float side = 0.40; 30 | float dotSize = 0.2; 31 | vec3 cube = vec3(side, side, side); 32 | float insideSphere, length; 33 | vec3 position = mod(testRotation * VertexCoord, cube) - cube * 0.5; 34 | length = sqrt( (position.x*position.x) + (position.y*position.y) + (position.z*position.z) ); 35 | insideSphere = step(length, dotSize); 36 | 37 | vec4 tempColor; 38 | vec4 baseColor; 39 | if (gl_FrontFacing){ 40 | // vec4 tempColor = vec4(1.0, 0.0, 1.0, 1.0); 41 | // vec4 baseColor = vec4(0.0, 1.0, 0.0, 0.0); 42 | // gl_FragColor = mix(baseColor, tempColor, insideSphere); 43 | if(insideSphere == 1.0){ 44 | tempColor = vec4(1.0, 0.0, 1.0, 1.0); 45 | baseColor = vec4(0.0, 1.0, 0.0, 0.0); 46 | }else{ 47 | discard; 48 | } 49 | return mix(baseColor, tempColor, insideSphere); 50 | }else{ 51 | vec4 tempColor = vec4(1.0, 1.0, 1.0, 1.0); 52 | vec4 baseColor = vec4(0.5, 0.5, 0.0, 0.0); 53 | return mix(baseColor, tempColor, insideSphere); 54 | } 55 | } 56 | 57 | void main() 58 | { 59 | // float RadianAngle = time/2.0; //90.0; 60 | float cosValue = cos(radianAngle/20.0); // Calculate Cos of Theta 61 | float sinValue = sin(radianAngle/10.0); // Calculate Sin of Theta 62 | 63 | vec3 VertexCoord = objectCoord; 64 | mat2 rotation = mat2(cosValue, sinValue, -sinValue, cosValue); 65 | // VertexCoord.xy = rotation * VertexCoord.xy; 66 | 67 | 68 | mat3 testRotation = mat3( 69 | cosValue, sinValue, 0, 70 | -sinValue, cosValue,0, 71 | 0, 0, 1); 72 | gl_FragColor = getHollowOutColor(testRotation, VertexCoord); //过程纹理的镂空效果 73 | // gl_FragColor = getBrickColor(rotation, VertexCoord); //过程纹理的铺砖效果 74 | 75 | 76 | 77 | 78 | 79 | // float side = 50.0; 80 | // float dotSize = side * 0.25; 81 | // vec2 square = vec2(side, side); 82 | // 83 | // vec2 position = mod(rotation * gl_FragCoord.xy, square) - square * 0.5; 84 | // float length = length(position); 85 | // float inside = step(length, dotSize); 86 | // vec4 tempColor = vec4(1,0,1,1); 87 | // vec4 baseColor = vec4(0,1,0,0); 88 | // 89 | // gl_FragColor = mix(baseColor, tempColor, inside); 90 | 91 | 92 | 93 | 94 | // if (VertexCoord.x > 0.0 && VertexCoord.y > 0.0) 95 | // gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 96 | // else if (VertexCoord.x > 0.0 && VertexCoord.y < 0.0) 97 | // gl_FragColor = vec4(0.0, 01.0, 0.0, 1.0); 98 | // else if (VertexCoord.x < 0.0 && VertexCoord.y > 0.0) 99 | // gl_FragColor = vec4(0.0, 01.0, 1.0, 1.0); 100 | // else if (VertexCoord.x < 0.0 && VertexCoord.y < 0.0) 101 | // gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0); 102 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/reflect_light_fragment.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | varying vec2 v_TextureCoordinates; 4 | uniform sampler2D u_TextureUnit; 5 | varying vec4 objectCoord; 6 | varying vec3 v_normalCoordinates; 7 | uniform vec4 u_light_point; 8 | uniform float textureMove; 9 | uniform samplerCube skyCube; 10 | 11 | vec4 LightColor=vec4(0.0, 1.0, 1.0 , 1.0); 12 | uniform mat4 u_MVMatrix; 13 | uniform mat4 u_IT_MVMATRIX; 14 | uniform mat4 matrixForSky; 15 | 16 | vec3 getReflectVector(vec3 toPointLight, vec3 cubeNormal){ //得到反射向量 17 | vec3 cubePointForCamera = vec3(toPointLight.x, toPointLight.y, toPointLight.z) * 1.0; 18 | vec3 cuberReflect = reflect(normalize(cubePointForCamera), cubeNormal); 19 | // vec3 cuberRefract = -refract(cubePointForCamera, cubeNormal, 0.9); 20 | vec4 tempVector = vec4(cuberReflect, 0.0); 21 | tempVector = matrixForSky * tempVector; 22 | cuberReflect = vec3(tempVector.x, tempVector.y, tempVector.z); 23 | return cuberReflect; 24 | } 25 | 26 | vec3 getRefractVector(vec3 toPointLight, vec3 cubeNormal){ //得到折射向量 27 | vec3 cubePointForCamera = vec3(toPointLight.x, toPointLight.y, toPointLight.z) * 1.0; 28 | vec3 cuberRefract = -refract(cubePointForCamera, cubeNormal, 0.9); 29 | vec4 tempVector = vec4(cuberRefract, 0.0); 30 | tempVector = matrixForSky * tempVector; 31 | cuberRefract = vec3(tempVector.x, tempVector.y, tempVector.z); 32 | return cuberRefract; 33 | } 34 | 35 | 36 | void main() 37 | { 38 | vec4 eyeSpacePosition = u_MVMatrix * objectCoord; 39 | vec3 eyeSpaceNormal = normalize(vec3(u_IT_MVMATRIX * vec4(v_normalCoordinates, 0.0))); 40 | vec3 viewer = -normalize(vec3(eyeSpacePosition)); //vec3(1,1,1); 41 | vec3 toPointLight = vec3(u_light_point) - vec3(eyeSpacePosition); 42 | 43 | float distance = length(vec3(toPointLight)); 44 | toPointLight = normalize(toPointLight); 45 | vec3 reflect = reflect(-toPointLight, eyeSpaceNormal); //vec3(1,1,1);// 46 | 47 | vec3 cubeNormal = eyeSpaceNormal; 48 | if(cubeNormal.z < 0.0){ 49 | cubeNormal.z = -cubeNormal.z; 50 | } 51 | vec3 cuberRefract = getRefractVector(toPointLight, cubeNormal);//折射 52 | // vec3 cuberReflect = getReflectVector(toPointLight, cubeNormal);//反射 53 | vec4 skyColor = textureCube(skyCube, cuberRefract); 54 | 55 | float intensity = pow(max(dot(reflect, viewer), 0.0), 1.0); 56 | vec4 TextureWood = texture2D(u_TextureUnit, v_TextureCoordinates+textureMove); 57 | float diffcuse = max(0.0, dot(eyeSpaceNormal, toPointLight)); 58 | gl_FragColor = (TextureWood * LightColor * intensity * 0.1) + (skyColor * 0.9); 59 | } 60 | 61 | -------------------------------------------------------------------------------- /app/src/main/res/raw/reflect_light_vertext.glsl: -------------------------------------------------------------------------------- 1 | attribute vec4 a_Position; 2 | attribute vec2 a_TextureCoordinates; 3 | attribute vec3 a_normalCoordinates; 4 | 5 | uniform mat4 u_Matrix; 6 | varying vec2 v_TextureCoordinates; 7 | varying vec4 objectCoord; 8 | varying vec3 v_normalCoordinates; 9 | 10 | void main() 11 | { 12 | v_TextureCoordinates = a_TextureCoordinates; 13 | objectCoord = a_Position; 14 | vec4 VertexCoord = a_Position; 15 | 16 | v_normalCoordinates = a_normalCoordinates; 17 | 18 | gl_Position = u_Matrix * VertexCoord; 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/res/raw/ripple_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | 2 | #extension GL_OES_EGL_image_external : require 3 | precision mediump float; 4 | 5 | varying vec2 v_TextureCoordinates; 6 | varying vec4 normalVector; 7 | varying float isRange; 8 | uniform samplerExternalOES u_TextureUnit; 9 | uniform samplerExternalOES a_TextureUnit; 10 | 11 | uniform float textureCoordinateY; 12 | 13 | uniform float time; 14 | 15 | const float step_w = 0.0015625; 16 | const float step_h = 0.0027778; 17 | const float maxW = 0.04; 18 | 19 | void main() 20 | { 21 | vec3 lightVector = normalize(vec3(0.0, 4.0, 4.0)); 22 | vec2 textureCoordinate = vec2(v_TextureCoordinates.x, v_TextureCoordinates.y + textureCoordinateY); 23 | // vec2 textureCoordinate = vec2(v_TextureCoordinates.x, (v_TextureCoordinates.y) + textureCoordinateY); 24 | // 25 | // float x = textureCoordinate.x - maxW*sin((textureCoordinate.y*80.0 - time*7.50)); 26 | // float x = textureCoordinate.x - maxW*sin((textureCoordinate.y*80.0 )); 27 | // x = fract(x); 28 | 29 | vec4 TextureFragile = texture2D(u_TextureUnit, vec2(textureCoordinate.x, textureCoordinate.y)); 30 | vec4 TextureWood = texture2D(a_TextureUnit, vec2(textureCoordinate.x, textureCoordinate.y)); 31 | 32 | // if(isRange == 1.0){ 33 | gl_FragColor = TextureWood * (max( 0.0, dot(vec3(normalVector.x, normalVector.y, normalVector.z),lightVector)) * 10.0); 34 | // }else{ 35 | // gl_FragColor = TextureWood; 36 | // } 37 | 38 | 39 | // float side = 80.0; 40 | // float dotSize = side * 0.25; 41 | // vec2 square = vec2(side, side); 42 | // 43 | // vec2 position = mod(gl_FragCoord.xy, square) - square * 0.5; 44 | // float length = length(position); 45 | // float inside = step(length, dotSize); 46 | // vec4 tempColor = vec4(1,0,1,1); 47 | // vec4 baseColor = vec4(0,1,0,0); 48 | // 49 | // gl_FragColor = mix(baseColor, tempColor, inside); 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/res/raw/rotate_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | 4 | varying vec2 v_TextureCoordinates; 5 | uniform sampler2D u_TextureUnit; 6 | 7 | void main() 8 | { 9 | vec4 TextureFragile = texture2D(u_TextureUnit, v_TextureCoordinates); 10 | gl_FragColor = TextureFragile; 11 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/rotate_vertex_shader.glsl: -------------------------------------------------------------------------------- 1 | 2 | attribute vec4 a_Position; 3 | attribute vec2 a_TextureCoordinates; 4 | uniform mat4 u_Matrix; 5 | varying vec2 v_TextureCoordinates; 6 | varying vec3 objectCoord; 7 | 8 | void main() 9 | { 10 | v_TextureCoordinates = a_TextureCoordinates; 11 | objectCoord = a_Position.xyz; 12 | 13 | gl_Position = u_Matrix * a_Position; 14 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/simple_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | //#version 300 es 3 | //#define side 0.5; 4 | 5 | varying vec2 v_TextureCoordinates; 6 | uniform sampler2D u_TextureUnit; 7 | uniform sampler2D a_TextureUnit; 8 | uniform vec4 u_Color; 9 | varying vec3 objectCoord; 10 | 11 | //uniform float side; 12 | uniform float time; 13 | 14 | 15 | vec3 MaterialAmbient=vec3(1.0, 0.0, 0.0); 16 | vec3 LightAmbient=vec3(1.0, 1.0, 1.0); 17 | 18 | 19 | void main() 20 | { 21 | // float side = 0.3; 22 | // float dotSize = side * 0.7075; 23 | // vec2 square = vec2(side, side); 24 | // vec3 modelColor = vec3(1.0, 0.0, 0.0); 25 | // vec3 dotColor = vec3(0.0, 1.0, 1.0); 26 | 27 | // vec2 position = mod(gl_FragColor.xy, square) - square*0.5; 28 | // float length = length(position); 29 | // float inside = step(length, dotSize); 30 | // if(inside == 0){ 31 | // gl_FragColor = vec4(dotColor, 1.0); 32 | // }else{ 33 | // gl_FragColor = vec4(modelColor, 1.0); 34 | // } 35 | //gl_FragColor = vec4(mix(dotColor, modelColor, inside), 1.0); 36 | 37 | vec4 TextureFragile = texture2D(u_TextureUnit, v_TextureCoordinates); 38 | vec4 TextureWood = texture2D(a_TextureUnit, v_TextureCoordinates+time); 39 | 40 | // vec4 ambient = vec4(LightAmbient, 1.0); 41 | 42 | gl_FragColor = TextureWood*(TextureFragile+0.05); 43 | //gl_FragColor = mix(TextureWood,TextureFragile,TextureFragile.a); 44 | //gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates); 45 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/simple_vertex_shader.glsl: -------------------------------------------------------------------------------- 1 | #define AMPLITUDE 1.2 2 | 3 | #define RIPPLE_AMPLITUDE 6.5 4 | #define FREQUENCY 1.0 5 | #define PI 3.14285714286 6 | 7 | attribute vec4 a_Position; 8 | attribute vec2 a_TextureCoordinates; 9 | uniform mat4 u_Matrix; 10 | uniform float time; 11 | varying vec2 v_TextureCoordinates; 12 | varying vec3 objectCoord; 13 | 14 | void main() 15 | { 16 | 17 | v_TextureCoordinates = a_TextureCoordinates; 18 | objectCoord = a_Position.xyz; 19 | vec4 VertexCoord = a_Position; 20 | // float distance = length(VertexCoord); 21 | //VertexCoord.y += sin(VertexCoord.x*time + time); 22 | // VertexCoord.y = sin( 2.0 * PI * distance * FREQUENCY + time)* RIPPLE_AMPLITUDE; 23 | 24 | //float angle = 1.0; 25 | //float cos = cos(angle); 26 | //float sin = sin(angle); 27 | //mat2 rotation = mat2(cos, sin, -sin, cos); 28 | //mat2 rotation = mat2(1.0, 2.0, -1.0, 2.0); 29 | //VertexCoord.x = rotation * VertexCoord.x; 30 | 31 | gl_Position = u_Matrix * VertexCoord; 32 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/skybox_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | 2 | precision mediump float; 3 | 4 | uniform samplerCube u_TextureUnit; 5 | varying vec3 v_Position; 6 | 7 | void main() 8 | { 9 | gl_FragColor = textureCube(u_TextureUnit, v_Position); 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/res/raw/skybox_vertex_shader.glsl: -------------------------------------------------------------------------------- 1 | uniform mat4 u_Matrix; 2 | attribute vec3 a_Position; 3 | varying vec3 v_Position; 4 | 5 | void main() 6 | { 7 | v_Position = a_Position; 8 | // Make sure to convert from the right-handed coordinate system of the 9 | // world to the left-handed coordinate system of the cube map, otherwise, 10 | // our cube map will still work but everything will be flipped. 11 | v_Position.z = -v_Position.z; 12 | 13 | gl_Position = u_Matrix * vec4(a_Position, 1.0); 14 | gl_Position = gl_Position.xyww; 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/res/raw/sobel_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | 4 | varying vec2 v_TextureCoordinates; 5 | uniform sampler2D u_TextureUnit; 6 | vec3 lum = vec3(0.2126, 0.7152, 0.0722); 7 | float p00,p10,p20,p01,p21,p02,p12,p22,x,y,px,py,distance; 8 | 9 | void main() 10 | { 11 | float x = 1.0/256.0; 12 | float y = 1.0/256.0; 13 | p00 = dot(texture2D(u_TextureUnit, v_TextureCoordinates+vec2(-x, y)).rgb, lum); 14 | p10 = dot(texture2D(u_TextureUnit, v_TextureCoordinates+vec2(-x,0.)).rgb, lum); 15 | p20 = dot(texture2D(u_TextureUnit, v_TextureCoordinates+vec2(-x,-y)).rgb, lum); 16 | p01 = dot(texture2D(u_TextureUnit, v_TextureCoordinates+vec2(0., y)).rgb, lum); 17 | p21 = dot(texture2D(u_TextureUnit, v_TextureCoordinates+vec2(0.,-y)).rgb, lum); 18 | p02 = dot(texture2D(u_TextureUnit, v_TextureCoordinates+vec2( x, y)).rgb, lum); 19 | p12 = dot(texture2D(u_TextureUnit, v_TextureCoordinates+vec2( x,0.)).rgb, lum); 20 | p22 = dot(texture2D(u_TextureUnit, v_TextureCoordinates+vec2( x,-y)).rgb, lum); 21 | // Apply Sobel Operator 22 | px = p00 + 1.0*p10 + p20 - (p02 + 1.0*p12 + p22); 23 | py = p00 + 1.0*p01 + p02 - (p20 + 1.0*p21 + p22); 24 | 25 | distance = px*px+py*py; 26 | // gl_FragColor = vec4(distance, distance, distance, 1.0); 27 | 28 | // Check frequency change with given threshold 29 | if ((distance = px*px+py*py) > 0.15 ){ 30 | gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); 31 | }else{ 32 | // gl_FragColor = vec4(1.0); 33 | vec4 TextureFragile = texture2D(u_TextureUnit, v_TextureCoordinates); 34 | gl_FragColor = TextureFragile; 35 | } 36 | 37 | 38 | // vec4 TextureFragile = texture2D(u_TextureUnit, v_TextureCoordinates); 39 | // gl_FragColor = TextureFragile; 40 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/test_fragment_shader.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | 4 | varying vec2 v_TextureCoordinates; 5 | uniform sampler2D u_TextureUnit; 6 | 7 | void main() 8 | { 9 | vec4 TextureFragile = texture2D(u_TextureUnit, v_TextureCoordinates); 10 | float textureBlue = 1.0;//TextureFragile.g-0.001; 11 | // if(textureBlue < 0.0){ 12 | // textureBlue = 1.0; 13 | // } 14 | TextureFragile = vec4(1.0, textureBlue, TextureFragile.b, 0); 15 | gl_FragColor = TextureFragile; 16 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/twirl_fragment.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | varying vec2 v_TextureCoordinates; 4 | uniform sampler2D u_TextureUnit; 5 | float twirlRadius = 100.0; 6 | uniform float angle; 7 | float imageWidth = 256.0; 8 | float imageHeight = 256.0; 9 | vec2 center = vec2(125.0,125.0); 10 | float radiusFacotr = 3.0; 11 | 12 | vec4 twirl(sampler2D tex, vec2 uv, float angle){ 13 | vec2 texSize = vec2(imageWidth, imageHeight); 14 | vec2 tc = (uv * texSize) - center; 15 | float distance = sqrt(tc.x*tc.x + tc.y*tc.y); 16 | if(distance < twirlRadius + angle * radiusFacotr){ 17 | float percent = (twirlRadius - distance) / twirlRadius; 18 | float theta = percent * percent * angle; 19 | float sinus = sin(theta); 20 | float cosine = cos(theta); 21 | tc = vec2(dot(tc, vec2(cosine, -sinus)), dot(tc, vec2(sinus, cosine))); 22 | } 23 | return texture2D(tex, (tc+center)/256.0); 24 | } 25 | 26 | void main() 27 | { 28 | // if(gl_FragCoord.x > 100.0){ 29 | gl_FragColor = twirl(u_TextureUnit, v_TextureCoordinates, angle); 30 | // }else{ 31 | // gl_FragColor = texture2D(u_TextureUnit, v_TextureCoordinates); 32 | // } 33 | 34 | 35 | 36 | // vec4 TextureFragile = texture2D(u_TextureUnit, v_TextureCoordinates); 37 | // gl_FragColor = TextureFragile; 38 | } -------------------------------------------------------------------------------- /app/src/main/res/raw/verital_gussian_blur_fragment.glsl: -------------------------------------------------------------------------------- 1 | precision highp float; 2 | 3 | varying vec2 v_TextureCoordinates; 4 | uniform sampler2D u_TextureUnit; 5 | float step = 10.0; 6 | uniform float[11] weightArray; 7 | 8 | void main() 9 | { 10 | float rowLength = step + 1.0; 11 | vec4 sum = texture2D(u_TextureUnit, v_TextureCoordinates); 12 | float unit = 0.01; 13 | for(float i=1.0; i 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | OpenglDemo 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | maven { 7 | url 'https://maven.google.com/' 8 | name 'Google' 9 | } 10 | } 11 | dependencies { 12 | classpath 'com.android.tools.build:gradle:2.3.2' 13 | 14 | // NOTE: Do not place your application dependencies here; they belong 15 | // in the individual module build.gradle files 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | jcenter() 22 | maven { 23 | url 'https://maven.google.com/' 24 | name 'Google' 25 | } 26 | } 27 | } 28 | 29 | task clean(type: Delete) { 30 | delete rootProject.buildDir 31 | } 32 | -------------------------------------------------------------------------------- /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 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GathinLion/AndroidOpengl/9ef82c4c288807543b427443ea506cf7272cd27b/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jun 02 13:49:27 CST 2017 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-3.3-all.zip 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------