Using the assimp library you want to import the original 3D model and export the 3D model in a new format
found the explanation of the official documentation unreadable. It is probably not for beginners.
#pragma once
#include <assimp/Importer.hpp> // C++ importer interface
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags
#include <iostream>
#include <string.h>
using namespace std;
#define print(x) cout << x << endl
#define printV2(x) cout <<"("<<x[0]<<", "<<x[1]<<")"
#define printV3(x) cout <<"("<<x[0]<<", "<<x[1]<<", "<<x[2]<<")"
#define printColor(x) cout <<"("<<x.r<<", "<<x.g<<", "<<x.b<<")"
#define len(x) sizeof(x)/sizeof(x[0])
#define type(x) typeid(x).name()
#include <assimp/Importer.hpp> // C++ importer interface
#include <assimp/scene.h> // Output data structure
#include <assimp/postprocess.h> // Post processing flags
#include <assimp/IOStream.hpp>
// 这部分主要是导入3D模型文件
bool DoTheImportThing(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 (nullptr != scene) {
print(importer.GetErrorString());
print("导入模型失败");
return false;
}
// Now we can access the file's contents.
print("成功获取scene 导入模型成功");
// We're done. Everything will be cleaned up by the importer destructor
return true;
}
// 这部分主要是构造IO逻辑
// My own implementation of IOStream
class MyIOStream : public Assimp::IOStream {
friend class MyIOSystem;
protected:
// Constructor protected for private usage by MyIOSystem
MyIOStream();
public:
~MyIOStream();
size_t Read(void* pvBuffer, size_t pSize, size_t pCount) { ... }
size_t Write(const void* pvBuffer, size_t pSize, size_t pCount) { ... }
aiReturn Seek(size_t pOffset, aiOrigin pOrigin) { ... }
size_t Tell() const { ... }
size_t FileSize() const { ... }
void Flush() { ... }
};
// Fisher Price - My First Filesystem
class MyIOSystem : public Assimp::IOSystem {
MyIOSystem() { ... }
~MyIOSystem() { ... }
// Check whether a specific file exists
bool Exists(const std::string& pFile) const {
..
}
// Get the path delimiter character we'd like to see
char GetOsSeparator() const {
return '/';
}
// ... and finally a method to open a custom stream
IOStream* Open(const std::string& pFile, const std::string& pMode) {
return new MyIOStream(...);
}
void Close(IOStream* pFile) { delete pFile; }
};
// 现在您的 IO 系统已实现,通过调用将其实例提供给 Importer 对象
Assimp::Importer::SetIOHandler().
// 这部分主要是导出3D模型
bool exporterTest() override {
::Assimp::Importer importer;
::Assimp::Exporter exporter;
const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/OBJ/spider.obj", aiProcess_ValidateDataStructure);
exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/OBJ/spider_out.obj");
return true;
}
>
may contain a lot of pseudo-code. As a beginner, I can't understand it at all. There are many syntax problems and unknown problems.
I can only solve it one by one, but some of them are not syntax problems, some of them are third-party library Settings that can't be solved in a short time.
I just want to use the assimp library to convert max 3D models to fbx 3D models
0 Answer
No answer yet
这家伙很懒,什么都没留下...