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
|
|
|
|
2017-05-09 17:57:36 +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 MD3Loader.cpp
|
2015-05-19 03:52:10 +00:00
|
|
|
* @brief Implementation of the MD3 importer class
|
|
|
|
*
|
|
|
|
* Sources:
|
2015-05-19 03:48:29 +00:00
|
|
|
* http://www.gamers.org/dEngine/quake3/UQ3S
|
|
|
|
* http://linux.ucla.edu/~phaethon/q3/formats/md3format.html
|
|
|
|
* http://www.heppler.com/shader/shader/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_MD3_IMPORTER
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
#include "AssetLib/MD3/MD3Loader.h"
|
2019-06-10 21:26:00 +00:00
|
|
|
#include "Common/Importer.h"
|
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/GenericProperty.h>
|
|
|
|
#include <assimp/ParsingUtils.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/RemoveComments.h>
|
|
|
|
#include <assimp/SceneCombiner.h>
|
|
|
|
#include <assimp/importerdesc.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/material.h>
|
|
|
|
#include <assimp/scene.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
2019-06-10 21:26:00 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
#include <cctype>
|
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
|
|
|
"Quake III Mesh Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportBinaryFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"md3"
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Convert a Q3 shader blend function to the appropriate enum value
|
2020-06-23 19:05:42 +00:00
|
|
|
Q3Shader::BlendFunc StringToBlendFunc(const std::string &m) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (m == "GL_ONE") {
|
|
|
|
return Q3Shader::BLEND_GL_ONE;
|
|
|
|
}
|
|
|
|
if (m == "GL_ZERO") {
|
|
|
|
return Q3Shader::BLEND_GL_ZERO;
|
|
|
|
}
|
|
|
|
if (m == "GL_SRC_ALPHA") {
|
|
|
|
return Q3Shader::BLEND_GL_SRC_ALPHA;
|
|
|
|
}
|
|
|
|
if (m == "GL_ONE_MINUS_SRC_ALPHA") {
|
|
|
|
return Q3Shader::BLEND_GL_ONE_MINUS_SRC_ALPHA;
|
|
|
|
}
|
|
|
|
if (m == "GL_ONE_MINUS_DST_COLOR") {
|
|
|
|
return Q3Shader::BLEND_GL_ONE_MINUS_DST_COLOR;
|
|
|
|
}
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_ERROR("Q3Shader: Unknown blend function: " + m);
|
2015-05-19 03:57:13 +00:00
|
|
|
return Q3Shader::BLEND_NONE;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Load a Quake 3 shader
|
2020-06-23 19:05:42 +00:00
|
|
|
bool Q3Shader::LoadShader(ShaderData &fill, const std::string &pFile, IOSystem *io) {
|
|
|
|
std::unique_ptr<IOStream> file(io->Open(pFile, "rt"));
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!file.get())
|
|
|
|
return false; // if we can't access the file, don't worry and return
|
|
|
|
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_INFO_F("Loading Quake3 shader file ", pFile);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read file in memory
|
|
|
|
const size_t s = file->FileSize();
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<char> _buff(s + 1);
|
|
|
|
file->Read(&_buff[0], s, 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
_buff[s] = 0;
|
|
|
|
|
|
|
|
// remove comments from it (C++ style)
|
2020-06-23 19:05:42 +00:00
|
|
|
CommentRemover::RemoveLineComments("//", &_buff[0]);
|
|
|
|
const char *buff = &_buff[0];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
Q3Shader::ShaderDataBlock *curData = nullptr;
|
|
|
|
Q3Shader::ShaderMapBlock *curMap = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read line per line
|
2020-06-23 19:05:42 +00:00
|
|
|
for (; SkipSpacesAndLineEnd(&buff); SkipLine(&buff)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (*buff == '{') {
|
|
|
|
++buff;
|
|
|
|
|
|
|
|
// append to last section, if any
|
|
|
|
if (!curData) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_ERROR("Q3Shader: Unexpected shader section token \'{\'");
|
2015-05-19 03:57:13 +00:00
|
|
|
return true; // still no failure, the file is there
|
|
|
|
}
|
|
|
|
|
|
|
|
// read this data section
|
2020-06-23 19:05:42 +00:00
|
|
|
for (; SkipSpacesAndLineEnd(&buff); SkipLine(&buff)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (*buff == '{') {
|
|
|
|
++buff;
|
|
|
|
// add new map section
|
|
|
|
curData->maps.push_back(Q3Shader::ShaderMapBlock());
|
|
|
|
curMap = &curData->maps.back();
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (; SkipSpacesAndLineEnd(&buff); SkipLine(&buff)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// 'map' - Specifies texture file name
|
2020-06-23 19:05:42 +00:00
|
|
|
if (TokenMatchI(buff, "map", 3) || TokenMatchI(buff, "clampmap", 8)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
curMap->name = GetNextToken(buff);
|
|
|
|
}
|
|
|
|
// 'blendfunc' - Alpha blending mode
|
2020-06-23 19:05:42 +00:00
|
|
|
else if (TokenMatchI(buff, "blendfunc", 9)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
const std::string blend_src = GetNextToken(buff);
|
|
|
|
if (blend_src == "add") {
|
2020-06-23 19:05:42 +00:00
|
|
|
curMap->blend_src = Q3Shader::BLEND_GL_ONE;
|
2015-05-19 03:57:13 +00:00
|
|
|
curMap->blend_dest = Q3Shader::BLEND_GL_ONE;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (blend_src == "filter") {
|
|
|
|
curMap->blend_src = Q3Shader::BLEND_GL_DST_COLOR;
|
2015-05-19 03:57:13 +00:00
|
|
|
curMap->blend_dest = Q3Shader::BLEND_GL_ZERO;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (blend_src == "blend") {
|
|
|
|
curMap->blend_src = Q3Shader::BLEND_GL_SRC_ALPHA;
|
2015-05-19 03:57:13 +00:00
|
|
|
curMap->blend_dest = Q3Shader::BLEND_GL_ONE_MINUS_SRC_ALPHA;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
|
|
|
curMap->blend_src = StringToBlendFunc(blend_src);
|
2015-05-19 03:57:13 +00:00
|
|
|
curMap->blend_dest = StringToBlendFunc(GetNextToken(buff));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 'alphafunc' - Alpha testing mode
|
2020-06-23 19:05:42 +00:00
|
|
|
else if (TokenMatchI(buff, "alphafunc", 9)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
const std::string at = GetNextToken(buff);
|
|
|
|
if (at == "GT0") {
|
|
|
|
curMap->alpha_test = Q3Shader::AT_GT0;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (at == "LT128") {
|
2015-05-19 03:57:13 +00:00
|
|
|
curMap->alpha_test = Q3Shader::AT_LT128;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (at == "GE128") {
|
2015-05-19 03:57:13 +00:00
|
|
|
curMap->alpha_test = Q3Shader::AT_GE128;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (*buff == '}') {
|
2015-05-19 03:57:13 +00:00
|
|
|
++buff;
|
|
|
|
// close this map section
|
2020-06-23 19:05:42 +00:00
|
|
|
curMap = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (*buff == '}') {
|
2015-05-19 03:57:13 +00:00
|
|
|
++buff;
|
2020-06-23 19:05:42 +00:00
|
|
|
curData = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'cull' specifies culling behaviour for the model
|
2020-06-23 19:05:42 +00:00
|
|
|
else if (TokenMatchI(buff, "cull", 4)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipSpaces(&buff);
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!ASSIMP_strincmp(buff, "back", 4)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
curData->cull = Q3Shader::CULL_CCW;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (!ASSIMP_strincmp(buff, "front", 5)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
curData->cull = Q3Shader::CULL_CW;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (!ASSIMP_strincmp(buff, "none", 4) || !ASSIMP_strincmp(buff, "disable", 7)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
curData->cull = Q3Shader::CULL_NONE;
|
2018-04-20 14:23:24 +00:00
|
|
|
} else {
|
|
|
|
ASSIMP_LOG_ERROR("Q3Shader: Unrecognized cull mode");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-20 14:23:24 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// add new section
|
|
|
|
fill.blocks.push_back(Q3Shader::ShaderDataBlock());
|
|
|
|
curData = &fill.blocks.back();
|
|
|
|
|
|
|
|
// get the name of this section
|
|
|
|
curData->name = GetNextToken(buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Load a Quake 3 skin
|
2020-06-23 19:05:42 +00:00
|
|
|
bool Q3Shader::LoadSkin(SkinData &fill, const std::string &pFile, IOSystem *io) {
|
|
|
|
std::unique_ptr<IOStream> file(io->Open(pFile, "rt"));
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!file.get())
|
|
|
|
return false; // if we can't access the file, don't worry and return
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_INFO("Loading Quake3 skin file " + pFile);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// read file in memory
|
|
|
|
const size_t s = file->FileSize();
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<char> _buff(s + 1);
|
|
|
|
const char *buff = &_buff[0];
|
|
|
|
file->Read(&_buff[0], s, 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
_buff[s] = 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// remove commas
|
2020-06-23 19:05:42 +00:00
|
|
|
std::replace(_buff.begin(), _buff.end(), ',', ' ');
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// read token by token and fill output table
|
2020-06-23 19:05:42 +00:00
|
|
|
for (; *buff;) {
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipSpacesAndLineEnd(&buff);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// get first identifier
|
|
|
|
std::string ss = GetNextToken(buff);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// ignore tokens starting with tag_
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!::strncmp(&ss[0], "tag_", std::min((size_t)4, ss.length())))
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
fill.textures.push_back(SkinData::TextureEntry());
|
2019-03-13 15:58:18 +00:00
|
|
|
SkinData::TextureEntry &entry = fill.textures.back();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
entry.first = ss;
|
2019-03-13 15:58:18 +00:00
|
|
|
entry.second = GetNextToken(buff);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
return true;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Convert Q3Shader to material
|
2020-06-23 19:05:42 +00:00
|
|
|
void Q3Shader::ConvertShaderToMaterial(aiMaterial *out, const ShaderDataBlock &shader) {
|
|
|
|
ai_assert(nullptr != out);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
/* IMPORTANT: This is not a real conversion. Actually we're just guessing and
|
|
|
|
* hacking around to build an aiMaterial that looks nearly equal to the
|
|
|
|
* original Quake 3 shader. We're missing some important features like
|
|
|
|
* animatable material properties in our material system, but at least
|
|
|
|
* multiple textures should be handled correctly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Two-sided material?
|
|
|
|
if (shader.cull == Q3Shader::CULL_NONE) {
|
|
|
|
const int twosided = 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
out->AddProperty(&twosided, 1, AI_MATKEY_TWOSIDED);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
unsigned int cur_emissive = 0, cur_diffuse = 0, cur_lm = 0;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Iterate through all textures
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::list<Q3Shader::ShaderMapBlock>::const_iterator it = shader.maps.begin(); it != shader.maps.end(); ++it) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// CONVERSION BEHAVIOUR:
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// If the texture is additive
|
|
|
|
// - if it is the first texture, assume additive blending for the whole material
|
|
|
|
// - otherwise register it as emissive texture.
|
|
|
|
//
|
|
|
|
// If the texture is using standard blend (or if the blend mode is unknown)
|
|
|
|
// - if first texture: assume default blending for material
|
|
|
|
// - in any case: set it as diffuse texture
|
|
|
|
//
|
|
|
|
// If the texture is using 'filter' blending
|
|
|
|
// - take as lightmap
|
|
|
|
//
|
|
|
|
// Textures with alpha funcs
|
|
|
|
// - aiTextureFlags_UseAlpha is set (otherwise aiTextureFlags_NoAlpha is explicitly set)
|
|
|
|
aiString s((*it).name);
|
2020-06-23 19:05:42 +00:00
|
|
|
aiTextureType type;
|
|
|
|
unsigned int index;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if ((*it).blend_src == Q3Shader::BLEND_GL_ONE && (*it).blend_dest == Q3Shader::BLEND_GL_ONE) {
|
|
|
|
if (it == shader.maps.begin()) {
|
|
|
|
const int additive = aiBlendMode_Additive;
|
2020-06-23 19:05:42 +00:00
|
|
|
out->AddProperty(&additive, 1, AI_MATKEY_BLEND_FUNC);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
index = cur_diffuse++;
|
2020-06-23 19:05:42 +00:00
|
|
|
type = aiTextureType_DIFFUSE;
|
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
index = cur_emissive++;
|
2020-06-23 19:05:42 +00:00
|
|
|
type = aiTextureType_EMISSIVE;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if ((*it).blend_src == Q3Shader::BLEND_GL_DST_COLOR && (*it).blend_dest == Q3Shader::BLEND_GL_ZERO) {
|
2015-05-19 03:57:13 +00:00
|
|
|
index = cur_lm++;
|
2020-06-23 19:05:42 +00:00
|
|
|
type = aiTextureType_LIGHTMAP;
|
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
const int blend = aiBlendMode_Default;
|
2020-06-23 19:05:42 +00:00
|
|
|
out->AddProperty(&blend, 1, AI_MATKEY_BLEND_FUNC);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
index = cur_diffuse++;
|
2020-06-23 19:05:42 +00:00
|
|
|
type = aiTextureType_DIFFUSE;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup texture
|
2020-06-23 19:05:42 +00:00
|
|
|
out->AddProperty(&s, AI_MATKEY_TEXTURE(type, index));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// setup texture flags
|
|
|
|
const int use_alpha = ((*it).alpha_test != Q3Shader::AT_NONE ? aiTextureFlags_UseAlpha : aiTextureFlags_IgnoreAlpha);
|
2020-06-23 19:05:42 +00:00
|
|
|
out->AddProperty(&use_alpha, 1, AI_MATKEY_TEXFLAGS(type, index));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
// If at least one emissive texture was set, set the emissive base color to 1 to ensure
|
|
|
|
// the texture is actually displayed.
|
|
|
|
if (0 != cur_emissive) {
|
2020-06-23 19:05:42 +00:00
|
|
|
aiColor3D one(1.f, 1.f, 1.f);
|
|
|
|
out->AddProperty(&one, 1, AI_MATKEY_COLOR_EMISSIVE);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
2020-06-23 19:05:42 +00:00
|
|
|
MD3Importer::MD3Importer() :
|
|
|
|
configFrameID(0), configHandleMP(true), configSpeedFlag(), pcHeader(), mBuffer(), fileSize(), mScene(), mIOHandler() {}
|
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
|
|
|
MD3Importer::~MD3Importer() {}
|
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 MD3Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
const std::string extension = GetExtension(pFile);
|
|
|
|
if (extension == "md3")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// if check for extension is not enough, check for the magic tokens
|
|
|
|
if (!extension.length() || checkSig) {
|
|
|
|
uint32_t tokens[1];
|
|
|
|
tokens[0] = AI_MD3_MAGIC_NUMBER_LE;
|
2020-06-23 19:05:42 +00:00
|
|
|
return CheckMagicToken(pIOHandler, pFile, tokens, 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
return false;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void MD3Importer::ValidateHeaderOffsets() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Check magic number
|
|
|
|
if (pcHeader->IDENT != AI_MD3_MAGIC_NUMBER_BE &&
|
2020-06-23 19:05:42 +00:00
|
|
|
pcHeader->IDENT != AI_MD3_MAGIC_NUMBER_LE)
|
|
|
|
throw DeadlyImportError("Invalid MD3 file: Magic bytes not found");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check file format version
|
|
|
|
if (pcHeader->VERSION > 15)
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_LOG_WARN("Unsupported MD3 file version. Continuing happily ...");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check some offset values whether they are valid
|
|
|
|
if (!pcHeader->NUM_SURFACES)
|
2020-06-23 19:05:42 +00:00
|
|
|
throw DeadlyImportError("Invalid md3 file: NUM_SURFACES is 0");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (pcHeader->OFS_FRAMES >= fileSize || pcHeader->OFS_SURFACES >= fileSize ||
|
2020-06-23 19:05:42 +00:00
|
|
|
pcHeader->OFS_EOF > fileSize) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("Invalid MD3 header: some offsets are outside the file");
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pcHeader->NUM_SURFACES > AI_MAX_ALLOC(MD3::Surface)) {
|
2016-03-12 22:54:23 +00:00
|
|
|
throw DeadlyImportError("Invalid MD3 header: too many surfaces, would overflow");
|
2020-06-23 19:05:42 +00:00
|
|
|
}
|
2016-03-12 22:54:23 +00:00
|
|
|
|
|
|
|
if (pcHeader->OFS_SURFACES + pcHeader->NUM_SURFACES * sizeof(MD3::Surface) >= fileSize) {
|
|
|
|
throw DeadlyImportError("Invalid MD3 header: some surfaces are outside the file");
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pcHeader->NUM_FRAMES <= configFrameID)
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("The requested frame is not existing the file");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void MD3Importer::ValidateSurfaceHeaderOffsets(const MD3::Surface *pcSurf) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Calculate the relative offset of the surface
|
2020-06-23 19:05:42 +00:00
|
|
|
const int32_t ofs = int32_t((const unsigned char *)pcSurf - this->mBuffer);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check whether all data chunks are inside the valid range
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pcSurf->OFS_TRIANGLES + ofs + pcSurf->NUM_TRIANGLES * sizeof(MD3::Triangle) > fileSize ||
|
|
|
|
pcSurf->OFS_SHADERS + ofs + pcSurf->NUM_SHADER * sizeof(MD3::Shader) > fileSize ||
|
|
|
|
pcSurf->OFS_ST + ofs + pcSurf->NUM_VERTICES * sizeof(MD3::TexCoord) > fileSize ||
|
|
|
|
pcSurf->OFS_XYZNORMAL + ofs + pcSurf->NUM_VERTICES * sizeof(MD3::Vertex) > fileSize) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
throw DeadlyImportError("Invalid MD3 surface header: some offsets are outside the file");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether all requirements for Q3 files are met. We don't
|
|
|
|
// care, but probably someone does.
|
|
|
|
if (pcSurf->NUM_TRIANGLES > AI_MD3_MAX_TRIANGLES) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_WARN("MD3: Quake III triangle limit exceeded");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pcSurf->NUM_SHADER > AI_MD3_MAX_SHADERS) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_WARN("MD3: Quake III shader limit exceeded");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pcSurf->NUM_VERTICES > AI_MD3_MAX_VERTS) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_WARN("MD3: Quake III vertex limit exceeded");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pcSurf->NUM_FRAMES > AI_MD3_MAX_FRAMES) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_WARN("MD3: Quake III frame limit exceeded");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiImporterDesc *MD3Importer::GetInfo() const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return &desc;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Setup configuration properties
|
2020-06-23 19:05:42 +00:00
|
|
|
void MD3Importer::SetupProperties(const Importer *pImp) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// The
|
|
|
|
// AI_CONFIG_IMPORT_MD3_KEYFRAME option overrides the
|
|
|
|
// AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option.
|
2020-06-23 19:05:42 +00:00
|
|
|
configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_MD3_KEYFRAME, -1);
|
|
|
|
if (static_cast<unsigned int>(-1) == configFrameID) {
|
|
|
|
configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME, 0);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// AI_CONFIG_IMPORT_MD3_HANDLE_MULTIPART
|
2020-06-23 19:05:42 +00:00
|
|
|
configHandleMP = (0 != pImp->GetPropertyInteger(AI_CONFIG_IMPORT_MD3_HANDLE_MULTIPART, 1));
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// AI_CONFIG_IMPORT_MD3_SKIN_NAME
|
2020-06-23 19:05:42 +00:00
|
|
|
configSkinFile = (pImp->GetPropertyString(AI_CONFIG_IMPORT_MD3_SKIN_NAME, "default"));
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// AI_CONFIG_IMPORT_MD3_SHADER_SRC
|
2020-06-23 19:05:42 +00:00
|
|
|
configShaderFile = (pImp->GetPropertyString(AI_CONFIG_IMPORT_MD3_SHADER_SRC, ""));
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// AI_CONFIG_FAVOUR_SPEED
|
2020-06-23 19:05:42 +00:00
|
|
|
configSpeedFlag = (0 != pImp->GetPropertyInteger(AI_CONFIG_FAVOUR_SPEED, 0));
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Try to read the skin for a MD3 file
|
2020-06-23 19:05:42 +00:00
|
|
|
void MD3Importer::ReadSkin(Q3Shader::SkinData &fill) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
// skip any postfixes (e.g. lower_1.md3)
|
|
|
|
std::string::size_type s = filename.find_last_of('_');
|
|
|
|
if (s == std::string::npos) {
|
|
|
|
s = filename.find_last_of('.');
|
|
|
|
if (s == std::string::npos) {
|
|
|
|
s = filename.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ai_assert(s != std::string::npos);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const std::string skin_file = path + filename.substr(0, s) + "_" + configSkinFile + ".skin";
|
|
|
|
Q3Shader::LoadSkin(fill, skin_file, mIOHandler);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Try to read the shader for a MD3 file
|
2020-06-23 19:05:42 +00:00
|
|
|
void MD3Importer::ReadShader(Q3Shader::ShaderData &fill) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Determine Q3 model name from given path
|
2020-06-23 19:05:42 +00:00
|
|
|
const std::string::size_type s = path.find_last_of("\\/", path.length() - 2);
|
|
|
|
const std::string model_file = path.substr(s + 1, path.length() - (s + 2));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// If no specific dir or file is given, use our default search behaviour
|
|
|
|
if (!configShaderFile.length()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!Q3Shader::LoadShader(fill, path + "..\\..\\..\\scripts\\" + model_file + ".shader", mIOHandler)) {
|
|
|
|
Q3Shader::LoadShader(fill, path + "..\\..\\..\\scripts\\" + filename + ".shader", mIOHandler);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// If the given string specifies a file, load this file.
|
|
|
|
// Otherwise it's a directory.
|
|
|
|
const std::string::size_type st = configShaderFile.find_last_of('.');
|
|
|
|
if (st == std::string::npos) {
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!Q3Shader::LoadShader(fill, configShaderFile + model_file + ".shader", mIOHandler)) {
|
|
|
|
Q3Shader::LoadShader(fill, configShaderFile + filename + ".shader", mIOHandler);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
|
|
|
Q3Shader::LoadShader(fill, configShaderFile, mIOHandler);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Tiny helper to remove a single node from its parent' list
|
2020-06-23 19:05:42 +00:00
|
|
|
void RemoveSingleNodeFromList(aiNode *nd) {
|
|
|
|
if (!nd || nd->mNumChildren || !nd->mParent) return;
|
|
|
|
aiNode *par = nd->mParent;
|
|
|
|
for (unsigned int i = 0; i < par->mNumChildren; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (par->mChildren[i] == nd) {
|
|
|
|
--par->mNumChildren;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (; i < par->mNumChildren; ++i) {
|
|
|
|
par->mChildren[i] = par->mChildren[i + 1];
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
delete nd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Read a multi-part Q3 player model
|
2020-06-23 19:05:42 +00:00
|
|
|
bool MD3Importer::ReadMultipartFile() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// check whether the file name contains a common postfix, e.g lower_2.md3
|
|
|
|
std::string::size_type s = filename.find_last_of('_'), t = filename.find_last_of('.');
|
|
|
|
|
|
|
|
if (t == std::string::npos)
|
|
|
|
t = filename.size();
|
|
|
|
if (s == std::string::npos)
|
|
|
|
s = t;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const std::string mod_filename = filename.substr(0, s);
|
|
|
|
const std::string suffix = filename.substr(s, t - s);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (mod_filename == "lower" || mod_filename == "upper" || mod_filename == "head") {
|
2015-05-19 03:57:13 +00:00
|
|
|
const std::string lower = path + "lower" + suffix + ".md3";
|
|
|
|
const std::string upper = path + "upper" + suffix + ".md3";
|
2020-06-23 19:05:42 +00:00
|
|
|
const std::string head = path + "head" + suffix + ".md3";
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiScene *scene_upper = nullptr;
|
|
|
|
aiScene *scene_lower = nullptr;
|
|
|
|
aiScene *scene_head = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string failure;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiNode *tag_torso, *tag_head;
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector<AttachmentInfo> attach;
|
|
|
|
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_INFO("Multi part MD3 player model: lower, upper and head parts are joined");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// ensure we won't try to load ourselves recursively
|
|
|
|
BatchLoader::PropertyMap props;
|
2020-06-23 19:05:42 +00:00
|
|
|
SetGenericProperty(props.ints, AI_CONFIG_IMPORT_MD3_HANDLE_MULTIPART, 0);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now read these three files
|
|
|
|
BatchLoader batch(mIOHandler);
|
2020-06-23 19:05:42 +00:00
|
|
|
const unsigned int _lower = batch.AddLoadRequest(lower, 0, &props);
|
|
|
|
const unsigned int _upper = batch.AddLoadRequest(upper, 0, &props);
|
|
|
|
const unsigned int _head = batch.AddLoadRequest(head, 0, &props);
|
2015-05-19 03:57:13 +00:00
|
|
|
batch.LoadAll();
|
|
|
|
|
|
|
|
// now construct a dummy scene to place these three parts in
|
2020-06-23 19:05:42 +00:00
|
|
|
aiScene *master = new aiScene();
|
|
|
|
aiNode *nd = master->mRootNode = new aiNode();
|
2015-05-19 03:57:13 +00:00
|
|
|
nd->mName.Set("<MD3_Player>");
|
|
|
|
|
|
|
|
// ... and get them. We need all of them.
|
|
|
|
scene_lower = batch.GetImport(_lower);
|
|
|
|
if (!scene_lower) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_ERROR("M3D: Failed to read multi part model, lower.md3 fails to load");
|
2015-05-19 03:57:13 +00:00
|
|
|
failure = "lower";
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
scene_upper = batch.GetImport(_upper);
|
|
|
|
if (!scene_upper) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_ERROR("M3D: Failed to read multi part model, upper.md3 fails to load");
|
2015-05-19 03:57:13 +00:00
|
|
|
failure = "upper";
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
scene_head = batch.GetImport(_head);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!scene_head) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_ERROR("M3D: Failed to read multi part model, head.md3 fails to load");
|
2015-05-19 03:57:13 +00:00
|
|
|
failure = "head";
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// build attachment infos. search for typical Q3 tags
|
|
|
|
|
|
|
|
// original root
|
|
|
|
scene_lower->mRootNode->mName.Set("lower");
|
|
|
|
attach.push_back(AttachmentInfo(scene_lower, nd));
|
|
|
|
|
|
|
|
// tag_torso
|
|
|
|
tag_torso = scene_lower->mRootNode->FindNode("tag_torso");
|
|
|
|
if (!tag_torso) {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_ERROR("M3D: Failed to find attachment tag for multi part model: tag_torso expected");
|
2015-05-19 03:57:13 +00:00
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
scene_upper->mRootNode->mName.Set("upper");
|
2020-06-23 19:05:42 +00:00
|
|
|
attach.push_back(AttachmentInfo(scene_upper, tag_torso));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// tag_head
|
|
|
|
tag_head = scene_upper->mRootNode->FindNode("tag_head");
|
|
|
|
if (!tag_head) {
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_LOG_ERROR("M3D: Failed to find attachment tag for multi part model: tag_head expected");
|
2015-05-19 03:57:13 +00:00
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
scene_head->mRootNode->mName.Set("head");
|
2020-06-23 19:05:42 +00:00
|
|
|
attach.push_back(AttachmentInfo(scene_head, tag_head));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Remove tag_head and tag_torso from all other model parts ...
|
|
|
|
// this ensures (together with AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY)
|
|
|
|
// that tag_torso/tag_head is also the name of the (unique) output node
|
2020-06-23 19:05:42 +00:00
|
|
|
RemoveSingleNodeFromList(scene_upper->mRootNode->FindNode("tag_torso"));
|
|
|
|
RemoveSingleNodeFromList(scene_head->mRootNode->FindNode("tag_head"));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Undo the rotations which we applied to the coordinate systems. We're
|
|
|
|
// working in global Quake space here
|
2020-06-23 19:05:42 +00:00
|
|
|
scene_head->mRootNode->mTransformation = aiMatrix4x4();
|
2015-05-19 03:57:13 +00:00
|
|
|
scene_lower->mRootNode->mTransformation = aiMatrix4x4();
|
|
|
|
scene_upper->mRootNode->mTransformation = aiMatrix4x4();
|
|
|
|
|
|
|
|
// and merge the scenes
|
2020-06-23 19:05:42 +00:00
|
|
|
SceneCombiner::MergeScenes(&mScene, master, attach,
|
|
|
|
AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES |
|
|
|
|
AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES |
|
|
|
|
AI_INT_MERGE_SCENE_RESOLVE_CROSS_ATTACHMENTS |
|
|
|
|
(!configSpeedFlag ? AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY : 0));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Now rotate the whole scene 90 degrees around the x axis to convert to internal coordinate system
|
2020-06-23 19:05:42 +00:00
|
|
|
mScene->mRootNode->mTransformation = aiMatrix4x4(1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f, 0.f, -1.f, 0.f, 0.f, 0.f, 0.f, 0.f, 1.f);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
return true;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
error_cleanup:
|
2015-05-19 03:57:13 +00:00
|
|
|
delete scene_upper;
|
|
|
|
delete scene_lower;
|
|
|
|
delete scene_head;
|
|
|
|
delete master;
|
|
|
|
|
|
|
|
if (failure == mod_filename) {
|
|
|
|
throw DeadlyImportError("MD3: failure to read multipart host file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Convert a MD3 path to a proper value
|
2020-06-23 19:05:42 +00:00
|
|
|
void MD3Importer::ConvertPath(const char *texture_name, const char *header_name, std::string &out) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
// If the MD3's internal path itself and the given path are using
|
|
|
|
// the same directory, remove it completely to get right output paths.
|
2020-06-23 19:05:42 +00:00
|
|
|
const char *end1 = ::strrchr(header_name, '\\');
|
|
|
|
if (!end1) end1 = ::strrchr(header_name, '/');
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const char *end2 = ::strrchr(texture_name, '\\');
|
|
|
|
if (!end2) end2 = ::strrchr(texture_name, '/');
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// HACK: If the paths starts with "models", ignore the
|
|
|
|
// next two hierarchy levels, it specifies just the model name.
|
|
|
|
// Ignored by Q3, it might be not equal to the real model location.
|
2020-06-23 19:05:42 +00:00
|
|
|
if (end2) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
size_t len2;
|
|
|
|
const size_t len1 = (size_t)(end1 - header_name);
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!ASSIMP_strincmp(texture_name, "models", 6) && (texture_name[6] == '/' || texture_name[6] == '\\')) {
|
2015-05-19 03:57:13 +00:00
|
|
|
len2 = 6; // ignore the seventh - could be slash or backslash
|
|
|
|
|
|
|
|
if (!header_name[0]) {
|
|
|
|
// Use the file name only
|
2020-06-23 19:05:42 +00:00
|
|
|
out = end2 + 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else
|
|
|
|
len2 = std::min(len1, (size_t)(end2 - texture_name));
|
|
|
|
if (!ASSIMP_strincmp(texture_name, header_name, static_cast<unsigned int>(len2))) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Use the file name only
|
2020-06-23 19:05:42 +00:00
|
|
|
out = end2 + 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Use the full path
|
|
|
|
out = texture_name;
|
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 MD3Importer::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mFile = pFile;
|
|
|
|
mScene = pScene;
|
|
|
|
mIOHandler = pIOHandler;
|
|
|
|
|
|
|
|
// get base path and file name
|
|
|
|
// todo ... move to PathConverter
|
|
|
|
std::string::size_type s = mFile.find_last_of("/\\");
|
|
|
|
if (s == std::string::npos) {
|
|
|
|
s = 0;
|
2019-03-13 15:58:18 +00:00
|
|
|
} else {
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
filename = mFile.substr(s), path = mFile.substr(0, s);
|
|
|
|
for (std::string::iterator it = filename.begin(); it != filename.end(); ++it) {
|
2020-06-23 19:05:42 +00:00
|
|
|
*it = static_cast<char>(tolower(*it));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load multi-part model file, if necessary
|
|
|
|
if (configHandleMP) {
|
|
|
|
if (ReadMultipartFile())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::unique_ptr<IOStream> file(pIOHandler->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 MD3 file ", pFile, ".");
|
2020-06-21 10:03:38 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check whether the md3 file is large enough to contain the header
|
|
|
|
fileSize = (unsigned int)file->FileSize();
|
2020-06-23 19:05:42 +00:00
|
|
|
if (fileSize < sizeof(MD3::Header))
|
|
|
|
throw DeadlyImportError("MD3 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
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<unsigned char> mBuffer2(fileSize);
|
|
|
|
file->Read(&mBuffer2[0], 1, fileSize);
|
2015-05-19 03:57:13 +00:00
|
|
|
mBuffer = &mBuffer2[0];
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
pcHeader = (BE_NCONST MD3::Header *)mBuffer;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2016-04-03 00:38:00 +00:00
|
|
|
// Ensure correct endianness
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifdef AI_BUILD_BIG_ENDIAN
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
AI_SWAP4(pcHeader->VERSION);
|
|
|
|
AI_SWAP4(pcHeader->FLAGS);
|
|
|
|
AI_SWAP4(pcHeader->IDENT);
|
|
|
|
AI_SWAP4(pcHeader->NUM_FRAMES);
|
|
|
|
AI_SWAP4(pcHeader->NUM_SKINS);
|
|
|
|
AI_SWAP4(pcHeader->NUM_SURFACES);
|
|
|
|
AI_SWAP4(pcHeader->NUM_TAGS);
|
|
|
|
AI_SWAP4(pcHeader->OFS_EOF);
|
|
|
|
AI_SWAP4(pcHeader->OFS_FRAMES);
|
|
|
|
AI_SWAP4(pcHeader->OFS_SURFACES);
|
|
|
|
AI_SWAP4(pcHeader->OFS_TAGS);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Validate the file header
|
|
|
|
ValidateHeaderOffsets();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Navigate to the list of surfaces
|
2020-06-23 19:05:42 +00:00
|
|
|
BE_NCONST MD3::Surface *pcSurfaces = (BE_NCONST MD3::Surface *)(mBuffer + pcHeader->OFS_SURFACES);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Navigate to the list of tags
|
2020-06-23 19:05:42 +00:00
|
|
|
BE_NCONST MD3::Tag *pcTags = (BE_NCONST MD3::Tag *)(mBuffer + pcHeader->OFS_TAGS);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Allocate output storage
|
|
|
|
pScene->mNumMeshes = pcHeader->NUM_SURFACES;
|
2015-08-11 13:09:19 +00:00
|
|
|
if (pcHeader->NUM_SURFACES == 0) {
|
|
|
|
throw DeadlyImportError("MD3: No surfaces");
|
2015-08-13 10:01:49 +00:00
|
|
|
} else if (pcHeader->NUM_SURFACES > AI_MAX_ALLOC(aiMesh)) {
|
|
|
|
// We allocate pointers but check against the size of aiMesh
|
|
|
|
// since those pointers will eventually have to point to real objects
|
2015-08-11 13:09:19 +00:00
|
|
|
throw DeadlyImportError("MD3: Too many surfaces, would run out of memory");
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mNumMaterials = pcHeader->NUM_SURFACES;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMaterials = new aiMaterial *[pScene->mNumMeshes];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Set arrays to zero to ensue proper destruction if an exception is raised
|
2020-06-23 19:05:42 +00:00
|
|
|
::memset(pScene->mMeshes, 0, pScene->mNumMeshes * sizeof(aiMesh *));
|
|
|
|
::memset(pScene->mMaterials, 0, pScene->mNumMaterials * sizeof(aiMaterial *));
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Now read possible skins from .skin file
|
|
|
|
Q3Shader::SkinData skins;
|
|
|
|
ReadSkin(skins);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// And check whether we can locate a shader file for this model
|
|
|
|
Q3Shader::ShaderData shaders;
|
|
|
|
ReadShader(shaders);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Adjust all texture paths in the shader
|
2020-06-23 19:05:42 +00:00
|
|
|
const char *header_name = pcHeader->NAME;
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!shaders.blocks.empty()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::list<Q3Shader::ShaderDataBlock>::iterator dit = shaders.blocks.begin(); dit != shaders.blocks.end(); ++dit) {
|
|
|
|
ConvertPath((*dit).name.c_str(), header_name, (*dit).name);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::list<Q3Shader::ShaderMapBlock>::iterator mit = (*dit).maps.begin(); mit != (*dit).maps.end(); ++mit) {
|
|
|
|
ConvertPath((*mit).name.c_str(), header_name, (*mit).name);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Read all surfaces from the file
|
|
|
|
unsigned int iNum = pcHeader->NUM_SURFACES;
|
|
|
|
unsigned int iNumMaterials = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
while (iNum-- > 0) {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-04-03 00:38:00 +00:00
|
|
|
// Ensure correct endianness
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifdef AI_BUILD_BIG_ENDIAN
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
AI_SWAP4(pcSurfaces->FLAGS);
|
|
|
|
AI_SWAP4(pcSurfaces->IDENT);
|
|
|
|
AI_SWAP4(pcSurfaces->NUM_FRAMES);
|
|
|
|
AI_SWAP4(pcSurfaces->NUM_SHADER);
|
|
|
|
AI_SWAP4(pcSurfaces->NUM_TRIANGLES);
|
|
|
|
AI_SWAP4(pcSurfaces->NUM_VERTICES);
|
|
|
|
AI_SWAP4(pcSurfaces->OFS_END);
|
|
|
|
AI_SWAP4(pcSurfaces->OFS_SHADERS);
|
|
|
|
AI_SWAP4(pcSurfaces->OFS_ST);
|
|
|
|
AI_SWAP4(pcSurfaces->OFS_TRIANGLES);
|
|
|
|
AI_SWAP4(pcSurfaces->OFS_XYZNORMAL);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Validate the surface header
|
|
|
|
ValidateSurfaceHeaderOffsets(pcSurfaces);
|
|
|
|
|
|
|
|
// Navigate to the vertex list of the surface
|
2020-06-23 19:05:42 +00:00
|
|
|
BE_NCONST MD3::Vertex *pcVertices = (BE_NCONST MD3::Vertex *)(((uint8_t *)pcSurfaces) + pcSurfaces->OFS_XYZNORMAL);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Navigate to the triangle list of the surface
|
2020-06-23 19:05:42 +00:00
|
|
|
BE_NCONST MD3::Triangle *pcTriangles = (BE_NCONST MD3::Triangle *)(((uint8_t *)pcSurfaces) + pcSurfaces->OFS_TRIANGLES);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Navigate to the texture coordinate list of the surface
|
2020-06-23 19:05:42 +00:00
|
|
|
BE_NCONST MD3::TexCoord *pcUVs = (BE_NCONST MD3::TexCoord *)(((uint8_t *)pcSurfaces) + pcSurfaces->OFS_ST);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Navigate to the shader list of the surface
|
2020-06-23 19:05:42 +00:00
|
|
|
BE_NCONST MD3::Shader *pcShaders = (BE_NCONST MD3::Shader *)(((uint8_t *)pcSurfaces) + pcSurfaces->OFS_SHADERS);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// If the submesh is empty ignore it
|
2020-06-23 19:05:42 +00:00
|
|
|
if (0 == pcSurfaces->NUM_VERTICES || 0 == pcSurfaces->NUM_TRIANGLES) {
|
|
|
|
pcSurfaces = (BE_NCONST MD3::Surface *)(((uint8_t *)pcSurfaces) + pcSurfaces->OFS_END);
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mNumMeshes--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate output mesh
|
|
|
|
pScene->mMeshes[iNum] = new aiMesh();
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *pcMesh = pScene->mMeshes[iNum];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
std::string _texture_name;
|
2020-06-23 19:05:42 +00:00
|
|
|
const char *texture_name = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check whether we have a texture record for this surface in the .skin file
|
2020-06-23 19:05:42 +00:00
|
|
|
std::list<Q3Shader::SkinData::TextureEntry>::iterator it = std::find(
|
|
|
|
skins.textures.begin(), skins.textures.end(), pcSurfaces->NAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (it != skins.textures.end()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
texture_name = &*(_texture_name = (*it).second).begin();
|
2020-05-15 15:59:42 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG_F("MD3: Assigning skin texture ", (*it).second, " to surface ", pcSurfaces->NAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
(*it).resolved = true; // mark entry as resolved
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the first shader (= texture?) assigned to the surface
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!texture_name && pcSurfaces->NUM_SHADER) {
|
2015-05-19 03:57:13 +00:00
|
|
|
texture_name = pcShaders->NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string convertedPath;
|
|
|
|
if (texture_name) {
|
2020-06-23 19:05:42 +00:00
|
|
|
ConvertPath(texture_name, header_name, convertedPath);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const Q3Shader::ShaderDataBlock *shader = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Now search the current shader for a record with this name (
|
|
|
|
// excluding texture file extension)
|
|
|
|
if (!shaders.blocks.empty()) {
|
2019-03-13 15:58:18 +00:00
|
|
|
std::string::size_type sh = convertedPath.find_last_of('.');
|
|
|
|
if (sh == std::string::npos) {
|
|
|
|
sh = convertedPath.length();
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const std::string without_ext = convertedPath.substr(0, sh);
|
|
|
|
std::list<Q3Shader::ShaderDataBlock>::const_iterator dit = std::find(shaders.blocks.begin(), shaders.blocks.end(), without_ext);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (dit != shaders.blocks.end()) {
|
2019-03-13 15:58:18 +00:00
|
|
|
// We made it!
|
2015-05-19 03:57:13 +00:00
|
|
|
shader = &*dit;
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_LOG_INFO("Found shader record for " + without_ext);
|
2018-04-20 14:23:24 +00:00
|
|
|
} else {
|
|
|
|
ASSIMP_LOG_WARN("Unable to find shader record for " + without_ext);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *pcHelper = new aiMaterial();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
const int iMode = (int)aiShadingMode_Gouraud;
|
|
|
|
pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
|
|
|
|
|
|
|
|
// Add a small ambient color value - Quake 3 seems to have one
|
|
|
|
aiColor3D clr;
|
|
|
|
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
|
|
|
|
|
|
|
clr.b = clr.g = clr.r = 1.0f;
|
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
|
|
|
|
|
|
|
// use surface name + skin_name as material name
|
|
|
|
aiString name;
|
|
|
|
name.Set("MD3_[" + configSkinFile + "][" + pcSurfaces->NAME + "]");
|
2020-06-23 19:05:42 +00:00
|
|
|
pcHelper->AddProperty(&name, AI_MATKEY_NAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (!shader) {
|
|
|
|
// Setup dummy texture file name to ensure UV coordinates are kept during postprocessing
|
|
|
|
aiString szString;
|
|
|
|
if (convertedPath.length()) {
|
|
|
|
szString.Set(convertedPath);
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2018-04-20 14:23:24 +00:00
|
|
|
ASSIMP_LOG_WARN("Texture file name has zero length. Using default name");
|
2015-05-19 03:57:13 +00:00
|
|
|
szString.Set("dummy_texture.bmp");
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
pcHelper->AddProperty(&szString, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// prevent transparency by default
|
|
|
|
int no_alpha = aiTextureFlags_IgnoreAlpha;
|
2020-06-23 19:05:42 +00:00
|
|
|
pcHelper->AddProperty(&no_alpha, 1, AI_MATKEY_TEXFLAGS_DIFFUSE(0));
|
2019-03-16 15:08:03 +00:00
|
|
|
} else {
|
2020-06-23 19:05:42 +00:00
|
|
|
Q3Shader::ConvertShaderToMaterial(pcHelper, *shader);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMaterials[iNumMaterials] = (aiMaterial *)pcHelper;
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mMaterialIndex = iNumMaterials++;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// Ensure correct endianness
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifdef AI_BUILD_BIG_ENDIAN
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (uint32_t i = 0; i < pcSurfaces->NUM_VERTICES; ++i) {
|
|
|
|
AI_SWAP2(pcVertices[i].NORMAL);
|
|
|
|
AI_SWAP2(pcVertices[i].X);
|
|
|
|
AI_SWAP2(pcVertices[i].Y);
|
|
|
|
AI_SWAP2(pcVertices[i].Z);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
AI_SWAP4(pcUVs[i].U);
|
|
|
|
AI_SWAP4(pcUVs[i].V);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
for (uint32_t i = 0; i < pcSurfaces->NUM_TRIANGLES; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
AI_SWAP4(pcTriangles[i].INDEXES[0]);
|
|
|
|
AI_SWAP4(pcTriangles[i].INDEXES[1]);
|
|
|
|
AI_SWAP4(pcTriangles[i].INDEXES[2]);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Fill mesh information
|
|
|
|
pcMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
pcMesh->mNumVertices = pcSurfaces->NUM_TRIANGLES * 3;
|
|
|
|
pcMesh->mNumFaces = pcSurfaces->NUM_TRIANGLES;
|
|
|
|
pcMesh->mFaces = new aiFace[pcSurfaces->NUM_TRIANGLES];
|
|
|
|
pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices];
|
|
|
|
pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices];
|
|
|
|
pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices];
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mNumUVComponents[0] = 2;
|
|
|
|
|
|
|
|
// Fill in all triangles
|
|
|
|
unsigned int iCurrent = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < (unsigned int)pcSurfaces->NUM_TRIANGLES; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mFaces[i].mIndices = new unsigned int[3];
|
|
|
|
pcMesh->mFaces[i].mNumIndices = 3;
|
|
|
|
|
|
|
|
//unsigned int iTemp = iCurrent;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int c = 0; c < 3; ++c, ++iCurrent) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mFaces[i].mIndices[c] = iCurrent;
|
|
|
|
|
|
|
|
// Read vertices
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D &vec = pcMesh->mVertices[iCurrent];
|
2016-03-12 19:50:34 +00:00
|
|
|
uint32_t index = pcTriangles->INDEXES[c];
|
|
|
|
if (index >= pcSurfaces->NUM_VERTICES) {
|
2020-06-23 19:05:42 +00:00
|
|
|
throw DeadlyImportError("MD3: Invalid vertex index");
|
2016-03-12 19:50:34 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
vec.x = pcVertices[index].X * AI_MD3_XYZ_SCALE;
|
|
|
|
vec.y = pcVertices[index].Y * AI_MD3_XYZ_SCALE;
|
|
|
|
vec.z = pcVertices[index].Z * AI_MD3_XYZ_SCALE;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Convert the normal vector to uncompressed float3 format
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D &nor = pcMesh->mNormals[iCurrent];
|
|
|
|
LatLngNormalToVec3(pcVertices[index].NORMAL, (ai_real *)&nor);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Read texture coordinates
|
2017-12-01 14:20:50 +00:00
|
|
|
pcMesh->mTextureCoords[0][iCurrent].x = pcUVs[index].U;
|
2020-06-23 19:05:42 +00:00
|
|
|
pcMesh->mTextureCoords[0][iCurrent].y = 1.0f - pcUVs[index].V;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
// Flip face order if necessary
|
|
|
|
if (!shader || shader->cull == Q3Shader::CULL_CW) {
|
2020-06-23 19:05:42 +00:00
|
|
|
std::swap(pcMesh->mFaces[i].mIndices[2], pcMesh->mFaces[i].mIndices[1]);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2019-03-16 15:08:03 +00:00
|
|
|
++pcTriangles;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go to the next surface
|
2020-06-23 19:05:42 +00:00
|
|
|
pcSurfaces = (BE_NCONST MD3::Surface *)(((unsigned char *)pcSurfaces) + pcSurfaces->OFS_END);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For debugging purposes: check whether we found matches for all entries in the skins file
|
|
|
|
if (!DefaultLogger::isNullLogger()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::list<Q3Shader::SkinData::TextureEntry>::const_iterator it = skins.textures.begin(); it != skins.textures.end(); ++it) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!(*it).resolved) {
|
2018-04-26 14:38:42 +00:00
|
|
|
ASSIMP_LOG_ERROR_F("MD3: Failed to match skin ", (*it).first, " to surface ", (*it).second);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 15:08:03 +00:00
|
|
|
if (!pScene->mNumMeshes) {
|
2020-06-23 19:05:42 +00:00
|
|
|
throw DeadlyImportError("MD3: File contains no valid mesh");
|
2019-03-16 15:08:03 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mNumMaterials = iNumMaterials;
|
|
|
|
|
|
|
|
// Now we need to generate an empty node graph
|
|
|
|
pScene->mRootNode = new aiNode("<MD3Root>");
|
|
|
|
pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
|
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
|
|
|
|
|
|
|
|
// Attach tiny children for all tags
|
|
|
|
if (pcHeader->NUM_TAGS) {
|
|
|
|
pScene->mRootNode->mNumChildren = pcHeader->NUM_TAGS;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mRootNode->mChildren = new aiNode *[pcHeader->NUM_TAGS];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < pcHeader->NUM_TAGS; ++i, ++pcTags) {
|
2020-06-23 19:05:42 +00:00
|
|
|
aiNode *nd = pScene->mRootNode->mChildren[i] = new aiNode();
|
|
|
|
nd->mName.Set((const char *)pcTags->NAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
nd->mParent = pScene->mRootNode;
|
|
|
|
|
|
|
|
AI_SWAP4(pcTags->origin.x);
|
|
|
|
AI_SWAP4(pcTags->origin.y);
|
|
|
|
AI_SWAP4(pcTags->origin.z);
|
|
|
|
|
|
|
|
// Copy local origin, again flip z,y
|
|
|
|
nd->mTransformation.a4 = pcTags->origin.x;
|
|
|
|
nd->mTransformation.b4 = pcTags->origin.y;
|
|
|
|
nd->mTransformation.c4 = pcTags->origin.z;
|
|
|
|
|
|
|
|
// Copy rest of transformation (need to transpose to match row-order matrix)
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < 3; ++a) {
|
|
|
|
for (unsigned int m = 0; m < 3; ++m) {
|
2015-05-19 03:57:13 +00:00
|
|
|
nd->mTransformation[m][a] = pcTags->orientation[a][m];
|
|
|
|
AI_SWAP4(nd->mTransformation[m][a]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < pScene->mNumMeshes; ++i)
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mRootNode->mMeshes[i] = i;
|
|
|
|
|
|
|
|
// Now rotate the whole scene 90 degrees around the x axis to convert to internal coordinate system
|
2019-03-16 15:08:03 +00:00
|
|
|
pScene->mRootNode->mTransformation = aiMatrix4x4(
|
2020-06-23 19:05:42 +00:00
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, -1.f, 0.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_MD3_IMPORTER
|