25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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