RawLoader.cpp 10 KB

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