Assimp 编译(Visual Studio 2019)

一、前言

Assimp 是一个非常流行的模型导入库,它是 Open Asset Import Library(开放的资源导入库)的缩写。Assimp 能够导入多种不同的模型文件格式(并也能够导出部分的格式),它能将不同格式的模型数据加载至 Assimp 的通用数据结构中。当 Assimp 加载完模型之后,我们就能够从 Assimp 的数据结构中提取我们所需的所有数据了。由于 Assimp 的数据结构保持不变,不论导入的是什么种类的文件格式,它都能够将我们从这些不同的文件格式中抽象出来,用同一种方式访问我们需要的数据。

二、源码下载

Assimp 5.0.1: https://github.com/assimp/assimp/releases/tag/v5.0.1

三、编译环境

操作系统:Win10 x64 专业版
编译环境:Visual Studion Enterprise 2019、CMake 3.18.0

四、编译

  1. 打开 cmake-gui.exe

  2. 配置环境变量选择源码路径与输出目录
    在这里插入图片描述

  3. 点击 Configure 配置想要输出 x64 版本 (x86 版本自己选择 win32)
    在这里插入图片描述

  4. 点击 Configure 完成后,再点击 Generate
    在这里插入图片描述

出现以下警告忽略即可,不影响编译
在这里插入图片描述

  1. 点击OpenProject 选择 Release 编译即可
    在这里插入图片描述
    在这里插入图片描述

编译完成后,可在code/Release 目录找到生成的 dll 与 lib

在这里插入图片描述

注:x86版本自己安照上面的教程编译,只需在步骤3 修改下导出版本,如果报找不到 DirectX,自行安装 Dx 即可,Dx 下载链接:https://www.microsoft.com/en-us/download/details.aspx?id=6812,或在导出配置时去掉 ASSIMP_BUILD_ASSIMP_VIEW

五、配置环境

  1. 配置 Include 文件
  • 将导出工程中的 config.h 拷到源码 include/assimp 目录中,或直接拷贝到VS include 目录,否则在引用 assimp 库会报以下错误
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 将源码 include 目录拷贝到 VS2019 安装目录下的 \Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.25.28610\include
    在这里插入图片描述

  1. 配置 .lib 文件
    将编译好的 assimp-vc142-mt.lib 拷贝到 \Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.25.28610\lib目录,注意区分x86 和 x64
    在这里插入图片描述

  2. 配置 .dll 文件
    拷贝 assimp-vc142-mt.dll 到系统目录,这里需要注意下 32 位 系统和 64 位 系统的区别。64 位 系统下 System32 目录是存放 64位 dll 的,SysWOW64 是用来存放 32位 dll 的,目录功能参考 https://zh.wikipedia.org/wiki/WoW64
    (1)64 位 系统:将 64位的 dll 拷贝到 C:\Windows\System32,32位 dll 拷贝到 C:\Windows\SysWOW64
    (2)32 位 系统:只需拷贝 32位 dll 到 C:\Windows\System32 即可

六、测试 DEMO

#include <assimp/Importer.hpp>      // C++ importer interface
#include <assimp/scene.h>           // Output data structure
#include <assimp/postprocess.h>     // Post processing flags
#include <iostream>

#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;
}

斯坦福兔子(bun_zipper.ply文件)下载地址 :http://graphics.stanford.edu/pub/3Dscanrep/bunny.tar.gz

在这里插入图片描述

参考链接:
[1] https://assimp-docs.readthedocs.io/en/latest/usage/use_the_lib.html
[2] https://learnopengl.com/Model-Loading/Assimp

欢迎关注个人公众号,实时推送最新博文!
在这里插入图片描述

### 如何在 Visual Studio 中使用 Assimp #### 安装 Assimp 为了在 Visual Studio 中成功集成和使用 Assimp ,需先下载并配置好此。通常情况下,像 Boost 这样的第三方可能需要编译特定版本的文件(例如 x64 或者 32位),但如果是简单的头文件,则只需解压即可使用[^1]。 对于 Assimp 而言,可以从其官方网站获取预编译二进制文件或是源码自行编译。推荐初学者采用前者以简化设置流程。确保所选版本与目标平台相匹配,比如 Windows 平台下的 VS 版本应对应选择 MSVC 编译器生成的 .lib 和 .dll 文件。 #### 创建新项目 启动 Visual Studio 后新建一个 C++ 控制台应用程序或其他适当类型的工程来作为载体加载模型数据处理逻辑。当创建含有 `requirements.txt` 文件的新项目时,IDE 可能会询问是否要建立虚拟环境;不过这主要针对 Python 开发,在 C++ 场景下可以忽略这部分提示[^2]。 #### 配置项目属性 接下来调整项目的链接器选项以便能够找到 Assimp 提供的相关静态/动态链接以及导入必要的头文件路径: - **C/C++ -> General -> Additional Include Directories**: 添加 Assimp 头文件所在目录; - **Linker -> General -> Additional Library Directories**: 设置为包含 `.lib` 文件的位置; - **Linker -> Input -> Additional Dependencies**: 列举所有需要用到的具体名,如 assimp.lib 等。 完成上述操作之后就可以编写测试代码验证安装情况了。下面给出一段简单示例用于读取 obj 格式的三维物体资源: ```cpp #include <assimp/Importer.hpp> // Provides the basic functionality. #include <assimp/scene.h> // Contains definitions of data structures like aiScene. #include <assimp/postprocess.h> // Post processing flags are defined here. int main() { const char* path = "path_to_your_model.obj"; Assimp::Importer importer; const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace); if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { printf("ERROR::ASSIMP::%s\n", importer.GetErrorString()); return EXIT_FAILURE; } } ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值