rev |
line source |
nuclear@0
|
1 /*
|
nuclear@0
|
2 ---------------------------------------------------------------------------
|
nuclear@0
|
3 Open Asset Import Library (assimp)
|
nuclear@0
|
4 ---------------------------------------------------------------------------
|
nuclear@0
|
5
|
nuclear@0
|
6 Copyright (c) 2006-2012, assimp team
|
nuclear@0
|
7
|
nuclear@0
|
8 All rights reserved.
|
nuclear@0
|
9
|
nuclear@0
|
10 Redistribution and use of this software in source and binary forms,
|
nuclear@0
|
11 with or without modification, are permitted provided that the following
|
nuclear@0
|
12 conditions are met:
|
nuclear@0
|
13
|
nuclear@0
|
14 * Redistributions of source code must retain the above
|
nuclear@0
|
15 copyright notice, this list of conditions and the
|
nuclear@0
|
16 following disclaimer.
|
nuclear@0
|
17
|
nuclear@0
|
18 * Redistributions in binary form must reproduce the above
|
nuclear@0
|
19 copyright notice, this list of conditions and the
|
nuclear@0
|
20 following disclaimer in the documentation and/or other
|
nuclear@0
|
21 materials provided with the distribution.
|
nuclear@0
|
22
|
nuclear@0
|
23 * Neither the name of the assimp team, nor the names of its
|
nuclear@0
|
24 contributors may be used to endorse or promote products
|
nuclear@0
|
25 derived from this software without specific prior
|
nuclear@0
|
26 written permission of the assimp team.
|
nuclear@0
|
27
|
nuclear@0
|
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
nuclear@0
|
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
nuclear@0
|
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
nuclear@0
|
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
nuclear@0
|
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
nuclear@0
|
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
nuclear@0
|
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
nuclear@0
|
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
nuclear@0
|
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
nuclear@0
|
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
nuclear@0
|
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
nuclear@0
|
39 ---------------------------------------------------------------------------
|
nuclear@0
|
40 */
|
nuclear@0
|
41
|
nuclear@0
|
42 /** @file Implementation of the Terragen importer class */
|
nuclear@0
|
43
|
nuclear@0
|
44 #include "AssimpPCH.h"
|
nuclear@0
|
45
|
nuclear@0
|
46 #ifndef ASSIMP_BUILD_NO_TERRAGEN_IMPORTER
|
nuclear@0
|
47 #include "TerragenLoader.h"
|
nuclear@0
|
48
|
nuclear@0
|
49 using namespace Assimp;
|
nuclear@0
|
50
|
nuclear@0
|
51 static const aiImporterDesc desc = {
|
nuclear@0
|
52 "Terragen Heightmap Importer",
|
nuclear@0
|
53 "",
|
nuclear@0
|
54 "",
|
nuclear@0
|
55 "http://www.planetside.co.uk/",
|
nuclear@0
|
56 aiImporterFlags_SupportBinaryFlavour,
|
nuclear@0
|
57 0,
|
nuclear@0
|
58 0,
|
nuclear@0
|
59 0,
|
nuclear@0
|
60 0,
|
nuclear@0
|
61 "ter"
|
nuclear@0
|
62 };
|
nuclear@0
|
63
|
nuclear@0
|
64 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
65 // Constructor to be privately used by Importer
|
nuclear@0
|
66 TerragenImporter::TerragenImporter()
|
nuclear@0
|
67 : configComputeUVs (false)
|
nuclear@0
|
68 {}
|
nuclear@0
|
69
|
nuclear@0
|
70 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
71 // Destructor, private as well
|
nuclear@0
|
72 TerragenImporter::~TerragenImporter()
|
nuclear@0
|
73 {}
|
nuclear@0
|
74
|
nuclear@0
|
75 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
76 // Returns whether the class can handle the format of the given file.
|
nuclear@0
|
77 bool TerragenImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
|
nuclear@0
|
78 {
|
nuclear@0
|
79 // check file extension
|
nuclear@0
|
80 std::string extension = GetExtension(pFile);
|
nuclear@0
|
81
|
nuclear@0
|
82 if( extension == "ter")
|
nuclear@0
|
83 return true;
|
nuclear@0
|
84
|
nuclear@0
|
85 if( !extension.length() || checkSig) {
|
nuclear@0
|
86 /* If CanRead() is called in order to check whether we
|
nuclear@0
|
87 * support a specific file extension in general pIOHandler
|
nuclear@0
|
88 * might be NULL and it's our duty to return true here.
|
nuclear@0
|
89 */
|
nuclear@0
|
90 if (!pIOHandler)return true;
|
nuclear@0
|
91 const char* tokens[] = {"terragen"};
|
nuclear@0
|
92 return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
|
nuclear@0
|
93 }
|
nuclear@0
|
94 return false;
|
nuclear@0
|
95 }
|
nuclear@0
|
96
|
nuclear@0
|
97 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
98 // Build a string of all file extensions supported
|
nuclear@0
|
99 const aiImporterDesc* TerragenImporter::GetInfo () const
|
nuclear@0
|
100 {
|
nuclear@0
|
101 return &desc;
|
nuclear@0
|
102 }
|
nuclear@0
|
103
|
nuclear@0
|
104 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
105 // Setup import properties
|
nuclear@0
|
106 void TerragenImporter::SetupProperties(const Importer* pImp)
|
nuclear@0
|
107 {
|
nuclear@0
|
108 // AI_CONFIG_IMPORT_TER_MAKE_UVS
|
nuclear@0
|
109 configComputeUVs = ( 0 != pImp->GetPropertyInteger(AI_CONFIG_IMPORT_TER_MAKE_UVS,0) );
|
nuclear@0
|
110 }
|
nuclear@0
|
111
|
nuclear@0
|
112 // ------------------------------------------------------------------------------------------------
|
nuclear@0
|
113 // Imports the given file into the given scene structure.
|
nuclear@0
|
114 void TerragenImporter::InternReadFile( const std::string& pFile,
|
nuclear@0
|
115 aiScene* pScene, IOSystem* pIOHandler)
|
nuclear@0
|
116 {
|
nuclear@0
|
117 IOStream* file = pIOHandler->Open( pFile, "rb");
|
nuclear@0
|
118
|
nuclear@0
|
119 // Check whether we can read from the file
|
nuclear@0
|
120 if( file == NULL)
|
nuclear@0
|
121 throw DeadlyImportError( "Failed to open TERRAGEN TERRAIN file " + pFile + ".");
|
nuclear@0
|
122
|
nuclear@0
|
123 // Construct a stream reader to read all data in the correct endianess
|
nuclear@0
|
124 StreamReaderLE reader(file);
|
nuclear@0
|
125 if(reader.GetRemainingSize() < 16)
|
nuclear@0
|
126 throw DeadlyImportError( "TER: file is too small" );
|
nuclear@0
|
127
|
nuclear@0
|
128 // Check for the existence of the two magic strings 'TERRAGEN' and 'TERRAIN '
|
nuclear@0
|
129 if (::strncmp((const char*)reader.GetPtr(),AI_TERR_BASE_STRING,8))
|
nuclear@0
|
130 throw DeadlyImportError( "TER: Magic string \'TERRAGEN\' not found" );
|
nuclear@0
|
131
|
nuclear@0
|
132 if (::strncmp((const char*)reader.GetPtr()+8,AI_TERR_TERRAIN_STRING,8))
|
nuclear@0
|
133 throw DeadlyImportError( "TER: Magic string \'TERRAIN\' not found" );
|
nuclear@0
|
134
|
nuclear@0
|
135 unsigned int x = 0,y = 0,mode = 0;
|
nuclear@0
|
136 float rad = 6370.f;
|
nuclear@0
|
137 (void)rad;
|
nuclear@0
|
138
|
nuclear@0
|
139
|
nuclear@0
|
140 aiNode* root = pScene->mRootNode = new aiNode();
|
nuclear@0
|
141 root->mName.Set("<TERRAGEN.TERRAIN>");
|
nuclear@0
|
142
|
nuclear@0
|
143 // Default scaling is 30
|
nuclear@0
|
144 root->mTransformation.a1 = root->mTransformation.b2 = root->mTransformation.c3 = 30.f;
|
nuclear@0
|
145
|
nuclear@0
|
146 // Now read all chunks until we're finished or an EOF marker is encountered
|
nuclear@0
|
147 reader.IncPtr(16);
|
nuclear@0
|
148 while (reader.GetRemainingSize() >= 4)
|
nuclear@0
|
149 {
|
nuclear@0
|
150 const char* head = (const char*)reader.GetPtr();
|
nuclear@0
|
151 reader.IncPtr(4);
|
nuclear@0
|
152
|
nuclear@0
|
153 // EOF, break in every case
|
nuclear@0
|
154 if (!::strncmp(head,AI_TERR_EOF_STRING,4))
|
nuclear@0
|
155 break;
|
nuclear@0
|
156
|
nuclear@0
|
157 // Number of x-data points
|
nuclear@0
|
158 if (!::strncmp(head,AI_TERR_CHUNK_XPTS,4))
|
nuclear@0
|
159 {
|
nuclear@0
|
160 x = (uint16_t)reader.GetI2();
|
nuclear@0
|
161 }
|
nuclear@0
|
162 // Number of y-data points
|
nuclear@0
|
163 else if (!::strncmp(head,AI_TERR_CHUNK_YPTS,4))
|
nuclear@0
|
164 {
|
nuclear@0
|
165 y = (uint16_t)reader.GetI2();
|
nuclear@0
|
166 }
|
nuclear@0
|
167 // Squared terrains width-1.
|
nuclear@0
|
168 else if (!::strncmp(head,AI_TERR_CHUNK_SIZE,4))
|
nuclear@0
|
169 {
|
nuclear@0
|
170 x = y = (uint16_t)reader.GetI2()+1;
|
nuclear@0
|
171 }
|
nuclear@0
|
172 // terrain scaling
|
nuclear@0
|
173 else if (!::strncmp(head,AI_TERR_CHUNK_SCAL,4))
|
nuclear@0
|
174 {
|
nuclear@0
|
175 root->mTransformation.a1 = reader.GetF4();
|
nuclear@0
|
176 root->mTransformation.b2 = reader.GetF4();
|
nuclear@0
|
177 root->mTransformation.c3 = reader.GetF4();
|
nuclear@0
|
178 }
|
nuclear@0
|
179 // mapping == 1: earth radius
|
nuclear@0
|
180 else if (!::strncmp(head,AI_TERR_CHUNK_CRAD,4))
|
nuclear@0
|
181 {
|
nuclear@0
|
182 rad = reader.GetF4();
|
nuclear@0
|
183 }
|
nuclear@0
|
184 // mapping mode
|
nuclear@0
|
185 else if (!::strncmp(head,AI_TERR_CHUNK_CRVM,4))
|
nuclear@0
|
186 {
|
nuclear@0
|
187 mode = reader.GetI1();
|
nuclear@0
|
188 if (0 != mode)
|
nuclear@0
|
189 DefaultLogger::get()->error("TER: Unsupported mapping mode, a flat terrain is returned");
|
nuclear@0
|
190 }
|
nuclear@0
|
191 // actual terrain data
|
nuclear@0
|
192 else if (!::strncmp(head,AI_TERR_CHUNK_ALTW,4))
|
nuclear@0
|
193 {
|
nuclear@0
|
194 float hscale = (float)reader.GetI2() / 65536;
|
nuclear@0
|
195 float bheight = (float)reader.GetI2();
|
nuclear@0
|
196
|
nuclear@0
|
197 if (!hscale)hscale = 1;
|
nuclear@0
|
198
|
nuclear@0
|
199 // Ensure we have enough data
|
nuclear@0
|
200 if (reader.GetRemainingSize() < x*y*2)
|
nuclear@0
|
201 throw DeadlyImportError("TER: ALTW chunk is too small");
|
nuclear@0
|
202
|
nuclear@0
|
203 if (x <= 1 || y <= 1)
|
nuclear@0
|
204 throw DeadlyImportError("TER: Invalid terrain size");
|
nuclear@0
|
205
|
nuclear@0
|
206 // Allocate the output mesh
|
nuclear@0
|
207 pScene->mMeshes = new aiMesh*[pScene->mNumMeshes = 1];
|
nuclear@0
|
208 aiMesh* m = pScene->mMeshes[0] = new aiMesh();
|
nuclear@0
|
209
|
nuclear@0
|
210 // We return quads
|
nuclear@0
|
211 aiFace* f = m->mFaces = new aiFace[m->mNumFaces = (x-1)*(y-1)];
|
nuclear@0
|
212 aiVector3D* pv = m->mVertices = new aiVector3D[m->mNumVertices = m->mNumFaces*4];
|
nuclear@0
|
213
|
nuclear@0
|
214 aiVector3D *uv( NULL );
|
nuclear@0
|
215 float step_y( 0.0f ), step_x( 0.0f );
|
nuclear@0
|
216 if (configComputeUVs) {
|
nuclear@0
|
217 uv = m->mTextureCoords[0] = new aiVector3D[m->mNumVertices];
|
nuclear@0
|
218 step_y = 1.f/y;
|
nuclear@0
|
219 step_x = 1.f/x;
|
nuclear@0
|
220 }
|
nuclear@0
|
221 const int16_t* data = (const int16_t*)reader.GetPtr();
|
nuclear@0
|
222
|
nuclear@0
|
223 for (unsigned int yy = 0, t = 0; yy < y-1;++yy) {
|
nuclear@0
|
224 for (unsigned int xx = 0; xx < x-1;++xx,++f) {
|
nuclear@0
|
225
|
nuclear@0
|
226 // make verts
|
nuclear@0
|
227 const float fy = (float)yy, fx = (float)xx;
|
nuclear@0
|
228 register unsigned tmp,tmp2;
|
nuclear@0
|
229 *pv++ = aiVector3D(fx,fy, (float)data[(tmp2=x*yy) + xx] * hscale + bheight);
|
nuclear@0
|
230 *pv++ = aiVector3D(fx,fy+1, (float)data[(tmp=x*(yy+1)) + xx] * hscale + bheight);
|
nuclear@0
|
231 *pv++ = aiVector3D(fx+1,fy+1,(float)data[tmp + xx+1] * hscale + bheight);
|
nuclear@0
|
232 *pv++ = aiVector3D(fx+1,fy, (float)data[tmp2 + xx+1] * hscale + bheight);
|
nuclear@0
|
233
|
nuclear@0
|
234 // also make texture coordinates, if necessary
|
nuclear@0
|
235 if (configComputeUVs) {
|
nuclear@0
|
236 *uv++ = aiVector3D( step_x*xx, step_y*yy, 0.f );
|
nuclear@0
|
237 *uv++ = aiVector3D( step_x*xx, step_y*(yy+1), 0.f );
|
nuclear@0
|
238 *uv++ = aiVector3D( step_x*(xx+1), step_y*(yy+1), 0.f );
|
nuclear@0
|
239 *uv++ = aiVector3D( step_x*(xx+1), step_y*yy, 0.f );
|
nuclear@0
|
240 }
|
nuclear@0
|
241
|
nuclear@0
|
242 // make indices
|
nuclear@0
|
243 f->mIndices = new unsigned int[f->mNumIndices = 4];
|
nuclear@0
|
244 for (unsigned int i = 0; i < 4;++i)
|
nuclear@0
|
245 f->mIndices[i] = t++;
|
nuclear@0
|
246 }
|
nuclear@0
|
247 }
|
nuclear@0
|
248
|
nuclear@0
|
249 // Add the mesh to the root node
|
nuclear@0
|
250 root->mMeshes = new unsigned int[root->mNumMeshes = 1];
|
nuclear@0
|
251 root->mMeshes[0] = 0;
|
nuclear@0
|
252 }
|
nuclear@0
|
253
|
nuclear@0
|
254 // Get to the next chunk (4 byte aligned)
|
nuclear@0
|
255 unsigned dtt;
|
nuclear@0
|
256 if ((dtt = reader.GetCurrentPos() & 0x3))
|
nuclear@0
|
257 reader.IncPtr(4-dtt);
|
nuclear@0
|
258 }
|
nuclear@0
|
259
|
nuclear@0
|
260 // Check whether we have a mesh now
|
nuclear@0
|
261 if (pScene->mNumMeshes != 1)
|
nuclear@0
|
262 throw DeadlyImportError("TER: Unable to load terrain");
|
nuclear@0
|
263
|
nuclear@0
|
264 // Set the AI_SCENE_FLAGS_TERRAIN bit
|
nuclear@0
|
265 pScene->mFlags |= AI_SCENE_FLAGS_TERRAIN;
|
nuclear@0
|
266 }
|
nuclear@0
|
267
|
nuclear@0
|
268 #endif // !! ASSIMP_BUILD_NO_TERRAGEN_IMPORTER
|