RawLoader.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 Implementation of the RAW importer class */
  35. #include "AssimpPCH.h"
  36. // internal headers
  37. #include "RawLoader.h"
  38. #include "ParsingUtils.h"
  39. #include "fast_atof.h"
  40. using namespace Assimp;
  41. // ------------------------------------------------------------------------------------------------
  42. // Constructor to be privately used by Importer
  43. RAWImporter::RAWImporter()
  44. {
  45. }
  46. // ------------------------------------------------------------------------------------------------
  47. // Destructor, private as well
  48. RAWImporter::~RAWImporter()
  49. {
  50. }
  51. // ------------------------------------------------------------------------------------------------
  52. // Returns whether the class can handle the format of the given file.
  53. bool RAWImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  54. {
  55. // simple check of file extension is enough for the moment
  56. std::string::size_type pos = pFile.find_last_of('.');
  57. // no file extension - can't read
  58. if( pos == std::string::npos)return false;
  59. std::string extension = pFile.substr( pos);
  60. if (extension.length() < 4)return false;
  61. if (extension[0] != '.')return false;
  62. return !(extension.length() != 4 || extension[0] != '.' ||
  63. extension[1] != 'r' && extension[1] != 'R' ||
  64. extension[2] != 'a' && extension[2] != 'A' ||
  65. extension[3] != 'w' && extension[3] != 'W');
  66. }
  67. // ------------------------------------------------------------------------------------------------
  68. // Imports the given file into the given scene structure.
  69. void RAWImporter::InternReadFile( const std::string& pFile,
  70. aiScene* pScene, IOSystem* pIOHandler)
  71. {
  72. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  73. // Check whether we can read from the file
  74. if( file.get() == NULL)
  75. throw new ImportErrorException( "Failed to open RAW file " + pFile + ".");
  76. unsigned int fileSize = (unsigned int)file->FileSize();
  77. // allocate storage and copy the contents of the file to a memory buffer
  78. // (terminate it with zero)
  79. std::vector<char> mBuffer2(fileSize+1);
  80. file->Read(&mBuffer2[0], 1, fileSize);
  81. mBuffer2[fileSize] = '\0';
  82. const char* buffer = &mBuffer2[0];
  83. // list of groups loaded from the file
  84. std::vector< GroupInformation > outGroups(1,GroupInformation("<default>"));
  85. std::vector< GroupInformation >::iterator curGroup = outGroups.begin();
  86. // now read all lines
  87. char line[4096];
  88. while (GetNextLine(buffer,line))
  89. {
  90. // if the line starts with a non-numeric identifier, it marks
  91. // the beginning of a new group
  92. const char* sz = line;SkipSpaces(&sz);
  93. if (IsLineEnd(*sz))continue;
  94. if (!IsNumeric(*sz))
  95. {
  96. const char* sz2 = sz;
  97. while (!IsSpaceOrNewLine(*sz2))++sz2;
  98. const unsigned int length = (unsigned int)(sz2-sz);
  99. // find an existing group with this name
  100. for (std::vector< GroupInformation >::iterator it = outGroups.begin(), end = outGroups.end();
  101. it != end;++it)
  102. {
  103. if (length == (*it).name.length() && !::strcmp(sz,(*it).name.c_str()))
  104. {
  105. curGroup = it;sz2 = NULL;
  106. break;
  107. }
  108. }
  109. if (sz2)
  110. {
  111. outGroups.push_back(GroupInformation(std::string(sz,length)));
  112. curGroup = outGroups.end()-1;
  113. }
  114. }
  115. else
  116. {
  117. // there can be maximally 12 floats plus an extra texture file name
  118. float data[12];
  119. unsigned int num;
  120. for (num = 0; num < 12;++num)
  121. {
  122. if(!SkipSpaces(&sz) || !IsNumeric(*sz))break;
  123. sz = fast_atof_move(sz,data[num]);
  124. }
  125. if (num != 12 && num != 9)
  126. {
  127. DefaultLogger::get()->error("A line may have either 9 or 12 floats and an optional texture");
  128. continue;
  129. }
  130. MeshInformation* output = NULL;
  131. const char* sz2 = sz;
  132. unsigned int length;
  133. if (!IsLineEnd(*sz))
  134. {
  135. while (!IsSpaceOrNewLine(*sz2))++sz2;
  136. length = (unsigned int)(sz2-sz);
  137. }
  138. else if (9 == num)
  139. {
  140. sz = "%default%";
  141. length = 9;
  142. }
  143. else
  144. {
  145. sz = "";
  146. length = 0;
  147. }
  148. // search in the list of meshes whether we have one with this texture
  149. for (std::vector< MeshInformation >::iterator it = (*curGroup).meshes.begin(),
  150. end = (*curGroup).meshes.end(); it != end; ++it)
  151. {
  152. if (length == (*it).name.length() && (length ? !::strcmp(sz,(*it).name.c_str()) : true))
  153. {
  154. output = &(*it);
  155. break;
  156. }
  157. }
  158. // if we don't have the mesh, create it
  159. if (!output)
  160. {
  161. (*curGroup).meshes.push_back(MeshInformation(std::string(sz,length)));
  162. output = &((*curGroup).meshes.back());
  163. }
  164. if (12 == num)
  165. {
  166. aiColor4D v(data[0],data[1],data[2],1.0f);
  167. output->colors.push_back(v);
  168. output->colors.push_back(v);
  169. output->colors.push_back(v);
  170. output->vertices.push_back(aiVector3D(data[3],data[4],data[5]));
  171. output->vertices.push_back(aiVector3D(data[6],data[7],data[8]));
  172. output->vertices.push_back(aiVector3D(data[9],data[10],data[11]));
  173. }
  174. else
  175. {
  176. output->vertices.push_back(aiVector3D(data[0],data[1],data[2]));
  177. output->vertices.push_back(aiVector3D(data[3],data[4],data[5]));
  178. output->vertices.push_back(aiVector3D(data[6],data[7],data[8]));
  179. }
  180. }
  181. }
  182. pScene->mRootNode = new aiNode();
  183. pScene->mRootNode->mName.Set("<RawRoot>");
  184. // count the number of valid groups
  185. // (meshes can't be empty)
  186. for (std::vector< GroupInformation >::iterator it = outGroups.begin(), end = outGroups.end();
  187. it != end;++it)
  188. {
  189. if (!(*it).meshes.empty())
  190. {
  191. ++pScene->mRootNode->mNumChildren;
  192. pScene->mNumMeshes += (unsigned int)(*it).meshes.size();
  193. }
  194. }
  195. if (!pScene->mNumMeshes)
  196. {
  197. throw new ImportErrorException("RAW: No meshes loaded. The file seems to be corrupt or empty.");
  198. }
  199. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  200. aiNode** cc;
  201. if (1 == pScene->mRootNode->mNumChildren)
  202. {
  203. cc = &pScene->mRootNode;
  204. pScene->mRootNode->mNumChildren = 0;
  205. }
  206. else cc = pScene->mRootNode->mChildren = new aiNode*[pScene->mRootNode->mNumChildren];
  207. pScene->mNumMaterials = pScene->mNumMeshes;
  208. aiMaterial** mats = pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  209. unsigned int meshIdx = 0;
  210. for (std::vector< GroupInformation >::iterator it = outGroups.begin(), end = outGroups.end();
  211. it != end;++it)
  212. {
  213. if ((*it).meshes.empty())continue;
  214. aiNode* node;
  215. if (pScene->mRootNode->mNumChildren)
  216. {
  217. node = *cc = new aiNode();
  218. node->mParent = pScene->mRootNode;
  219. }
  220. else node = *cc;++cc;
  221. node->mName.Set((*it).name);
  222. // add all meshes
  223. node->mNumMeshes = (unsigned int)(*it).meshes.size();
  224. unsigned int* pi = node->mMeshes = new unsigned int[ node->mNumMeshes ];
  225. for (std::vector< MeshInformation >::iterator it2 = (*it).meshes.begin(),
  226. end2 = (*it).meshes.end(); it2 != end2; ++it2)
  227. {
  228. ai_assert(!(*it2).vertices.empty());
  229. // allocate the mesh
  230. *pi++ = meshIdx;
  231. aiMesh* mesh = pScene->mMeshes[meshIdx] = new aiMesh();
  232. mesh->mMaterialIndex = meshIdx++;
  233. mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  234. // allocate storage for the vertex components and copy them
  235. mesh->mNumVertices = (unsigned int)(*it2).vertices.size();
  236. mesh->mVertices = new aiVector3D[ mesh->mNumVertices ];
  237. ::memcpy(mesh->mVertices,&(*it2).vertices[0],sizeof(aiVector3D)*mesh->mNumVertices);
  238. if ((*it2).colors.size())
  239. {
  240. ai_assert((*it2).colors.size() == mesh->mNumVertices);
  241. mesh->mColors[0] = new aiColor4D[ mesh->mNumVertices ];
  242. ::memcpy(mesh->mColors[0],&(*it2).colors[0],sizeof(aiColor4D)*mesh->mNumVertices);
  243. }
  244. // generate triangles
  245. ai_assert(0 == mesh->mNumVertices % 3);
  246. aiFace* fc = mesh->mFaces = new aiFace[ mesh->mNumFaces = mesh->mNumVertices/3 ];
  247. aiFace* const fcEnd = fc + mesh->mNumFaces;
  248. unsigned int n = 0;
  249. while (fc != fcEnd)
  250. {
  251. aiFace& f = *fc++;
  252. f.mIndices = new unsigned int[f.mNumIndices = 3];
  253. for (unsigned int m = 0; m < 3;++m)
  254. f.mIndices[m] = n++;
  255. }
  256. // generate a material for the mesh
  257. MaterialHelper* mat = new MaterialHelper();
  258. aiColor4D clr(1.0f,1.0f,1.0f,1.0f);
  259. if ("%default%" == (*it2).name) // a gray default material
  260. {
  261. clr.r = clr.g = clr.b = 0.6f;
  262. }
  263. else if ((*it2).name.length() > 0) // a texture
  264. {
  265. aiString s;
  266. s.Set((*it2).name);
  267. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  268. }
  269. mat->AddProperty<aiColor4D>(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
  270. *mats++ = mat;
  271. }
  272. }
  273. }