学过了不同光照的渲染,接下来就轮到更多模型的拓展了,现实生活中我们不可能所有物体都是简单的立方体,还会有更复杂的模型,怎么将这些模型导入OpenGL?
Assimp,它是Open Asset Import Library(开放的资产导入库)的缩写。Assimp能够导入很多种不同的模型文件格式(并也能够导出部分的格式),它会将所有的模型数据加载至Assimp的通用数据结构中。
为了使用这个库,我们需要进入这个地址下载它的源码(选择第三项)
将下载好的文件解压后,打开之前安装过的Cmake,选择解压的目录为源代码目录,然后在该目录下创建一个build文件夹,选择它为build文件的存储路径:
如果出现Could not locate DirectX,请去安装DirectX SDK
安装DirectX SDK失败,出现Error Code:s1023?
卸载所有电脑内C++ Redistributable packages:
出现这个界面之后不用担心,点击左下角Generate,完成之后再点击Open Project打开VS,右键解决方案,选择生成解决方案:
出现无法打开包括文件: “gtest/gtest.h”或者“gtest.lib”?
看看是不是下载的最新版的assimp库
一切无误后会出现成功提示:
将导出工程中build\include\assimp 的 config.h 拷到源码 include/assimp 目录中
将源码 include\assimp 文件夹拷贝到 VS2019 安装目录下的 \Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.25.28610\include
将编译好的build\lib\Debug 下的 assimp-vc142-mt.lib 拷贝到 \Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.25.28610\lib\x64目录
拷贝 build\bin\Debug的assimp-vc142-mt.dll 到系统目录,这里需要注意下 32 位 系统和 64 位 系统的区别。64 位 系统下 System32 目录是存放 64位 dll 的,SysWOW64 是用来存放 32位 dll 的
#include // C++ importer interface
#include // Output data structure
#include // Post processing flags
#include
#pragma comment (lib, "assimp-vc142-mt.lib")
void LoadFinish(const aiScene* scene)
{
std::cout << "LoadFinish ! NumVertices : " << (*(scene->mMeshes))->mNumVertices << std::endl;
}
bool LoadModel(const std::string& pFile)
{
// Create an instance of the Importer class
Assimp::Importer importer;
// And have it read the given file with some example postprocessing
// Usually - if speed is not the most important aspect for you - you'll
// probably to request more postprocessing than we do in this example.
const aiScene* scene = importer.ReadFile(pFile,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType);
// If the import failed, report it
if (!scene)
{
std::cout << importer.GetErrorString() << std::endl;
return false;
}
// Now we can access the file's contents.
LoadFinish(scene);
// We're done. Everything will be cleaned up by the importer destructor
return true;
}
int main()
{
LoadModel("bun_zipper.ply");
return 0;
}