├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── assets │ └── image_0.jpg │ ├── java │ └── com │ │ └── kenneycode │ │ ├── GlobalConstants.kt │ │ ├── MainActivity.kt │ │ ├── SampleActivity.kt │ │ ├── Util.kt │ │ └── samples │ │ ├── fragment │ │ ├── Sample2Triangles.kt │ │ ├── SampleDrawMode.kt │ │ ├── SampleFragmentShader.kt │ │ ├── SampleFrameBuffer.kt │ │ ├── SampleHelloWorld.kt │ │ ├── SampleTexture.kt │ │ └── SampleVertexShader.kt │ │ └── renderer │ │ ├── Sample2TrianglesRenderer.kt │ │ ├── SampleCheckError.kt │ │ ├── SampleDrawModeRenderer.kt │ │ ├── SampleFragmentShaderRenderer.kt │ │ ├── SampleFrameBufferRenderer.kt │ │ ├── SampleHelloWorldRenderer.kt │ │ ├── SamplePointAndLine.kt │ │ ├── SampleTextureRenderer.kt │ │ └── SampleVertexShaderRenderer.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ ├── activity_main.xml │ ├── activity_sample.xml │ ├── fragment_common_sample.xml │ ├── fragment_sample_draw_mode.xml │ └── layout_sample_list_item.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 │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | *.aab 5 | 6 | # Files for the ART/Dalvik VM 7 | *.dex 8 | 9 | # Java class files 10 | *.class 11 | 12 | # Generated files 13 | bin/ 14 | gen/ 15 | out/ 16 | 17 | # Gradle files 18 | .gradle/ 19 | build/ 20 | 21 | # Local configuration file (sdk path, etc) 22 | local.properties 23 | 24 | # Proguard folder generated by Eclipse 25 | proguard/ 26 | 27 | # Log Files 28 | *.log 29 | 30 | # Android Studio Navigation editor temp files 31 | .navigation/ 32 | 33 | # Android Studio captures folder 34 | captures/ 35 | 36 | # IntelliJ 37 | *.iml 38 | .idea/ 39 | 40 | # Keystore files 41 | # Uncomment the following lines if you do not want to check your keystore files in. 42 | #*.jks 43 | #*.keystore 44 | 45 | # External native build folder generated in Android Studio 2.2 and later 46 | .externalNativeBuild 47 | 48 | # Google Services (e.g. APIs or Firebase) 49 | google-services.json 50 | 51 | # Freeline 52 | freeline.py 53 | freeline/ 54 | freeline_project_description.json 55 | 56 | # fastlane 57 | fastlane/report.xml 58 | fastlane/Preview.html 59 | fastlane/screenshots 60 | fastlane/test_output 61 | fastlane/readme.md 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenGLES2.0SamplesForAndroid 2 | 3 | 4 | 5 | 这是一个Android上的OpenGL ES 2.0 学习工程,我会不断地补充sample,并在关键代码处附上中英文注释。 6 | 7 | This is a OpenGL 2.0 learning project for Android, and I will continue to code and commit samples and write chinese and english commets for some import code. 8 | 9 | 10 | 11 | 在我的**掘金**上,我会发布相关的Android OpenGL ES 2.0教学文章,包括每个例子的对应的讲解文章,以及一些其它知识,欢迎关注:https://juejin.im/user/5afabe81f265da0b7b361036/posts 12 | 13 | I will publish some Android OpenGL ES 2.0 learning articles on my **juejin**, including the articles related to every samples in this project, and other knowledge. Welcome! Link: https://juejin.im/user/5afabe81f265da0b7b361036/posts 14 | 15 | 16 | 17 | 18 | 目前工程中有6个例子: 19 | 20 | Now, this project contains 6 samples. 21 | 22 | 23 | 24 | - **SampleHelloWorld** 25 | 26 | 一个简单的Hello World例子,渲染一个三角形 27 | 28 | A simple Hello World, render a triangle. 29 | 30 | https://juejin.im/post/5c8d0011f265da2d8c7e14a6 31 | 32 | 33 | 34 | - **Sample2Triangles** 35 | 36 | 渲染2个三角形。通过一次传递6个顶点,我们可以一次渲染2个三角形。 37 | 38 | A simple of Rendering 2 triangles. We can render 2 triangles at a time by setting 6 vertices. 39 | 40 | https://juejin.im/post/5c8e3652f265da67e20a3964 41 | 42 | 43 | 44 | - **SampleVertexShader** 45 | 46 | 这是一个使用vertex shader做顶点变换的例子,例子中将演示平移、缩放和旋转变换 47 | 48 | This sample demonstrates how to do vertex translation,scale and rotation using vertex shader. 49 | 50 | https://juejin.im/post/5c91c561f265da61246d5c57 51 | 52 | 53 | 54 | - **SampleFragmentShader** 55 | 56 | 这是一个利用fragment shader渲染彩色三角形例子 57 | 58 | This is a sample of using fragment shader to render a colorful triangle 59 | 60 | https://juejin.im/post/5c946bc4e51d45637f7a6434 61 | 62 | 63 | 64 | - **SampleDrawMode** 65 | 66 | 这是一个演示`GL_TRIANGLES`、`GL_TRIANGLE_STRIP`和`GL_TRIANGLE_FAN`三种绘制模式例子 67 | 68 | This sample demonstrates three commonly used draw modes, `GL_TRIANGLES`、`GL_TRIANGLE_STRIP` and `GL_TRIANGLE_FAN` 69 | 70 | https://juejin.im/post/5cbb1787e51d456e46603e2d 71 | 72 | 73 | 74 | - **SampleTexture** 75 | 76 | 这是使用纹理的例子 77 | 78 | This sample demonstrates how to use texture 79 | 80 | https://juejin.im/post/5ccbcdfe6fb9a0325031c1d0 81 | 82 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 28 9 | defaultConfig { 10 | applicationId "com.kenneycode" 11 | minSdkVersion 15 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName "1.0" 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | implementation fileTree(include: ['*.jar'], dir: 'libs') 26 | implementation 'com.android.support:appcompat-v7:28.0.0' 27 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 28 | implementation 'com.android.support:recyclerview-v7:28.0.0' 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 30 | implementation "org.jetbrains.kotlin:kotlin-reflect:1.2.71" 31 | } 32 | repositories { 33 | mavenCentral() 34 | } 35 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/assets/image_0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/assets/image_0.jpg -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/GlobalConstants.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode 2 | 3 | /** 4 | * 5 | * Coded by kenney 6 | * 7 | * http://www.github.com/kenneycode 8 | * 9 | * 全局常量 10 | * Global constants 11 | **/ 12 | 13 | class GlobalConstants { 14 | companion object { 15 | const val KEY_SAMPLE_INDEX = "sample_index" 16 | const val KEY_SAMPLE_NAME = "sample_name" 17 | } 18 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import android.support.v7.app.AppCompatActivity 6 | import android.support.v7.widget.LinearLayoutManager 7 | import android.support.v7.widget.RecyclerView 8 | import android.view.LayoutInflater 9 | import android.view.View 10 | import android.view.ViewGroup 11 | import android.widget.Button 12 | 13 | /** 14 | * 15 | * Coded by kenney 16 | * 17 | * http://www.github.com/kenneycode 18 | * 19 | * 20 | **/ 21 | 22 | class MainActivity : AppCompatActivity() { 23 | 24 | override fun onCreate(savedInstanceState: Bundle?) { 25 | super.onCreate(savedInstanceState) 26 | Util.context = applicationContext 27 | setContentView(R.layout.activity_main) 28 | val samplesList = findViewById(R.id.list) 29 | val layoutManager = LinearLayoutManager(this) 30 | layoutManager.orientation = LinearLayoutManager.VERTICAL 31 | samplesList.layoutManager = layoutManager 32 | samplesList.adapter = MyAdapter() 33 | } 34 | 35 | inner class MyAdapter : RecyclerView.Adapter() { 36 | 37 | private val sampleNames = 38 | arrayOf(resources.getString(R.string.sample_0), 39 | resources.getString(R.string.sample_1), 40 | resources.getString(R.string.sample_2), 41 | resources.getString(R.string.sample_3), 42 | resources.getString(R.string.sample_4), 43 | resources.getString(R.string.sample_5), 44 | resources.getString(R.string.sample_6)) 45 | 46 | override fun onCreateViewHolder(p0: ViewGroup, p1: Int): VH { 47 | val view = LayoutInflater.from(p0.context).inflate(R.layout.layout_sample_list_item, p0, false) 48 | return VH(view) 49 | } 50 | 51 | override fun getItemCount(): Int { 52 | return sampleNames.size 53 | } 54 | 55 | override fun onBindViewHolder(p0: VH, p1: Int) { 56 | p0.button.text = sampleNames[p1] 57 | p0.button.setOnClickListener { 58 | val intent = Intent(this@MainActivity, SimpleActivity::class.java) 59 | intent.putExtra(GlobalConstants.KEY_SAMPLE_INDEX, p1) 60 | intent.putExtra(GlobalConstants.KEY_SAMPLE_NAME, sampleNames[p1]) 61 | this@MainActivity.startActivity(intent) 62 | } 63 | } 64 | 65 | } 66 | 67 | inner class VH(itemView : View) : RecyclerView.ViewHolder(itemView) { 68 | var button : Button = itemView.findViewById(R.id.button) 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/SampleActivity.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import com.kenneycode.samples.fragment.* 6 | import com.kenneycode.samples.renderer.* 7 | 8 | /** 9 | * 10 | * Coded by kenney 11 | * 12 | * http://www.github.com/kenneycode 13 | * 14 | * Sample展示类 15 | * This sample activity 16 | * 17 | **/ 18 | 19 | class SimpleActivity : AppCompatActivity() { 20 | 21 | private val samples = 22 | arrayOf( 23 | SampleHelloWorld(), 24 | Sample2Triangles(), 25 | SampleVertexShader(), 26 | SampleFragmentShader(), 27 | SampleDrawMode(), 28 | SampleTexture(), 29 | SampleFrameBuffer() 30 | ) 31 | 32 | override fun onCreate(savedInstanceState: Bundle?) { 33 | super.onCreate(savedInstanceState) 34 | setContentView(R.layout.activity_sample) 35 | title = intent.getStringExtra(GlobalConstants.KEY_SAMPLE_NAME) 36 | val sampleIndex = intent.getIntExtra(GlobalConstants.KEY_SAMPLE_INDEX, -1) 37 | val transaction = supportFragmentManager.beginTransaction() 38 | transaction.replace(R.id.content, samples[sampleIndex]) 39 | transaction.commit() 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/Util.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode 2 | 3 | import android.content.Context 4 | import android.graphics.Bitmap 5 | import android.graphics.BitmapFactory 6 | import android.opengl.GLES30 7 | import android.util.Log 8 | 9 | /** 10 | * 11 | * Coded by kenney 12 | * 13 | * http://www.github.com/kenneycode 14 | * 15 | * 16 | **/ 17 | 18 | class Util { 19 | 20 | companion object { 21 | 22 | lateinit var context: Context 23 | 24 | fun decodeBitmapFromAssets(filename : String) : Bitmap { 25 | val options = BitmapFactory.Options() 26 | options.inSampleSize = 1 27 | val bitmap = BitmapFactory.decodeStream(context.assets.open(filename)) 28 | if (bitmap == null) { 29 | Log.e("debug", "bitmap decode fail, path = $filename") 30 | } 31 | return bitmap 32 | } 33 | 34 | fun checkGLError() { 35 | val error = GLES30.glGetError() 36 | if (error != GLES30.GL_NO_ERROR) { 37 | val hexErrorCode = Integer.toHexString(error) 38 | Log.e("debug", "glError: $hexErrorCode") 39 | throw RuntimeException("GLError") 40 | } 41 | } 42 | 43 | fun getScreenWidth() : Int { 44 | return 1440 45 | } 46 | 47 | fun getScreenHeight() : Int { 48 | return 2560 49 | } 50 | 51 | } 52 | 53 | 54 | 55 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/fragment/Sample2Triangles.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.fragment 2 | 3 | import android.opengl.GLSurfaceView 4 | import android.os.Bundle 5 | import android.support.v4.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import com.kenneycode.R 10 | import com.kenneycode.samples.renderer.Sample2TrianglesRenderer 11 | import com.kenneycode.samples.renderer.SampleHelloWorldRenderer 12 | 13 | /** 14 | * 15 | * Coded by kenney 16 | * 17 | * http://www.github.com/kenneycode 18 | * 19 | * 在第一个例子Hello World的基础上稍作修改,通过Sample2TrianglesRenderer渲染2个三角形的例子 20 | * This sample demonstrates how to render 2 triangles in Sample2TrianglesRenderer base on the code of our first sample 21 | * 22 | **/ 23 | 24 | class Sample2Triangles : Fragment() { 25 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 26 | val rootView = inflater.inflate(R.layout.fragment_common_sample, container, false) 27 | val glSurfaceView = rootView.findViewById(R.id.glsurfaceview) 28 | // 设置RGBA颜色缓冲、深度缓冲及stencil缓冲大小 29 | // Set the size of RGBA、depth and stencil buffer 30 | glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0) 31 | // 设置GL版本,这里设置为2.0 32 | // Set GL version, here I set it to 2.0 33 | glSurfaceView.setEGLContextClientVersion(2) 34 | // 设置对应sample的渲染器 35 | // Set the corresponding sample renderer 36 | glSurfaceView.setRenderer(Sample2TrianglesRenderer()) 37 | return rootView 38 | } 39 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/fragment/SampleDrawMode.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.fragment 2 | 3 | import android.opengl.GLSurfaceView 4 | import android.os.Bundle 5 | import android.support.v4.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import com.kenneycode.R 10 | import com.kenneycode.samples.renderer.SampleDrawModeRenderer 11 | 12 | /** 13 | * 14 | * Coded by kenney 15 | * 16 | * http://www.github.com/kenneycode 17 | * 18 | * 这是一个在SampleDrawModeRenderer中演示不同绘制模式的例子 19 | * This sample demonstrates the difference between the draw modes in SampleDrawModeRenderer 20 | * 21 | **/ 22 | 23 | class SampleDrawMode : Fragment() { 24 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 25 | val rootView = inflater.inflate(R.layout.fragment_sample_draw_mode, container, false) 26 | val glSurfaceView = rootView.findViewById(R.id.glsurfaceview) 27 | // 设置RGBA颜色缓冲、深度缓冲及stencil缓冲大小 28 | // Set the size of RGBA、depth and stencil buffer 29 | glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0) 30 | // 设置GL版本,这里设置为2.0 31 | // Set GL version, here I set it to 2.0 32 | glSurfaceView.setEGLContextClientVersion(2) 33 | // 设置对应sample的渲染器 34 | // Set the corresponding sample renderer 35 | glSurfaceView.setRenderer(SampleDrawModeRenderer(rootView)) 36 | return rootView 37 | } 38 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/fragment/SampleFragmentShader.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.fragment 2 | 3 | import android.opengl.GLSurfaceView 4 | import android.os.Bundle 5 | import android.support.v4.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import com.kenneycode.R 10 | import com.kenneycode.samples.renderer.SampleFragmentShaderRenderer 11 | 12 | /** 13 | * 14 | * Coded by kenney 15 | * 16 | * http://www.github.com/kenneycode 17 | * 18 | * 这是一个在SampleFragmentShaderRenderer中利用fragment shader渲染彩色三角形例子 19 | * This is a sample of using fragment shader to render a colorful triangle in SampleFragmentShaderRenderer 20 | * 21 | **/ 22 | 23 | class SampleFragmentShader : Fragment() { 24 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 25 | val rootView = inflater.inflate(R.layout.fragment_common_sample, container, false) 26 | val glSurfaceView = rootView.findViewById(R.id.glsurfaceview) 27 | // 设置RGBA颜色缓冲、深度缓冲及stencil缓冲大小 28 | // Set the size of RGBA、depth and stencil buffer 29 | glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0) 30 | // 设置GL版本,这里设置为2.0 31 | // Set GL version, here I set it to 2.0 32 | glSurfaceView.setEGLContextClientVersion(2) 33 | // 设置对应sample的渲染器 34 | // Set the corresponding sample renderer 35 | glSurfaceView.setRenderer(SampleFragmentShaderRenderer()) 36 | return rootView 37 | } 38 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/fragment/SampleFrameBuffer.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.fragment 2 | 3 | import android.opengl.GLSurfaceView 4 | import android.os.Bundle 5 | import android.support.v4.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import com.kenneycode.R 10 | import com.kenneycode.samples.renderer.SampleFrameBufferRenderer 11 | import com.kenneycode.samples.renderer.SampleTextureRenderer 12 | 13 | /** 14 | * 15 | * Coded by kenney 16 | * 17 | * http://www.github.com/kenneycode 18 | * 19 | * 这是一个使用帧缓存的例子 20 | * This sample demonstrates how to use frame buffer. 21 | * 22 | **/ 23 | 24 | class SampleFrameBuffer : Fragment() { 25 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 26 | val rootView = inflater.inflate(R.layout.fragment_common_sample, container, false) 27 | val glSurfaceView = rootView.findViewById(R.id.glsurfaceview) 28 | // 设置RGBA颜色缓冲、深度缓冲及stencil缓冲大小 29 | // Set the size of RGBA、depth and stencil buffer 30 | glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0) 31 | // 设置GL版本,这里设置为2.0 32 | // Set GL version, here I set it to 2.0 33 | glSurfaceView.setEGLContextClientVersion(2) 34 | // 设置对应sample的渲染器 35 | // Set the corresponding sample renderer 36 | glSurfaceView.setRenderer(SampleFrameBufferRenderer()) 37 | return rootView 38 | } 39 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/fragment/SampleHelloWorld.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.fragment 2 | 3 | import android.opengl.GLSurfaceView 4 | import android.os.Bundle 5 | import android.support.v4.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import com.kenneycode.R 10 | import com.kenneycode.samples.renderer.SampleHelloWorldRenderer 11 | 12 | /** 13 | * 14 | * Coded by kenney 15 | * 16 | * http://www.github.com/kenneycode 17 | * 18 | * 这是一个Hello World例子,通过SampleHelloWorldRenderer渲染一个最简单的三角形 19 | * This is the first sample Hello World, and it will show you how to render a simplest triangle in SampleHelloWorldRenderer 20 | * 21 | **/ 22 | 23 | class SampleHelloWorld : Fragment() { 24 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 25 | val rootView = inflater.inflate(R.layout.fragment_common_sample, container, false) 26 | val glSurfaceView = rootView.findViewById(R.id.glsurfaceview) 27 | // 设置RGBA颜色缓冲、深度缓冲及stencil缓冲大小 28 | // Set the size of RGBA、depth and stencil buffer 29 | glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0) 30 | // 设置GL版本,这里设置为2.0 31 | // Set GL version, here I set it to 2.0 32 | glSurfaceView.setEGLContextClientVersion(2) 33 | // 设置对应sample的渲染器 34 | // Set the corresponding sample renderer 35 | glSurfaceView.setRenderer(SampleHelloWorldRenderer()) 36 | return rootView 37 | } 38 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/fragment/SampleTexture.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.fragment 2 | 3 | import android.opengl.GLSurfaceView 4 | import android.os.Bundle 5 | import android.support.v4.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import com.kenneycode.R 10 | import com.kenneycode.samples.renderer.SampleTextureRenderer 11 | 12 | /** 13 | * 14 | * Coded by kenney 15 | * 16 | * http://www.github.com/kenneycode 17 | * 18 | * 这是一个渲染纹理的例子 19 | * This sample demonstrates how to use texture. 20 | * 21 | **/ 22 | 23 | class SampleTexture : Fragment() { 24 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 25 | val rootView = inflater.inflate(R.layout.fragment_common_sample, container, false) 26 | val glSurfaceView = rootView.findViewById(R.id.glsurfaceview) 27 | // 设置RGBA颜色缓冲、深度缓冲及stencil缓冲大小 28 | // Set the size of RGBA、depth and stencil buffer 29 | glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0) 30 | // 设置GL版本,这里设置为2.0 31 | // Set GL version, here I set it to 2.0 32 | glSurfaceView.setEGLContextClientVersion(2) 33 | // 设置对应sample的渲染器 34 | // Set the corresponding sample renderer 35 | glSurfaceView.setRenderer(SampleTextureRenderer()) 36 | return rootView 37 | } 38 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/fragment/SampleVertexShader.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.fragment 2 | 3 | import android.opengl.GLSurfaceView 4 | import android.os.Bundle 5 | import android.support.v4.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import com.kenneycode.R 10 | import com.kenneycode.samples.renderer.SampleVertexShaderRenderer 11 | 12 | /** 13 | * 14 | * Coded by kenney 15 | * 16 | * http://www.github.com/kenneycode 17 | * 18 | * 这是一个在SampleVertexShaderRenderer使用vertex shader做顶点变换的例子,例子中将演示平移、缩放和旋转变换 19 | * This sample demonstrates how to do vertex translation,scale and rotation using vertex shader in SampleVertexShaderRenderer 20 | * 21 | **/ 22 | 23 | class SampleVertexShader : Fragment() { 24 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 25 | val rootView = inflater.inflate(R.layout.fragment_common_sample, container, false) 26 | val glSurfaceView = rootView.findViewById(R.id.glsurfaceview) 27 | // 设置RGBA颜色缓冲、深度缓冲及stencil缓冲大小 28 | // Set the size of RGBA、depth and stencil buffer 29 | glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0) 30 | // 设置GL版本,这里设置为2.0 31 | // Set GL version, here I set it to 2.0 32 | glSurfaceView.setEGLContextClientVersion(2) 33 | // 设置对应sample的渲染器 34 | // Set the corresponding sample renderer 35 | glSurfaceView.setRenderer(SampleVertexShaderRenderer()) 36 | return rootView 37 | } 38 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/renderer/Sample2TrianglesRenderer.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.renderer 2 | 3 | import android.opengl.GLES20 4 | import android.opengl.GLSurfaceView 5 | import java.nio.ByteBuffer 6 | import java.nio.ByteOrder 7 | import javax.microedition.khronos.egl.EGLConfig 8 | import javax.microedition.khronos.opengles.GL10 9 | 10 | /** 11 | * 12 | * Coded by kenney 13 | * 14 | * http://www.github.com/kenneycode 15 | * 16 | * 在第一个例子Hello World的基础上稍作修改,渲染2个三角形的例子 17 | * This sample demonstrates how to render 2 triangles base on the code of our first sample 18 | * 19 | **/ 20 | 21 | class Sample2TrianglesRenderer : GLSurfaceView.Renderer { 22 | 23 | private val vertexShaderCode = 24 | "precision mediump float;\n" + 25 | "attribute vec4 a_Position;\n" + 26 | "void main() {\n" + 27 | " gl_Position = a_Position;\n" + 28 | "}" 29 | 30 | private val fragmentShaderCode = 31 | "precision mediump float;\n" + 32 | "void main() {\n" + 33 | " gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);\n" + 34 | "}" 35 | 36 | private var glSurfaceViewWidth = 0 37 | private var glSurfaceViewHeight = 0 38 | 39 | // 三角形顶点数据 40 | // The vertex data of a triangle 41 | private val vertexData = floatArrayOf(-0.5f, 1f, -1f, 0f, 0f, 0f, 0.5f, 0f, 0f, -1f, 1f, -1f) 42 | 43 | // 每个顶点的成份数 44 | // The num of components of per vertex 45 | private val VERTEX_COMPONENT_COUNT = 2 46 | 47 | override fun onDrawFrame(gl: GL10?) { 48 | // 设置清屏颜色 49 | // Set the color which the screen will be cleared to 50 | GLES20.glClearColor(0.9f, 0.9f, 0.9f, 1f) 51 | 52 | // 清屏 53 | // Clear the screen 54 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT) 55 | 56 | // 设置视口,这里设置为整个GLSurfaceView区域 57 | // Set the viewport to the full GLSurfaceView 58 | GLES20.glViewport(0, 0, glSurfaceViewWidth, glSurfaceViewHeight) 59 | 60 | // 调用draw方法用TRIANGLES的方式执行渲染,顶点数量为3个 61 | // Call the draw method with GL_TRIANGLES to render 3 vertices 62 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexData.size / VERTEX_COMPONENT_COUNT) 63 | } 64 | 65 | override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { 66 | // 记录GLSurfaceView的宽高 67 | // Record the width and height of the GLSurfaceView 68 | glSurfaceViewWidth = width 69 | glSurfaceViewHeight = height 70 | } 71 | 72 | override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { 73 | // 创建GL程序 74 | // Create GL program 75 | val programId = GLES20.glCreateProgram() 76 | 77 | // 加载、编译vertex shader和fragment shader 78 | // Load and compile vertex shader and fragment shader 79 | val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER) 80 | val fragmentShader= GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER) 81 | GLES20.glShaderSource(vertexShader, vertexShaderCode) 82 | GLES20.glShaderSource(fragmentShader, fragmentShaderCode) 83 | GLES20.glCompileShader(vertexShader) 84 | GLES20.glCompileShader(fragmentShader) 85 | 86 | // 将shader程序附着到GL程序上 87 | // Attach the compiled shaders to the GL program 88 | GLES20.glAttachShader(programId, vertexShader) 89 | GLES20.glAttachShader(programId, fragmentShader) 90 | 91 | // 链接GL程序 92 | // Link the GL program 93 | GLES20.glLinkProgram(programId) 94 | 95 | // 将三角形顶点数据放入buffer中 96 | // Put the triangle vertex data into the buffer 97 | val buffer = ByteBuffer.allocateDirect(vertexData.size * java.lang.Float.SIZE) 98 | .order(ByteOrder.nativeOrder()) 99 | .asFloatBuffer() 100 | buffer.put(vertexData) 101 | buffer.position(0) 102 | 103 | // 应用GL程序 104 | // Use the GL program 105 | GLES20.glUseProgram(programId) 106 | 107 | // 获取字段a_Position在shader中的位置 108 | // Get the location of a_Position in the shader 109 | val location = GLES20.glGetAttribLocation(programId, "a_Position") 110 | 111 | // 启动对应位置的参数 112 | // Enable the parameter of the location 113 | GLES20.glEnableVertexAttribArray(location) 114 | 115 | // 指定a_Position所使用的顶点数据 116 | // Specify the vertex data of a_Position 117 | GLES20.glVertexAttribPointer(location, VERTEX_COMPONENT_COUNT, GLES20.GL_FLOAT, false,0, buffer) 118 | } 119 | 120 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/renderer/SampleCheckError.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.renderer 2 | 3 | import android.opengl.GLES20 4 | import android.opengl.GLSurfaceView 5 | import android.util.Log 6 | import java.nio.ByteBuffer 7 | import java.nio.ByteOrder 8 | import javax.microedition.khronos.egl.EGLConfig 9 | import javax.microedition.khronos.opengles.GL10 10 | 11 | /** 12 | * 13 | * Coded by kenney 14 | * 15 | * http://www.github.com/kenneycode 16 | * 17 | * 这是一个Hello World例子,渲染一个最简单的三角形 18 | * This is the first sample Hello World, and it will show you how to render a simplest triangle 19 | * 20 | **/ 21 | 22 | class SampleCheckError : GLSurfaceView.Renderer { 23 | 24 | companion object { 25 | val LOG_TAG = SampleCheckError::class.simpleName 26 | } 27 | 28 | 29 | private var glSurfaceViewWidth = 0 30 | private var glSurfaceViewHeight = 0 31 | 32 | // 三角形顶点数据 33 | // The vertex data of a triangle 34 | private val vertexData = floatArrayOf(0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f) 35 | 36 | // 每个顶点的成份数 37 | // The num of components of per vertex 38 | private val VERTEX_COMPONENT_COUNT = 2 39 | 40 | override fun onDrawFrame(gl: GL10?) { 41 | // 设置视口,这里设置为整个GLSurfaceView区域 42 | // Set the viewport to the full GLSurfaceView 43 | GLES20.glViewport(0, 0, glSurfaceViewWidth, glSurfaceViewHeight) 44 | 45 | // 调用draw方法用TRIANGLES的方式执行渲染,顶点数量为3个 46 | // Call the draw method with GL_TRIANGLES to render 3 vertices 47 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexData.size / VERTEX_COMPONENT_COUNT) 48 | } 49 | 50 | override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { 51 | // 记录GLSurfaceView的宽高 52 | // Record the width and height of the GLSurfaceView 53 | glSurfaceViewWidth = width 54 | glSurfaceViewHeight = height 55 | } 56 | 57 | override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { 58 | // 创建GL程序 59 | // Create GL program 60 | val programId = GLES20.glCreateProgram() 61 | 62 | // 加载、编译vertex shader和fragment shader 63 | // Load and compile vertex shader and fragment shader 64 | val vertexShaderCodeWrong = 65 | "precision mediump float;\n" + 66 | "attribute vec4 a_Position;\n" + 67 | "void main() {\n" + 68 | " gl_Posss ition = a_Position;\n" + 69 | "}" 70 | 71 | val fragmentShaderCode = 72 | "precision mediump float;\n" + 73 | "void main() {\n" + 74 | " gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);\n" + 75 | "}" 76 | val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER) 77 | val fragmentShader= GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER) 78 | GLES20.glShaderSource(vertexShader, vertexShaderCodeWrong) 79 | checkErrorForGL() 80 | GLES20.glShaderSource(fragmentShader, fragmentShaderCode) 81 | checkErrorForGL() 82 | GLES20.glCompileShader(vertexShader) 83 | checkErrorForGL() 84 | val compileStatus = intArrayOf(1) 85 | GLES20.glGetShaderiv(vertexShader, GLES20.GL_COMPILE_STATUS, compileStatus, 0) 86 | if (compileStatus[0] != 0) { 87 | val info = GLES20.glGetShaderInfoLog(vertexShader) 88 | Log.e(LOG_TAG, "shader compile info: $info") 89 | } 90 | GLES20.glCompileShader(fragmentShader) 91 | 92 | // 将shader程序附着到GL程序上 93 | // Attach the compiled shaders to the GL program 94 | GLES20.glAttachShader(programId, vertexShader) 95 | GLES20.glAttachShader(programId, fragmentShader) 96 | 97 | // 链接GL程序 98 | // Link the GL program 99 | GLES20.glLinkProgram(programId) 100 | 101 | // 将三角形顶点数据放入buffer中 102 | // Put the triangle vertex data into the buffer 103 | val buffer = ByteBuffer.allocateDirect(vertexData.size * java.lang.Float.SIZE) 104 | .order(ByteOrder.nativeOrder()) 105 | .asFloatBuffer() 106 | buffer.put(vertexData) 107 | buffer.position(0) 108 | 109 | // 应用GL程序 110 | // Use the GL program 111 | GLES20.glUseProgram(programId) 112 | 113 | // 获取字段a_Position在shader中的位置 114 | // Get the location of a_Position in the shader 115 | val location = GLES20.glGetAttribLocation(programId, "a_Position") 116 | 117 | // 启动对应位置的参数 118 | // Enable the parameter of the location 119 | GLES20.glEnableVertexAttribArray(location) 120 | 121 | // 指定a_Position所使用的顶点数据 122 | // Specify the vertex data of a_Position 123 | GLES20.glVertexAttribPointer(location, 2, GLES20.GL_FLOAT, false,0, buffer) 124 | } 125 | 126 | fun checkErrorForGL() { 127 | val error = GLES20.glGetError() 128 | if (error != GLES20.GL_NO_ERROR) { 129 | val hexErrorCode = Integer.toHexString(error) 130 | val s = "gl Error: $hexErrorCode" 131 | Log.e(LOG_TAG, s) 132 | throw RuntimeException(s) 133 | } 134 | } 135 | 136 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/renderer/SampleDrawModeRenderer.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.renderer 2 | 3 | import android.opengl.GLES20 4 | import android.opengl.GLSurfaceView 5 | import android.view.View 6 | import com.kenneycode.R 7 | import kotlinx.android.synthetic.main.fragment_sample_draw_mode.view.* 8 | import java.nio.ByteBuffer 9 | import java.nio.ByteOrder 10 | import javax.microedition.khronos.egl.EGLConfig 11 | import javax.microedition.khronos.opengles.GL10 12 | 13 | /** 14 | * 15 | * Coded by kenney 16 | * 17 | * http://www.github.com/kenneycode 18 | * 19 | * 这是一个演示不同绘制模式的例子 20 | * This sample demonstrates the difference between the draw modes 21 | * 22 | **/ 23 | 24 | class SampleDrawModeRenderer : GLSurfaceView.Renderer { 25 | 26 | private val vertexShaderCode = 27 | "precision mediump float;\n" + 28 | "attribute vec4 a_Position;\n" + 29 | "void main() {\n" + 30 | " gl_Position = a_Position;\n" + 31 | "}" 32 | 33 | private val fragmentShaderCode = 34 | "precision mediump float;\n" + 35 | "void main() {\n" + 36 | " gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);\n" + 37 | "}" 38 | 39 | private var glSurfaceViewWidth = 0 40 | private var glSurfaceViewHeight = 0 41 | 42 | // 三角形顶点数据 43 | // The vertex data of a triangle 44 | private val vertexData = floatArrayOf(0f, 0f, -0.5f, 0f, 0f, 0.5f, 0.5f, 0f, 0f, -0.5f, -0.5f, -0.5f) 45 | 46 | // 每个顶点的成份数 47 | // The num of components of per vertex 48 | private val VERTEX_COMPONENT_COUNT = 2 49 | 50 | 51 | private var drawMode = GLES20.GL_TRIANGLES 52 | 53 | constructor(rootView : View) { 54 | rootView.findViewById(R.id.button_draw_mode_triangles).setOnClickListener { 55 | drawMode = GLES20.GL_TRIANGLES 56 | } 57 | rootView.findViewById(R.id.button_draw_mode_triangle_stripe).setOnClickListener { 58 | drawMode = GLES20.GL_TRIANGLE_STRIP 59 | } 60 | rootView.findViewById(R.id.button_draw_mode_triangle_fan).setOnClickListener { 61 | drawMode = GLES20.GL_TRIANGLE_FAN 62 | } 63 | } 64 | 65 | override fun onDrawFrame(gl: GL10?) { 66 | GLES20.glClearColor(0.9f, 0.9f, 0.9f, 1f) 67 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT) 68 | // 设置视口,这里设置为整个GLSurfaceView区域 69 | // Set the viewport to the full GLSurfaceView 70 | GLES20.glViewport(0, 0, glSurfaceViewWidth, glSurfaceViewHeight) 71 | 72 | // 调用draw方法用TRIANGLES的方式执行渲染,顶点数量为3个 73 | // Call the draw method with GL_TRIANGLES to render 3 vertices 74 | GLES20.glDrawArrays(drawMode, 0, vertexData.size / VERTEX_COMPONENT_COUNT) 75 | } 76 | 77 | override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { 78 | // 记录GLSurfaceView的宽高 79 | // Record the width and height of the GLSurfaceView 80 | glSurfaceViewWidth = width 81 | glSurfaceViewHeight = height 82 | } 83 | 84 | override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { 85 | // 创建GL程序 86 | // Create GL program 87 | val programId = GLES20.glCreateProgram() 88 | 89 | // 加载、编译vertex shader和fragment shader 90 | // Load and compile vertex shader and fragment shader 91 | val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER) 92 | val fragmentShader= GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER) 93 | GLES20.glShaderSource(vertexShader, vertexShaderCode) 94 | GLES20.glShaderSource(fragmentShader, fragmentShaderCode) 95 | GLES20.glCompileShader(vertexShader) 96 | GLES20.glCompileShader(fragmentShader) 97 | 98 | // 将shader程序附着到GL程序上 99 | // Attach the compiled shaders to the GL program 100 | GLES20.glAttachShader(programId, vertexShader) 101 | GLES20.glAttachShader(programId, fragmentShader) 102 | 103 | // 链接GL程序 104 | // Link the GL program 105 | GLES20.glLinkProgram(programId) 106 | 107 | // 将三角形顶点数据放入buffer中 108 | // Put the triangle vertex data into the buffer 109 | val buffer = ByteBuffer.allocateDirect(vertexData.size * java.lang.Float.SIZE) 110 | .order(ByteOrder.nativeOrder()) 111 | .asFloatBuffer() 112 | buffer.put(vertexData) 113 | buffer.position(0) 114 | 115 | // 应用GL程序 116 | // Use the GL program 117 | GLES20.glUseProgram(programId) 118 | 119 | // 获取字段a_Position在shader中的位置 120 | // Get the location of a_Position in the shader 121 | val location = GLES20.glGetAttribLocation(programId, "a_Position") 122 | 123 | // 启动对应位置的参数 124 | // Enable the parameter of the location 125 | GLES20.glEnableVertexAttribArray(location) 126 | 127 | // 指定a_Position所使用的顶点数据 128 | // Specify the vertex data of a_Position 129 | GLES20.glVertexAttribPointer(location, VERTEX_COMPONENT_COUNT, GLES20.GL_FLOAT, false,0, buffer) 130 | } 131 | 132 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/renderer/SampleFragmentShaderRenderer.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.renderer 2 | 3 | import android.opengl.GLES20 4 | import android.opengl.GLSurfaceView 5 | import java.nio.ByteBuffer 6 | import java.nio.ByteOrder 7 | import javax.microedition.khronos.egl.EGLConfig 8 | import javax.microedition.khronos.opengles.GL10 9 | 10 | /** 11 | * 12 | * Coded by kenney 13 | * 14 | * http://www.github.com/kenneycode 15 | * 16 | * 这是一个利用fragment shader渲染彩色三角形例子 17 | * This is a sample of using fragment shader to render a colorful triangle 18 | * 19 | **/ 20 | 21 | class SampleFragmentShaderRenderer : GLSurfaceView.Renderer { 22 | 23 | private val vertexShaderCode = 24 | "precision mediump float;\n" + 25 | "attribute vec4 a_Position;\n" + 26 | "attribute vec4 a_Color;\n" + 27 | "varying vec4 v_Color;\n" + 28 | "void main() {\n" + 29 | " v_Color = a_Color;\n" + 30 | " gl_Position = a_Position;\n" + 31 | "}" 32 | 33 | private val fragmentShaderCode = 34 | "precision mediump float;\n" + 35 | "varying vec4 v_Color;\n" + 36 | "void main() {\n" + 37 | " gl_FragColor = v_Color;\n" + 38 | "}" 39 | 40 | private var glSurfaceViewWidth = 0 41 | private var glSurfaceViewHeight = 0 42 | 43 | // 三角形顶点数据 44 | // The vertex data of a triangle 45 | private val vertexData = floatArrayOf(0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f) 46 | 47 | 48 | private val colorData = floatArrayOf( 49 | 1.0f, 0.0f, 0.0f, 1.0f, 50 | 0.0f, 1.0f, 0.0f, 1.0f, 51 | 0.0f, 0.0f, 1.0f, 1.0f) 52 | 53 | // 每个顶点的成份数 54 | // The num of components of per vertex 55 | private val VERTEX_COMPONENT_COUNT = 2 56 | 57 | // 每个颜色的成份数(RGBA) 58 | // The num of components of per color(RGBA) 59 | private val COLOR_COMPONENT_COUNT = 4 60 | 61 | override fun onDrawFrame(gl: GL10?) { 62 | // 设置清屏颜色 63 | // Set the color which the screen will be cleared to 64 | GLES20.glClearColor(0.9f, 0.9f, 0.9f, 1f) 65 | 66 | // 清屏 67 | // Clear the screen 68 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT) 69 | 70 | // 设置视口,这里设置为整个GLSurfaceView区域 71 | // Set the viewport to the full GLSurfaceView 72 | GLES20.glViewport(0, 0, glSurfaceViewWidth, glSurfaceViewHeight) 73 | 74 | // 调用draw方法用TRIANGLES的方式执行渲染,顶点数量为3个 75 | // Call the draw method with GL_TRIANGLES to render 3 vertices 76 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexData.size / VERTEX_COMPONENT_COUNT) 77 | } 78 | 79 | override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { 80 | // 记录GLSurfaceView的宽高 81 | // Record the width and height of the GLSurfaceView 82 | glSurfaceViewWidth = width 83 | glSurfaceViewHeight = height 84 | } 85 | 86 | override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { 87 | // 创建GL程序 88 | // Create GL program 89 | val programId = GLES20.glCreateProgram() 90 | 91 | // 加载、编译vertex shader和fragment shader 92 | // Load and compile vertex shader and fragment shader 93 | val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER) 94 | val fragmentShader= GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER) 95 | GLES20.glShaderSource(vertexShader, vertexShaderCode) 96 | GLES20.glShaderSource(fragmentShader, fragmentShaderCode) 97 | GLES20.glCompileShader(vertexShader) 98 | GLES20.glCompileShader(fragmentShader) 99 | 100 | // 将shader程序附着到GL程序上 101 | // Attach the compiled shaders to the GL program 102 | GLES20.glAttachShader(programId, vertexShader) 103 | GLES20.glAttachShader(programId, fragmentShader) 104 | 105 | // 链接GL程序 106 | // Link the GL program 107 | GLES20.glLinkProgram(programId) 108 | 109 | // 将三角形顶点数据放入buffer中 110 | // Put the triangle vertex data into the buffer 111 | val vertexDataBuffer = ByteBuffer.allocateDirect(vertexData.size * java.lang.Float.SIZE) 112 | .order(ByteOrder.nativeOrder()) 113 | .asFloatBuffer() 114 | vertexDataBuffer.put(vertexData) 115 | vertexDataBuffer.position(0) 116 | 117 | // 应用GL程序 118 | // Use the GL program 119 | GLES20.glUseProgram(programId) 120 | 121 | // 获取字段a_Position在shader中的位置 122 | // Get the location of a_Position in the shader 123 | val aPositionLocation = GLES20.glGetAttribLocation(programId, "a_Position") 124 | 125 | // 启动对应位置的参数 126 | // Enable the parameter of the location 127 | GLES20.glEnableVertexAttribArray(aPositionLocation) 128 | 129 | // 指定a_Position所使用的顶点数据 130 | // Specify the vertex data of a_Position 131 | GLES20.glVertexAttribPointer(aPositionLocation, VERTEX_COMPONENT_COUNT, GLES20.GL_FLOAT, false,0, vertexDataBuffer) 132 | 133 | // 将颜色数据放入buffer中 134 | // Put the color data into the buffer 135 | val colorDataBuffer = ByteBuffer.allocateDirect(colorData.size * java.lang.Float.SIZE) 136 | .order(ByteOrder.nativeOrder()) 137 | .asFloatBuffer() 138 | colorDataBuffer.put(colorData) 139 | colorDataBuffer.position(0) 140 | 141 | // 获取字段a_Color在shader中的位置 142 | // Get the location of a_Color in the shader 143 | val aColorLocation = GLES20.glGetAttribLocation(programId, "a_Color") 144 | 145 | // 启动对应位置的参数 146 | // Enable the parameter of the location 147 | GLES20.glEnableVertexAttribArray(aColorLocation) 148 | 149 | // 指定a_Color所使用的顶点数据 150 | // Specify the vertex data of a_Color 151 | GLES20.glVertexAttribPointer(aColorLocation, COLOR_COMPONENT_COUNT, GLES20.GL_FLOAT, false,0, colorDataBuffer) 152 | } 153 | 154 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/renderer/SampleFrameBufferRenderer.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.renderer 2 | 3 | import android.opengl.GLES20 4 | import android.opengl.GLES30 5 | import android.opengl.GLSurfaceView 6 | import android.opengl.GLUtils 7 | import com.kenneycode.Util 8 | import java.nio.ByteBuffer 9 | import java.nio.ByteOrder 10 | import java.nio.FloatBuffer 11 | import javax.microedition.khronos.egl.EGLConfig 12 | import javax.microedition.khronos.opengles.GL10 13 | 14 | /** 15 | * 16 | * Coded by kenney 17 | * 18 | * http://www.github.com/kenneycode 19 | * 20 | * 这是一个使用帧缓存的例子 21 | * This sample demonstrates how to use frame buffer. 22 | * 23 | **/ 24 | 25 | class SampleFrameBufferRenderer : GLSurfaceView.Renderer { 26 | 27 | private val vertexShaderCode = 28 | "precision mediump float;\n" + 29 | "attribute vec4 a_position;\n" + 30 | "attribute vec2 a_textureCoordinate;\n" + 31 | "varying vec2 v_textureCoordinate;\n" + 32 | "void main() {\n" + 33 | " v_textureCoordinate = a_textureCoordinate;\n" + 34 | " gl_Position = a_position;\n" + 35 | "}" 36 | 37 | private val fragmentShaderCode0 = 38 | "precision mediump float;\n" + 39 | "varying vec2 v_textureCoordinate;\n" + 40 | "uniform sampler2D u_texture;\n" + 41 | "void main() {\n" + 42 | " vec4 color = texture2D(u_texture, v_textureCoordinate);\n" + 43 | " color.b = 0.5;\n" + 44 | " gl_FragColor = color;\n" + 45 | "}" 46 | 47 | private val fragmentShaderCode1 = 48 | "precision mediump float;\n" + 49 | "varying vec2 v_textureCoordinate;\n" + 50 | "uniform sampler2D u_texture;\n" + 51 | "void main() {\n" + 52 | " float offset = 0.005;\n" + 53 | " vec4 color = texture2D(u_texture, v_textureCoordinate) * 0.11111;\n" + 54 | " color += texture2D(u_texture, vec2(v_textureCoordinate.x - offset, v_textureCoordinate.y)) * 0.11111;\n" + 55 | " color += texture2D(u_texture, vec2(v_textureCoordinate.x + offset, v_textureCoordinate.y)) * 0.11111;\n" + 56 | " color += texture2D(u_texture, vec2(v_textureCoordinate.x - offset * 2.0, v_textureCoordinate.y)) * 0.11111;\n" + 57 | " color += texture2D(u_texture, vec2(v_textureCoordinate.x + offset * 2.0, v_textureCoordinate.y)) * 0.11111;\n" + 58 | " color += texture2D(u_texture, vec2(v_textureCoordinate.x - offset * 3.0, v_textureCoordinate.y)) * 0.11111;\n" + 59 | " color += texture2D(u_texture, vec2(v_textureCoordinate.x + offset * 3.0, v_textureCoordinate.y)) * 0.11111;\n" + 60 | " color += texture2D(u_texture, vec2(v_textureCoordinate.x - offset * 4.0, v_textureCoordinate.y)) * 0.11111;\n" + 61 | " color += texture2D(u_texture, vec2(v_textureCoordinate.x + offset * 4.0, v_textureCoordinate.y)) * 0.11111;\n" + 62 | " gl_FragColor = color;\n" + 63 | "}" 64 | 65 | // GLSurfaceView的宽高 66 | // The width and height of GLSurfaceView 67 | private var glSurfaceViewWidth = 0 68 | private var glSurfaceViewHeight = 0 69 | 70 | // 纹理顶点数据 71 | // The vertex data of the texture 72 | private val vertexData = floatArrayOf(-1f, -1f, -1f, 1f, 1f, 1f, -1f, -1f, 1f, 1f, 1f, -1f) 73 | private val VERTEX_COMPONENT_COUNT = 2 74 | private lateinit var vertexDataBuffer : FloatBuffer 75 | 76 | // 纹理坐标 77 | // The texture coordinate 78 | private val textureCoordinateData0 = floatArrayOf(0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, 1f, 0f, 1f, 1f) 79 | private val textureCoordinateData1 = floatArrayOf(0f, 0f, 0f, 1f, 1f, 1f, 0f, 0f, 1f, 1f, 1f, 0f) 80 | private lateinit var textureCoordinateDataBuffer0 : FloatBuffer 81 | private lateinit var textureCoordinateDataBuffer1 : FloatBuffer 82 | private val TEXTURE_COORDINATE_COMPONENT_COUNT = 2 83 | 84 | // 2个GL Program 85 | // two GL Programs 86 | private var programId0 = 0 87 | private var programId1 = 0 88 | 89 | // 帧缓存 90 | // frame buffer 91 | private var frameBuffer = 0 92 | 93 | // 帧缓绑定的texture 94 | // the texture bind on the frame buffer 95 | private var frameBufferTexture = 0 96 | 97 | // 图片texture 98 | // image texture 99 | private var imageTexture = 0 100 | 101 | 102 | override fun onDrawFrame(gl: GL10?) { 103 | 104 | // 绑定第0个GL Program 105 | // Bind GL program 0 106 | bindGLProgram(programId0, imageTexture, textureCoordinateDataBuffer0) 107 | 108 | // 绑定frame buffer 109 | // Bind the frame buffer 110 | bindFrameBuffer(frameBuffer) 111 | 112 | // 执行渲染,渲染效果为将图片的蓝色通道全部设为0.5 113 | // Perform rendering, and we can get the result of blue channel set to 0.5 114 | render() 115 | 116 | // 绑定第1个GL Program 117 | // Bind GL program 1 118 | bindGLProgram(programId1, frameBufferTexture, textureCoordinateDataBuffer1) 119 | 120 | // 绑定0号frame buffer 121 | // Bind the 0# frame buffer 122 | bindFrameBuffer(0) 123 | 124 | // 执行渲染,渲染效果水平方向的模糊 125 | // Perform rendering, and we can get the result of horizontal blur base on the previous result 126 | render() 127 | 128 | } 129 | 130 | override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { 131 | 132 | // 记录GLSurfaceView的宽高 133 | // Record the width and height of the GLSurfaceView 134 | glSurfaceViewWidth = width 135 | glSurfaceViewHeight = height 136 | 137 | 138 | // 初始化frame buffer 139 | // Init the frame buffer 140 | initFrameBuffer(width, height) 141 | 142 | } 143 | 144 | override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { 145 | 146 | // 初始化坐标、图片数据 147 | // Init the coordinates and image texture 148 | initData() 149 | 150 | // 创建2个GL Program,第一个将图片的蓝色通道全部设为0.5,第二做水平方向模糊 151 | // Create two GL programs, and one is used for set the blue channel to 0.5, while the other is used for horizontal blur 152 | programId0 = createGLProgram(vertexShaderCode, fragmentShaderCode0) 153 | programId1 = createGLProgram(vertexShaderCode, fragmentShaderCode1) 154 | 155 | } 156 | 157 | private fun initData() { 158 | 159 | // 将三角形顶点数据放入buffer中 160 | // Put the triangle vertex data into the vertexDataBuffer 161 | vertexDataBuffer = ByteBuffer.allocateDirect(vertexData.size * java.lang.Float.SIZE / 8) 162 | .order(ByteOrder.nativeOrder()) 163 | .asFloatBuffer() 164 | vertexDataBuffer.put(vertexData) 165 | vertexDataBuffer.position(0) 166 | 167 | // 将纹理坐标数据放入buffer中 168 | // Put the texture coordinates into the textureCoordinateDataBuffer 169 | textureCoordinateDataBuffer0 = ByteBuffer.allocateDirect(textureCoordinateData0.size * java.lang.Float.SIZE / 8) 170 | .order(ByteOrder.nativeOrder()) 171 | .asFloatBuffer() 172 | textureCoordinateDataBuffer0.put(textureCoordinateData0) 173 | textureCoordinateDataBuffer0.position(0) 174 | 175 | textureCoordinateDataBuffer1 = ByteBuffer.allocateDirect(textureCoordinateData1.size * java.lang.Float.SIZE / 8) 176 | .order(ByteOrder.nativeOrder()) 177 | .asFloatBuffer() 178 | textureCoordinateDataBuffer1.put(textureCoordinateData1) 179 | textureCoordinateDataBuffer1.position(0) 180 | 181 | // 创建图片纹理 182 | // Create texture 183 | val textures = IntArray(1) 184 | GLES20.glGenTextures(textures.size, textures, 0) 185 | imageTexture = textures[0] 186 | 187 | // 设置纹理参数 188 | // Set texture parameters 189 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, imageTexture) 190 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR) 191 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR) 192 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE) 193 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE) 194 | 195 | // 解码图片并加载到纹理中 196 | // Decode the image and load it into texture 197 | val bitmap = Util.decodeBitmapFromAssets("image_0.jpg") 198 | val b = ByteBuffer.allocate(bitmap.width * bitmap.height * 4) 199 | bitmap.copyPixelsToBuffer(b) 200 | b.position(0) 201 | GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, bitmap.width, bitmap.height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, b) 202 | 203 | } 204 | 205 | private fun initFrameBuffer(width : Int, height : Int) { 206 | 207 | // 创建frame buffer绑定的纹理 208 | // Create texture which binds to frame buffer 209 | val textures = IntArray(1) 210 | GLES20.glGenTextures(textures.size, textures, 0) 211 | frameBufferTexture = textures[0] 212 | 213 | // 创建frame buffer 214 | // Create frame buffer 215 | val frameBuffers = IntArray(1) 216 | GLES20.glGenFramebuffers(frameBuffers.size, frameBuffers, 0) 217 | frameBuffer = frameBuffers[0] 218 | 219 | // 将frame buffer与texture绑定 220 | // Bind the texture to frame buffer 221 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, frameBufferTexture) 222 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR) 223 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR) 224 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE) 225 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE) 226 | GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null) 227 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffer) 228 | GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, frameBufferTexture, 0) 229 | } 230 | 231 | private fun createGLProgram(vertexShaderCode : String, fragmentShaderCode : String) : Int { 232 | 233 | // 创建GL程序 234 | // Create the GL program 235 | val programId = GLES20.glCreateProgram() 236 | 237 | // 加载、编译vertex shader和fragment shader 238 | // Load and compile vertex shader and fragment shader 239 | val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER) 240 | val fragmentShader= GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER) 241 | GLES20.glShaderSource(vertexShader, vertexShaderCode) 242 | GLES20.glShaderSource(fragmentShader, fragmentShaderCode) 243 | GLES20.glCompileShader(vertexShader) 244 | GLES20.glCompileShader(fragmentShader) 245 | 246 | // 将shader程序附着到GL程序上 247 | // Attach the compiled shaders to the GL program 248 | GLES20.glAttachShader(programId, vertexShader) 249 | GLES20.glAttachShader(programId, fragmentShader) 250 | 251 | // 链接GL程序 252 | // Link the GL program 253 | GLES20.glLinkProgram(programId) 254 | 255 | Util.checkGLError() 256 | 257 | return programId 258 | 259 | } 260 | 261 | private fun bindGLProgram(programId : Int, texture : Int, textureCoordinateDataBuffer : FloatBuffer) { 262 | 263 | // 应用GL程序 264 | // Use the GL program 265 | GLES20.glUseProgram(programId) 266 | 267 | // 获取字段a_position在shader中的位置 268 | // Get the location of a_position in the shader 269 | val aPositionLocation = GLES20.glGetAttribLocation(programId, "a_position") 270 | 271 | // 启动对应位置的参数 272 | // Enable the parameter of the location 273 | GLES20.glEnableVertexAttribArray(aPositionLocation) 274 | 275 | // 指定a_position所使用的顶点数据 276 | // Specify the data of a_position 277 | GLES20.glVertexAttribPointer(aPositionLocation, VERTEX_COMPONENT_COUNT, GLES20.GL_FLOAT, false,0, vertexDataBuffer) 278 | 279 | // 获取字段a_textureCoordinate在shader中的位置 280 | // Get the location of a_textureCoordinate in the shader 281 | val aTextureCoordinateLocation = GLES20.glGetAttribLocation(programId, "a_textureCoordinate") 282 | 283 | // 启动对应位置的参数 284 | // Enable the parameter of the location 285 | GLES20.glEnableVertexAttribArray(aTextureCoordinateLocation) 286 | 287 | // 指定a_textureCoordinate所使用的顶点数据 288 | // Specify the data of a_textureCoordinate 289 | GLES20.glVertexAttribPointer(aTextureCoordinateLocation, TEXTURE_COORDINATE_COMPONENT_COUNT, GLES20.GL_FLOAT, false,0, textureCoordinateDataBuffer) 290 | 291 | // 绑定纹理并设置u_texture参数 292 | // Bind the texture and set the u_texture parameter 293 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture) 294 | val uTextureLocation = GLES20.glGetAttribLocation(programId, "u_texture") 295 | GLES20.glUniform1i(uTextureLocation, 0) 296 | 297 | } 298 | 299 | private fun bindFrameBuffer(frameBuffer : Int) { 300 | 301 | // 绑定frame buffer 302 | // Bind the frame buffer 303 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffer) 304 | 305 | } 306 | 307 | private fun render() { 308 | 309 | // 设置清屏颜色 310 | // Set the color which the screen will be cleared to 311 | GLES20.glClearColor(0.9f, 0.9f, 0.9f, 1f) 312 | 313 | // 清屏 314 | // Clear the screen 315 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT) 316 | 317 | // 设置视口,这里设置为整个GLSurfaceView区域 318 | // Set the viewport to the full GLSurfaceView 319 | GLES20.glViewport(0, 0, glSurfaceViewWidth, glSurfaceViewHeight) 320 | 321 | // 调用draw方法用TRIANGLES的方式执行渲染,顶点数量为3个 322 | // Call the draw method with GL_TRIANGLES to render 3 vertices 323 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexData.size / VERTEX_COMPONENT_COUNT) 324 | 325 | } 326 | 327 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/renderer/SampleHelloWorldRenderer.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.renderer 2 | 3 | import android.opengl.GLES20 4 | import android.opengl.GLSurfaceView 5 | import java.nio.ByteBuffer 6 | import java.nio.ByteOrder 7 | import javax.microedition.khronos.egl.EGLConfig 8 | import javax.microedition.khronos.opengles.GL10 9 | 10 | /** 11 | * 12 | * Coded by kenney 13 | * 14 | * http://www.github.com/kenneycode 15 | * 16 | * 这是一个Hello World例子,渲染一个最简单的三角形 17 | * This is the first sample Hello World, and it will show you how to render a simplest triangle 18 | * 19 | **/ 20 | 21 | class SampleHelloWorldRenderer : GLSurfaceView.Renderer { 22 | 23 | private val vertexShaderCode = 24 | "precision mediump float;\n" + 25 | "attribute vec4 a_Position;\n" + 26 | "void main() {\n" + 27 | " gl_Position = a_Position;\n" + 28 | "}" 29 | 30 | private val fragmentShaderCode = 31 | "precision mediump float;\n" + 32 | "void main() {\n" + 33 | " gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);\n" + 34 | "}" 35 | 36 | private var glSurfaceViewWidth = 0 37 | private var glSurfaceViewHeight = 0 38 | 39 | // 三角形顶点数据 40 | // The vertex data of a triangle 41 | private val vertexData = floatArrayOf(0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f) 42 | 43 | // 每个顶点的成份数 44 | // The num of components of per vertex 45 | private val VERTEX_COMPONENT_COUNT = 2 46 | 47 | override fun onDrawFrame(gl: GL10?) { 48 | // 设置清屏颜色 49 | // Set the color which the screen will be cleared to 50 | GLES20.glClearColor(0.9f, 0.9f, 0.9f, 1f) 51 | 52 | // 清屏 53 | // Clear the screen 54 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT) 55 | 56 | // 设置视口,这里设置为整个GLSurfaceView区域 57 | // Set the viewport to the full GLSurfaceView 58 | GLES20.glViewport(0, 0, glSurfaceViewWidth, glSurfaceViewHeight) 59 | 60 | // 调用draw方法用TRIANGLES的方式执行渲染,顶点数量为3个 61 | // Call the draw method with GL_TRIANGLES to render 3 vertices 62 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexData.size / VERTEX_COMPONENT_COUNT) 63 | } 64 | 65 | override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { 66 | // 记录GLSurfaceView的宽高 67 | // Record the width and height of the GLSurfaceView 68 | glSurfaceViewWidth = width 69 | glSurfaceViewHeight = height 70 | } 71 | 72 | override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { 73 | // 创建GL程序 74 | // Create GL program 75 | val programId = GLES20.glCreateProgram() 76 | 77 | // 加载、编译vertex shader和fragment shader 78 | // Load and compile vertex shader and fragment shader 79 | val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER) 80 | val fragmentShader= GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER) 81 | GLES20.glShaderSource(vertexShader, vertexShaderCode) 82 | GLES20.glShaderSource(fragmentShader, fragmentShaderCode) 83 | GLES20.glCompileShader(vertexShader) 84 | GLES20.glCompileShader(fragmentShader) 85 | 86 | // 将shader程序附着到GL程序上 87 | // Attach the compiled shaders to the GL program 88 | GLES20.glAttachShader(programId, vertexShader) 89 | GLES20.glAttachShader(programId, fragmentShader) 90 | 91 | // 链接GL程序 92 | // Link the GL program 93 | GLES20.glLinkProgram(programId) 94 | 95 | // 将三角形顶点数据放入buffer中 96 | // Put the triangle vertex data into the buffer 97 | val buffer = ByteBuffer.allocateDirect(vertexData.size * java.lang.Float.SIZE) 98 | .order(ByteOrder.nativeOrder()) 99 | .asFloatBuffer() 100 | buffer.put(vertexData) 101 | buffer.position(0) 102 | 103 | // 应用GL程序 104 | // Use the GL program 105 | GLES20.glUseProgram(programId) 106 | 107 | // 获取字段a_Position在shader中的位置 108 | // Get the location of a_Position in the shader 109 | val location = GLES20.glGetAttribLocation(programId, "a_Position") 110 | 111 | // 启动对应位置的参数 112 | // Enable the parameter of the location 113 | GLES20.glEnableVertexAttribArray(location) 114 | 115 | // 指定a_Position所使用的顶点数据 116 | // Specify the vertex data of a_Position 117 | GLES20.glVertexAttribPointer(location, VERTEX_COMPONENT_COUNT, GLES20.GL_FLOAT, false,0, buffer) 118 | } 119 | 120 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/renderer/SamplePointAndLine.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.renderer 2 | 3 | import android.opengl.GLES20 4 | import android.opengl.GLSurfaceView 5 | import java.nio.ByteBuffer 6 | import java.nio.ByteOrder 7 | import javax.microedition.khronos.egl.EGLConfig 8 | import javax.microedition.khronos.opengles.GL10 9 | 10 | /** 11 | * 12 | * Coded by kenney 13 | * 14 | * http://www.github.com/kenneycode 15 | * 16 | * 这是一个使用vertex shader做顶点变换的例子,例子中将演示平移、缩放和旋转变换 17 | * This sample demonstrates how to do vertex tranlation,scale and ratation using vertex shader 18 | * 19 | **/ 20 | 21 | class SamplePointAndLine : GLSurfaceView.Renderer { 22 | 23 | private val vertexShaderCode = 24 | "precision mediump float;\n" + 25 | "attribute vec4 a_Position;\n" + 26 | "\n" + 27 | "uniform vec2 u_Translate;\n" + 28 | "uniform float u_Scale;\n" + 29 | "uniform float u_Rotate;\n" + 30 | "uniform float u_Ratio;\n" + 31 | "\n" + 32 | "void main() {\n" + 33 | " vec4 p = a_Position;\n" + 34 | " p.y = p.y / u_Ratio;\n" + 35 | " mat4 translateMatrix = mat4(1.0, 0.0, 0.0, 0.0,\n" + 36 | " 0.0, 1.0, 0.0, 0.0,\n" + 37 | " 0.0, 0.0, 1.0, 0.0,\n" + 38 | " u_Translate.x, u_Translate.y, 0.0, 1.0);\n" + 39 | " mat4 scaleMatrix = mat4(u_Scale, 0.0, 0.0, 0.0,\n" + 40 | " 0.0, u_Scale, 0.0, 0.0,\n" + 41 | " 0.0, 0.0, 1.0, 0.0,\n" + 42 | " 0.0, 0.0, 0.0, 1.0);\n" + 43 | " mat4 rotateMatrix = mat4(cos(u_Rotate), sin(u_Rotate), 0.0, 0.0,\n" + 44 | " -sin(u_Rotate), cos(u_Rotate), 0.0, 0.0,\n" + 45 | " 0.0, 0.0, 1.0, 0.0,\n" + 46 | " 0.0, 0.0, 0.0, 1.0);\n" + 47 | " p = translateMatrix * rotateMatrix * scaleMatrix * p;\n" + 48 | " p.y = p.y * u_Ratio;\n" + 49 | " gl_Position = p;\n" + 50 | "}" 51 | 52 | private val fragmentShaderCode = 53 | "precision mediump float;\n" + 54 | "void main() {\n" + 55 | " gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);\n" + 56 | "}" 57 | 58 | private var glSurfaceViewWidth = 0 59 | private var glSurfaceViewHeight = 0 60 | 61 | // 三角形顶点数据 62 | // The vertex data of a triangle 63 | private val vertexData = floatArrayOf(0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f) 64 | 65 | // 每个顶点的成份数 66 | // The num of components of per vertex 67 | private val VERTEX_COMPONENT_COUNT = 2 68 | 69 | // GL程序id 70 | // The GL program id 71 | private var programId: Int = 0 72 | 73 | override fun onDrawFrame(gl: GL10?) { 74 | // 设置视口,这里设置为整个GLSurfaceView区域 75 | // Set the viewport to the full GLSurfaceView 76 | GLES20.glViewport(0, 0, glSurfaceViewWidth, glSurfaceViewHeight) 77 | 78 | // 调用draw方法用TRIANGLES的方式执行渲染,顶点数量为3个 79 | // Call the draw method with GL_TRIANGLES to render 3 vertices 80 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexData.size / VERTEX_COMPONENT_COUNT) 81 | } 82 | 83 | override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { 84 | // 记录GLSurfaceView的宽高 85 | // Record the width and height of the GLSurfaceView 86 | glSurfaceViewWidth = width 87 | glSurfaceViewHeight = height 88 | 89 | // 获取字段u_Ratio在shader中的位置 90 | // Get the location of u_Rotate in the shader 91 | val uRatioLocation = GLES20.glGetUniformLocation(programId, "u_Ratio") 92 | 93 | // 启动对应位置的参数 94 | // Enable the parameter of the location 95 | GLES20.glEnableVertexAttribArray(uRatioLocation) 96 | 97 | // 指定u_Ratio所使用的顶点数据 98 | // Specify the vertex data of u_Ratio 99 | GLES20.glUniform1f(uRatioLocation, glSurfaceViewWidth * 1.0f / glSurfaceViewHeight) 100 | } 101 | 102 | override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { 103 | // 创建GL程序 104 | // Create GL program 105 | programId = GLES20.glCreateProgram() 106 | 107 | // 加载、编译vertex shader和fragment shader 108 | // Load and compile vertex shader and fragment shader 109 | val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER) 110 | val fragmentShader= GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER) 111 | GLES20.glShaderSource(vertexShader, vertexShaderCode) 112 | GLES20.glShaderSource(fragmentShader, fragmentShaderCode) 113 | GLES20.glCompileShader(vertexShader) 114 | GLES20.glCompileShader(fragmentShader) 115 | 116 | // 将shader程序附着到GL程序上 117 | // Attach the compiled shaders to the GL program 118 | GLES20.glAttachShader(programId, vertexShader) 119 | GLES20.glAttachShader(programId, fragmentShader) 120 | 121 | // 链接GL程序 122 | // Link the GL program 123 | GLES20.glLinkProgram(programId) 124 | 125 | // 将三角形顶点数据放入buffer中 126 | // Put the triangle vertex data into the buffer 127 | val buffer = ByteBuffer.allocateDirect(vertexData.size * java.lang.Float.SIZE) 128 | .order(ByteOrder.nativeOrder()) 129 | .asFloatBuffer() 130 | buffer.put(vertexData) 131 | buffer.position(0) 132 | 133 | // 应用GL程序 134 | // Use the GL program 135 | GLES20.glUseProgram(programId) 136 | 137 | // 获取字段a_Position在shader中的位置 138 | // Get the location of a_Position in the shader 139 | val aPositionLocation = GLES20.glGetAttribLocation(programId, "a_Position") 140 | 141 | // 启动对应位置的参数 142 | // Enable the parameter of the location 143 | GLES20.glEnableVertexAttribArray(aPositionLocation) 144 | 145 | // 指定a_Position所使用的顶点数据 146 | // Specify the vertex data of a_Position 147 | GLES20.glVertexAttribPointer(aPositionLocation, 2, GLES20.GL_FLOAT, false,0, buffer) 148 | 149 | // 获取字段u_Offset在shader中的位置 150 | // Get the location of translate in the shader 151 | val uTranslateLocation = GLES20.glGetUniformLocation(programId, "u_Translate") 152 | 153 | // 启动对应位置的参数 154 | // Enable the parameter of the location 155 | GLES20.glEnableVertexAttribArray(uTranslateLocation) 156 | 157 | // 指定u_Offset所使用的顶点数据 158 | // Specify the vertex data of translate 159 | GLES20.glUniform2f(uTranslateLocation, 0f, 0f) 160 | 161 | // 获取字段u_Offset在shader中的位置 162 | // Get the location of u_Scale in the shader 163 | val uScaleLocation = GLES20.glGetUniformLocation(programId, "u_Scale") 164 | 165 | // 启动对应位置的参数 166 | // Enable the parameter of the location 167 | GLES20.glEnableVertexAttribArray(uScaleLocation) 168 | 169 | // 指定u_Scale所使用的顶点数据 170 | // Specify the vertex data of u_Scale 171 | GLES20.glUniform1f(uScaleLocation, 1.0f) 172 | 173 | // 获取字段u_Offset在shader中的位置 174 | // Get the location of u_Rotate in the shader 175 | val uRotateLocation = GLES20.glGetUniformLocation(programId, "u_Rotate") 176 | 177 | // 启动对应位置的参数 178 | // Enable the parameter of the location 179 | GLES20.glEnableVertexAttribArray(uRotateLocation) 180 | 181 | // 指定u_Rotate所使用的顶点数据 182 | // Specify the vertex data of u_Rotate 183 | GLES20.glUniform1f(uRotateLocation, Math.toRadians(90.0).toFloat()) 184 | 185 | } 186 | 187 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/renderer/SampleTextureRenderer.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.renderer 2 | 3 | import android.opengl.GLES20 4 | import android.opengl.GLSurfaceView 5 | import com.kenneycode.Util 6 | import java.nio.ByteBuffer 7 | import java.nio.ByteOrder 8 | import java.nio.FloatBuffer 9 | import javax.microedition.khronos.egl.EGLConfig 10 | import javax.microedition.khronos.opengles.GL10 11 | 12 | /** 13 | * 14 | * Coded by kenney 15 | * 16 | * http://www.github.com/kenneycode 17 | * 18 | * 这是一个渲染纹理的例子 19 | * This sample demonstrates how to use texture. 20 | * 21 | **/ 22 | 23 | class SampleTextureRenderer : GLSurfaceView.Renderer { 24 | 25 | private val vertexShaderCode = 26 | "precision mediump float;\n" + 27 | "attribute vec4 a_position;\n" + 28 | "attribute vec2 a_textureCoordinate;\n" + 29 | "varying vec2 v_textureCoordinate;\n" + 30 | "void main() {\n" + 31 | " v_textureCoordinate = a_textureCoordinate;\n" + 32 | " gl_Position = a_position;\n" + 33 | "}" 34 | 35 | private val fragmentShaderCode = 36 | "precision mediump float;\n" + 37 | "varying vec2 v_textureCoordinate;\n" + 38 | "uniform sampler2D u_texture;\n" + 39 | "void main() {\n" + 40 | " gl_FragColor = texture2D(u_texture, v_textureCoordinate);\n" + 41 | "}" 42 | 43 | // GLSurfaceView的宽高 44 | // The width and height of GLSurfaceView 45 | private var glSurfaceViewWidth = 0 46 | private var glSurfaceViewHeight = 0 47 | 48 | // 纹理顶点数据 49 | // The vertex data of the texture 50 | private val vertexData = floatArrayOf(-1f, -1f, -1f, 1f, 1f, 1f, -1f, -1f, 1f, 1f, 1f, -1f) 51 | private val VERTEX_COMPONENT_COUNT = 2 52 | private lateinit var vertexDataBuffer : FloatBuffer 53 | 54 | // 纹理坐标 55 | // The texture coordinate 56 | private val textureCoordinateData = floatArrayOf(0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, 1f, 0f, 1f, 1f) 57 | private val TEXTURE_COORDINATE_COMPONENT_COUNT = 2 58 | private lateinit var textureCoordinateDataBuffer : FloatBuffer 59 | 60 | override fun onDrawFrame(gl: GL10?) { 61 | 62 | // 设置清屏颜色 63 | // Set the color which the screen will be cleared to 64 | GLES20.glClearColor(0.9f, 0.9f, 0.9f, 1f) 65 | 66 | // 清屏 67 | // Clear the screen 68 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT) 69 | 70 | // 设置视口,这里设置为整个GLSurfaceView区域 71 | // Set the viewport to the full GLSurfaceView 72 | GLES20.glViewport(0, 0, glSurfaceViewWidth, glSurfaceViewHeight) 73 | 74 | // 调用draw方法用TRIANGLES的方式执行渲染,顶点数量为3个 75 | // Call the draw method with GL_TRIANGLES to render 3 vertices 76 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexData.size / VERTEX_COMPONENT_COUNT) 77 | 78 | } 79 | 80 | override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { 81 | // 记录GLSurfaceView的宽高 82 | // Record the width and height of the GLSurfaceView 83 | glSurfaceViewWidth = width 84 | glSurfaceViewHeight = height 85 | } 86 | 87 | override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { 88 | 89 | // 创建GL程序 90 | // Create GL program 91 | val programId = GLES20.glCreateProgram() 92 | 93 | // 加载、编译vertex shader和fragment shader 94 | // Load and compile vertex shader and fragment shader 95 | val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER) 96 | val fragmentShader= GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER) 97 | GLES20.glShaderSource(vertexShader, vertexShaderCode) 98 | GLES20.glShaderSource(fragmentShader, fragmentShaderCode) 99 | GLES20.glCompileShader(vertexShader) 100 | GLES20.glCompileShader(fragmentShader) 101 | 102 | // 将shader程序附着到GL程序上 103 | // Attach the compiled shaders to the GL program 104 | GLES20.glAttachShader(programId, vertexShader) 105 | GLES20.glAttachShader(programId, fragmentShader) 106 | 107 | // 链接GL程序 108 | // Link the GL program 109 | GLES20.glLinkProgram(programId) 110 | 111 | // 应用GL程序 112 | // Use the GL program 113 | GLES20.glUseProgram(programId) 114 | 115 | // for (i in 0 until vertexData.size) { 116 | // vertexData[i] *= 5f 117 | // } 118 | 119 | // 将三角形顶点数据放入buffer中 120 | // Put the triangle vertex data into the vertexDataBuffer 121 | vertexDataBuffer = ByteBuffer.allocateDirect(vertexData.size * java.lang.Float.SIZE / 8) 122 | .order(ByteOrder.nativeOrder()) 123 | .asFloatBuffer() 124 | vertexDataBuffer.put(vertexData) 125 | vertexDataBuffer.position(0) 126 | 127 | // 获取字段a_position在shader中的位置 128 | // Get the location of a_position in the shader 129 | val aPositionLocation = GLES20.glGetAttribLocation(programId, "a_position") 130 | 131 | // 启动对应位置的参数 132 | // Enable the parameter of the location 133 | GLES20.glEnableVertexAttribArray(aPositionLocation) 134 | 135 | // 指定a_position所使用的顶点数据 136 | // Specify the data of a_position 137 | GLES20.glVertexAttribPointer(aPositionLocation, VERTEX_COMPONENT_COUNT, GLES20.GL_FLOAT, false,0, vertexDataBuffer) 138 | 139 | // for (i in 0 until textureCoordinateData.size) { 140 | // textureCoordinateData[i] *= 3f 141 | // } 142 | 143 | // 将纹理坐标数据放入buffer中 144 | // Put the texture coordinates into the textureCoordinateDataBuffer 145 | textureCoordinateDataBuffer = ByteBuffer.allocateDirect(textureCoordinateData.size * java.lang.Float.SIZE / 8) 146 | .order(ByteOrder.nativeOrder()) 147 | .asFloatBuffer() 148 | textureCoordinateDataBuffer.put(textureCoordinateData) 149 | textureCoordinateDataBuffer.position(0) 150 | 151 | // 获取字段a_textureCoordinate在shader中的位置 152 | // Get the location of a_textureCoordinate in the shader 153 | val aTextureCoordinateLocation = GLES20.glGetAttribLocation(programId, "a_textureCoordinate") 154 | 155 | // 启动对应位置的参数 156 | // Enable the parameter of the location 157 | GLES20.glEnableVertexAttribArray(aTextureCoordinateLocation) 158 | 159 | // 指定a_textureCoordinate所使用的顶点数据 160 | // Specify the data of a_textureCoordinate 161 | GLES20.glVertexAttribPointer(aTextureCoordinateLocation, TEXTURE_COORDINATE_COMPONENT_COUNT, GLES20.GL_FLOAT, false,0, textureCoordinateDataBuffer) 162 | 163 | // 创建图片纹理 164 | // Create texture 165 | val textures = IntArray(1) 166 | GLES20.glGenTextures(textures.size, textures, 0) 167 | val imageTexture = textures[0] 168 | 169 | // 设置纹理参数 170 | // Set texture parameters 171 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, imageTexture) 172 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR) 173 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR) 174 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE) 175 | GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE) 176 | 177 | // 解码图片并加载到纹理中 178 | // Decode the image and load it into texture 179 | val bitmap = Util.decodeBitmapFromAssets("image_0.jpg") 180 | val b = ByteBuffer.allocate(bitmap.width * bitmap.height * 4) 181 | bitmap.copyPixelsToBuffer(b) 182 | b.position(0) 183 | GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, bitmap.width, bitmap.height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, b) 184 | val uTextureLocation = GLES20.glGetAttribLocation(programId, "u_texture") 185 | GLES20.glUniform1i(uTextureLocation, 0) 186 | 187 | } 188 | 189 | } -------------------------------------------------------------------------------- /app/src/main/java/com/kenneycode/samples/renderer/SampleVertexShaderRenderer.kt: -------------------------------------------------------------------------------- 1 | package com.kenneycode.samples.renderer 2 | 3 | import android.opengl.GLES20 4 | import android.opengl.GLSurfaceView 5 | import java.nio.ByteBuffer 6 | import java.nio.ByteOrder 7 | import javax.microedition.khronos.egl.EGLConfig 8 | import javax.microedition.khronos.opengles.GL10 9 | 10 | /** 11 | * 12 | * Coded by kenney 13 | * 14 | * http://www.github.com/kenneycode 15 | * 16 | * 这是一个使用vertex shader做顶点变换的例子,例子中将演示平移、缩放和旋转变换 17 | * This sample demonstrates how to do vertex translation,scale and rotation using vertex shader 18 | * 19 | **/ 20 | 21 | class SampleVertexShaderRenderer : GLSurfaceView.Renderer { 22 | 23 | private val vertexShaderCode = 24 | "precision mediump float;\n" + 25 | "attribute vec4 a_Position;\n" + 26 | "\n" + 27 | "uniform vec2 u_Translate;\n" + 28 | "uniform float u_Scale;\n" + 29 | "uniform float u_Rotate;\n" + 30 | "uniform float u_Ratio;\n" + 31 | "\n" + 32 | "void main() {\n" + 33 | " vec4 p = a_Position;\n" + 34 | " p.y = p.y / u_Ratio;\n" + 35 | " mat4 translateMatrix = mat4(1.0, 0.0, 0.0, 0.0,\n" + 36 | " 0.0, 1.0, 0.0, 0.0,\n" + 37 | " 0.0, 0.0, 1.0, 0.0,\n" + 38 | " u_Translate.x, u_Translate.y, 0.0, 1.0);\n" + 39 | " mat4 scaleMatrix = mat4(u_Scale, 0.0, 0.0, 0.0,\n" + 40 | " 0.0, u_Scale, 0.0, 0.0,\n" + 41 | " 0.0, 0.0, 1.0, 0.0,\n" + 42 | " 0.0, 0.0, 0.0, 1.0);\n" + 43 | " mat4 rotateMatrix = mat4(cos(u_Rotate), sin(u_Rotate), 0.0, 0.0,\n" + 44 | " -sin(u_Rotate), cos(u_Rotate), 0.0, 0.0,\n" + 45 | " 0.0, 0.0, 1.0, 0.0,\n" + 46 | " 0.0, 0.0, 0.0, 1.0);\n" + 47 | " p = translateMatrix * rotateMatrix * scaleMatrix * p;\n" + 48 | " p.y = p.y * u_Ratio;\n" + 49 | " gl_Position = p;\n" + 50 | "}" 51 | 52 | private val fragmentShaderCode = 53 | "precision mediump float;\n" + 54 | "void main() {\n" + 55 | " gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);\n" + 56 | "}" 57 | 58 | private var glSurfaceViewWidth = 0 59 | private var glSurfaceViewHeight = 0 60 | 61 | // 三角形顶点数据 62 | // The vertex data of a triangle 63 | private val vertexData = floatArrayOf(0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f) 64 | 65 | // 每个顶点的成份数 66 | // The num of components of per vertex 67 | private val VERTEX_COMPONENT_COUNT = 2 68 | 69 | // GL程序id 70 | // The GL program id 71 | private var programId: Int = 0 72 | 73 | override fun onDrawFrame(gl: GL10?) { 74 | // 设置清屏颜色 75 | // Set the color which the screen will be cleared to 76 | GLES20.glClearColor(0.9f, 0.9f, 0.9f, 1f) 77 | 78 | // 清屏 79 | // Clear the screen 80 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT) 81 | 82 | // 设置视口,这里设置为整个GLSurfaceView区域 83 | // Set the viewport to the full GLSurfaceView 84 | GLES20.glViewport(0, 0, glSurfaceViewWidth, glSurfaceViewHeight) 85 | 86 | // 调用draw方法用TRIANGLES的方式执行渲染,顶点数量为3个 87 | // Call the draw method with GL_TRIANGLES to render 3 vertices 88 | GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexData.size / VERTEX_COMPONENT_COUNT) 89 | } 90 | 91 | override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) { 92 | // 记录GLSurfaceView的宽高 93 | // Record the width and height of the GLSurfaceView 94 | glSurfaceViewWidth = width 95 | glSurfaceViewHeight = height 96 | 97 | // 获取字段u_Ratio在shader中的位置 98 | // Get the location of u_Rotate in the shader 99 | val uRatioLocation = GLES20.glGetUniformLocation(programId, "u_Ratio") 100 | 101 | // 启动对应位置的参数 102 | // Enable the parameter of the location 103 | GLES20.glEnableVertexAttribArray(uRatioLocation) 104 | 105 | // 指定u_Ratio所使用的顶点数据 106 | // Specify the vertex data of u_Ratio 107 | GLES20.glUniform1f(uRatioLocation, glSurfaceViewWidth * 1.0f / glSurfaceViewHeight) 108 | } 109 | 110 | override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) { 111 | // 创建GL程序 112 | // Create GL program 113 | programId = GLES20.glCreateProgram() 114 | 115 | // 加载、编译vertex shader和fragment shader 116 | // Load and compile vertex shader and fragment shader 117 | val vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER) 118 | val fragmentShader= GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER) 119 | GLES20.glShaderSource(vertexShader, vertexShaderCode) 120 | GLES20.glShaderSource(fragmentShader, fragmentShaderCode) 121 | GLES20.glCompileShader(vertexShader) 122 | GLES20.glCompileShader(fragmentShader) 123 | 124 | // 将shader程序附着到GL程序上 125 | // Attach the compiled shaders to the GL program 126 | GLES20.glAttachShader(programId, vertexShader) 127 | GLES20.glAttachShader(programId, fragmentShader) 128 | 129 | // 链接GL程序 130 | // Link the GL program 131 | GLES20.glLinkProgram(programId) 132 | 133 | // 将三角形顶点数据放入buffer中 134 | // Put the triangle vertex data into the buffer 135 | val buffer = ByteBuffer.allocateDirect(vertexData.size * java.lang.Float.SIZE) 136 | .order(ByteOrder.nativeOrder()) 137 | .asFloatBuffer() 138 | buffer.put(vertexData) 139 | buffer.position(0) 140 | 141 | // 应用GL程序 142 | // Use the GL program 143 | GLES20.glUseProgram(programId) 144 | 145 | // 获取字段a_Position在shader中的位置 146 | // Get the location of a_Position in the shader 147 | val aPositionLocation = GLES20.glGetAttribLocation(programId, "a_Position") 148 | 149 | // 启动对应位置的参数 150 | // Enable the parameter of the location 151 | GLES20.glEnableVertexAttribArray(aPositionLocation) 152 | 153 | // 指定a_Position所使用的顶点数据 154 | // Specify the vertex data of a_Position 155 | GLES20.glVertexAttribPointer(aPositionLocation, 2, GLES20.GL_FLOAT, false,0, buffer) 156 | 157 | // 获取字段u_Offset在shader中的位置 158 | // Get the location of translate in the shader 159 | val uTranslateLocation = GLES20.glGetUniformLocation(programId, "u_Translate") 160 | 161 | // 启动对应位置的参数 162 | // Enable the parameter of the location 163 | GLES20.glEnableVertexAttribArray(uTranslateLocation) 164 | 165 | // 指定u_Offset所使用的顶点数据 166 | // Specify the vertex data of translate 167 | GLES20.glUniform2f(uTranslateLocation, 0.3f, 0.3f) 168 | 169 | // 获取字段u_Offset在shader中的位置 170 | // Get the location of u_Scale in the shader 171 | val uScaleLocation = GLES20.glGetUniformLocation(programId, "u_Scale") 172 | 173 | // 启动对应位置的参数 174 | // Enable the parameter of the location 175 | GLES20.glEnableVertexAttribArray(uScaleLocation) 176 | 177 | // 指定u_Scale所使用的顶点数据 178 | // Specify the vertex data of u_Scale 179 | GLES20.glUniform1f(uScaleLocation, 0.5f) 180 | 181 | // 获取字段u_Offset在shader中的位置 182 | // Get the location of u_Rotate in the shader 183 | val uRotateLocation = GLES20.glGetUniformLocation(programId, "u_Rotate") 184 | 185 | // 启动对应位置的参数 186 | // Enable the parameter of the location 187 | GLES20.glEnableVertexAttribArray(uRotateLocation) 188 | 189 | // 指定u_Rotate所使用的顶点数据 190 | // Specify the vertex data of u_Rotate 191 | GLES20.glUniform1f(uRotateLocation, Math.toRadians(45.0).toFloat()) 192 | 193 | } 194 | 195 | } -------------------------------------------------------------------------------- /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/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_common_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_sample_draw_mode.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | -------------------------------------------------------------------------------- /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/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | OpenGL2.0SamplesForAndroid 3 | Hello World 4 | 两个三角形(2 Triangles) 5 | 顶点着色器(Vertex Shader) 6 | 片段着色器(Fragment Shader) 7 | 绘制模式(Draw Mode) 8 | 纹理(Texture) 9 | 帧缓存(Frame Buffer) 10 | 11 | -------------------------------------------------------------------------------- /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 | ext.kotlin_version = '1.2.71' 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.2.1' 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 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 | # Kotlin code style for this project: "official" or "obsolete": 15 | kotlin.code.style=official 16 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kenneycode/OpenGLES2.0SamplesForAndroid/0864ee22db26da2070ab374d06821e36690d7ce1/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------