TerragenLoader.cpp 9.4 KB

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