RawLoader.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file RawLoader.cpp
  35. * @brief Implementation of the RAW importer class
  36. */
  37. #include "AssimpPCH.h"
  38. #ifndef ASSIMP_BUILD_NO_RAW_IMPORTER
  39. // internal headers
  40. #include "RawLoader.h"
  41. #include "ParsingUtils.h"
  42. #include "fast_atof.h"
  43. using namespace Assimp;
  44. // ------------------------------------------------------------------------------------------------
  45. // Constructor to be privately used by Importer
  46. RAWImporter::RAWImporter()
  47. {}
  48. // ------------------------------------------------------------------------------------------------
  49. // Destructor, private as well
  50. RAWImporter::~RAWImporter()
  51. {}
  52. // ------------------------------------------------------------------------------------------------
  53. // Returns whether the class can handle the format of the given file.
  54. bool RAWImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  55. {
  56. return SimpleExtensionCheck(pFile,"raw");
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. // Get the list of all supported file extensions
  60. void RAWImporter::GetExtensionList(std::string& append)
  61. {
  62. append.append("*.raw");
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. // Imports the given file into the given scene structure.
  66. void RAWImporter::InternReadFile( const std::string& pFile,
  67. aiScene* pScene, IOSystem* pIOHandler)
  68. {
  69. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  70. // Check whether we can read from the file
  71. if( file.get() == NULL)
  72. throw new ImportErrorException( "Failed to open RAW file " + pFile + ".");
  73. unsigned int fileSize = (unsigned int)file->FileSize();
  74. // allocate storage and copy the contents of the file to a memory buffer
  75. // (terminate it with zero)
  76. std::vector<char> mBuffer2(fileSize+1);
  77. file->Read(&mBuffer2[0], 1, fileSize);
  78. mBuffer2[fileSize] = '\0';
  79. const char* buffer = &mBuffer2[0];
  80. // list of groups loaded from the file
  81. std::vector< GroupInformation > outGroups(1,GroupInformation("<default>"));
  82. std::vector< GroupInformation >::iterator curGroup = outGroups.begin();
  83. // now read all lines
  84. char line[4096];
  85. while (GetNextLine(buffer,line))
  86. {
  87. // if the line starts with a non-numeric identifier, it marks
  88. // the beginning of a new group
  89. const char* sz = line;SkipSpaces(&sz);
  90. if (IsLineEnd(*sz))continue;
  91. if (!IsNumeric(*sz))
  92. {
  93. const char* sz2 = sz;
  94. while (!IsSpaceOrNewLine(*sz2))++sz2;
  95. const unsigned int length = (unsigned int)(sz2-sz);
  96. // find an existing group with this name
  97. for (std::vector< GroupInformation >::iterator it = outGroups.begin(), end = outGroups.end();
  98. it != end;++it)
  99. {
  100. if (length == (*it).name.length() && !::strcmp(sz,(*it).name.c_str()))
  101. {
  102. curGroup = it;sz2 = NULL;
  103. break;
  104. }
  105. }
  106. if (sz2)
  107. {
  108. outGroups.push_back(GroupInformation(std::string(sz,length)));
  109. curGroup = outGroups.end()-1;
  110. }
  111. }
  112. else
  113. {
  114. // there can be maximally 12 floats plus an extra texture file name
  115. float data[12];
  116. unsigned int num;
  117. for (num = 0; num < 12;++num)
  118. {
  119. if(!SkipSpaces(&sz) || !IsNumeric(*sz))break;
  120. sz = fast_atof_move(sz,data[num]);
  121. }
  122. if (num != 12 && num != 9)
  123. {
  124. DefaultLogger::get()->error("A line may have either 9 or 12 floats and an optional texture");
  125. continue;
  126. }
  127. MeshInformation* output = NULL;
  128. const char* sz2 = sz;
  129. unsigned int length;
  130. if (!IsLineEnd(*sz))
  131. {
  132. while (!IsSpaceOrNewLine(*sz2))++sz2;
  133. length = (unsigned int)(sz2-sz);
  134. }
  135. else if (9 == num)
  136. {
  137. sz = "%default%";
  138. length = 9;
  139. }
  140. else
  141. {
  142. sz = "";
  143. length = 0;
  144. }
  145. // search in the list of meshes whether we have one with this texture
  146. for (std::vector< MeshInformation >::iterator it = (*curGroup).meshes.begin(),
  147. end = (*curGroup).meshes.end(); it != end; ++it)
  148. {
  149. if (length == (*it).name.length() && (length ? !::strcmp(sz,(*it).name.c_str()) : true))
  150. {
  151. output = &(*it);
  152. break;
  153. }
  154. }
  155. // if we don't have the mesh, create it
  156. if (!output)
  157. {
  158. (*curGroup).meshes.push_back(MeshInformation(std::string(sz,length)));
  159. output = &((*curGroup).meshes.back());
  160. }
  161. if (12 == num)
  162. {
  163. aiColor4D v(data[0],data[1],data[2],1.0f);
  164. output->colors.push_back(v);
  165. output->colors.push_back(v);
  166. output->colors.push_back(v);
  167. output->vertices.push_back(aiVector3D(data[3],data[4],data[5]));
  168. output->vertices.push_back(aiVector3D(data[6],data[7],data[8]));
  169. output->vertices.push_back(aiVector3D(data[9],data[10],data[11]));
  170. }
  171. else
  172. {
  173. output->vertices.push_back(aiVector3D(data[0],data[1],data[2]));
  174. output->vertices.push_back(aiVector3D(data[3],data[4],data[5]));
  175. output->vertices.push_back(aiVector3D(data[6],data[7],data[8]));
  176. }
  177. }
  178. }
  179. pScene->mRootNode = new aiNode();
  180. pScene->mRootNode->mName.Set("<RawRoot>");
  181. // count the number of valid groups
  182. // (meshes can't be empty)
  183. for (std::vector< GroupInformation >::iterator it = outGroups.begin(), end = outGroups.end();
  184. it != end;++it)
  185. {
  186. if (!(*it).meshes.empty())
  187. {
  188. ++pScene->mRootNode->mNumChildren;
  189. pScene->mNumMeshes += (unsigned int)(*it).meshes.size();
  190. }
  191. }
  192. if (!pScene->mNumMeshes)
  193. {
  194. throw new ImportErrorException("RAW: No meshes loaded. The file seems to be corrupt or empty.");
  195. }
  196. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  197. aiNode** cc;
  198. if (1 == pScene->mRootNode->mNumChildren)
  199. {
  200. cc = &pScene->mRootNode;
  201. pScene->mRootNode->mNumChildren = 0;
  202. }
  203. else cc = pScene->mRootNode->mChildren = new aiNode*[pScene->mRootNode->mNumChildren];
  204. pScene->mNumMaterials = pScene->mNumMeshes;
  205. aiMaterial** mats = pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  206. unsigned int meshIdx = 0;
  207. for (std::vector< GroupInformation >::iterator it = outGroups.begin(), end = outGroups.end();
  208. it != end;++it)
  209. {
  210. if ((*it).meshes.empty())continue;
  211. aiNode* node;
  212. if (pScene->mRootNode->mNumChildren)
  213. {
  214. node = *cc = new aiNode();
  215. node->mParent = pScene->mRootNode;
  216. }
  217. else node = *cc;++cc;
  218. node->mName.Set((*it).name);
  219. // add all meshes
  220. node->mNumMeshes = (unsigned int)(*it).meshes.size();
  221. unsigned int* pi = node->mMeshes = new unsigned int[ node->mNumMeshes ];
  222. for (std::vector< MeshInformation >::iterator it2 = (*it).meshes.begin(),
  223. end2 = (*it).meshes.end(); it2 != end2; ++it2)
  224. {
  225. ai_assert(!(*it2).vertices.empty());
  226. // allocate the mesh
  227. *pi++ = meshIdx;
  228. aiMesh* mesh = pScene->mMeshes[meshIdx] = new aiMesh();
  229. mesh->mMaterialIndex = meshIdx++;
  230. mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  231. // allocate storage for the vertex components and copy them
  232. mesh->mNumVertices = (unsigned int)(*it2).vertices.size();
  233. mesh->mVertices = new aiVector3D[ mesh->mNumVertices ];
  234. ::memcpy(mesh->mVertices,&(*it2).vertices[0],sizeof(aiVector3D)*mesh->mNumVertices);
  235. if ((*it2).colors.size())
  236. {
  237. ai_assert((*it2).colors.size() == mesh->mNumVertices);
  238. mesh->mColors[0] = new aiColor4D[ mesh->mNumVertices ];
  239. ::memcpy(mesh->mColors[0],&(*it2).colors[0],sizeof(aiColor4D)*mesh->mNumVertices);
  240. }
  241. // generate triangles
  242. ai_assert(0 == mesh->mNumVertices % 3);
  243. aiFace* fc = mesh->mFaces = new aiFace[ mesh->mNumFaces = mesh->mNumVertices/3 ];
  244. aiFace* const fcEnd = fc + mesh->mNumFaces;
  245. unsigned int n = 0;
  246. while (fc != fcEnd)
  247. {
  248. aiFace& f = *fc++;
  249. f.mIndices = new unsigned int[f.mNumIndices = 3];
  250. for (unsigned int m = 0; m < 3;++m)
  251. f.mIndices[m] = n++;
  252. }
  253. // generate a material for the mesh
  254. MaterialHelper* mat = new MaterialHelper();
  255. aiColor4D clr(1.0f,1.0f,1.0f,1.0f);
  256. if ("%default%" == (*it2).name) // a gray default material
  257. {
  258. clr.r = clr.g = clr.b = 0.6f;
  259. }
  260. else if ((*it2).name.length() > 0) // a texture
  261. {
  262. aiString s;
  263. s.Set((*it2).name);
  264. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  265. }
  266. mat->AddProperty<aiColor4D>(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
  267. *mats++ = mat;
  268. }
  269. }
  270. }
  271. #endif // !! ASSIMP_BUILD_NO_RAW_IMPORTER