2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
Copyright (c) 2006-2021, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2015-05-19 03:48:29 +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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file Implementation of the MDL importer class */
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_HMP_IMPORTER
|
|
|
|
|
|
|
|
// internal headers
|
2020-05-02 13:14:38 +00:00
|
|
|
#include "AssetLib/HMP/HMPLoader.h"
|
|
|
|
#include "AssetLib/MD2/MD2FileData.h"
|
2019-06-10 21:26:00 +00:00
|
|
|
|
2021-05-04 23:16:23 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
2017-02-22 16:20:26 +00:00
|
|
|
#include <assimp/importerdesc.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-06-10 21:26:00 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
static const aiImporterDesc desc = {
|
2015-05-19 03:57:13 +00:00
|
|
|
"3D GameStudio Heightmap (HMP) Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportBinaryFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"hmp"
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
2020-06-23 19:05:42 +00:00
|
|
|
HMPImporter::HMPImporter() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// nothing to do here
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Destructor, private as well
|
2020-06-23 19:05:42 +00:00
|
|
|
HMPImporter::~HMPImporter() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// nothing to do here
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Returns whether the class can handle the format of the given file.
|
2020-06-23 19:05:42 +00:00
|
|
|
bool HMPImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool cs) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
const std::string extension = GetExtension(pFile);
|
2020-06-23 19:05:42 +00:00
|
|
|
if (extension == "hmp")
|
2015-05-19 03:57:13 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// if check for extension is not enough, check for the magic tokens
|
|
|
|
if (!extension.length() || cs) {
|
|
|
|
uint32_t tokens[3];
|
|
|
|
tokens[0] = AI_HMP_MAGIC_NUMBER_LE_4;
|
|
|
|
tokens[1] = AI_HMP_MAGIC_NUMBER_LE_5;
|
|
|
|
tokens[2] = AI_HMP_MAGIC_NUMBER_LE_7;
|
2020-06-23 19:05:42 +00:00
|
|
|
return CheckMagicToken(pIOHandler, pFile, tokens, 3, 0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
return false;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Get list of all file extensions that are handled by this loader
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiImporterDesc *HMPImporter::GetInfo() const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return &desc;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Imports the given file into the given scene structure.
|
2020-06-23 19:05:42 +00:00
|
|
|
void HMPImporter::InternReadFile(const std::string &pFile,
|
|
|
|
aiScene *_pScene, IOSystem *_pIOHandler) {
|
|
|
|
pScene = _pScene;
|
2020-02-18 13:41:19 +00:00
|
|
|
mIOHandler = _pIOHandler;
|
|
|
|
std::unique_ptr<IOStream> file(mIOHandler->Open(pFile));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check whether we can read from the file
|
2020-06-21 10:03:38 +00:00
|
|
|
if (file.get() == nullptr) {
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("Failed to open HMP file ", pFile, ".");
|
2020-06-21 10:03:38 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check whether the HMP file is large enough to contain
|
|
|
|
// at least the file header
|
|
|
|
const size_t fileSize = file->FileSize();
|
2020-06-23 19:05:42 +00:00
|
|
|
if (fileSize < 50)
|
|
|
|
throw DeadlyImportError("HMP File is too small.");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Allocate storage and copy the contents of the file to a memory buffer
|
2016-09-24 14:27:40 +00:00
|
|
|
mBuffer = new uint8_t[fileSize];
|
2020-06-23 19:05:42 +00:00
|
|
|
file->Read((void *)mBuffer, 1, fileSize);
|
2015-05-19 03:57:13 +00:00
|
|
|
iFileSize = (unsigned int)fileSize;
|
|
|
|
|
|
|
|
// Determine the file subtype and call the appropriate member function
|
2020-06-23 19:05:42 +00:00
|
|
|
const uint32_t iMagic = *((uint32_t *)this->mBuffer);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// HMP4 format
|
|
|
|
if (AI_HMP_MAGIC_NUMBER_LE_4 == iMagic ||
|
2020-06-23 19:05:42 +00:00
|
|
|
AI_HMP_MAGIC_NUMBER_BE_4 == iMagic) {
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_DEBUG("HMP subtype: 3D GameStudio A4, magic word is HMP4");
|
2015-05-19 03:57:13 +00:00
|
|
|
InternReadFile_HMP4();
|
|
|
|
}
|
|
|
|
// HMP5 format
|
|
|
|
else if (AI_HMP_MAGIC_NUMBER_LE_5 == iMagic ||
|
2020-06-23 19:05:42 +00:00
|
|
|
AI_HMP_MAGIC_NUMBER_BE_5 == iMagic) {
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_DEBUG("HMP subtype: 3D GameStudio A5, magic word is HMP5");
|
2015-05-19 03:57:13 +00:00
|
|
|
InternReadFile_HMP5();
|
|
|
|
}
|
|
|
|
// HMP7 format
|
|
|
|
else if (AI_HMP_MAGIC_NUMBER_LE_7 == iMagic ||
|
2020-06-23 19:05:42 +00:00
|
|
|
AI_HMP_MAGIC_NUMBER_BE_7 == iMagic) {
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_DEBUG("HMP subtype: 3D GameStudio A7, magic word is HMP7");
|
2015-05-19 03:57:13 +00:00
|
|
|
InternReadFile_HMP7();
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Print the magic word to the logger
|
2021-05-04 23:16:23 +00:00
|
|
|
std::string szBuffer = ai_str_toprintable((const char *)&iMagic, sizeof(iMagic));
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2021-04-05 19:24:54 +00:00
|
|
|
delete[] mBuffer;
|
|
|
|
mBuffer = nullptr;
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// We're definitely unable to load this file
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("Unknown HMP subformat ", pFile,
|
|
|
|
". Magic word (", szBuffer, ") is not known");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the AI_SCENE_FLAGS_TERRAIN bit
|
|
|
|
pScene->mFlags |= AI_SCENE_FLAGS_TERRAIN;
|
|
|
|
|
2016-09-24 14:27:40 +00:00
|
|
|
delete[] mBuffer;
|
2020-06-23 19:05:42 +00:00
|
|
|
mBuffer = nullptr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void HMPImporter::ValidateHeader_HMP457() {
|
|
|
|
const HMP::Header_HMP5 *const pcHeader = (const HMP::Header_HMP5 *)mBuffer;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (120 > iFileSize) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("HMP file is too small (header size is "
|
2020-06-23 19:05:42 +00:00
|
|
|
"120 bytes, this file is smaller)");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!pcHeader->ftrisize_x || !pcHeader->ftrisize_y)
|
|
|
|
throw DeadlyImportError("Size of triangles in either x or y direction is zero");
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pcHeader->fnumverts_x < 1.0f || (pcHeader->numverts / pcHeader->fnumverts_x) < 1.0f)
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("Number of triangles in either x or y direction is zero");
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!pcHeader->numframes)
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("There are no frames. At least one should be there");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void HMPImporter::InternReadFile_HMP4() {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("HMP4 is currently not supported");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void HMPImporter::InternReadFile_HMP5() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read the file header and skip everything to byte 84
|
2020-06-23 19:05:42 +00:00
|
|
|
const HMP::Header_HMP5 *pcHeader = (const HMP::Header_HMP5 *)mBuffer;
|
|
|
|
const unsigned char *szCurrent = (const unsigned char *)(mBuffer + 84);
|
2015-05-19 03:57:13 +00:00
|
|
|
ValidateHeader_HMP457();
|
|
|
|
|
|
|
|
// generate an output mesh
|
|
|
|
pScene->mNumMeshes = 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMeshes = new aiMesh *[1];
|
|
|
|
aiMesh *pcMesh = pScene->mMeshes[0] = new aiMesh();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
pcMesh->mMaterialIndex = 0;
|
|
|
|
pcMesh->mVertices = new aiVector3D[pcHeader->numverts];
|
|
|
|
pcMesh->mNormals = new aiVector3D[pcHeader->numverts];
|
|
|
|
|
|
|
|
const unsigned int height = (unsigned int)(pcHeader->numverts / pcHeader->fnumverts_x);
|
2020-06-23 19:05:42 +00:00
|
|
|
const unsigned int width = (unsigned int)pcHeader->fnumverts_x;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// generate/load a material for the terrain
|
2020-06-23 19:05:42 +00:00
|
|
|
CreateMaterial(szCurrent, &szCurrent);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// goto offset 120, I don't know why ...
|
|
|
|
// (fixme) is this the frame header? I assume yes since it starts with 2.
|
|
|
|
szCurrent += 36;
|
2020-06-23 19:05:42 +00:00
|
|
|
SizeCheck(szCurrent + sizeof(const HMP::Vertex_HMP7) * height * width);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now load all vertices from the file
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *pcVertOut = pcMesh->mVertices;
|
|
|
|
aiVector3D *pcNorOut = pcMesh->mNormals;
|
|
|
|
const HMP::Vertex_HMP5 *src = (const HMP::Vertex_HMP5 *)szCurrent;
|
|
|
|
for (unsigned int y = 0; y < height; ++y) {
|
|
|
|
for (unsigned int x = 0; x < width; ++x) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcVertOut->x = x * pcHeader->ftrisize_x;
|
|
|
|
pcVertOut->y = y * pcHeader->ftrisize_y;
|
2020-06-23 19:05:42 +00:00
|
|
|
pcVertOut->z = (((float)src->z / 0xffff) - 0.5f) * pcHeader->ftrisize_x * 8.0f;
|
|
|
|
MD2::LookupNormalIndex(src->normals162index, *pcNorOut);
|
|
|
|
++pcVertOut;
|
|
|
|
++pcNorOut;
|
|
|
|
++src;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate texture coordinates if necessary
|
|
|
|
if (pcHeader->numskins)
|
2020-06-23 19:05:42 +00:00
|
|
|
GenerateTextureCoords(width, height);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now build a list of faces
|
2020-06-23 19:05:42 +00:00
|
|
|
CreateOutputFaceList(width, height);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// there is no nodegraph in HMP files. Simply assign the one mesh
|
|
|
|
// (no, not the one ring) to the root node
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
pScene->mRootNode->mName.Set("terrain_root");
|
|
|
|
pScene->mRootNode->mNumMeshes = 1;
|
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[1];
|
|
|
|
pScene->mRootNode->mMeshes[0] = 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void HMPImporter::InternReadFile_HMP7() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// read the file header and skip everything to byte 84
|
2020-06-23 19:05:42 +00:00
|
|
|
const HMP::Header_HMP5 *const pcHeader = (const HMP::Header_HMP5 *)mBuffer;
|
|
|
|
const unsigned char *szCurrent = (const unsigned char *)(mBuffer + 84);
|
2015-05-19 03:57:13 +00:00
|
|
|
ValidateHeader_HMP457();
|
|
|
|
|
|
|
|
// generate an output mesh
|
|
|
|
pScene->mNumMeshes = 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMeshes = new aiMesh *[1];
|
|
|
|
aiMesh *pcMesh = pScene->mMeshes[0] = new aiMesh();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
pcMesh->mMaterialIndex = 0;
|
|
|
|
pcMesh->mVertices = new aiVector3D[pcHeader->numverts];
|
|
|
|
pcMesh->mNormals = new aiVector3D[pcHeader->numverts];
|
|
|
|
|
|
|
|
const unsigned int height = (unsigned int)(pcHeader->numverts / pcHeader->fnumverts_x);
|
|
|
|
const unsigned int width = (unsigned int)pcHeader->fnumverts_x;
|
|
|
|
|
|
|
|
// generate/load a material for the terrain
|
2020-06-23 19:05:42 +00:00
|
|
|
CreateMaterial(szCurrent, &szCurrent);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// goto offset 120, I don't know why ...
|
|
|
|
// (fixme) is this the frame header? I assume yes since it starts with 2.
|
|
|
|
szCurrent += 36;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
SizeCheck(szCurrent + sizeof(const HMP::Vertex_HMP7) * height * width);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now load all vertices from the file
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *pcVertOut = pcMesh->mVertices;
|
|
|
|
aiVector3D *pcNorOut = pcMesh->mNormals;
|
|
|
|
const HMP::Vertex_HMP7 *src = (const HMP::Vertex_HMP7 *)szCurrent;
|
|
|
|
for (unsigned int y = 0; y < height; ++y) {
|
|
|
|
for (unsigned int x = 0; x < width; ++x) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcVertOut->x = x * pcHeader->ftrisize_x;
|
|
|
|
pcVertOut->y = y * pcHeader->ftrisize_y;
|
|
|
|
|
|
|
|
// FIXME: What exctly is the correct scaling factor to use?
|
|
|
|
// possibly pcHeader->scale_origin[2] in combination with a
|
|
|
|
// signed interpretation of src->z?
|
2020-06-23 19:05:42 +00:00
|
|
|
pcVertOut->z = (((float)src->z / 0xffff) - 0.5f) * pcHeader->ftrisize_x * 8.0f;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
pcNorOut->x = ((float)src->normal_x / 0x80); // * pcHeader->scale_origin[0];
|
|
|
|
pcNorOut->y = ((float)src->normal_y / 0x80); // * pcHeader->scale_origin[1];
|
2015-05-19 03:57:13 +00:00
|
|
|
pcNorOut->z = 1.0f;
|
|
|
|
pcNorOut->Normalize();
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
++pcVertOut;
|
|
|
|
++pcNorOut;
|
|
|
|
++src;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate texture coordinates if necessary
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pcHeader->numskins) GenerateTextureCoords(width, height);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now build a list of faces
|
2020-06-23 19:05:42 +00:00
|
|
|
CreateOutputFaceList(width, height);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// there is no nodegraph in HMP files. Simply assign the one mesh
|
|
|
|
// (no, not the One Ring) to the root node
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
pScene->mRootNode->mName.Set("terrain_root");
|
|
|
|
pScene->mRootNode->mNumMeshes = 1;
|
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[1];
|
|
|
|
pScene->mRootNode->mMeshes[0] = 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void HMPImporter::CreateMaterial(const unsigned char *szCurrent,
|
|
|
|
const unsigned char **szCurrentOut) {
|
|
|
|
aiMesh *const pcMesh = pScene->mMeshes[0];
|
|
|
|
const HMP::Header_HMP5 *const pcHeader = (const HMP::Header_HMP5 *)mBuffer;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// we don't need to generate texture coordinates if
|
|
|
|
// we have no textures in the file ...
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pcHeader->numskins) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mTextureCoords[0] = new aiVector3D[pcHeader->numverts];
|
|
|
|
pcMesh->mNumUVComponents[0] = 2;
|
|
|
|
|
|
|
|
// now read the first skin and skip all others
|
2020-06-23 19:05:42 +00:00
|
|
|
ReadFirstSkin(pcHeader->numskins, szCurrent, &szCurrent);
|
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// generate a default material
|
|
|
|
const int iMode = (int)aiShadingMode_Gouraud;
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *pcHelper = new aiMaterial();
|
2015-05-19 03:57:13 +00:00
|
|
|
pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
|
|
|
|
|
|
|
|
aiColor3D clr;
|
|
|
|
clr.b = clr.g = clr.r = 0.6f;
|
2020-06-23 19:05:42 +00:00
|
|
|
pcHelper->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
pcHelper->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_SPECULAR);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
clr.b = clr.g = clr.r = 0.05f;
|
2020-06-23 19:05:42 +00:00
|
|
|
pcHelper->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_AMBIENT);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
aiString szName;
|
|
|
|
szName.Set(AI_DEFAULT_MATERIAL_NAME);
|
2020-06-23 19:05:42 +00:00
|
|
|
pcHelper->AddProperty(&szName, AI_MATKEY_NAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// add the material to the scene
|
|
|
|
pScene->mNumMaterials = 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMaterials = new aiMaterial *[1];
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mMaterials[0] = pcHelper;
|
|
|
|
}
|
|
|
|
*szCurrentOut = szCurrent;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void HMPImporter::CreateOutputFaceList(unsigned int width, unsigned int height) {
|
|
|
|
aiMesh *const pcMesh = this->pScene->mMeshes[0];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Allocate enough storage
|
2020-06-23 19:05:42 +00:00
|
|
|
pcMesh->mNumFaces = (width - 1) * (height - 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
pcMesh->mNumVertices = pcMesh->mNumFaces * 4;
|
|
|
|
aiVector3D *pcVertices = new aiVector3D[pcMesh->mNumVertices];
|
|
|
|
aiVector3D *pcNormals = new aiVector3D[pcMesh->mNumVertices];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiFace *pcFaceOut(pcMesh->mFaces);
|
|
|
|
aiVector3D *pcVertOut = pcVertices;
|
|
|
|
aiVector3D *pcNorOut = pcNormals;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *pcUVs = pcMesh->mTextureCoords[0] ? new aiVector3D[pcMesh->mNumVertices] : nullptr;
|
|
|
|
aiVector3D *pcUVOut(pcUVs);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Build the terrain square
|
|
|
|
unsigned int iCurrent = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int y = 0; y < height - 1; ++y) {
|
|
|
|
for (unsigned int x = 0; x < width - 1; ++x, ++pcFaceOut) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcFaceOut->mNumIndices = 4;
|
|
|
|
pcFaceOut->mIndices = new unsigned int[4];
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
*pcVertOut++ = pcMesh->mVertices[y * width + x];
|
|
|
|
*pcVertOut++ = pcMesh->mVertices[(y + 1) * width + x];
|
|
|
|
*pcVertOut++ = pcMesh->mVertices[(y + 1) * width + x + 1];
|
|
|
|
*pcVertOut++ = pcMesh->mVertices[y * width + x + 1];
|
|
|
|
|
|
|
|
*pcNorOut++ = pcMesh->mNormals[y * width + x];
|
|
|
|
*pcNorOut++ = pcMesh->mNormals[(y + 1) * width + x];
|
|
|
|
*pcNorOut++ = pcMesh->mNormals[(y + 1) * width + x + 1];
|
|
|
|
*pcNorOut++ = pcMesh->mNormals[y * width + x + 1];
|
|
|
|
|
|
|
|
if (pcMesh->mTextureCoords[0]) {
|
|
|
|
*pcUVOut++ = pcMesh->mTextureCoords[0][y * width + x];
|
|
|
|
*pcUVOut++ = pcMesh->mTextureCoords[0][(y + 1) * width + x];
|
|
|
|
*pcUVOut++ = pcMesh->mTextureCoords[0][(y + 1) * width + x + 1];
|
|
|
|
*pcUVOut++ = pcMesh->mTextureCoords[0][y * width + x + 1];
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
2015-05-19 03:57:13 +00:00
|
|
|
pcFaceOut->mIndices[i] = iCurrent++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete[] pcMesh->mVertices;
|
|
|
|
pcMesh->mVertices = pcVertices;
|
|
|
|
|
|
|
|
delete[] pcMesh->mNormals;
|
|
|
|
pcMesh->mNormals = pcNormals;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pcMesh->mTextureCoords[0]) {
|
2015-05-19 03:57:13 +00:00
|
|
|
delete[] pcMesh->mTextureCoords[0];
|
|
|
|
pcMesh->mTextureCoords[0] = pcUVs;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void HMPImporter::ReadFirstSkin(unsigned int iNumSkins, const unsigned char *szCursor,
|
|
|
|
const unsigned char **szCursorOut) {
|
|
|
|
ai_assert(0 != iNumSkins);
|
|
|
|
ai_assert(nullptr != szCursor);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read the type of the skin ...
|
|
|
|
// sometimes we need to skip 12 bytes here, I don't know why ...
|
2020-06-23 19:05:42 +00:00
|
|
|
uint32_t iType = *((uint32_t *)szCursor);
|
2016-09-24 14:27:40 +00:00
|
|
|
szCursor += sizeof(uint32_t);
|
2020-06-23 19:05:42 +00:00
|
|
|
if (0 == iType) {
|
2015-05-19 03:57:13 +00:00
|
|
|
szCursor += sizeof(uint32_t) * 2;
|
2020-06-23 19:05:42 +00:00
|
|
|
iType = *((uint32_t *)szCursor);
|
2016-09-24 14:27:40 +00:00
|
|
|
szCursor += sizeof(uint32_t);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!iType)
|
|
|
|
throw DeadlyImportError("Unable to read HMP7 skin chunk");
|
|
|
|
}
|
|
|
|
// read width and height
|
2020-06-23 19:05:42 +00:00
|
|
|
uint32_t iWidth = *((uint32_t *)szCursor);
|
|
|
|
szCursor += sizeof(uint32_t);
|
|
|
|
uint32_t iHeight = *((uint32_t *)szCursor);
|
|
|
|
szCursor += sizeof(uint32_t);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// allocate an output material
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *pcMat = new aiMaterial();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read the skin, this works exactly as for MDL7
|
2020-06-23 19:05:42 +00:00
|
|
|
ParseSkinLump_3DGS_MDL7(szCursor, &szCursor,
|
|
|
|
pcMat, iType, iWidth, iHeight);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now we need to skip any other skins ...
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 1; i < iNumSkins; ++i) {
|
|
|
|
iType = *((uint32_t *)szCursor);
|
|
|
|
szCursor += sizeof(uint32_t);
|
|
|
|
iWidth = *((uint32_t *)szCursor);
|
|
|
|
szCursor += sizeof(uint32_t);
|
|
|
|
iHeight = *((uint32_t *)szCursor);
|
|
|
|
szCursor += sizeof(uint32_t);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
SkipSkinLump_3DGS_MDL7(szCursor, &szCursor, iType, iWidth, iHeight);
|
2015-05-19 03:57:13 +00:00
|
|
|
SizeCheck(szCursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the material ...
|
|
|
|
pScene->mNumMaterials = 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMaterials = new aiMaterial *[1];
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mMaterials[0] = pcMat;
|
|
|
|
|
|
|
|
*szCursorOut = szCursor;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:48:29 +00:00
|
|
|
// Generate proepr texture coords
|
|
|
|
void HMPImporter::GenerateTextureCoords(
|
2020-06-23 19:05:42 +00:00
|
|
|
const unsigned int width, const unsigned int height) {
|
|
|
|
ai_assert(nullptr != pScene->mMeshes);
|
|
|
|
ai_assert(nullptr != pScene->mMeshes[0]);
|
|
|
|
ai_assert(nullptr != pScene->mMeshes[0]->mTextureCoords[0]);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *uv = pScene->mMeshes[0]->mTextureCoords[0];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const float fY = (1.0f / height) + (1.0f / height) / (height - 1);
|
|
|
|
const float fX = (1.0f / width) + (1.0f / width) / (width - 1);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int y = 0; y < height; ++y) {
|
|
|
|
for (unsigned int x = 0; x < width; ++x, ++uv) {
|
|
|
|
uv->y = fY * y;
|
|
|
|
uv->x = fX * x;
|
2015-05-19 03:57:13 +00:00
|
|
|
uv->z = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_HMP_IMPORTER
|