实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.tim.shadow.ball; import static android.opengl.GLES20.*; import static android.opengl.Matrix.*; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.util.ArrayList; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.opengl.GLES20; import android.opengl.GLSurfaceView.Renderer; import android.opengl.Matrix; import com.tim.shadow.glutils.TimGL2Utils; public class Ball implements Renderer { Context mContext; private int mProgram; private int mAPositionHandler; private int mUProjectMatrixHandler; private int mATextureCoordHandler; private final float[] projectMatrix = new float[16]; private int mSize; private FloatBuffer vertexBuff; private FloatBuffer textureBuff; private int textrueID; public Ball(Context context) { mContext = context; init(); } public void init() { int perVertex = 36; double perRadius = 2 * Math.PI / (float) perVertex; double perW = 1 / (float) perVertex; double perH = 1 / (float) (perVertex); ArrayList<Float> vetexList = new ArrayList<Float>(); ArrayList<Float> textureList = new ArrayList<Float>(); for (int a = 0; a < perVertex; a ) { for (int b = 0; b < perVertex; b ) { float w1 = (float) (a * perH); float h1 = (float) (b * perW); float w2 = (float) ((a 1) * perH); float h2 = (float) (b * perW); float w3 = (float) ((a 1) * perH); float h3 = (float) ((b 1) * perW); float w4 = (float) (a * perH); float h4 = (float) ((b 1) * perW); textureList.add(h1); textureList.add(w1); textureList.add(h2); textureList.add(w2); textureList.add(h3); textureList.add(w3); textureList.add(h3); textureList.add(w3); textureList.add(h4); textureList.add(w4); textureList.add(h1); textureList.add(w1); float x1 = (float) (Math.sin(a * perRadius / 2) * Math.cos(b * perRadius)); float z1 = (float) (Math.sin(a * perRadius / 2) * Math.sin(b * perRadius)); float y1 = (float) Math.cos(a * perRadius / 2); float x2 = (float) (Math.sin((a 1) * perRadius / 2) * Math .cos(b * perRadius)); float z2 = (float) (Math.sin((a 1) * perRadius / 2) * Math .sin(b * perRadius)); float y2 = (float) Math.cos((a 1) * perRadius / 2); float x3 = (float) (Math.sin((a 1) * perRadius / 2) * Math .cos((b 1) * perRadius)); float z3 = (float) (Math.sin((a 1) * perRadius / 2) * Math .sin((b 1) * perRadius)); float y3 = (float) Math.cos((a 1) * perRadius / 2); float x4 = (float) (Math.sin(a * perRadius / 2) * Math .cos((b 1) * perRadius)); float z4 = (float) (Math.sin(a * perRadius / 2) * Math .sin((b 1) * perRadius)); float y4 = (float) Math.cos(a * perRadius / 2); vetexList.add(x1); vetexList.add(y1); vetexList.add(z1); vetexList.add(x2); vetexList.add(y2); vetexList.add(z2); vetexList.add(x3); vetexList.add(y3); vetexList.add(z3); vetexList.add(x3); vetexList.add(y3); vetexList.add(z3); vetexList.add(x4); vetexList.add(y4); vetexList.add(z4); vetexList.add(x1); vetexList.add(y1); vetexList.add(z1); } } mSize = vetexList.size() / 3; float texture[] = new float[mSize * 2]; for (int i = 0; i < texture.length; i ) { texture[i] = textureList.get(i); } textureBuff = ByteBuffer.allocateDirect(texture.length * 4) .order(ByteOrder.nativeOrder()).asFloatBuffer(); textureBuff.put(texture); textureBuff.position(0); float vetex[] = new float[mSize * 3]; for (int i = 0; i < vetex.length; i ) { vetex[i] = vetexList.get(i); } vertexBuff = ByteBuffer.allocateDirect(vetex.length * 4) .order(ByteOrder.nativeOrder()).asFloatBuffer(); vertexBuff.put(vetex); vertexBuff.position(0); } @Override public void onDrawFrame(GL10 arg0) { rotateM(mCurrMatrix, 0, -xAngle, 1, 0, 0); rotateM(mCurrMatrix, 0, -yAngle, 0, 1, 0); rotateM(mCurrMatrix, 0, -zAngle, 0, 0, 1); glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glActiveTexture(GLES20.GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textrueID); glUniformMatrix4fv(mUProjectMatrixHandler, 1, false, getfinalMVPMatrix(), 0); glDrawArrays(GL_TRIANGLES, 0, mSize); } public float xAngle; public float yAngle; public float zAngle; final float mCurrMatrix[] = new float[16]; final float mMVPMatrix[] = new float[16]; public float[] getfinalMVPMatrix() { Matrix.multiplyMM(mMVPMatrix, 0, projectMatrix, 0, mCurrMatrix, 0); Matrix.setIdentityM(mCurrMatrix, 0); return mMVPMatrix; } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { glViewport(0, 0, width, height); GLES20.glEnable(GLES20.GL_CULL_FACE); float ratio = width / (float) height; frustumM(projectMatrix, 0, -ratio, ratio, -1, 1, 1, 20); Matrix.setIdentityM(mCurrMatrix, 0); Matrix.setIdentityM(mMVPMatrix, 0); translateM(projectMatrix, 0, 0, 0, -2); // rotateM(projectMatrix, 0, -90, 1, 0, 0); scaleM(projectMatrix, 0, 4, 4, 4); mProgram = TimGL2Utils.getProgram(mContext); glUseProgram(mProgram); mAPositionHandler = glGetAttribLocation(mProgram, "aPosition"); mUProjectMatrixHandler = glGetUniformLocation(mProgram, "uProjectMatrix"); mATextureCoordHandler = glGetAttribLocation(mProgram, "aTextureCoord"); System.out.println("mAPositionHandler:" mAPositionHandler); System.out.println("mUProjectMatrixHandler:" mUProjectMatrixHandler); System.out.println("mATextureCoordHandler:" mATextureCoordHandler); textrueID = TimGL2Utils.initTexture(mContext, R.drawable.p3); System.out.println("textureID:" textrueID); glVertexAttribPointer(mAPositionHandler, 3, GL_FLOAT, false, 0, vertexBuff); glVertexAttribPointer(mATextureCoordHandler, 2, GL_FLOAT, false, 0, textureBuff); glEnableVertexAttribArray(mAPositionHandler); glEnableVertexAttribArray(mATextureCoordHandler); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)