diff --git a/code/NFFLoader.cpp b/code/NFFLoader.cpp index 45977a7ce..467c2f247 100644 --- a/code/NFFLoader.cpp +++ b/code/NFFLoader.cpp @@ -90,14 +90,15 @@ bool NFFImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const // ------------------------------------------------------------------------------------------------ bool GetNextLine(const char*& buffer, char out[4096]) { + if ('\0' == *buffer)return false; + char* _out = out; char* const end = _out+4096; while (!IsLineEnd( *buffer ) && _out < end) *_out++ = *buffer++; *_out = '\0'; - if ('\0' == *buffer)return false; - while (IsLineEnd( *buffer ))++buffer; + if ('\0' != *buffer)while (IsLineEnd( *buffer ))++buffer; return true; } @@ -150,6 +151,9 @@ void NFFImporter::InternReadFile( const std::string& pFile, ShadingInfo s; // current material info + // degree of tesselation + unsigned int iTesselation = 4; + char line[4096]; const char* sz; unsigned int sphere = 0,cylinder = 0,cone = 0,numNamed = 0; @@ -182,25 +186,26 @@ void NFFImporter::InternReadFile( const std::string& pFile, SkipSpaces(sz,&sz); m = strtol10(sz); - out->faces.push_back(m); + out->vertices.reserve(out->vertices.size()+m); for (unsigned int n = 0; n < m;++n) { if(!GetNextLine(buffer,line)) { DefaultLogger::get()->error("NFF: Unexpected EOF was encountered"); - break; + continue; } aiVector3D v; sz = &line[0]; AI_NFF_PARSE_TRIPLE(v); out->vertices.push_back(v); - if (&meshesWithNormals.back() == out) + if ('p' == line[1]) { AI_NFF_PARSE_TRIPLE(v); out->normals.push_back(v); } } + out->faces.push_back(m); } // 'f' - shading information block else if ('f' == line[0] && IsSpace(line[1])) @@ -254,12 +259,18 @@ void NFFImporter::InternReadFile( const std::string& pFile, AI_NFF_PARSE_SHAPE_INFORMATION(); // generate the sphere - it consists of simple triangles - StandardShapes::MakeSphere(aiVector3D(), radius, 500.0f, currentMesh.vertices); - currentMesh.faces.resize(currentMesh.vertices.size(),3); + StandardShapes::MakeSphere(aiVector3D(), radius, iTesselation, currentMesh.vertices); + currentMesh.faces.resize(currentMesh.vertices.size()/3,3); // generate a name for the mesh ::sprintf(currentMesh.name,"sphere_%i",sphere++); } + // 'tess' - tesselation + else if (!strncmp(line,"tess",4) && IsSpace(line[4])) + { + sz = &line[5];SkipSpaces(&sz); + iTesselation = strtol10(sz); + } // 'c' - cone else if ('c' == line[0] && IsSpace(line[1])) { @@ -280,8 +291,8 @@ void NFFImporter::InternReadFile( const std::string& pFile, center1 = -center2; // generate the cone - it consists of simple triangles - StandardShapes::MakeCone(center1, radius1, center2, radius2, 500.0f, currentMesh.vertices); - currentMesh.faces.resize(currentMesh.vertices.size(),3); + StandardShapes::MakeCone(center1, radius1, center2, radius2, iTesselation, currentMesh.vertices); + currentMesh.faces.resize(currentMesh.vertices.size()/3,3); // generate a name for the mesh if (radius1 != radius2) @@ -384,7 +395,9 @@ void NFFImporter::InternReadFile( const std::string& pFile, // generate a material for the mesh MaterialHelper* pcMat = (MaterialHelper*)(pScene-> - mMaterials[m++] = new MaterialHelper()); + mMaterials[m] = new MaterialHelper()); + + mesh->mMaterialIndex = m++; aiString s; s.Set(AI_DEFAULT_MATERIAL_NAME); diff --git a/code/StandardShapes.cpp b/code/StandardShapes.cpp index e696f78e0..29406a6f1 100644 Binary files a/code/StandardShapes.cpp and b/code/StandardShapes.cpp differ diff --git a/code/StandardShapes.h b/code/StandardShapes.h index 213dcdd19..194d44870 100644 --- a/code/StandardShapes.h +++ b/code/StandardShapes.h @@ -69,14 +69,33 @@ public: std::vector& positions); + /** @brief Generates a dodecahedron + * + * @param center Center point of the dodecahedron + * @param length Face length of the dodecahedron + * @param positions Receives output triangles. + */ + static void MakeDodecahedron(aiVector3D& center,float length, + std::vector& positions); + + /** @brief Generates an octahedron + * + * @param center Center point of the octahedron + * @param length Face length of the octahedron + * @param positions Receives output triangles. + */ + static void MakeOctahedron(aiVector3D& center,float length, + std::vector& positions); + + /** @brief Generates a sphere * * @param center Center point of the sphere * @param radius Radius of the sphere - * @param tess Number of triangles in the equator line of the sphere + * @param tess Number of subdivions - 0 generates a octahedron * @param positions Receives output triangles. */ - static void MakeSphere(aiVector3D& center,float radius,float tess, + static void MakeSphere(aiVector3D& center,float radius,unsigned int tess, std::vector& positions); /** @brief Generates a cone or a cylinder, either opened or closed. @@ -100,12 +119,12 @@ public: * @param radius1 First radius * @param center2 Second center point * @param radius2 Second radius - * @param tess Number of triangles per circle + * @param tess Number of subdivisions * @param bOpened true for an open cone/cylinder. * @param positions Receives output triangles. */ static void MakeCone(aiVector3D& center1,float radius1, - aiVector3D& center2,float radius2,float tess, + aiVector3D& center2,float radius2,unsigned int tess, std::vector& positions,bool bOpened = false); /** @brief Generates a flat circle @@ -115,11 +134,11 @@ public: * @param normal Normal vector of the circle. * This is also the normal vector of all triangles generated by * this function. - * @param tess Number of triangles + * @param tess Number of triangles * @param positions Receives output triangles. */ static void MakeCircle(aiVector3D& center, aiVector3D& normal, - float radius, float tess, + float radius, unsigned int tess, std::vector& positions); }; diff --git a/test/NFF/positionTest.nff b/test/NFF/positionTest.nff new file mode 100644 index 000000000..17c62b3f0 --- /dev/null +++ b/test/NFF/positionTest.nff @@ -0,0 +1,10 @@ + +# a centered sphere +s 0 0 0 5 + +# a polgyon that should not be visible +p 4 +5 0 0 +0 -5 0 +-5 0 0 +0 5 0 \ No newline at end of file diff --git a/test/NFF/spheres.nff b/test/NFF/spheres.nff index b432d140c..a029e555f 100644 --- a/test/NFF/spheres.nff +++ b/test/NFF/spheres.nff @@ -2,7 +2,7 @@ #default color # A simple sphere -s 5.0 5.0 5.0 10.0 +s 5.0 5.0 5.0 3.0 #blue f 0.0 0.0 1.0 0 1 1