{"id":3956,"date":"2018-04-18T21:07:28","date_gmt":"2018-04-18T13:07:28","guid":{"rendered":"http:\/\/www.bookcard.net\/wordpress\/?p=3956"},"modified":"2018-10-14T21:21:29","modified_gmt":"2018-10-14T13:21:29","slug":"opengl_frustum_culling","status":"publish","type":"post","link":"https:\/\/www.bookcard.net\/wordpress\/opengl_frustum_culling\/","title":{"rendered":"OpenGL\u4e4b\u8996\u9310\u9ad4\u88c1\u526a"},"content":{"rendered":"<figure id=\"attachment_3957\" aria-describedby=\"caption-attachment-3957\" style=\"width: 1024px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Frustum_Culling.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3957\" src=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Frustum_Culling.jpg\" alt=\"\" width=\"1024\" height=\"768\" srcset=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Frustum_Culling.jpg 1024w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Frustum_Culling-300x225.jpg 300w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGL_Frustum_Culling-768x576.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-3957\" class=\"wp-caption-text\">OpenGL\u4e4b\u8996\u9310\u9ad4\u88c1\u526a<\/figcaption><\/figure>\n<p>\u5716\u5f62\u5f15\u64ce\u7e6a\u88fd\u4efb\u4f55\u7747\u4e0d\u898b(\u8996\u9310\u622a\u9ad4\u4e4b\u5916)\u7684\u4efb\u4f55\u7269\u9ad4,\u90fd\u6703\u6d6a\u8cbb\u4fdd\u8cb4GPU\u8cc7\u6e90.\u76f8\u6a5f\u7684\u8996\u9310\u9ad4\u5b9a\u7fa9\u4f60\u6240\u80fd\u5920\u68af\u5230\u76843D\u4e16\u754c\u7a7a\u9593.\u8996\u9310\u9ad4\u7531\u76f8\u6a5f\u7684\u4f4d\u7f6e\u548c\u8996\u91ce\u7d44\u6210.\u8996\u9310\u9ad4\u7531\u516d\u500b\u5e73\u9762\u7d44\u6210.\u53ea\u8981\u9032\u5165\u8996\u9310\u9ad4\u5167\u76843D\u6a21\u578b\u90fd\u61c9\u88ab\u6e32\u67d3.(\u88ab\u5176\u5b833D\u6a21\u578b\u906e\u6a94\u9664\u5916).\u4f4d\u65bc\u8996\u9310\u9ad4\u4e4b\u5916\u5247\u4e0d\u5fc5\u6e32\u67d3.<\/p>\n<p>OpenGL\u6703\u81ea\u52d5\u4e1f\u68c4\u5728\u8996\u9310\u9ad4\u4e4b\u5916\u7684\u4e09\u89d2\u5f62.\u4f46OpenGL\u662f\u9010\u500b\u4e09\u89d2\u5f62\u9032\u884c\u8996\u9310\u9ad4\u88c1\u526a.\u7576\u4f60\u6709\u5927\u91cf(\u5e7e\u842c\u500b)\u7684\u4e09\u89d2\u5f62\u9032\u5165OpenGL\u88c1\u526a\u968a\u5217\u6642.\u6703\u5927\u5927\u5730\u964d\u4f4e\u7a0b\u5f0f\u6027\u80fd.<\/p>\n<p>\u6700\u7c21\u55ae\u7684\u512a\u5316\u65b9\u5f0f\u662f\u5b9a\u7fa93D\u7403\u5f62\u5bb9\u5668,\u7528\u65bc\u5305\u88f9\u4f60\u76843D\u6a21\u578b.\u7136\u5f8c\u4f7f\u7528\u8996\u9310\u9ad4\u82073D\u7403\u5f62\u9032\u884c\u6e2c\u8a66.\u5982\u679c3D\u7403\u5f62\u5b8c\u5168\u5728\u8996\u9310\u9ad4\u4e4b\u5916.\u5373\u53ef\u653e\u68c4\u6e32\u67d33D\u6a21\u578b.\u5982\u679c3D\u6a21\u578b\u90e8\u5206\u6216\u5168\u90e8\u9032\u5165\u8996\u9310\u9ad4\u5167,\u5247\u4ea4\u7531OpenGL\u7e7c\u5e8f\u9032\u884c\u4e09\u89d2\u5f62\u88c1\u526a<\/p>\n<p>\u6f14\u793a\u7a0b\u5f0f\u4e2d\u6709\u5e7e\u767e\u500b\u96a8\u6a5f\u79fb\u52d5\u7403\u9ad4,\u6309\u7a7a\u683c\u9375\u555f\u7528\u6216\u7981\u7528\u300e\u8996\u9310\u9ad4\u88c1\u526a\u300f.\u672a\u555f\u7528\u4e4b\u524d\u4e0d\u5230200\u5e40.\u4e00\u4f46\u555f\u52d5\u300e\u8996\u9310\u9ad4\u88c1\u526a\u300f\u5247\u8d85\u904e1000\u5e40.\u6027\u80fd\u5927\u5e45\u63d0\u9ad8\u4e94\u500d(\u8996\u6e32\u67d3\u7403\u9ad4\u7684\u500b\u6578\u800c\u5b9a).<a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/04\/OpenGLFrustumCulling.zip\">\u7a0b\u5f0f\u4e0b\u8f09<\/a>:<\/p>\n<p>&nbsp;<\/p>\n<p>\u5b9a\u7fa93D\u7403\u9ad4<\/p>\n<p>typedef struct SPHERE3D{<\/p>\n<p>float\u00a0\u00a0\u00a0 x, y, z;\/\/ \u4e2d\u5fc3\u9ede<\/p>\n<p>float\u00a0\u00a0\u00a0 radius;\/\/ \u7403\u9ad4\u534a\u5f91<\/p>\n<p>}SPHERE3D,* SPHERE3D_PTR;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5b9a\u7fa93D\u5e73\u9762<\/p>\n<p>typedef struct PLANE3D_TYP{<\/p>\n<p>float A, B, C;\/\/ \u5e73\u9762\u6cd5\u7dda\u5411\u91cf<\/p>\n<p>float D;\/\/\u5e73\u9762\u5230\u539f\u9ede\u6700\u8fd1\u8ddd\u96e2<\/p>\n<p>}PLANE3D,*PLANE3D_PTR;<\/p>\n<p>&nbsp;<\/p>\n<p>\u5b9a\u7fa9\u8996\u9310\u9ad4<\/p>\n<p>typedef struct FRUSTUM_TYP {<\/p>\n<p>PLANE3D planes[6];\/\/6\u500b\u5e73\u9762<\/p>\n<p>}FRUSTUM, *FRUSTUM_PTR;<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u5e73\u9762\u65b9\u7a0b\u5f0f\u5b9a\u7fa9:<\/p>\n<p>Ax + By + Cz + D = 0<\/p>\n<p>A,B,C:\u5e73\u9762\u7684\u6cd5\u7dda\u5411\u91cf<\/p>\n<p>D:\u5f9e\u5e73\u9762\u5230\u539f\u9ede\u7684\u8ddd\u96e2<\/p>\n<p>x,y,z:\u5e73\u9762\u4e0a\u7684\u4efb\u610f\u9ede<\/p>\n<p>\u7d50\u679c\u70ba\u96f6:\u8a72\u9ede\u843d\u5728\u5e73\u9762\u4e0a<\/p>\n<p>\u7d50\u679c\u70ba\u6b63:\u8a72\u9ede\u70ba\u4e8e\u5e73\u9762\u7684\u524d\u65b9<\/p>\n<p>\u7d50\u679c\u70ba\u8ca0:\u8a72\u9ede\u70ba\u65bc\u5e73\u9762\u7684\u5f8c\u65b9<\/p>\n<p>&nbsp;<\/p>\n<p>2.\u78ba\u5b9a\u8996\u9310\u9ad4\u5c3a\u5bf8,\u7372\u53d6\u6295\u5f71\u77e9\u9663\u548c\u6a21\u578b\u8996\u53e3\u77e9\u9663<\/p>\n<p>glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat*)&amp;projection);<\/p>\n<p>glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&amp;modelview);<\/p>\n<p>&nbsp;<\/p>\n<p>3.\u6295\u5f71\u77e9\u9663\u4e58\u4ee5\u6a21\u578b\u8996\u53e3\u77e9\u9663<\/p>\n<p>glPushMatrix();<\/p>\n<p>glLoadMatrixf((GLfloat*)&amp;projection);<\/p>\n<p>glMultMatrixf((GLfloat*)&amp;modelview);<\/p>\n<p>glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat*)&amp;modelview);<\/p>\n<p>glPopMatrix();<\/p>\n<p>&nbsp;<\/p>\n<p>4.\u63d0\u53d6\u8996\u9310\u9ad4\u7684\u516d\u500b\u5e73\u9762<\/p>\n<p>int scale = (row &lt; 0) ? -1 : 1;<\/p>\n<p>row = abs(row) &#8211; 1;<\/p>\n<p>plane-&gt;A = mat-&gt;M[0][3] + scale * mat-&gt;M[0][row];<\/p>\n<p>plane-&gt;B = mat-&gt;M[1][3] + scale * mat-&gt;M[1][row];<\/p>\n<p>plane-&gt;C = mat-&gt;M[2][3] + scale * mat-&gt;M[2][row];<\/p>\n<p>plane-&gt;D = mat-&gt;M[3][3] + scale * mat-&gt;M[3][row];<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"277\">\u5e73\u9762(plane)<\/td>\n<td width=\"277\">\u884c(row)<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u5de6\u5247<\/td>\n<td width=\"277\">1<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u53f3\u5247<\/td>\n<td width=\"277\">-1<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u5e95\u90e8<\/td>\n<td width=\"277\">2<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u9802\u90e8<\/td>\n<td width=\"277\">-2<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u8fd1\u7aef<\/td>\n<td width=\"277\">3<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u9060\u7aef<\/td>\n<td width=\"277\">-3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>5.\u5e73\u9762\u6b78\u4e00\u5316<\/p>\n<p>float length = sqrtf(plane-&gt;A * plane-&gt;A + plane-&gt;B * plane-&gt;B + plane-&gt;C * plane-&gt;C);<\/p>\n<p>plane-&gt;A \/= length;<\/p>\n<p>plane-&gt;B \/= length;<\/p>\n<p>plane-&gt;C \/= length;<\/p>\n<p>plane-&gt;D \/= length;<\/p>\n<p>&nbsp;<\/p>\n<p>6.\u6aa2\u67e53D\u7403\u9ad4\u662f\u5426\u70ba\u65bc\u8996\u9310\u9ad4\u5167<\/p>\n<p>frustum:\u8996\u9310\u9ad4<\/p>\n<p>sphere:\u7403\u9ad4<\/p>\n<p>\u8fd4\u56de\u503c:\u82e5\u5728\u8996\u9310\u9ad4\u5167\u8fd4\u56deTRUE,\u5426\u5247\u53cd\u56deFALSE<\/p>\n<p>bool Compute_Sphere_In_Frustum(FRUSTUM_PTR frustum, SPHERE3D_PTR sphere)<\/p>\n<p>{<\/p>\n<p>float dist;<\/p>\n<p>for (int i=0;i&lt;6;++i)<\/p>\n<p>{\/\/ \u5224\u65b7\u7403\u9ad4\u662f\u5426\u70ba\u8207\u516d\u500b\u5e73\u9762\u4e4b\u524d<\/p>\n<p>PLANE3D_PTR plane = &amp;frustum-&gt;planes[i];<\/p>\n<p>\/\/ \u8a08\u7b97\u9ede\u8207\u5e73\u9762\u7684\u8ddd\u96e2,\u82e5\u70ba\u6b63\u5728\u8996\u9310\u9ad4\u5167,\u82e5\u70ba\u8ca0\u5728\u8996\u9310\u9ad4\u5916<\/p>\n<p>dist = plane-&gt;A * sphere-&gt;x + plane-&gt;B * sphere-&gt;y + plane-&gt;C * sphere-&gt;z + plane-&gt;D;<\/p>\n<p>if (dist &lt;= -sphere-&gt;radius)<\/p>\n<p>return false;\/\/ \u5728\u8996\u9310\u9ad4\u5916<\/p>\n<p>}<\/p>\n<p>return true; \/\/ \u5728\u8996\u9310\u9ad4\u5167<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5716\u5f62\u5f15\u64ce\u7e6a\u88fd\u4efb\u4f55\u7747\u4e0d\u898b(\u8996\u9310\u622a\u9ad4\u4e4b\u5916)\u7684\u4efb\u4f55\u7269\u9ad4,\u90fd\u6703\u6d6a\u8cbb\u4fdd\u8cb4GPU\u8cc7\u6e90.\u76f8\u6a5f\u7684\u8996\u9310\u9ad4\u5b9a\u7fa9\u4f60\u6240\u80fd\u5920\u68af\u5230\u76843D\u4e16 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/opengl_frustum_culling\/\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">OpenGL\u4e4b\u8996\u9310\u9ad4\u88c1\u526a<\/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-3956","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\/3956","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=3956"}],"version-history":[{"count":3,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/3956\/revisions"}],"predecessor-version":[{"id":3961,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/3956\/revisions\/3961"}],"wp:attachment":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/media?parent=3956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/categories?post=3956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/tags?post=3956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}