TerragenLoader.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, 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 Implementation of the Terragen importer class */
  35. #ifndef ASSIMP_BUILD_NO_TERRAGEN_IMPORTER
  36. #include "TerragenLoader.h"
  37. #include "StreamReader.h"
  38. #include <assimp/Importer.hpp>
  39. #include <assimp/IOSystem.hpp>
  40. #include <assimp/scene.h>
  41. #include <assimp/DefaultLogger.hpp>
  42. using namespace Assimp;
  43. static const aiImporterDesc desc = {
  44. "Terragen Heightmap Importer",
  45. "",
  46. "",
  47. "http://www.planetside.co.uk/",
  48. aiImporterFlags_SupportBinaryFlavour,
  49. 0,
  50. 0,
  51. 0,
  52. 0,
  53. "ter"
  54. };
  55. // ------------------------------------------------------------------------------------------------
  56. // Constructor to be privately used by Importer
  57. TerragenImporter::TerragenImporter()
  58. : configComputeUVs (false)
  59. {}
  60. // ------------------------------------------------------------------------------------------------
  61. // Destructor, private as well
  62. TerragenImporter::~TerragenImporter()
  63. {}
  64. // ------------------------------------------------------------------------------------------------
  65. // Returns whether the class can handle the format of the given file.
  66. bool TerragenImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  67. {
  68. // check file extension
  69. std::string extension = GetExtension(pFile);
  70. if( extension == "ter")
  71. return true;
  72. if( !extension.length() || checkSig) {
  73. /* If CanRead() is called in order to check whether we
  74. * support a specific file extension in general pIOHandler
  75. * might be NULL and it's our duty to return true here.
  76. */
  77. if (!pIOHandler)return true;
  78. const char* tokens[] = {"terragen"};
  79. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  80. }
  81. return false;
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. // Build a string of all file extensions supported
  85. const aiImporterDesc* TerragenImporter::GetInfo () const
  86. {
  87. return &desc;
  88. }
  89. // ------------------------------------------------------------------------------------------------
  90. // Setup import properties
  91. void TerragenImporter::SetupProperties(const Importer* pImp)
  92. {
  93. // AI_CONFIG_IMPORT_TER_MAKE_UVS
  94. configComputeUVs = ( 0 != pImp->GetPropertyInteger(AI_CONFIG_IMPORT_TER_MAKE_UVS,0) );
  95. }
  96. // ------------------------------------------------------------------------------------------------
  97. // Imports the given file into the given scene structure.
  98. void TerragenImporter::InternReadFile( const std::string& pFile,
  99. aiScene* pScene, IOSystem* pIOHandler)
  100. {
  101. IOStream* file = pIOHandler->Open( pFile, "rb");
  102. // Check whether we can read from the file
  103. if( file == NULL)
  104. throw DeadlyImportError( "Failed to open TERRAGEN TERRAIN file " + pFile + ".");
  105. // Construct a stream reader to read all data in the correct endianness
  106. StreamReaderLE reader(file);
  107. if(reader.GetRemainingSize() < 16)
  108. throw DeadlyImportError( "TER: file is too small" );
  109. // Check for the existence of the two magic strings 'TERRAGEN' and 'TERRAIN '
  110. if (::strncmp((const char*)reader.GetPtr(),AI_TERR_BASE_STRING,8))
  111. throw DeadlyImportError( "TER: Magic string \'TERRAGEN\' not found" );
  112. if (::strncmp((const char*)reader.GetPtr()+8,AI_TERR_TERRAIN_STRING,8))
  113. throw DeadlyImportError( "TER: Magic string \'TERRAIN\' not found" );
  114. unsigned int x = 0,y = 0,mode = 0;
  115. float rad = 6370.f;
  116. (void)rad;
  117. aiNode* root = pScene->mRootNode = new aiNode();
  118. root->mName.Set("<TERRAGEN.TERRAIN>");
  119. // Default scaling is 30
  120. root->mTransformation.a1 = root->mTransformation.b2 = root->mTransformation.c3 = 30.f;
  121. // Now read all chunks until we're finished or an EOF marker is encountered
  122. reader.IncPtr(16);
  123. while (reader.GetRemainingSize() >= 4)
  124. {
  125. const char* head = (const char*)reader.GetPtr();
  126. reader.IncPtr(4);
  127. // EOF, break in every case
  128. if (!::strncmp(head,AI_TERR_EOF_STRING,4))
  129. break;
  130. // Number of x-data points
  131. if (!::strncmp(head,AI_TERR_CHUNK_XPTS,4))
  132. {
  133. x = (uint16_t)reader.GetI2();
  134. }
  135. // Number of y-data points
  136. else if (!::strncmp(head,AI_TERR_CHUNK_YPTS,4))
  137. {
  138. y = (uint16_t)reader.GetI2();
  139. }
  140. // Squared terrains width-1.
  141. else if (!::strncmp(head,AI_TERR_CHUNK_SIZE,4))
  142. {
  143. x = y = (uint16_t)reader.GetI2()+1;
  144. }
  145. // terrain scaling
  146. else if (!::strncmp(head,AI_TERR_CHUNK_SCAL,4))
  147. {
  148. root->mTransformation.a1 = reader.GetF4();
  149. root->mTransformation.b2 = reader.GetF4();
  150. root->mTransformation.c3 = reader.GetF4();
  151. }
  152. // mapping == 1: earth radius
  153. else if (!::strncmp(head,AI_TERR_CHUNK_CRAD,4))
  154. {
  155. rad = reader.GetF4();
  156. }
  157. // mapping mode
  158. else if (!::strncmp(head,AI_TERR_CHUNK_CRVM,4))
  159. {
  160. mode = reader.GetI1();
  161. if (0 != mode)
  162. DefaultLogger::get()->error("TER: Unsupported mapping mode, a flat terrain is returned");
  163. }
  164. // actual terrain data
  165. else if (!::strncmp(head,AI_TERR_CHUNK_ALTW,4))
  166. {
  167. float hscale = (float)reader.GetI2() / 65536;
  168. float bheight = (float)reader.GetI2();
  169. if (!hscale)hscale = 1;
  170. // Ensure we have enough data
  171. if (reader.GetRemainingSize() < x*y*2)
  172. throw DeadlyImportError("TER: ALTW chunk is too small");
  173. if (x <= 1 || y <= 1)
  174. throw DeadlyImportError("TER: Invalid terrain size");
  175. // Allocate the output mesh
  176. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes = 1];
  177. aiMesh* m = pScene->mMeshes[0] = new aiMesh();
  178. // We return quads
  179. aiFace* f = m->mFaces = new aiFace[m->mNumFaces = (x-1)*(y-1)];
  180. aiVector3D* pv = m->mVertices = new aiVector3D[m->mNumVertices = m->mNumFaces*4];
  181. aiVector3D *uv( NULL );
  182. float step_y( 0.0f ), step_x( 0.0f );
  183. if (configComputeUVs) {
  184. uv = m->mTextureCoords[0] = new aiVector3D[m->mNumVertices];
  185. step_y = 1.f/y;
  186. step_x = 1.f/x;
  187. }
  188. const int16_t* data = (const int16_t*)reader.GetPtr();
  189. for (unsigned int yy = 0, t = 0; yy < y-1;++yy) {
  190. for (unsigned int xx = 0; xx < x-1;++xx,++f) {
  191. // make verts
  192. const float fy = (float)yy, fx = (float)xx;
  193. unsigned tmp,tmp2;
  194. *pv++ = aiVector3D(fx,fy, (float)data[(tmp2=x*yy) + xx] * hscale + bheight);
  195. *pv++ = aiVector3D(fx,fy+1, (float)data[(tmp=x*(yy+1)) + xx] * hscale + bheight);
  196. *pv++ = aiVector3D(fx+1,fy+1,(float)data[tmp + xx+1] * hscale + bheight);
  197. *pv++ = aiVector3D(fx+1,fy, (float)data[tmp2 + xx+1] * hscale + bheight);
  198. // also make texture coordinates, if necessary
  199. if (configComputeUVs) {
  200. *uv++ = aiVector3D( step_x*xx, step_y*yy, 0.f );
  201. *uv++ = aiVector3D( step_x*xx, step_y*(yy+1), 0.f );
  202. *uv++ = aiVector3D( step_x*(xx+1), step_y*(yy+1), 0.f );
  203. *uv++ = aiVector3D( step_x*(xx+1), step_y*yy, 0.f );
  204. }
  205. // make indices
  206. f->mIndices = new unsigned int[f->mNumIndices = 4];
  207. for (unsigned int i = 0; i < 4;++i)
  208. f->mIndices[i] = t++;
  209. }
  210. }
  211. // Add the mesh to the root node
  212. root->mMeshes = new unsigned int[root->mNumMeshes = 1];
  213. root->mMeshes[0] = 0;
  214. }
  215. // Get to the next chunk (4 byte aligned)
  216. unsigned dtt;
  217. if ((dtt = reader.GetCurrentPos() & 0x3))
  218. reader.IncPtr(4-dtt);
  219. }
  220. // Check whether we have a mesh now
  221. if (pScene->mNumMeshes != 1)
  222. throw DeadlyImportError("TER: Unable to load terrain");
  223. // Set the AI_SCENE_FLAGS_TERRAIN bit
  224. pScene->mFlags |= AI_SCENE_FLAGS_TERRAIN;
  225. }
  226. #endif // !! ASSIMP_BUILD_NO_TERRAGEN_IMPORTER