vrshoot
diff libs/assimp/NFFLoader.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/NFFLoader.cpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,1268 @@ 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 Implementation of the STL importer class */ 1.46 + 1.47 +#include "AssimpPCH.h" 1.48 +#ifndef ASSIMP_BUILD_NO_NFF_IMPORTER 1.49 + 1.50 +// internal headers 1.51 +#include "NFFLoader.h" 1.52 +#include "ParsingUtils.h" 1.53 +#include "StandardShapes.h" 1.54 +#include "fast_atof.h" 1.55 +#include "RemoveComments.h" 1.56 + 1.57 +using namespace Assimp; 1.58 + 1.59 +static const aiImporterDesc desc = { 1.60 + "Neutral File Format Importer", 1.61 + "", 1.62 + "", 1.63 + "", 1.64 + aiImporterFlags_SupportBinaryFlavour, 1.65 + 0, 1.66 + 0, 1.67 + 0, 1.68 + 0, 1.69 + "enff nff" 1.70 +}; 1.71 + 1.72 +// ------------------------------------------------------------------------------------------------ 1.73 +// Constructor to be privately used by Importer 1.74 +NFFImporter::NFFImporter() 1.75 +{} 1.76 + 1.77 +// ------------------------------------------------------------------------------------------------ 1.78 +// Destructor, private as well 1.79 +NFFImporter::~NFFImporter() 1.80 +{} 1.81 + 1.82 +// ------------------------------------------------------------------------------------------------ 1.83 +// Returns whether the class can handle the format of the given file. 1.84 +bool NFFImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const 1.85 +{ 1.86 + return SimpleExtensionCheck(pFile,"nff","enff"); 1.87 +} 1.88 + 1.89 +// ------------------------------------------------------------------------------------------------ 1.90 +// Get the list of all supported file extensions 1.91 +const aiImporterDesc* NFFImporter::GetInfo () const 1.92 +{ 1.93 + return &desc; 1.94 +} 1.95 + 1.96 +// ------------------------------------------------------------------------------------------------ 1.97 +#define AI_NFF_PARSE_FLOAT(f) \ 1.98 + SkipSpaces(&sz); \ 1.99 + if (!::IsLineEnd(*sz))sz = fast_atoreal_move<float>(sz, (float&)f); 1.100 + 1.101 +// ------------------------------------------------------------------------------------------------ 1.102 +#define AI_NFF_PARSE_TRIPLE(v) \ 1.103 + AI_NFF_PARSE_FLOAT(v[0]) \ 1.104 + AI_NFF_PARSE_FLOAT(v[1]) \ 1.105 + AI_NFF_PARSE_FLOAT(v[2]) 1.106 + 1.107 +// ------------------------------------------------------------------------------------------------ 1.108 +#define AI_NFF_PARSE_SHAPE_INFORMATION() \ 1.109 + aiVector3D center, radius(1.0f,get_qnan(),get_qnan()); \ 1.110 + AI_NFF_PARSE_TRIPLE(center); \ 1.111 + AI_NFF_PARSE_TRIPLE(radius); \ 1.112 + if (is_qnan(radius.z))radius.z = radius.x; \ 1.113 + if (is_qnan(radius.y))radius.y = radius.x; \ 1.114 + currentMesh.radius = radius; \ 1.115 + currentMesh.center = center; 1.116 + 1.117 +// ------------------------------------------------------------------------------------------------ 1.118 +#define AI_NFF2_GET_NEXT_TOKEN() \ 1.119 + do \ 1.120 + { \ 1.121 + if (!GetNextLine(buffer,line)) \ 1.122 + {DefaultLogger::get()->warn("NFF2: Unexpected EOF, can't read next token");break;} \ 1.123 + SkipSpaces(line,&sz); \ 1.124 + } \ 1.125 + while(IsLineEnd(*sz)) 1.126 + 1.127 + 1.128 +// ------------------------------------------------------------------------------------------------ 1.129 +// Loads the materail table for the NFF2 file format from an external file 1.130 +void NFFImporter::LoadNFF2MaterialTable(std::vector<ShadingInfo>& output, 1.131 + const std::string& path, IOSystem* pIOHandler) 1.132 +{ 1.133 + boost::scoped_ptr<IOStream> file( pIOHandler->Open( path, "rb")); 1.134 + 1.135 + // Check whether we can read from the file 1.136 + if( !file.get()) { 1.137 + DefaultLogger::get()->error("NFF2: Unable to open material library " + path + "."); 1.138 + return; 1.139 + } 1.140 + 1.141 + // get the size of the file 1.142 + const unsigned int m = (unsigned int)file->FileSize(); 1.143 + 1.144 + // allocate storage and copy the contents of the file to a memory buffer 1.145 + // (terminate it with zero) 1.146 + std::vector<char> mBuffer2(m+1); 1.147 + TextFileToBuffer(file.get(),mBuffer2); 1.148 + const char* buffer = &mBuffer2[0]; 1.149 + 1.150 + // First of all: remove all comments from the file 1.151 + CommentRemover::RemoveLineComments("//",&mBuffer2[0]); 1.152 + 1.153 + // The file should start with the magic sequence "mat" 1.154 + if (!TokenMatch(buffer,"mat",3)) { 1.155 + DefaultLogger::get()->error("NFF2: Not a valid material library " + path + "."); 1.156 + return; 1.157 + } 1.158 + 1.159 + ShadingInfo* curShader = NULL; 1.160 + 1.161 + // No read the file line per line 1.162 + char line[4096]; 1.163 + const char* sz; 1.164 + while (GetNextLine(buffer,line)) 1.165 + { 1.166 + SkipSpaces(line,&sz); 1.167 + 1.168 + // 'version' defines the version of the file format 1.169 + if (TokenMatch(sz,"version",7)) 1.170 + { 1.171 + DefaultLogger::get()->info("NFF (Sense8) material library file format: " + std::string(sz)); 1.172 + } 1.173 + // 'matdef' starts a new material in the file 1.174 + else if (TokenMatch(sz,"matdef",6)) 1.175 + { 1.176 + // add a new material to the list 1.177 + output.push_back( ShadingInfo() ); 1.178 + curShader = & output.back(); 1.179 + 1.180 + // parse the name of the material 1.181 + } 1.182 + else if (!TokenMatch(sz,"valid",5)) 1.183 + { 1.184 + // check whether we have an active material at the moment 1.185 + if (!IsLineEnd(*sz)) 1.186 + { 1.187 + if (!curShader) 1.188 + { 1.189 + DefaultLogger::get()->error(std::string("NFF2 material library: Found element ") + 1.190 + sz + "but there is no active material"); 1.191 + continue; 1.192 + } 1.193 + } 1.194 + else continue; 1.195 + 1.196 + // now read the material property and determine its type 1.197 + aiColor3D c; 1.198 + if (TokenMatch(sz,"ambient",7)) 1.199 + { 1.200 + AI_NFF_PARSE_TRIPLE(c); 1.201 + curShader->ambient = c; 1.202 + } 1.203 + else if (TokenMatch(sz,"diffuse",7) || TokenMatch(sz,"ambientdiffuse",14) /* correct? */) 1.204 + { 1.205 + AI_NFF_PARSE_TRIPLE(c); 1.206 + curShader->diffuse = curShader->ambient = c; 1.207 + } 1.208 + else if (TokenMatch(sz,"specular",8)) 1.209 + { 1.210 + AI_NFF_PARSE_TRIPLE(c); 1.211 + curShader->specular = c; 1.212 + } 1.213 + else if (TokenMatch(sz,"emission",8)) 1.214 + { 1.215 + AI_NFF_PARSE_TRIPLE(c); 1.216 + curShader->emissive = c; 1.217 + } 1.218 + else if (TokenMatch(sz,"shininess",9)) 1.219 + { 1.220 + AI_NFF_PARSE_FLOAT(curShader->shininess); 1.221 + } 1.222 + else if (TokenMatch(sz,"opacity",7)) 1.223 + { 1.224 + AI_NFF_PARSE_FLOAT(curShader->opacity); 1.225 + } 1.226 + } 1.227 + } 1.228 +} 1.229 + 1.230 +// ------------------------------------------------------------------------------------------------ 1.231 +// Imports the given file into the given scene structure. 1.232 +void NFFImporter::InternReadFile( const std::string& pFile, 1.233 + aiScene* pScene, IOSystem* pIOHandler) 1.234 +{ 1.235 + boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb")); 1.236 + 1.237 + // Check whether we can read from the file 1.238 + if( !file.get()) 1.239 + throw DeadlyImportError( "Failed to open NFF file " + pFile + "."); 1.240 + 1.241 + unsigned int m = (unsigned int)file->FileSize(); 1.242 + 1.243 + // allocate storage and copy the contents of the file to a memory buffer 1.244 + // (terminate it with zero) 1.245 + std::vector<char> mBuffer2; 1.246 + TextFileToBuffer(file.get(),mBuffer2); 1.247 + const char* buffer = &mBuffer2[0]; 1.248 + 1.249 + // mesh arrays - separate here to make the handling of the pointers below easier. 1.250 + std::vector<MeshInfo> meshes; 1.251 + std::vector<MeshInfo> meshesWithNormals; 1.252 + std::vector<MeshInfo> meshesWithUVCoords; 1.253 + std::vector<MeshInfo> meshesLocked; 1.254 + 1.255 + char line[4096]; 1.256 + const char* sz; 1.257 + 1.258 + // camera parameters 1.259 + aiVector3D camPos, camUp(0.f,1.f,0.f), camLookAt(0.f,0.f,1.f); 1.260 + float angle = 45.f; 1.261 + aiVector2D resolution; 1.262 + 1.263 + bool hasCam = false; 1.264 + 1.265 + MeshInfo* currentMeshWithNormals = NULL; 1.266 + MeshInfo* currentMesh = NULL; 1.267 + MeshInfo* currentMeshWithUVCoords = NULL; 1.268 + 1.269 + ShadingInfo s; // current material info 1.270 + 1.271 + // degree of tesselation 1.272 + unsigned int iTesselation = 4; 1.273 + 1.274 + // some temporary variables we need to parse the file 1.275 + unsigned int sphere = 0, 1.276 + cylinder = 0, 1.277 + cone = 0, 1.278 + numNamed = 0, 1.279 + dodecahedron = 0, 1.280 + octahedron = 0, 1.281 + tetrahedron = 0, 1.282 + hexahedron = 0; 1.283 + 1.284 + // lights imported from the file 1.285 + std::vector<Light> lights; 1.286 + 1.287 + // check whether this is the NFF2 file format 1.288 + if (TokenMatch(buffer,"nff",3)) 1.289 + { 1.290 + const float qnan = get_qnan(); 1.291 + const aiColor4D cQNAN = aiColor4D (qnan,0.f,0.f,1.f); 1.292 + const aiVector3D vQNAN = aiVector3D(qnan,0.f,0.f); 1.293 + 1.294 + // another NFF file format ... just a raw parser has been implemented 1.295 + // no support for further details, I don't think it is worth the effort 1.296 + // http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/nff/nff2.html 1.297 + // http://www.netghost.narod.ru/gff/graphics/summary/sense8.htm 1.298 + 1.299 + // First of all: remove all comments from the file 1.300 + CommentRemover::RemoveLineComments("//",&mBuffer2[0]); 1.301 + 1.302 + while (GetNextLine(buffer,line)) 1.303 + { 1.304 + SkipSpaces(line,&sz); 1.305 + if (TokenMatch(sz,"version",7)) 1.306 + { 1.307 + DefaultLogger::get()->info("NFF (Sense8) file format: " + std::string(sz)); 1.308 + } 1.309 + else if (TokenMatch(sz,"viewpos",7)) 1.310 + { 1.311 + AI_NFF_PARSE_TRIPLE(camPos); 1.312 + hasCam = true; 1.313 + } 1.314 + else if (TokenMatch(sz,"viewdir",7)) 1.315 + { 1.316 + AI_NFF_PARSE_TRIPLE(camLookAt); 1.317 + hasCam = true; 1.318 + } 1.319 + // This starts a new object section 1.320 + else if (!IsSpaceOrNewLine(*sz)) 1.321 + { 1.322 + unsigned int subMeshIdx = 0; 1.323 + 1.324 + // read the name of the object, skip all spaces 1.325 + // at the end of it. 1.326 + const char* sz3 = sz; 1.327 + while (!IsSpaceOrNewLine(*sz))++sz; 1.328 + std::string objectName = std::string(sz3,(unsigned int)(sz-sz3)); 1.329 + 1.330 + const unsigned int objStart = (unsigned int)meshes.size(); 1.331 + 1.332 + // There could be a material table in a separate file 1.333 + std::vector<ShadingInfo> materialTable; 1.334 + while (true) 1.335 + { 1.336 + AI_NFF2_GET_NEXT_TOKEN(); 1.337 + 1.338 + // material table - an external file 1.339 + if (TokenMatch(sz,"mtable",6)) 1.340 + { 1.341 + SkipSpaces(&sz); 1.342 + sz3 = sz; 1.343 + while (!IsSpaceOrNewLine(*sz))++sz; 1.344 + const unsigned int diff = (unsigned int)(sz-sz3); 1.345 + if (!diff)DefaultLogger::get()->warn("NFF2: Found empty mtable token"); 1.346 + else 1.347 + { 1.348 + // The material table has the file extension .mat. 1.349 + // If it is not there, we need to append it 1.350 + std::string path = std::string(sz3,diff); 1.351 + if(std::string::npos == path.find_last_of(".mat")) 1.352 + { 1.353 + path.append(".mat"); 1.354 + } 1.355 + 1.356 + // Now extract the working directory from the path to 1.357 + // this file and append the material library filename 1.358 + // to it. 1.359 + std::string::size_type s; 1.360 + if ((std::string::npos == (s = path.find_last_of('\\')) || !s) && 1.361 + (std::string::npos == (s = path.find_last_of('/')) || !s) ) 1.362 + { 1.363 + s = pFile.find_last_of('\\'); 1.364 + if (std::string::npos == s)s = pFile.find_last_of('/'); 1.365 + if (std::string::npos != s) 1.366 + { 1.367 + path = pFile.substr(0,s+1) + path; 1.368 + } 1.369 + } 1.370 + LoadNFF2MaterialTable(materialTable,path,pIOHandler); 1.371 + } 1.372 + } 1.373 + else break; 1.374 + } 1.375 + 1.376 + // read the numbr of vertices 1.377 + unsigned int num = ::strtoul10(sz,&sz); 1.378 + 1.379 + // temporary storage 1.380 + std::vector<aiColor4D> tempColors; 1.381 + std::vector<aiVector3D> tempPositions,tempTextureCoords,tempNormals; 1.382 + 1.383 + bool hasNormals = false,hasUVs = false,hasColor = false; 1.384 + 1.385 + tempPositions.reserve (num); 1.386 + tempColors.reserve (num); 1.387 + tempNormals.reserve (num); 1.388 + tempTextureCoords.reserve (num); 1.389 + for (unsigned int i = 0; i < num; ++i) 1.390 + { 1.391 + AI_NFF2_GET_NEXT_TOKEN(); 1.392 + aiVector3D v; 1.393 + AI_NFF_PARSE_TRIPLE(v); 1.394 + tempPositions.push_back(v); 1.395 + 1.396 + // parse all other attributes in the line 1.397 + while (true) 1.398 + { 1.399 + SkipSpaces(&sz); 1.400 + if (IsLineEnd(*sz))break; 1.401 + 1.402 + // color definition 1.403 + if (TokenMatch(sz,"0x",2)) 1.404 + { 1.405 + hasColor = true; 1.406 + register unsigned int numIdx = ::strtoul16(sz,&sz); 1.407 + aiColor4D clr; 1.408 + clr.a = 1.f; 1.409 + 1.410 + // 0xRRGGBB 1.411 + clr.r = ((numIdx >> 16u) & 0xff) / 255.f; 1.412 + clr.g = ((numIdx >> 8u) & 0xff) / 255.f; 1.413 + clr.b = ((numIdx) & 0xff) / 255.f; 1.414 + tempColors.push_back(clr); 1.415 + } 1.416 + // normal vector 1.417 + else if (TokenMatch(sz,"norm",4)) 1.418 + { 1.419 + hasNormals = true; 1.420 + AI_NFF_PARSE_TRIPLE(v); 1.421 + tempNormals.push_back(v); 1.422 + } 1.423 + // UV coordinate 1.424 + else if (TokenMatch(sz,"uv",2)) 1.425 + { 1.426 + hasUVs = true; 1.427 + AI_NFF_PARSE_FLOAT(v.x); 1.428 + AI_NFF_PARSE_FLOAT(v.y); 1.429 + v.z = 0.f; 1.430 + tempTextureCoords.push_back(v); 1.431 + } 1.432 + } 1.433 + 1.434 + // fill in dummies for all attributes that have not been set 1.435 + if (tempNormals.size() != tempPositions.size()) 1.436 + tempNormals.push_back(vQNAN); 1.437 + 1.438 + if (tempTextureCoords.size() != tempPositions.size()) 1.439 + tempTextureCoords.push_back(vQNAN); 1.440 + 1.441 + if (tempColors.size() != tempPositions.size()) 1.442 + tempColors.push_back(cQNAN); 1.443 + } 1.444 + 1.445 + AI_NFF2_GET_NEXT_TOKEN(); 1.446 + if (!num)throw DeadlyImportError("NFF2: There are zero vertices"); 1.447 + num = ::strtoul10(sz,&sz); 1.448 + 1.449 + std::vector<unsigned int> tempIdx; 1.450 + tempIdx.reserve(10); 1.451 + for (unsigned int i = 0; i < num; ++i) 1.452 + { 1.453 + AI_NFF2_GET_NEXT_TOKEN(); 1.454 + SkipSpaces(line,&sz); 1.455 + unsigned int numIdx = ::strtoul10(sz,&sz); 1.456 + 1.457 + // read all faces indices 1.458 + if (numIdx) 1.459 + { 1.460 + // mesh.faces.push_back(numIdx); 1.461 + // tempIdx.erase(tempIdx.begin(),tempIdx.end()); 1.462 + tempIdx.resize(numIdx); 1.463 + 1.464 + for (unsigned int a = 0; a < numIdx;++a) 1.465 + { 1.466 + SkipSpaces(sz,&sz); 1.467 + m = ::strtoul10(sz,&sz); 1.468 + if (m >= (unsigned int)tempPositions.size()) 1.469 + { 1.470 + DefaultLogger::get()->error("NFF2: Vertex index overflow"); 1.471 + m= 0; 1.472 + } 1.473 + // mesh.vertices.push_back (tempPositions[idx]); 1.474 + tempIdx[a] = m; 1.475 + } 1.476 + } 1.477 + 1.478 + // build a temporary shader object for the face. 1.479 + ShadingInfo shader; 1.480 + unsigned int matIdx = 0; 1.481 + 1.482 + // white material color - we have vertex colors 1.483 + shader.color = aiColor3D(1.f,1.f,1.f); 1.484 + aiColor4D c = aiColor4D(1.f,1.f,1.f,1.f); 1.485 + while (true) 1.486 + { 1.487 + SkipSpaces(sz,&sz); 1.488 + if(IsLineEnd(*sz))break; 1.489 + 1.490 + // per-polygon colors 1.491 + if (TokenMatch(sz,"0x",2)) 1.492 + { 1.493 + hasColor = true; 1.494 + const char* sz2 = sz; 1.495 + numIdx = ::strtoul16(sz,&sz); 1.496 + const unsigned int diff = (unsigned int)(sz-sz2); 1.497 + 1.498 + // 0xRRGGBB 1.499 + if (diff > 3) 1.500 + { 1.501 + c.r = ((numIdx >> 16u) & 0xff) / 255.f; 1.502 + c.g = ((numIdx >> 8u) & 0xff) / 255.f; 1.503 + c.b = ((numIdx) & 0xff) / 255.f; 1.504 + } 1.505 + // 0xRGB 1.506 + else 1.507 + { 1.508 + c.r = ((numIdx >> 8u) & 0xf) / 16.f; 1.509 + c.g = ((numIdx >> 4u) & 0xf) / 16.f; 1.510 + c.b = ((numIdx) & 0xf) / 16.f; 1.511 + } 1.512 + } 1.513 + // TODO - implement texture mapping here 1.514 +#if 0 1.515 + // mirror vertex texture coordinate? 1.516 + else if (TokenMatch(sz,"mirror",6)) 1.517 + { 1.518 + } 1.519 + // texture coordinate scaling 1.520 + else if (TokenMatch(sz,"scale",5)) 1.521 + { 1.522 + } 1.523 + // texture coordinate translation 1.524 + else if (TokenMatch(sz,"trans",5)) 1.525 + { 1.526 + } 1.527 + // texture coordinate rotation angle 1.528 + else if (TokenMatch(sz,"rot",3)) 1.529 + { 1.530 + } 1.531 +#endif 1.532 + 1.533 + // texture file name for this polygon + mapping information 1.534 + else if ('_' == sz[0]) 1.535 + { 1.536 + // get mapping information 1.537 + switch (sz[1]) 1.538 + { 1.539 + case 'v': 1.540 + case 'V': 1.541 + 1.542 + shader.shaded = false; 1.543 + break; 1.544 + 1.545 + case 't': 1.546 + case 'T': 1.547 + case 'u': 1.548 + case 'U': 1.549 + 1.550 + DefaultLogger::get()->warn("Unsupported NFF2 texture attribute: trans"); 1.551 + }; 1.552 + if (!sz[1] || '_' != sz[2]) 1.553 + { 1.554 + DefaultLogger::get()->warn("NFF2: Expected underscore after texture attributes"); 1.555 + continue; 1.556 + } 1.557 + const char* sz2 = sz+3; 1.558 + while (!IsSpaceOrNewLine( *sz ))++sz; 1.559 + const unsigned int diff = (unsigned int)(sz-sz2); 1.560 + if (diff)shader.texFile = std::string(sz2,diff); 1.561 + } 1.562 + 1.563 + // Two-sided material? 1.564 + else if (TokenMatch(sz,"both",4)) 1.565 + { 1.566 + shader.twoSided = true; 1.567 + } 1.568 + 1.569 + // Material ID? 1.570 + else if (!materialTable.empty() && TokenMatch(sz,"matid",5)) 1.571 + { 1.572 + SkipSpaces(&sz); 1.573 + matIdx = ::strtoul10(sz,&sz); 1.574 + if (matIdx >= materialTable.size()) 1.575 + { 1.576 + DefaultLogger::get()->error("NFF2: Material index overflow."); 1.577 + matIdx = 0; 1.578 + } 1.579 + 1.580 + // now combine our current shader with the shader we 1.581 + // read from the material table. 1.582 + ShadingInfo& mat = materialTable[matIdx]; 1.583 + shader.ambient = mat.ambient; 1.584 + shader.diffuse = mat.diffuse; 1.585 + shader.emissive = mat.emissive; 1.586 + shader.opacity = mat.opacity; 1.587 + shader.specular = mat.specular; 1.588 + shader.shininess = mat.shininess; 1.589 + } 1.590 + else SkipToken(sz); 1.591 + } 1.592 + 1.593 + // search the list of all shaders we have for this object whether 1.594 + // there is an identical one. In this case, we append our mesh 1.595 + // data to it. 1.596 + MeshInfo* mesh = NULL; 1.597 + for (std::vector<MeshInfo>::iterator it = meshes.begin() + objStart, end = meshes.end(); 1.598 + it != end; ++it) 1.599 + { 1.600 + if ((*it).shader == shader && (*it).matIndex == matIdx) 1.601 + { 1.602 + // we have one, we can append our data to it 1.603 + mesh = &(*it); 1.604 + } 1.605 + } 1.606 + if (!mesh) 1.607 + { 1.608 + meshes.push_back(MeshInfo(PatchType_Simple,false)); 1.609 + mesh = &meshes.back(); 1.610 + mesh->matIndex = matIdx; 1.611 + 1.612 + // We need to add a new mesh to the list. We assign 1.613 + // an unique name to it to make sure the scene will 1.614 + // pass the validation step for the moment. 1.615 + // TODO: fix naming of objects in the scenegraph later 1.616 + if (objectName.length()) 1.617 + { 1.618 + ::strcpy(mesh->name,objectName.c_str()); 1.619 + ASSIMP_itoa10(&mesh->name[objectName.length()],30,subMeshIdx++); 1.620 + } 1.621 + 1.622 + // copy the shader to the mesh. 1.623 + mesh->shader = shader; 1.624 + } 1.625 + 1.626 + // fill the mesh with data 1.627 + if (!tempIdx.empty()) 1.628 + { 1.629 + mesh->faces.push_back((unsigned int)tempIdx.size()); 1.630 + for (std::vector<unsigned int>::const_iterator it = tempIdx.begin(), end = tempIdx.end(); 1.631 + it != end;++it) 1.632 + { 1.633 + m = *it; 1.634 + 1.635 + // copy colors -vertex color specifications override polygon color specifications 1.636 + if (hasColor) 1.637 + { 1.638 + const aiColor4D& clr = tempColors[m]; 1.639 + mesh->colors.push_back((is_qnan( clr.r ) ? c : clr)); 1.640 + } 1.641 + 1.642 + // positions should always be there 1.643 + mesh->vertices.push_back (tempPositions[m]); 1.644 + 1.645 + // copy normal vectors 1.646 + if (hasNormals) 1.647 + mesh->normals.push_back (tempNormals[m]); 1.648 + 1.649 + // copy texture coordinates 1.650 + if (hasUVs) 1.651 + mesh->uvs.push_back (tempTextureCoords[m]); 1.652 + } 1.653 + } 1.654 + } 1.655 + if (!num)throw DeadlyImportError("NFF2: There are zero faces"); 1.656 + } 1.657 + } 1.658 + camLookAt = camLookAt + camPos; 1.659 + } 1.660 + else // "Normal" Neutral file format that is quite more common 1.661 + { 1.662 + while (GetNextLine(buffer,line)) 1.663 + { 1.664 + sz = line; 1.665 + if ('p' == line[0] || TokenMatch(sz,"tpp",3)) 1.666 + { 1.667 + MeshInfo* out = NULL; 1.668 + 1.669 + // 'tpp' - texture polygon patch primitive 1.670 + if ('t' == line[0]) 1.671 + { 1.672 + currentMeshWithUVCoords = NULL; 1.673 + for (std::vector<MeshInfo>::iterator it = meshesWithUVCoords.begin(), end = meshesWithUVCoords.end(); 1.674 + it != end;++it) 1.675 + { 1.676 + if ((*it).shader == s) 1.677 + { 1.678 + currentMeshWithUVCoords = &(*it); 1.679 + break; 1.680 + } 1.681 + } 1.682 + 1.683 + if (!currentMeshWithUVCoords) 1.684 + { 1.685 + meshesWithUVCoords.push_back(MeshInfo(PatchType_UVAndNormals)); 1.686 + currentMeshWithUVCoords = &meshesWithUVCoords.back(); 1.687 + currentMeshWithUVCoords->shader = s; 1.688 + } 1.689 + out = currentMeshWithUVCoords; 1.690 + } 1.691 + // 'pp' - polygon patch primitive 1.692 + else if ('p' == line[1]) 1.693 + { 1.694 + currentMeshWithNormals = NULL; 1.695 + for (std::vector<MeshInfo>::iterator it = meshesWithNormals.begin(), end = meshesWithNormals.end(); 1.696 + it != end;++it) 1.697 + { 1.698 + if ((*it).shader == s) 1.699 + { 1.700 + currentMeshWithNormals = &(*it); 1.701 + break; 1.702 + } 1.703 + } 1.704 + 1.705 + if (!currentMeshWithNormals) 1.706 + { 1.707 + meshesWithNormals.push_back(MeshInfo(PatchType_Normals)); 1.708 + currentMeshWithNormals = &meshesWithNormals.back(); 1.709 + currentMeshWithNormals->shader = s; 1.710 + } 1.711 + sz = &line[2];out = currentMeshWithNormals; 1.712 + } 1.713 + // 'p' - polygon primitive 1.714 + else 1.715 + { 1.716 + currentMesh = NULL; 1.717 + for (std::vector<MeshInfo>::iterator it = meshes.begin(), end = meshes.end(); 1.718 + it != end;++it) 1.719 + { 1.720 + if ((*it).shader == s) 1.721 + { 1.722 + currentMesh = &(*it); 1.723 + break; 1.724 + } 1.725 + } 1.726 + 1.727 + if (!currentMesh) 1.728 + { 1.729 + meshes.push_back(MeshInfo(PatchType_Simple)); 1.730 + currentMesh = &meshes.back(); 1.731 + currentMesh->shader = s; 1.732 + } 1.733 + sz = &line[1];out = currentMesh; 1.734 + } 1.735 + SkipSpaces(sz,&sz); 1.736 + m = strtoul10(sz); 1.737 + 1.738 + // ---- flip the face order 1.739 + out->vertices.resize(out->vertices.size()+m); 1.740 + if (out != currentMesh) 1.741 + { 1.742 + out->normals.resize(out->vertices.size()); 1.743 + } 1.744 + if (out == currentMeshWithUVCoords) 1.745 + { 1.746 + out->uvs.resize(out->vertices.size()); 1.747 + } 1.748 + for (unsigned int n = 0; n < m;++n) 1.749 + { 1.750 + if(!GetNextLine(buffer,line)) 1.751 + { 1.752 + DefaultLogger::get()->error("NFF: Unexpected EOF was encountered. Patch definition incomplete"); 1.753 + continue; 1.754 + } 1.755 + 1.756 + aiVector3D v; sz = &line[0]; 1.757 + AI_NFF_PARSE_TRIPLE(v); 1.758 + out->vertices[out->vertices.size()-n-1] = v; 1.759 + 1.760 + if (out != currentMesh) 1.761 + { 1.762 + AI_NFF_PARSE_TRIPLE(v); 1.763 + out->normals[out->vertices.size()-n-1] = v; 1.764 + } 1.765 + if (out == currentMeshWithUVCoords) 1.766 + { 1.767 + // FIX: in one test file this wraps over multiple lines 1.768 + SkipSpaces(&sz); 1.769 + if (IsLineEnd(*sz)) 1.770 + { 1.771 + GetNextLine(buffer,line); 1.772 + sz = line; 1.773 + } 1.774 + AI_NFF_PARSE_FLOAT(v.x); 1.775 + SkipSpaces(&sz); 1.776 + if (IsLineEnd(*sz)) 1.777 + { 1.778 + GetNextLine(buffer,line); 1.779 + sz = line; 1.780 + } 1.781 + AI_NFF_PARSE_FLOAT(v.y); 1.782 + v.y = 1.f - v.y; 1.783 + out->uvs[out->vertices.size()-n-1] = v; 1.784 + } 1.785 + } 1.786 + out->faces.push_back(m); 1.787 + } 1.788 + // 'f' - shading information block 1.789 + else if (TokenMatch(sz,"f",1)) 1.790 + { 1.791 + float d; 1.792 + 1.793 + // read the RGB colors 1.794 + AI_NFF_PARSE_TRIPLE(s.color); 1.795 + 1.796 + // read the other properties 1.797 + AI_NFF_PARSE_FLOAT(s.diffuse.r); 1.798 + AI_NFF_PARSE_FLOAT(s.specular.r); 1.799 + AI_NFF_PARSE_FLOAT(d); // skip shininess and transmittance 1.800 + AI_NFF_PARSE_FLOAT(d); 1.801 + AI_NFF_PARSE_FLOAT(s.refracti); 1.802 + 1.803 + // NFF2 uses full colors here so we need to use them too 1.804 + // although NFF uses simple scaling factors 1.805 + s.diffuse.g = s.diffuse.b = s.diffuse.r; 1.806 + s.specular.g = s.specular.b = s.specular.r; 1.807 + 1.808 + // if the next one is NOT a number we assume it is a texture file name 1.809 + // this feature is used by some NFF files on the internet and it has 1.810 + // been implemented as it can be really useful 1.811 + SkipSpaces(&sz); 1.812 + if (!IsNumeric(*sz)) 1.813 + { 1.814 + // TODO: Support full file names with spaces and quotation marks ... 1.815 + const char* p = sz; 1.816 + while (!IsSpaceOrNewLine( *sz ))++sz; 1.817 + 1.818 + unsigned int diff = (unsigned int)(sz-p); 1.819 + if (diff) 1.820 + { 1.821 + s.texFile = std::string(p,diff); 1.822 + } 1.823 + } 1.824 + else 1.825 + { 1.826 + AI_NFF_PARSE_FLOAT(s.ambient); // optional 1.827 + } 1.828 + } 1.829 + // 'shader' - other way to specify a texture 1.830 + else if (TokenMatch(sz,"shader",6)) 1.831 + { 1.832 + SkipSpaces(&sz); 1.833 + const char* old = sz; 1.834 + while (!IsSpaceOrNewLine(*sz))++sz; 1.835 + s.texFile = std::string(old, (uintptr_t)sz - (uintptr_t)old); 1.836 + } 1.837 + // 'l' - light source 1.838 + else if (TokenMatch(sz,"l",1)) 1.839 + { 1.840 + lights.push_back(Light()); 1.841 + Light& light = lights.back(); 1.842 + 1.843 + AI_NFF_PARSE_TRIPLE(light.position); 1.844 + AI_NFF_PARSE_FLOAT (light.intensity); 1.845 + AI_NFF_PARSE_TRIPLE(light.color); 1.846 + } 1.847 + // 's' - sphere 1.848 + else if (TokenMatch(sz,"s",1)) 1.849 + { 1.850 + meshesLocked.push_back(MeshInfo(PatchType_Simple,true)); 1.851 + MeshInfo& currentMesh = meshesLocked.back(); 1.852 + currentMesh.shader = s; 1.853 + currentMesh.shader.mapping = aiTextureMapping_SPHERE; 1.854 + 1.855 + AI_NFF_PARSE_SHAPE_INFORMATION(); 1.856 + 1.857 + // we don't need scaling or translation here - we do it in the node's transform 1.858 + StandardShapes::MakeSphere(iTesselation, currentMesh.vertices); 1.859 + currentMesh.faces.resize(currentMesh.vertices.size()/3,3); 1.860 + 1.861 + // generate a name for the mesh 1.862 + ::sprintf(currentMesh.name,"sphere_%i",sphere++); 1.863 + } 1.864 + // 'dod' - dodecahedron 1.865 + else if (TokenMatch(sz,"dod",3)) 1.866 + { 1.867 + meshesLocked.push_back(MeshInfo(PatchType_Simple,true)); 1.868 + MeshInfo& currentMesh = meshesLocked.back(); 1.869 + currentMesh.shader = s; 1.870 + currentMesh.shader.mapping = aiTextureMapping_SPHERE; 1.871 + 1.872 + AI_NFF_PARSE_SHAPE_INFORMATION(); 1.873 + 1.874 + // we don't need scaling or translation here - we do it in the node's transform 1.875 + StandardShapes::MakeDodecahedron(currentMesh.vertices); 1.876 + currentMesh.faces.resize(currentMesh.vertices.size()/3,3); 1.877 + 1.878 + // generate a name for the mesh 1.879 + ::sprintf(currentMesh.name,"dodecahedron_%i",dodecahedron++); 1.880 + } 1.881 + 1.882 + // 'oct' - octahedron 1.883 + else if (TokenMatch(sz,"oct",3)) 1.884 + { 1.885 + meshesLocked.push_back(MeshInfo(PatchType_Simple,true)); 1.886 + MeshInfo& currentMesh = meshesLocked.back(); 1.887 + currentMesh.shader = s; 1.888 + currentMesh.shader.mapping = aiTextureMapping_SPHERE; 1.889 + 1.890 + AI_NFF_PARSE_SHAPE_INFORMATION(); 1.891 + 1.892 + // we don't need scaling or translation here - we do it in the node's transform 1.893 + StandardShapes::MakeOctahedron(currentMesh.vertices); 1.894 + currentMesh.faces.resize(currentMesh.vertices.size()/3,3); 1.895 + 1.896 + // generate a name for the mesh 1.897 + ::sprintf(currentMesh.name,"octahedron_%i",octahedron++); 1.898 + } 1.899 + 1.900 + // 'tet' - tetrahedron 1.901 + else if (TokenMatch(sz,"tet",3)) 1.902 + { 1.903 + meshesLocked.push_back(MeshInfo(PatchType_Simple,true)); 1.904 + MeshInfo& currentMesh = meshesLocked.back(); 1.905 + currentMesh.shader = s; 1.906 + currentMesh.shader.mapping = aiTextureMapping_SPHERE; 1.907 + 1.908 + AI_NFF_PARSE_SHAPE_INFORMATION(); 1.909 + 1.910 + // we don't need scaling or translation here - we do it in the node's transform 1.911 + StandardShapes::MakeTetrahedron(currentMesh.vertices); 1.912 + currentMesh.faces.resize(currentMesh.vertices.size()/3,3); 1.913 + 1.914 + // generate a name for the mesh 1.915 + ::sprintf(currentMesh.name,"tetrahedron_%i",tetrahedron++); 1.916 + } 1.917 + 1.918 + // 'hex' - hexahedron 1.919 + else if (TokenMatch(sz,"hex",3)) 1.920 + { 1.921 + meshesLocked.push_back(MeshInfo(PatchType_Simple,true)); 1.922 + MeshInfo& currentMesh = meshesLocked.back(); 1.923 + currentMesh.shader = s; 1.924 + currentMesh.shader.mapping = aiTextureMapping_BOX; 1.925 + 1.926 + AI_NFF_PARSE_SHAPE_INFORMATION(); 1.927 + 1.928 + // we don't need scaling or translation here - we do it in the node's transform 1.929 + StandardShapes::MakeHexahedron(currentMesh.vertices); 1.930 + currentMesh.faces.resize(currentMesh.vertices.size()/3,3); 1.931 + 1.932 + // generate a name for the mesh 1.933 + ::sprintf(currentMesh.name,"hexahedron_%i",hexahedron++); 1.934 + } 1.935 + // 'c' - cone 1.936 + else if (TokenMatch(sz,"c",1)) 1.937 + { 1.938 + meshesLocked.push_back(MeshInfo(PatchType_Simple,true)); 1.939 + MeshInfo& currentMesh = meshesLocked.back(); 1.940 + currentMesh.shader = s; 1.941 + currentMesh.shader.mapping = aiTextureMapping_CYLINDER; 1.942 + 1.943 + if(!GetNextLine(buffer,line)) 1.944 + { 1.945 + DefaultLogger::get()->error("NFF: Unexpected end of file (cone definition not complete)"); 1.946 + break; 1.947 + } 1.948 + sz = line; 1.949 + 1.950 + // read the two center points and the respective radii 1.951 + aiVector3D center1, center2; float radius1, radius2; 1.952 + AI_NFF_PARSE_TRIPLE(center1); 1.953 + AI_NFF_PARSE_FLOAT(radius1); 1.954 + 1.955 + if(!GetNextLine(buffer,line)) 1.956 + { 1.957 + DefaultLogger::get()->error("NFF: Unexpected end of file (cone definition not complete)"); 1.958 + break; 1.959 + } 1.960 + sz = line; 1.961 + 1.962 + AI_NFF_PARSE_TRIPLE(center2); 1.963 + AI_NFF_PARSE_FLOAT(radius2); 1.964 + 1.965 + // compute the center point of the cone/cylinder - 1.966 + // it is its local transformation origin 1.967 + currentMesh.dir = center2-center1; 1.968 + currentMesh.center = center1+currentMesh.dir/2.f; 1.969 + 1.970 + float f; 1.971 + if (( f = currentMesh.dir.Length()) < 10e-3f ) 1.972 + { 1.973 + DefaultLogger::get()->error("NFF: Cone height is close to zero"); 1.974 + continue; 1.975 + } 1.976 + currentMesh.dir /= f; // normalize 1.977 + 1.978 + // generate the cone - it consists of simple triangles 1.979 + StandardShapes::MakeCone(f, radius1, radius2, 1.980 + integer_pow(4, iTesselation), currentMesh.vertices); 1.981 + 1.982 + // MakeCone() returns tris 1.983 + currentMesh.faces.resize(currentMesh.vertices.size()/3,3); 1.984 + 1.985 + // generate a name for the mesh. 'cone' if it a cone, 1.986 + // 'cylinder' if it is a cylinder. Funny, isn't it? 1.987 + if (radius1 != radius2) 1.988 + ::sprintf(currentMesh.name,"cone_%i",cone++); 1.989 + else ::sprintf(currentMesh.name,"cylinder_%i",cylinder++); 1.990 + } 1.991 + // 'tess' - tesselation 1.992 + else if (TokenMatch(sz,"tess",4)) 1.993 + { 1.994 + SkipSpaces(&sz); 1.995 + iTesselation = strtoul10(sz); 1.996 + } 1.997 + // 'from' - camera position 1.998 + else if (TokenMatch(sz,"from",4)) 1.999 + { 1.1000 + AI_NFF_PARSE_TRIPLE(camPos); 1.1001 + hasCam = true; 1.1002 + } 1.1003 + // 'at' - camera look-at vector 1.1004 + else if (TokenMatch(sz,"at",2)) 1.1005 + { 1.1006 + AI_NFF_PARSE_TRIPLE(camLookAt); 1.1007 + hasCam = true; 1.1008 + } 1.1009 + // 'up' - camera up vector 1.1010 + else if (TokenMatch(sz,"up",2)) 1.1011 + { 1.1012 + AI_NFF_PARSE_TRIPLE(camUp); 1.1013 + hasCam = true; 1.1014 + } 1.1015 + // 'angle' - (half?) camera field of view 1.1016 + else if (TokenMatch(sz,"angle",5)) 1.1017 + { 1.1018 + AI_NFF_PARSE_FLOAT(angle); 1.1019 + hasCam = true; 1.1020 + } 1.1021 + // 'resolution' - used to compute the screen aspect 1.1022 + else if (TokenMatch(sz,"resolution",10)) 1.1023 + { 1.1024 + AI_NFF_PARSE_FLOAT(resolution.x); 1.1025 + AI_NFF_PARSE_FLOAT(resolution.y); 1.1026 + hasCam = true; 1.1027 + } 1.1028 + // 'pb' - bezier patch. Not supported yet 1.1029 + else if (TokenMatch(sz,"pb",2)) 1.1030 + { 1.1031 + DefaultLogger::get()->error("NFF: Encountered unsupported ID: bezier patch"); 1.1032 + } 1.1033 + // 'pn' - NURBS. Not supported yet 1.1034 + else if (TokenMatch(sz,"pn",2) || TokenMatch(sz,"pnn",3)) 1.1035 + { 1.1036 + DefaultLogger::get()->error("NFF: Encountered unsupported ID: NURBS"); 1.1037 + } 1.1038 + // '' - comment 1.1039 + else if ('#' == line[0]) 1.1040 + { 1.1041 + const char* sz;SkipSpaces(&line[1],&sz); 1.1042 + if (!IsLineEnd(*sz))DefaultLogger::get()->info(sz); 1.1043 + } 1.1044 + } 1.1045 + } 1.1046 + 1.1047 + // copy all arrays into one large 1.1048 + meshes.reserve (meshes.size()+meshesLocked.size()+meshesWithNormals.size()+meshesWithUVCoords.size()); 1.1049 + meshes.insert (meshes.end(),meshesLocked.begin(),meshesLocked.end()); 1.1050 + meshes.insert (meshes.end(),meshesWithNormals.begin(),meshesWithNormals.end()); 1.1051 + meshes.insert (meshes.end(),meshesWithUVCoords.begin(),meshesWithUVCoords.end()); 1.1052 + 1.1053 + // now generate output meshes. first find out how many meshes we'll need 1.1054 + std::vector<MeshInfo>::const_iterator it = meshes.begin(), end = meshes.end(); 1.1055 + for (;it != end;++it) 1.1056 + { 1.1057 + if (!(*it).faces.empty()) 1.1058 + { 1.1059 + ++pScene->mNumMeshes; 1.1060 + if ((*it).name[0])++numNamed; 1.1061 + } 1.1062 + } 1.1063 + 1.1064 + // generate a dummy root node - assign all unnamed elements such 1.1065 + // as polygons and polygon patches to the root node and generate 1.1066 + // sub nodes for named objects such as spheres and cones. 1.1067 + aiNode* const root = new aiNode(); 1.1068 + root->mName.Set("<NFF_Root>"); 1.1069 + root->mNumChildren = numNamed + (hasCam ? 1 : 0) + (unsigned int) lights.size(); 1.1070 + root->mNumMeshes = pScene->mNumMeshes-numNamed; 1.1071 + 1.1072 + aiNode** ppcChildren = NULL; 1.1073 + unsigned int* pMeshes = NULL; 1.1074 + if (root->mNumMeshes) 1.1075 + pMeshes = root->mMeshes = new unsigned int[root->mNumMeshes]; 1.1076 + if (root->mNumChildren) 1.1077 + ppcChildren = root->mChildren = new aiNode*[root->mNumChildren]; 1.1078 + 1.1079 + // generate the camera 1.1080 + if (hasCam) 1.1081 + { 1.1082 + aiNode* nd = *ppcChildren = new aiNode(); 1.1083 + nd->mName.Set("<NFF_Camera>"); 1.1084 + nd->mParent = root; 1.1085 + 1.1086 + // allocate the camera in the scene 1.1087 + pScene->mNumCameras = 1; 1.1088 + pScene->mCameras = new aiCamera*[1]; 1.1089 + aiCamera* c = pScene->mCameras[0] = new aiCamera; 1.1090 + 1.1091 + c->mName = nd->mName; // make sure the names are identical 1.1092 + c->mHorizontalFOV = AI_DEG_TO_RAD( angle ); 1.1093 + c->mLookAt = camLookAt - camPos; 1.1094 + c->mPosition = camPos; 1.1095 + c->mUp = camUp; 1.1096 + 1.1097 + // If the resolution is not specified in the file, we 1.1098 + // need to set 1.0 as aspect. 1.1099 + c->mAspect = (!resolution.y ? 0.f : resolution.x / resolution.y); 1.1100 + ++ppcChildren; 1.1101 + } 1.1102 + 1.1103 + // generate light sources 1.1104 + if (!lights.empty()) 1.1105 + { 1.1106 + pScene->mNumLights = (unsigned int)lights.size(); 1.1107 + pScene->mLights = new aiLight*[pScene->mNumLights]; 1.1108 + for (unsigned int i = 0; i < pScene->mNumLights;++i,++ppcChildren) 1.1109 + { 1.1110 + const Light& l = lights[i]; 1.1111 + 1.1112 + aiNode* nd = *ppcChildren = new aiNode(); 1.1113 + nd->mParent = root; 1.1114 + 1.1115 + nd->mName.length = ::sprintf(nd->mName.data,"<NFF_Light%i>",i); 1.1116 + 1.1117 + // allocate the light in the scene data structure 1.1118 + aiLight* out = pScene->mLights[i] = new aiLight(); 1.1119 + out->mName = nd->mName; // make sure the names are identical 1.1120 + out->mType = aiLightSource_POINT; 1.1121 + out->mColorDiffuse = out->mColorSpecular = l.color * l.intensity; 1.1122 + out->mPosition = l.position; 1.1123 + } 1.1124 + } 1.1125 + 1.1126 + if (!pScene->mNumMeshes)throw DeadlyImportError("NFF: No meshes loaded"); 1.1127 + pScene->mMeshes = new aiMesh*[pScene->mNumMeshes]; 1.1128 + pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = pScene->mNumMeshes]; 1.1129 + for (it = meshes.begin(), m = 0; it != end;++it) 1.1130 + { 1.1131 + if ((*it).faces.empty())continue; 1.1132 + 1.1133 + const MeshInfo& src = *it; 1.1134 + aiMesh* const mesh = pScene->mMeshes[m] = new aiMesh(); 1.1135 + mesh->mNumVertices = (unsigned int)src.vertices.size(); 1.1136 + mesh->mNumFaces = (unsigned int)src.faces.size(); 1.1137 + 1.1138 + // Generate sub nodes for named meshes 1.1139 + if (src.name[0]) 1.1140 + { 1.1141 + aiNode* const node = *ppcChildren = new aiNode(); 1.1142 + node->mParent = root; 1.1143 + node->mNumMeshes = 1; 1.1144 + node->mMeshes = new unsigned int[1]; 1.1145 + node->mMeshes[0] = m; 1.1146 + node->mName.Set(src.name); 1.1147 + 1.1148 + // setup the transformation matrix of the node 1.1149 + aiMatrix4x4::FromToMatrix(aiVector3D(0.f,1.f,0.f), 1.1150 + src.dir,node->mTransformation); 1.1151 + 1.1152 + aiMatrix4x4& mat = node->mTransformation; 1.1153 + mat.a1 *= src.radius.x; mat.b1 *= src.radius.x; mat.c1 *= src.radius.x; 1.1154 + mat.a2 *= src.radius.y; mat.b2 *= src.radius.y; mat.c2 *= src.radius.y; 1.1155 + mat.a3 *= src.radius.z; mat.b3 *= src.radius.z; mat.c3 *= src.radius.z; 1.1156 + mat.a4 = src.center.x; 1.1157 + mat.b4 = src.center.y; 1.1158 + mat.c4 = src.center.z; 1.1159 + 1.1160 + ++ppcChildren; 1.1161 + } 1.1162 + else *pMeshes++ = m; 1.1163 + 1.1164 + // copy vertex positions 1.1165 + mesh->mVertices = new aiVector3D[mesh->mNumVertices]; 1.1166 + ::memcpy(mesh->mVertices,&src.vertices[0], 1.1167 + sizeof(aiVector3D)*mesh->mNumVertices); 1.1168 + 1.1169 + // NFF2: there could be vertex colors 1.1170 + if (!src.colors.empty()) 1.1171 + { 1.1172 + ai_assert(src.colors.size() == src.vertices.size()); 1.1173 + 1.1174 + // copy vertex colors 1.1175 + mesh->mColors[0] = new aiColor4D[mesh->mNumVertices]; 1.1176 + ::memcpy(mesh->mColors[0],&src.colors[0], 1.1177 + sizeof(aiColor4D)*mesh->mNumVertices); 1.1178 + } 1.1179 + 1.1180 + if (!src.normals.empty()) 1.1181 + { 1.1182 + ai_assert(src.normals.size() == src.vertices.size()); 1.1183 + 1.1184 + // copy normal vectors 1.1185 + mesh->mNormals = new aiVector3D[mesh->mNumVertices]; 1.1186 + ::memcpy(mesh->mNormals,&src.normals[0], 1.1187 + sizeof(aiVector3D)*mesh->mNumVertices); 1.1188 + } 1.1189 + 1.1190 + if (!src.uvs.empty()) 1.1191 + { 1.1192 + ai_assert(src.uvs.size() == src.vertices.size()); 1.1193 + 1.1194 + // copy texture coordinates 1.1195 + mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices]; 1.1196 + ::memcpy(mesh->mTextureCoords[0],&src.uvs[0], 1.1197 + sizeof(aiVector3D)*mesh->mNumVertices); 1.1198 + } 1.1199 + 1.1200 + // generate faces 1.1201 + unsigned int p = 0; 1.1202 + aiFace* pFace = mesh->mFaces = new aiFace[mesh->mNumFaces]; 1.1203 + for (std::vector<unsigned int>::const_iterator it2 = src.faces.begin(), 1.1204 + end2 = src.faces.end(); 1.1205 + it2 != end2;++it2,++pFace) 1.1206 + { 1.1207 + pFace->mIndices = new unsigned int [ pFace->mNumIndices = *it2 ]; 1.1208 + for (unsigned int o = 0; o < pFace->mNumIndices;++o) 1.1209 + pFace->mIndices[o] = p++; 1.1210 + } 1.1211 + 1.1212 + // generate a material for the mesh 1.1213 + aiMaterial* pcMat = (aiMaterial*)(pScene->mMaterials[m] = new aiMaterial()); 1.1214 + 1.1215 + mesh->mMaterialIndex = m++; 1.1216 + 1.1217 + aiString s; 1.1218 + s.Set(AI_DEFAULT_MATERIAL_NAME); 1.1219 + pcMat->AddProperty(&s, AI_MATKEY_NAME); 1.1220 + 1.1221 + // FIX: Ignore diffuse == 0 1.1222 + aiColor3D c = src.shader.color * (src.shader.diffuse.r ? src.shader.diffuse : aiColor3D(1.f,1.f,1.f)); 1.1223 + pcMat->AddProperty(&c,1,AI_MATKEY_COLOR_DIFFUSE); 1.1224 + c = src.shader.color * src.shader.specular; 1.1225 + pcMat->AddProperty(&c,1,AI_MATKEY_COLOR_SPECULAR); 1.1226 + 1.1227 + // NFF2 - default values for NFF 1.1228 + pcMat->AddProperty(&src.shader.ambient, 1,AI_MATKEY_COLOR_AMBIENT); 1.1229 + pcMat->AddProperty(&src.shader.emissive,1,AI_MATKEY_COLOR_EMISSIVE); 1.1230 + pcMat->AddProperty(&src.shader.opacity, 1,AI_MATKEY_OPACITY); 1.1231 + 1.1232 + // setup the first texture layer, if existing 1.1233 + if (src.shader.texFile.length()) 1.1234 + { 1.1235 + s.Set(src.shader.texFile); 1.1236 + pcMat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0)); 1.1237 + 1.1238 + if (aiTextureMapping_UV != src.shader.mapping) { 1.1239 + 1.1240 + aiVector3D v(0.f,-1.f,0.f); 1.1241 + pcMat->AddProperty(&v, 1,AI_MATKEY_TEXMAP_AXIS_DIFFUSE(0)); 1.1242 + pcMat->AddProperty((int*)&src.shader.mapping, 1,AI_MATKEY_MAPPING_DIFFUSE(0)); 1.1243 + } 1.1244 + } 1.1245 + 1.1246 + // setup the name of the material 1.1247 + if (src.shader.name.length()) 1.1248 + { 1.1249 + s.Set(src.shader.texFile); 1.1250 + pcMat->AddProperty(&s,AI_MATKEY_NAME); 1.1251 + } 1.1252 + 1.1253 + // setup some more material properties that are specific to NFF2 1.1254 + int i; 1.1255 + if (src.shader.twoSided) 1.1256 + { 1.1257 + i = 1; 1.1258 + pcMat->AddProperty(&i,1,AI_MATKEY_TWOSIDED); 1.1259 + } 1.1260 + i = (src.shader.shaded ? aiShadingMode_Gouraud : aiShadingMode_NoShading); 1.1261 + if (src.shader.shininess) 1.1262 + { 1.1263 + i = aiShadingMode_Phong; 1.1264 + pcMat->AddProperty(&src.shader.shininess,1,AI_MATKEY_SHININESS); 1.1265 + } 1.1266 + pcMat->AddProperty(&i,1,AI_MATKEY_SHADING_MODEL); 1.1267 + } 1.1268 + pScene->mRootNode = root; 1.1269 +} 1.1270 + 1.1271 +#endif // !! ASSIMP_BUILD_NO_NFF_IMPORTER