2014-07-23 16:27:22 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2024-02-23 21:30:05 +00:00
|
|
|
Copyright (c) 2006-2024, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2014-07-23 16:27:22 +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
|
2014-07-23 16:27:22 +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
|
2014-07-23 16:27:22 +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
|
2014-07-23 16:27:22 +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
|
2014-07-23 16:27:22 +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
|
2014-07-23 16:27:22 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_EXPORT
|
|
|
|
#ifndef ASSIMP_BUILD_NO_3DS_EXPORTER
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
#include "AssetLib/3DS/3DSExporter.h"
|
|
|
|
#include "AssetLib/3DS/3DSHelper.h"
|
|
|
|
#include "AssetLib/3DS/3DSLoader.h"
|
2019-06-10 21:26:00 +00:00
|
|
|
#include "PostProcessing/SplitLargeMeshes.h"
|
|
|
|
|
2017-06-21 12:31:37 +00:00
|
|
|
#include <assimp/SceneCombiner.h>
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StringComparison.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
#include <assimp/Exporter.hpp>
|
2024-08-13 09:17:45 +00:00
|
|
|
#include <assimp/Exceptional.h>
|
2020-05-02 13:14:38 +00:00
|
|
|
#include <assimp/IOSystem.hpp>
|
2019-06-10 21:26:00 +00:00
|
|
|
|
2015-04-15 23:00:17 +00:00
|
|
|
#include <memory>
|
2014-07-23 16:27:22 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
namespace Assimp {
|
2021-09-13 20:38:20 +00:00
|
|
|
|
2016-11-02 18:54:53 +00:00
|
|
|
using namespace D3DS;
|
2014-07-23 16:27:22 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Scope utility to write a 3DS file chunk.
|
|
|
|
//
|
|
|
|
// Upon construction, the chunk header is written with the chunk type (flags)
|
|
|
|
// filled out, but the chunk size left empty. Upon destruction, the correct chunk
|
|
|
|
// size based on the then-position of the output stream cursor is filled in.
|
|
|
|
class ChunkWriter {
|
|
|
|
enum {
|
|
|
|
CHUNK_SIZE_NOT_SET = 0xdeadbeef,
|
|
|
|
SIZE_OFFSET = 2
|
|
|
|
};
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
public:
|
|
|
|
ChunkWriter(StreamWriterLE &writer, uint16_t chunk_type) :
|
|
|
|
writer(writer) {
|
|
|
|
chunk_start_pos = writer.GetCurrentPos();
|
|
|
|
writer.PutU2(chunk_type);
|
|
|
|
writer.PutU4((uint32_t)CHUNK_SIZE_NOT_SET);
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
~ChunkWriter() {
|
|
|
|
std::size_t head_pos = writer.GetCurrentPos();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
ai_assert(head_pos > chunk_start_pos);
|
|
|
|
const std::size_t chunk_size = head_pos - chunk_start_pos;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
writer.SetCurrentPos(chunk_start_pos + SIZE_OFFSET);
|
|
|
|
writer.PutU4(static_cast<uint32_t>(chunk_size));
|
|
|
|
writer.SetCurrentPos(head_pos);
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
private:
|
|
|
|
StreamWriterLE &writer;
|
|
|
|
std::size_t chunk_start_pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Return an unique name for a given |mesh| attached to |node| that
|
|
|
|
// preserves the mesh's given name if it has one. |index| is the index
|
|
|
|
// of the mesh in |aiScene::mMeshes|.
|
|
|
|
std::string GetMeshName(const aiMesh &mesh, unsigned int index, const aiNode &node) {
|
2021-05-04 08:57:30 +00:00
|
|
|
static const char underscore = '_';
|
2020-05-02 13:14:38 +00:00
|
|
|
char postfix[10] = { 0 };
|
|
|
|
ASSIMP_itoa10(postfix, index);
|
|
|
|
|
|
|
|
std::string result = node.mName.C_Str();
|
|
|
|
if (mesh.mName.length > 0) {
|
2021-05-04 08:57:30 +00:00
|
|
|
result += underscore;
|
|
|
|
result += mesh.mName.C_Str();
|
2020-05-02 13:14:38 +00:00
|
|
|
}
|
|
|
|
return result + underscore + postfix;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
// Return an unique name for a given |mat| with original position |index|
|
|
|
|
// in |aiScene::mMaterials|. The name preserves the original material
|
|
|
|
// name if possible.
|
|
|
|
std::string GetMaterialName(const aiMaterial &mat, unsigned int index) {
|
|
|
|
static const std::string underscore = "_";
|
|
|
|
char postfix[10] = { 0 };
|
|
|
|
ASSIMP_itoa10(postfix, index);
|
|
|
|
|
|
|
|
aiString mat_name;
|
|
|
|
if (AI_SUCCESS == mat.Get(AI_MATKEY_NAME, mat_name)) {
|
|
|
|
return mat_name.C_Str() + underscore + postfix;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
return "Material" + underscore + postfix;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
// Collect world transformations for each node
|
|
|
|
void CollectTrafos(const aiNode *node, std::map<const aiNode *, aiMatrix4x4> &trafos) {
|
|
|
|
const aiMatrix4x4 &parent = node->mParent ? trafos[node->mParent] : aiMatrix4x4();
|
|
|
|
trafos[node] = parent * node->mTransformation;
|
|
|
|
for (unsigned int i = 0; i < node->mNumChildren; ++i) {
|
|
|
|
CollectTrafos(node->mChildren[i], trafos);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-05-02 13:14:38 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
// Generate a flat list of the meshes (by index) assigned to each node
|
|
|
|
void CollectMeshes(const aiNode *node, std::multimap<const aiNode *, unsigned int> &meshes) {
|
|
|
|
for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
|
|
|
|
meshes.insert(std::make_pair(node, node->mMeshes[i]));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-05-02 13:14:38 +00:00
|
|
|
for (unsigned int i = 0; i < node->mNumChildren; ++i) {
|
|
|
|
CollectMeshes(node->mChildren[i], meshes);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
2020-05-02 13:14:38 +00:00
|
|
|
} // namespace
|
2014-07-23 16:27:22 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Worker function for exporting a scene to 3DS. Prototyped and registered in Exporter.cpp
|
2020-05-02 13:14:38 +00:00
|
|
|
void ExportScene3DS(const char *pFile, IOSystem *pIOSystem, const aiScene *pScene, const ExportProperties * /*pProperties*/) {
|
|
|
|
std::shared_ptr<IOStream> outfile(pIOSystem->Open(pFile, "wb"));
|
|
|
|
if (!outfile) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyExportError("Could not open output .3ds file: " + std::string(pFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This extra copy should be avoided and all of this made a preprocess
|
|
|
|
// requirement of the 3DS exporter.
|
|
|
|
//
|
|
|
|
// 3DS meshes can be max 0xffff (16 Bit) vertices and faces, respectively.
|
|
|
|
// SplitLargeMeshes can do this, but it requires the correct limit to be set
|
|
|
|
// which is not possible with the current way of specifying preprocess steps
|
|
|
|
// in |Exporter::ExportFormatEntry|.
|
2020-05-02 13:14:38 +00:00
|
|
|
aiScene *scenecopy_tmp;
|
|
|
|
SceneCombiner::CopyScene(&scenecopy_tmp, pScene);
|
2016-04-05 20:56:11 +00:00
|
|
|
std::unique_ptr<aiScene> scenecopy(scenecopy_tmp);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
SplitLargeMeshesProcess_Triangle tri_splitter;
|
|
|
|
tri_splitter.SetLimit(0xffff);
|
|
|
|
tri_splitter.Execute(scenecopy.get());
|
|
|
|
|
|
|
|
SplitLargeMeshesProcess_Vertex vert_splitter;
|
|
|
|
vert_splitter.SetLimit(0xffff);
|
|
|
|
vert_splitter.Execute(scenecopy.get());
|
|
|
|
|
|
|
|
// Invoke the actual exporter
|
|
|
|
Discreet3DSExporter exporter(outfile, scenecopy.get());
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end of namespace Assimp
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
Discreet3DSExporter::Discreet3DSExporter(std::shared_ptr<IOStream> &outfile, const aiScene *scene) :
|
|
|
|
scene(scene), writer(outfile) {
|
2015-05-19 03:57:13 +00:00
|
|
|
CollectTrafos(scene->mRootNode, trafos);
|
|
|
|
CollectMeshes(scene->mRootNode, meshes);
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curRootChunk(writer, Discreet3DS::CHUNK_MAIN);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_OBJMESH);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteMaterials();
|
|
|
|
WriteMeshes();
|
|
|
|
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk1(writer, Discreet3DS::CHUNK_MASTER_SCALE);
|
2015-05-19 03:57:13 +00:00
|
|
|
writer.PutF4(1.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_KEYFRAMER);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteHierarchy(*scene->mRootNode, -1, -1);
|
|
|
|
}
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:36:17 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2022-08-25 16:20:13 +00:00
|
|
|
Discreet3DSExporter::~Discreet3DSExporter() = default;
|
2017-11-20 21:36:17 +00:00
|
|
|
|
2014-07-23 20:08:35 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
int Discreet3DSExporter::WriteHierarchy(const aiNode &node, int seq, int sibling_level) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// 3DS scene hierarchy is serialized as in http://www.martinreddy.net/gfx/3d/3DS.spec
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curRootChunk(writer, Discreet3DS::CHUNK_TRACKINFO);
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_TRACKOBJNAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Assimp node names are unique and distinct from all mesh-node
|
|
|
|
// names we generate; thus we can use them as-is
|
|
|
|
WriteString(node.mName);
|
|
|
|
|
|
|
|
// Two unknown int16 values - it is even unclear if 0 is a safe value
|
|
|
|
// but luckily importers do not know better either.
|
|
|
|
writer.PutI4(0);
|
|
|
|
|
|
|
|
int16_t hierarchy_pos = static_cast<int16_t>(seq);
|
|
|
|
if (sibling_level != -1) {
|
2020-05-02 13:14:38 +00:00
|
|
|
hierarchy_pos = (uint16_t)sibling_level;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the hierarchy position
|
|
|
|
writer.PutI2(hierarchy_pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: write transformation chunks
|
|
|
|
|
|
|
|
++seq;
|
|
|
|
sibling_level = seq;
|
|
|
|
|
|
|
|
// Write all children
|
|
|
|
for (unsigned int i = 0; i < node.mNumChildren; ++i) {
|
|
|
|
seq = WriteHierarchy(*node.mChildren[i], seq, i == 0 ? -1 : sibling_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write all meshes as separate nodes to be able to reference the meshes by name
|
|
|
|
for (unsigned int i = 0; i < node.mNumMeshes; ++i) {
|
|
|
|
const bool first_child = node.mNumChildren == 0 && i == 0;
|
|
|
|
|
|
|
|
const unsigned int mesh_idx = node.mMeshes[i];
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiMesh &mesh = *scene->mMeshes[mesh_idx];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_TRACKINFO);
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
|
|
|
ChunkWriter chunk(writer, Discreet3DS::CHUNK_TRACKOBJNAME);
|
|
|
|
WriteString(GetMeshName(mesh, mesh_idx, node));
|
|
|
|
|
|
|
|
writer.PutI4(0);
|
|
|
|
writer.PutI2(static_cast<int16_t>(first_child ? seq : sibling_level));
|
|
|
|
++seq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return seq;
|
2014-07-23 20:08:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 16:27:22 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void Discreet3DSExporter::WriteMaterials() {
|
2015-05-19 03:57:13 +00:00
|
|
|
for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curRootChunk(writer, Discreet3DS::CHUNK_MAT_MATERIAL);
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiMaterial &mat = *scene->mMaterials[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_MATNAME);
|
2020-05-02 13:14:38 +00:00
|
|
|
const std::string &name = GetMaterialName(mat, i);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteString(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
aiColor3D color;
|
|
|
|
if (mat.Get(AI_MATKEY_COLOR_DIFFUSE, color) == AI_SUCCESS) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_DIFFUSE);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mat.Get(AI_MATKEY_COLOR_SPECULAR, color) == AI_SUCCESS) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_SPECULAR);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteColor(color);
|
|
|
|
}
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (mat.Get(AI_MATKEY_COLOR_AMBIENT, color) == AI_SUCCESS) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_AMBIENT);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteColor(color);
|
|
|
|
}
|
|
|
|
|
2020-08-04 15:52:43 +00:00
|
|
|
float f;
|
2020-08-04 15:41:28 +00:00
|
|
|
if (mat.Get(AI_MATKEY_OPACITY, f) == AI_SUCCESS) {
|
|
|
|
ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_TRANSPARENCY);
|
|
|
|
WritePercentChunk(1.0f - f);
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (mat.Get(AI_MATKEY_COLOR_EMISSIVE, color) == AI_SUCCESS) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_SELF_ILLUM);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
aiShadingMode shading_mode = aiShadingMode_Flat;
|
|
|
|
if (mat.Get(AI_MATKEY_SHADING_MODEL, shading_mode) == AI_SUCCESS) {
|
|
|
|
ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_SHADING);
|
|
|
|
|
|
|
|
Discreet3DS::shadetype3ds shading_mode_out;
|
2020-05-02 13:14:38 +00:00
|
|
|
switch (shading_mode) {
|
2015-05-19 03:57:13 +00:00
|
|
|
case aiShadingMode_Flat:
|
|
|
|
case aiShadingMode_NoShading:
|
|
|
|
shading_mode_out = Discreet3DS::Flat;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case aiShadingMode_Gouraud:
|
|
|
|
case aiShadingMode_Toon:
|
|
|
|
case aiShadingMode_OrenNayar:
|
|
|
|
case aiShadingMode_Minnaert:
|
|
|
|
shading_mode_out = Discreet3DS::Gouraud;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case aiShadingMode_Phong:
|
|
|
|
case aiShadingMode_Blinn:
|
|
|
|
case aiShadingMode_CookTorrance:
|
|
|
|
case aiShadingMode_Fresnel:
|
2021-11-08 11:06:15 +00:00
|
|
|
case aiShadingMode_PBR_BRDF: // Possibly should be Discreet3DS::Metal in some cases but this is undocumented
|
2015-05-19 03:57:13 +00:00
|
|
|
shading_mode_out = Discreet3DS::Phong;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
shading_mode_out = Discreet3DS::Flat;
|
|
|
|
ai_assert(false);
|
|
|
|
};
|
|
|
|
writer.PutU2(static_cast<uint16_t>(shading_mode_out));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mat.Get(AI_MATKEY_SHININESS, f) == AI_SUCCESS) {
|
|
|
|
ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_SHININESS);
|
|
|
|
WritePercentChunk(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mat.Get(AI_MATKEY_SHININESS_STRENGTH, f) == AI_SUCCESS) {
|
|
|
|
ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_SHININESS_PERCENT);
|
|
|
|
WritePercentChunk(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
int twosided;
|
|
|
|
if (mat.Get(AI_MATKEY_TWOSIDED, twosided) == AI_SUCCESS && twosided != 0) {
|
|
|
|
ChunkWriter chunk(writer, Discreet3DS::CHUNK_MAT_TWO_SIDE);
|
|
|
|
writer.PutI2(1);
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:06:15 +00:00
|
|
|
// Fallback to BASE_COLOR if no DIFFUSE
|
|
|
|
if (!WriteTexture(mat, aiTextureType_DIFFUSE, Discreet3DS::CHUNK_MAT_TEXTURE))
|
|
|
|
WriteTexture(mat, aiTextureType_BASE_COLOR, Discreet3DS::CHUNK_MAT_TEXTURE);
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteTexture(mat, aiTextureType_HEIGHT, Discreet3DS::CHUNK_MAT_BUMPMAP);
|
|
|
|
WriteTexture(mat, aiTextureType_OPACITY, Discreet3DS::CHUNK_MAT_OPACMAP);
|
|
|
|
WriteTexture(mat, aiTextureType_SHININESS, Discreet3DS::CHUNK_MAT_MAT_SHINMAP);
|
|
|
|
WriteTexture(mat, aiTextureType_SPECULAR, Discreet3DS::CHUNK_MAT_SPECMAP);
|
|
|
|
WriteTexture(mat, aiTextureType_EMISSIVE, Discreet3DS::CHUNK_MAT_SELFIMAP);
|
|
|
|
WriteTexture(mat, aiTextureType_REFLECTION, Discreet3DS::CHUNK_MAT_REFLMAP);
|
|
|
|
}
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-11-08 11:06:15 +00:00
|
|
|
// returns true if the texture existed
|
|
|
|
bool Discreet3DSExporter::WriteTexture(const aiMaterial &mat, aiTextureType type, uint16_t chunk_flags) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiString path;
|
|
|
|
aiTextureMapMode map_mode[2] = {
|
|
|
|
aiTextureMapMode_Wrap, aiTextureMapMode_Wrap
|
|
|
|
};
|
2016-07-16 23:49:28 +00:00
|
|
|
ai_real blend = 1.0;
|
2020-06-23 19:05:42 +00:00
|
|
|
if (mat.GetTexture(type, 0, &path, nullptr, nullptr, &blend, nullptr, map_mode) != AI_SUCCESS || !path.length) {
|
2021-11-08 11:06:15 +00:00
|
|
|
return false;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle embedded textures properly
|
|
|
|
if (path.data[0] == '*') {
|
2021-05-13 11:05:31 +00:00
|
|
|
ASSIMP_LOG_ERROR("Ignoring embedded texture for export: ", path.C_Str());
|
2021-11-08 11:06:15 +00:00
|
|
|
return false;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ChunkWriter chunk(writer, chunk_flags);
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAPFILE);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteString(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
WritePercentChunk(blend);
|
|
|
|
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAT_MAP_TILING);
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t val = 0; // WRAP
|
|
|
|
if (map_mode[0] == aiTextureMapMode_Mirror) {
|
|
|
|
val = 0x2;
|
2020-05-02 13:14:38 +00:00
|
|
|
} else if (map_mode[0] == aiTextureMapMode_Decal) {
|
2015-05-19 03:57:13 +00:00
|
|
|
val = 0x10;
|
|
|
|
}
|
|
|
|
writer.PutU2(val);
|
|
|
|
}
|
|
|
|
// TODO: export texture transformation (i.e. UV offset, scale, rotation)
|
2021-11-08 11:06:15 +00:00
|
|
|
return true;
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void Discreet3DSExporter::WriteMeshes() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// NOTE: 3DS allows for instances. However:
|
|
|
|
// i) not all importers support reading them
|
|
|
|
// ii) instances are not as flexible as they are in assimp, in particular,
|
|
|
|
// nodes can carry (and instance) only one mesh.
|
|
|
|
//
|
|
|
|
// This exporter currently deep clones all instanced meshes, i.e. for each mesh
|
|
|
|
// attached to a node a full TRIMESH chunk is written to the file.
|
|
|
|
//
|
|
|
|
// Furthermore, the TRIMESH is transformed into world space so that it will
|
|
|
|
// appear correctly if importers don't read the scene hierarchy at all.
|
|
|
|
for (MeshesByNodeMap::const_iterator it = meshes.begin(); it != meshes.end(); ++it) {
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiNode &node = *(*it).first;
|
2015-05-19 03:57:13 +00:00
|
|
|
const unsigned int mesh_idx = (*it).second;
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiMesh &mesh = *scene->mMeshes[mesh_idx];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// This should not happen if the SLM step is correctly executed
|
|
|
|
// before the scene is handed to the exporter
|
|
|
|
ai_assert(mesh.mNumVertices <= 0xffff);
|
|
|
|
ai_assert(mesh.mNumFaces <= 0xffff);
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiMatrix4x4 &trafo = trafos[&node];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
ChunkWriter chunk(writer, Discreet3DS::CHUNK_OBJBLOCK);
|
|
|
|
|
|
|
|
// Mesh name is tied to the node it is attached to so it can later be referenced
|
2020-05-02 13:14:38 +00:00
|
|
|
const std::string &name = GetMeshName(mesh, mesh_idx, node);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteString(name);
|
|
|
|
|
|
|
|
// TRIMESH chunk
|
|
|
|
ChunkWriter chunk2(writer, Discreet3DS::CHUNK_TRIMESH);
|
|
|
|
|
|
|
|
// Vertices in world space
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_VERTLIST);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
const uint16_t count = static_cast<uint16_t>(mesh.mNumVertices);
|
|
|
|
writer.PutU2(count);
|
|
|
|
for (unsigned int i = 0; i < mesh.mNumVertices; ++i) {
|
2021-03-26 21:21:45 +00:00
|
|
|
const aiVector3D &v = mesh.mVertices[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
writer.PutF4(v.x);
|
|
|
|
writer.PutF4(v.y);
|
|
|
|
writer.PutF4(v.z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UV coordinates
|
|
|
|
if (mesh.HasTextureCoords(0)) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_MAPLIST);
|
2015-05-19 03:57:13 +00:00
|
|
|
const uint16_t count = static_cast<uint16_t>(mesh.mNumVertices);
|
|
|
|
writer.PutU2(count);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < mesh.mNumVertices; ++i) {
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiVector3D &v = mesh.mTextureCoords[0][i];
|
2015-05-19 03:57:13 +00:00
|
|
|
writer.PutF4(v.x);
|
|
|
|
writer.PutF4(v.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Faces (indices)
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_FACELIST);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
ai_assert(mesh.mNumFaces <= 0xffff);
|
|
|
|
|
|
|
|
// Count triangles, discard lines and points
|
|
|
|
uint16_t count = 0;
|
|
|
|
for (unsigned int i = 0; i < mesh.mNumFaces; ++i) {
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiFace &f = mesh.mFaces[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
if (f.mNumIndices < 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TRIANGULATE step is a pre-requisite so we should not see polys here
|
|
|
|
ai_assert(f.mNumIndices == 3);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.PutU2(count);
|
|
|
|
for (unsigned int i = 0; i < mesh.mNumFaces; ++i) {
|
2020-05-02 13:14:38 +00:00
|
|
|
const aiFace &f = mesh.mFaces[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
if (f.mNumIndices < 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int j = 0; j < 3; ++j) {
|
|
|
|
ai_assert(f.mIndices[j] <= 0xffff);
|
|
|
|
writer.PutI2(static_cast<uint16_t>(f.mIndices[j]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edge visibility flag
|
|
|
|
writer.PutI2(0x0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: write smoothing groups (CHUNK_SMOOLIST)
|
|
|
|
|
|
|
|
WriteFaceMaterialChunk(mesh);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transformation matrix by which the mesh vertices have been pre-transformed with.
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_TRMATRIX);
|
2021-03-26 21:21:45 +00:00
|
|
|
// Store rotation 3x3 matrix row wise
|
|
|
|
for (unsigned int r = 0; r < 3; ++r) {
|
2015-05-19 03:57:13 +00:00
|
|
|
for (unsigned int c = 0; c < 3; ++c) {
|
|
|
|
writer.PutF4(trafo[r][c]);
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 21:21:45 +00:00
|
|
|
// Store translation sub vector column wise
|
|
|
|
for (unsigned int r = 0; r < 3; ++r) {
|
|
|
|
writer.PutF4(trafo[r][3]);
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void Discreet3DSExporter::WriteFaceMaterialChunk(const aiMesh &mesh) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_FACEMAT);
|
2020-05-02 13:14:38 +00:00
|
|
|
const std::string &name = GetMaterialName(*scene->mMaterials[mesh.mMaterialIndex], mesh.mMaterialIndex);
|
2015-05-19 03:57:13 +00:00
|
|
|
WriteString(name);
|
|
|
|
|
|
|
|
// Because assimp splits meshes by material, only a single
|
|
|
|
// FACEMAT chunk needs to be written
|
|
|
|
ai_assert(mesh.mNumFaces <= 0xffff);
|
|
|
|
const uint16_t count = static_cast<uint16_t>(mesh.mNumFaces);
|
|
|
|
writer.PutU2(count);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < mesh.mNumFaces; ++i) {
|
|
|
|
writer.PutU2(static_cast<uint16_t>(i));
|
|
|
|
}
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void Discreet3DSExporter::WriteString(const std::string &s) {
|
2015-05-19 03:57:13 +00:00
|
|
|
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
|
|
|
|
writer.PutI1(*it);
|
|
|
|
}
|
|
|
|
writer.PutI1('\0');
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void Discreet3DSExporter::WriteString(const aiString &s) {
|
2015-05-19 03:57:13 +00:00
|
|
|
for (std::size_t i = 0; i < s.length; ++i) {
|
|
|
|
writer.PutI1(s.data[i]);
|
|
|
|
}
|
|
|
|
writer.PutI1('\0');
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void Discreet3DSExporter::WriteColor(const aiColor3D &color) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_RGBF);
|
2015-05-19 03:57:13 +00:00
|
|
|
writer.PutF4(color.r);
|
|
|
|
writer.PutF4(color.g);
|
|
|
|
writer.PutF4(color.b);
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Discreet3DSExporter::WritePercentChunk(float f) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter curChunk(writer, Discreet3DS::CHUNK_PERCENTF);
|
2015-05-19 03:57:13 +00:00
|
|
|
writer.PutF4(f);
|
2014-07-23 16:27:22 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 23:49:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Discreet3DSExporter::WritePercentChunk(double f) {
|
2020-03-08 20:24:01 +00:00
|
|
|
ChunkWriter ccurChunkhunk(writer, Discreet3DS::CHUNK_PERCENTD);
|
2016-07-16 23:49:28 +00:00
|
|
|
writer.PutF8(f);
|
|
|
|
}
|
|
|
|
|
2014-07-23 16:27:22 +00:00
|
|
|
#endif // ASSIMP_BUILD_NO_3DS_EXPORTER
|
|
|
|
#endif // ASSIMP_BUILD_NO_EXPORT
|