{"id":4333,"date":"2018-07-27T12:51:53","date_gmt":"2018-07-27T04:51:53","guid":{"rendered":"http:\/\/www.bookcard.net\/wordpress\/?p=4333"},"modified":"2018-10-14T21:28:40","modified_gmt":"2018-10-14T13:28:40","slug":"android_game_texture_mapping","status":"publish","type":"post","link":"https:\/\/www.bookcard.net\/wordpress\/android_game_texture_mapping\/","title":{"rendered":"Android\u904a\u6232\u4e4b\u7d0b\u7406\u6620\u5c04"},"content":{"rendered":"<figure id=\"attachment_4334\" aria-describedby=\"caption-attachment-4334\" style=\"width: 1200px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Texture_mapping.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4334\" src=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Texture_mapping.jpg\" alt=\"\" width=\"1200\" height=\"800\" srcset=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Texture_mapping.jpg 1200w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Texture_mapping-300x200.jpg 300w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Texture_mapping-768x512.jpg 768w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Texture_mapping-1024x683.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><figcaption id=\"caption-attachment-4334\" class=\"wp-caption-text\">Android\u904a\u6232\u4e4b\u7d0b\u7406\u6620\u5c04<\/figcaption><\/figure>\n<p>\u5c07\u300e<a href=\"https:\/\/www.bookcard.net\/wordpress\/android_game_bitmap_read\/\">Bitmap<\/a>\u300f\u52a0\u8f09\u7d66OpenGL ES\u7136\u5f8c\u52a0\u8f09\u5230\u5716\u5f62\u5361\u8a18\u61b6\u9ad4\u91cd,\u4e26\u6700\u7d42\u4ea4\u7d66GPU\u6e32\u67d3.\u56e0\u70baOpenGL\u6700\u7d42\u4ee5\u4e09\u89d2\u5f62\u70ba\u55ae\u4f4d\u9032\u884c\u6e32\u67d3.\u800c\u5c07\u300e<a href=\"https:\/\/www.bookcard.net\/wordpress\/android_game_bitmap_read\/\">Bitmap<\/a>\u300f\u6620\u5c04\u5230\u4e09\u89d2\u5f62\u4e0a.\u9700\u8981\u70ba\u4e09\u89d2\u5f62\u6bcf\u500b\u9802\u9ede\u8a2d\u5b9a\u7d0b\u7406\u5ea7\u6a19(texture coordinates).3D\u5ea7\u6a19(x,y,z)\u548c2D\u5ea7\u6a19(x,y)\u5c0d\u61c9\u7d0b\u7406\u5ea7\u6a19(u,v)\u4f4d\u5716\u9802\u9ede.\u800c(u,v)\u5ea7\u6a19\u76f8\u7576\u65bc(\u6a6b,\u7e31)\u5ea7\u6a19.\u7d0b\u7406\u5bec\u8207\u9ad8\u5fc5\u9700\u662f2\u500d\u6578.<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"277\">\u7d0b\u7406\u6620\u5c04<\/td>\n<td width=\"277\">(u,v)<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u5de6\u4e0a\u89d2<\/td>\n<td width=\"277\">(0,0)<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u53f3\u4e0b\u89d2<\/td>\n<td width=\"277\">(1,1)\u5373\u4f7f\u5bec\u8207\u9ad8\u4e0d\u76f8\u7b49<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u53f3\u4e0a\u89d2<\/td>\n<td width=\"277\">(1.0)<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u5de6\u4e0b\u89d2<\/td>\n<td width=\"277\">(0,1)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u751f\u6210\u7d0b\u7406\u968a\u5217,\u7528\u65bc\u4fdd\u5b58\u7d0b\u7406ID<\/p>\n<p>int[] ids = new int[1];<\/p>\n<p>\u751f\u6210\u7d0b\u7406\u4e26\u7372\u53d6id.\u7d0b\u7406\u4f9d\u7136\u70ba\u7a7a<\/p>\n<p>gl.glGenTextures(1, ids,0);<\/p>\n<p>\u7d0b\u7406id<\/p>\n<p>int id = ids[0];<\/p>\n<p>\u8b80\u53d6\u4f4d\u5716<\/p>\n<p><a href=\"https:\/\/www.bookcard.net\/wordpress\/android_game_bitmap_read\/\">Bitmap bitmap = BITMAP.Read(file_name);<\/a><\/p>\n<p>\u7372\u53d6\u4f4d\u5716\u5bec\u5ea6<\/p>\n<p>int\u00a0\u00a0 width = bitmap.getWidth();<\/p>\n<p>\u7372\u53d6\u4f4d\u5716\u9ad8\u5ea6<\/p>\n<p>int\u00a0\u00a0 height = bitmap.getHeight();<\/p>\n<p>\u7d81\u5b9a2D\u7d0b\u7406<\/p>\n<p>gl.glBindTexture(GL10.GL_TEXTURE_2D, id);<\/p>\n<p>\u4e0a\u50b32D\u7d0b\u7406<\/p>\n<p>GLUtils.texImage2D(GL10.GL_TEXTURE_2D,0,<a href=\"https:\/\/www.bookcard.net\/wordpress\/android_game_bitmap_read\/\">bitmap<\/a>,0);<\/p>\n<p>\u8a2d\u7f6e\u7d0b\u7406\u5c6c\u6027\u6307\u5b9a\u7e2e\u5c0f\u500d\u6578\u904e\u6ffe\u5668<\/p>\n<p>gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);<\/p>\n<p>\u8a2d\u7f6e\u7d0b\u7406\u5c6c\u6027\u6307\u5b9a\u653e\u5927\u500d\u6578\u904e\u6ffe\u5668<\/p>\n<p>gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST);<\/p>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"277\">\u904e\u6ffe<\/td>\n<td width=\"277\">\u7c21\u4ecb<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">GL10.GL_NEAREST<\/td>\n<td width=\"277\">\u50cf\u7d20\u63a1\u6a23\u9ede\u6700\u63a5\u8fd1\u4e2d\u5fc3\u9ede\u300e\u6e05\u6670\u300f<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">GL10.GL_LINEAR<\/td>\n<td width=\"277\">\u50cf\u7d20\u63a1\u6a23\u9ede\u7dda\u6027\u63d2\u503c\u5e73\u5747\u52a0\u6b0a\u300e\u6a21\u7cca\u300f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53d6\u6d88\u7d81\u5b9a\u7d0b\u7406ID<\/p>\n<p>gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);<\/p>\n<p>\u56de\u6536\u4f4d\u5716.\u4ee5\u514d\u6d6a\u8cbb\u8a18\u61b6\u9ad4<\/p>\n<p><a href=\"https:\/\/www.bookcard.net\/wordpress\/android_game_bitmap_read\/\">bitmap.recycle()<\/a>;<\/p>\n<p>\u6bcf\u7576Activity\u88ab\u66ab\u505c(APP\u5207\u63db)GLSurfaceView\u90fd\u6703\u88ab\u92b7\u6bc0.\u6bcf\u7576Activity\u6062\u5fa9\u6642\u8abf\u7528GLSurfaceView. onResume()\u8b93GLSurfaceView\u91cd\u69cb\u6e32\u67d3\u754c\u9762.\u4f46OpenGL ES\u4e2d\u4e0a\u50b3\u7d0b\u7406\u90fd\u6703\u4e1f\u5931.\u4f60\u9700\u8981\u5728onSurfaceCreated()\u4e2d\u9700\u91cd\u65b0\u8f09\u5165\u4e26\u4e0a\u50b3\u7d0b\u7406.<\/p>\n<p>\u91cd\u65b0\u8f09\u5165\u4ee3\u78bc\u898b\u4e0a\u6587<\/p>\n<p>Texture.load(filen_ame);<\/p>\n<p>\u91cd\u65b0\u7d81\u5b9a\u7d0b\u7406\u5f8c\u8a2d\u7f6e\u7d0b\u7406\u5c6c\u6027<\/p>\n<p>gl.glBindTexture(GL10.GL_TEXTURE_2D, id);<\/p>\n<p>gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);<\/p>\n<p>gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST);<\/p>\n<p>\u53d6\u6d88\u7d81\u5b9a\u7d0b\u7406<\/p>\n<p>gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);<\/p>\n<p>&nbsp;<\/p>\n<p>\u7576\u904a\u6232\u9000\u51fa.\u4fbf\u8981\u522a\u9664\u7d0b\u7406\u91cb\u653e\u8a18\u61b6\u9ad4.\u9996\u5148\u53d6\u6d88\u7d0b\u7406\u90a6\u5b9a<\/p>\n<p>gl.glBindTexture(GL10.GL_TEXTURE_2D, id);<\/p>\n<p>\u522a\u9664\u7d0b\u7406<\/p>\n<p>int[] ids = { id };<\/p>\n<p>gl.glDeleteTextures(1,ids,0);<\/p>\n<p>\u6e32\u67d3\u4e09\u89d2\u5f62\u6642\u8981\u544a\u77e5OpenGL\u8981\u555f\u7528\u7d0b\u7406\u6e32\u67d3<\/p>\n<p>gl.glEnable(GL10.GL_TEXTURE_2D);<\/p>\n<p>\u5b8c\u6210\u6e32\u67d3\u5f8c\u7981\u7528\u7d0b\u7406\u6e32\u67d3<\/p>\n<p>gl.glDisable(GL10.GL_TEXTURE_2D);<\/p>\n<p>2D\u904a\u6232\u901a\u5e38\u53ea\u4f54\u7528\u5716\u50cf\u4e00\u5c0f\u7247\u5340\u57df.\u9700\u8981\u5c07\u50cf\u7d20\u5ea7\u6a19(x,y)\u8f49\u63db\u70ba\u7d0b\u7406\u5ea7\u6a19(u,v)<\/p>\n<p>\u8a08\u7b97\u7d0b\u7406\u5ea7\u6a19\u5de6\u4e0a\u89d2<\/p>\n<p>u1 = x\/texture.width;<\/p>\n<p>v1 = y\/texture.height;<\/p>\n<p>\u8a08\u7b97\u7d0b\u7406\u5ea7\u6a19\u53f3\u4e0b\u89d2<\/p>\n<p>u2 = u1 + width\/texture.width;<\/p>\n<p>v2 = v1 + height\/texture.height;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c07\u300eBitmap\u300f\u52a0\u8f09\u7d66OpenGL ES\u7136\u5f8c\u52a0\u8f09\u5230\u5716\u5f62\u5361\u8a18\u61b6\u9ad4\u91cd,\u4e26\u6700\u7d42\u4ea4\u7d66GPU\u6e32\u67d3.\u56e0\u70baOpenGL\u6700\u7d42 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/android_game_texture_mapping\/\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">Android\u904a\u6232\u4e4b\u7d0b\u7406\u6620\u5c04<\/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,50],"class_list":["post-4333","post","type-post","status-publish","format-standard","hentry","category-android","category-opengl","tag-android","tag-opengl","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4333","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=4333"}],"version-history":[{"count":3,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4333\/revisions"}],"predecessor-version":[{"id":4384,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4333\/revisions\/4384"}],"wp:attachment":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/media?parent=4333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/categories?post=4333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/tags?post=4333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}