nuclear@0: /* nuclear@0: --------------------------------------------------------------------------- nuclear@0: Open Asset Import Library (assimp) nuclear@0: --------------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the following nuclear@0: conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: --------------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: /** @file Implementation of the Terragen importer class */ nuclear@0: nuclear@0: #include "AssimpPCH.h" nuclear@0: nuclear@0: #ifndef ASSIMP_BUILD_NO_TERRAGEN_IMPORTER nuclear@0: #include "TerragenLoader.h" nuclear@0: nuclear@0: using namespace Assimp; nuclear@0: nuclear@0: static const aiImporterDesc desc = { nuclear@0: "Terragen Heightmap Importer", nuclear@0: "", nuclear@0: "", nuclear@0: "http://www.planetside.co.uk/", nuclear@0: aiImporterFlags_SupportBinaryFlavour, nuclear@0: 0, nuclear@0: 0, nuclear@0: 0, nuclear@0: 0, nuclear@0: "ter" nuclear@0: }; nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Constructor to be privately used by Importer nuclear@0: TerragenImporter::TerragenImporter() nuclear@0: : configComputeUVs (false) nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Destructor, private as well nuclear@0: TerragenImporter::~TerragenImporter() nuclear@0: {} nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Returns whether the class can handle the format of the given file. nuclear@0: bool TerragenImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const nuclear@0: { nuclear@0: // check file extension nuclear@0: std::string extension = GetExtension(pFile); nuclear@0: nuclear@0: if( extension == "ter") nuclear@0: return true; nuclear@0: nuclear@0: if( !extension.length() || checkSig) { nuclear@0: /* If CanRead() is called in order to check whether we nuclear@0: * support a specific file extension in general pIOHandler nuclear@0: * might be NULL and it's our duty to return true here. nuclear@0: */ nuclear@0: if (!pIOHandler)return true; nuclear@0: const char* tokens[] = {"terragen"}; nuclear@0: return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1); nuclear@0: } nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Build a string of all file extensions supported nuclear@0: const aiImporterDesc* TerragenImporter::GetInfo () const nuclear@0: { nuclear@0: return &desc; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Setup import properties nuclear@0: void TerragenImporter::SetupProperties(const Importer* pImp) nuclear@0: { nuclear@0: // AI_CONFIG_IMPORT_TER_MAKE_UVS nuclear@0: configComputeUVs = ( 0 != pImp->GetPropertyInteger(AI_CONFIG_IMPORT_TER_MAKE_UVS,0) ); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Imports the given file into the given scene structure. nuclear@0: void TerragenImporter::InternReadFile( const std::string& pFile, nuclear@0: aiScene* pScene, IOSystem* pIOHandler) nuclear@0: { nuclear@0: IOStream* file = pIOHandler->Open( pFile, "rb"); nuclear@0: nuclear@0: // Check whether we can read from the file nuclear@0: if( file == NULL) nuclear@0: throw DeadlyImportError( "Failed to open TERRAGEN TERRAIN file " + pFile + "."); nuclear@0: nuclear@0: // Construct a stream reader to read all data in the correct endianess nuclear@0: StreamReaderLE reader(file); nuclear@0: if(reader.GetRemainingSize() < 16) nuclear@0: throw DeadlyImportError( "TER: file is too small" ); nuclear@0: nuclear@0: // Check for the existence of the two magic strings 'TERRAGEN' and 'TERRAIN ' nuclear@0: if (::strncmp((const char*)reader.GetPtr(),AI_TERR_BASE_STRING,8)) nuclear@0: throw DeadlyImportError( "TER: Magic string \'TERRAGEN\' not found" ); nuclear@0: nuclear@0: if (::strncmp((const char*)reader.GetPtr()+8,AI_TERR_TERRAIN_STRING,8)) nuclear@0: throw DeadlyImportError( "TER: Magic string \'TERRAIN\' not found" ); nuclear@0: nuclear@0: unsigned int x = 0,y = 0,mode = 0; nuclear@0: float rad = 6370.f; nuclear@0: (void)rad; nuclear@0: nuclear@0: nuclear@0: aiNode* root = pScene->mRootNode = new aiNode(); nuclear@0: root->mName.Set(""); nuclear@0: nuclear@0: // Default scaling is 30 nuclear@0: root->mTransformation.a1 = root->mTransformation.b2 = root->mTransformation.c3 = 30.f; nuclear@0: nuclear@0: // Now read all chunks until we're finished or an EOF marker is encountered nuclear@0: reader.IncPtr(16); nuclear@0: while (reader.GetRemainingSize() >= 4) nuclear@0: { nuclear@0: const char* head = (const char*)reader.GetPtr(); nuclear@0: reader.IncPtr(4); nuclear@0: nuclear@0: // EOF, break in every case nuclear@0: if (!::strncmp(head,AI_TERR_EOF_STRING,4)) nuclear@0: break; nuclear@0: nuclear@0: // Number of x-data points nuclear@0: if (!::strncmp(head,AI_TERR_CHUNK_XPTS,4)) nuclear@0: { nuclear@0: x = (uint16_t)reader.GetI2(); nuclear@0: } nuclear@0: // Number of y-data points nuclear@0: else if (!::strncmp(head,AI_TERR_CHUNK_YPTS,4)) nuclear@0: { nuclear@0: y = (uint16_t)reader.GetI2(); nuclear@0: } nuclear@0: // Squared terrains width-1. nuclear@0: else if (!::strncmp(head,AI_TERR_CHUNK_SIZE,4)) nuclear@0: { nuclear@0: x = y = (uint16_t)reader.GetI2()+1; nuclear@0: } nuclear@0: // terrain scaling nuclear@0: else if (!::strncmp(head,AI_TERR_CHUNK_SCAL,4)) nuclear@0: { nuclear@0: root->mTransformation.a1 = reader.GetF4(); nuclear@0: root->mTransformation.b2 = reader.GetF4(); nuclear@0: root->mTransformation.c3 = reader.GetF4(); nuclear@0: } nuclear@0: // mapping == 1: earth radius nuclear@0: else if (!::strncmp(head,AI_TERR_CHUNK_CRAD,4)) nuclear@0: { nuclear@0: rad = reader.GetF4(); nuclear@0: } nuclear@0: // mapping mode nuclear@0: else if (!::strncmp(head,AI_TERR_CHUNK_CRVM,4)) nuclear@0: { nuclear@0: mode = reader.GetI1(); nuclear@0: if (0 != mode) nuclear@0: DefaultLogger::get()->error("TER: Unsupported mapping mode, a flat terrain is returned"); nuclear@0: } nuclear@0: // actual terrain data nuclear@0: else if (!::strncmp(head,AI_TERR_CHUNK_ALTW,4)) nuclear@0: { nuclear@0: float hscale = (float)reader.GetI2() / 65536; nuclear@0: float bheight = (float)reader.GetI2(); nuclear@0: nuclear@0: if (!hscale)hscale = 1; nuclear@0: nuclear@0: // Ensure we have enough data nuclear@0: if (reader.GetRemainingSize() < x*y*2) nuclear@0: throw DeadlyImportError("TER: ALTW chunk is too small"); nuclear@0: nuclear@0: if (x <= 1 || y <= 1) nuclear@0: throw DeadlyImportError("TER: Invalid terrain size"); nuclear@0: nuclear@0: // Allocate the output mesh nuclear@0: pScene->mMeshes = new aiMesh*[pScene->mNumMeshes = 1]; nuclear@0: aiMesh* m = pScene->mMeshes[0] = new aiMesh(); nuclear@0: nuclear@0: // We return quads nuclear@0: aiFace* f = m->mFaces = new aiFace[m->mNumFaces = (x-1)*(y-1)]; nuclear@0: aiVector3D* pv = m->mVertices = new aiVector3D[m->mNumVertices = m->mNumFaces*4]; nuclear@0: nuclear@0: aiVector3D *uv( NULL ); nuclear@0: float step_y( 0.0f ), step_x( 0.0f ); nuclear@0: if (configComputeUVs) { nuclear@0: uv = m->mTextureCoords[0] = new aiVector3D[m->mNumVertices]; nuclear@0: step_y = 1.f/y; nuclear@0: step_x = 1.f/x; nuclear@0: } nuclear@0: const int16_t* data = (const int16_t*)reader.GetPtr(); nuclear@0: nuclear@0: for (unsigned int yy = 0, t = 0; yy < y-1;++yy) { nuclear@0: for (unsigned int xx = 0; xx < x-1;++xx,++f) { nuclear@0: nuclear@0: // make verts nuclear@0: const float fy = (float)yy, fx = (float)xx; nuclear@0: register unsigned tmp,tmp2; nuclear@0: *pv++ = aiVector3D(fx,fy, (float)data[(tmp2=x*yy) + xx] * hscale + bheight); nuclear@0: *pv++ = aiVector3D(fx,fy+1, (float)data[(tmp=x*(yy+1)) + xx] * hscale + bheight); nuclear@0: *pv++ = aiVector3D(fx+1,fy+1,(float)data[tmp + xx+1] * hscale + bheight); nuclear@0: *pv++ = aiVector3D(fx+1,fy, (float)data[tmp2 + xx+1] * hscale + bheight); nuclear@0: nuclear@0: // also make texture coordinates, if necessary nuclear@0: if (configComputeUVs) { nuclear@0: *uv++ = aiVector3D( step_x*xx, step_y*yy, 0.f ); nuclear@0: *uv++ = aiVector3D( step_x*xx, step_y*(yy+1), 0.f ); nuclear@0: *uv++ = aiVector3D( step_x*(xx+1), step_y*(yy+1), 0.f ); nuclear@0: *uv++ = aiVector3D( step_x*(xx+1), step_y*yy, 0.f ); nuclear@0: } nuclear@0: nuclear@0: // make indices nuclear@0: f->mIndices = new unsigned int[f->mNumIndices = 4]; nuclear@0: for (unsigned int i = 0; i < 4;++i) nuclear@0: f->mIndices[i] = t++; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // Add the mesh to the root node nuclear@0: root->mMeshes = new unsigned int[root->mNumMeshes = 1]; nuclear@0: root->mMeshes[0] = 0; nuclear@0: } nuclear@0: nuclear@0: // Get to the next chunk (4 byte aligned) nuclear@0: unsigned dtt; nuclear@0: if ((dtt = reader.GetCurrentPos() & 0x3)) nuclear@0: reader.IncPtr(4-dtt); nuclear@0: } nuclear@0: nuclear@0: // Check whether we have a mesh now nuclear@0: if (pScene->mNumMeshes != 1) nuclear@0: throw DeadlyImportError("TER: Unable to load terrain"); nuclear@0: nuclear@0: // Set the AI_SCENE_FLAGS_TERRAIN bit nuclear@0: pScene->mFlags |= AI_SCENE_FLAGS_TERRAIN; nuclear@0: } nuclear@0: nuclear@0: #endif // !! ASSIMP_BUILD_NO_TERRAGEN_IMPORTER