Add bare scene and parsing of primitives

parse lines, triangles and quads and add them to the scene
pull/289/head
diiigle 2013-12-29 20:20:23 +01:00
parent 646d0d29a4
commit f5ef8772d8
2 changed files with 139 additions and 3 deletions

View File

@ -54,14 +54,135 @@ void LDrawImporter::InternReadFile(const std::string& pFile,
boost::scoped_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
// Check whether we can read from the file
if (file.get() == NULL) {
throw DeadlyImportError("Failed to open LDraw file " + pFile + ".");
ThrowException("Failed to open LDraw file " + pFile + ".");
}
// Your task: fill pScene
// Throw a ImportErrorException with a meaningful (!) error message if
// something goes wrong.
std::vector<char> buffer;
TextFileToBuffer(file.get(), buffer);
std::vector<char> vecBuffer;
TextFileToBuffer(file.get(), vecBuffer);
const char * buffer = &vecBuffer[0];
//TODO estimate sizes
std::vector<aiVector3D> * vertices = new std::vector<aiVector3D>();
std::vector<aiFace> * faces = new std::vector<aiFace>();
unsigned int primitivesType = 0;
//iterate line by line
char line[4096];
while (GetNextLine(buffer, line))
{
//line pointer, used as iterator through the line
const char* lp = line;
std::string wholeline = std::string(line);
SkipSpaces(&lp);
if (IsLineEnd(*lp))continue;
if (IsNumeric(*lp)){
int command = ::atoi(lp);
++lp;
if (command == 0){
//its a comment
continue;
}
else if (command == 1){
//its a sub file reference, load it
}
else if (command == 2 || command == 3 || command == 4){
//its a line or a triangle or a quad
float * params = NULL;
//read a colour constant and 2 (line) or 3 (triangle) or 4 (quad) vertices
if (!ReadNumFloats(lp, params, 1 + (command * 3))){
ThrowException(Formatter::format("could not read ") << (1 + (command * 3)) <<" command parameter floats from the line '" << line<<"'");
}
unsigned int index = vertices->size();
//TODO colour @params[0]
vertices->push_back(aiVector3D(params[1], params[2], params[3]));
vertices->push_back(aiVector3D(params[4], params[5], params[6]));
aiFace f = aiFace();
f.mNumIndices = command;
f.mIndices = new unsigned int[command];
f.mIndices[0] = index;
f.mIndices[1] = index + 1;
if (command == 3 || command == 4){
vertices->push_back(aiVector3D(params[7], params[8], params[9]));
f.mIndices[2] = index + 2;
if (command == 3){
//its a triangle
primitivesType = primitivesType | aiPrimitiveType_TRIANGLE;
}
else if (command == 4){
//its a quad
vertices->push_back(aiVector3D(params[10], params[11], params[12]));
f.mIndices[3] = index + 3;
primitivesType = primitivesType | aiPrimitiveType_POLYGON;
}
}
else
{
//its a line
primitivesType = primitivesType | aiPrimitiveType_LINE;
}
faces->push_back(f);
}
else
{
//its an optional 'line' or an unknown command, ignore them
continue;
}
}
else
{
//Line starts not with an identifier
ThrowException("Line not starting with an Command Identifier");
}
}
//we did read the whole file, now build the scenegraph
pScene->mRootNode = new aiNode("<LDrawRoot>");
pScene->mRootNode->mTransformation = aiMatrix4x4();
pScene->mNumMeshes = 1;
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
pScene->mFlags = AI_SCENE_FLAGS_INCOMPLETE;
aiMesh* mesh = new aiMesh();
mesh->mNumFaces = faces->size();
mesh->mFaces = new aiFace[mesh->mNumFaces];
memcpy(mesh->mFaces, faces->data(), sizeof(aiFace) * mesh->mNumFaces);
mesh->mNumVertices = vertices->size();
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
::memcpy(mesh->mVertices, vertices->data(), sizeof(aiVector3D)* mesh->mNumVertices);
mesh->mPrimitiveTypes = primitivesType;
pScene->mMeshes[0] = mesh;
pScene->mRootNode->mNumMeshes = 1;
pScene->mRootNode->mMeshes = new unsigned int[pScene->mRootNode->mNumMeshes];
pScene->mRootNode->mMeshes[0] = 0;
}
bool LDrawImporter::ReadNumFloats(const char* line, float* & out, unsigned int num){
out = new float[num];
for (unsigned int i = 0; i < num; ++i){
std::string token = GetNextToken(line);
if (token == ""){
//unexpected end of line
return false;
}
out[i] = fast_atof(token.c_str());
}
return true;
}
#endif // !ASSIMP_BUILD_NO_LDR_IMPORTER

View File

@ -5,6 +5,9 @@
#define INCLUDED_AI_LDR_IMPORTER_H
#include "AssimpPCH.h"
#include "ParsingUtils.h"
#include "fast_atof.h"
#include "TinyFormatter.h"
namespace Assimp{
@ -31,6 +34,18 @@ namespace Assimp{
// -------------------------------------------------------------------
private:
// Read num floats from the buffer and store it in the out array
bool ReadNumFloats(const char* line, float* &out, unsigned int num);
// -------------------------------------------------------------------
//throw an DeadlyImportError with the specified Message
void ThrowException(const std::string &Message){
throw DeadlyImportError("LDraw: " + Message);
}
}; //end of class LDrawImporter
} // end of namespace Assimp