{"id":3855,"date":"2018-03-07T20:35:02","date_gmt":"2018-03-07T12:35:02","guid":{"rendered":"http:\/\/www.bookcard.net\/wordpress\/?p=3855"},"modified":"2018-10-14T21:22:05","modified_gmt":"2018-10-14T13:22:05","slug":"opengl_texture_terrain","status":"publish","type":"post","link":"https:\/\/www.bookcard.net\/wordpress\/opengl_texture_terrain\/","title":{"rendered":"OpenGL\u4e4b\u7d0b\u7406\u5730\u5f62"},"content":{"rendered":"<figure id=\"attachment_3856\" aria-describedby=\"caption-attachment-3856\" style=\"width: 1024px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/03\/OpenGL_texture_terrain.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3856\" src=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/03\/OpenGL_texture_terrain.jpg\" alt=\"\" width=\"1024\" height=\"768\" srcset=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/03\/OpenGL_texture_terrain.jpg 1024w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/03\/OpenGL_texture_terrain-300x225.jpg 300w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/03\/OpenGL_texture_terrain-768x576.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-3856\" class=\"wp-caption-text\">OpenGL\u4e4b\u7d0b\u7406\u5730\u5f62<\/figcaption><\/figure>\n<p>\u5730\u5f62\u6587\u6a94\u5176\u5be6\u5c31\u662f\u7070\u5ea6\u5716,\u6bcf\u4e00\u4f4d\u7070\u5ea6(0~255)\u5c0d\u61c9\u5176\u9ad8\u5ea6\u503c,\u7531\u9ad8\u5ea6\u503c\u7d44\u6210\u7684\u4e8c\u7dad\u9ede,\u4e8c\u7dad\u9ede\u4ee5\u6cbfX\u8ef8\u548cZ\u8ef8\u5206\u4f48. \u800cY\u8ef8\u5247\u4ee3\u8868\u5730\u5f62\u7684\u9ad8\u5ea6,\u5c07\u9019\u4e9b\u6578\u64da\u4f5c\u70ba\u7db2\u683c\u6e32\u67d3\u5f97\u5230\u5927\u6982\u7684\u5730\u8c8c\u300e\u5730\u5f62\u5716\u300f,\u5c07\u300e\u7070\u5ea6\u503c\u300f\u5269\u4ee5\u300e\u5730\u5716\u6bd4\u4f8b\u300f\u7684\u9ad8\u5730\u5f62\u7684\u9ad8\u5ea6\u503c,\u8f03\u4eae\u7684\u7070\u5ea6\u5c0d\u61c9\u65bc\u8f03\u9ad8\u7684\u5730\u5f62,\u8f03\u6697\u7684\u7070\u5ea6\u5c0d\u61c9\u65bc\u8f03\u4f4e\u7684\u5730\u5f62.<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"553\">\u300e\u9ad8\u5ea6\u503c=\u7070\u5ea6\u503c*\u5730\u5716\u5c3a\u5bf8\u6bd4\u4f8b\u56e0\u5b50\u300f<\/td>\n<\/tr>\n<tr>\n<td width=\"553\">\u300e\u5730\u5716\u9802\u9ede\u5ea7\u6a19=\u4e8c\u7dad\u9ede\u7d22\u5f15*\u5730\u5716\u5c3a\u5bf8\u6bd4\u4f8b\u56e0\u5b50\u300f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6e32\u67d3\u5730\u5f62\u6642\u6cbf\u8457Z\u8ef8\u65b9\u5411\u6bcf\u500b\u4e8c\u7dad\u9ede\u4f7f\u7528GL_TRIANGLE_STRIP\u7e6a\u756b\u76f8\u9023\u7684\u4e09\u89d2\u5f62,\u6cbfX\u8ef8\u6b63\u65b9\u5411\u79fb\u52d5\u6309Z\u5b57\u5f62\u8def\u7dda\u6a21\u5f0f\u4f86\u7e6a\u88fd,\u7576\u9054\u5230\u884c\u7684\u4ee4\u4e00\u7aef\u6642\u5c31\u79fb\u5230\u4e0b\u4e00\u884c\u7528\u540c\u6a23\u7684\u65b9\u6cd5\u9032\u884c\u7e6a\u88fd,\u5982\u6b64\u76f4\u81f3\u5b8c\u6210\u7e6a\u756b.\u5c0d\u5730\u5f62\u9032\u884c\u7d0b\u7406\u6620\u5c04,\u56e0\u70ba\u7d0b\u7406\u662f\u6b63\u65b9\u5f62,\u6240\u4ee5\u6bcf\u56db\u500b\u9ede\u9802\u9ede\u6307\u5b9a\u4e00\u500b\u7d0b\u7406,\u7531\u5169\u500b\u4e09\u89d2\u5f62\u7d44\u6210\u56db\u908a\u5f62,\u6bcf\u500b\u56db\u908a\u5f62\u5c0d\u61c9\u4e00\u500b\u7d0b\u7406.<\/p>\n<p>\u9664\u5730\u5f62\u5716\u5916\u9084\u6709\u6d77\u5e73\u9762\u63a9\u84cb\u4f4e\u7aaa\u5730\u5e36,\u6d77\u5e73\u9762\u7531\u56db\u908a\u5f62\u548c\u7684\u9ad8\u5ea6\u503c\u7d44\u6210,\u518d\u5c07\u300e\u6c34\u7d0b\u7406\u300f\u61c9\u7528\u65bc\u56db\u908a\u5f62\u4f7f\u5f97\u6d77\u6c34\u5177\u6709\u771f\u5be6\u611f.<\/p>\n<p>\u7e6a\u756b\u7d0b\u7406\u5730\u5f62\u7684C\u4ee3\u78bc:<\/p>\n<p>void Draw_Terrain(TERRAIN_PTR terrain)<\/p>\n<p>{<\/p>\n<p>\/\/ \u7e6a\u756b\u7d0b\u7406\u5730\u5f62<\/p>\n<p>glBindTexture(GL_TEXTURE_2D, terrain-&gt;grass.ID);<\/p>\n<p>\/\/ \u7d0b\u7406\u984f\u8272\u8207\u50cf\u7d20\u984f\u8272\u76f8\u5269<\/p>\n<p>glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);<\/p>\n<p>for (int z = 0; z &lt; terrain-&gt;width &#8211; 1; ++z)<\/p>\n<p>{<\/p>\n<p>glBegin(GL_TRIANGLE_STRIP);\/\/ \u7e6a\u756b\u4e09\u89d2\u5f62<\/p>\n<p>for (int x = 0; x &lt; terrain-&gt;width; ++x)<\/p>\n<p>{\/\/ \u4e00\u6b21\u6e32\u67d3\u5169\u500b\u9802\u9ede<\/p>\n<p>float currScaledHeight = terrain-&gt;data[z * terrain-&gt;width + x] \/ terrain-&gt;scale;<\/p>\n<p>float nextScaledHeight = terrain-&gt;data[(z + 1)* terrain-&gt;width + x] \/ terrain-&gt;scale;<\/p>\n<p>float color = 0.5f + 0.5f * currScaledHeight \/ terrain-&gt;max_height; \/\/ \u7070\u5ea6\u503c<\/p>\n<p>float nextColor = 0.5f + 0.5f * nextScaledHeight \/ terrain-&gt;max_height; \/\/ \u7070\u5ea6\u503c<\/p>\n<p>glColor3f(color, color, color);<\/p>\n<p>glTexCoord2f((GLfloat)x \/ terrain-&gt;width * 8.0f, (GLfloat)z \/ terrain-&gt;width * 8.0f);<\/p>\n<p>glVertex3f((GLfloat)(x &#8211; terrain-&gt;width\/2.0f), currScaledHeight,\u00a0 (GLfloat)(z &#8211; terrain-&gt;width \/ 2.0f));<\/p>\n<p>glColor3f(nextColor, nextColor, nextColor);<\/p>\n<p>glTexCoord2f((GLfloat)x \/ terrain-&gt;width * 8.0f, (GLfloat)(z + 1.0f) \/ terrain-&gt;width * 8.0f);<\/p>\n<p>glVertex3f((GLfloat)(x &#8211; terrain-&gt;width\/2.0f), nextScaledHeight, (GLfloat)(z + 1.0f &#8211; terrain-&gt;width\/2.0f));<\/p>\n<p>}<\/p>\n<p>glEnd();<\/p>\n<p>}<\/p>\n<p>\/\/ \u7e6a\u756b\u6c34\u9762<\/p>\n<p>glBindTexture(GL_TEXTURE_2D, terrain-&gt;water.ID);\/\/ \u7d81\u5b9a\u7d0b\u7406<\/p>\n<p>glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);\/\/ \u91cd\u8907\u7684\u7d0b\u7406<\/p>\n<p>glBegin(GL_QUADS);<\/p>\n<p>glTexCoord2f(0.0f, 0.0f);\/\/ \u7d0b\u7406\u5ea7\u6a19<\/p>\n<p>glVertex3f(-terrain-&gt;width \/ 2.1f, terrain-&gt;water_height, terrain-&gt;width \/ 2.1f);\/\/ \u9802\u9ede\u5ea7\u6a19<\/p>\n<p>glTexCoord2f(terrain-&gt;width \/ 4.0f, 0.0f);<\/p>\n<p>glVertex3f(terrain-&gt;width \/ 2.1f, terrain-&gt;water_height, terrain-&gt;width \/ 2.1f);<\/p>\n<p>glTexCoord2f(terrain-&gt;width \/ 4.0f, terrain-&gt;width \/ 4.0f);<\/p>\n<p>glVertex3f(terrain-&gt;width \/ 2.1f, terrain-&gt;water_height, -terrain-&gt;width \/ 2.1f);<\/p>\n<p>glTexCoord2f(0.0f, terrain-&gt;width \/ 4.0f);<\/p>\n<p>glVertex3f(-terrain-&gt;width \/ 2.1f, terrain-&gt;water_height, -terrain-&gt;width \/ 2.1f);<\/p>\n<p>glEnd();<\/p>\n<p>}<\/p>\n<p>\u6f14\u793a\u7a0b\u5f0f:<a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/03\/OpenGLTextureTerrain.zip\">\u4e0b\u8f09<\/a><\/p>\n<ol>\n<li>\u9f20\u6a19\u300e\u5de6\/\u53f3\u300f\u9375\u79fb\u52d5\u76f8\u6a5f\u7e5eY\u8ef8\u65cb\u8f49<\/li>\n<li>\u9f20\u6a19\u300e\u4e0a\/\u4e0b\u300f\u9375\u79fb\u52d5\u76f8\u6a5f\u7e5eX\u8ef8\u65cb\u8f49<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5730\u5f62\u6587\u6a94\u5176\u5be6\u5c31\u662f\u7070\u5ea6\u5716,\u6bcf\u4e00\u4f4d\u7070\u5ea6(0~255)\u5c0d\u61c9\u5176\u9ad8\u5ea6\u503c,\u7531\u9ad8\u5ea6\u503c\u7d44\u6210\u7684\u4e8c\u7dad\u9ede,\u4e8c\u7dad\u9ede\u4ee5\u6cbfX\u8ef8\u548cZ\u8ef8\u5206\u4f48. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/opengl_texture_terrain\/\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">OpenGL\u4e4b\u7d0b\u7406\u5730\u5f62<\/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":[62],"tags":[50],"class_list":["post-3855","post","type-post","status-publish","format-standard","hentry","category-opengl","tag-opengl","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/3855","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=3855"}],"version-history":[{"count":1,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/3855\/revisions"}],"predecessor-version":[{"id":3858,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/3855\/revisions\/3858"}],"wp:attachment":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/media?parent=3855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/categories?post=3855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/tags?post=3855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}