vrshoot

annotate libs/assimp/PlyExporter.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
rev   line source
nuclear@0 1 /*
nuclear@0 2 Open Asset Import Library (assimp)
nuclear@0 3 ----------------------------------------------------------------------
nuclear@0 4
nuclear@0 5 Copyright (c) 2006-2012, assimp team
nuclear@0 6 All rights reserved.
nuclear@0 7
nuclear@0 8 Redistribution and use of this software in source and binary forms,
nuclear@0 9 with or without modification, are permitted provided that the
nuclear@0 10 following conditions are met:
nuclear@0 11
nuclear@0 12 * Redistributions of source code must retain the above
nuclear@0 13 copyright notice, this list of conditions and the
nuclear@0 14 following disclaimer.
nuclear@0 15
nuclear@0 16 * Redistributions in binary form must reproduce the above
nuclear@0 17 copyright notice, this list of conditions and the
nuclear@0 18 following disclaimer in the documentation and/or other
nuclear@0 19 materials provided with the distribution.
nuclear@0 20
nuclear@0 21 * Neither the name of the assimp team, nor the names of its
nuclear@0 22 contributors may be used to endorse or promote products
nuclear@0 23 derived from this software without specific prior
nuclear@0 24 written permission of the assimp team.
nuclear@0 25
nuclear@0 26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
nuclear@0 27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
nuclear@0 28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
nuclear@0 29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
nuclear@0 30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
nuclear@0 31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
nuclear@0 32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
nuclear@0 33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
nuclear@0 34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
nuclear@0 35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nuclear@0 36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nuclear@0 37
nuclear@0 38 ----------------------------------------------------------------------
nuclear@0 39 */
nuclear@0 40
nuclear@0 41 #include "AssimpPCH.h"
nuclear@0 42
nuclear@0 43 #if !defined(ASSIMP_BUILD_NO_EXPORT) && !defined(ASSIMP_BUILD_NO_PLY_EXPORTER)
nuclear@0 44
nuclear@0 45 #include "PlyExporter.h"
nuclear@0 46 #include "assimp/version.h"
nuclear@0 47
nuclear@0 48 using namespace Assimp;
nuclear@0 49 namespace Assimp {
nuclear@0 50
nuclear@0 51 // ------------------------------------------------------------------------------------------------
nuclear@0 52 // Worker function for exporting a scene to PLY. Prototyped and registered in Exporter.cpp
nuclear@0 53 void ExportScenePly(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
nuclear@0 54 {
nuclear@0 55 // invoke the exporter
nuclear@0 56 PlyExporter exporter(pFile, pScene);
nuclear@0 57
nuclear@0 58 // we're still here - export successfully completed. Write the file.
nuclear@0 59 boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
nuclear@0 60 if(outfile == NULL) {
nuclear@0 61 throw DeadlyExportError("could not open output .ply file: " + std::string(pFile));
nuclear@0 62 }
nuclear@0 63
nuclear@0 64 outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
nuclear@0 65 }
nuclear@0 66
nuclear@0 67 } // end of namespace Assimp
nuclear@0 68
nuclear@0 69 #define PLY_EXPORT_HAS_NORMALS 0x1
nuclear@0 70 #define PLY_EXPORT_HAS_TANGENTS_BITANGENTS 0x2
nuclear@0 71 #define PLY_EXPORT_HAS_TEXCOORDS 0x4
nuclear@0 72 #define PLY_EXPORT_HAS_COLORS (PLY_EXPORT_HAS_TEXCOORDS << AI_MAX_NUMBER_OF_TEXTURECOORDS)
nuclear@0 73
nuclear@0 74 // ------------------------------------------------------------------------------------------------
nuclear@0 75 PlyExporter :: PlyExporter(const char* _filename, const aiScene* pScene)
nuclear@0 76 : filename(_filename)
nuclear@0 77 , pScene(pScene)
nuclear@0 78 , endl("\n")
nuclear@0 79 {
nuclear@0 80 // make sure that all formatting happens using the standard, C locale and not the user's current locale
nuclear@0 81 const std::locale& l = std::locale("C");
nuclear@0 82 mOutput.imbue(l);
nuclear@0 83
nuclear@0 84 unsigned int faces = 0u, vertices = 0u, components = 0u;
nuclear@0 85 for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
nuclear@0 86 const aiMesh& m = *pScene->mMeshes[i];
nuclear@0 87 faces += m.mNumFaces;
nuclear@0 88 vertices += m.mNumVertices;
nuclear@0 89
nuclear@0 90 if (m.HasNormals()) {
nuclear@0 91 components |= PLY_EXPORT_HAS_NORMALS;
nuclear@0 92 }
nuclear@0 93 if (m.HasTangentsAndBitangents()) {
nuclear@0 94 components |= PLY_EXPORT_HAS_TANGENTS_BITANGENTS;
nuclear@0 95 }
nuclear@0 96 for (unsigned int t = 0; m.HasTextureCoords(t); ++t) {
nuclear@0 97 components |= PLY_EXPORT_HAS_TEXCOORDS << t;
nuclear@0 98 }
nuclear@0 99 for (unsigned int t = 0; m.HasVertexColors(t); ++t) {
nuclear@0 100 components |= PLY_EXPORT_HAS_COLORS << t;
nuclear@0 101 }
nuclear@0 102 }
nuclear@0 103
nuclear@0 104 mOutput << "ply" << endl;
nuclear@0 105 mOutput << "format ascii 1.0" << endl;
nuclear@0 106 mOutput << "Created by Open Asset Import Library - http://assimp.sf.net (v"
nuclear@0 107 << aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.'
nuclear@0 108 << aiGetVersionRevision() << ")" << endl;
nuclear@0 109
nuclear@0 110 mOutput << "element vertex " << vertices << endl;
nuclear@0 111 mOutput << "property float x" << endl;
nuclear@0 112 mOutput << "property float y" << endl;
nuclear@0 113 mOutput << "property float z" << endl;
nuclear@0 114
nuclear@0 115 if(components & PLY_EXPORT_HAS_NORMALS) {
nuclear@0 116 mOutput << "property float nx" << endl;
nuclear@0 117 mOutput << "property float ny" << endl;
nuclear@0 118 mOutput << "property float nz" << endl;
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 // write texcoords first, just in case an importer does not support tangents
nuclear@0 122 // bitangents and just skips over the rest of the line upon encountering
nuclear@0 123 // unknown fields (Ply leaves pretty much every vertex component open,
nuclear@0 124 // but in reality most importers only know about vertex positions, normals
nuclear@0 125 // and texture coordinates).
nuclear@0 126 for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
nuclear@0 127 if (!c) {
nuclear@0 128 mOutput << "property float s" << endl;
nuclear@0 129 mOutput << "property float t" << endl;
nuclear@0 130 }
nuclear@0 131 else {
nuclear@0 132 mOutput << "property float s" << c << endl;
nuclear@0 133 mOutput << "property float t" << c << endl;
nuclear@0 134 }
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
nuclear@0 138 if (!c) {
nuclear@0 139 mOutput << "property float r" << endl;
nuclear@0 140 mOutput << "property float g" << endl;
nuclear@0 141 mOutput << "property float b" << endl;
nuclear@0 142 mOutput << "property float a" << endl;
nuclear@0 143 }
nuclear@0 144 else {
nuclear@0 145 mOutput << "property float r" << c << endl;
nuclear@0 146 mOutput << "property float g" << c << endl;
nuclear@0 147 mOutput << "property float b" << c << endl;
nuclear@0 148 mOutput << "property float a" << c << endl;
nuclear@0 149 }
nuclear@0 150 }
nuclear@0 151
nuclear@0 152 if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
nuclear@0 153 mOutput << "property float tx" << endl;
nuclear@0 154 mOutput << "property float ty" << endl;
nuclear@0 155 mOutput << "property float tz" << endl;
nuclear@0 156 mOutput << "property float bx" << endl;
nuclear@0 157 mOutput << "property float by" << endl;
nuclear@0 158 mOutput << "property float bz" << endl;
nuclear@0 159 }
nuclear@0 160
nuclear@0 161 mOutput << "element face " << faces << endl;
nuclear@0 162 mOutput << "property list uint uint vertex_indices" << endl;
nuclear@0 163 mOutput << "end_header" << endl;
nuclear@0 164
nuclear@0 165 for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
nuclear@0 166 WriteMeshVerts(pScene->mMeshes[i],components);
nuclear@0 167 }
nuclear@0 168 for (unsigned int i = 0, ofs = 0; i < pScene->mNumMeshes; ++i) {
nuclear@0 169 WriteMeshIndices(pScene->mMeshes[i],ofs);
nuclear@0 170 ofs += pScene->mMeshes[i]->mNumVertices;
nuclear@0 171 }
nuclear@0 172 }
nuclear@0 173
nuclear@0 174 // ------------------------------------------------------------------------------------------------
nuclear@0 175 void PlyExporter :: WriteMeshVerts(const aiMesh* m, unsigned int components)
nuclear@0 176 {
nuclear@0 177 for (unsigned int i = 0; i < m->mNumVertices; ++i) {
nuclear@0 178 mOutput <<
nuclear@0 179 m->mVertices[i].x << " " <<
nuclear@0 180 m->mVertices[i].y << " " <<
nuclear@0 181 m->mVertices[i].z
nuclear@0 182 ;
nuclear@0 183 if(components & PLY_EXPORT_HAS_NORMALS) {
nuclear@0 184 if (m->HasNormals()) {
nuclear@0 185 mOutput <<
nuclear@0 186 " " << m->mNormals[i].x <<
nuclear@0 187 " " << m->mNormals[i].y <<
nuclear@0 188 " " << m->mNormals[i].z;
nuclear@0 189 }
nuclear@0 190 else {
nuclear@0 191 mOutput << " 0.0 0.0 0.0";
nuclear@0 192 }
nuclear@0 193 }
nuclear@0 194
nuclear@0 195 for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
nuclear@0 196 if (m->HasTextureCoords(c)) {
nuclear@0 197 mOutput <<
nuclear@0 198 " " << m->mTextureCoords[c][i].x <<
nuclear@0 199 " " << m->mTextureCoords[c][i].y;
nuclear@0 200 }
nuclear@0 201 else {
nuclear@0 202 mOutput << " -1.0 -1.0";
nuclear@0 203 }
nuclear@0 204 }
nuclear@0 205
nuclear@0 206 for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
nuclear@0 207 if (m->HasVertexColors(c)) {
nuclear@0 208 mOutput <<
nuclear@0 209 " " << m->mColors[c][i].r <<
nuclear@0 210 " " << m->mColors[c][i].g <<
nuclear@0 211 " " << m->mColors[c][i].b <<
nuclear@0 212 " " << m->mColors[c][i].a;
nuclear@0 213 }
nuclear@0 214 else {
nuclear@0 215 mOutput << " -1.0 -1.0 -1.0 -1.0";
nuclear@0 216 }
nuclear@0 217 }
nuclear@0 218
nuclear@0 219 if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
nuclear@0 220 if (m->HasTangentsAndBitangents()) {
nuclear@0 221 mOutput <<
nuclear@0 222 " " << m->mTangents[i].x <<
nuclear@0 223 " " << m->mTangents[i].y <<
nuclear@0 224 " " << m->mTangents[i].z <<
nuclear@0 225 " " << m->mBitangents[i].x <<
nuclear@0 226 " " << m->mBitangents[i].y <<
nuclear@0 227 " " << m->mBitangents[i].z
nuclear@0 228 ;
nuclear@0 229 }
nuclear@0 230 else {
nuclear@0 231 mOutput << " 0.0 0.0 0.0 0.0 0.0 0.0";
nuclear@0 232 }
nuclear@0 233 }
nuclear@0 234
nuclear@0 235 mOutput << endl;
nuclear@0 236 }
nuclear@0 237 }
nuclear@0 238
nuclear@0 239 // ------------------------------------------------------------------------------------------------
nuclear@0 240 void PlyExporter :: WriteMeshIndices(const aiMesh* m, unsigned int offset)
nuclear@0 241 {
nuclear@0 242 for (unsigned int i = 0; i < m->mNumFaces; ++i) {
nuclear@0 243 const aiFace& f = m->mFaces[i];
nuclear@0 244 mOutput << f.mNumIndices << " ";
nuclear@0 245 for(unsigned int c = 0; c < f.mNumIndices; ++c) {
nuclear@0 246 mOutput << (f.mIndices[c] + offset) << (c == f.mNumIndices-1 ? endl : " ");
nuclear@0 247 }
nuclear@0 248 }
nuclear@0 249 }
nuclear@0 250
nuclear@0 251 #endif