{"id":4080,"date":"2018-05-14T20:11:03","date_gmt":"2018-05-14T12:11:03","guid":{"rendered":"http:\/\/www.bookcard.net\/wordpress\/?p=4080"},"modified":"2018-10-14T21:09:26","modified_gmt":"2018-10-14T13:09:26","slug":"opengl_particle","status":"publish","type":"post","link":"https:\/\/www.bookcard.net\/wordpress\/opengl_particle\/","title":{"rendered":"OpenGL\u4e4b\u7c92\u5b50\u7cfb\u7d71"},"content":{"rendered":"<figure id=\"attachment_4083\" aria-describedby=\"caption-attachment-4083\" style=\"width: 1024px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/05\/OpenGL_Particle.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4083\" src=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/05\/OpenGL_Particle.jpg\" alt=\"\" width=\"1024\" height=\"768\" srcset=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/05\/OpenGL_Particle.jpg 1024w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/05\/OpenGL_Particle-300x225.jpg 300w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/05\/OpenGL_Particle-768x576.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-4083\" class=\"wp-caption-text\">OpenGL\u4e4b\u7c92\u5b50\u7cfb\u7d71<\/figcaption><\/figure>\n<p>\u300e\u7c92\u5b50\u7cfb\u7d71\u300f\u7531\u591a\u500b\u300e\u7c92\u5b50\u300f,\u6bcf\u500b\u300e\u7c92\u5b50\u300f\u90fd\u6709\u5176\u7368\u7acb\u300e\u5c6c\u6027\u300f\u5982\u300e\u5c3a\u5bf8\u300f\u300e\u984f\u8272\u300f\u300e\u901f\u5ea6\u300f\uff0c\u6bcf\u500b\u300e\u7c92\u5b50\u300f\u90fd\u53ef\u7368\u7acb\u904b\u4f5c.\u7576\u5927\u91cf\u300e\u7c92\u5b50\u300f\u540c\u6642\u904b\u4f5c\u4fbf\u53ef\u7522\u751f\u5176\u7279\u76843D\u6548\u679c.\u300e\u98c4\u96ea\u300f\u300e\u6d41\u661f\u300f\u300e<a href=\"https:\/\/www.bookcard.net\/wordpress\/opengl_explosion\/\">\u7206\u70b8<\/a>\u300f\u300e\u98db\u6a5f\u5f15\u64ce\u706b\u7130\u300f.<\/p>\n<p>\u300e\u98c4\u96ea\u300f\u6f14\u793a\u7a0b\u5f0f\u5728\u5929\u7a7a\u843d\u96ea,\u6642\u7528\u300e<a href=\"https:\/\/www.bookcard.net\/wordpress\/opengl_billboard\/\">\u5ee3\u544a\u724c<\/a>\u300f\u6280\u8853\u53e6\u5ee3\u544a\u724c\u7e3d\u662f\u5c0d\u9019\u93e1\u982d.\u4e0b\u8f09\u300e<a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/05\/OpenGLParticle.zip\">\u98c4\u96ea<\/a>\u300f<\/p>\n<p>\u7c92\u5b50\u7cfb\u7d71\u5927\u7d04\u5206\u70ba<\/p>\n<p>1.\u521d\u6b64\u5316\u7c92\u5b50\u7cfb\u7d71<\/p>\n<p>2.\u751f\u6210\u7c92\u5b50<\/p>\n<p>3.\u66f4\u65b0\u7c92\u5b50\u7cfb\u7d71<\/p>\n<p>4.\u6e32\u67d3\u7c92\u5b50\u7cfb\u7d71<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"277\">\u57fa\u672c\u300e\u7c92\u5b50\u300f\u5c6c\u6027<\/td>\n<td width=\"277\">\u7c21\u4ecb<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u4f4d\u7f6e<\/td>\n<td width=\"277\">3D\u7a7a\u9593\u4e2d\u7684\u5ea7\u6a19<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u901f\u5ea6<\/td>\n<td width=\"277\">\u57fa\u672c\u4e0a\u7c92\u5b50\u90fd\u6703\u79fb\u52d5,\u4f60\u9700\u8981\u65b9\u5411\u5411\u91cf\u518d\u4e58\u4ee5\u6642\u9593\u800c\u6c42\u5f97\u901f\u5ea6<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u5c3a\u5bf8<\/td>\n<td width=\"277\">\u7c92\u5b50\u7684\u5c3a\u5bf8\u53ef\u80fd\u8b8a\u5316<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u91cd\u91cf<\/td>\n<td width=\"277\">\u5916\u529b\u5c0d\u7c92\u5b50\u5f71\u97ff<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u984f\u8272<\/td>\n<td width=\"277\">\u70ba\u7c92\u5b50\u8a2d\u5b9a\u6e32\u67d3\u984f\u8272<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u5f62\u614b<\/td>\n<td width=\"277\">\u5e38\u898b\u300e\u9ede\u300f\u300e\u76f4\u7dda\u300f\u300e\u7d0b\u7406\u56db\u908a\u5f62\u300f<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u5bbf\u4e3b<\/td>\n<td width=\"277\">\u7c92\u5b50\u64c1\u6709\u8005<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u751f\u547d\u9031\u671f<\/td>\n<td width=\"277\">\u7c92\u5b50\u5b58\u6d3b\u6642\u9593<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9a\u7fa9\u7c92\u5b50\u7d50\u69cb<\/p>\n<p>typedef struct PARTICLE_TYP {<\/p>\n<p>VECTOR3D pos;\/\/ \u4f4d\u7f6e<\/p>\n<p>VECTOR3D velocity;\/\/ \u901f\u5ea6\u8207\u65b9\u5411<\/p>\n<p>VECTOR3D acceleration;\/\/ \u52a0\u901f\u5ea6<\/p>\n<p>float energy;\/\/ \u751f\u547d\u9031\u671f(\u79d2)<\/p>\n<p>float size;\/\/ \u5c3a\u5bf8<\/p>\n<p>float weight;\/\/ \u91cd\u91cf<\/p>\n<p>float color[4];\/\/ \u984f\u8272<\/p>\n<p>}PARTICLE,*PARTICLE_PTR;<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"277\">\u300e\u7c92\u5b50\u7cfb\u7d71\u300f\u5c6c\u6027<\/td>\n<td width=\"277\">\u7c21\u4ecb<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u7c92\u5b50\u968a\u5217<\/td>\n<td width=\"277\">\u8981\u7ba1\u7406\u7684\u6240\u6709\u7c92\u5b50<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u6578\u91cf<\/td>\n<td width=\"277\">\u7c92\u5b50\u91cf<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u4f4d\u7f6e<\/td>\n<td width=\"277\">\u6d3b\u52d5\u7bc4\u570d\u4e2d\u5fc3\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u7bc4\u570d<\/td>\n<td width=\"277\">\u7c92\u5b50\u6d3b\u52d5\u7bc4\u570d<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u5c6c\u6027<\/td>\n<td width=\"277\">\u5e38\u898b\u300e\u9ede\u300f\u300e\u76f4\u7dda\u300f\u300e\u7d0b\u7406\u56db\u908a\u5f62\u300f<\/td>\n<\/tr>\n<tr>\n<td width=\"277\">\u7d0b\u7406<\/td>\n<td width=\"277\">\u628a\u7d0b\u7406\u5b58\u5132\u5728\u300e\u7c92\u5b50\u7cfb\u7d71\u300f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Particle(\u7c92\u5b50\u7cfb\u7d71)<\/p>\n<p>typedef struct PARTICLE_SYSTEM_TYP{<\/p>\n<p>int\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 attr;\/\/ \u5c6c\u6027<\/p>\n<p>PARTICLE_PTR array;\/\/ \u6578\u7d44<\/p>\n<p>int\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 count;\/\/ \u5be6\u5236\u500b\u6578<\/p>\n<p>VECTOR3D\u00a0\u00a0\u00a0\u00a0 origin;\/\/ \u539f\u9ede<\/p>\n<p>VECTOR3D\u00a0\u00a0\u00a0\u00a0 velocity;\/\/ \u901f\u5ea6\u8207\u65b9\u5411<\/p>\n<p>VECTOR3D\u00a0\u00a0\u00a0\u00a0 velocity_variation;\/\/ \u901f\u5ea6\u8b8a\u91cf<\/p>\n<p>float\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 energy;\/\/ \u751f\u547d\u9031\u671f(\u79d2)<\/p>\n<p>\/\/\u6709\u6548\u7bc4\u570d<\/p>\n<p>float\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 height;\/\/ \u9ad8<\/p>\n<p>float\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 width; \/\/ \u5bec<\/p>\n<p>float\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 depth; \/\/ \u6df1<\/p>\n<p>float\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 size;\/\/ \u5c3a\u5bf8<\/p>\n<p>TEXTURE_PTR\u00a0 texture;\/\/ \u7d0b\u7406<\/p>\n<p>}PARTICLE_SYSTEM, *PARTICLE_SYSTEM_PTR;<\/p>\n<p>1.\u521d\u6b64\u5316\u7c92\u5b50\u7cfb\u7d71<\/p>\n<p>bool Init_Particle(PARTICLE_SYSTEM_PTR system, int count,<\/p>\n<p>VECTOR3D_PTR origin, VECTOR3D_PTR velocity, VECTOR3D_PTR variation,<\/p>\n<p>float height, float width, float depth,float size,float energy,<\/p>\n<p>TEXTURE_PTR texture)\/\/ \u7d0b\u7406<\/p>\n<p>{<\/p>\n<p>if (system == NULL)<\/p>\n<p>return false;<\/p>\n<p>memset(system,0,sizeof(PARTICLE_SYSTEM));\/\/ \u6e05\u7a7a<\/p>\n<p>system-&gt;count = count; \/\/ \u7c92\u5b50\u6578\u91cf<\/p>\n<p>system-&gt;array = (PARTICLE_PTR)malloc(sizeof(PARTICLE)*count);\/\/ \u5206\u914d\u7a7a\u9593<\/p>\n<p>system-&gt;texture = texture;\/\/ \u7d0b\u7406<\/p>\n<p>system-&gt;size\u00a0\u00a0\u00a0 = size;\/\/ \u5c3a\u5bf8<\/p>\n<p>system-&gt;height\u00a0 = height;\/\/ \u9ad8<\/p>\n<p>system-&gt;width\u00a0\u00a0 = width;\/\/ \u5bec<\/p>\n<p>system-&gt;depth\u00a0\u00a0 = depth;\/\/ \u6df1<\/p>\n<p>Copy_VECTOR3D(&amp;system-&gt;origin, origin);\/\/ \u539f\u9ede<\/p>\n<p>Copy_VECTOR3D(&amp;system-&gt;velocity, velocity);\/\/\u00a0 \u901f\u5ea6\u8207\u65b9\u5411<\/p>\n<p>Copy_VECTOR3D(&amp;system-&gt;velocity_variation, variation);\/\/\u00a0 \u901f\u5ea6\u8b8a\u91cf<\/p>\n<p>PARTICLE_PTR particle;\/\/ \u7c92\u5b50<\/p>\n<p>for (int i = 0; i &lt; count; ++i)<\/p>\n<p>{<\/p>\n<p>particle = &amp;system-&gt;array[i];\/\/ \u7c92\u5b50<\/p>\n<p>Buid_Particle(system, particle);<\/p>\n<p>}<\/p>\n<p>return true;<\/p>\n<p>}<\/p>\n<p>2.\u751f\u6210\u7c92\u5b50<\/p>\n<p>void Buid_Particle(PARTICLE_SYSTEM_PTR\u00a0 system, PARTICLE_PTR\u00a0 particle)<\/p>\n<p>{<\/p>\n<p>particle-&gt;energy = system-&gt;energy;\/\/ \u751f\u547d\u9031\u671f(\u79d2)<\/p>\n<p>particle-&gt;size\u00a0\u00a0 = system-&gt;size;\/\/ \u5c3a\u5bf8<\/p>\n<p>\/\/ \u7c92\u5b50\u4f4d\u7f6e<\/p>\n<p>particle-&gt;pos.x = system-&gt;origin.x + FRAND_RANGE1() * (system-&gt;width \/2.0f);\/\/ x<\/p>\n<p>particle-&gt;pos.y = system-&gt;origin.y + FRAND_RANGE1() * (system-&gt;height\/2.0f);\/\/ y<\/p>\n<p>particle-&gt;pos.z = system-&gt;origin.z + FRAND_RANGE1() * (system-&gt;depth \/2.0f);\/\/ z<\/p>\n<p>\/\/ \u7c92\u5b50\u901f\u5ea6<\/p>\n<p>particle-&gt;velocity.x = system-&gt;velocity.x\u00a0 ;<\/p>\n<p>particle-&gt;velocity.y = system-&gt;velocity.y\u00a0 ;<\/p>\n<p>particle-&gt;velocity.z = system-&gt;velocity.z\u00a0 ;<\/p>\n<p>}<\/p>\n<p>3.\u66f4\u65b0\u7c92\u5b50\u7cfb\u7d71<\/p>\n<p>void Update_Particle(PARTICLE_SYSTEM_PTR\u00a0 system,float time)<\/p>\n<p>{<\/p>\n<p>PARTICLE_PTR\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 particle;\/\/ \u7c92\u5b50<\/p>\n<p>for (int i = 0; i &lt; system-&gt;count; ++i)<\/p>\n<p>{<\/p>\n<p>particle = &amp;system-&gt;array[i];\/\/ \u7c92\u5b50<\/p>\n<p>VECTOR3D vector;<\/p>\n<p>Scale_VECTOR3D(&amp;vector, &amp;particle-&gt;velocity, time);<\/p>\n<p>Add_VECTOR3D(&amp;particle-&gt;pos, &amp;particle-&gt;pos, &amp;vector);\/\/ \u79fb\u52d5\u7c92\u5b50<\/p>\n<p>\/\/ \u5224\u65b7\u6709\u7121\u8d85\u51fa\u8fd4\u570d<\/p>\n<p>particle-&gt;energy = particle-&gt;energy &#8211; time;\/\/ \u8a08\u7b97\u751f\u547d\u9031\u671f<\/p>\n<p>if (particle-&gt;energy &lt; 0 &amp;&amp; particle-&gt;pos.y &lt; 0)\/\/ \u751f\u547d\u9031\u671f<\/p>\n<p>Buid_Particle(system, particle);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>4.\u7e6a\u756b\u8c9d\u7c92\u5b50\u7cfb\u7d71(\u7d0b\u7406)<\/p>\n<p>bool Draw_Texture_Particle(PARTICLE_SYSTEM_PTR system,<\/p>\n<p>CAMERA3D_PTR camera, BILLBOARD_PTR billboard)<\/p>\n<p>{<\/p>\n<p>glPushMatrix();\/\/ \u58d3\u5165\u77e9\u9663<\/p>\n<p>glPushAttrib(GL_ALL_ATTRIB_BITS);<\/p>\n<p>glDisable(GL_LIGHTING);\/\/ \u7981\u7528\u5149\u7167<\/p>\n<p>glDisable(GL_LIGHT0);\/\/ \u7981\u75280\u865f\u71c8\u5149<\/p>\n<p>glEnable(GL_DEPTH_TEST);\/\/ \u555f\u7528\u6df1\u5ea6\u6e2c\u8a66<\/p>\n<p>glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);<\/p>\n<p>glEnable(GL_ALPHA_TEST);\/\/ \u555f\u7528\u900f\u9762\u6e2c\u8a66<\/p>\n<p>glAlphaFunc(GL_GREATER, 0);\/\/ \u900f\u9762\u8272<\/p>\n<p>PARTICLE_PTR\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 particle;\/\/ \u7c92\u5b50<\/p>\n<p>for (int i = 0; i &lt; system-&gt;count; ++i)<\/p>\n<p>{<\/p>\n<p>particle = &amp;system-&gt;array[i];\/\/ \u7c92\u5b50<\/p>\n<p>\/\/\u6aa2\u67e53D\u7403\u9ad4\u662f\u5426\u70ba\u65bc\u8996\u9310\u9ad4\u5167<\/p>\n<p>if (<a href=\"https:\/\/www.bookcard.net\/wordpress\/opengl_frustum_culling\/\">Compute_Sphere_In_Frustum<\/a>(&amp;camera-&gt;frustum, &amp;particle-&gt;pos, particle-&gt;size) == true)<\/p>\n<p>{\u00a0\u00a0 \/\/ \u7e6a\u756bBillboard\u5ee3\u544a\u724c<\/p>\n<p><a href=\"https:\/\/www.bookcard.net\/wordpress\/opengl_billboard\/\">Draw_Billboard<\/a>(billboard, &amp;particle-&gt;pos, particle-&gt;size);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>glPopAttrib();\/\/ \u5f48\u51fa\u5c6c\u6027<\/p>\n<p>glPopMatrix();\/\/ \u5f48\u51fa\u77e9\u9663<\/p>\n<p>return true;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300e\u7c92\u5b50\u7cfb\u7d71\u300f\u7531\u591a\u500b\u300e\u7c92\u5b50\u300f,\u6bcf\u500b\u300e\u7c92\u5b50\u300f\u90fd\u6709\u5176\u7368\u7acb\u300e\u5c6c\u6027\u300f\u5982\u300e\u5c3a\u5bf8\u300f\u300e\u984f\u8272\u300f\u300e\u901f\u5ea6\u300f\uff0c\u6bcf\u500b\u300e\u7c92\u5b50\u300f\u90fd\u53ef\u7368\u7acb\u904b\u4f5c &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/opengl_particle\/\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">OpenGL\u4e4b\u7c92\u5b50\u7cfb\u7d71<\/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-4080","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\/4080","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=4080"}],"version-history":[{"count":4,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4080\/revisions"}],"predecessor-version":[{"id":4198,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4080\/revisions\/4198"}],"wp:attachment":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/media?parent=4080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/categories?post=4080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/tags?post=4080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}