{"id":416,"date":"2021-11-23T23:06:36","date_gmt":"2021-11-23T15:06:36","guid":{"rendered":"http:\/\/liyanliang.net\/?p=416"},"modified":"2022-01-15T17:48:22","modified_gmt":"2022-01-15T09:48:22","slug":"opengl-geometryshader","status":"publish","type":"post","link":"http:\/\/liyanliang.net\/index.php\/2021\/11\/23\/opengl-geometryshader\/","title":{"rendered":"OpenGL \u51e0\u4f55\u7740\u8272\u5668\u7684\u5e94\u7528"},"content":{"rendered":"\n<p class=\"has-cyan-bluish-gray-background-color has-background\"><strong>\u5173\u4e8e\u51e0\u4f55\u7740\u8272\u5668<\/strong><\/p>\n\n\n\n<p>\u4ece OpenGL 3.2 \u5f00\u59cb\uff0c\u5728\u9876\u70b9\u7740\u8272\u5668\u548c\u7247\u6bb5\u7740\u8272\u5668\u4e4b\u95f4\u6709\u7b2c\u4e09\u79cd\u53ef\u9009\u7c7b\u578b\u7684\u7740\u8272\u5668\uff0c\u79f0\u4e3a\u51e0\u4f55\u7740\u8272\u5668\u3002\u8be5\u7740\u8272\u5668\u5177\u6709\u4f7f\u7528\u9876\u70b9\u7740\u8272\u5668\u7684\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u52a8\u6001\u521b\u5efa\u65b0\u51e0\u4f55\u4f53\u7684\u72ec\u7279\u80fd\u529b\u3002<\/p>\n\n\n\n<p>\u51e0\u4f55\u7740\u8272\u5668\u5728\u6e32\u67d3\u7ba1\u9053\u4e2d\u7684\u4f4d\u7f6e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img decoding=\"async\" src=\"https:\/\/liyanliangpublic.oss-cn-hongkong.aliyuncs.com\/img\/20211122195014.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background\"><strong>\u5e94\u75281\uff1a\u51e0\u4f55\u7740\u8272\u5668\u751f\u6210B\u00e9zier\u66f2\u7ebf<\/strong><\/p>\n\n\n\n<p>B\u00e9zier\u66f2\u7ebf\u65b9\u7a0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img decoding=\"async\" src=\"https:\/\/liyanliangpublic.oss-cn-hongkong.aliyuncs.com\/img\/20211122200725.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u9700\u89814\u4e2a\u63a7\u5236\u70b9\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img decoding=\"async\" src=\"https:\/\/liyanliangpublic.oss-cn-hongkong.aliyuncs.com\/img\/20211122200805.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>beziercurve.geom<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#version 330 core\nlayout( lines_adjacency ) in;\nlayout( line_strip, max_vertices = 200 ) out;\n\u200b\nvoid main() {       \n    int uNum = 3;\n    float dt = 1. \/ float(uNum);\n    float t = 0.;\n    \n    for( int i = 0; i &lt;= uNum; i++ )\n    {\n        float omt = 1. - t;\n        float omt2 = omt * omt;\n        float omt3 = omt * omt2;\n        float t2 = t * t;\n        float t3 = t * t2;\n        gl_Position = omt3 * gl_in[0].gl_Position.xyzw \n                    + 3. * t * omt2 * gl_in[1].gl_Position.xyzw \n                    + 3. * t2 * omt * gl_in[2].gl_Position.xyzw \n                    + t3 * gl_in[3].gl_Position.xyzw;\n                    \n        fColor = vec3(1.0, 1.0, 1.0);           \n        EmitVertex();\n        \n        t += dt;\n    }\n    \n    EndPrimitive();\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background\"><strong>\u5e94\u7528\u4e8c\uff1a\u51e0\u4f55\u7740\u8272\u5668\u751f\u6210\u7206\u7834\u7269\u4f53<\/strong><\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u8bf4\u7206\u7834\u4e00\u4e2a\u7269\u4f53\u65f6\uff0c\u6211\u4eec\u5e76\u4e0d\u662f\u6307\u8981\u5c06\u5b9d\u8d35\u7684\u9876\u70b9\u96c6\u7ed9\u70b8\u6389\uff0c\u6211\u4eec\u662f\u8981\u5c06\u6bcf\u4e2a\u4e09\u89d2\u5f62\u6cbf\u7740\u6cd5\u5411\u91cf\u7684\u65b9\u5411\u79fb\u52a8\u4e00\u5c0f\u6bb5\u65f6\u95f4\u3002\u6548\u679c\u5c31\u662f\uff0c\u6574\u4e2a\u7269\u4f53\u770b\u8d77\u6765\u50cf\u662f\u6cbf\u7740\u6bcf\u4e2a\u4e09\u89d2\u5f62\u7684\u6cd5\u7ebf\u5411\u91cf\u7206\u70b8\u4e00\u6837\u3002<\/p>\n\n\n\n<p>geom.gs<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#version 330 core\nlayout (triangles) in;\nlayout (triangle_strip, max_vertices = 3) out;\n\u200b\nin VS_OUT {\n    vec2 texCoords;\n} gs_in[];\n\u200b\nout vec2 TexCoords; \n\u200b\nuniform float time;\n\u200b\nvec4 explode(vec4 position, vec3 normal)\n{\n    float magnitude = 2.0;\n    vec3 direction = normal * ((sin(time) + 1.0) \/ 2.0) * magnitude; \n    return position + vec4(direction, 0.0);\n}\n\u200b\nvec3 GetNormal()\n{\n    vec3 a = vec3(gl_in[0].gl_Position) - vec3(gl_in[1].gl_Position);\n    vec3 b = vec3(gl_in[2].gl_Position) - vec3(gl_in[1].gl_Position);\n    return normalize(cross(a, b));\n}\n\u200b\nvoid main() {    \n    vec3 normal = GetNormal();\n\u200b\n    gl_Position = explode(gl_in[0].gl_Position, normal);\n    TexCoords = gs_in[0].texCoords;\n    EmitVertex();\n    gl_Position = explode(gl_in[1].gl_Position, normal);\n    TexCoords = gs_in[1].texCoords;\n    EmitVertex();\n    gl_Position = explode(gl_in[2].gl_Position, normal);\n    TexCoords = gs_in[2].texCoords;\n    EmitVertex();\n    EndPrimitive();\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img decoding=\"async\" src=\"https:\/\/liyanliangpublic.oss-cn-hongkong.aliyuncs.com\/img\/OpenGL%20Model.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background\"><strong>\u5e94\u7528\u4e09\uff1a\u6cd5\u5411\u91cf\u53ef\u89c6\u5316\u6216\u751f\u6210\u6bdb\u53d1<\/strong><\/p>\n\n\n\n<p>Geom.gs<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#version 330 core\nlayout (triangles) in;\nlayout (line_strip, max_vertices = 6) out;\n\u200b\nin VS_OUT {\n    vec3 normal;\n} gs_in[];\n\u200b\nconst float MAGNITUDE = 0.4;\n\u200b\nvoid GenerateLine(int index)\n{\n    gl_Position = gl_in[index].gl_Position;\n    EmitVertex();\n    gl_Position = gl_in[index].gl_Position + vec4(gs_in[index].normal, 0.0) * MAGNITUDE;\n    EmitVertex();\n    EndPrimitive();\n}\n\u200b\nvoid main()\n{\n    GenerateLine(0); \/\/ \u7b2c\u4e00\u4e2a\u9876\u70b9\u6cd5\u7ebf\n    GenerateLine(1); \/\/ \u7b2c\u4e8c\u4e2a\u9876\u70b9\u6cd5\u7ebf\n    GenerateLine(2); \/\/ \u7b2c\u4e09\u4e2a\u9876\u70b9\u6cd5\u7ebf\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img decoding=\"async\" src=\"https:\/\/liyanliangpublic.oss-cn-hongkong.aliyuncs.com\/img\/lightingman.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Reference:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/learnopengl-cn.github.io\/04%20Advanced%20OpenGL\/09%20Geometry%20Shader\/\">https:\/\/learnopengl-cn.github.io\/04%20Advanced%20OpenGL\/09%20Geometry%20Shader\/<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/web.engr.oregonstate.edu\/~mjb\/cs519\/Handouts\/geometry_shaders.1pp.pdf\">https:\/\/web.engr.oregonstate.edu\/~mjb\/cs519\/Handouts\/geometry_shaders.1pp.pdf<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e\u51e0\u4f55\u7740\u8272\u5668 \u4ece OpenGL 3.2 \u5f00\u59cb\uff0c\u5728\u9876\u70b9\u7740\u8272\u5668\u548c\u7247\u6bb5\u7740\u8272\u5668\u4e4b\u95f4\u6709\u7b2c\u4e09\u79cd\u53ef\u9009\u7c7b\u578b\u7684\u7740\u8272\u5668\uff0c\u79f0\u4e3a\u51e0\u4f55\u7740\u8272\u5668\u3002\u8be5\u7740\u8272\u5668\u5177\u6709\u4f7f\u7528\u9876\u70b9\u7740\u8272\u5668\u7684\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u52a8\u6001\u521b\u5efa\u65b0\u51e0\u4f55\u4f53\u7684\u72ec\u7279\u80fd\u529b\u3002 \u51e0\u4f55\u7740\u8272\u5668\u5728\u6e32\u67d3\u7ba1\u9053\u4e2d\u7684\u4f4d\u7f6e\uff1a \u5e94\u75281\uff1a\u51e0\u4f55\u7740\u8272\u5668\u751f\u6210B\u00e9zier\u66f2\u7ebf B\u00e9zier\u66f2\u7ebf\u65b9\u7a0b\uff1a \u9700\u89814\u4e2a\u63a7\u5236\u70b9\uff1a beziercurve.geom \u5e94\u7528\u4e8c\uff1a\u51e0\u4f55\u7740\u8272\u5668\u751f\u6210\u7206\u7834\u7269\u4f53 \u5f53\u6211\u4eec\u8bf4\u7206\u7834\u4e00\u4e2a\u7269\u4f53\u65f6\uff0c\u6211\u4eec\u5e76\u4e0d\u662f\u6307\u8981\u5c06\u5b9d\u8d35\u7684\u9876\u70b9\u96c6\u7ed9\u70b8\u6389\uff0c\u6211\u4eec\u662f\u8981\u5c06\u6bcf\u4e2a\u4e09\u89d2\u5f62\u6cbf\u7740\u6cd5\u5411\u91cf\u7684\u65b9\u5411\u79fb\u52a8\u4e00\u5c0f\u6bb5\u65f6\u95f4\u3002\u6548\u679c\u5c31\u662f\uff0c\u6574\u4e2a\u7269\u4f53\u770b\u8d77\u6765\u50cf\u662f\u6cbf\u7740\u6bcf\u4e2a\u4e09\u89d2\u5f62\u7684\u6cd5\u7ebf\u5411\u91cf\u7206\u70b8\u4e00\u6837\u3002 geom.gs \u5e94\u7528\u4e09\uff1a\u6cd5\u5411\u91cf\u53ef\u89c6\u5316\u6216\u751f\u6210\u6bdb\u53d1 Geom.gs Reference: https:\/\/learnopengl-cn.github.io\/04%20Advanced%20OpenGL\/09%20Geometry%20Shader\/ https:\/\/web.engr.oregonstate.edu\/~mjb\/cs519\/Handouts\/geometry_shaders.1pp.pdf<\/p>\n","protected":false},"author":1,"featured_media":419,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,14],"tags":[15],"class_list":["post-416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","category-opengl","tag-opengl"],"_links":{"self":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/comments?post=416"}],"version-history":[{"count":4,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts\/416\/revisions"}],"predecessor-version":[{"id":423,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts\/416\/revisions\/423"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/media\/419"}],"wp:attachment":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/media?parent=416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/categories?post=416"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/tags?post=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}