{"id":628,"date":"2018-02-24T14:52:00","date_gmt":"2018-02-24T06:52:00","guid":{"rendered":"http:\/\/60.205.208.74\/?p=628"},"modified":"2018-02-24T14:52:00","modified_gmt":"2018-02-24T06:52:00","slug":"android%e7%b3%bb%e7%bb%9f%e4%b8%8b%e4%bd%bf%e7%94%a8opengl%e5%bc%80%e5%8f%91demo","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=628","title":{"rendered":"Android\u7cfb\u7edf\u4e0b\u4f7f\u7528OpenGL\u5f00\u53d1demo"},"content":{"rendered":"<span itemprop=\"description\"><p>\u5bf9openGL\u65e9\u6709\u8033\u95fb\uff0c\u4f46\u662f\u521a\u63a5\u89e6android\u4e0b\u7684opengl,\u505a\u4e2ademo\u5b66\u4e60\u4e0b\uff0c<\/p>\n<p>\u5148\u628aopengl\u7684\u6846\u67b6\u642d\u5efa\u8d77\u6765:\u53ea\u8981\u6709\u5982\u4e0b\u51e0\u4e2a\u6587\u4ef6\uff0c<\/p>\n<p>openActivity.java &#8212;-\u4f5c\u4e3a\u542f\u52a8\u5165\u53e3,android\u901a\u8fc7GLSurfaceView\u663e\u793aopenGL\u8bd5\u56fe\uff0c\u800cGLSurfaceView\u63d0\u4f9b\u4e86\u4e13\u95e8\u6e32\u67d33D\u7684Renderer\u63a5\u53e3<\/p>\n<p>GLRender.java &#8212;-\u6e32\u67d3\u56fe\u5f62<\/p>\n<p>GLImage.java &#8212;-\u83b7\u53d6\u8d44\u6e90\u6587\u4ef6<\/p>\n<p>GLRender.java \u5b9e\u73b0Renderer\u63a5\u53e3\uff1a\u53ea\u8981\u5b9e\u73b0\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p>public void onDrawFrame(GL10 gl); \/\/\u8fdb\u884c\u56fe\u5f62\u7684\u7ed8\u5236<\/p>\n<p>public void onSurfaceChanged(GL10 gl, int width, int height);<\/p>\n<p>public void onSurfaceCreated(GL10 gl, EGLConfig config);<\/p>\n<p>GLRender.java \u4ee3\u7801\u5982\u4e0b\uff1a<br \/>\npackage com.aurora.android.render;<\/p>\n<p>import java.nio.ByteBuffer;<br \/>\nimport java.nio.IntBuffer;<br \/>\nimport java.util.Random;<\/p>\n<p>import javax.microedition.khronos.egl.EGLConfig;<br \/>\nimport javax.microedition.khronos.opengles.GL10;<\/p>\n<p>import com.aurora.android.model.GLImage;<br \/>\nimport com.aurora.android.model.Star;<\/p>\n<p>import android.opengl.GLUtils;<br \/>\nimport android.opengl.GLSurfaceView.Renderer;<br \/>\nimport android.view.KeyEvent;<\/p>\n<p>public class GLRender implements Renderer {<\/p>\n<p>public static final int num = 50;<br \/>\npublic Star[] star = new Star[num];<br \/>\nRandom random = new Random();<br \/>\nboolean key;<br \/>\nboolean twinkle = true;<br \/>\nfloat spin;<br \/>\nfloat zoom = -10.0f;<br \/>\nfloat tilt = 90.0f;<br \/>\nint texture;<\/p>\n<p>int one = 0x10000;<\/p>\n<p>\/\/\u7eb9\u7406\u6570\u636e<br \/>\nIntBuffer coords = IntBuffer.wrap(new int[] {<br \/>\n0, 0,<br \/>\none, 0,<br \/>\none, one,<br \/>\n0,one });<\/p>\n<p>IntBuffer vertexs = IntBuffer.wrap(new int[] {<br \/>\n-one, -one, 0,<br \/>\none, -one, 0,<br \/>\none, one, 0,<br \/>\n-one, one, 0 });<\/p>\n<p>ByteBuffer indices = ByteBuffer.wrap(new byte[] {<br \/>\n1, 0, 2, 3<br \/>\n});<\/p>\n<p>@Override<br \/>\npublic void onDrawFrame(GL10 gl) {<br \/>\n\/\/ TODO Auto-generated method stub<\/p>\n<p>gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);<br \/>\ngl.glBindTexture(GL10.GL_TEXTURE_2D, texture);\/\/\u7ed1\u5b9a\u7eb9\u7406<\/p>\n<p>for (int i = 0; i &lt; num; i++) {<br \/>\ngl.glLoadIdentity();<\/p>\n<p>gl.glTranslatef(0.0f, 0.0f, zoom);<br \/>\ngl.glRotatef(tilt, 1.0f, 0.0f, 0.0f);<br \/>\ngl.glRotatef(star[i].angle, 0.0f, 1.0f, 0.0f);<br \/>\ngl.glTranslatef(star[i].dist, 0.0f, 0.0f);<br \/>\ngl.glRotatef(-star[i].angle, 0.0f, 1.0f, 0.0f);<br \/>\ngl.glRotatef(-tilt, 1.0f, 0.0f, 0.0f);<\/p>\n<p>gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);\/\/\u542f\u7528\u9876\u70b9<br \/>\ngl.glEnableClientState(GL10.GL_COLOR_ARRAY);\/\/\u542f\u7528\u7740\u8272<br \/>\ngl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);\/\/\u542f\u7528\u7eb9\u7406<\/p>\n<p>if (twinkle) {\/\/\u542f\u7528\u661f\u661f\u95ea\u70c1\u6548\u679c<br \/>\n\/\/\u4f7f\u7528byte\u578b\u6570\u503c\u6307\u5b9a\u4e00\u989c\u8272<br \/>\ngl.glColor4f((float) star[(num &#8211; i) &#8211; 1].r \/ 255.0f,<br \/>\n(float) star[(num &#8211; i) &#8211; 1].g \/ 255.0f,<br \/>\n(float) star[(num &#8211; i) &#8211; 1].b \/ 255.0f, 1.0f);<br \/>\ngl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexs);<br \/>\ngl.glTexCoordPointer(2, GL10.GL_FIXED, 0, coords);<\/p>\n<p>{<br \/>\ncoords.position(0);<br \/>\nvertexs.position(0);<br \/>\nindices.position(0);<br \/>\n\/\/\u7ed8\u56fe<br \/>\ngl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4,<br \/>\nGL10.GL_UNSIGNED_BYTE, indices);<\/p>\n<p>}<br \/>\n\/\/\u95ea\u70c1\u661f\u661f\u7ed8\u5236\u5b8c\u6210<br \/>\ngl.glFinish();<br \/>\n}<br \/>\n\/\/\u7ed5z\u8f74\u65cb\u8f6c<br \/>\ngl.glRotatef(spin, 0.0f, 0.0f, 1.0f);<\/p>\n<p>gl.glColor4f((float) star[(num &#8211; i) &#8211; 1].r \/ 255.0f,<br \/>\n(float) star[(num &#8211; i) &#8211; 1].g \/ 255.0f,<br \/>\n(float) star[(num &#8211; i) &#8211; 1].b \/ 255.0f, 1.0f);<br \/>\ngl.glVertexPointer(3, GL10.GL_FIXED, 0, vertexs);<br \/>\ngl.glTexCoordPointer(2, GL10.GL_FIXED, 0, coords);<\/p>\n<p>{<br \/>\ncoords.position(0);<br \/>\nvertexs.position(0);<br \/>\nindices.position(0);<\/p>\n<p>gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4,<br \/>\nGL10.GL_UNSIGNED_BYTE, indices);<\/p>\n<p>}<br \/>\ngl.glFinish();<\/p>\n<p>gl.glDisableClientState(GL10.GL_COLOR_ARRAY);<br \/>\ngl.glDisableClientState(GL10.GL_VERTEX_ARRAY);<br \/>\ngl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);<\/p>\n<p>spin += 0.01f;<br \/>\nstar[i].angle += (float) (i) \/ (float) num;<br \/>\nstar[i].dist -= 0.01f;<br \/>\nif (star[i].dist &lt; 0.0f) {<br \/>\nstar[i].dist += 5.0f;<br \/>\nstar[i].r = random.nextInt(256);<br \/>\nstar[i].g = random.nextInt(256);<br \/>\nstar[i].b = random.nextInt(256);<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p>\n<p>@Override<br \/>\npublic void onSurfaceChanged(GL10 gl, int width, int height) {<br \/>\n\/\/ TODO Auto-generated method stub<br \/>\nfloat ratio = (float) width \/ height;<br \/>\ngl.glViewport(0, 0, width, height);\/\/ \u8bbe\u7f6e\u573a\u666f\u5927\u5c0f<br \/>\ngl.glMatrixMode(GL10.GL_PROJECTION);\/\/ \u8bbe\u7f6e\u6295\u5f71\u77e9\u9635<br \/>\ngl.glLoadIdentity();<br \/>\ngl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);\/\/ \u8bbe\u7f6e\u89c6\u53e3\u5927\u5c0f<br \/>\ngl.glMatrixMode(GL10.GL_MODELVIEW);\/\/ \u8bbe\u7f6e\u89c2\u5bdf\u6a21\u5f0f<br \/>\ngl.glLoadIdentity();<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic void onSurfaceCreated(GL10 gl, EGLConfig config) {<br \/>\n\/\/ TODO Auto-generated method stub<\/p>\n<p>gl.glShadeModel(GL10.GL_SMOOTH);\/\/ \u542f\u7528\u5e73\u6ed1<br \/>\ngl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\/\/ \u8bbe\u7f6e\u9ed1\u80cc\u666f<br \/>\ngl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);\/\/ \u7cbe\u7ec6\u7684\u900f\u89c6\u4fee\u6b63<\/p>\n<p>IntBuffer intBuffer = IntBuffer.allocate(1);<br \/>\ngl.glGenTextures(1, intBuffer);<br \/>\ntexture = intBuffer.get();<\/p>\n<p>gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);<br \/>\ngl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,<br \/>\nGL10.GL_LINEAR);<br \/>\ngl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,<br \/>\nGL10.GL_LINEAR);<br \/>\nGLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 0);<\/p>\n<p>gl.glEnable(GL10.GL_TEXTURE_2D);\/\/ \u8bbe\u7f6e\u7eb9\u7406<br \/>\ngl.glShadeModel(GL10.GL_SMOOTH);\/\/ \u542f\u7528\u5e73\u6ed1<br \/>\ngl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\/\/ \u8bbe\u7f6e\u9ed1\u80cc\u666f<br \/>\ngl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);\/\/ \u7cbe\u7ec6\u7684\u900f\u89c6\u4fee\u6b63<br \/>\ngl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);\/\/ \u8bbe\u7f6e\u6df7\u5408\u51fd\u6570\u53d6\u534a\u900f\u660e<br \/>\ngl.glEnable(GL10.GL_BLEND);\/\/ \u8bbe\u7f6e\u6df7\u8272<\/p>\n<p>for (int i = 0; i &lt; num; i++) {<\/p>\n<p>Star starTemp = new Star();<br \/>\nstarTemp.angle = 0.0f;<br \/>\nstarTemp.dist = ((float) (i) \/ (float) num) * 5.0f;<\/p>\n<p>starTemp.r = random.nextInt(256);<br \/>\nstarTemp.g = random.nextInt(256);<br \/>\nstarTemp.b = random.nextInt(256);<br \/>\nstar[i] = starTemp;<\/p>\n<p>}<br \/>\n}<\/p>\n<p>public boolean onKeyUp(int keyCode, KeyEvent event) {<br \/>\ntwinkle = !twinkle;<br \/>\nreturn false;<br \/>\n}<br \/>\n}<\/p>\n<p>openActivity.java\u4ee3\u7801\u5982\u4e0b:<br \/>\npackage com.aurora.android.advanced;<\/p>\n<p>import com.aurora.android.model.GLImage;<br \/>\nimport com.aurora.android.render.GLRender;<\/p>\n<p>import android.app.Activity;<br \/>\nimport android.opengl.GLSurfaceView;<br \/>\nimport android.os.Bundle;<br \/>\nimport android.view.KeyEvent;<\/p>\n<p>public class OpenActivity extends Activity {<br \/>\nGLRender render = new GLRender();<\/p>\n<p>\/** Called when the activity is first created. *\/<br \/>\n@Override<br \/>\npublic void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nGLImage.load(this.getResources());<br \/>\nGLSurfaceView glView = new GLSurfaceView(this);<br \/>\nglView.setRenderer(render);<br \/>\nsetContentView(glView);<br \/>\n}<\/p>\n<p>public boolean onKeyUp(int keyCode, KeyEvent event){<br \/>\nrender.onKeyUp(keyCode, event);<br \/>\nreturn false;<br \/>\n}<br \/>\n}<\/p>\n<p>GLImage.java:<\/p>\n<p>package com.aurora.android.model;<\/p>\n<p>import com.aurora.android.advanced.R;<\/p>\n<p>import android.content.res.Resources;<br \/>\nimport android.graphics.Bitmap;<br \/>\nimport android.graphics.BitmapFactory;<\/p>\n<p>public class GLImage {<\/p>\n<p>public static Bitmap mBitmap;<\/p>\n<p>public static void load(Resources resource){<br \/>\n\/\/\u83b7\u53d6\u56fe\u7247\u8d44\u6e90<br \/>\nmBitmap = BitmapFactory.decodeResource(resource, R.drawable.star);<br \/>\n}<br \/>\n}<\/p>\n<p>Star.java:\u661f\u661f<\/p>\n<p>package com.aurora.android.model;<\/p>\n<p>public class Star {<\/p>\n<p>public int r,g,b; \/\/\u661f\u661fRGB\u989c\u8272<br \/>\npublic float dist;\/\/\u661f\u661f\u8ddd\u79bb\u4e2d\u5fc3\u7684\u8ddd\u79bb<br \/>\npublic float angle = 0.0f; \/\/\u5f53\u524d\u661f\u661f\u5904\u7684\u89d2\u5ea6<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/dl2.iteye.com\/upload\/attachment\/0059\/9057\/a16e3294-8f01-3289-8a54-4ebc70f49046.png\" target=\"_blank\" rel=\"noopener\">\u8fd0\u884c\u540e\u7684\u6548\u679c<\/a><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/dl2.iteye.com\/upload\/attachment\/0059\/9057\/a16e3294-8f01-3289-8a54-4ebc70f49046.png\" alt=\"\u8fd0\u884c\u540e\u7684\u6548\u679c\" width=\"551\" height=\"531\" \/><\/p>\n<p><a href=\"http:\/\/dl.iteye.com\/topics\/download\/b9d0b23a-ed31-3b23-bd08-4bfb58259af6\">AdvancedOpenGL.rar<\/a><\/p>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">\u5bf9openGL\u65e9\u6709\u8033\u95fb\uff0c\u4f46\u662f\u521a\u63a5\u89e6android\u4e0b\u7684opengl,\u505a\u4e2ademo\u5b66\u4e60\u4e0b\uff0c \u5148&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=628\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,26,12],"tags":[42,99],"class_list":["post-628","post","type-post","status-publish","format-standard","hentry","category-android","category-mobile","category-blog","tag-android","tag-opengl","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=628"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/628\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}