{"id":4386,"date":"2018-08-03T19:30:06","date_gmt":"2018-08-03T11:30:06","guid":{"rendered":"http:\/\/www.bookcard.net\/wordpress\/?p=4386"},"modified":"2018-10-14T21:28:03","modified_gmt":"2018-10-14T13:28:03","slug":"android_game_vertices","status":"publish","type":"post","link":"https:\/\/www.bookcard.net\/wordpress\/android_game_vertices\/","title":{"rendered":"Android\u904a\u6232\u4e4b\u9802\u9ede\u7d22\u5f15"},"content":{"rendered":"<figure id=\"attachment_4387\" aria-describedby=\"caption-attachment-4387\" style=\"width: 1200px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/08\/Android_Game_Vertices.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4387\" src=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/08\/Android_Game_Vertices.jpg\" alt=\"\" width=\"1200\" height=\"800\" srcset=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/08\/Android_Game_Vertices.jpg 1200w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/08\/Android_Game_Vertices-300x200.jpg 300w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/08\/Android_Game_Vertices-768x512.jpg 768w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/08\/Android_Game_Vertices-1024x683.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><figcaption id=\"caption-attachment-4387\" class=\"wp-caption-text\">Android\u904a\u6232\u4e4b\u9802\u9ede\u7d22\u5f15<\/figcaption><\/figure>\n<p>OpenGL\u5747\u63a1\u7528\u4e09\u89d2\u5f62\u5217\u8868\u9032\u884c\u6e32\u67d3.\u6bcf\u500b\u4e09\u89d2\u5f62\u90fd\u6709\u4e09\u500b\u9802\u9ede.\u5728\u6709\u4e9b\u60c5\u6cc1\u4e0b\u5169\u500b\u6216\u591a\u500b\u4e09\u89d2\u5f62\u6703\u5171\u7528\u9802\u9ede.\u5982\u4e0a\u5716\u6709\u5169\u500b\u9802\u9ede\u5177\u6709\u76f8\u540c\u300e\u4f4d\u7f6e\u300f\u300e\u984f\u8272\u300f\u300e\u7d0b\u7406\u5ea7\u6a19\u300f.\u4f46\u7518\u6d6a\u8cbb\u7a7a\u9593.\u66f4\u597d\u89e3\u537b\u65b9\u6cd5\u662f\u5c07\u6240\u6709\u9802\u9ede\u4fdd\u5b58\u5728\u5217\u8868.\u800c\u4e09\u89d2\u5f62\u9802\u9ede\u4fdd\u5b58\u7d22\u5f15.OpenGL ES\u8981\u6c42\u7d22\u5f15\u503c\u4f7f\u7528\u77ed\u6574\u6578\u6216\u5b57\u7bc0.\u5373OpenGL ES\u6bcf\u6b21\u6700\u591a\u6e32\u67d365536\u500b\u9802\u9ede.<\/p>\n<p>\u6211\u5730\u9700\u8981\u4e00\u500bVertices\u985e,\u7528\u65bc\u5b58\u5132\u6bcf\u500b\u9802\u9ede\u300e\u4f4d\u7f6e\u300f\u300e\u984f\u8272\u300f\u8207\u300e\u7d0b\u7406\u5ea7\u6a19\u300f,\u4e26\u4e14\u5b83\u9700\u8981\u5169\u500b\u9078\u9805.\u9802\u9ede\u662f\u5426\u300e\u984f\u8272\u300f\u8207\u300e\u7d0b\u7406\u5ea7\u6a19\u300f<\/p>\n<p>public class VERTICES {<\/p>\n<p>boolean color_have = false;\/\/\u00a0 \u64c1\u6709\u984f\u8272<\/p>\n<p>boolean texture_have =false;\/\/ \u64c1\u6709\u7d0b\u7406\u5750\u6a19<\/p>\n<p>int vertex_size = 0;\/\/ \u6bcf\u500b\u9802\u9ede\u6240\u5360\u5927\u5c0f<\/p>\n<p>\u4f7f\u7528FloatBuffer\u4fdd\u5b58\u9802\u9ede<\/p>\n<p>FloatBuffer\u00a0 vertex_array = null;<\/p>\n<p>\u4f7f\u7528ShortBuffer\u9802\u9ede\u7d22\u5f15<\/p>\n<p>ShortBuffer index_array = null;<\/p>\n<p>\u9802\u9ede\u968a\u5217\u5206\u914d\u8a18\u61b6\u9ad4,vertex_max\u70ba\u6700\u5927\u9802\u9ede\u91cf, index_max\u70ba\u6700\u5927\u7d22\u5f15\u91cf<\/p>\n<p>VERTICES(int vertex_max,int index_max,boolean color_have,boolean texture_have){<\/p>\n<p>this.color_have\u00a0 = color_have;\/\/\u00a0 \u64c1\u6709\u9802\u9ede\u984f\u8272<\/p>\n<p>this.texture_have = texture_have;\/\/ \u64c1\u6709\u7d0b\u7406\u5750\u6a19<\/p>\n<p>\u8a08\u7b97\u6bcf\u9802\u9ede\u6240\u5360\u5927\u5c0f.\u984f\u8272\u53604\u55ae\u5143.\u7d0b\u7406\u5ea7\u6a19\u53602\u55ae\u5143.2D\u5ea7\u6a19\u53602\u55ae\u5143.\u6bcf\u500b\u6574\u6578\u53604\u5b57\u7bc0<\/p>\n<p>int vertex_size = (2 + (color_have?4:0) + (texture_have?2:0) ) * 4;<\/p>\n<p>\u56e0\u70baOpenGL ES\u662f\u4ee5C API\u7d50\u53e3\u63d0\u4f9b.\u7121\u6cd5\u76f4\u63a5\u4f7f\u7528JAVA\u6578\u7d44.\u56e0\u6b64\u4f60\u9700\u8981C\u6578\u7d44\u7cfb\u7d71\u5806\u68e7\u8a18\u61b6\u9ad4.\u800c\u975eJAVA\u865b\u64ec\u6a5f\u8a18\u61b6\u9ad4.\u9700\u8981 FloatBuffer\u5206\u914d\u9802\u9ede\u8a18\u61b6\u9ad4.<\/p>\n<p>vertex_max\u70ba\u6700\u5927\u9802\u9ede\u91cf.<\/p>\n<p>ByteBuffer buffer = ByteBuffer.allocateDirect(vertex_size * vertex_max);<\/p>\n<p>\u5c07\u300e\u7db2\u7d61\u5b57\u7bc0\u300f\u6539\u70ba\u300e\u4e3b\u6a5f\u5b57\u7bc0\u300f\u6216\u7a31\u70ba\u300eCPU\u5b57\u7bc0\u300f<\/p>\n<p>buffer.order(ByteOrder.nativeOrder());<\/p>\n<p>\u7372\u53d6\u6574\u6578\u6578\u7d44<\/p>\n<p>vertex_array = buffer.asIntBuffer();<\/p>\n<p>\u6bcf\u500b\u77ed\u6574\u5f62\u5360\u5169\u500b\u5b57\u7bc0.index_max\u70ba\u6700\u5927\u7d22\u5f15\u91cf<\/p>\n<p>buffer = ByteBuffer.allocateDirect(index_max * Short.SIZE\/8);<\/p>\n<p>\u5c07\u300e\u7db2\u7d61\u5b57\u7bc0\u300f\u6539\u70ba\u300e\u4e3b\u6a5f\u5b57\u7bc0\u300f<\/p>\n<p>buffer.order(ByteOrder.nativeOrder());<\/p>\n<p>\u9802\u9ede\u77ed\u6574\u6578\u6578\u7d44<\/p>\n<p>index_array = buffer.asShortBuffer();<\/p>\n<p>}<\/p>\n<p>\u5c07\u9802\u9ede\u63d0\u4ea4\u7d66OpenGL\u6578\u7d44\u4e26\u89f8\u767c<\/p>\n<p>public void SetVertices(float[] vertices,int offset,int count){<\/p>\n<p>vertex_array.clear();\/\/ \u6e05\u7a7a\u7de9\u5b58.\u8a2d\u5b9a\u7576\u524d\u4f4d\u7f6e<\/p>\n<p>int len = offset + count;<\/p>\n<p>for(int i=offset, j=0; i &lt; len; i++, j++)<\/p>\n<p>vertex_Buffer[j] = Float.floatToRawIntBits(vertices[i]);<\/p>\n<p>vertex_array.put(vertex_Buffer, 0, count); \/\/ \u5beb\u5165\u6578\u64da.\u79fb\u52d5\u7576\u524d\u4f4d\u7f6e<\/p>\n<p>vertex_array.flip();\/\/ \u89f8\u767c<\/p>\n<p>}<\/p>\n<p>\u5c07\u9802\u9ede\u7d22\u5f15\u63d0\u4ea4\u7d66OpenGL\u6578\u7d44<\/p>\n<p>public void SetIndices(short[] indices,int offset,int count) {<\/p>\n<p>index_array.clear();\/\/ \u6e05\u7a7a\u7de9\u5b58.\u8a2d\u5b9a\u7576\u524d\u4f4d\u7f6e<\/p>\n<p>index_array.put(indices, offset, count); \/\/ \u5beb\u5165\u6578\u64da.\u79fb\u52d5\u7576\u524d\u4f4d\u7f6e<\/p>\n<p>index_array.flip();\/\/ \u89f8\u767c<\/p>\n<p>}<\/p>\n<p>\u7d81\u5b9a\u6578\u64da<\/p>\n<p>public void Bind(){<\/p>\n<p>GL10\u00a0 gl = GRAPHICS.gl;<\/p>\n<p>\u555f\u7528\u9802\u9ede\u6578\u7d44<\/p>\n<p>gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);<\/p>\n<p>\u8a2d\u7f6e\u7576\u524d\u5beb\u5165\u4f4d\u7f6e0<\/p>\n<p>vertex_array.position(0);<\/p>\n<p>\u8a2d\u7f6e\u9802\u9ede\u6307\u91dd,\u6bcf\u500b\u9802\u9ede\u5169\u500b\u5143\u7d20.xy\u5169\u5206\u91cf<\/p>\n<p>gl.glVertexPointer(2, GL10.GL_FLOAT, vertex_size, vertex_array);<\/p>\n<p>if(color_have == true){\/\/\u984f\u8272<\/p>\n<p>\u555f\u7528\u9802\u9ede\u984f\u8272\u6578\u7d44<\/p>\n<p>gl.glEnableClientState(GL10.GL_COLOR_ARRAY);<\/p>\n<p>\u8a2d\u7f6e\u7576\u524d\u5beb\u5165\u4f4d\u7f6e2<\/p>\n<p>vertex_array.position(2);<\/p>\n<p>\u8a2d\u7f6e\u984f\u8272\u6307\u91dd,RGBA\u56db\u5206\u91cf<\/p>\n<p>gl.glColorPointer(4, GL10.GL_FLOAT, vertex_size, vertex_array);<\/p>\n<p>}<\/p>\n<p>if(texture_have ){\/\/ \u7d0b\u7406\u5750\u6a19<\/p>\n<p>\u555f\u7528\u7d0b\u7406\u5750\u6a19\u6578\u7d44<\/p>\n<p>gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);<\/p>\n<p>vertex_array.position(color_have?6:2);\/\/ \u5beb\u5165\u4f4d\u7f6e<\/p>\n<p>\u8a2d\u7f6e\u7d0b\u7406\u5750\u6a19\u6307\u91dd,UV\u5169\u5206\u91cf<\/p>\n<p>gl.glTexCoordPointer(2, GL10.GL_FLOAT, vertex_size, vertex_array);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u53d6\u6d88\u7d81\u5b9a\u6578\u64da<\/p>\n<p>public void Unbind(){<\/p>\n<p>GL10 gl = GRAPHICS.gl;<\/p>\n<p>\u95dc\u9589\u9802\u9ede\u7d0b\u7406\u6578\u7d44<\/p>\n<p>if(color_have)<\/p>\n<p>gl.glDisableClientState(GL10.GL_COLOR_ARRAY );<\/p>\n<p>\u95dc\u9589\u9802\u9ede\u984f\u8272\u6578\u7d44<\/p>\n<p>if(texture_have)<\/p>\n<p>gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);<\/p>\n<p>}<\/p>\n<p>\u7e6a\u756b\u4e09\u89d2\u5f62<\/p>\n<p>public void Draw(int mode,int offset,int count){<\/p>\n<p>GL10 gl = GRAPHICS.gl;<\/p>\n<p>\u7e6a\u756b\u9802\u9ede<\/p>\n<p>if(index_array != null){\/\/ \u7e6a\u756b<\/p>\n<p>index_array.position(offset);\/\/<\/p>\n<p>gl.glDrawElements(mode, count, GL10.GL_UNSIGNED_SHORT, index_array);<\/p>\n<p>}<\/p>\n<p>else{<\/p>\n<p>gl.glDrawArrays(mode, offset, count);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OpenGL\u5747\u63a1\u7528\u4e09\u89d2\u5f62\u5217\u8868\u9032\u884c\u6e32\u67d3.\u6bcf\u500b\u4e09\u89d2\u5f62\u90fd\u6709\u4e09\u500b\u9802\u9ede.\u5728\u6709\u4e9b\u60c5\u6cc1\u4e0b\u5169\u500b\u6216\u591a\u500b\u4e09\u89d2\u5f62\u6703\u5171\u7528\u9802\u9ede.\u5982\u4e0a\u5716\u6709 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/android_game_vertices\/\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">Android\u904a\u6232\u4e4b\u9802\u9ede\u7d22\u5f15<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,62],"tags":[2],"class_list":["post-4386","post","type-post","status-publish","format-standard","hentry","category-android","category-opengl","tag-android","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/comments?post=4386"}],"version-history":[{"count":1,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4386\/revisions"}],"predecessor-version":[{"id":4388,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4386\/revisions\/4388"}],"wp:attachment":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/media?parent=4386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/categories?post=4386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/tags?post=4386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}