简体   繁体   English

如何将文件(3D model 文件)导入 c++ 中的缓冲区

[英]How to import a file ( 3D model file ) into a buffer in c++

** more specific question ** How to read a 3d file (the type of data is not known) into memory to feed the following method. ** 更具体的问题 ** 如何将 3d 文件(数据类型未知)读入 memory 以提供以下方法。

i am simply trying to import a file (a 3d model.stl file) into memory and then pass it to the following method.我只是想将一个文件(一个 3d model.stl 文件)导入 ZCD69B4957F06,然后将其传递给以下方法。

 const aiScene* ReadFileFromMemory(
        const void* pBuffer,
        size_t pLength,
        unsigned int pFlags,
        const char* pHint = "");

but i don't know how to provide the following 2 arguments for the method mentioned above:但我不知道如何为上述方法提供以下 2 个 arguments:

const void* pBuffer 
size_t pLength

Can someone provide me with a block of code for this?有人可以为此提供一段代码吗? How can i use ifstream to read a file from memory (like "model.stl"), and then put it inside a buffer and pass it to the mentioned method如何使用 ifstream 从 memory 读取文件(如“model.stl”),然后将其放入缓冲区并将其传递给上述方法

Tnx肿瘤坏死因子

ps.附言。 ReadFileFromMemory requires a pointer to the data. ReadFileFromMemory 需要一个指向数据的指针。 The data can be a char[], std::string, std::vector<uint8_t> or similar.数据可以是 char[]、std::string、std::vector<uint8_t> 或类似的。 Anything that holds raw data in contiguous memory and can provide a pointer to it任何在连续 memory 中保存原始数据并可以提供指向它的指针的东西

How to achieve this?如何做到这一点?

===================== UPDATE ====================== ===================== 更新 ======================

Solution解决方案

    string readFile(const string& fileName)
 {
        ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);

        ifstream::pos_type fileSize = ifs.tellg();
        ifs.seekg(0, ios::beg);

        vector<char> bytes(fileSize);
        ifs.read(bytes.data(), fileSize);

        return string(bytes.data(), fileSize);
    }

and then take the buffer and it's length as the following:然后获取缓冲区及其长度如下:

std::string my_file = readFile("models/model_file.ext");
   const char* pbuffer = my_file.data();
    size_t  plenght =  my_file.length();

To load a file to memory, you can use this,要将文件加载到 memory,您可以使用这个,

std::ifstream file(file_path, std::ios::binary | std::ios::ate);

if(!file.is_open()) { /* Bail out if the file couldn't be loaded. */ }

size_t fileSize = file.tellg();
file.seekg(0);

char* pBuffer = new char[fileSize];
file.read(pBuffer, fileSize);

file.close();

// After everything is done.
delete[] pBuffer;

And then you can pass the pBuffer and fileSize to pBuffer and length parameters respectively.然后您可以将pBufferfileSize分别传递给pBufferlength参数。

Assimp also provides an easy ReadFile function in the Importer class which does all that for you. Assimp 还在Importer class 中提供了一个简单的ReadFile function,它可以为您完成所有这些工作。 You just need to pass the asset file's path and the format flags to it.您只需将资产文件的路径和格式标志传递给它。

as @MikeCAT said, pbuffer is pointer to the memory, where your file is stored and pLength is the size of that buffer in bytes.正如@MikeCAT 所说,pbuffer 是指向 memory 的指针,您的文件存储在其中,pLength 是该缓冲区的大小(以字节为单位)。 Check out function specs here: http://assimp.sourceforge.net/lib_html/class_assimp_1_1_importer.html#a9b3c5e8b1042702f449e84a95b3324f6在此处查看 function 规格: http://assimp.sourceforge.net/lib_html/class_assimp_1_1_importer.html#a9b3c5e8b1042702f449e84a95b3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM