{"id":4031,"date":"2018-04-29T20:01:42","date_gmt":"2018-04-29T12:01:42","guid":{"rendered":"http:\/\/www.bookcard.net\/wordpress\/?p=4031"},"modified":"2018-10-14T21:19:26","modified_gmt":"2018-10-14T13:19:26","slug":"opengl_stencil_buffer","status":"publish","type":"post","link":"https:\/\/www.bookcard.net\/wordpress\/opengl_stencil_buffer\/","title":{"rendered":"OpenGL\u4e4b\u6a21\u677f\u7de9\u5b58"},"content":{"rendered":"<div class=\"mceTemp\"><\/div>\n<figure id=\"attachment_4033\" aria-describedby=\"caption-attachment-4033\" style=\"width: 1024px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Stencil_Buffer.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4033\" src=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Stencil_Buffer.jpg\" alt=\"\" width=\"1024\" height=\"768\" srcset=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Stencil_Buffer.jpg 1024w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Stencil_Buffer-300x225.jpg 300w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Stencil_Buffer-768x576.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-4033\" class=\"wp-caption-text\">OpenGL\u4e4b\u6a21\u677f\u7de9\u5b58<\/figcaption><\/figure>\n<p>\u4f7f\u7528\u300e\u6a21\u677f\u7de9\u5b58\u300f\u53ef\u4ee5\u5c07\u5c4f\u5e55\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u5f9e\u8996\u91ce\u4e2d\u5c4f\u853d.\u5176\u4e2d\u7684\u7279\u6548\u662f\u300e\u93e1\u50cf\u300f\u6548\u679c.\u93e1\u50cf(\u5012\u5f71)\u6f14\u793a\u7a0b\u5f0f:<a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGLStencilBuffer.zip\">\u4e0b\u8f09<\/a><\/p>\n<p>1.\u8a2d\u7f6e\u50cf\u7d20\u683c\u5f0f\u6642\u5c0dPIXELFORMATDESCRIPTOR\u5c0dcStencilBits\u8a2d\u7f6e\u70ba 16bit\u300e\u6a21\u677f\u7de9\u5b58\u300f.<\/p>\n<p>PIXELFORMATDESCRIPTOR pdf;<\/p>\n<p>pfd.cStencilBits = 16;<\/p>\n<p>2.\u8981\u555f\u7528\u300e\u6a21\u677f\u7de9\u5b58\u300f<\/p>\n<p>glEnable(GL_STENCIL_TEST);<\/p>\n<p>3.\u8a2d\u7f6e\u6a21\u677f\u64cd\u4f5c<\/p>\n<p>void glStencilOp(GLenum fail,GLenum zfail,GLenum zpass);<\/p>\n<p>fail:\u6a21\u677f\u6e2c\u8a66\u5931\u6557<\/p>\n<p>zfail:\u6a21\u677f\u6e2c\u8a66\u901a\u904e,\u4f46\u6df1\u5ea6\u6e2c\u8a66\u5931\u6557<\/p>\n<p>zpass:\u6a21\u677f\u6e2c\u8a66\u8207\u6df1\u5ea6\u6e2c\u8a66\u5747\u901a\u904e<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"277\">\u64cd\u4f5c<\/td>\n<td width=\"277\">\u7c21\u4ecb<\/td>\n<\/tr>\n<tr>\n<td width=\"277\"><strong>GL_KEEP<\/strong><\/td>\n<td width=\"277\">\u4fdd\u6301\u7576\u524d\u503c<\/td>\n<\/tr>\n<tr>\n<td width=\"277\"><strong>GL_ZERO<\/strong><\/td>\n<td width=\"277\">\u5c07\u6a21\u677f\u7de9\u5b58\u7684\u503c\u8a2d\u5b9a\u70ba0<\/td>\n<\/tr>\n<tr>\n<td width=\"277\"><strong>GL_REPLACE<\/strong><\/td>\n<td width=\"277\">\u5c07\u6a21\u677f\u7de9\u5b58\u7684\u503c\u8a2d\u5b9a\u70baglStencilFunc()\u7684\u53c3\u8003\u503cref.<\/td>\n<\/tr>\n<tr>\n<td width=\"277\"><strong>GL_INCR<\/strong><\/td>\n<td width=\"277\">\u589e\u52a0\u7576\u524d\u6a21\u677f\u7684\u7de9\u5b58\u503c<\/td>\n<\/tr>\n<tr>\n<td width=\"277\"><strong>GL_DECR<\/strong><\/td>\n<td width=\"277\">\u6e1b\u5c0f\u7576\u524d\u6a21\u677f\u7684\u7de9\u5b58\u503c<\/td>\n<\/tr>\n<tr>\n<td width=\"277\"><strong>GL_INVERT<\/strong><\/td>\n<td width=\"277\">\u5c07\u6a21\u677f\u7de9\u5b58\u7684\u503c\u53cd\u8f49<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>4.\u8a2d\u7f6e\u6a21\u677f\u6bd4\u8f03\u7a0b\u5e8f<\/p>\n<p>void glStencilFunc(GLenum func,GLint ref,GLuint mask);<\/p>\n<p>func:\u6bd4\u8f03\u7a0b\u5f0f<\/p>\n<p>ref:\u53c3\u8003\u503c<\/p>\n<p>mask:\u6a21\u677f\u63a9\u78bc<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"236\">\u6bd4\u8f03\u7a0b\u5f0f<\/td>\n<td width=\"317\">\u7c21\u4ecb<\/td>\n<\/tr>\n<tr>\n<td width=\"236\"><strong>GL_NEVER<\/strong><\/td>\n<td width=\"317\">\u7e3d\u662f\u5931\u6557<\/td>\n<\/tr>\n<tr>\n<td width=\"236\"><strong>GL_LESS<\/strong><\/td>\n<td width=\"317\">\u689d\u4ef6\u6bd4\u8f03:if (<em>ref<\/em> &amp; <em>mask<\/em>) &lt; (<em>stencil<\/em> &amp; <em>mask<\/em>).<\/td>\n<\/tr>\n<tr>\n<td width=\"236\"><strong>GL_LEQUAL<\/strong><\/td>\n<td width=\"317\">\u689d\u4ef6\u6bd4\u8f03:if (<em>ref<\/em> &amp; <em>mask<\/em>) \u2264 (<em>stencil<\/em> &amp; <em>mask<\/em>).<\/td>\n<\/tr>\n<tr>\n<td width=\"236\"><strong>GL_GREATER<\/strong><\/td>\n<td width=\"317\">\u689d\u4ef6\u6bd4\u8f03:if (<em>ref<\/em> &amp; <em>mask<\/em>) &gt; (<em>stencil<\/em> &amp; <em>mask<\/em>).<\/td>\n<\/tr>\n<tr>\n<td width=\"236\"><strong>GL_GEQUAL<\/strong><\/td>\n<td width=\"317\">\u689d\u4ef6\u6bd4\u8f03: if (<em>ref<\/em> &amp; <em>mask<\/em>) \u2265 (<em>stencil<\/em> &amp; <em>mask<\/em>).<\/td>\n<\/tr>\n<tr>\n<td width=\"236\"><strong>GL_EQUAL<\/strong><\/td>\n<td width=\"317\">\u689d\u4ef6\u6bd4\u8f03: if (<em>ref<\/em> &amp; <em>mask<\/em>) = (<em>stencil<\/em> &amp; <em>mask<\/em>).<\/td>\n<\/tr>\n<tr>\n<td width=\"236\"><strong>GL_NOTEQUAL<\/strong><\/td>\n<td width=\"317\">\u689d\u4ef6\u6bd4\u8f03: if (<em>ref<\/em> &amp; <em>mask<\/em>) \u2260 (<em>stencil<\/em> &amp; <em>mask<\/em>).<\/td>\n<\/tr>\n<tr>\n<td width=\"236\"><strong>GL_ALWAYS<\/strong><\/td>\n<td width=\"317\">\u7e3d\u662f\u5141\u8a31<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7e6a\u756b\u93e1\u50cf\u51fd\u5f0f\u4ee3\u78bc\u6f14\u793a:<\/p>\n<p>1.\u7981\u7528\u6df1\u5ea6\u6e2c\u8a66<\/p>\n<p>glDisable(GL_DEPTH_TEST);<\/p>\n<p>2.\u7981\u7528\u5c0d\u6240\u6709\u7684\u7136\u8272\u5206\u91cf\u7684\u4fee\u6539<\/p>\n<p>glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);<\/p>\n<p>3.\u555f\u7528\u6a21\u677f\u6e2c\u8a66<\/p>\n<p>glEnable(GL_STENCIL_TEST);<\/p>\n<p>4.\u5c07\u5730\u677f\u7e6a\u88fd\u5230\u6a21\u677f\u7de9\u5b58\u4e2d,\u5f15\u7528\u53c3\u8003\u503cref<\/p>\n<p>glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);<\/p>\n<p>glStencilFunc(GL_ALWAYS, 1, 1);<\/p>\n<p>5.\u7e6a\u756b\u5730\u677f\u5f8c\u96d6\u7136\u9084\u672a\u7747\u5230\u5730\u677f,\u4f46\u5df2\u5728\u6a21\u677f\u7de9\u5b58\u4e2d\u751f\u6210\u5b54,\u5728\u5b54\u4e2d\u7684\u591a\u908a\u5f62\u5c07\u4e0d\u88ab\u7e6a\u88fd<\/p>\n<p>Draw_Floor();<\/p>\n<p>6.\u555f\u7528\u6240\u6709\u984f\u8272\u5206\u91cf\u7684\u4fee\u6539<\/p>\n<p>glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);<\/p>\n<p>7.\u555f\u7528\u6df1\u5ea6\u6e2c\u8a66<\/p>\n<p>glEnable(GL_DEPTH_TEST);<\/p>\n<p>8.\u8a2d\u7f6e\u6a21\u677f\u6bd4\u8f03\u7a0b\u5e8f,\u53ea\u80fd\u5728\u6a21\u677f\u7de9\u5b58\u4e2d\u503c\u70ba1\u7684\u76f8\u61c9\u53bb\u5340\u57df\u7e6a\u88fd<\/p>\n<p>glStencilFunc(GL_EQUAL, 1, 1);<\/p>\n<p>glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);<\/p>\n<p>9.\u58d3\u5165\u6a21\u578b\u77e9\u9663.\u6e96\u5099\u7e6a\u88fd&#8221;\u93e1\u50cf&#8221;<\/p>\n<p>glPushMatrix();<\/p>\n<p>10.\u8fd4\u8f49\u5883\u50cf<\/p>\n<p>glScalef(1.0f, -1.0f, 1.0f);<\/p>\n<p>11.\u7e6a\u756b\u93e1\u50cf(\u5012\u5f71)<\/p>\n<p>Draw_Mirror();<\/p>\n<p>11.\u5f48\u51fa\u6a21\u578b\u77e9\u9663\u7d50\u675f\u93e1\u50cf\u7e6a\u88fd<\/p>\n<p>PopMatrix();<\/p>\n<p>12.\u7981\u7528\u6a21\u677f\u6e2c\u8a66<\/p>\n<p>glDisable(GL_STENCIL_TEST);<\/p>\n<p>13.\u4ee550%\u7e6a\u88fd\u900f\u660e\u5730\u677f, \u555f\u7528\u6df7\u5408<\/p>\n<p>glEnable(GL_BLEND);<\/p>\n<p>glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);<\/p>\n<p>glColor4f(1.0f,1.0f,1.0f,0.5f);<\/p>\n<p>14.\u7e6a\u756b\u5730\u677f<\/p>\n<p>Draw_Floor();<\/p>\n<p>15.\u7981\u7528\u6df7\u5408<\/p>\n<p>glDisable(GL_BLEND);<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u300e\u6a21\u677f\u7de9\u5b58\u300f\u53ef\u4ee5\u5c07\u5c4f\u5e55\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u5f9e\u8996\u91ce\u4e2d\u5c4f\u853d.\u5176\u4e2d\u7684\u7279\u6548\u662f\u300e\u93e1\u50cf\u300f\u6548\u679c.\u93e1\u50cf(\u5012\u5f71)\u6f14\u793a\u7a0b\u5f0f:\u4e0b\u8f09 1. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/opengl_stencil_buffer\/\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">OpenGL\u4e4b\u6a21\u677f\u7de9\u5b58<\/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-4031","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\/4031","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=4031"}],"version-history":[{"count":1,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4031\/revisions"}],"predecessor-version":[{"id":4035,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4031\/revisions\/4035"}],"wp:attachment":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/media?parent=4031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/categories?post=4031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/tags?post=4031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}