Assimp
ãã¹ãŠã®ã¬ãã¹ã³ã§ãç§ãã¡ã¯äž»ã«å°ããªå人-ã³ã³ããã䜿çšããŸãããããã°ãããããšã芪åã§ããå°ãéå±ã«ãªããŸãã 倧èŠæš¡ãªã°ã©ãã£ã«ã«ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãéåžžãéçã³ã³ãããããèŠæ ãã®è¯ãã¢ãã«ãå€æ°ãããŸãã ã³ã³ãããšã¯ç°ãªãã家ã人åãªã©ã®è€éãªã¢ãã«ã®ãã¹ãŠã®é ç¹ãæ³ç·ããã¯ã¹ãã£åº§æšãæåã§æ±ºå®ããããšã¯éåžžã«å°é£ã§ãã 代ããã«ãã¢ããªã±ãŒã·ã§ã³ã«ã¢ãã«ãã€ã³ããŒãããŸãã Blenderã3DS MAXãMayaãªã©ã®3Dãšãã£ã¿ãŒã§æ éã«ãã€ã³ããããã¢ãã«ã
ã¡ãã¥ãŒ
ãããã®ãããã3Dã¢ããªã³ã°ããŒã«ã䜿çšãããšãã¢ãŒãã£ã¹ãã¯è€éãªã¢ãã«ãäœæãããã¯ã¹ãã£ãããã³ã°ïŒuvãããã³ã°ïŒãšåŒã°ããããã»ã¹ã䜿çšããŠãããã«ãã¯ã¹ãã£ãé©çšã§ããŸãã ããŒã«ã¯ããã¹ãŠã®é ç¹åº§æšãæ³ç·é ç¹ãããã³ãã¯ã¹ãã£åº§æšãèªåçã«çæããããããã¢ãã«ãã¡ã€ã«ã«ãšã¯ã¹ããŒãããŸãã ãããã£ãŠãã¢ãŒãã£ã¹ãã«ã¯ãæè¡çãªè©³çŽ°ãæ°ã«ããããšãªããé«å質ã®ã¢ãã«ãäœæããããã®åºç¯ãªããŒã«ã»ãããçšæãããŠããŸãã ãšã¯ã¹ããŒãããããã¡ã€ã«ã«ã¯ããã¹ãŠã®æè¡çåŽé¢ãé ãããŠããŸãã ã°ã©ãã£ãã¯ããã°ã©ããšããŠããããã®æè¡çãªè©³çŽ°ã«æ³šæããå¿ èŠããããŸãã
ãããã£ãŠãç§ãã¡ã®ä»äºã¯ããããã®ãšã¯ã¹ããŒããããã¢ãã«ãã¡ã€ã«ãåæãããã¹ãŠã®é¢é£æ å ±ãæœåºããŠãOpenGLãç解ã§ãã圢åŒã§ä¿åã§ããããã«ããããšã§ãã ãã ããäžè¬çãªåé¡ã¯ãããŸããŸãªãã¡ã€ã«åœ¢åŒãå€æ°ããããããããç¬èªã®æ¹æ³ã§ã¢ãã«ããŒã¿ããšã¯ã¹ããŒãããããšã§ãã Wavefront .objã®ãããªã¢ãã«åœ¢åŒã«ã¯ãã¢ãã«ã®è²ãæ¡æ£/ã°ã¬ã¢ããããªã©ã®ãã€ããŒãªãããªã¢ã«æ å ±ãå«ãã¢ãã«ããŒã¿ã®ã¿ãå«ãŸããŸãããXMLããŒã¹ã®ã¢ãã«ãã¡ã€ã«ã¯éåžžã«åºç¯å²ã§ãã¢ãã«ãç §æããããªã¢ã«ã«é¢ããå€ãã®æ å ±ãå«ãŸããŸããã¢ãã¡ãŒã·ã§ã³ãªã©ã obj圢åŒã¯ç°¡åã«è§£æããããšèŠãªãããŸãã ãŠã£ãããã£ã¢ã®ããŒãžãªã©ã§ããã¡ã€ã«æ§é ã«ç²Ÿéããããšããå§ãããŸãã ããã«ãããã¢ãã«ãobjãã¡ã€ã«ã«ä¿åãããæ¹æ³ã«é¢ããåºæ¬çãªæ å ±ãåŸãããŸãã
äžè¬çã«ãä¿åãããããŒã¿æ§é ãç°ãªãå€ãã®ç°ãªã圢åŒããããŸãã ãããã£ãŠããããã®ãã¡ã€ã«ããã¢ãã«ãã€ã³ããŒãããå Žåã¯ãã€ã³ããŒãããå圢åŒããšã«ã€ã³ããŒã¿ãŒãäœæããå¿ èŠããããŸãã ç§ãã¡ã«ãšã£ãŠã¯è¯ãããšã§ããããã«ã¯ã©ã€ãã©ãªããããŸãã
ã¢ãã«ãããŒãããããã®ã©ã€ãã©ãª
Assimpã¯ãã¢ãã«ãã€ã³ããŒãããããã®éåžžã«äººæ°ã®ããã©ã€ãã©ãªã§ãOpen Asset Import Libraryã®ç¥ã§ãã ãã®ã©ã€ãã©ãªã¯ãã¢ãã«ãå«ãããŸããŸãªåœ¢åŒãã€ã³ããŒãããã€ã³ããŒããããããŒã¿ãåçŽãªéå±€ããŒã¿æ§é ã®åœ¢åŒã§ä¿åã§ããŸãã Assimpãã¢ãã«ã®èªã¿èŸŒã¿ãå®äºãããšããã®æ§é ããå¿ èŠãªãã¹ãŠã®ããŒã¿ãååŸã§ããŸãã ã€ã³ããŒããããã¡ã€ã«åœ¢åŒã¯é¢ä¿ãããŸãããæ§é å ã®ããŒã¿ã¢ã¯ã»ã¹ã¯å€æŽããããæ§é ã¯ãã¹ãŠã®ç°ãªããã¡ã€ã«åœ¢åŒã«å¯ŸããŠåããŸãŸã§ãã
Assimpã䜿çšããŠã¢ãã«ãã€ã³ããŒããããšãã©ã€ãã©ãªã¯ã€ã³ããŒããããã¢ãã«ã®ãã¹ãŠã®ããŒã¿ãå«ãã·ãŒã³ãªããžã§ã¯ãïŒã·ãŒã³ïŒã«ã¢ãã«å šäœãããŒãããŸãã 次ã«Assimpã¯ããŒãã®ã³ã¬ã¯ã·ã§ã³ãäœæããŸããåããŒãã«ã¯ã·ãŒã³ãªããžã§ã¯ãã«æ ŒçŽãããŠããããŒã¿ãžã®ã€ã³ããã¯ã¹ãå«ãŸããåããŒãã«ã¯åå«ãå«ããããšãã§ããŸãã åçŽãªAssimpæ§é ã¢ãã«ã以äžã«ç€ºããŸãã
- ãã¹ãŠã®ã¢ãã«ããŒã¿ã¯Sceneãªããžã§ã¯ãã«å«ãŸããŠããããããªã¢ã«ããã³ããªãŽã³ã¡ãã·ã¥ããŒã¿ãå«ãŸããŠããŸãã ã·ãŒã³ã«ã¯ãã·ãŒã³ã®ã«ãŒãããŒããžã®ãªã³ã¯ãå«ãŸããŠããŸãã
- ã«ãŒãããŒãïŒã«ãŒãããŒãïŒã¯åå«ïŒåããŒãïŒïŒããã³ä»ã®ããŒãïŒãæã€ããšãã§ããmmeshesã·ãŒã³ãªããžã§ã¯ãã®é åã«æ ŒçŽãããããªãŽã³ã¡ãã·ã¥ã®ããŒã¿ã瀺ãã€ã³ããã¯ã¹ãæã€ããšãã§ããŸãã ã«ãŒãããŒãã®mMeshesé åã«ã¯ãããªãŽã³ã¡ãã·ã¥ã®ç¹å®ã®ãªããžã§ã¯ããå«ãŸããåå«ã®mMeshesé åã®å€ã¯ãã«ãŒãããŒãã®ã°ãªããã®é åããéžæããããã®ã€ã³ããã¯ã¹ã«ãããŸããã
- ã¡ãã·ã¥ãªããžã§ã¯ãïŒã¡ãã·ã¥ïŒã«ã¯ãã¬ã³ããªã³ã°ã«å¿ èŠãªãã¹ãŠã®é¢é£ããŒã¿ïŒé ç¹åº§æšãæ³ç·ãã¯ãã«ããã¯ã¹ãã£åº§æšãé¢ããªããžã§ã¯ãã®ãããªã¢ã«ã«é¢ããæ å ±ïŒãå«ãŸããŠããŸãã
- ããªãŽã³ã¡ãã·ã¥ïŒã¡ãã·ã¥ïŒã«ã¯è€æ°ã®é¢ãå«ãŸããŠããŸãã é¢ã¯ããªããžã§ã¯ãã®ããªããã£ãïŒäžè§åœ¢ãæ£æ¹åœ¢ãç¹ïŒã§ãã ããªããã£ãã圢æããé ç¹ã€ã³ããã¯ã¹ãå«ãŸããŠããŸãã é ç¹ãšãã®ã€ã³ããã¯ã¹ããããããã¬ã³ããªã³ã°ã¯èŠçŽ ãããã¡ãŒãªããžã§ã¯ãïŒ Hello Triangle ïŒã®ãããã§ã¯ããã«ç°¡åã«ãªããŸã
- ãŸããããªãŽã³ã¡ãã·ã¥ã«ã¯ããããªã¢ã«ããããã£ïŒãããªã¢ã«ïŒã®ã€ã³ããã¯ã¹ãå«ãŸããŠããããããªã¢ã«ããããã£ãååŸããããã®ããã€ãã®æ©èœããããŸãã
ãã®ããããŸããªããžã§ã¯ããã·ãŒã³ãªããžã§ã¯ãã«ããŒãããåããŒããã察å¿ããããªãŽã³ã¡ãã·ã¥ãªããžã§ã¯ããååž°çã«æœåºãïŒåããŒãã®åå«ã«æ²¿ã£ãŠååž°çã«æ©ããŠïŒãããªãŽã³ã¡ãã·ã¥ã®åèŠçŽ ãåŠçããŠãé ç¹ãã€ã³ããã¯ã¹ãããã³ãããªã¢ã«ããããã£ãæœåºããå¿ èŠããããŸãã çµæã¯ãModelãªããžã§ã¯ãã«å«ãŸããããªãŽã³ã¡ãã·ã¥ã®ã³ã¬ã¯ã·ã§ã³ã§ãã
ã¡ãã·ã¥-é ç¹ãšäžè§åœ¢ã®ã»ãã
1ã€ã®ã¡ãã·ã¥ïŒã¡ãã·ã¥ïŒã¯ãOpenGLããŒã«ïŒé ç¹ãã€ã³ããã¯ã¹ããããªã¢ã«ããŒã¿ïŒã䜿çšããåºåã«å¿ èŠãªæå°éã®ããŒã¿ã»ããã§ãã ååãšããŠãã¢ãã«ã¯è€æ°ã®ã°ãªããã§æ§æãããŸãã ç¹å¥ãªããã°ã©ã ïŒBlenderã3D maxïŒã§ãªããžã§ã¯ããã¢ããªã³ã°ããå Žåãã¢ãŒãã£ã¹ãã¯1ã€ã®ãã©ãŒã ããã¢ãã«å šäœãäœæããŸããã éåžžãåã¢ãã«ã«ã¯è€æ°ã®ãµãã¢ãã«\ãã©ãŒã ããããŸãã 人ãã¢ãã«ãšèããŠãã ãããéåžžãã¢ãŒãã£ã¹ãã¯é ãæ足ãè¡£æãæŠåšããã¹ãŠå¥ã ã®ã³ã³ããŒãã³ããšããŠã¢ããªã³ã°ãããã¹ãŠã®ãµãã¢ãã«ãçµã¿åãããŠãå ã®ã¢ãã«ãååŸããŸãã
次ã®ã¬ãã¹ã³ã§ã¯ã説æããæ§é ã䜿çšããŠã€ã³ããŒãããã¢ãã«ãããŒãããã³ä¿åããç¬èªã®Modelããã³Meshã¯ã©ã¹ãäœæããŸãã ã¢ãã«ãæç»ããå Žåãã¢ãã«å šäœã衚瀺ããã®ã§ã¯ãªããã¢ãã«ãæ§æããåã°ãªããã®åºåãåå¥ã«å®è¡ããŸãã ã¢ãã«ãã€ã³ããŒãããåã«ãæåã«Assimpããããžã§ã¯ãã«å«ããå¿ èŠããããŸãã
Assimpã¢ã»ã³ããª
é©åãªããŒãžã§ã³ãéžæããŠããã®ããŒãžããAssimpãããŠã³ããŒãã§ããŸãã å·çæç¹ã§ã¯ãAssimpã®ææ°ããŒãžã§ã³ã¯3.1.1ã§ããã ããªã³ã³ãã€ã«ãããã©ã€ãã©ãªã¯ã»ãšãã©ã®ã·ã¹ãã ã§åäœããªããããã©ã€ãã©ãªãèªåã§ã³ã³ãã€ã«ããããšããå§ãããŸãã CMakeã䜿çšããŠã©ã€ãã©ãªãèªåã§ã³ã³ãã€ã«ããæ¹æ³ãå¿ããå Žåã¯ã ãŠã£ã³ããŠã®äœæã¬ãã¹ã³ã確èªããŠãã ããã
Assimpã®ãã«ãäžã«ããã€ãã®åé¡ãçºçãããããããã§ããŒã¯ããŸããåããšã©ãŒãçºçããå Žåã®è§£æ±ºçã瀺ããŸãã
- CMakeã¯ã次ã®ãããªDiretXã©ã€ãã©ãªã®æ¬ èœã«é¢ãããšã©ãŒãã¹ããŒããŸãã
DirectXãèŠã€ãããŸããã§ãã
cmake-modules / FindPkgMacros.cmakeã§ã®CMakeãšã©ãŒïŒ110ïŒã¡ãã»ãŒãžïŒïŒ
å¿ èŠãªã©ã€ãã©ãªDirectXãèŠã€ãããŸããïŒ ã©ã€ãã©ãªãã€ã³ã¹ããŒã«ããŸãïŒdevããã±ãŒãžãå«ãïŒ
ããäžåºŠããçŽããŠãã ããã ã©ã€ãã©ãªãæ¢ã«ã€ã³ã¹ããŒã«ãããŠããå Žåãäžè¶³ããŠããå€æ°ãèšå®ããŸã
cmakeã§æåã§ã
DirectX SDKãã€ã³ã¹ããŒã«ãããŠããªãå Žåã¯ãã€ã³ã¹ããŒã«ããå¿ èŠããããŸãã SDKã¯ãã¡ãããããŠã³ããŒãã§ããŸãã - ã€ã³ã¹ããŒã«äžã«ãDirectX SDKã¯ãšã©ãŒã³ãŒãs1023ããããã¢ããããŸãã
ãã®å ŽåãSDKãã€ã³ã¹ããŒã«ããåã«ããŸãC ++ããã±ãŒãžãã€ã³ã¹ããŒã«ããå¿ èŠããããŸãã
ããã§èª¬æãããããã« ã
- ã»ããã¢ãããå®äºãããããããžã§ã¯ããã¡ã€ã«ãäœæããŠéããã©ã€ãã©ãªãã³ã³ãã€ã«ã§ããŸãã
- ããã©ã«ãã§ã¯ãAssimpã¯åçã©ã€ãã©ãªãšããŠæäŸãããŠãããããassimp.DLLãšããååã®å¯Ÿå¿ããdllããããžã§ã¯ãã«å«ããå¿ èŠããããŸãã ã¢ããªã±ãŒã·ã§ã³ã®å®è¡å¯èœãã¡ã€ã«ãšåããã©ã«ããŒã«DLLããã¡ã€ã«ããã ãã§ãã
- ã³ã³ãã€ã«åŸãã©ã€ãã©ãªãšãã®dllãã¡ã€ã«ã¯code / DebugãŸãã¯code / Releaseãã©ã«ããŒã«é 眮ãããŸãã
- 次ã«ãã©ã€ãã©ãªãã¡ã€ã«ãšdllãã¡ã€ã«ããããžã§ã¯ãã«ãªã³ã¯ããAssimpããããŒãã¡ã€ã«ãå«ããããšãå¿ããªãã§ãã ããã
ãã«ãã¹ã¬ããã䜿çšããŠããã©ãŒãã³ã¹ãåäžãããå ŽåãBossã§Assimpããã«ãã§ããŸãã å®å šãªæé ã¯ãã¡ãã§ãã
ãã®æç¹ã§ãAssimpãã³ã³ãã€ã«ããã¢ããªã±ãŒã·ã§ã³ã«æ¿å ¥ããå¿ èŠããããŸãã
ã¡ãã·ã¥ã¯ã©ã¹
Assimpã䜿çšãããšãå€ãã®ç°ãªãã¢ãã«ãã¢ããªã±ãŒã·ã§ã³ã«ããŒãã§ããŸãããããŒãåŸãããŒã¿ã¯Assimpæ§é äœã«ä¿åãããŸãã ã¢ãã«ãæç»ã§ããããã«ããã®ããŒã¿ãOpenGLãç解ã§ãã圢åŒã«å€æããå¿ èŠããããŸãã åã®ã¬ãã¹ã³ã§ã¯ãããªãŽã³ã¡ãã·ã¥ãåäžã®æç»ãšã³ãã£ãã£ã§ããããšãåŠç¿ããã®ã§ãç¬èªã®Meshã¯ã©ã¹ãå®çŸ©ããããšããå§ããŸãããã
ã¯ã©ã¹ã«å¿ èŠãªããŒã¿ã»ããã«ã€ããŠèããŠã¿ãŸãããã ããªãŽã³ã¡ãã·ã¥ã«ã¯äžé£ã®é ç¹ãå¿ èŠã§ããåé ç¹ã«ã¯ãäœçœ®ãã¯ãã«ãæ³ç·ãã¯ãã«ãããã³ãã¯ã¹ãã£åº§æšãã¯ãã«ãå«ãŸããŠããŸãã ã¡ãã·ã¥ã«ã¯ã€ã³ããã¯ã¹ãå«ãŸããŠããå¿ èŠãããã
ã€ã³ããã¯ã¹ä»ãã¬ã³ããªã³ã°çšãããã³ãããªã¢ã«çšã®ããŒã¿ã
ããã§ãé ç¹ã®æ§é ãå®çŸ©ã§ããŸãã
struct Vertex { glm::vec3 Position; glm::vec3 Normal; glm::vec2 TexCoords; };
åé ç¹ã¯é ç¹æ§é ã«æ ŒçŽãããåé ç¹ã®ã€ã³ããã¯ã¹åã«äœ¿çšã§ããŸãã é ç¹æ§é ã«å ããŠããã¯ã¹ãã£ããŒã¿ãæ ŒçŽããæ§é ãäœæããå¿ èŠããããŸãã
struct Texture { unsigned int id; string type; };
IDãšãã¯ã¹ãã£ã¿ã€ãïŒæ¡æ£ãŸãã¯ãã¬ã¢ïŒãä¿åããŸãã
æ§é ãäœæããããã¯ã©ã¹ã®äœæãéå§ã§ããŸãã
class Mesh { public: /* Mesh Data */ vector<Vertex> vertices; vector<unsigned int> indices; vector<Texture> textures; /* Functions */ Mesh(vector<Vertex> vertices, vector<unsigned int> indices, vector<Texture> textures); void Draw(Shader shader); private: /* Render data */ unsigned int VAO, VBO, EBO; /* Functions */ void setupMesh(); };
ã芧ã®ãšãããã¯ã©ã¹ã¯ããã»ã©è€éã§ã¯ãããŸããã ã³ã³ã¹ãã©ã¯ã¿ãŒã¯å¿ èŠãªãã¹ãŠã®ããŒã¿ãåãåããsetupMeshã¡ãœããã§ãããã¡ãŒãåæåããDrawã¡ãœããã§ããªãŽã³ã¡ãã·ã¥ãæç»ããŸãã Drawé¢æ°ã¯ã·ã§ãŒããŒãªããžã§ã¯ããåãå ¥ãããããã¬ã³ããªã³ã°ã®åã«å¯Ÿå¿ããåäžå€æ°ãèšå®ã§ããããšã«æ³šæããŠãã ããã
ã³ã³ã¹ãã©ã¯ã¿ãŒã®ã³ãŒãã¯éåžžã«åçŽã§ã察å¿ããåŒæ°ãã¯ã©ã¹åŒæ°ã«å²ãåœãŠãã ãã§ãã setupMeshé¢æ°ãåŒã³åºããŸãã
Mesh(vector<Vertex> vertices, vector<unsigned int> indices, vector<Texture> textures) { this->vertices = vertices; this->indices = indices; this->textures = textures; setupMesh(); }
ã芧ã®ãšãããããã§ã¯ç°åžžãªããšã¯äœãèµ·ããŠããŸããã 次ã«ãsetupMeshé¢æ°ã«é²ã¿ãŸãã
åæå
ã³ã³ã¹ãã©ã¯ã¿ãŒã®ãããã§ãã¬ã³ããªã³ã°ã«äœ¿çšã§ããå¿ èŠãªããŒã¿ããã¹ãŠæã£ãŠããŸãã ãã ããæåã«é©åãªãããã¡ãŒãæ§æããå¿ èŠããããŸãã ãã®æç¹ã§ã¯ããããã®æŠå¿µã«åé¡ã¯ãªãã¯ãã§ããããããããæ§é å ã®ãããã¡ã«ããŒã¿ã転éããæ¹æ³ãå°ãé©ãããã§ãããã
void setupMesh() { glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); // vertex positions glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0); // vertex normals glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal)); // vertex texture coords glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords)); glBindVertexArray(0); }
ã³ãŒãã¯æåŸ ãããã®ãšå€§å·®ãããŸããããããã€ãã®å°ããªããªãã¯ãVertexãã¬ãŒã ã¯ãŒã¯ã§äœ¿çšãããŸããã
C ++ã§ã¯ãæ§é äœã«ã¯åªããç¹æ§ããããŸã-ã¡ã¢ãªã¯ã·ãŒã±ã³ã·ã£ã«ã§ãã ã€ãŸããæ§é äœãããŒã¿ã®é åãšããŠè¡šçŸããå Žåãæ§é äœèªäœã§å®çŸ©ãããŠããé åºã§å€æ°ãå«ãŸããŸãã ããšãã°ãVertexæ§é äœã«ããã€ãã®å€ãå ¥åãããšãã¡ã¢ãªå ã§ã®é 眮ã¯æ¬¡ã®ããã«ãªããŸãã
Vertex vertex; vertex.Position = glm::vec3(0.2f, 0.4f, 0.6f); vertex.Normal = glm::vec3(0.0f, 1.0f, 0.0f); vertex.TexCoords = glm::vec2(1.0f, 0.0f); // = [0.2f, 0.4f, 0.6f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f];
ããšãã°ããã®ããããã£ã«ãããsizeofé¢æ°ãæ§é ã«é©çšãããšãå®çŸ©ãããŠãããã¹ãŠã®åŒæ°ã®ãµã€ãºãè¿ãããŸãã éãã¯32ãã€ãã§ãªããã°ãªããŸãã
ïŒ8 * 4-ãµã€ãº1ãããŒãïŒã glBufferDataé¢æ°ã«ããã䜿çšã§ããŸãïŒ
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices[0], GL_STATIC_DRAW);
ãŸããæåã®åŒæ°ãšããŠæ§é äœãã2çªç®ã®æ§é äœå€æ°ã®ååããšãoffsetofãã¯ãã䜿çšããŸãã ãããŠã2çªç®ã®åŒæ°ã§æž¡ãããå€æ°ã«ãæå®ãããæ§é äœã®ãªãã»ããããã€ãåäœã§è¿ããŸãã ããã¯ãglVertexAttribPointeré¢æ°ã®æåŸã®ãã©ã¡ãŒã¿ãŒãå®çŸ©ããã®ã«çæ³çã§ãã
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
ãªãã»ããã¯ãoffsetofãã¯ãã䜿çšããŠæ±ºå®ãããŸãããã®å Žåãéåžžã®ãã¯ãã«ã®ãã€ããªãã»ãããèšå®ããŸãã ãŸããæ§é ã®ãµã€ãºã«çããã¹ããããµã€ãºãæå®ããŠããããšã«æ³šæããŠãã ããã
ãã®æ§é ã䜿çšãããšãã³ãŒããèªã¿ããããªãã ãã§ãªããå°æ¥çã«æ¡åŒµããããšãã§ããŸãã ä»ã®é ç¹ã¢ãŒãã£ãã¡ã¯ãã䜿çšããå Žåã¯ãæ§é ã«ç°¡åã«è¿œå ã§ããŸããæè»æ§ããããããã³ãŒãã¯å£ããŸããã
æç»
æåŸã«èšè¿°ããé¢æ°ã¯Drawã§ãã ãã ããããªãŽã³ãæç»ããåã«ãglDrawElementsé¢æ°ãåŒã³åºãåã«ããŸããã¯ã¹ãã£ããã€ã³ãããå¿ èŠããããŸãã ãã ããããã¯å°ãé£ããã§ãã ãã¯ã¹ãã£ã®æ°ïŒååšããå ŽåïŒãšãã®ã¿ã€ãã¯ããããŸããã ãŸããã·ã§ãŒããŒã§ãã¯ã¹ãã£ãŠããããšãã¯ã¹ãã£ãªããžã§ã¯ããèšå®ããæ¹æ³ã¯ïŒ
ãã®åé¡ã解決ããã«ã¯ãããã€ãã®åœåèŠåãåãå ¥ããŸãïŒåæ¡æ£ãã¯ã¹ãã£ã¯texture_diffuseNãšåŒã°ããåã°ã¬ã¢ãã¯ã¹ãã£ãããã¯texture_specularNãšåŒã°ããå¿ èŠããããŸããNã¯1ããèš±å¯ããããã¯ã¹ãã£ã®æ倧æ°ãŸã§ã®ä»»æã®æ°ã§ãã ç¹å®ã®ããªãŽã³ã¡ãã·ã¥ã«3ã€ã®æ¡æ£ãã¯ã¹ãã£ãš2ã€ã®ã°ã¬ã¢ãã¯ã¹ãã£ããããšããŸãããããã次ã®ããã«å®çŸ©ããå¿ èŠããããŸãã
uniform sampler2D texture_diffuse1; uniform sampler2D texture_diffuse2; uniform sampler2D texture_diffuse3; uniform sampler2D texture_specular1; uniform sampler2D texture_specular2;
ãã®åæã®ãããã§ããã¯ã¹ãã£ãªããžã§ã¯ããå¿ èŠãªæ°ã ãå®çŸ©ããããšãã§ããããªãŽã³ã¡ãã·ã¥ã«å®éã«å€ãã®ãã¯ã¹ãã£ãå«ãŸããŠããå Žåããã®ååããããããã«ãªããŸãã 1ã€ã®ããªãŽã³ã¡ãã·ã¥ã§ä»»æã®æ°ã®ãã¯ã¹ãã£ãåŠçã§ããéçºè ã¯ã·ã§ãŒããŒã§è¿œå ã®ãã¯ã¹ãã£ãªããžã§ã¯ããå®çŸ©ããã ãã§ãå¿ èŠãªã ããã¯ã¹ãã£ãèªç±ã«äœ¿çšã§ããŸãã
ãã®ãœãªã¥ãŒã·ã§ã³ã«å ããŠãä»ã«ãå€ãã®ãœãªã¥ãŒã·ã§ã³ããããŸããæ°ã«å ¥ããªãå Žåã¯ãåµé æ§ãçºæ®ããŠç¬èªã®ãœãªã¥ãŒã·ã§ã³ãèãåºãããšãã§ããŸãã
ã¡ãœããã³ãŒãã®æç»ïŒ
void Draw(Shader shader) { unsigned int diffuseNr = 1; unsigned int specularNr = 1; for(unsigned int i = 0; i < textures.size(); i++) { glActiveTexture(GL_TEXTURE0 + i); // , // stringstream ss; string number; string name = textures[i].type; if(name == "texture_diffuse") ss << diffuseNr++; // unsigned int stream else if(name == "texture_specular") ss << specularNr++; // unsigned int stream number = ss.str(); shader.setFloat(("material." + name + number).c_str(), i); glBindTexture(GL_TEXTURE_2D, textures[i].id); } glActiveTexture(GL_TEXTURE0); // glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); glBindVertexArray(0); }
ããã¯æãçŸããã³ãŒãã§ã¯ãããŸããããC ++ã¯ãããšãã°ãintããstringãžã®åå€æãæããªããããéšåçã«è²¬ä»»ããããŸãã N-textureã調ã¹ãŠããããã®ã¿ã€ããæååå€æ°ã«å²ãåœãŠãããšã決å®ããç¹å®ã®ã¿ã€ãã®ãã¯ã¹ãã£ã®æ°ãè¡šãããã«ããŸãã 次ã«ããã¯ã¹ãã£ãªããžã§ã¯ãã®çªå·ã確èªããæåŸã«ãã®ãã¯ã¹ãã£ãªããžã§ã¯ããã¢ã¯ãã£ããªãã¯ã¹ãã£ãããã¯ã«å¯Ÿå¿ããçªå·ã«èšå®ããŠããã¯ã¹ãã£ããã€ã³ãããŸãã éåžžã®ããã«ãMaterialãªããžã§ã¯ãã«ãã¯ã¹ãã£ãªããžã§ã¯ããä¿åããããšã«æ³šæããŠãã ããã
æ¡æ£ããã³ãã¬ã¢ã«ãŠã³ã¿ãŒãå¢ããããšã§ãããã«æååã¹ããªãŒã ã«æž¡ãããšã«æ³šæããŠãã ããã C ++ã®å³ã€ã³ã¯ãªã¡ã³ãã¯å€ã1ã€ã³ã¯ãªã¡ã³ãããŸãããå€ãå€ãè¿ããŸãã
Meshã¯ã©ã¹ã®å®å šãªã³ãŒãã«ã€ããŠã¯ã ãã¡ããã芧ãã ãã ã
次ã®ã¬ãã¹ã³ã§ã¯ãMeshã¯ã©ã¹ã®ãªããžã§ã¯ãã®ã³ã³ããã®ããã«æ©èœããå®éã«AssimpããŒãã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ããModelã¯ã©ã¹ãäœæããŸãã