TerragenLoader.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 Terragen importer class */
  35. #include "AssimpPCH.h"
  36. #ifndef AI_BUILD_NO_TERRAGEN_IMPORTER
  37. #include "TerragenLoader.h"
  38. using namespace Assimp;
  39. // ------------------------------------------------------------------------------------------------
  40. // Constructor to be privately used by Importer
  41. TerragenImporter::TerragenImporter()
  42. {}
  43. // ------------------------------------------------------------------------------------------------
  44. // Destructor, private as well
  45. TerragenImporter::~TerragenImporter()
  46. {}
  47. // ------------------------------------------------------------------------------------------------
  48. // Returns whether the class can handle the format of the given file.
  49. bool TerragenImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  50. {
  51. // simple check of file extension is enough for the moment
  52. std::string::size_type pos = pFile.find_last_of('.');
  53. // no file extension - can't read
  54. if( pos == std::string::npos)return false;
  55. std::string extension = pFile.substr( pos);
  56. for( std::string::iterator it = extension.begin(); it != extension.end(); ++it)
  57. *it = tolower( *it);
  58. return extension == ".ter";
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. // Build a string of all file extensions supported
  62. void TerragenImporter::GetExtensionList(std::string& append)
  63. {
  64. append.append("*.ter;");
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Imports the given file into the given scene structure.
  68. void TerragenImporter::InternReadFile( const std::string& pFile,
  69. aiScene* pScene, IOSystem* pIOHandler)
  70. {
  71. IOStream* file = pIOHandler->Open( pFile, "rb");
  72. // Check whether we can read from the file
  73. if( file == NULL)
  74. throw new ImportErrorException( "Failed to open TERRAGEN TERRAIN file " + pFile + ".");
  75. // Construct a stream reader to read all data in the correct endianess
  76. StreamReaderLE reader(file);
  77. if(reader.GetRemainingSize() < 16)
  78. throw new ImportErrorException( "TER: file is too small" );
  79. // Check for the existence of the two magic strings 'TERRAGEN' and 'TERRAIN '
  80. if (::strncmp((const char*)reader.GetPtr(),AI_TERR_BASE_STRING,8))
  81. throw new ImportErrorException( "TER: Magic string \'TERRAGEN\' not found" );
  82. if (::strncmp((const char*)reader.GetPtr()+8,AI_TERR_TERRAIN_STRING,8))
  83. throw new ImportErrorException( "TER: Magic string \'TERRAIN\' not found" );
  84. unsigned int x = 0,y = 0,mode = 0;
  85. float rad = 6370.f;
  86. aiNode* root = pScene->mRootNode = new aiNode();
  87. root->mName.Set("<TERRAGEN.TERRAIN>");
  88. // Default scaling is 30
  89. root->mTransformation.a1 = root->mTransformation.b2 = root->mTransformation.c3 = 30.f;
  90. // Now read all chunks until we're finished or an EOF marker is encountered
  91. reader.IncPtr(16);
  92. while (reader.GetRemainingSize() >= 4)
  93. {
  94. const char* head = (const char*)reader.GetPtr();
  95. reader.IncPtr(4);
  96. // EOF, break in every case
  97. if (!::strncmp(head,AI_TERR_EOF_STRING,4))
  98. break;
  99. // Number of x-data points
  100. if (!::strncmp(head,AI_TERR_CHUNK_XPTS,4))
  101. {
  102. x = (uint16_t)reader.GetI2();
  103. }
  104. // Number of y-data points
  105. else if (!::strncmp(head,AI_TERR_CHUNK_YPTS,4))
  106. {
  107. y = (uint16_t)reader.GetI2();
  108. }
  109. // Squared terrains width-1.
  110. else if (!::strncmp(head,AI_TERR_CHUNK_SIZE,4))
  111. {
  112. x = y = (uint16_t)reader.GetI2()+1;
  113. }
  114. // terrain scaling
  115. else if (!::strncmp(head,AI_TERR_CHUNK_SCAL,4))
  116. {
  117. root->mTransformation.a1 = reader.GetF4();
  118. root->mTransformation.b2 = reader.GetF4();
  119. root->mTransformation.c3 = reader.GetF4();
  120. }
  121. // mapping == 1: earth radius
  122. else if (!::strncmp(head,AI_TERR_CHUNK_CRAD,4))
  123. {
  124. rad = reader.GetF4();
  125. }
  126. // mapping mode
  127. else if (!::strncmp(head,AI_TERR_CHUNK_CRVM,4))
  128. {
  129. mode = reader.GetI1();
  130. if (0 != mode)
  131. DefaultLogger::get()->error("TER: Unsupported mapping mode, a flat terrain is returned");
  132. }
  133. // actual terrain data
  134. else if (!::strncmp(head,AI_TERR_CHUNK_ALTW,4))
  135. {
  136. float hscale = (float)reader.GetI2() / 65536;
  137. float bheight = (float)reader.GetI2();
  138. if (!hscale)hscale = 1;
  139. // Ensure we have enough data
  140. if (reader.GetRemainingSize() < x*y*2)
  141. throw new ImportErrorException("TER: ALTW chunk is too small");
  142. if (x <= 1 || y <= 1)
  143. throw new ImportErrorException("TER: Invalid terrain size");
  144. // Allocate the output mesh
  145. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes = 1];
  146. aiMesh* m = pScene->mMeshes[0] = new aiMesh();
  147. // We return quads
  148. aiFace* f = m->mFaces = new aiFace[m->mNumFaces = (x-1)*(y-1)];
  149. aiVector3D* pv = m->mVertices = new aiVector3D[m->mNumVertices = m->mNumFaces*4];
  150. const int16_t* data = (const int16_t*)reader.GetPtr();
  151. for (unsigned int yy = 0, t = 0; yy < y-1;++yy) {
  152. for (unsigned int xx = 0; xx < x-1;++xx,++f) {
  153. // make verts
  154. const float fy = (float)yy, fx = (float)xx;
  155. register unsigned tmp,tmp2;
  156. *pv++ = aiVector3D(fx,fy, (float)data[(tmp2=x*yy) + xx] * hscale + bheight);
  157. *pv++ = aiVector3D(fx,fy+1, (float)data[(tmp=x*(yy+1)) + xx] * hscale + bheight);
  158. *pv++ = aiVector3D(fx+1,fy+1,(float)data[tmp + xx+1] * hscale + bheight);
  159. *pv++ = aiVector3D(fx+1,fy, (float)data[tmp2 + xx+1] * hscale + bheight);
  160. // make indices
  161. f->mIndices = new unsigned int[f->mNumIndices = 4];
  162. for (unsigned int i = 0; i < 4;++i)
  163. f->mIndices[i] = t++;
  164. }
  165. }
  166. // Add the mesh to the root node
  167. root->mMeshes = new unsigned int[root->mNumMeshes = 1];
  168. root->mMeshes[0] = 0;
  169. }
  170. // Get to the next chunk (4 byte aligned)
  171. unsigned dtt;
  172. if ((dtt = reader.GetCurrentPos() & 0x3))
  173. reader.IncPtr(4-dtt);
  174. }
  175. // Check whether we have a mesh now
  176. if (pScene->mNumMeshes != 1)
  177. throw new ImportErrorException("TER: Unable to load terrain");
  178. // Set the AI_SCENE_FLAGS_TERRAIN bit
  179. pScene->mFlags |= AI_SCENE_FLAGS_TERRAIN;
  180. }
  181. #endif // !! AI_BUILD_NO_TERRAGEN_IMPORTER