2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, 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 Q3DLoader.cpp
|
|
|
|
* @brief Implementation of the Q3D importer class
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_Q3D_IMPORTER
|
|
|
|
|
|
|
|
// internal headers
|
|
|
|
#include "Q3DLoader.h"
|
2021-05-04 23:16:23 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StreamReader.h>
|
|
|
|
#include <assimp/fast_atof.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
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
static const aiImporterDesc desc = {
|
2015-05-19 03:57:13 +00:00
|
|
|
"Quick3D Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"http://www.quick3d.com/",
|
|
|
|
aiImporterFlags_SupportBinaryFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"q3o q3s"
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
2020-06-23 19:05:42 +00:00
|
|
|
Q3DImporter::Q3DImporter() {
|
|
|
|
// empty
|
|
|
|
}
|
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
|
|
|
Q3DImporter::~Q3DImporter() {
|
|
|
|
// empty
|
|
|
|
}
|
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.
|
2021-04-23 22:17:50 +00:00
|
|
|
bool Q3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
|
|
|
|
static const char *tokens[] = { "quick3Do", "quick3Ds" };
|
2021-05-04 22:08:54 +00:00
|
|
|
return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiImporterDesc *Q3DImporter::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 Q3DImporter::InternReadFile(const std::string &pFile,
|
|
|
|
aiScene *pScene, IOSystem *pIOHandler) {
|
2021-05-05 21:31:50 +00:00
|
|
|
|
|
|
|
auto file = pIOHandler->Open(pFile, "rb");
|
|
|
|
if (!file)
|
|
|
|
throw DeadlyImportError("Quick3D: Could not open ", pFile);
|
|
|
|
|
|
|
|
StreamReaderLE stream(file);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// The header is 22 bytes large
|
|
|
|
if (stream.GetRemainingSize() < 22)
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("File is either empty or corrupt: ", pFile);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check the file's signature
|
2020-06-23 19:05:42 +00:00
|
|
|
if (ASSIMP_strincmp((const char *)stream.GetPtr(), "quick3Do", 8) &&
|
|
|
|
ASSIMP_strincmp((const char *)stream.GetPtr(), "quick3Ds", 8)) {
|
2021-05-04 23:16:23 +00:00
|
|
|
throw DeadlyImportError("Not a Quick3D file. Signature string is: ", ai_str_toprintable((const char *)stream.GetPtr(), 8));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print the file format version
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_INFO("Quick3D File format version: ",
|
2020-06-23 19:05:42 +00:00
|
|
|
std::string(&((const char *)stream.GetPtr())[8], 2));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// ... an store it
|
2020-06-23 19:05:42 +00:00
|
|
|
char major = ((const char *)stream.GetPtr())[8];
|
|
|
|
char minor = ((const char *)stream.GetPtr())[9];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
stream.IncPtr(10);
|
2020-06-23 19:05:42 +00:00
|
|
|
unsigned int numMeshes = (unsigned int)stream.GetI4();
|
|
|
|
unsigned int numMats = (unsigned int)stream.GetI4();
|
|
|
|
unsigned int numTextures = (unsigned int)stream.GetI4();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
std::vector<Material> materials;
|
2022-07-05 20:50:54 +00:00
|
|
|
try {
|
|
|
|
materials.reserve(numMats);
|
2022-07-05 21:01:18 +00:00
|
|
|
} catch(const std::bad_alloc&) {
|
2022-07-05 20:50:54 +00:00
|
|
|
ASSIMP_LOG_ERROR("Invalid alloc for materials.");
|
|
|
|
throw DeadlyImportError("Invalid Quick3D-file, material allocation failed.");
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
std::vector<Mesh> meshes;
|
2022-07-05 20:50:54 +00:00
|
|
|
try {
|
|
|
|
meshes.reserve(numMeshes);
|
2022-07-05 21:01:18 +00:00
|
|
|
} catch(const std::bad_alloc&) {
|
2022-07-05 20:50:54 +00:00
|
|
|
ASSIMP_LOG_ERROR("Invalid alloc for meshes.");
|
|
|
|
throw DeadlyImportError("Invalid Quick3D-file, mesh allocation failed.");
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Allocate the scene root node
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiColor3D fgColor(0.6f, 0.6f, 0.6f);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Now read all file chunks
|
2020-06-23 19:05:42 +00:00
|
|
|
while (true) {
|
|
|
|
if (stream.GetRemainingSize() < 1) break;
|
2015-05-19 03:57:13 +00:00
|
|
|
char c = stream.GetI1();
|
2020-06-23 19:05:42 +00:00
|
|
|
switch (c) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Meshes chunk
|
2020-06-23 19:05:42 +00:00
|
|
|
case 'm': {
|
|
|
|
for (unsigned int quak = 0; quak < numMeshes; ++quak) {
|
|
|
|
meshes.push_back(Mesh());
|
|
|
|
Mesh &mesh = meshes.back();
|
|
|
|
|
|
|
|
// read all vertices
|
|
|
|
unsigned int numVerts = (unsigned int)stream.GetI4();
|
|
|
|
if (!numVerts)
|
|
|
|
throw DeadlyImportError("Quick3D: Found mesh with zero vertices");
|
|
|
|
|
|
|
|
std::vector<aiVector3D> &verts = mesh.verts;
|
|
|
|
verts.resize(numVerts);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < numVerts; ++i) {
|
|
|
|
verts[i].x = stream.GetF4();
|
|
|
|
verts[i].y = stream.GetF4();
|
|
|
|
verts[i].z = stream.GetF4();
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// read all faces
|
|
|
|
numVerts = (unsigned int)stream.GetI4();
|
|
|
|
if (!numVerts)
|
|
|
|
throw DeadlyImportError("Quick3D: Found mesh with zero faces");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<Face> &faces = mesh.faces;
|
|
|
|
faces.reserve(numVerts);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// number of indices
|
|
|
|
for (unsigned int i = 0; i < numVerts; ++i) {
|
|
|
|
faces.push_back(Face(stream.GetI2()));
|
|
|
|
if (faces.back().indices.empty())
|
|
|
|
throw DeadlyImportError("Quick3D: Found face with zero indices");
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// indices
|
|
|
|
for (unsigned int i = 0; i < numVerts; ++i) {
|
|
|
|
Face &vec = faces[i];
|
|
|
|
for (unsigned int a = 0; a < (unsigned int)vec.indices.size(); ++a)
|
|
|
|
vec.indices[a] = stream.GetI4();
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// material indices
|
|
|
|
for (unsigned int i = 0; i < numVerts; ++i) {
|
|
|
|
faces[i].mat = (unsigned int)stream.GetI4();
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// read all normals
|
|
|
|
numVerts = (unsigned int)stream.GetI4();
|
|
|
|
std::vector<aiVector3D> &normals = mesh.normals;
|
|
|
|
normals.resize(numVerts);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < numVerts; ++i) {
|
|
|
|
normals[i].x = stream.GetF4();
|
|
|
|
normals[i].y = stream.GetF4();
|
|
|
|
normals[i].z = stream.GetF4();
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
numVerts = (unsigned int)stream.GetI4();
|
|
|
|
if (numTextures && numVerts) {
|
|
|
|
// read all texture coordinates
|
|
|
|
std::vector<aiVector3D> &uv = mesh.uv;
|
|
|
|
uv.resize(numVerts);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < numVerts; ++i) {
|
|
|
|
uv[i].x = stream.GetF4();
|
|
|
|
uv[i].y = stream.GetF4();
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// UV indices
|
|
|
|
for (unsigned int i = 0; i < (unsigned int)faces.size(); ++i) {
|
|
|
|
Face &vec = faces[i];
|
|
|
|
for (unsigned int a = 0; a < (unsigned int)vec.indices.size(); ++a) {
|
|
|
|
vec.uvindices[a] = stream.GetI4();
|
|
|
|
if (!i && !a)
|
|
|
|
mesh.prevUVIdx = vec.uvindices[a];
|
|
|
|
else if (vec.uvindices[a] != mesh.prevUVIdx)
|
|
|
|
mesh.prevUVIdx = UINT_MAX;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
|
|
|
|
// we don't need the rest, but we need to get to the next chunk
|
|
|
|
stream.IncPtr(36);
|
|
|
|
if (minor > '0' && major == '3')
|
|
|
|
stream.IncPtr(mesh.faces.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
// stream.IncPtr(4); // unknown value here
|
|
|
|
} break;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// materials chunk
|
|
|
|
case 'c':
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < numMats; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
materials.push_back(Material());
|
2020-06-23 19:05:42 +00:00
|
|
|
Material &mat = materials.back();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read the material name
|
2020-03-08 20:24:01 +00:00
|
|
|
c = stream.GetI1();
|
|
|
|
while (c) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mat.name.data[mat.name.length++] = c;
|
2020-03-08 20:24:01 +00:00
|
|
|
c = stream.GetI1();
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// add the terminal character
|
|
|
|
mat.name.data[mat.name.length] = '\0';
|
|
|
|
|
|
|
|
// read the ambient color
|
|
|
|
mat.ambient.r = stream.GetF4();
|
|
|
|
mat.ambient.g = stream.GetF4();
|
|
|
|
mat.ambient.b = stream.GetF4();
|
|
|
|
|
|
|
|
// read the diffuse color
|
|
|
|
mat.diffuse.r = stream.GetF4();
|
|
|
|
mat.diffuse.g = stream.GetF4();
|
|
|
|
mat.diffuse.b = stream.GetF4();
|
|
|
|
|
|
|
|
// read the ambient color
|
|
|
|
mat.specular.r = stream.GetF4();
|
|
|
|
mat.specular.g = stream.GetF4();
|
|
|
|
mat.specular.b = stream.GetF4();
|
|
|
|
|
|
|
|
// read the transparency
|
|
|
|
mat.transparency = stream.GetF4();
|
|
|
|
|
|
|
|
// unknown value here
|
|
|
|
// stream.IncPtr(4);
|
|
|
|
// FIX: it could be the texture index ...
|
|
|
|
mat.texIdx = (unsigned int)stream.GetI4();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// texture chunk
|
|
|
|
case 't':
|
|
|
|
|
|
|
|
pScene->mNumTextures = numTextures;
|
2018-12-30 14:59:15 +00:00
|
|
|
if (!numTextures) {
|
|
|
|
break;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mTextures = new aiTexture *[pScene->mNumTextures];
|
2015-05-19 03:57:13 +00:00
|
|
|
// to make sure we won't crash if we leave through an exception
|
2020-06-23 19:05:42 +00:00
|
|
|
::memset(pScene->mTextures, 0, sizeof(void *) * pScene->mNumTextures);
|
2018-12-30 14:59:15 +00:00
|
|
|
for (unsigned int i = 0; i < pScene->mNumTextures; ++i) {
|
2020-06-23 19:05:42 +00:00
|
|
|
aiTexture *tex = pScene->mTextures[i] = new aiTexture;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// skip the texture name
|
2020-06-23 19:05:42 +00:00
|
|
|
while (stream.GetI1())
|
|
|
|
;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// read texture width and height
|
2020-06-23 19:05:42 +00:00
|
|
|
tex->mWidth = (unsigned int)stream.GetI4();
|
2015-05-19 03:57:13 +00:00
|
|
|
tex->mHeight = (unsigned int)stream.GetI4();
|
|
|
|
|
2018-12-30 14:59:15 +00:00
|
|
|
if (!tex->mWidth || !tex->mHeight) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("Quick3D: Invalid texture. Width or height is zero");
|
2018-12-30 14:59:15 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
unsigned int mul = tex->mWidth * tex->mHeight;
|
2020-06-23 19:05:42 +00:00
|
|
|
aiTexel *begin = tex->pcData = new aiTexel[mul];
|
|
|
|
aiTexel *const end = &begin[mul - 1] + 1;
|
2018-12-30 14:59:15 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (; begin != end; ++begin) {
|
2015-05-19 03:57:13 +00:00
|
|
|
begin->r = stream.GetI1();
|
|
|
|
begin->g = stream.GetI1();
|
|
|
|
begin->b = stream.GetI1();
|
|
|
|
begin->a = 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// scene chunk
|
2020-06-23 19:05:42 +00:00
|
|
|
case 's': {
|
|
|
|
// skip position and rotation
|
|
|
|
stream.IncPtr(12);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
|
|
|
for (unsigned int a = 0; a < 4; ++a)
|
|
|
|
pScene->mRootNode->mTransformation[i][a] = stream.GetF4();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
stream.IncPtr(16);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// now setup a single camera
|
|
|
|
pScene->mNumCameras = 1;
|
|
|
|
pScene->mCameras = new aiCamera *[1];
|
|
|
|
aiCamera *cam = pScene->mCameras[0] = new aiCamera();
|
|
|
|
cam->mPosition.x = stream.GetF4();
|
|
|
|
cam->mPosition.y = stream.GetF4();
|
|
|
|
cam->mPosition.z = stream.GetF4();
|
|
|
|
cam->mName.Set("Q3DCamera");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// skip eye rotation for the moment
|
|
|
|
stream.IncPtr(12);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// read the default material color
|
|
|
|
fgColor.r = stream.GetF4();
|
|
|
|
fgColor.g = stream.GetF4();
|
|
|
|
fgColor.b = stream.GetF4();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// skip some unimportant properties
|
|
|
|
stream.IncPtr(29);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// setup a single point light with no attenuation
|
|
|
|
pScene->mNumLights = 1;
|
|
|
|
pScene->mLights = new aiLight *[1];
|
|
|
|
aiLight *light = pScene->mLights[0] = new aiLight();
|
|
|
|
light->mName.Set("Q3DLight");
|
|
|
|
light->mType = aiLightSource_POINT;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
light->mAttenuationConstant = 1;
|
|
|
|
light->mAttenuationLinear = 0;
|
|
|
|
light->mAttenuationQuadratic = 0;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
light->mColorDiffuse.r = stream.GetF4();
|
|
|
|
light->mColorDiffuse.g = stream.GetF4();
|
|
|
|
light->mColorDiffuse.b = stream.GetF4();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
light->mColorSpecular = light->mColorDiffuse;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// We don't need the rest, but we need to know where this chunk ends.
|
|
|
|
unsigned int temp = (unsigned int)(stream.GetI4() * stream.GetI4());
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// skip the background file name
|
|
|
|
while (stream.GetI1())
|
|
|
|
;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// skip background texture data + the remaining fields
|
|
|
|
stream.IncPtr(temp * 3 + 20); // 4 bytes of unknown data here
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// TODO
|
|
|
|
goto outer;
|
|
|
|
} break;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw DeadlyImportError("Quick3D: Unknown chunk");
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
outer:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// If we have no mesh loaded - break here
|
|
|
|
if (meshes.empty())
|
|
|
|
throw DeadlyImportError("Quick3D: No meshes loaded");
|
|
|
|
|
|
|
|
// If we have no materials loaded - generate a default mat
|
2020-06-23 19:05:42 +00:00
|
|
|
if (materials.empty()) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_INFO("Quick3D: No material found, generating one");
|
2015-05-19 03:57:13 +00:00
|
|
|
materials.push_back(Material());
|
2020-06-23 19:05:42 +00:00
|
|
|
materials.back().diffuse = fgColor;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// find out which materials we'll need
|
|
|
|
typedef std::pair<unsigned int, unsigned int> FaceIdx;
|
2020-06-23 19:05:42 +00:00
|
|
|
typedef std::vector<FaceIdx> FaceIdxArray;
|
|
|
|
FaceIdxArray *fidx = new FaceIdxArray[materials.size()];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
unsigned int p = 0;
|
|
|
|
for (std::vector<Mesh>::iterator it = meshes.begin(), end = meshes.end();
|
2020-06-23 19:05:42 +00:00
|
|
|
it != end; ++it, ++p) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int q = 0;
|
|
|
|
for (std::vector<Face>::iterator fit = (*it).faces.begin(), fend = (*it).faces.end();
|
2020-06-23 19:05:42 +00:00
|
|
|
fit != fend; ++fit, ++q) {
|
|
|
|
if ((*fit).mat >= materials.size()) {
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_WARN("Quick3D: Material index overflow");
|
2015-05-19 03:57:13 +00:00
|
|
|
(*fit).mat = 0;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
if (fidx[(*fit).mat].empty()) ++pScene->mNumMeshes;
|
|
|
|
fidx[(*fit).mat].push_back(FaceIdx(p, q));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pScene->mNumMaterials = pScene->mNumMeshes;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
|
|
|
|
pScene->mMeshes = new aiMesh *[pScene->mNumMaterials];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0, real = 0; i < (unsigned int)materials.size(); ++i) {
|
|
|
|
if (fidx[i].empty()) continue;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Allocate a mesh and a material
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *mesh = pScene->mMeshes[real] = new aiMesh();
|
|
|
|
aiMaterial *mat = new aiMaterial();
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mMaterials[real] = mat;
|
|
|
|
|
|
|
|
mesh->mMaterialIndex = real;
|
|
|
|
|
|
|
|
// Build the output material
|
2020-06-23 19:05:42 +00:00
|
|
|
Material &srcMat = materials[i];
|
|
|
|
mat->AddProperty(&srcMat.diffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
mat->AddProperty(&srcMat.specular, 1, AI_MATKEY_COLOR_SPECULAR);
|
|
|
|
mat->AddProperty(&srcMat.ambient, 1, AI_MATKEY_COLOR_AMBIENT);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// NOTE: Ignore transparency for the moment - it seems
|
|
|
|
// unclear how to interpret the data
|
2015-05-19 03:48:29 +00:00
|
|
|
#if 0
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!(minor > '0' && major == '3'))
|
|
|
|
srcMat.transparency = 1.0f - srcMat.transparency;
|
|
|
|
mat->AddProperty(&srcMat.transparency, 1, AI_MATKEY_OPACITY);
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// add shininess - Quick3D seems to use it ins its viewer
|
|
|
|
srcMat.transparency = 16.f;
|
|
|
|
mat->AddProperty(&srcMat.transparency, 1, AI_MATKEY_SHININESS);
|
|
|
|
|
|
|
|
int m = (int)aiShadingMode_Phong;
|
|
|
|
mat->AddProperty(&m, 1, AI_MATKEY_SHADING_MODEL);
|
|
|
|
|
|
|
|
if (srcMat.name.length)
|
2020-06-23 19:05:42 +00:00
|
|
|
mat->AddProperty(&srcMat.name, AI_MATKEY_NAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Add a texture
|
2020-06-23 19:05:42 +00:00
|
|
|
if (srcMat.texIdx < pScene->mNumTextures || real < pScene->mNumTextures) {
|
2015-05-19 03:57:13 +00:00
|
|
|
srcMat.name.data[0] = '*';
|
2020-06-23 19:05:42 +00:00
|
|
|
srcMat.name.length = ASSIMP_itoa10(&srcMat.name.data[1], 1000,
|
|
|
|
(srcMat.texIdx < pScene->mNumTextures ? srcMat.texIdx : real));
|
|
|
|
mat->AddProperty(&srcMat.name, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mesh->mNumFaces = (unsigned int)fidx[i].size();
|
2020-06-23 19:05:42 +00:00
|
|
|
aiFace *faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Now build the output mesh. First find out how many
|
|
|
|
// vertices we'll need
|
2020-06-23 19:05:42 +00:00
|
|
|
for (FaceIdxArray::const_iterator it = fidx[i].begin(), end = fidx[i].end();
|
|
|
|
it != end; ++it) {
|
|
|
|
mesh->mNumVertices += (unsigned int)meshes[(*it).first].faces[(*it).second].indices.size();
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
|
|
|
|
aiVector3D *norms = mesh->mNormals = new aiVector3D[mesh->mNumVertices];
|
|
|
|
aiVector3D *uv = nullptr;
|
|
|
|
if (real < pScene->mNumTextures) {
|
|
|
|
uv = mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
|
|
|
|
mesh->mNumUVComponents[0] = 2;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the final array
|
|
|
|
unsigned int cnt = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (FaceIdxArray::const_iterator it = fidx[i].begin(), end = fidx[i].end();
|
|
|
|
it != end; ++it, ++faces) {
|
|
|
|
Mesh &curMesh = meshes[(*it).first];
|
2020-03-01 12:15:45 +00:00
|
|
|
Face &face = curMesh.faces[(*it).second];
|
2015-05-19 03:57:13 +00:00
|
|
|
faces->mNumIndices = (unsigned int)face.indices.size();
|
2020-06-23 19:05:42 +00:00
|
|
|
faces->mIndices = new unsigned int[faces->mNumIndices];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
aiVector3D faceNormal;
|
|
|
|
bool fnOK = false;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int n = 0; n < faces->mNumIndices; ++n, ++cnt, ++norms, ++verts) {
|
|
|
|
if (face.indices[n] >= curMesh.verts.size()) {
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_WARN("Quick3D: Vertex index overflow");
|
2015-05-19 03:57:13 +00:00
|
|
|
face.indices[n] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy vertices
|
2020-03-01 12:15:45 +00:00
|
|
|
*verts = curMesh.verts[face.indices[n]];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (face.indices[n] >= curMesh.normals.size() && faces->mNumIndices >= 3) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// we have no normal here - assign the face normal
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!fnOK) {
|
2020-03-01 12:15:45 +00:00
|
|
|
const aiVector3D &pV1 = curMesh.verts[face.indices[0]];
|
|
|
|
const aiVector3D &pV2 = curMesh.verts[face.indices[1]];
|
|
|
|
const aiVector3D &pV3 = curMesh.verts[face.indices.size() - 1];
|
2015-05-19 03:57:13 +00:00
|
|
|
faceNormal = (pV2 - pV1) ^ (pV3 - pV1).Normalize();
|
|
|
|
fnOK = true;
|
|
|
|
}
|
|
|
|
*norms = faceNormal;
|
2020-03-01 12:15:45 +00:00
|
|
|
} else {
|
|
|
|
*norms = curMesh.normals[face.indices[n]];
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy texture coordinates
|
2020-06-23 19:05:42 +00:00
|
|
|
if (uv && curMesh.uv.size()) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (curMesh.prevUVIdx != 0xffffffff && curMesh.uv.size() >= curMesh.verts.size()) // workaround
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
*uv = curMesh.uv[face.indices[n]];
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
|
|
|
if (face.uvindices[n] >= curMesh.uv.size()) {
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_WARN("Quick3D: Texture coordinate index overflow");
|
2015-05-19 03:57:13 +00:00
|
|
|
face.uvindices[n] = 0;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
*uv = curMesh.uv[face.uvindices[n]];
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
uv->y = 1.f - uv->y;
|
|
|
|
++uv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the new vertex index
|
|
|
|
faces->mIndices[n] = cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++real;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete our nice helper array
|
|
|
|
delete[] fidx;
|
|
|
|
|
|
|
|
// Now we need to attach the meshes to the root node of the scene
|
|
|
|
pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
|
|
|
|
for (unsigned int i = 0; i < pScene->mNumMeshes; ++i)
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mRootNode->mMeshes[i] = i;
|
|
|
|
|
|
|
|
/*pScene->mRootNode->mTransformation *= aiMatrix4x4(
|
|
|
|
1.f, 0.f, 0.f, 0.f,
|
|
|
|
0.f, -1.f,0.f, 0.f,
|
|
|
|
0.f, 0.f, 1.f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f);*/
|
|
|
|
|
|
|
|
// Add cameras and light sources to the scene root node
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mRootNode->mNumChildren = pScene->mNumLights + pScene->mNumCameras;
|
|
|
|
if (pScene->mRootNode->mNumChildren) {
|
|
|
|
pScene->mRootNode->mChildren = new aiNode *[pScene->mRootNode->mNumChildren];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// the light source
|
2020-06-23 19:05:42 +00:00
|
|
|
aiNode *nd = pScene->mRootNode->mChildren[0] = new aiNode();
|
2015-05-19 03:57:13 +00:00
|
|
|
nd->mParent = pScene->mRootNode;
|
|
|
|
nd->mName.Set("Q3DLight");
|
|
|
|
nd->mTransformation = pScene->mRootNode->mTransformation;
|
|
|
|
nd->mTransformation.Inverse();
|
|
|
|
|
|
|
|
// camera
|
|
|
|
nd = pScene->mRootNode->mChildren[1] = new aiNode();
|
|
|
|
nd->mParent = pScene->mRootNode;
|
|
|
|
nd->mName.Set("Q3DCamera");
|
|
|
|
nd->mTransformation = pScene->mRootNode->mChildren[0]->mTransformation;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_Q3D_IMPORTER
|