【Android源码 栏目提醒】:网学会员Android源码 为您提供GLES20TriangleRenderer java android源码 - 开发文档参考,解决您在GLES20TriangleRenderer java android源码 - 开发文档学习中工作中的难题,参考学习。
GLES20TriangleRenderer.java
android源码 / 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 GLES20TriangleRendererContext context mContext context mTriangleVertices ByteBuffer .allocateDirectmTriangleVerticesData.length FLOAT_SIZE_BYTES .orderByteOrder.nativeOrder.asFloatBuffer mTriangleVertices.putmTriangleVerticesData.position0 public void onDrawFrameGL10 glUnused // Ignore the passed-in GL10 interface and use the GLES20 // classs static methods instead. GLES20.glClearColor0.0f 0.0f 1.0f 1.0f GLES20.glClearGLES20.GL_DEPTH_BUFFER_BIT GLES20.GL_COLOR_BUFFER_BIT GLES20.glUseProgrammProgram checkGlErrorglUseProgram GLES20.glActiveTextureGLES20.GL_TEXTURE0 GLES20.glBindTextureGLES20.GL_TEXTURE_2D mTextureID mTriangleVertices.positionTRIANGLE_VERTICES_DATA_POS_OFFSET GLES20.glVertexAttribPointermaPositionHandle 3 GLES20.GL_FLOAT false TRIANGLE_VERTICES_DATA_STRIDE_BYTES mTriangleVertices checkGlErrorglVertexAttribPointer maPosition mTriangleVertices.positionTRIANGLE_VERTICES_DATA_UV_OFFSET GLES20.glEnableVertexAttribArraymaPositionHandle checkGlErrorglEnableVertexAttribArray maPositionHandle GLES20.glVertexAttribPointermaTextureHandle 2 GLES20.GL_FLOAT false TRIANGLE_VERTICES_DATA_STRIDE_BYTES mTriangleVertices checkGlErrorglVertexAttribPointer maTextureHandle GLES20.glEnableVertexAttribArraymaTextureHandle checkGlErrorglEnableVertexAttribArray maTextureHandle long time SystemClock.uptimeMillis 4000L float angle 0.090f int time Matrix.setRotateMmMMatrix 0 angle 0 0 1.0f Matrix.multiplyMMmMVPMatrix 0 mVMatrix 0 mMMatrix 0 Matrix.multiplyMMmMVPMatrix 0 mProjMatrix 0 mMVPMatrix 0 GLES20.glUniformMatrix4fvmuMVPMatrixHandle 1 false mMVPMatrix 0 GLES20.glDrawArraysGLES20.GL_TRIANGLES 0 3 checkGlErrorglDrawArrays public void onSurfaceChangedGL10 glUnused int width int height // Ignore the passed-in GL10 interface and use the GLES20 // classs static methods instead. GLES20.glViewport0 0 width height float ratio float width / height Matrix.frustumMmProjMatrix 0 -ratio ratio -1 1 3 7 public void onSurfaceCreatedGL10 glUnused EGLConfig config // Ignore the passed-in GL10 interface and use the GLES20 // classs static methods instead. mProgram createProgrammVertexShader mFragmentShader if mProgram 0 return maPositionHandle GLES20.glGetAttribLocationmProgram aPosition checkGlErrorglGetAttribLocation aPosition if maPositionHandle -1 throw new RuntimeException Could not get attrib location for aPosition maTextureHandle GLES20.glGetAttribLocationmProgram aTextureCoord checkGlErrorglGetAttribLocation aTextureCoord if maTextureHandle -1 throw new RuntimeException Could not get attrib location for aTextureCoord muMVPMatrixHandle GLES20.glGetUniformLocationmProgram uMVPMatrix checkGlErrorglGetUniformLocation 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 int1 GLES20.glGenTextures1 textures 0 mTextureID textures0 GLES20.glBindTextureGLES20.GL_TEXTURE_2D mTextureID GLES20.glTexParameterfGLES20.GL_TEXTURE_2D GLES20.GL_TEXTURE_MIN_FILTER GLES20.GL_NEAREST GLES20.glTexParameterfGLES20.GL_TEXTURE_2D GLES20.GL_TEXTURE_MAG_FILTER GLES20.GL_LINEAR GLES20.glTexParameteriGLES20.GL_TEXTURE_2D GLES20.GL_TEXTURE_WRAP_S GLES20.GL_REPEAT GLES20.glTexParameteriGLES20.GL_TEXTURE_2D GLES20.GL_TEXTURE_WRAP_T GLES20.GL_REPEAT InputStream is mContext.getResources.openRawResourceR.raw.robot Bitmap bitmap try bitmap BitmapFactory.decodeStreamis finally try is.close catch IOException e // Ignore. GLUtils.texImage2DGLES20.GL_TEXTURE_2D 0 bitmap 0 bitmap.recycle Matrix.setLookAtMmVMatrix 0 0 0 -5 0f 0f 0f 0f 1.0f 0.0f private int loadShaderint shaderType String source int shader GLES20.glCreateShadershaderType if shader 0 GLES20.glShaderSourceshader source GLES20.glCompileShadershader int compiled new int1 GLES20.glGetShaderivshader GLES20.GL_COMPILE_STATUS compiled 0 if compiled0 0 Log.eTAG Could not compile shader shaderType : Log.eTAG GLES20.glGetShaderInfoLogshader GLES20.glDeleteShadershader shader 0 return shader private int createProgramString vertexSource String fragmentSource int vertexShader loadShaderGLES20.GL_VERTEX_SHADER vertexSource if vertexShader 0 return 0 int pixelShader loadShaderGLES20.GL_FRAGMENT_SHADER fragmentSource if pixelShader 0 return 0 int program GLES20.glCreateProgram if program 0 GLES20.glAttachShaderprogram vertexShader checkGlErrorglAttachShader GLES20.glAttachShaderprogram pixelShader checkGlErrorglAttachShader GLES20.glLinkProgramprogram int linkStatus new int1 GLES20.glGetProgramivprogram GLES20.GL_LINK_STATUS linkStatus 0 if linkStatus0 GLES20.GL_TRUE Log.eTAG Could not link program: Log.eTAG GLES20.glGetProgramInfoLogprogram GLES20.glDeleteProgramprogram program 0 return program private void checkGlErrorString op int error while error GLES20.glGetError GLES20.GL_NO_ERROR Log.eTAG op : glError error throw new RuntimeExceptionop : 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 uMVPMatrixn attribute vec4 aPositionn attribute vec2 aTextureCoordn varying vec2 vTextureCoordn void main n gl_Position uMVPMatrix aPositionn vTextureCoord aTextureCoordn n private final String mFragmentShader precision mediump floatn varying vec2 vTextureCoordn uniform sampler2D sTexturen void main n gl_FragColor texture2DsTexture vTextureCoordn n private float mMVPMatrix new float16 private float mProjMatrix new float16 private float mMMatrix new float16 private float mVMatrix new float16 private int mProgram private int mTextureID private int muMVPMatrixHandle private int maPositionHandle private int maTextureHandle private Context mContext private static String TAG GLES20TriangleRenderer