vrshoot
diff libs/assimp/TerragenLoader.cpp @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/TerragenLoader.cpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,268 @@ 1.4 +/* 1.5 +--------------------------------------------------------------------------- 1.6 +Open Asset Import Library (assimp) 1.7 +--------------------------------------------------------------------------- 1.8 + 1.9 +Copyright (c) 2006-2012, assimp team 1.10 + 1.11 +All rights reserved. 1.12 + 1.13 +Redistribution and use of this software in source and binary forms, 1.14 +with or without modification, are permitted provided that the following 1.15 +conditions are met: 1.16 + 1.17 +* Redistributions of source code must retain the above 1.18 + copyright notice, this list of conditions and the 1.19 + following disclaimer. 1.20 + 1.21 +* Redistributions in binary form must reproduce the above 1.22 + copyright notice, this list of conditions and the 1.23 + following disclaimer in the documentation and/or other 1.24 + materials provided with the distribution. 1.25 + 1.26 +* Neither the name of the assimp team, nor the names of its 1.27 + contributors may be used to endorse or promote products 1.28 + derived from this software without specific prior 1.29 + written permission of the assimp team. 1.30 + 1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.42 +--------------------------------------------------------------------------- 1.43 +*/ 1.44 + 1.45 +/** @file Implementation of the Terragen importer class */ 1.46 + 1.47 +#include "AssimpPCH.h" 1.48 + 1.49 +#ifndef ASSIMP_BUILD_NO_TERRAGEN_IMPORTER 1.50 +#include "TerragenLoader.h" 1.51 + 1.52 +using namespace Assimp; 1.53 + 1.54 +static const aiImporterDesc desc = { 1.55 + "Terragen Heightmap Importer", 1.56 + "", 1.57 + "", 1.58 + "http://www.planetside.co.uk/", 1.59 + aiImporterFlags_SupportBinaryFlavour, 1.60 + 0, 1.61 + 0, 1.62 + 0, 1.63 + 0, 1.64 + "ter" 1.65 +}; 1.66 + 1.67 +// ------------------------------------------------------------------------------------------------ 1.68 +// Constructor to be privately used by Importer 1.69 +TerragenImporter::TerragenImporter() 1.70 +: configComputeUVs (false) 1.71 +{} 1.72 + 1.73 +// ------------------------------------------------------------------------------------------------ 1.74 +// Destructor, private as well 1.75 +TerragenImporter::~TerragenImporter() 1.76 +{} 1.77 + 1.78 +// ------------------------------------------------------------------------------------------------ 1.79 +// Returns whether the class can handle the format of the given file. 1.80 +bool TerragenImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const 1.81 +{ 1.82 + // check file extension 1.83 + std::string extension = GetExtension(pFile); 1.84 + 1.85 + if( extension == "ter") 1.86 + return true; 1.87 + 1.88 + if( !extension.length() || checkSig) { 1.89 + /* If CanRead() is called in order to check whether we 1.90 + * support a specific file extension in general pIOHandler 1.91 + * might be NULL and it's our duty to return true here. 1.92 + */ 1.93 + if (!pIOHandler)return true; 1.94 + const char* tokens[] = {"terragen"}; 1.95 + return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1); 1.96 + } 1.97 + return false; 1.98 +} 1.99 + 1.100 +// ------------------------------------------------------------------------------------------------ 1.101 +// Build a string of all file extensions supported 1.102 +const aiImporterDesc* TerragenImporter::GetInfo () const 1.103 +{ 1.104 + return &desc; 1.105 +} 1.106 + 1.107 +// ------------------------------------------------------------------------------------------------ 1.108 +// Setup import properties 1.109 +void TerragenImporter::SetupProperties(const Importer* pImp) 1.110 +{ 1.111 + // AI_CONFIG_IMPORT_TER_MAKE_UVS 1.112 + configComputeUVs = ( 0 != pImp->GetPropertyInteger(AI_CONFIG_IMPORT_TER_MAKE_UVS,0) ); 1.113 +} 1.114 + 1.115 +// ------------------------------------------------------------------------------------------------ 1.116 +// Imports the given file into the given scene structure. 1.117 +void TerragenImporter::InternReadFile( const std::string& pFile, 1.118 + aiScene* pScene, IOSystem* pIOHandler) 1.119 +{ 1.120 + IOStream* file = pIOHandler->Open( pFile, "rb"); 1.121 + 1.122 + // Check whether we can read from the file 1.123 + if( file == NULL) 1.124 + throw DeadlyImportError( "Failed to open TERRAGEN TERRAIN file " + pFile + "."); 1.125 + 1.126 + // Construct a stream reader to read all data in the correct endianess 1.127 + StreamReaderLE reader(file); 1.128 + if(reader.GetRemainingSize() < 16) 1.129 + throw DeadlyImportError( "TER: file is too small" ); 1.130 + 1.131 + // Check for the existence of the two magic strings 'TERRAGEN' and 'TERRAIN ' 1.132 + if (::strncmp((const char*)reader.GetPtr(),AI_TERR_BASE_STRING,8)) 1.133 + throw DeadlyImportError( "TER: Magic string \'TERRAGEN\' not found" ); 1.134 + 1.135 + if (::strncmp((const char*)reader.GetPtr()+8,AI_TERR_TERRAIN_STRING,8)) 1.136 + throw DeadlyImportError( "TER: Magic string \'TERRAIN\' not found" ); 1.137 + 1.138 + unsigned int x = 0,y = 0,mode = 0; 1.139 + float rad = 6370.f; 1.140 + (void)rad; 1.141 + 1.142 + 1.143 + aiNode* root = pScene->mRootNode = new aiNode(); 1.144 + root->mName.Set("<TERRAGEN.TERRAIN>"); 1.145 + 1.146 + // Default scaling is 30 1.147 + root->mTransformation.a1 = root->mTransformation.b2 = root->mTransformation.c3 = 30.f; 1.148 + 1.149 + // Now read all chunks until we're finished or an EOF marker is encountered 1.150 + reader.IncPtr(16); 1.151 + while (reader.GetRemainingSize() >= 4) 1.152 + { 1.153 + const char* head = (const char*)reader.GetPtr(); 1.154 + reader.IncPtr(4); 1.155 + 1.156 + // EOF, break in every case 1.157 + if (!::strncmp(head,AI_TERR_EOF_STRING,4)) 1.158 + break; 1.159 + 1.160 + // Number of x-data points 1.161 + if (!::strncmp(head,AI_TERR_CHUNK_XPTS,4)) 1.162 + { 1.163 + x = (uint16_t)reader.GetI2(); 1.164 + } 1.165 + // Number of y-data points 1.166 + else if (!::strncmp(head,AI_TERR_CHUNK_YPTS,4)) 1.167 + { 1.168 + y = (uint16_t)reader.GetI2(); 1.169 + } 1.170 + // Squared terrains width-1. 1.171 + else if (!::strncmp(head,AI_TERR_CHUNK_SIZE,4)) 1.172 + { 1.173 + x = y = (uint16_t)reader.GetI2()+1; 1.174 + } 1.175 + // terrain scaling 1.176 + else if (!::strncmp(head,AI_TERR_CHUNK_SCAL,4)) 1.177 + { 1.178 + root->mTransformation.a1 = reader.GetF4(); 1.179 + root->mTransformation.b2 = reader.GetF4(); 1.180 + root->mTransformation.c3 = reader.GetF4(); 1.181 + } 1.182 + // mapping == 1: earth radius 1.183 + else if (!::strncmp(head,AI_TERR_CHUNK_CRAD,4)) 1.184 + { 1.185 + rad = reader.GetF4(); 1.186 + } 1.187 + // mapping mode 1.188 + else if (!::strncmp(head,AI_TERR_CHUNK_CRVM,4)) 1.189 + { 1.190 + mode = reader.GetI1(); 1.191 + if (0 != mode) 1.192 + DefaultLogger::get()->error("TER: Unsupported mapping mode, a flat terrain is returned"); 1.193 + } 1.194 + // actual terrain data 1.195 + else if (!::strncmp(head,AI_TERR_CHUNK_ALTW,4)) 1.196 + { 1.197 + float hscale = (float)reader.GetI2() / 65536; 1.198 + float bheight = (float)reader.GetI2(); 1.199 + 1.200 + if (!hscale)hscale = 1; 1.201 + 1.202 + // Ensure we have enough data 1.203 + if (reader.GetRemainingSize() < x*y*2) 1.204 + throw DeadlyImportError("TER: ALTW chunk is too small"); 1.205 + 1.206 + if (x <= 1 || y <= 1) 1.207 + throw DeadlyImportError("TER: Invalid terrain size"); 1.208 + 1.209 + // Allocate the output mesh 1.210 + pScene->mMeshes = new aiMesh*[pScene->mNumMeshes = 1]; 1.211 + aiMesh* m = pScene->mMeshes[0] = new aiMesh(); 1.212 + 1.213 + // We return quads 1.214 + aiFace* f = m->mFaces = new aiFace[m->mNumFaces = (x-1)*(y-1)]; 1.215 + aiVector3D* pv = m->mVertices = new aiVector3D[m->mNumVertices = m->mNumFaces*4]; 1.216 + 1.217 + aiVector3D *uv( NULL ); 1.218 + float step_y( 0.0f ), step_x( 0.0f ); 1.219 + if (configComputeUVs) { 1.220 + uv = m->mTextureCoords[0] = new aiVector3D[m->mNumVertices]; 1.221 + step_y = 1.f/y; 1.222 + step_x = 1.f/x; 1.223 + } 1.224 + const int16_t* data = (const int16_t*)reader.GetPtr(); 1.225 + 1.226 + for (unsigned int yy = 0, t = 0; yy < y-1;++yy) { 1.227 + for (unsigned int xx = 0; xx < x-1;++xx,++f) { 1.228 + 1.229 + // make verts 1.230 + const float fy = (float)yy, fx = (float)xx; 1.231 + register unsigned tmp,tmp2; 1.232 + *pv++ = aiVector3D(fx,fy, (float)data[(tmp2=x*yy) + xx] * hscale + bheight); 1.233 + *pv++ = aiVector3D(fx,fy+1, (float)data[(tmp=x*(yy+1)) + xx] * hscale + bheight); 1.234 + *pv++ = aiVector3D(fx+1,fy+1,(float)data[tmp + xx+1] * hscale + bheight); 1.235 + *pv++ = aiVector3D(fx+1,fy, (float)data[tmp2 + xx+1] * hscale + bheight); 1.236 + 1.237 + // also make texture coordinates, if necessary 1.238 + if (configComputeUVs) { 1.239 + *uv++ = aiVector3D( step_x*xx, step_y*yy, 0.f ); 1.240 + *uv++ = aiVector3D( step_x*xx, step_y*(yy+1), 0.f ); 1.241 + *uv++ = aiVector3D( step_x*(xx+1), step_y*(yy+1), 0.f ); 1.242 + *uv++ = aiVector3D( step_x*(xx+1), step_y*yy, 0.f ); 1.243 + } 1.244 + 1.245 + // make indices 1.246 + f->mIndices = new unsigned int[f->mNumIndices = 4]; 1.247 + for (unsigned int i = 0; i < 4;++i) 1.248 + f->mIndices[i] = t++; 1.249 + } 1.250 + } 1.251 + 1.252 + // Add the mesh to the root node 1.253 + root->mMeshes = new unsigned int[root->mNumMeshes = 1]; 1.254 + root->mMeshes[0] = 0; 1.255 + } 1.256 + 1.257 + // Get to the next chunk (4 byte aligned) 1.258 + unsigned dtt; 1.259 + if ((dtt = reader.GetCurrentPos() & 0x3)) 1.260 + reader.IncPtr(4-dtt); 1.261 + } 1.262 + 1.263 + // Check whether we have a mesh now 1.264 + if (pScene->mNumMeshes != 1) 1.265 + throw DeadlyImportError("TER: Unable to load terrain"); 1.266 + 1.267 + // Set the AI_SCENE_FLAGS_TERRAIN bit 1.268 + pScene->mFlags |= AI_SCENE_FLAGS_TERRAIN; 1.269 +} 1.270 + 1.271 +#endif // !! ASSIMP_BUILD_NO_TERRAGEN_IMPORTER