From ebb6bc2fc89ed6bf3a5135fc9a282a5821392a3c Mon Sep 17 00:00:00 2001 From: Jack Palevich Date: Mon, 17 Jan 2011 17:48:44 -0800 Subject: [PATCH] Add BasicGLSurfaceView sample. Show how to get started with OpenGL ES 2.0. Change-Id: Iee83ca6a52eee64e8c6b77bc9a8abff11a1eabfd --- build/sdk.atree | 1 + samples/BasicGLSurfaceView/Android.mk | 13 + .../BasicGLSurfaceView/AndroidManifest.xml | 36 +++ samples/BasicGLSurfaceView/res/raw/robot.png | Bin 0 -> 5634 bytes .../BasicGLSurfaceView/res/values/strings.xml | 29 ++ .../BasicGLSurfaceView.java | 31 +++ .../BasicGLSurfaceViewActivity.java | 48 ++++ .../GLES20TriangleRenderer.java | 253 ++++++++++++++++++ 8 files changed, 411 insertions(+) create mode 100644 samples/BasicGLSurfaceView/Android.mk create mode 100644 samples/BasicGLSurfaceView/AndroidManifest.xml create mode 100644 samples/BasicGLSurfaceView/res/raw/robot.png create mode 100644 samples/BasicGLSurfaceView/res/values/strings.xml create mode 100644 samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/BasicGLSurfaceView.java create mode 100644 samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/BasicGLSurfaceViewActivity.java create mode 100644 samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/GLES20TriangleRenderer.java diff --git a/build/sdk.atree b/build/sdk.atree index 0c5edbf8c..ef8757e5a 100644 --- a/build/sdk.atree +++ b/build/sdk.atree @@ -154,6 +154,7 @@ development/samples/AccessibilityService samples/${PLATFORM_NAME}/Accessibil development/samples/AccelerometerPlay samples/${PLATFORM_NAME}/AccelerometerPlay development/samples/ApiDemos samples/${PLATFORM_NAME}/ApiDemos development/samples/BackupRestore samples/${PLATFORM_NAME}/BackupRestore +development/samples/BasicGLSurfaceView samples/${PLATFORM_NAME}/BasicGLSurfaceView development/samples/BluetoothChat samples/${PLATFORM_NAME}/BluetoothChat development/samples/ContactManager samples/${PLATFORM_NAME}/ContactManager development/samples/CrossCompatibility samples/${PLATFORM_NAME}/CrossCompatibility diff --git a/samples/BasicGLSurfaceView/Android.mk b/samples/BasicGLSurfaceView/Android.mk new file mode 100644 index 000000000..1898bb7ed --- /dev/null +++ b/samples/BasicGLSurfaceView/Android.mk @@ -0,0 +1,13 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := samples + +# Only compile source java files in this apk. +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := BasicGLSurfaceView + +LOCAL_SDK_VERSION := current + +include $(BUILD_PACKAGE) diff --git a/samples/BasicGLSurfaceView/AndroidManifest.xml b/samples/BasicGLSurfaceView/AndroidManifest.xml new file mode 100644 index 000000000..5dc7df47f --- /dev/null +++ b/samples/BasicGLSurfaceView/AndroidManifest.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + diff --git a/samples/BasicGLSurfaceView/res/raw/robot.png b/samples/BasicGLSurfaceView/res/raw/robot.png new file mode 100644 index 0000000000000000000000000000000000000000..8a9e6984be96ea93dcb63ae1973e530bdfb1eda0 GIT binary patch literal 5634 zcma);Wl+@7yT^Y^F1;Wf%F?WKN~d&(bPG#JHwY}95+dEwUDDkkCEXw`D4>LZz;gM| zeR=Plxi6lWd7d-RobNej&L>(!O&$-M5*q*jJVgart*40ikAYF1yo&MP?5BX`qF~?- z065S7V;~?in;ZbJ)$L?tG&F3TJ)GTbon2@ZWn^ew+?;Lf-dO{H&uWe~Tu1weRP1{5 zQd&6*@k!ZPiv&!oB^`?(PGW*Vu^>uO(1H~b%|61{uhFPR^P^DX;}LNrnryf+*vpuE z(9iLqg;6nI@3#F59Tq#U4$4+3riMMYoI^h#UfaVXFkJ8io z0AU#bvD~?y=%1H0kVDv$QW*`RZ45%MgN?xKu9$jy9M4&%6vpqS>0_h-EE-g{q@!N?j43;9+O~sw43k!6PGH*)YBia!YtPh66Eh&a31ez=I90$E!*^A z#JieAuJN*&aTh$rbkWi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ouc zf3}H|Bp?F<)j2Y7zyYqhKLqOG?8_C%UnLIlNG(+eZ$&&cO|?LO7+Tk>NENaO-q)o1 zZOIt{8qx5Fb2yh4YtQ|az*jtUktM-XooIgxkVdqBz^)p)2o7act|(*kV>*7G)#8qnYF8FP+rL9W?_;&CNwD?wx>>o$pHFf&rhRK_<_D zrCl0Zy#A2?d<`)5 zwV@;e%kswqJ40_}NOIZBgN=L79anZ@hr~{8Q$%DVO88a>_on&f6KOWmc`>|1V4CZCnt zfTa7>@+!w_x46twxbhsBZ?Bjh=RcrFYRRhy{>7l>$%%x7yj)%&@@xIxvN5xTMVKef z&k<(+^uG8v;~A-{{~R90LMNnT-a&x zg{eWvq<}^}8Sude@ICzLkyH~WG_o54PfSxCF?bmp=jMd5 zUWs!m9a>N6QhTtTjXV(CupbrVe}sWl96>QH<;THC%^Hvp!e8&o18v*qj4EExEembL zw2<;UATXyQ^LeX`9!tA{z>+)e(Q%H*W216vd* zE+Whv&EW|e))#NOqN^Pl5e7$%e;#q;y-P0gEHqF~Q!yS%dmNgdMPe0t)Bc&RpAf>I z-z~$r6Ii%)YAMb5i%z@ye{tu3lc+i2Q4lIH__hn&n4S-Rf^0zXwVG^=w0X#X0T7*d A761SM literal 0 HcmV?d00001 diff --git a/samples/BasicGLSurfaceView/res/values/strings.xml b/samples/BasicGLSurfaceView/res/values/strings.xml new file mode 100644 index 000000000..a3150cbc9 --- /dev/null +++ b/samples/BasicGLSurfaceView/res/values/strings.xml @@ -0,0 +1,29 @@ + + + + + + + + Basic GLSurfaceView + + + diff --git a/samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/BasicGLSurfaceView.java b/samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/BasicGLSurfaceView.java new file mode 100644 index 000000000..545a97e7a --- /dev/null +++ b/samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/BasicGLSurfaceView.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.basicglsurfaceview; + +import android.content.Context; +import android.opengl.GLSurfaceView; + +import android.opengl.GLES20; + +class BasicGLSurfaceView extends GLSurfaceView { + public BasicGLSurfaceView(Context context) { + super(context); + setEGLContextClientVersion(2); + setRenderer(new GLES20TriangleRenderer(context)); + } +} + diff --git a/samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/BasicGLSurfaceViewActivity.java b/samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/BasicGLSurfaceViewActivity.java new file mode 100644 index 000000000..da2544150 --- /dev/null +++ b/samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/BasicGLSurfaceViewActivity.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.basicglsurfaceview; + +import android.app.Activity; +import android.os.Bundle; +import android.view.WindowManager; + +import java.io.File; + + +public class BasicGLSurfaceViewActivity extends Activity { + + private BasicGLSurfaceView mView; + + @Override + protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + mView = new BasicGLSurfaceView(getApplication()); + setContentView(mView); + } + + @Override + protected void onPause() { + super.onPause(); + mView.onPause(); + } + + @Override + protected void onResume() { + super.onResume(); + mView.onResume(); + } +} diff --git a/samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/GLES20TriangleRenderer.java b/samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/GLES20TriangleRenderer.java new file mode 100644 index 000000000..f8c943e04 --- /dev/null +++ b/samples/BasicGLSurfaceView/src/com/example/android/basicglsurfaceview/GLES20TriangleRenderer.java @@ -0,0 +1,253 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.basicglsurfaceview; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.FloatBuffer; + +import javax.microedition.khronos.egl.EGLConfig; +import javax.microedition.khronos.opengles.GL10; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.opengl.GLES20; +import android.opengl.GLSurfaceView; +import android.opengl.GLUtils; +import android.opengl.Matrix; +import android.os.SystemClock; +import android.util.Log; + +class GLES20TriangleRenderer implements GLSurfaceView.Renderer { + + public GLES20TriangleRenderer(Context context) { + mContext = context; + mTriangleVertices = ByteBuffer.allocateDirect(mTriangleVerticesData.length + * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer(); + mTriangleVertices.put(mTriangleVerticesData).position(0); + } + + public void onDrawFrame(GL10 glUnused) { + // Ignore the passed-in GL10 interface, and use the GLES20 + // class's static methods instead. + GLES20.glClearColor(0.0f, 0.0f, 1.0f, 1.0f); + GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); + GLES20.glUseProgram(mProgram); + checkGlError("glUseProgram"); + + GLES20.glActiveTexture(GLES20.GL_TEXTURE0); + GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID); + + mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET); + GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, + TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices); + checkGlError("glVertexAttribPointer maPosition"); + mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET); + GLES20.glEnableVertexAttribArray(maPositionHandle); + checkGlError("glEnableVertexAttribArray maPositionHandle"); + GLES20.glVertexAttribPointer(maTextureHandle, 2, GLES20.GL_FLOAT, false, + TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices); + checkGlError("glVertexAttribPointer maTextureHandle"); + GLES20.glEnableVertexAttribArray(maTextureHandle); + checkGlError("glEnableVertexAttribArray maTextureHandle"); + + long time = SystemClock.uptimeMillis() % 4000L; + float angle = 0.090f * ((int) time); + Matrix.setRotateM(mMMatrix, 0, angle, 0, 0, 1.0f); + Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0); + Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); + + GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0); + GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); + checkGlError("glDrawArrays"); + } + + public void onSurfaceChanged(GL10 glUnused, int width, int height) { + // Ignore the passed-in GL10 interface, and use the GLES20 + // class's static methods instead. + GLES20.glViewport(0, 0, width, height); + float ratio = (float) width / height; + Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); + } + + public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { + // Ignore the passed-in GL10 interface, and use the GLES20 + // class's static methods instead. + mProgram = createProgram(mVertexShader, mFragmentShader); + if (mProgram == 0) { + return; + } + maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition"); + checkGlError("glGetAttribLocation aPosition"); + if (maPositionHandle == -1) { + throw new RuntimeException("Could not get attrib location for aPosition"); + } + maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord"); + checkGlError("glGetAttribLocation aTextureCoord"); + if (maTextureHandle == -1) { + throw new RuntimeException("Could not get attrib location for aTextureCoord"); + } + + muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); + checkGlError("glGetUniformLocation uMVPMatrix"); + if (muMVPMatrixHandle == -1) { + throw new RuntimeException("Could not get attrib location for uMVPMatrix"); + } + + /* + * Create our texture. This has to be done each time the + * surface is created. + */ + + int[] textures = new int[1]; + GLES20.glGenTextures(1, textures, 0); + + mTextureID = textures[0]; + GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID); + + GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, + GLES20.GL_NEAREST); + GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, + GLES20.GL_TEXTURE_MAG_FILTER, + GLES20.GL_LINEAR); + + GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, + GLES20.GL_REPEAT); + GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, + GLES20.GL_REPEAT); + + InputStream is = mContext.getResources() + .openRawResource(R.raw.robot); + Bitmap bitmap; + try { + bitmap = BitmapFactory.decodeStream(is); + } finally { + try { + is.close(); + } catch(IOException e) { + // Ignore. + } + } + + GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); + bitmap.recycle(); + + Matrix.setLookAtM(mVMatrix, 0, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f); + } + + private int loadShader(int shaderType, String source) { + int shader = GLES20.glCreateShader(shaderType); + if (shader != 0) { + GLES20.glShaderSource(shader, source); + GLES20.glCompileShader(shader); + int[] compiled = new int[1]; + GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); + if (compiled[0] == 0) { + Log.e(TAG, "Could not compile shader " + shaderType + ":"); + Log.e(TAG, GLES20.glGetShaderInfoLog(shader)); + GLES20.glDeleteShader(shader); + shader = 0; + } + } + return shader; + } + + private int createProgram(String vertexSource, String fragmentSource) { + int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource); + if (vertexShader == 0) { + return 0; + } + + int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource); + if (pixelShader == 0) { + return 0; + } + + int program = GLES20.glCreateProgram(); + if (program != 0) { + GLES20.glAttachShader(program, vertexShader); + checkGlError("glAttachShader"); + GLES20.glAttachShader(program, pixelShader); + checkGlError("glAttachShader"); + GLES20.glLinkProgram(program); + int[] linkStatus = new int[1]; + GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0); + if (linkStatus[0] != GLES20.GL_TRUE) { + Log.e(TAG, "Could not link program: "); + Log.e(TAG, GLES20.glGetProgramInfoLog(program)); + GLES20.glDeleteProgram(program); + program = 0; + } + } + return program; + } + + private void checkGlError(String op) { + int error; + while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { + Log.e(TAG, op + ": glError " + error); + throw new RuntimeException(op + ": glError " + error); + } + } + + private static final int FLOAT_SIZE_BYTES = 4; + private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES; + private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0; + private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3; + private final float[] mTriangleVerticesData = { + // X, Y, Z, U, V + -1.0f, -0.5f, 0, -0.5f, 0.0f, + 1.0f, -0.5f, 0, 1.5f, -0.0f, + 0.0f, 1.11803399f, 0, 0.5f, 1.61803399f }; + + private FloatBuffer mTriangleVertices; + + private final String mVertexShader = + "uniform mat4 uMVPMatrix;\n" + + "attribute vec4 aPosition;\n" + + "attribute vec2 aTextureCoord;\n" + + "varying vec2 vTextureCoord;\n" + + "void main() {\n" + + " gl_Position = uMVPMatrix * aPosition;\n" + + " vTextureCoord = aTextureCoord;\n" + + "}\n"; + + private final String mFragmentShader = + "precision mediump float;\n" + + "varying vec2 vTextureCoord;\n" + + "uniform sampler2D sTexture;\n" + + "void main() {\n" + + " gl_FragColor = texture2D(sTexture, vTextureCoord);\n" + + "}\n"; + + private float[] mMVPMatrix = new float[16]; + private float[] mProjMatrix = new float[16]; + private float[] mMMatrix = new float[16]; + private float[] mVMatrix = new float[16]; + + private int mProgram; + private int mTextureID; + private int muMVPMatrixHandle; + private int maPositionHandle; + private int maTextureHandle; + + private Context mContext; + private static String TAG = "GLES20TriangleRenderer"; +}