├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hzw │ │ └── opengles │ │ └── introduction │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── hzw │ │ │ └── opengles │ │ │ └── introduction │ │ │ ├── GLImageHandler.java │ │ │ ├── GLShowImageActivity.java │ │ │ ├── MainActivity.java │ │ │ └── util │ │ │ └── OpenGlUtils.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable-xxhdpi │ │ ├── thelittleprince.png │ │ └── thelittleprince2.png │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_01.xml │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── values-v21 │ │ └── styles.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── hzw │ └── opengles │ └── introduction │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Gradle 2 | .gradle 3 | build 4 | local.properties 5 | reports 6 | 7 | # Maven 8 | target 9 | pom.xml.* 10 | release.properties 11 | gen-external-apklibs 12 | 13 | # Eclipse 14 | .classpath 15 | .project 16 | .settings 17 | eclipsebin 18 | 19 | # IntelliJ IDEA 20 | .idea 21 | *.iml 22 | *.ipl 23 | *.iws 24 | classes/ 25 | idea-classes/ 26 | coverage-error.log 27 | 28 | # Android 29 | gen 30 | bin 31 | project.properties 32 | out 33 | 34 | # Finder 35 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android OpenGL ES 2.0 2 | 3 | # 从显示一张图片开始学习OpenGL ES 4 | 5 | 文章:[《从显示一张图片开始学习OpenGL ES》](https://blog.csdn.net/u012964944/article/details/82788620) -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.hzw.opengles.introduction" 7 | minSdkVersion 15 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:28.0.0-rc02' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/hzw/opengles/introduction/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.hzw.opengles.introduction; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.hzw.opengles.introduction", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/opengles/introduction/GLImageHandler.java: -------------------------------------------------------------------------------- 1 | package com.hzw.opengles.introduction; 2 | 3 | import android.opengl.GLES20; 4 | 5 | import com.hzw.opengles.introduction.util.OpenGlUtils; 6 | 7 | import java.nio.FloatBuffer; 8 | import java.util.LinkedList; 9 | 10 | /** 11 | * 负责显示一张图片 12 | */ 13 | public class GLImageHandler { 14 | // 数据中有多少个顶点,管线就调用多少次顶点着色器 15 | public static final String NO_FILTER_VERTEX_SHADER = "" + 16 | "attribute vec4 position;\n" + // 顶点着色器的顶点坐标,由外部程序传入 17 | "attribute vec4 inputTextureCoordinate;\n" + // 传入的纹理坐标 18 | " \n" + 19 | "varying vec2 textureCoordinate;\n" + 20 | " \n" + 21 | "void main()\n" + 22 | "{\n" + 23 | " gl_Position = position;\n" + 24 | " textureCoordinate = inputTextureCoordinate.xy;\n" + // 最终顶点位置 25 | "}"; 26 | 27 | // 光栅化后产生了多少个片段,就会插值计算出多少个varying变量,同时渲染管线就会调用多少次片段着色器 28 | public static final String NO_FILTER_FRAGMENT_SHADER = "" + 29 | "varying highp vec2 textureCoordinate;\n" + // 最终顶点位置,上面顶点着色器的varying变量会传递到这里 30 | " \n" + 31 | "uniform sampler2D inputImageTexture;\n" + // 外部传入的图片纹理 即代表整张图片的数据 32 | " \n" + 33 | "void main()\n" + 34 | "{\n" + 35 | " gl_FragColor = texture2D(inputImageTexture, textureCoordinate);\n" + // 调用函数 进行纹理贴图 36 | "}"; 37 | 38 | private final LinkedList mRunOnDraw; 39 | private final String mVertexShader; 40 | private final String mFragmentShader; 41 | protected int mGLProgId; 42 | protected int mGLAttribPosition; 43 | protected int mGLUniformTexture; 44 | protected int mGLAttribTextureCoordinate; 45 | 46 | public GLImageHandler() { 47 | this(NO_FILTER_VERTEX_SHADER, NO_FILTER_FRAGMENT_SHADER); 48 | } 49 | 50 | public GLImageHandler(final String vertexShader, final String fragmentShader) { 51 | mRunOnDraw = new LinkedList(); 52 | mVertexShader = vertexShader; 53 | mFragmentShader = fragmentShader; 54 | } 55 | 56 | public final void init() { 57 | mGLProgId = OpenGlUtils.loadProgram(mVertexShader, mFragmentShader); // 编译链接着色器,创建着色器程序 58 | mGLAttribPosition = GLES20.glGetAttribLocation(mGLProgId, "position"); // 顶点着色器的顶点坐标 59 | mGLUniformTexture = GLES20.glGetUniformLocation(mGLProgId, "inputImageTexture"); // 传入的图片纹理 60 | mGLAttribTextureCoordinate = GLES20.glGetAttribLocation(mGLProgId, "inputTextureCoordinate"); // 顶点着色器的纹理坐标 61 | } 62 | 63 | public void onDraw(final int textureId, final FloatBuffer cubeBuffer, 64 | final FloatBuffer textureBuffer) { 65 | GLES20.glUseProgram(mGLProgId); 66 | // 顶点着色器的顶点坐标 67 | cubeBuffer.position(0); 68 | GLES20.glVertexAttribPointer(mGLAttribPosition, 2, GLES20.GL_FLOAT, false, 0, cubeBuffer); 69 | GLES20.glEnableVertexAttribArray(mGLAttribPosition); 70 | // 顶点着色器的纹理坐标 71 | textureBuffer.position(0); 72 | GLES20.glVertexAttribPointer(mGLAttribTextureCoordinate, 2, GLES20.GL_FLOAT, false, 0, textureBuffer); 73 | GLES20.glEnableVertexAttribArray(mGLAttribTextureCoordinate); 74 | // 传入的图片纹理 75 | if (textureId != OpenGlUtils.NO_TEXTURE) { 76 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 77 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId); 78 | GLES20.glUniform1i(mGLUniformTexture, 0); 79 | } 80 | 81 | // 绘制顶点 ,方式有顶点法和索引法 82 | // GLES20.GL_TRIANGLE_STRIP即每相邻三个顶点组成一个三角形,为一系列相接三角形构成 83 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); // 顶点法,按照传入渲染管线的顶点顺序及采用的绘制方式将顶点组成图元进行绘制 84 | 85 | GLES20.glDisableVertexAttribArray(mGLAttribPosition); 86 | GLES20.glDisableVertexAttribArray(mGLAttribTextureCoordinate); 87 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/opengles/introduction/GLShowImageActivity.java: -------------------------------------------------------------------------------- 1 | package com.hzw.opengles.introduction; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Bitmap; 5 | import android.graphics.BitmapFactory; 6 | import android.opengl.GLES20; 7 | import android.opengl.GLSurfaceView; 8 | import android.os.Bundle; 9 | import android.support.annotation.Nullable; 10 | 11 | import com.hzw.opengles.introduction.util.OpenGlUtils; 12 | 13 | import java.nio.ByteBuffer; 14 | import java.nio.ByteOrder; 15 | import java.nio.FloatBuffer; 16 | 17 | import javax.microedition.khronos.egl.EGLConfig; 18 | import javax.microedition.khronos.opengles.GL10; 19 | 20 | public class GLShowImageActivity extends Activity { 21 | // 绘制图片的原理:定义一组矩形区域的顶点,然后根据纹理坐标把图片作为纹理贴在该矩形区域内。 22 | 23 | // 原始的矩形区域的顶点坐标,因为后面使用了顶点法绘制顶点,所以不用定义绘制顶点的索引。无论窗口的大小为多少,在OpenGL二维坐标系中都是为下面表示的矩形区域 24 | static final float CUBE[] = { // 窗口中心为OpenGL二维坐标系的原点(0,0) 25 | -1.0f, -1.0f, // v1 26 | 1.0f, -1.0f, // v2 27 | -1.0f, 1.0f, // v3 28 | 1.0f, 1.0f, // v4 29 | }; 30 | // 纹理也有坐标系,称UV坐标,或者ST坐标。UV坐标定义为左上角(0,0),右下角(1,1),一张图片无论大小为多少,在UV坐标系中都是图片左上角为(0,0),右下角(1,1) 31 | // 纹理坐标,每个坐标的纹理采样对应上面顶点坐标。 32 | public static final float TEXTURE_NO_ROTATION[] = { 33 | 0.0f, 1.0f, // v1 34 | 1.0f, 1.0f, // v2 35 | 0.0f, 0.0f, // v3 36 | 1.0f, 0.0f, // v4 37 | }; 38 | 39 | private GLSurfaceView mGLSurfaceView; 40 | private int mGLTextureId = OpenGlUtils.NO_TEXTURE; // 纹理id 41 | private GLImageHandler mGLImageHandler = new GLImageHandler(); 42 | 43 | private FloatBuffer mGLCubeBuffer; 44 | private FloatBuffer mGLTextureBuffer; 45 | private int mOutputWidth, mOutputHeight; // 窗口大小 46 | private int mImageWidth, mImageHeight; // bitmap图片实际大小 47 | 48 | @Override 49 | protected void onCreate(@Nullable Bundle savedInstanceState) { 50 | super.onCreate(savedInstanceState); 51 | setContentView(R.layout.activity_01); 52 | mGLSurfaceView = findViewById(R.id.gl_surfaceview); 53 | mGLSurfaceView.setEGLContextClientVersion(2); // 创建OpenGL ES 2.0 的上下文环境 54 | 55 | mGLSurfaceView.setRenderer(new MyRender()); 56 | mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // 手动刷新 57 | } 58 | 59 | private class MyRender implements GLSurfaceView.Renderer { 60 | 61 | @Override 62 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 63 | GLES20.glClearColor(0, 0, 0, 1); 64 | GLES20.glDisable(GLES20.GL_DEPTH_TEST); // 当我们需要绘制透明图片时,就需要关闭它 65 | mGLImageHandler.init(); 66 | 67 | // 需要显示的图片 68 | Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.thelittleprince); 69 | mImageWidth = bitmap.getWidth(); 70 | mImageHeight = bitmap.getHeight(); 71 | // 把图片数据加载进GPU,生成对应的纹理id 72 | mGLTextureId = OpenGlUtils.loadTexture(bitmap, mGLTextureId, true); // 加载纹理 73 | 74 | // 顶点数组缓冲器 75 | mGLCubeBuffer = ByteBuffer.allocateDirect(CUBE.length * 4) 76 | .order(ByteOrder.nativeOrder()) 77 | .asFloatBuffer(); 78 | mGLCubeBuffer.put(CUBE).position(0); 79 | 80 | // 纹理数组缓冲器 81 | mGLTextureBuffer = ByteBuffer.allocateDirect(TEXTURE_NO_ROTATION.length * 4) 82 | .order(ByteOrder.nativeOrder()) 83 | .asFloatBuffer(); 84 | mGLTextureBuffer.put(TEXTURE_NO_ROTATION).position(0); 85 | } 86 | 87 | @Override 88 | public void onSurfaceChanged(GL10 gl, int width, int height) { 89 | mOutputWidth = width; 90 | mOutputHeight = height; 91 | GLES20.glViewport(0, 0, width, height); // 设置窗口大小 92 | adjustImageScaling(); // 调整图片显示大小。如果不调用该方法,则会导致图片整个拉伸到填充窗口显示区域 93 | } 94 | 95 | @Override 96 | public void onDrawFrame(GL10 gl) { // 绘制 97 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 98 | // 根据纹理id,顶点和纹理坐标数据绘制图片 99 | mGLImageHandler.onDraw(mGLTextureId, mGLCubeBuffer, mGLTextureBuffer); 100 | } 101 | 102 | // 调整图片显示大小为居中显示 103 | private void adjustImageScaling() { 104 | float outputWidth = mOutputWidth; 105 | float outputHeight = mOutputHeight; 106 | 107 | float ratio1 = outputWidth / mImageWidth; 108 | float ratio2 = outputHeight / mImageHeight; 109 | float ratioMax = Math.min(ratio1, ratio2); 110 | // 居中后图片显示的大小 111 | int imageWidthNew = Math.round(mImageWidth * ratioMax); 112 | int imageHeightNew = Math.round(mImageHeight * ratioMax); 113 | 114 | // 图片被拉伸的比例 115 | float ratioWidth = outputWidth / imageWidthNew; 116 | float ratioHeight = outputHeight / imageHeightNew; 117 | // 根据拉伸比例还原顶点 118 | float[] cube = new float[]{ 119 | CUBE[0] / ratioWidth, CUBE[1] / ratioHeight, 120 | CUBE[2] / ratioWidth, CUBE[3] / ratioHeight, 121 | CUBE[4] / ratioWidth, CUBE[5] / ratioHeight, 122 | CUBE[6] / ratioWidth, CUBE[7] / ratioHeight, 123 | }; 124 | 125 | mGLCubeBuffer.clear(); 126 | mGLCubeBuffer.put(cube).position(0); 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/opengles/introduction/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.hzw.opengles.introduction; 2 | 3 | import android.app.ListActivity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | import android.widget.ListView; 8 | import android.widget.SimpleAdapter; 9 | 10 | import java.util.ArrayList; 11 | import java.util.HashMap; 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | public class MainActivity extends ListActivity { 16 | 17 | public static final String TITLE = "title"; 18 | public static final String SUBTITLE = "subtitle"; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | 24 | setListAdapter(new SimpleAdapter(this, createData(), 25 | android.R.layout.simple_list_item_2, new String[]{TITLE, 26 | SUBTITLE}, new int[]{android.R.id.text1, 27 | android.R.id.text2})); 28 | } 29 | 30 | @Override 31 | protected void onListItemClick(ListView l, View v, int position, long id) { 32 | if (position == 0) { 33 | startActivity(new Intent(getApplicationContext(), GLShowImageActivity.class)); 34 | 35 | } else if (position == 1) { 36 | 37 | 38 | } 39 | } 40 | 41 | private List> createData() { 42 | List> data = new ArrayList>(); 43 | data.add(createItem("绘制图片", "绘制一张图片")); 44 | 45 | return data; 46 | } 47 | 48 | private Map createItem(String title, String subtitle) { 49 | Map item = new HashMap(); 50 | 51 | item.put(TITLE, title); 52 | item.put(SUBTITLE, subtitle); 53 | 54 | return item; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/com/hzw/opengles/introduction/util/OpenGlUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 CyberAgent 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.hzw.opengles.introduction.util; 18 | 19 | import android.graphics.Bitmap; 20 | import android.graphics.Bitmap.Config; 21 | import android.hardware.Camera.Size; 22 | import android.opengl.GLES20; 23 | import android.opengl.GLUtils; 24 | import android.opengl.Matrix; 25 | import android.util.Log; 26 | 27 | import java.nio.IntBuffer; 28 | 29 | public class OpenGlUtils { 30 | public static final int NO_TEXTURE = -1; 31 | 32 | public static void deleteTexture(int textureID) { 33 | GLES20.glDeleteTextures(1, new int[]{ 34 | textureID 35 | }, 0); 36 | } 37 | 38 | public static int loadTexture(final Bitmap img, final int usedTexId) { 39 | return loadTexture(img, usedTexId, true); 40 | } 41 | 42 | public static int loadTexture(final Bitmap img, final int usedTexId, final boolean recycle) { 43 | int textures[] = new int[]{-1}; 44 | if (usedTexId == NO_TEXTURE) { 45 | GLES20.glGenTextures(1, textures, 0); 46 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]); 47 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); 48 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); 49 | //纹理也有坐标系,称UV坐标,或者ST坐标 50 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT); // S轴的拉伸方式为重复,决定采样值的坐标超出图片范围时的采样方式 51 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT); // T轴的拉伸方式为重复 52 | 53 | GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0); 54 | } else { 55 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId); 56 | GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img); 57 | textures[0] = usedTexId; 58 | } 59 | if (recycle) { 60 | img.recycle(); 61 | } 62 | return textures[0]; 63 | } 64 | 65 | public static int loadTexture(final IntBuffer data, final Size size, final int usedTexId) { 66 | int textures[] = new int[1]; 67 | if (usedTexId == NO_TEXTURE) { 68 | GLES20.glGenTextures(1, textures, 0); 69 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]); 70 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, 71 | GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR); 72 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, 73 | GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR); 74 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, 75 | GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); 76 | GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, 77 | GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); 78 | GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, size.width, size.height, 79 | 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, data); 80 | } else { 81 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId); 82 | GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, size.width, 83 | size.height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, data); 84 | textures[0] = usedTexId; 85 | } 86 | return textures[0]; 87 | } 88 | 89 | public static int loadTextureAsBitmap(final IntBuffer data, final Size size, final int usedTexId) { 90 | Bitmap bitmap = Bitmap 91 | .createBitmap(data.array(), size.width, size.height, Config.ARGB_8888); 92 | return loadTexture(bitmap, usedTexId); 93 | } 94 | 95 | /** 96 | * 加载着色器 97 | * @param strSource 98 | * @param iType 99 | * @return 100 | */ 101 | public static int loadShader(final String strSource, final int iType) { 102 | int[] compiled = new int[1]; 103 | int iShader = GLES20.glCreateShader(iType); 104 | GLES20.glShaderSource(iShader, strSource); 105 | GLES20.glCompileShader(iShader); 106 | GLES20.glGetShaderiv(iShader, GLES20.GL_COMPILE_STATUS, compiled, 0); 107 | if (compiled[0] == 0) { 108 | Log.d("Load Shader Failed", "Compilation\n" + GLES20.glGetShaderInfoLog(iShader)); 109 | return 0; 110 | } 111 | return iShader; 112 | } 113 | 114 | /** 115 | * 加载着色器程序 116 | * @param strVSource 117 | * @param strFSource 118 | * @return 119 | */ 120 | public static int loadProgram(final String strVSource, final String strFSource) { 121 | int iVShader; 122 | int iFShader; 123 | int iProgId; 124 | 125 | iVShader = loadShader(strVSource, GLES20.GL_VERTEX_SHADER); // 顶点着色器 126 | if (iVShader == 0) { 127 | Log.d("Load Program", "Vertex Shader Failed"); 128 | return 0; 129 | } 130 | iFShader = loadShader(strFSource, GLES20.GL_FRAGMENT_SHADER); // 片元着色器 131 | if (iFShader == 0) { 132 | Log.d("Load Program", "Fragment Shader Failed"); 133 | return 0; 134 | } 135 | 136 | iProgId = GLES20.glCreateProgram(); 137 | 138 | GLES20.glAttachShader(iProgId, iVShader); 139 | GLES20.glAttachShader(iProgId, iFShader); 140 | 141 | GLES20.glLinkProgram(iProgId); 142 | // 获取program的链接情况 143 | int[] link = new int[1]; 144 | GLES20.glGetProgramiv(iProgId, GLES20.GL_LINK_STATUS, link, 0); 145 | if (link[0] <= 0) { 146 | Log.d("Load Program", "Linking Failed"); 147 | return 0; 148 | } 149 | GLES20.glDeleteShader(iVShader); 150 | GLES20.glDeleteShader(iFShader); 151 | return iProgId; 152 | } 153 | 154 | public static float rnd(final float min, final float max) { 155 | float fRandNum = (float) Math.random(); 156 | return min + (max - min) * fRandNum; 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/thelittleprince.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/drawable-xxhdpi/thelittleprince.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/thelittleprince2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/drawable-xxhdpi/thelittleprince2.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_01.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | OpenGLESIntroduction 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/test/java/com/hzw/opengles/introduction/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.hzw.opengles.introduction; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.1.2' 11 | 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1993hzw/OpenGLESIntroduction/0bae03ce963c801a05cb2da011258356d0beb452/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Sep 15 11:12:48 CST 2018 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-4.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------