vrshoot

diff libs/assimp/RawLoader.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/RawLoader.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,324 @@
     1.4 +/*
     1.5 +---------------------------------------------------------------------------
     1.6 +Open Asset Import Library (assimp)
     1.7 +---------------------------------------------------------------------------
     1.8 +
     1.9 +Copyright (c) 2006-2012, assimp team
    1.10 +
    1.11 +All rights reserved.
    1.12 +
    1.13 +Redistribution and use of this software in source and binary forms, 
    1.14 +with or without modification, are permitted provided that the following 
    1.15 +conditions are met:
    1.16 +
    1.17 +* Redistributions of source code must retain the above
    1.18 +  copyright notice, this list of conditions and the
    1.19 +  following disclaimer.
    1.20 +
    1.21 +* Redistributions in binary form must reproduce the above
    1.22 +  copyright notice, this list of conditions and the
    1.23 +  following disclaimer in the documentation and/or other
    1.24 +  materials provided with the distribution.
    1.25 +
    1.26 +* Neither the name of the assimp team, nor the names of its
    1.27 +  contributors may be used to endorse or promote products
    1.28 +  derived from this software without specific prior
    1.29 +  written permission of the assimp team.
    1.30 +
    1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.42 +---------------------------------------------------------------------------
    1.43 +*/
    1.44 +
    1.45 +/** @file  RawLoader.cpp
    1.46 + *  @brief Implementation of the RAW importer class 
    1.47 + */
    1.48 +
    1.49 +#include "AssimpPCH.h"
    1.50 +#ifndef ASSIMP_BUILD_NO_RAW_IMPORTER
    1.51 +
    1.52 +// internal headers
    1.53 +#include "RawLoader.h"
    1.54 +#include "ParsingUtils.h"
    1.55 +#include "fast_atof.h"
    1.56 +
    1.57 +using namespace Assimp;
    1.58 +
    1.59 +static const aiImporterDesc desc = {
    1.60 +	"Raw Importer",
    1.61 +	"",
    1.62 +	"",
    1.63 +	"",
    1.64 +	aiImporterFlags_SupportTextFlavour,
    1.65 +	0,
    1.66 +	0,
    1.67 +	0,
    1.68 +	0,
    1.69 +	"raw"
    1.70 +};
    1.71 +
    1.72 +// ------------------------------------------------------------------------------------------------
    1.73 +// Constructor to be privately used by Importer
    1.74 +RAWImporter::RAWImporter()
    1.75 +{}
    1.76 +
    1.77 +// ------------------------------------------------------------------------------------------------
    1.78 +// Destructor, private as well 
    1.79 +RAWImporter::~RAWImporter()
    1.80 +{}
    1.81 +
    1.82 +// ------------------------------------------------------------------------------------------------
    1.83 +// Returns whether the class can handle the format of the given file. 
    1.84 +bool RAWImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const
    1.85 +{
    1.86 +	return SimpleExtensionCheck(pFile,"raw");
    1.87 +}
    1.88 +
    1.89 +// ------------------------------------------------------------------------------------------------
    1.90 +const aiImporterDesc* RAWImporter::GetInfo () const
    1.91 +{
    1.92 +	return &desc;
    1.93 +}
    1.94 +
    1.95 +// ------------------------------------------------------------------------------------------------
    1.96 +// Imports the given file into the given scene structure. 
    1.97 +void RAWImporter::InternReadFile( const std::string& pFile, 
    1.98 +	aiScene* pScene, IOSystem* pIOHandler)
    1.99 +{
   1.100 +	boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
   1.101 +
   1.102 +	// Check whether we can read from the file
   1.103 +	if( file.get() == NULL) {
   1.104 +		throw DeadlyImportError( "Failed to open RAW file " + pFile + ".");
   1.105 +	}
   1.106 +
   1.107 +	// allocate storage and copy the contents of the file to a memory buffer
   1.108 +	// (terminate it with zero)
   1.109 +	std::vector<char> mBuffer2;
   1.110 +	TextFileToBuffer(file.get(),mBuffer2);
   1.111 +	const char* buffer = &mBuffer2[0];
   1.112 +
   1.113 +	// list of groups loaded from the file
   1.114 +	std::vector< GroupInformation > outGroups(1,GroupInformation("<default>"));
   1.115 +	std::vector< GroupInformation >::iterator curGroup = outGroups.begin();
   1.116 +
   1.117 +	// now read all lines
   1.118 +	char line[4096];
   1.119 +	while (GetNextLine(buffer,line))
   1.120 +	{
   1.121 +		// if the line starts with a non-numeric identifier, it marks
   1.122 +		// the beginning of a new group
   1.123 +		const char* sz = line;SkipSpaces(&sz);
   1.124 +		if (IsLineEnd(*sz))continue;
   1.125 +		if (!IsNumeric(*sz))
   1.126 +		{
   1.127 +			const char* sz2 = sz;
   1.128 +			while (!IsSpaceOrNewLine(*sz2))++sz2;
   1.129 +			const unsigned int length = (unsigned int)(sz2-sz);
   1.130 +
   1.131 +			// find an existing group with this name
   1.132 +			for (std::vector< GroupInformation >::iterator it = outGroups.begin(), end = outGroups.end();
   1.133 +				it != end;++it)
   1.134 +			{
   1.135 +				if (length == (*it).name.length() && !::strcmp(sz,(*it).name.c_str()))
   1.136 +				{
   1.137 +					curGroup = it;sz2 = NULL;
   1.138 +					break;
   1.139 +				}
   1.140 +			}
   1.141 +			if (sz2)
   1.142 +			{
   1.143 +				outGroups.push_back(GroupInformation(std::string(sz,length)));
   1.144 +				curGroup = outGroups.end()-1;
   1.145 +			}
   1.146 +		}
   1.147 +		else
   1.148 +		{
   1.149 +			// there can be maximally 12 floats plus an extra texture file name
   1.150 +			float data[12];
   1.151 +			unsigned int num;
   1.152 +			for (num = 0; num < 12;++num)
   1.153 +			{
   1.154 +				if(!SkipSpaces(&sz) || !IsNumeric(*sz))break;
   1.155 +				sz = fast_atoreal_move<float>(sz,data[num]);
   1.156 +			}
   1.157 +			if (num != 12 && num != 9)
   1.158 +			{
   1.159 +				DefaultLogger::get()->error("A line may have either 9 or 12 floats and an optional texture");
   1.160 +				continue;
   1.161 +			}
   1.162 +
   1.163 +			MeshInformation* output = NULL;
   1.164 +
   1.165 +			const char* sz2 = sz;
   1.166 +			unsigned int length;
   1.167 +			if (!IsLineEnd(*sz))
   1.168 +			{
   1.169 +				while (!IsSpaceOrNewLine(*sz2))++sz2;
   1.170 +				length = (unsigned int)(sz2-sz);
   1.171 +			}
   1.172 +			else if (9 == num)
   1.173 +			{
   1.174 +				sz = "%default%";
   1.175 +				length = 9;
   1.176 +			}
   1.177 +			else 
   1.178 +			{
   1.179 +				sz = "";
   1.180 +				length = 0;
   1.181 +			}
   1.182 +
   1.183 +			// search in the list of meshes whether we have one with this texture
   1.184 +			for (std::vector< MeshInformation >::iterator it = (*curGroup).meshes.begin(),
   1.185 +				end = (*curGroup).meshes.end(); it != end; ++it)
   1.186 +			{
   1.187 +				if (length == (*it).name.length() && (length ? !::strcmp(sz,(*it).name.c_str()) : true))
   1.188 +				{
   1.189 +					output = &(*it);
   1.190 +					break;
   1.191 +				}
   1.192 +			}
   1.193 +			// if we don't have the mesh, create it
   1.194 +			if (!output)
   1.195 +			{
   1.196 +				(*curGroup).meshes.push_back(MeshInformation(std::string(sz,length)));
   1.197 +				output = &((*curGroup).meshes.back());
   1.198 +			}
   1.199 +			if (12 == num)
   1.200 +			{
   1.201 +				aiColor4D v(data[0],data[1],data[2],1.0f);
   1.202 +				output->colors.push_back(v);
   1.203 +				output->colors.push_back(v);
   1.204 +				output->colors.push_back(v);
   1.205 +
   1.206 +				output->vertices.push_back(aiVector3D(data[3],data[4],data[5]));
   1.207 +				output->vertices.push_back(aiVector3D(data[6],data[7],data[8]));
   1.208 +				output->vertices.push_back(aiVector3D(data[9],data[10],data[11]));
   1.209 +			}
   1.210 +			else
   1.211 +			{
   1.212 +				output->vertices.push_back(aiVector3D(data[0],data[1],data[2]));
   1.213 +				output->vertices.push_back(aiVector3D(data[3],data[4],data[5]));
   1.214 +				output->vertices.push_back(aiVector3D(data[6],data[7],data[8]));
   1.215 +			}
   1.216 +		}
   1.217 +	}
   1.218 +
   1.219 +	pScene->mRootNode = new aiNode();
   1.220 +	pScene->mRootNode->mName.Set("<RawRoot>");
   1.221 +
   1.222 +	// count the number of valid groups
   1.223 +	// (meshes can't be empty)
   1.224 +	for (std::vector< GroupInformation >::iterator it = outGroups.begin(), end = outGroups.end();
   1.225 +		it != end;++it)
   1.226 +	{
   1.227 +		if (!(*it).meshes.empty())
   1.228 +		{
   1.229 +			++pScene->mRootNode->mNumChildren; 
   1.230 +			pScene->mNumMeshes += (unsigned int)(*it).meshes.size();
   1.231 +		}
   1.232 +	}
   1.233 +
   1.234 +	if (!pScene->mNumMeshes)
   1.235 +	{
   1.236 +		throw DeadlyImportError("RAW: No meshes loaded. The file seems to be corrupt or empty.");
   1.237 +	}
   1.238 +
   1.239 +	pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
   1.240 +	aiNode** cc;
   1.241 +	if (1 == pScene->mRootNode->mNumChildren)
   1.242 +	{
   1.243 +		cc = &pScene->mRootNode;
   1.244 +		pScene->mRootNode->mNumChildren = 0;
   1.245 +	}
   1.246 +	else cc = pScene->mRootNode->mChildren = new aiNode*[pScene->mRootNode->mNumChildren];
   1.247 +
   1.248 +	pScene->mNumMaterials = pScene->mNumMeshes;
   1.249 +	aiMaterial** mats = pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
   1.250 +
   1.251 +	unsigned int meshIdx = 0;
   1.252 +	for (std::vector< GroupInformation >::iterator it = outGroups.begin(), end = outGroups.end();
   1.253 +		it != end;++it)
   1.254 +	{
   1.255 +		if ((*it).meshes.empty())continue;
   1.256 +		
   1.257 +		aiNode* node;
   1.258 +		if (pScene->mRootNode->mNumChildren)
   1.259 +		{
   1.260 +			node = *cc = new aiNode();
   1.261 +			node->mParent = pScene->mRootNode;
   1.262 +		}
   1.263 +		else node = *cc;++cc;
   1.264 +		node->mName.Set((*it).name);
   1.265 +
   1.266 +		// add all meshes
   1.267 +		node->mNumMeshes = (unsigned int)(*it).meshes.size();
   1.268 +		unsigned int* pi = node->mMeshes = new unsigned int[ node->mNumMeshes ];
   1.269 +		for (std::vector< MeshInformation >::iterator it2 = (*it).meshes.begin(),
   1.270 +			end2 = (*it).meshes.end(); it2 != end2; ++it2)
   1.271 +		{
   1.272 +			ai_assert(!(*it2).vertices.empty());
   1.273 +
   1.274 +			// allocate the mesh
   1.275 +			*pi++ = meshIdx;
   1.276 +			aiMesh* mesh = pScene->mMeshes[meshIdx] = new aiMesh();
   1.277 +			mesh->mMaterialIndex = meshIdx++;
   1.278 +
   1.279 +			mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
   1.280 +
   1.281 +			// allocate storage for the vertex components and copy them
   1.282 +			mesh->mNumVertices = (unsigned int)(*it2).vertices.size();
   1.283 +			mesh->mVertices = new aiVector3D[ mesh->mNumVertices ];
   1.284 +			::memcpy(mesh->mVertices,&(*it2).vertices[0],sizeof(aiVector3D)*mesh->mNumVertices);
   1.285 +
   1.286 +			if ((*it2).colors.size())
   1.287 +			{
   1.288 +				ai_assert((*it2).colors.size() == mesh->mNumVertices);
   1.289 +
   1.290 +				mesh->mColors[0] = new aiColor4D[ mesh->mNumVertices ];
   1.291 +				::memcpy(mesh->mColors[0],&(*it2).colors[0],sizeof(aiColor4D)*mesh->mNumVertices);
   1.292 +			}
   1.293 +
   1.294 +			// generate triangles
   1.295 +			ai_assert(0 == mesh->mNumVertices % 3);
   1.296 +			aiFace* fc = mesh->mFaces = new aiFace[ mesh->mNumFaces = mesh->mNumVertices/3 ];
   1.297 +			aiFace* const fcEnd = fc + mesh->mNumFaces;
   1.298 +			unsigned int n = 0;
   1.299 +			while (fc != fcEnd)
   1.300 +			{
   1.301 +				aiFace& f = *fc++;
   1.302 +				f.mIndices = new unsigned int[f.mNumIndices = 3];
   1.303 +				for (unsigned int m = 0; m < 3;++m)
   1.304 +					f.mIndices[m] = n++;
   1.305 +			}
   1.306 +
   1.307 +			// generate a material for the mesh
   1.308 +			aiMaterial* mat = new aiMaterial();
   1.309 +
   1.310 +			aiColor4D clr(1.0f,1.0f,1.0f,1.0f);
   1.311 +			if ("%default%" == (*it2).name) // a gray default material
   1.312 +			{
   1.313 +				clr.r = clr.g = clr.b = 0.6f;
   1.314 +			}
   1.315 +			else if ((*it2).name.length() > 0) // a texture
   1.316 +			{
   1.317 +				aiString s;
   1.318 +				s.Set((*it2).name);
   1.319 +				mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
   1.320 +			}
   1.321 +			mat->AddProperty<aiColor4D>(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
   1.322 +			*mats++ = mat;
   1.323 +		}
   1.324 +	}
   1.325 +}
   1.326 +
   1.327 +#endif // !! ASSIMP_BUILD_NO_RAW_IMPORTER