nuclear@0: /* nuclear@0: Open Asset Import Library (assimp) nuclear@0: ---------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the nuclear@0: following conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: nuclear@0: ---------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: #include "AssimpPCH.h" nuclear@0: nuclear@0: #if !defined(ASSIMP_BUILD_NO_EXPORT) && !defined(ASSIMP_BUILD_NO_PLY_EXPORTER) nuclear@0: nuclear@0: #include "PlyExporter.h" nuclear@0: #include "assimp/version.h" nuclear@0: nuclear@0: using namespace Assimp; nuclear@0: namespace Assimp { nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Worker function for exporting a scene to PLY. Prototyped and registered in Exporter.cpp nuclear@0: void ExportScenePly(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene) nuclear@0: { nuclear@0: // invoke the exporter nuclear@0: PlyExporter exporter(pFile, pScene); nuclear@0: nuclear@0: // we're still here - export successfully completed. Write the file. nuclear@0: boost::scoped_ptr outfile (pIOSystem->Open(pFile,"wt")); nuclear@0: if(outfile == NULL) { nuclear@0: throw DeadlyExportError("could not open output .ply file: " + std::string(pFile)); nuclear@0: } nuclear@0: nuclear@0: outfile->Write( exporter.mOutput.str().c_str(), static_cast(exporter.mOutput.tellp()),1); nuclear@0: } nuclear@0: nuclear@0: } // end of namespace Assimp nuclear@0: nuclear@0: #define PLY_EXPORT_HAS_NORMALS 0x1 nuclear@0: #define PLY_EXPORT_HAS_TANGENTS_BITANGENTS 0x2 nuclear@0: #define PLY_EXPORT_HAS_TEXCOORDS 0x4 nuclear@0: #define PLY_EXPORT_HAS_COLORS (PLY_EXPORT_HAS_TEXCOORDS << AI_MAX_NUMBER_OF_TEXTURECOORDS) nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: PlyExporter :: PlyExporter(const char* _filename, const aiScene* pScene) nuclear@0: : filename(_filename) nuclear@0: , pScene(pScene) nuclear@0: , endl("\n") nuclear@0: { nuclear@0: // make sure that all formatting happens using the standard, C locale and not the user's current locale nuclear@0: const std::locale& l = std::locale("C"); nuclear@0: mOutput.imbue(l); nuclear@0: nuclear@0: unsigned int faces = 0u, vertices = 0u, components = 0u; nuclear@0: for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { nuclear@0: const aiMesh& m = *pScene->mMeshes[i]; nuclear@0: faces += m.mNumFaces; nuclear@0: vertices += m.mNumVertices; nuclear@0: nuclear@0: if (m.HasNormals()) { nuclear@0: components |= PLY_EXPORT_HAS_NORMALS; nuclear@0: } nuclear@0: if (m.HasTangentsAndBitangents()) { nuclear@0: components |= PLY_EXPORT_HAS_TANGENTS_BITANGENTS; nuclear@0: } nuclear@0: for (unsigned int t = 0; m.HasTextureCoords(t); ++t) { nuclear@0: components |= PLY_EXPORT_HAS_TEXCOORDS << t; nuclear@0: } nuclear@0: for (unsigned int t = 0; m.HasVertexColors(t); ++t) { nuclear@0: components |= PLY_EXPORT_HAS_COLORS << t; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: mOutput << "ply" << endl; nuclear@0: mOutput << "format ascii 1.0" << endl; nuclear@0: mOutput << "Created by Open Asset Import Library - http://assimp.sf.net (v" nuclear@0: << aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.' nuclear@0: << aiGetVersionRevision() << ")" << endl; nuclear@0: nuclear@0: mOutput << "element vertex " << vertices << endl; nuclear@0: mOutput << "property float x" << endl; nuclear@0: mOutput << "property float y" << endl; nuclear@0: mOutput << "property float z" << endl; nuclear@0: nuclear@0: if(components & PLY_EXPORT_HAS_NORMALS) { nuclear@0: mOutput << "property float nx" << endl; nuclear@0: mOutput << "property float ny" << endl; nuclear@0: mOutput << "property float nz" << endl; nuclear@0: } nuclear@0: nuclear@0: // write texcoords first, just in case an importer does not support tangents nuclear@0: // bitangents and just skips over the rest of the line upon encountering nuclear@0: // unknown fields (Ply leaves pretty much every vertex component open, nuclear@0: // but in reality most importers only know about vertex positions, normals nuclear@0: // and texture coordinates). nuclear@0: for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) { nuclear@0: if (!c) { nuclear@0: mOutput << "property float s" << endl; nuclear@0: mOutput << "property float t" << endl; nuclear@0: } nuclear@0: else { nuclear@0: mOutput << "property float s" << c << endl; nuclear@0: mOutput << "property float t" << c << endl; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) { nuclear@0: if (!c) { nuclear@0: mOutput << "property float r" << endl; nuclear@0: mOutput << "property float g" << endl; nuclear@0: mOutput << "property float b" << endl; nuclear@0: mOutput << "property float a" << endl; nuclear@0: } nuclear@0: else { nuclear@0: mOutput << "property float r" << c << endl; nuclear@0: mOutput << "property float g" << c << endl; nuclear@0: mOutput << "property float b" << c << endl; nuclear@0: mOutput << "property float a" << c << endl; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) { nuclear@0: mOutput << "property float tx" << endl; nuclear@0: mOutput << "property float ty" << endl; nuclear@0: mOutput << "property float tz" << endl; nuclear@0: mOutput << "property float bx" << endl; nuclear@0: mOutput << "property float by" << endl; nuclear@0: mOutput << "property float bz" << endl; nuclear@0: } nuclear@0: nuclear@0: mOutput << "element face " << faces << endl; nuclear@0: mOutput << "property list uint uint vertex_indices" << endl; nuclear@0: mOutput << "end_header" << endl; nuclear@0: nuclear@0: for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { nuclear@0: WriteMeshVerts(pScene->mMeshes[i],components); nuclear@0: } nuclear@0: for (unsigned int i = 0, ofs = 0; i < pScene->mNumMeshes; ++i) { nuclear@0: WriteMeshIndices(pScene->mMeshes[i],ofs); nuclear@0: ofs += pScene->mMeshes[i]->mNumVertices; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: void PlyExporter :: WriteMeshVerts(const aiMesh* m, unsigned int components) nuclear@0: { nuclear@0: for (unsigned int i = 0; i < m->mNumVertices; ++i) { nuclear@0: mOutput << nuclear@0: m->mVertices[i].x << " " << nuclear@0: m->mVertices[i].y << " " << nuclear@0: m->mVertices[i].z nuclear@0: ; nuclear@0: if(components & PLY_EXPORT_HAS_NORMALS) { nuclear@0: if (m->HasNormals()) { nuclear@0: mOutput << nuclear@0: " " << m->mNormals[i].x << nuclear@0: " " << m->mNormals[i].y << nuclear@0: " " << m->mNormals[i].z; nuclear@0: } nuclear@0: else { nuclear@0: mOutput << " 0.0 0.0 0.0"; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) { nuclear@0: if (m->HasTextureCoords(c)) { nuclear@0: mOutput << nuclear@0: " " << m->mTextureCoords[c][i].x << nuclear@0: " " << m->mTextureCoords[c][i].y; nuclear@0: } nuclear@0: else { nuclear@0: mOutput << " -1.0 -1.0"; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) { nuclear@0: if (m->HasVertexColors(c)) { nuclear@0: mOutput << nuclear@0: " " << m->mColors[c][i].r << nuclear@0: " " << m->mColors[c][i].g << nuclear@0: " " << m->mColors[c][i].b << nuclear@0: " " << m->mColors[c][i].a; nuclear@0: } nuclear@0: else { nuclear@0: mOutput << " -1.0 -1.0 -1.0 -1.0"; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) { nuclear@0: if (m->HasTangentsAndBitangents()) { nuclear@0: mOutput << nuclear@0: " " << m->mTangents[i].x << nuclear@0: " " << m->mTangents[i].y << nuclear@0: " " << m->mTangents[i].z << nuclear@0: " " << m->mBitangents[i].x << nuclear@0: " " << m->mBitangents[i].y << nuclear@0: " " << m->mBitangents[i].z nuclear@0: ; nuclear@0: } nuclear@0: else { nuclear@0: mOutput << " 0.0 0.0 0.0 0.0 0.0 0.0"; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: mOutput << endl; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: void PlyExporter :: WriteMeshIndices(const aiMesh* m, unsigned int offset) nuclear@0: { nuclear@0: for (unsigned int i = 0; i < m->mNumFaces; ++i) { nuclear@0: const aiFace& f = m->mFaces[i]; nuclear@0: mOutput << f.mNumIndices << " "; nuclear@0: for(unsigned int c = 0; c < f.mNumIndices; ++c) { nuclear@0: mOutput << (f.mIndices[c] + offset) << (c == f.mNumIndices-1 ? endl : " "); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: #endif