{"id":789,"date":"2022-07-18T20:40:34","date_gmt":"2022-07-18T12:40:34","guid":{"rendered":"https:\/\/www.acgloby.com\/?p=789"},"modified":"2022-07-18T20:40:34","modified_gmt":"2022-07-18T12:40:34","slug":"opengl-%e5%9d%90%e6%a0%87%e5%8f%98%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.acgloby.com\/index.php\/2022\/07\/18\/opengl-%e5%9d%90%e6%a0%87%e5%8f%98%e6%8d%a2\/","title":{"rendered":"OpenGL-\u5750\u6807\u53d8\u6362"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">OpenGL\u53d8\u6362\u662f\u7528\u4e00\u4e2a<a href=\"https:\/\/glm.g-truc.net\/0.9.8\/index.html\" target=\"_blank\"  rel=\"nofollow\" >GLM<\/a>\u5e93\uff0cGLM\u662f<strong>OpenGL&nbsp;Mathematics<\/strong>\u7684\u7f29\u5199\uff0c\u5b83\u662f\u4e00\u4e2a\u53ea\u6709\u5934\u6587\u4ef6\u7684\u5e93\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u9700\u8981\u7684GLM\u7684\u5927\u591a\u6570\u529f\u80fd\u90fd\u53ef\u4ee5\u4ece\u4e0b\u9762\u8fd93\u4e2a\u5934\u6587\u4ef6\u4e2d\u627e\u5230\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt; glm\/glm.hpp&gt;\n#include &lt; glm\/gtc\/matrix_transform.hpp&gt;\n#include &lt; glm\/gtc\/type_ptr.hpp&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u65cb\u8f6c\u4f4d\u79fb\u7f29\u653e<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>glm::mat4 trans = glm::mat4(1.0f);\n\/\/\u7f29\u653e\ntrans = glm::scale(trans, glm::vec3(1.001f, 1.001f, 1.001f));\n\/\/\u65cb\u8f6c\ntrans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0, 0, 1.0f));\n\/\/\u4f4d\u79fb\ntrans = glm::translate(trans, glm::vec3(1, 0, 0));<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5750\u6807\u7cfb\u7edf<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u89c1\u7684\u6240\u6709\u9876\u70b9\u90fd\u4e3a<strong>\u6807\u51c6\u5316\u8bbe\u5907\u5750\u6807(Normalized Device Coordinate, NDC)<\/strong>\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6bcf\u4e2a\u9876\u70b9\u7684<strong>x<\/strong>\uff0c<strong>y<\/strong>\uff0c<strong>z<\/strong>\u5750\u6807\u90fd\u5e94\u8be5\u5728<strong>-1.0<\/strong>\u5230<strong>1.0<\/strong>\u4e4b\u95f4\uff0c\u8d85\u51fa\u8fd9\u4e2a\u5750\u6807\u8303\u56f4\u7684\u9876\u70b9\u90fd\u5c06\u4e0d\u53ef\u89c1\u3002\u6211\u4eec\u901a\u5e38\u4f1a\u81ea\u5df1\u8bbe\u5b9a\u4e00\u4e2a\u5750\u6807\u7684\u8303\u56f4\uff0c\u4e4b\u540e\u518d\u5728\u9876\u70b9\u7740\u8272\u5668\u4e2d\u5c06\u8fd9\u4e9b\u5750\u6807\u53d8\u6362\u4e3a\u6807\u51c6\u5316\u8bbe\u5907\u5750\u6807\u3002\u7136\u540e\u5c06\u8fd9\u4e9b\u6807\u51c6\u5316\u8bbe\u5907\u5750\u6807\u4f20\u5165<strong>\u5149\u6805\u5668(Rasterizer)<\/strong>\uff0c\u5c06\u5b83\u4eec\u53d8\u6362\u4e3a\u5c4f\u5e55\u4e0a\u7684\u4e8c\u7ef4\u5750\u6807\u6216\u50cf\u7d20\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6d41\u6c34\u7ebf\u4e2d\uff0c\u7269\u4f53\u7684\u9876\u70b9\u5728\u6700\u7ec8\u8f6c\u5316\u4e3a\u5c4f\u5e55\u5750\u6807\u4e4b\u524d\u8fd8\u4f1a\u88ab\u53d8\u6362\u5230\u591a\u4e2a<strong>\u5750\u6807\u7cfb\u7edf(Coordinate System)<\/strong>\u3002\u5c06\u7269\u4f53\u7684\u5750\u6807\u53d8\u6362\u5230\u51e0\u4e2a<strong>\u8fc7\u6e21\u5750\u6807\u7cfb(Intermediate Coordinate System)<\/strong>\u7684\u4f18\u70b9\u5728\u4e8e\uff0c\u5728\u8fd9\u4e9b\u7279\u5b9a\u7684\u5750\u6807\u7cfb\u7edf\u4e2d\uff0c\u4e00\u4e9b\u64cd\u4f5c\u6216\u8fd0\u7b97\u66f4\u52a0\u65b9\u4fbf\u548c\u5bb9\u6613\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u5c40\u90e8\u7a7a\u95f4<\/strong>(<strong>Local Space<\/strong>\uff0c\u6216\u8005\u79f0\u4e3a<strong>\u7269\u4f53\u7a7a\u95f4(Object Space)<\/strong>)<\/li><li><strong>\u4e16\u754c\u7a7a\u95f4<\/strong>(<strong>World Space)<\/strong><\/li><li><strong>\u89c2\u5bdf\u7a7a\u95f4<\/strong>(<strong>View Space<\/strong>\uff0c\u6216\u8005\u79f0\u4e3a<strong>\u89c6\u89c9\u7a7a\u95f4(Eye Space)<\/strong>)<\/li><li><strong>\u88c1\u526a\u7a7a\u95f4(Clip Space)<\/strong><\/li><li><strong>\u5c4f\u5e55\u7a7a\u95f4(Screen Space)<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u5c06\u5750\u6807\u4ece\u4e00\u4e2a\u5750\u6807\u7cfb\u53d8\u6362\u5230\u53e6\u4e00\u4e2a\u5750\u6807\u7cfb\uff0c\u6211\u4eec\u9700\u8981\u7528\u5230\u51e0\u4e2a\u53d8\u6362\u77e9\u9635\uff0c\u6700\u91cd\u8981\u7684\u51e0\u4e2a\u5206\u522b\u662f<strong>\u6a21\u578b(Model)\u3001\u89c2\u5bdf(View)\u3001\u6295\u5f71(Projection)<\/strong>\u4e09\u4e2a\u77e9\u9635\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u7684\u9876\u70b9\u5750\u6807\u8d77\u59cb\u4e8e<strong>\u5c40\u90e8\u5750\u6807(Local Coordinate)<\/strong>\uff0c\u5b83\u5728\u4e4b\u540e\u4f1a\u53d8\u4e3a<strong>\u4e16\u754c\u5750\u6807(World Coordinate)<\/strong>\uff0c<strong>\u89c2\u5bdf\u5750\u6807(View Coordinate)<\/strong>\uff0c<strong>\u88c1\u526a\u5750\u6807(Clip Coordinate)<\/strong>\uff0c\u5e76\u6700\u540e\u4ee5<strong>\u5c4f\u5e55\u5750\u6807(Screen Coordinate)<\/strong>\u7684\u5f62\u5f0f\u7ed3\u675f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/07\/image-30.png\" alt=\"\" class=\"wp-image-804\"\/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5c40\u90e8\u5750\u6807\u662f\u5bf9\u8c61\u76f8\u5bf9\u4e8e\u5c40\u90e8\u539f\u70b9\u7684\u5750\u6807\uff0c\u4e5f\u662f\u7269\u4f53\u8d77\u59cb\u7684\u5750\u6807\u3002<\/li><li>\u4e0b\u4e00\u6b65\u662f\u5c06\u5c40\u90e8\u5750\u6807\u53d8\u6362\u4e3a\u4e16\u754c\u7a7a\u95f4\u5750\u6807\uff0c\u4e16\u754c\u7a7a\u95f4\u5750\u6807\u662f\u5904\u4e8e\u4e00\u4e2a\u66f4\u5927\u7684\u7a7a\u95f4\u8303\u56f4\u7684\u3002\u8fd9\u4e9b\u5750\u6807\u76f8\u5bf9\u4e8e\u4e16\u754c\u7684\u5168\u5c40\u539f\u70b9\uff0c\u5b83\u4eec\u4f1a\u548c\u5176\u5b83\u7269\u4f53\u4e00\u8d77\u76f8\u5bf9\u4e8e\u4e16\u754c\u7684\u539f\u70b9\u8fdb\u884c\u6446\u653e\u3002<\/li><li>\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u4e16\u754c\u5750\u6807\u53d8\u6362\u4e3a\u89c2\u5bdf\u7a7a\u95f4\u5750\u6807\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u5750\u6807\u90fd\u662f\u4ece\u6444\u50cf\u673a\u6216\u8005\u8bf4\u89c2\u5bdf\u8005\u7684\u89d2\u5ea6\u8fdb\u884c\u89c2\u5bdf\u7684\u3002<\/li><li>\u5750\u6807\u5230\u8fbe\u89c2\u5bdf\u7a7a\u95f4\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5176\u6295\u5f71\u5230\u88c1\u526a\u5750\u6807\u3002\u88c1\u526a\u5750\u6807\u4f1a\u88ab\u5904\u7406\u81f3-1.0\u52301.0\u7684\u8303\u56f4\u5185\uff0c\u5e76\u5224\u65ad\u54ea\u4e9b\u9876\u70b9\u5c06\u4f1a\u51fa\u73b0\u5728\u5c4f\u5e55\u4e0a\u3002<\/li><li>\u6700\u540e\uff0c\u6211\u4eec\u5c06\u88c1\u526a\u5750\u6807\u53d8\u6362\u4e3a\u5c4f\u5e55\u5750\u6807\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u4e00\u4e2a\u53eb\u505a\u89c6\u53e3\u53d8\u6362(Viewport Transform)\u7684\u8fc7\u7a0b\u3002\u89c6\u53e3\u53d8\u6362\u5c06\u4f4d\u4e8e-1.0\u52301.0\u8303\u56f4\u7684\u5750\u6807\u53d8\u6362\u5230\u7531glViewport\u51fd\u6570\u6240\u5b9a\u4e49\u7684\u5750\u6807\u8303\u56f4\u5185\u3002\u6700\u540e\u53d8\u6362\u51fa\u6765\u7684\u5750\u6807\u5c06\u4f1a\u9001\u5230\u5149\u6805\u5668\uff0c\u5c06\u5176\u8f6c\u5316\u4e3a\u7247\u6bb5\u3002<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6295\u5f71\u77e9\u9635<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u5c06\u9876\u70b9\u5750\u6807\u4ece\u89c2\u5bdf\u53d8\u6362\u5230\u88c1\u526a\u7a7a\u95f4\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a<strong>\u6295\u5f71\u77e9\u9635(Projection Matrix)<\/strong>\uff0c\u5b83\u6307\u5b9a\u4e86\u4e00\u4e2a\u8303\u56f4\u7684\u5750\u6807\uff0c\u6bd4\u5982\u5728\u6bcf\u4e2a\u7ef4\u5ea6\u4e0a\u7684-1000\u52301000\u3002\u6295\u5f71\u77e9\u9635\u63a5\u7740\u4f1a\u5c06\u5728\u8fd9\u4e2a\u6307\u5b9a\u7684\u8303\u56f4\u5185\u7684\u5750\u6807\u53d8\u6362\u4e3a\u6807\u51c6\u5316\u8bbe\u5907\u5750\u6807\u7684\u8303\u56f4(-1.0, 1.0)\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u53ea\u662f\u56fe\u5143(Primitive)\uff0c\u4f8b\u5982\u4e09\u89d2\u5f62\uff0c\u7684\u4e00\u90e8\u5206\u8d85\u51fa\u4e86<strong>\u88c1\u526a\u4f53\u79ef(Clipping Volume)<\/strong>\uff0c\u5219OpenGL\u4f1a\u91cd\u65b0\u6784\u5efa\u8fd9\u4e2a\u4e09\u89d2\u5f62\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u4e09\u89d2\u5f62\u8ba9\u5176\u80fd\u591f\u9002\u5408\u8fd9\u4e2a\u88c1\u526a\u8303\u56f4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u6295\u5f71\u77e9\u9635\u521b\u5efa\u7684<strong>\u89c2\u5bdf\u7bb1(Viewing Box)<\/strong>\u88ab\u79f0\u4e3a<strong>\u5e73\u622a\u5934\u4f53(Frustum)<\/strong>\uff0c\u6bcf\u4e2a\u51fa\u73b0\u5728\u5e73\u622a\u5934\u4f53\u8303\u56f4\u5185\u7684\u5750\u6807\u90fd\u4f1a\u6700\u7ec8\u51fa\u73b0\u5728\u7528\u6237\u7684\u5c4f\u5e55\u4e0a\u3002\u5c06\u7279\u5b9a\u8303\u56f4\u5185\u7684\u5750\u6807\u8f6c\u5316\u5230\u6807\u51c6\u5316\u8bbe\u5907\u5750\u6807\u7cfb\u7684\u8fc7\u7a0b\uff08\u800c\u4e14\u5b83\u5f88\u5bb9\u6613\u88ab\u6620\u5c04\u52302D\u89c2\u5bdf\u7a7a\u95f4\u5750\u6807\uff09\u88ab\u79f0\u4e4b\u4e3a<strong>\u6295\u5f71(Projection)<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u65e6\u6240\u6709\u9876\u70b9\u88ab\u53d8\u6362\u5230\u88c1\u526a\u7a7a\u95f4\uff0c\u6700\u7ec8\u7684\u64cd\u4f5c\u2014\u2014<strong>\u900f\u89c6\u9664\u6cd5(Perspective Division)<\/strong>\u5c06\u4f1a\u6267\u884c\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u5c06\u4f4d\u7f6e\u5411\u91cf\u7684x\uff0cy\uff0cz\u5206\u91cf\u5206\u522b\u9664\u4ee5\u5411\u91cf\u7684\u9f50\u6b21w\u5206\u91cf\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u5c04\u6295\u5f71<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6b63\u5c04\u6295\u5f71\u77e9\u9635(Orthographic Projection Matrix)<\/strong>\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u4f3c\u7acb\u65b9\u4f53\u7684\u5e73\u622a\u5934\u7bb1\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u4e2a\u88c1\u526a\u7a7a\u95f4\uff0c\u5728\u8fd9\u7a7a\u95f4\u4e4b\u5916\u7684\u9876\u70b9\u90fd\u4f1a\u88ab\u88c1\u526a\u6389\u3002\u521b\u5efa\u4e00\u4e2a\u6b63\u5c04\u6295\u5f71\u77e9\u9635\u7531<strong>\u5bbd<\/strong>\u3001<strong>\u9ad8<\/strong>\u3001<strong>\u8fd1(Near)\u5e73\u9762<\/strong>\u548c<strong>\u8fdc(Far)\u5e73\u9762<\/strong>\u6240\u6307\u5b9a\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/07\/image-31.png\" alt=\"\" class=\"wp-image-807\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u4e00\u4e2a\u6b63\u5c04\u6295\u5f71\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528GLM\u7684\u5185\u7f6e\u51fd\u6570<code>glm::ortho<\/code>\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f);<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u900f\u89c6\u6295\u5f71<\/h4>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/07\/image-33.png\" alt=\"\" class=\"wp-image-810\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>\u900f\u89c6\u6295\u5f71\u77e9\u9635(Perspective Projection Matrix)<\/strong>\u5c06\u7ed9\u5b9a\u7684\u5e73\u622a\u5934\u4f53\u8303\u56f4\u6620\u5c04\u5230\u88c1\u526a\u7a7a\u95f4\uff0c\u9664\u6b64\u4e4b\u5916\u8fd8\u4fee\u6539\u4e86\u6bcf\u4e2a\u9876\u70b9\u5750\u6807\u7684w\u503c\uff0c\u4ece\u800c\u4f7f\u5f97\u79bb\u89c2\u5bdf\u8005\u8d8a\u8fdc\u7684\u9876\u70b9\u5750\u6807w\u5206\u91cf\u8d8a\u5927\u3002\u88ab\u53d8\u6362\u5230\u88c1\u526a\u7a7a\u95f4\u7684\u5750\u6807\u90fd\u4f1a\u5728-w\u5230w\u7684\u8303\u56f4\u4e4b\u95f4\uff08\u4efb\u4f55\u5927\u4e8e\u8fd9\u4e2a\u8303\u56f4\u7684\u5750\u6807\u90fd\u4f1a\u88ab\u88c1\u526a\u6389\uff09\u3002OpenGL\u8981\u6c42\u6240\u6709\u53ef\u89c1\u7684\u5750\u6807\u90fd\u843d\u5728-1.0\u52301.0\u8303\u56f4\u5185\uff0c\u4f5c\u4e3a\u9876\u70b9\u7740\u8272\u5668\u6700\u540e\u7684\u8f93\u51fa\uff0c\u56e0\u6b64\uff0c\u4e00\u65e6\u5750\u6807\u5728\u88c1\u526a\u7a7a\u95f4\u5185\u4e4b\u540e\uff0c\u900f\u89c6\u9664\u6cd5\u5c31\u4f1a\u88ab\u5e94\u7528\u5230\u88c1\u526a\u7a7a\u95f4\u5750\u6807\u4e0a\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/07\/image-32.png\" alt=\"\" class=\"wp-image-809\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">\u9876\u70b9\u5750\u6807\u7684\u6bcf\u4e2a\u5206\u91cf\u90fd\u4f1a\u9664\u4ee5\u5b83\u7684w\u5206\u91cf\uff0c\u8ddd\u79bb\u89c2\u5bdf\u8005\u8d8a\u8fdc\u9876\u70b9\u5750\u6807\u5c31\u4f1a\u8d8a\u5c0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528GLM\u7684\u5185\u7f6e\u51fd\u6570<code>glm::perspective<\/code>\u521b\u5efa\u900f\u89c6\u6295\u5f71\u77e9\u9635\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width\/(float)height, 0.1f, 100.0f);<\/code><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/07\/image-34.png\" alt=\"\" class=\"wp-image-817\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e00\u4e2a\u53c2\u6570\u5b9a\u4e49\u4e86<strong>fov<\/strong>\u7684\u503c\uff0c\u5b83\u8868\u793a\u7684\u662f<strong>\u89c6\u91ce(Field of View)<\/strong>\uff0c\u5e76\u4e14\u8bbe\u7f6e\u4e86\u89c2\u5bdf\u7a7a\u95f4\u7684\u5927\u5c0f\u3002\u5982\u679c\u60f3\u8981\u4e00\u4e2a\u771f\u5b9e\u7684\u89c2\u5bdf\u6548\u679c\uff0c\u5b83\u7684\u503c\u901a\u5e38\u8bbe\u7f6e\u4e3a45.0f\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e86\u5bbd\u9ad8\u6bd4\uff0c\u7531\u89c6\u53e3\u7684\u5bbd\u9664\u4ee5\u9ad8\u6240\u5f97\u3002\u7b2c\u4e09\u548c\u7b2c\u56db\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e86\u5e73\u622a\u5934\u4f53\u7684<strong>\u8fd1<\/strong>\u548c<strong>\u8fdc<\/strong>\u5e73\u9762\u3002\u6211\u4eec\u901a\u5e38\u8bbe\u7f6e\u8fd1\u8ddd\u79bb\u4e3a0.1f\uff0c\u800c\u8fdc\u8ddd\u79bb\u8bbe\u4e3a100.0f\u3002\u6240\u6709\u5728\u8fd1\u5e73\u9762\u548c\u8fdc\u5e73\u9762\u5185\u4e14\u5904\u4e8e\u5e73\u622a\u5934\u4f53\u5185\u7684\u9876\u70b9\u90fd\u4f1a\u88ab\u6e32\u67d3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u88c1\u526a\u5750\u6807<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u9876\u70b9\u5750\u6807\u5c06\u4f1a\u6839\u636e\u4ee5\u4e0b\u8fc7\u7a0b\u88ab\u53d8\u6362\u5230\u88c1\u526a\u5750\u6807\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/07\/image-35.png\" alt=\"\" class=\"wp-image-819\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\u77e9\u9635\u8fd0\u7b97\u7684\u987a\u5e8f\u662f\u76f8\u53cd\u7684\uff08\u4ece\u53f3\u5f80\u5de6\u9605\u8bfb\u77e9\u9635\u7684\u4e58\u6cd5\uff09\uff0c\u6700\u540e\u7684\u9876\u70b9\u5e94\u8be5\u88ab\u8d4b\u503c\u5230\u9876\u70b9\u7740\u8272\u5668\u4e2d\u7684<var><code>gl_Position<\/code><\/var>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ed8\u5236Cube<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u8bbe\u7f6eMVP\u77e9\u9635\nglm::mat4 model = glm::mat4(1);\nmodel = glm::translate(model, glm::vec3(0.0f,0.0f,0.0f));\nmodel = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));\nshaderProgram-&gt;setMatrix4fv(\"model\", glm::value_ptr(model));\nglm::mat4 view = glm::mat4(1);\nview = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));\nshaderProgram-&gt;setMatrix4fv(\"view\", glm::value_ptr(view));\nglm::mat4 projection = glm::mat4(1);\nprojection = glm::perspective(glm::radians(45.0f), WIDTH \/ HEIGHT, 0.1f, 100.0f);\nshaderProgram-&gt;setMatrix4fv(\"projection\", glm::value_ptr(projection));\nglDrawArrays(GL_TRIANGLES, 0, 36);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9876\u70b9\u7740\u8272\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#version 330 core\nlayout (location = 0) in vec3 aPos;\nlayout (location = 1) in vec3 aColor;\nlayout (location = 2) in vec2 aTexCoord;\n\nout vec4 vertexColor;\nout vec2 TexCoord;\n\nuniform mat4 model;\nuniform mat4 view;\nuniform mat4 projection;\n\nvoid main()\n{\n        \/\/ (\u4e58MVP\u77e9\u9635)\u6ce8\u610f\u4e58\u6cd5\u8981\u4ece\u53f3\u5411\u5de6\u8bfb\n\tgl_Position = projection * view * model * vec4(aPos, 1.0f);\n\tvertexColor = vec4(aColor.x,aColor.y,aColor.z,1.0f);\n\tTexCoord = aTexCoord;\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/07\/cube-1.gif\" alt=\"\" class=\"wp-image-826\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Z\u7f13\u51b2<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">OpenGL\u5b58\u50a8\u5b83\u7684\u6240\u6709\u6df1\u5ea6\u4fe1\u606f\u4e8e\u4e00\u4e2a<strong>Z\u7f13\u51b2(Z-buffer)<\/strong>\u4e2d\uff0c\u4e5f\u88ab\u79f0\u4e3a<strong>\u6df1\u5ea6\u7f13\u51b2(Depth Buffer)<\/strong>\u3002GLFW\u4f1a\u81ea\u52a8\u4e3a\u4f60\u751f\u6210\u8fd9\u6837\u4e00\u4e2a\u7f13\u51b2\uff08\u5c31\u50cf\u5b83\u4e5f\u6709\u4e00\u4e2a\u989c\u8272\u7f13\u51b2\u6765\u5b58\u50a8\u8f93\u51fa\u56fe\u50cf\u7684\u989c\u8272\uff09\u3002\u6df1\u5ea6\u503c\u5b58\u50a8\u5728\u6bcf\u4e2a\u7247\u6bb5\u91cc\u9762\uff08\u4f5c\u4e3a\u7247\u6bb5\u7684<strong>z<\/strong>\u503c\uff09\uff0c\u5f53\u7247\u6bb5\u60f3\u8981\u8f93\u51fa\u5b83\u7684\u989c\u8272\u65f6\uff0cOpenGL\u4f1a\u5c06\u5b83\u7684\u6df1\u5ea6\u503c\u548cz\u7f13\u51b2\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u5f53\u524d\u7684\u7247\u6bb5\u5728\u5176\u5b83\u7247\u6bb5\u4e4b\u540e\uff0c\u5b83\u5c06\u4f1a\u88ab\u4e22\u5f03\uff0c\u5426\u5219\u5c06\u4f1a\u8986\u76d6\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a<strong>\u6df1\u5ea6\u6d4b\u8bd5(Depth Testing)<\/strong>\uff0c\u5b83\u662f\u7531OpenGL\u81ea\u52a8\u5b8c\u6210\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>glEnable(GL_DEPTH_TEST);<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u4e3a\u4f7f\u7528\u4e86\u6df1\u5ea6\u6d4b\u8bd5\uff0c\u8981\u5728\u6bcf\u6b21\u6e32\u67d3\u8fed\u4ee3\u4e4b\u524d\u6e05\u9664\u6df1\u5ea6\u7f13\u51b2\uff08\u5426\u5219\u524d\u4e00\u5e27\u7684\u6df1\u5ea6\u4fe1\u606f\u4ecd\u7136\u4fdd\u5b58\u5728\u7f13\u51b2\u4e2d\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728glClear\u51fd\u6570\u4e2d\u6307\u5b9a<var>DEPTH_BUFFER_BIT<\/var>\u4f4d\u6765\u6e05\u9664\u6df1\u5ea6\u7f13\u51b2\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/07\/cube2.gif\" alt=\"\" class=\"wp-image-828\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5faa\u73af\u6570\u7ec4\u8bbe\u7f6e\u5750\u6807\u8c03\u7528\u5341\u6b21<code>glDrawArrays(GL_TRIANGLES, 0, 36)<\/code>\uff0c\u5f97\u5230\u4e00\u4e0b\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.acgloby.com\/wp-content\/uploads\/2022\/07\/cube3.gif\" alt=\"\" class=\"wp-image-830\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b8c\u6574\u5de5\u7a0b\u4ee3\u7801Github\uff1a<\/p>\n\n\n\n[github repo=\"acgloby\/LearnOpenGL\"]\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>OpenGL\u53d8\u6362\u662f\u7528\u4e00\u4e2aGLM\u5e93\uff0cGLM\u662fOpenGL&nbsp;Mathematics\u7684\u7f29\u5199\uff0c\u5b83\u662f\u4e00\u4e2a\u53ea\u6709\u5934\u6587\u4ef6\u7684\u5e93\u3002 \u6211\u4eec\u9700\u8981 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":828,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-789","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-graphics"],"_links":{"self":[{"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/posts\/789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/comments?post=789"}],"version-history":[{"count":0,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/posts\/789\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/media?parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/categories?post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.acgloby.com\/index.php\/wp-json\/wp\/v2\/tags?post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}