-2

** more specific question ** How to read a 3d file (the type of data is not known) into memory to feed the following method .

i am simply trying to import a file (a 3d model.stl file) into memory and then pass it to the following method .

 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 :

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

Tnx

ps . ReadFileFromMemory requires a pointer to the data. The data can be a char[], std::string, std::vector<uint8_t> or similar. Anything that holds raw data in contiguous memory and can provide a pointer to it

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();

2 Answers2

1

To load a file to memory, you can use this,

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.

Assimp also provides an easy ReadFile function in the Importer class which does all that for you. You just need to pass the asset file's path and the format flags to it.

D-RAJ
  • 2,907
  • 1
  • 4
  • 20
  • thanks . if you check the assimp importer lib , it has 2 methods , ReadFile , ReadFileFromMemory . i need to use the second one , since i want to feed the importer with stream of data at the end. plus , do you think it is possible to do this /? – Pooya Sobhanipour Apr 15 '21 at 14:21
  • @PooyaSobhanipour Woops sorry my bad, and Yes, absolutely! – D-RAJ Apr 15 '21 at 14:24
  • plus , file.read() can not accept the unsigned char * as an input . would you please check it . tnx – Pooya Sobhanipour Apr 15 '21 at 14:25
0

as @MikeCAT said, pbuffer is pointer to the memory, where your file is stored and pLength is the size of that buffer in bytes. Check out function specs here: http://assimp.sourceforge.net/lib_html/class_assimp_1_1_importer.html#a9b3c5e8b1042702f449e84a95b3324f6