assimp/code/AssetLib/Terragen/TerragenLoader.cpp

264 lines
10 KiB
C++
Raw Normal View History

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