2015-02-23 13:23:28 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2021-05-01 16:46:12 +00:00
|
|
|
Copyright (c) 2006-2021, assimp team
|
2015-02-23 13:23:28 +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
|
2015-02-23 13:23:28 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-02-23 13:23:28 +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-02-23 13:23:28 +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-02-23 13:23:28 +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-02-23 13:23:28 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file C4DImporter.cpp
|
|
|
|
* @brief Implementation of the Cinema4D importer class.
|
|
|
|
*/
|
2015-03-22 16:53:53 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_C4D_IMPORTER
|
2015-02-23 13:23:28 +00:00
|
|
|
|
|
|
|
// no #ifdefing here, Cinema4D support is carried out in a branch of assimp
|
2015-05-19 03:52:10 +00:00
|
|
|
// where it is turned on in the CMake settings.
|
2015-02-23 13:23:28 +00:00
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
2015-05-19 03:57:13 +00:00
|
|
|
# error C4D support is currently MSVC only
|
2015-05-19 03:52:10 +00:00
|
|
|
#endif
|
2015-02-23 13:23:28 +00:00
|
|
|
|
|
|
|
#include "C4DImporter.h"
|
2016-07-01 12:50:46 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/ai_assert.h>
|
2015-02-23 13:23:28 +00:00
|
|
|
|
|
|
|
#if defined(_M_X64) || defined(__amd64__)
|
2015-05-19 03:57:13 +00:00
|
|
|
# define __C4D_64BIT
|
2015-02-23 13:23:28 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
#define __PC
|
2015-02-23 13:23:28 +00:00
|
|
|
#include "c4d_file.h"
|
|
|
|
#include "default_alien_overloads.h"
|
|
|
|
|
2021-05-01 16:46:12 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
aiString aiStringFrom(cineware::String const & cinestring) {
|
|
|
|
aiString result;
|
|
|
|
cinestring.GetCString(result.data, MAXLEN-1);
|
|
|
|
result.length = static_cast<ai_uint32>(cinestring.GetLength());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
using namespace cineware;
|
2015-02-23 13:23:28 +00:00
|
|
|
|
|
|
|
// overload this function and fill in your own unique data
|
2019-04-03 18:15:55 +00:00
|
|
|
void GetWriterInfo(int &id, String &appname) {
|
2015-05-19 03:57:13 +00:00
|
|
|
id = 2424226;
|
|
|
|
appname = "Open Asset Import Library";
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Assimp {
|
2019-04-03 18:15:55 +00:00
|
|
|
template<> const char* LogFunctions<C4DImporter>::Prefix() {
|
2019-04-02 18:43:03 +00:00
|
|
|
static auto prefix = "C4D: ";
|
|
|
|
return prefix;
|
|
|
|
}
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const aiImporterDesc desc = {
|
2015-05-19 03:57:13 +00:00
|
|
|
"Cinema4D Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportBinaryFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"c4d"
|
2015-02-23 13:23:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-04-03 18:15:55 +00:00
|
|
|
bool C4DImporter::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 == "c4d") {
|
|
|
|
return true;
|
2019-04-03 18:15:55 +00:00
|
|
|
} else if ((!extension.length() || checkSig) && pIOHandler) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// TODO
|
|
|
|
}
|
2019-04-03 18:15:55 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-04-03 18:15:55 +00:00
|
|
|
const aiImporterDesc* C4DImporter::GetInfo () const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return &desc;
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Imports the given file into the given scene structure.
|
2019-04-03 18:15:55 +00:00
|
|
|
void C4DImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) {
|
2016-04-05 21:23:53 +00:00
|
|
|
std::unique_ptr<IOStream> file( pIOHandler->Open( pFile));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2019-04-03 18:15:55 +00:00
|
|
|
if( file.get() == nullptr ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("failed to open file " + pFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t file_size = file->FileSize();
|
|
|
|
|
|
|
|
std::vector<uint8_t> mBuffer(file_size);
|
|
|
|
file->Read(&mBuffer[0], 1, file_size);
|
|
|
|
|
|
|
|
Filename f;
|
|
|
|
f.SetMemoryReadMode(&mBuffer[0], file_size);
|
|
|
|
|
|
|
|
// open document first
|
|
|
|
BaseDocument* doc = LoadDocument(f, SCENEFILTER_OBJECTS | SCENEFILTER_MATERIALS);
|
2019-04-03 18:15:55 +00:00
|
|
|
if(doc == nullptr ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("failed to read document " + pFile);
|
|
|
|
}
|
|
|
|
|
2021-07-28 08:39:39 +00:00
|
|
|
// Generate the root-node
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mRootNode = new aiNode("<C4DRoot>");
|
2021-07-28 08:39:39 +00:00
|
|
|
|
|
|
|
// convert left-handed to right-handed
|
|
|
|
pScene->mRootNode->mTransformation.a1 = 0.01f;
|
|
|
|
pScene->mRootNode->mTransformation.b2 = 0.01f;
|
|
|
|
pScene->mRootNode->mTransformation.c3 = -0.01f;
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// first convert all materials
|
|
|
|
ReadMaterials(doc->GetFirstMaterial());
|
|
|
|
|
2019-04-03 18:15:55 +00:00
|
|
|
// process C4D scene-graph recursively
|
2015-05-19 03:57:13 +00:00
|
|
|
try {
|
|
|
|
RecurseHierarchy(doc->GetFirstObject(), pScene->mRootNode);
|
2019-04-03 18:15:55 +00:00
|
|
|
} catch(...) {
|
2016-04-05 20:53:54 +00:00
|
|
|
for(aiMesh* mesh : meshes) {
|
2015-05-19 03:57:13 +00:00
|
|
|
delete mesh;
|
|
|
|
}
|
|
|
|
BaseDocument::Free(doc);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
BaseDocument::Free(doc);
|
|
|
|
|
|
|
|
// copy meshes over
|
|
|
|
pScene->mNumMeshes = static_cast<unsigned int>(meshes.size());
|
|
|
|
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes]();
|
|
|
|
std::copy(meshes.begin(), meshes.end(), pScene->mMeshes);
|
|
|
|
|
|
|
|
// copy materials over, adding a default material if necessary
|
|
|
|
unsigned int mat_count = static_cast<unsigned int>(materials.size());
|
2016-04-05 20:53:54 +00:00
|
|
|
for(aiMesh* mesh : meshes) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(mesh->mMaterialIndex <= mat_count);
|
|
|
|
if(mesh->mMaterialIndex >= mat_count) {
|
|
|
|
++mat_count;
|
|
|
|
|
2017-12-02 15:23:49 +00:00
|
|
|
std::unique_ptr<aiMaterial> def_material(new aiMaterial());
|
2015-05-19 03:57:13 +00:00
|
|
|
const aiString name(AI_DEFAULT_MATERIAL_NAME);
|
|
|
|
def_material->AddProperty(&name, AI_MATKEY_NAME);
|
|
|
|
|
2017-12-02 15:23:49 +00:00
|
|
|
materials.push_back(def_material.release());
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pScene->mNumMaterials = static_cast<unsigned int>(materials.size());
|
|
|
|
pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials]();
|
|
|
|
std::copy(materials.begin(), materials.end(), pScene->mMaterials);
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-05-01 16:46:12 +00:00
|
|
|
bool C4DImporter::ReadShader(aiMaterial* out, BaseShader* shader) {
|
|
|
|
// based on Cineware sample code (C4DImportExport.cpp)
|
2015-05-19 03:57:13 +00:00
|
|
|
while(shader) {
|
|
|
|
if(shader->GetType() == Xlayer) {
|
|
|
|
BaseContainer* container = shader->GetDataInstance();
|
|
|
|
GeData blend = container->GetData(SLA_LAYER_BLEND);
|
|
|
|
iBlendDataType* blend_list = reinterpret_cast<iBlendDataType*>(blend.GetCustomDataType(CUSTOMDATA_BLEND_LIST));
|
|
|
|
if (!blend_list)
|
|
|
|
{
|
|
|
|
LogWarn("ignoring XLayer shader: no blend list given");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerShaderLayer *lsl = dynamic_cast<LayerShaderLayer*>(blend_list->m_BlendLayers.GetObject(0));
|
|
|
|
|
|
|
|
// Ignore the actual layer blending - models for real-time rendering should not
|
|
|
|
// use them in a non-trivial way. Just try to find textures that we can apply
|
|
|
|
// to the model.
|
2019-04-03 18:15:55 +00:00
|
|
|
while (lsl) {
|
|
|
|
if (lsl->GetType() == TypeFolder) {
|
2015-05-19 03:57:13 +00:00
|
|
|
BlendFolder* const folder = dynamic_cast<BlendFolder*>(lsl);
|
|
|
|
LayerShaderLayer *subLsl = dynamic_cast<LayerShaderLayer*>(folder->m_Children.GetObject(0));
|
|
|
|
|
2019-04-03 18:15:55 +00:00
|
|
|
while (subLsl) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (subLsl->GetType() == TypeShader) {
|
|
|
|
BlendShader* const shader = dynamic_cast<BlendShader*>(subLsl);
|
|
|
|
if(ReadShader(out, static_cast<BaseShader*>(shader->m_pLink->GetLink()))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subLsl = subLsl->GetNext();
|
|
|
|
}
|
2019-04-03 18:15:55 +00:00
|
|
|
} else if (lsl->GetType() == TypeShader) {
|
2015-05-19 03:57:13 +00:00
|
|
|
BlendShader* const shader = dynamic_cast<BlendShader*>(lsl);
|
|
|
|
if(ReadShader(out, static_cast<BaseShader*>(shader->m_pLink->GetLink()))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lsl = lsl->GetNext();
|
|
|
|
}
|
2019-04-03 18:15:55 +00:00
|
|
|
} else if ( shader->GetType() == Xbitmap ) {
|
2021-05-01 16:46:12 +00:00
|
|
|
auto const path = aiStringFrom(shader->GetFileName().GetString());
|
2015-05-19 03:57:13 +00:00
|
|
|
out->AddProperty(&path, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
|
|
|
return true;
|
2019-04-03 18:15:55 +00:00
|
|
|
} else {
|
2021-05-13 11:05:31 +00:00
|
|
|
LogWarn("ignoring shader type: ", GetObjectTypeName(shader->GetType()));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
shader = shader->GetNext();
|
|
|
|
}
|
2019-04-03 18:15:55 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-05-01 16:46:12 +00:00
|
|
|
void C4DImporter::ReadMaterials(BaseMaterial* mat) {
|
|
|
|
// based on Cineware sample code
|
2019-04-03 18:15:55 +00:00
|
|
|
while (mat) {
|
|
|
|
if (mat->GetType() == Mmaterial) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMaterial* out = new aiMaterial();
|
|
|
|
material_mapping[mat] = static_cast<unsigned int>(materials.size());
|
|
|
|
materials.push_back(out);
|
|
|
|
|
2021-05-01 16:46:12 +00:00
|
|
|
auto const ai_name = aiStringFrom(mat->GetName());
|
2015-05-19 03:57:13 +00:00
|
|
|
out->AddProperty(&ai_name, AI_MATKEY_NAME);
|
|
|
|
|
|
|
|
Material& m = dynamic_cast<Material&>(*mat);
|
|
|
|
|
2019-04-03 18:15:55 +00:00
|
|
|
if (m.GetChannelState(CHANNEL_COLOR)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
GeData data;
|
|
|
|
mat->GetParameter(MATERIAL_COLOR_COLOR, data);
|
|
|
|
Vector color = data.GetVector();
|
|
|
|
mat->GetParameter(MATERIAL_COLOR_BRIGHTNESS, data);
|
2016-07-01 15:02:54 +00:00
|
|
|
const Float brightness = data.GetFloat();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
color *= brightness;
|
|
|
|
|
|
|
|
aiVector3D v;
|
|
|
|
v.x = color.x;
|
|
|
|
v.y = color.y;
|
|
|
|
v.z = color.z;
|
|
|
|
out->AddProperty(&v, 1, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseShader* const shader = m.GetShader(MATERIAL_COLOR_SHADER);
|
|
|
|
if(shader) {
|
|
|
|
ReadShader(out, shader);
|
|
|
|
}
|
2019-04-03 18:15:55 +00:00
|
|
|
} else {
|
2021-05-13 11:05:31 +00:00
|
|
|
LogWarn("ignoring plugin material: ", GetObjectTypeName(mat->GetType()));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
mat = mat->GetNext();
|
|
|
|
}
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-04-03 18:15:55 +00:00
|
|
|
void C4DImporter::RecurseHierarchy(BaseObject* object, aiNode* parent) {
|
|
|
|
ai_assert(parent != nullptr );
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector<aiNode*> nodes;
|
|
|
|
|
2021-05-01 16:46:12 +00:00
|
|
|
// based on Cineware sample code
|
2019-04-03 18:15:55 +00:00
|
|
|
while (object) {
|
2015-05-19 03:57:13 +00:00
|
|
|
const LONG type = object->GetType();
|
|
|
|
const Matrix& ml = object->GetMl();
|
|
|
|
|
|
|
|
aiNode* const nd = new aiNode();
|
|
|
|
|
|
|
|
nd->mParent = parent;
|
2021-05-01 16:46:12 +00:00
|
|
|
nd->mName = aiStringFrom(object->GetName());
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
nd->mTransformation.a1 = ml.v1.x;
|
|
|
|
nd->mTransformation.b1 = ml.v1.y;
|
|
|
|
nd->mTransformation.c1 = ml.v1.z;
|
|
|
|
|
|
|
|
nd->mTransformation.a2 = ml.v2.x;
|
|
|
|
nd->mTransformation.b2 = ml.v2.y;
|
|
|
|
nd->mTransformation.c2 = ml.v2.z;
|
|
|
|
|
|
|
|
nd->mTransformation.a3 = ml.v3.x;
|
|
|
|
nd->mTransformation.b3 = ml.v3.y;
|
|
|
|
nd->mTransformation.c3 = ml.v3.z;
|
|
|
|
|
|
|
|
nd->mTransformation.a4 = ml.off.x;
|
|
|
|
nd->mTransformation.b4 = ml.off.y;
|
|
|
|
nd->mTransformation.c4 = ml.off.z;
|
|
|
|
|
|
|
|
nodes.push_back(nd);
|
|
|
|
|
|
|
|
GeData data;
|
2019-04-03 18:15:55 +00:00
|
|
|
if (type == Ocamera) {
|
2015-05-19 03:57:13 +00:00
|
|
|
object->GetParameter(CAMERAOBJECT_FOV, data);
|
|
|
|
// TODO: read camera
|
2019-04-03 18:15:55 +00:00
|
|
|
} else if (type == Olight) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// TODO: read light
|
2019-04-03 18:15:55 +00:00
|
|
|
} else if (type == Opolygon) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMesh* const mesh = ReadMesh(object);
|
2019-04-03 18:15:55 +00:00
|
|
|
if(mesh != nullptr) {
|
2015-05-19 03:57:13 +00:00
|
|
|
nd->mNumMeshes = 1;
|
|
|
|
nd->mMeshes = new unsigned int[1];
|
|
|
|
nd->mMeshes[0] = static_cast<unsigned int>(meshes.size());
|
|
|
|
meshes.push_back(mesh);
|
|
|
|
}
|
2019-04-03 18:15:55 +00:00
|
|
|
} else {
|
2021-05-13 11:05:31 +00:00
|
|
|
LogWarn("ignoring object: ", GetObjectTypeName(type));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RecurseHierarchy(object->GetDown(), nd);
|
|
|
|
object = object->GetNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy nodes over to parent
|
|
|
|
parent->mNumChildren = static_cast<unsigned int>(nodes.size());
|
|
|
|
parent->mChildren = new aiNode*[parent->mNumChildren]();
|
|
|
|
std::copy(nodes.begin(), nodes.end(), parent->mChildren);
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-04-03 18:15:55 +00:00
|
|
|
aiMesh* C4DImporter::ReadMesh(BaseObject* object) {
|
|
|
|
ai_assert(object != nullptr);
|
|
|
|
ai_assert( object->GetType() == Opolygon );
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-05-01 16:46:12 +00:00
|
|
|
// based on Cineware sample code
|
2015-05-19 03:57:13 +00:00
|
|
|
PolygonObject* const polyObject = dynamic_cast<PolygonObject*>(object);
|
2019-04-03 18:15:55 +00:00
|
|
|
ai_assert(polyObject != nullptr);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
const LONG pointCount = polyObject->GetPointCount();
|
|
|
|
const LONG polyCount = polyObject->GetPolygonCount();
|
|
|
|
if(!polyObject || !pointCount) {
|
|
|
|
LogWarn("ignoring mesh with zero vertices or faces");
|
2019-04-03 18:15:55 +00:00
|
|
|
return nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vector* points = polyObject->GetPointR();
|
2019-04-03 18:15:55 +00:00
|
|
|
ai_assert(points != nullptr);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
const CPolygon* polys = polyObject->GetPolygonR();
|
2019-04-03 18:15:55 +00:00
|
|
|
ai_assert(polys != nullptr);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2017-12-02 15:23:49 +00:00
|
|
|
std::unique_ptr<aiMesh> mesh(new aiMesh());
|
2015-05-19 03:57:13 +00:00
|
|
|
mesh->mNumFaces = static_cast<unsigned int>(polyCount);
|
|
|
|
aiFace* face = mesh->mFaces = new aiFace[mesh->mNumFaces]();
|
|
|
|
|
|
|
|
mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
|
|
|
|
mesh->mMaterialIndex = 0;
|
|
|
|
|
|
|
|
unsigned int vcount = 0;
|
|
|
|
|
|
|
|
// first count vertices
|
|
|
|
for (LONG i = 0; i < polyCount; i++)
|
|
|
|
{
|
|
|
|
vcount += 3;
|
|
|
|
|
|
|
|
// TODO: do we also need to handle lines or points with similar checks?
|
|
|
|
if (polys[i].c != polys[i].d)
|
|
|
|
{
|
|
|
|
mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
|
|
|
|
++vcount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ai_assert(vcount > 0);
|
|
|
|
|
|
|
|
mesh->mNumVertices = vcount;
|
|
|
|
aiVector3D* verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
|
|
|
|
aiVector3D* normals, *uvs, *tangents, *bitangents;
|
|
|
|
unsigned int n = 0;
|
|
|
|
|
|
|
|
// check if there are normals, tangents or UVW coordinates
|
|
|
|
BaseTag* tag = object->GetTag(Tnormal);
|
2019-04-03 18:15:55 +00:00
|
|
|
NormalTag* normals_src = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
if(tag) {
|
|
|
|
normals_src = dynamic_cast<NormalTag*>(tag);
|
|
|
|
normals = mesh->mNormals = new aiVector3D[mesh->mNumVertices]();
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = object->GetTag(Ttangent);
|
2019-04-03 18:15:55 +00:00
|
|
|
TangentTag* tangents_src = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
if(tag) {
|
|
|
|
tangents_src = dynamic_cast<TangentTag*>(tag);
|
|
|
|
tangents = mesh->mTangents = new aiVector3D[mesh->mNumVertices]();
|
|
|
|
bitangents = mesh->mBitangents = new aiVector3D[mesh->mNumVertices]();
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = object->GetTag(Tuvw);
|
2019-04-03 18:15:55 +00:00
|
|
|
UVWTag* uvs_src = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
if(tag) {
|
|
|
|
uvs_src = dynamic_cast<UVWTag*>(tag);
|
|
|
|
uvs = mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices]();
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy vertices and extra channels over and populate faces
|
2019-04-03 18:15:55 +00:00
|
|
|
for (LONG i = 0; i < polyCount; ++i, ++face) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(polys[i].a < pointCount && polys[i].a >= 0);
|
|
|
|
const Vector& pointA = points[polys[i].a];
|
|
|
|
verts->x = pointA.x;
|
|
|
|
verts->y = pointA.y;
|
|
|
|
verts->z = pointA.z;
|
|
|
|
++verts;
|
|
|
|
|
|
|
|
ai_assert(polys[i].b < pointCount && polys[i].b >= 0);
|
|
|
|
const Vector& pointB = points[polys[i].b];
|
|
|
|
verts->x = pointB.x;
|
|
|
|
verts->y = pointB.y;
|
|
|
|
verts->z = pointB.z;
|
|
|
|
++verts;
|
|
|
|
|
|
|
|
ai_assert(polys[i].c < pointCount && polys[i].c >= 0);
|
|
|
|
const Vector& pointC = points[polys[i].c];
|
|
|
|
verts->x = pointC.x;
|
|
|
|
verts->y = pointC.y;
|
|
|
|
verts->z = pointC.z;
|
|
|
|
++verts;
|
|
|
|
|
|
|
|
// TODO: do we also need to handle lines or points with similar checks?
|
2019-04-03 18:15:55 +00:00
|
|
|
if (polys[i].c != polys[i].d) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(polys[i].d < pointCount && polys[i].d >= 0);
|
|
|
|
|
|
|
|
face->mNumIndices = 4;
|
|
|
|
mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
|
|
|
|
const Vector& pointD = points[polys[i].d];
|
|
|
|
verts->x = pointD.x;
|
|
|
|
verts->y = pointD.y;
|
|
|
|
verts->z = pointD.z;
|
|
|
|
++verts;
|
2019-04-03 18:15:55 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
face->mNumIndices = 3;
|
|
|
|
}
|
|
|
|
face->mIndices = new unsigned int[face->mNumIndices];
|
|
|
|
for(unsigned int j = 0; j < face->mNumIndices; ++j) {
|
|
|
|
face->mIndices[j] = n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy normals
|
|
|
|
if (normals_src) {
|
2016-07-01 15:08:28 +00:00
|
|
|
if(i >= normals_src->GetDataCount()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
LogError("unexpected number of normals, ignoring");
|
2019-04-03 18:15:55 +00:00
|
|
|
} else {
|
2016-07-01 15:02:54 +00:00
|
|
|
ConstNormalHandle normal_handle = normals_src->GetDataAddressR();
|
|
|
|
NormalStruct nor;
|
|
|
|
NormalTag::Get(normal_handle, i, nor);
|
2015-05-19 03:57:13 +00:00
|
|
|
normals->x = nor.a.x;
|
|
|
|
normals->y = nor.a.y;
|
|
|
|
normals->z = nor.a.z;
|
|
|
|
++normals;
|
|
|
|
|
|
|
|
normals->x = nor.b.x;
|
|
|
|
normals->y = nor.b.y;
|
|
|
|
normals->z = nor.b.z;
|
|
|
|
++normals;
|
|
|
|
|
|
|
|
normals->x = nor.c.x;
|
|
|
|
normals->y = nor.c.y;
|
|
|
|
normals->z = nor.c.z;
|
|
|
|
++normals;
|
|
|
|
|
|
|
|
if(face->mNumIndices == 4) {
|
|
|
|
normals->x = nor.d.x;
|
|
|
|
normals->y = nor.d.y;
|
|
|
|
normals->z = nor.d.z;
|
|
|
|
++normals;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy tangents and bitangents
|
|
|
|
if (tangents_src) {
|
|
|
|
|
|
|
|
for(unsigned int k = 0; k < face->mNumIndices; ++k) {
|
|
|
|
LONG l;
|
|
|
|
switch(k) {
|
|
|
|
case 0:
|
|
|
|
l = polys[i].a;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
l = polys[i].b;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
l = polys[i].c;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
l = polys[i].d;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ai_assert(false);
|
|
|
|
}
|
|
|
|
if(l >= tangents_src->GetDataCount()) {
|
|
|
|
LogError("unexpected number of tangents, ignoring");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tangent tan = tangents_src->GetDataR()[l];
|
|
|
|
tangents->x = tan.vl.x;
|
|
|
|
tangents->y = tan.vl.y;
|
|
|
|
tangents->z = tan.vl.z;
|
|
|
|
++tangents;
|
|
|
|
|
|
|
|
bitangents->x = tan.vr.x;
|
|
|
|
bitangents->y = tan.vr.y;
|
|
|
|
bitangents->z = tan.vr.z;
|
|
|
|
++bitangents;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy UVs
|
|
|
|
if (uvs_src) {
|
|
|
|
if(i >= uvs_src->GetDataCount()) {
|
|
|
|
LogError("unexpected number of UV coordinates, ignoring");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
UVWStruct uvw;
|
|
|
|
uvs_src->Get(uvs_src->GetDataAddressR(),i,uvw);
|
|
|
|
|
|
|
|
uvs->x = uvw.a.x;
|
|
|
|
uvs->y = 1.0f-uvw.a.y;
|
|
|
|
uvs->z = uvw.a.z;
|
|
|
|
++uvs;
|
|
|
|
|
|
|
|
uvs->x = uvw.b.x;
|
|
|
|
uvs->y = 1.0f-uvw.b.y;
|
|
|
|
uvs->z = uvw.b.z;
|
|
|
|
++uvs;
|
|
|
|
|
|
|
|
uvs->x = uvw.c.x;
|
|
|
|
uvs->y = 1.0f-uvw.c.y;
|
|
|
|
uvs->z = uvw.c.z;
|
|
|
|
++uvs;
|
|
|
|
|
|
|
|
if(face->mNumIndices == 4) {
|
|
|
|
uvs->x = uvw.d.x;
|
|
|
|
uvs->y = 1.0f-uvw.d.y;
|
|
|
|
uvs->z = uvw.d.z;
|
|
|
|
++uvs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh->mMaterialIndex = ResolveMaterial(polyObject);
|
2019-04-03 18:15:55 +00:00
|
|
|
|
2017-12-02 15:23:49 +00:00
|
|
|
return mesh.release();
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-04-03 18:15:55 +00:00
|
|
|
unsigned int C4DImporter::ResolveMaterial(PolygonObject* obj) {
|
|
|
|
ai_assert(obj != nullptr);
|
2015-02-23 13:23:28 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
const unsigned int mat_count = static_cast<unsigned int>(materials.size());
|
2015-02-23 13:23:28 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
BaseTag* tag = obj->GetTag(Ttexture);
|
2019-04-03 18:15:55 +00:00
|
|
|
if(tag == nullptr) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return mat_count;
|
|
|
|
}
|
2015-02-23 13:23:28 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
TextureTag& ttag = dynamic_cast<TextureTag&>(*tag);
|
2015-02-23 13:23:28 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
BaseMaterial* const mat = ttag.GetMaterial();
|
2019-04-03 18:15:55 +00:00
|
|
|
ai_assert(mat != nullptr);
|
2015-02-23 13:23:28 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
const MaterialMap::const_iterator it = material_mapping.find(mat);
|
|
|
|
if(it == material_mapping.end()) {
|
|
|
|
return mat_count;
|
|
|
|
}
|
2015-02-23 13:23:28 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert((*it).second < mat_count);
|
2019-04-03 18:15:55 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return (*it).second;
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
2015-04-16 01:13:19 +00:00
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_C4D_IMPORTER
|