2011-07-19 21:41:59 +00:00
|
|
|
/*
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2011-07-19 21:41:59 +00:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2015-06-28 19:48:25 +00:00
|
|
|
Copyright (c) 2006-2015, assimp team
|
2011-07-19 21:41:59 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
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.
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2011-07-19 21:41:59 +00:00
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
2012-02-03 03:38:30 +00:00
|
|
|
written permission of the assimp team.
|
2011-07-19 21:41:59 +00:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
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
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2015-04-15 23:00:17 +00:00
|
|
|
|
2011-07-19 21:41:59 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_EXPORT
|
|
|
|
#ifndef ASSIMP_BUILD_NO_OBJ_EXPORTER
|
|
|
|
|
|
|
|
#include "ObjExporter.h"
|
2015-06-28 19:48:25 +00:00
|
|
|
#include "../include/assimp/version.h"
|
|
|
|
#include "../include/assimp/IOSystem.hpp"
|
|
|
|
#include "../include/assimp/Exporter.hpp"
|
|
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
#include "Exceptional.h"
|
|
|
|
#include "../include/assimp/material.h"
|
|
|
|
#include "../include/assimp/scene.h"
|
|
|
|
#include "StringComparison.h"
|
|
|
|
#include <boost/foreach.hpp>
|
2015-04-15 23:00:17 +00:00
|
|
|
|
2011-07-19 21:41:59 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
namespace Assimp {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Worker function for exporting a scene to Wavefront OBJ. Prototyped and registered in Exporter.cpp
|
2015-03-14 13:03:14 +00:00
|
|
|
void ExportSceneObj(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
|
2011-07-19 21:41:59 +00:00
|
|
|
{
|
|
|
|
// invoke the exporter
|
|
|
|
ObjExporter exporter(pFile, pScene);
|
|
|
|
|
|
|
|
// we're still here - export successfully completed. Write both the main OBJ file and the material script
|
|
|
|
{
|
|
|
|
boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
|
2013-09-30 18:11:37 +00:00
|
|
|
if(outfile == NULL) {
|
2013-09-30 18:12:38 +00:00
|
|
|
throw DeadlyExportError("could not open output .obj file: " + std::string(pFile));
|
2013-09-30 18:11:37 +00:00
|
|
|
}
|
2011-07-19 21:41:59 +00:00
|
|
|
outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(exporter.GetMaterialLibFileName(),"wt"));
|
2013-09-30 18:11:37 +00:00
|
|
|
if(outfile == NULL) {
|
|
|
|
throw DeadlyExportError("could not open output .mtl file: " + std::string(exporter.GetMaterialLibFileName()));
|
|
|
|
}
|
2011-07-19 21:41:59 +00:00
|
|
|
outfile->Write( exporter.mOutputMat.str().c_str(), static_cast<size_t>(exporter.mOutputMat.tellp()),1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of namespace Assimp
|
|
|
|
|
2014-07-03 18:33:52 +00:00
|
|
|
static const std::string MaterialExt = ".mtl";
|
2011-07-19 21:41:59 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
ObjExporter :: ObjExporter(const char* _filename, const aiScene* pScene)
|
|
|
|
: filename(_filename)
|
|
|
|
, pScene(pScene)
|
|
|
|
, endl("\n")
|
|
|
|
{
|
|
|
|
// make sure that all formatting happens using the standard, C locale and not the user's current locale
|
|
|
|
const std::locale& l = std::locale("C");
|
|
|
|
mOutput.imbue(l);
|
|
|
|
mOutputMat.imbue(l);
|
|
|
|
|
|
|
|
WriteGeometryFile();
|
|
|
|
WriteMaterialFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
std::string ObjExporter :: GetMaterialLibName()
|
|
|
|
{
|
|
|
|
// within the Obj file, we use just the relative file name with the path stripped
|
|
|
|
const std::string& s = GetMaterialLibFileName();
|
|
|
|
std::string::size_type il = s.find_last_of("/\\");
|
|
|
|
if (il != std::string::npos) {
|
|
|
|
return s.substr(il + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
std::string ObjExporter :: GetMaterialLibFileName()
|
|
|
|
{
|
2014-07-03 18:33:52 +00:00
|
|
|
return filename + MaterialExt;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ObjExporter :: WriteHeader(std::ostringstream& out)
|
|
|
|
{
|
|
|
|
out << "# File produced by Open Asset Import Library (http://www.assimp.sf.net)" << endl;
|
|
|
|
out << "# (assimp v" << aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.' << aiGetVersionRevision() << ")" << endl << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
std::string ObjExporter :: GetMaterialName(unsigned int index)
|
|
|
|
{
|
|
|
|
const aiMaterial* const mat = pScene->mMaterials[index];
|
|
|
|
aiString s;
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_NAME,s)) {
|
|
|
|
return std::string(s.data,s.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
char number[ sizeof(unsigned int) * 3 + 1 ];
|
|
|
|
ASSIMP_itoa10(number,index);
|
|
|
|
return "$Material_" + std::string(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2014-07-03 18:33:52 +00:00
|
|
|
void ObjExporter::WriteMaterialFile()
|
2011-07-19 21:41:59 +00:00
|
|
|
{
|
|
|
|
WriteHeader(mOutputMat);
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < pScene->mNumMaterials; ++i) {
|
|
|
|
const aiMaterial* const mat = pScene->mMaterials[i];
|
|
|
|
|
|
|
|
int illum = 1;
|
|
|
|
mOutputMat << "newmtl " << GetMaterialName(i) << endl;
|
|
|
|
|
|
|
|
aiColor4D c;
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_COLOR_DIFFUSE,c)) {
|
2014-07-09 13:37:27 +00:00
|
|
|
mOutputMat << "Kd " << c.r << " " << c.g << " " << c.b << endl;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_COLOR_AMBIENT,c)) {
|
2014-07-09 13:37:27 +00:00
|
|
|
mOutputMat << "Ka " << c.r << " " << c.g << " " << c.b << endl;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_COLOR_SPECULAR,c)) {
|
2014-07-09 13:37:27 +00:00
|
|
|
mOutputMat << "Ks " << c.r << " " << c.g << " " << c.b << endl;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
2014-03-16 20:30:10 +00:00
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_COLOR_EMISSIVE,c)) {
|
2014-07-09 13:37:27 +00:00
|
|
|
mOutputMat << "Ke " << c.r << " " << c.g << " " << c.b << endl;
|
2014-03-16 20:30:10 +00:00
|
|
|
}
|
2011-07-19 21:41:59 +00:00
|
|
|
|
|
|
|
float o;
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_OPACITY,o)) {
|
|
|
|
mOutputMat << "d " << o << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_SHININESS,o) && o) {
|
|
|
|
mOutputMat << "Ns " << o << endl;
|
|
|
|
illum = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOutputMat << "illum " << illum << endl;
|
|
|
|
|
|
|
|
aiString s;
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_DIFFUSE(0),s)) {
|
2014-07-09 13:37:27 +00:00
|
|
|
mOutputMat << "map_Kd " << s.data << endl;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_AMBIENT(0),s)) {
|
2014-07-09 13:37:27 +00:00
|
|
|
mOutputMat << "map_Ka " << s.data << endl;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_SPECULAR(0),s)) {
|
2014-07-09 13:37:27 +00:00
|
|
|
mOutputMat << "map_Ks " << s.data << endl;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_SHININESS(0),s)) {
|
2014-07-09 13:37:27 +00:00
|
|
|
mOutputMat << "map_Ns " << s.data << endl;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
2014-07-09 13:35:00 +00:00
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_OPACITY(0),s)) {
|
|
|
|
mOutputMat << "map_d " << s.data << endl;
|
|
|
|
}
|
2011-07-19 21:41:59 +00:00
|
|
|
if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_HEIGHT(0),s) || AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_NORMALS(0),s)) {
|
|
|
|
// implementations seem to vary here, so write both variants
|
|
|
|
mOutputMat << "bump " << s.data << endl;
|
|
|
|
mOutputMat << "map_bump " << s.data << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOutputMat << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void ObjExporter :: WriteGeometryFile()
|
|
|
|
{
|
|
|
|
WriteHeader(mOutput);
|
|
|
|
mOutput << "mtllib " << GetMaterialLibName() << endl << endl;
|
|
|
|
|
|
|
|
// collect mesh geometry
|
|
|
|
aiMatrix4x4 mBase;
|
2014-06-17 12:15:48 +00:00
|
|
|
AddNode(pScene->mRootNode, mBase);
|
2011-07-19 21:41:59 +00:00
|
|
|
|
|
|
|
// write vertex positions
|
2013-09-30 18:07:53 +00:00
|
|
|
vpMap.getVectors(vp);
|
2011-07-19 21:41:59 +00:00
|
|
|
mOutput << "# " << vp.size() << " vertex positions" << endl;
|
|
|
|
BOOST_FOREACH(const aiVector3D& v, vp) {
|
|
|
|
mOutput << "v " << v.x << " " << v.y << " " << v.z << endl;
|
|
|
|
}
|
|
|
|
mOutput << endl;
|
|
|
|
|
|
|
|
// write uv coordinates
|
2013-09-30 18:07:53 +00:00
|
|
|
vtMap.getVectors(vt);
|
2011-07-19 21:41:59 +00:00
|
|
|
mOutput << "# " << vt.size() << " UV coordinates" << endl;
|
|
|
|
BOOST_FOREACH(const aiVector3D& v, vt) {
|
|
|
|
mOutput << "vt " << v.x << " " << v.y << " " << v.z << endl;
|
|
|
|
}
|
|
|
|
mOutput << endl;
|
|
|
|
|
|
|
|
// write vertex normals
|
2013-09-30 18:07:53 +00:00
|
|
|
vnMap.getVectors(vn);
|
2011-07-19 21:41:59 +00:00
|
|
|
mOutput << "# " << vn.size() << " vertex normals" << endl;
|
|
|
|
BOOST_FOREACH(const aiVector3D& v, vn) {
|
|
|
|
mOutput << "vn " << v.x << " " << v.y << " " << v.z << endl;
|
|
|
|
}
|
|
|
|
mOutput << endl;
|
|
|
|
|
|
|
|
// now write all mesh instances
|
|
|
|
BOOST_FOREACH(const MeshInstance& m, meshes) {
|
|
|
|
mOutput << "# Mesh \'" << m.name << "\' with " << m.faces.size() << " faces" << endl;
|
2014-06-20 23:06:04 +00:00
|
|
|
if (!m.name.empty()) {
|
|
|
|
mOutput << "g " << m.name << endl;
|
|
|
|
}
|
2011-07-19 21:41:59 +00:00
|
|
|
mOutput << "usemtl " << m.matname << endl;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Face& f, m.faces) {
|
2011-08-27 23:32:53 +00:00
|
|
|
mOutput << f.kind << ' ';
|
2011-07-19 21:41:59 +00:00
|
|
|
BOOST_FOREACH(const FaceVertex& fv, f.indices) {
|
2011-08-27 23:32:53 +00:00
|
|
|
mOutput << ' ' << fv.vp;
|
|
|
|
|
|
|
|
if (f.kind != 'p') {
|
|
|
|
if (fv.vt || f.kind == 'f') {
|
|
|
|
mOutput << '/';
|
|
|
|
}
|
|
|
|
if (fv.vt) {
|
|
|
|
mOutput << fv.vt;
|
|
|
|
}
|
2014-06-20 22:08:29 +00:00
|
|
|
if (f.kind == 'f' && fv.vn) {
|
|
|
|
mOutput << '/' << fv.vn;
|
2011-08-27 23:32:53 +00:00
|
|
|
}
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mOutput << endl;
|
|
|
|
}
|
|
|
|
mOutput << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 12:15:48 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2013-09-30 18:07:53 +00:00
|
|
|
int ObjExporter::vecIndexMap::getIndex(const aiVector3D& vec)
|
|
|
|
{
|
2014-06-17 12:15:48 +00:00
|
|
|
vecIndexMap::dataType::iterator vertIt = vecMap.find(vec);
|
|
|
|
// vertex already exists, so reference it
|
|
|
|
if(vertIt != vecMap.end()){
|
2013-09-30 18:07:53 +00:00
|
|
|
return vertIt->second;
|
|
|
|
}
|
|
|
|
vecMap[vec] = mNextIndex;
|
|
|
|
int ret = mNextIndex;
|
|
|
|
mNextIndex++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-17 12:15:48 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2013-09-30 18:07:53 +00:00
|
|
|
void ObjExporter::vecIndexMap::getVectors( std::vector<aiVector3D>& vecs )
|
|
|
|
{
|
|
|
|
vecs.resize(vecMap.size());
|
2015-06-05 07:52:06 +00:00
|
|
|
for(vecIndexMap::dataType::iterator it = vecMap.begin(); it != vecMap.end(); ++it){
|
2013-09-30 18:07:53 +00:00
|
|
|
vecs[it->second-1] = it->first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-19 21:41:59 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2014-07-03 18:33:52 +00:00
|
|
|
void ObjExporter::AddMesh(const aiString& name, const aiMesh* m, const aiMatrix4x4& mat)
|
2011-07-19 21:41:59 +00:00
|
|
|
{
|
|
|
|
meshes.push_back(MeshInstance());
|
|
|
|
MeshInstance& mesh = meshes.back();
|
|
|
|
|
2014-06-17 12:15:48 +00:00
|
|
|
mesh.name = std::string(name.data,name.length) + (m->mName.length ? "_" + std::string(m->mName.data,m->mName.length) : "");
|
2011-07-19 21:41:59 +00:00
|
|
|
mesh.matname = GetMaterialName(m->mMaterialIndex);
|
|
|
|
|
|
|
|
mesh.faces.resize(m->mNumFaces);
|
2013-09-30 18:07:53 +00:00
|
|
|
|
2011-07-19 21:41:59 +00:00
|
|
|
for(unsigned int i = 0; i < m->mNumFaces; ++i) {
|
|
|
|
const aiFace& f = m->mFaces[i];
|
|
|
|
|
|
|
|
Face& face = mesh.faces[i];
|
2011-08-27 23:32:53 +00:00
|
|
|
switch (f.mNumIndices) {
|
|
|
|
case 1:
|
|
|
|
face.kind = 'p';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
face.kind = 'l';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
face.kind = 'f';
|
|
|
|
}
|
2011-07-19 21:41:59 +00:00
|
|
|
face.indices.resize(f.mNumIndices);
|
|
|
|
|
|
|
|
for(unsigned int a = 0; a < f.mNumIndices; ++a) {
|
|
|
|
const unsigned int idx = f.mIndices[a];
|
|
|
|
|
2013-09-30 18:07:53 +00:00
|
|
|
aiVector3D vert = mat * m->mVertices[idx];
|
|
|
|
face.indices[a].vp = vpMap.getIndex(vert);
|
2011-07-19 21:41:59 +00:00
|
|
|
|
|
|
|
if (m->mNormals) {
|
2014-06-17 12:15:48 +00:00
|
|
|
aiVector3D norm = aiMatrix3x3(mat) * m->mNormals[idx];
|
|
|
|
face.indices[a].vn = vnMap.getIndex(norm);
|
2013-09-30 18:07:53 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
face.indices[a].vn = 0;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m->mTextureCoords[0]) {
|
2013-09-30 18:07:53 +00:00
|
|
|
face.indices[a].vt = vtMap.getIndex(m->mTextureCoords[0][idx]);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
face.indices[a].vt = 0;
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2014-07-03 18:33:52 +00:00
|
|
|
void ObjExporter::AddNode(const aiNode* nd, const aiMatrix4x4& mParent)
|
2011-07-19 21:41:59 +00:00
|
|
|
{
|
|
|
|
const aiMatrix4x4& mAbs = mParent * nd->mTransformation;
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < nd->mNumMeshes; ++i) {
|
2014-06-17 12:15:48 +00:00
|
|
|
AddMesh(nd->mName, pScene->mMeshes[nd->mMeshes[i]], mAbs);
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < nd->mNumChildren; ++i) {
|
2014-06-17 12:15:48 +00:00
|
|
|
AddNode(nd->mChildren[i], mAbs);
|
2011-07-19 21:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 18:33:52 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_OBJ_EXPORTER
|
|
|
|
#endif // ASSIMP_BUILD_NO_EXPORT
|