ply-importer: fix creation of vertex attributes.

pull/1378/head
Kim Kulling 2017-08-03 14:57:48 +02:00
parent 8478df7dbd
commit ba658e7813
2 changed files with 178 additions and 219 deletions

View File

@ -299,252 +299,205 @@ void PLYImporter::InternReadFile(const std::string& pFile,
} }
} }
void PLYImporter::LoadVertex(const PLY::Element* pcElement, const PLY::ElementInstance* instElement, unsigned int pos) void PLYImporter::LoadVertex(const PLY::Element* pcElement, const PLY::ElementInstance* instElement, unsigned int pos) {
{ ai_assert(NULL != pcElement);
ai_assert(NULL != pcElement); ai_assert(NULL != instElement);
ai_assert(NULL != instElement);
ai_uint aiPositions[3] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; ai_uint aiPositions[3] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
PLY::EDataType aiTypes[3] = { EDT_Char, EDT_Char, EDT_Char }; PLY::EDataType aiTypes[3] = { EDT_Char, EDT_Char, EDT_Char };
ai_uint aiNormal[3] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; ai_uint aiNormal[3] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
PLY::EDataType aiNormalTypes[3] = { EDT_Char, EDT_Char, EDT_Char }; PLY::EDataType aiNormalTypes[3] = { EDT_Char, EDT_Char, EDT_Char };
unsigned int aiColors[4] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; unsigned int aiColors[4] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
PLY::EDataType aiColorsTypes[4] = { EDT_Char, EDT_Char, EDT_Char, EDT_Char }; PLY::EDataType aiColorsTypes[4] = { EDT_Char, EDT_Char, EDT_Char, EDT_Char };
unsigned int aiTexcoord[2] = { 0xFFFFFFFF, 0xFFFFFFFF }; unsigned int aiTexcoord[2] = { 0xFFFFFFFF, 0xFFFFFFFF };
PLY::EDataType aiTexcoordTypes[2] = { EDT_Char, EDT_Char }; PLY::EDataType aiTexcoordTypes[2] = { EDT_Char, EDT_Char };
unsigned int cnt = 0; // now check whether which normal components are available
unsigned int _a( 0 ), cnt( 0 );
for ( std::vector<PLY::Property>::const_iterator a = pcElement->alProperties.begin();
a != pcElement->alProperties.end(); ++a, ++_a) {
if ((*a).bIsList) {
continue;
}
// now check whether which normal components are available // Positions
unsigned int _a = 0; if (PLY::EST_XCoord == (*a).Semantic) {
for (std::vector<PLY::Property>::const_iterator a = pcElement->alProperties.begin(); ++cnt;
a != pcElement->alProperties.end(); ++a, ++_a) aiPositions[0] = _a;
{ aiTypes[0] = (*a).eType;
if ((*a).bIsList)continue; } else if (PLY::EST_YCoord == (*a).Semantic) {
++cnt;
// Positions aiPositions[1] = _a;
if (PLY::EST_XCoord == (*a).Semantic) aiTypes[1] = (*a).eType;
{ } else if (PLY::EST_ZCoord == (*a).Semantic) {
cnt++; ++cnt;
aiPositions[0] = _a; aiPositions[2] = _a;
aiTypes[0] = (*a).eType; aiTypes[2] = (*a).eType;
} } else if (PLY::EST_XNormal == (*a).Semantic) {
else if (PLY::EST_YCoord == (*a).Semantic) // Normals
{ ++cnt;
cnt++; aiNormal[0] = _a;
aiPositions[1] = _a; aiNormalTypes[0] = (*a).eType;
aiTypes[1] = (*a).eType; } else if (PLY::EST_YNormal == (*a).Semantic) {
} ++cnt;
else if (PLY::EST_ZCoord == (*a).Semantic) aiNormal[1] = _a;
{ aiNormalTypes[1] = (*a).eType;
cnt++; } else if (PLY::EST_ZNormal == (*a).Semantic) {
aiPositions[2] = _a; ++cnt;
aiTypes[2] = (*a).eType; aiNormal[2] = _a;
aiNormalTypes[2] = (*a).eType;
} else if (PLY::EST_Red == (*a).Semantic) {
// Colors
++cnt;
aiColors[0] = _a;
aiColorsTypes[0] = (*a).eType;
} else if (PLY::EST_Green == (*a).Semantic) {
++cnt;
aiColors[1] = _a;
aiColorsTypes[1] = (*a).eType;
} else if (PLY::EST_Blue == (*a).Semantic) {
++cnt;
aiColors[2] = _a;
aiColorsTypes[2] = (*a).eType;
} else if (PLY::EST_Alpha == (*a).Semantic) {
++cnt;
aiColors[3] = _a;
aiColorsTypes[3] = (*a).eType;
} else if (PLY::EST_UTextureCoord == (*a).Semantic) {
// Texture coordinates
++cnt;
aiTexcoord[0] = _a;
aiTexcoordTypes[0] = (*a).eType;
} else if (PLY::EST_VTextureCoord == (*a).Semantic) {
++cnt;
aiTexcoord[1] = _a;
aiTexcoordTypes[1] = (*a).eType;
}
} }
// Normals // check whether we have a valid source for the vertex data
else if (PLY::EST_XNormal == (*a).Semantic) if (0 != cnt) {
{ // Position
cnt++; aiVector3D vOut;
aiNormal[0] = _a; if (0xFFFFFFFF != aiPositions[0]) {
aiNormalTypes[0] = (*a).eType; vOut.x = PLY::PropertyInstance::ConvertTo<ai_real>(
} GetProperty(instElement->alProperties, aiPositions[0]).avList.front(), aiTypes[0]);
else if (PLY::EST_YNormal == (*a).Semantic) }
{
cnt++;
aiNormal[1] = _a;
aiNormalTypes[1] = (*a).eType;
}
else if (PLY::EST_ZNormal == (*a).Semantic)
{
cnt++;
aiNormal[2] = _a;
aiNormalTypes[2] = (*a).eType;
}
// Colors
else if (PLY::EST_Red == (*a).Semantic)
{
cnt++;
aiColors[0] = _a;
aiColorsTypes[0] = (*a).eType;
}
else if (PLY::EST_Green == (*a).Semantic)
{
cnt++;
aiColors[1] = _a;
aiColorsTypes[1] = (*a).eType;
}
else if (PLY::EST_Blue == (*a).Semantic)
{
cnt++;
aiColors[2] = _a;
aiColorsTypes[2] = (*a).eType;
}
else if (PLY::EST_Alpha == (*a).Semantic)
{
cnt++;
aiColors[3] = _a;
aiColorsTypes[3] = (*a).eType;
}
// Texture coordinates
else if (PLY::EST_UTextureCoord == (*a).Semantic)
{
cnt++;
aiTexcoord[0] = _a;
aiTexcoordTypes[0] = (*a).eType;
}
else if (PLY::EST_VTextureCoord == (*a).Semantic)
{
cnt++;
aiTexcoord[1] = _a;
aiTexcoordTypes[1] = (*a).eType;
}
}
// check whether we have a valid source for the vertex data if (0xFFFFFFFF != aiPositions[1]) {
if (0 != cnt) vOut.y = PLY::PropertyInstance::ConvertTo<ai_real>(
{ GetProperty(instElement->alProperties, aiPositions[1]).avList.front(), aiTypes[1]);
// Position }
aiVector3D vOut;
if (0xFFFFFFFF != aiPositions[0])
{
vOut.x = PLY::PropertyInstance::ConvertTo<ai_real>(
GetProperty(instElement->alProperties, aiPositions[0]).avList.front(), aiTypes[0]);
}
if (0xFFFFFFFF != aiPositions[1]) if (0xFFFFFFFF != aiPositions[2]) {
{ vOut.z = PLY::PropertyInstance::ConvertTo<ai_real>(
vOut.y = PLY::PropertyInstance::ConvertTo<ai_real>( GetProperty(instElement->alProperties, aiPositions[2]).avList.front(), aiTypes[2]);
GetProperty(instElement->alProperties, aiPositions[1]).avList.front(), aiTypes[1]); }
}
if (0xFFFFFFFF != aiPositions[2]) // Normals
{ aiVector3D nOut;
vOut.z = PLY::PropertyInstance::ConvertTo<ai_real>( bool haveNormal = false;
GetProperty(instElement->alProperties, aiPositions[2]).avList.front(), aiTypes[2]); if (0xFFFFFFFF != aiNormal[0]) {
} nOut.x = PLY::PropertyInstance::ConvertTo<ai_real>(
GetProperty(instElement->alProperties, aiNormal[0]).avList.front(), aiNormalTypes[0]);
haveNormal = true;
}
// Normals if (0xFFFFFFFF != aiNormal[1]) {
aiVector3D nOut; nOut.y = PLY::PropertyInstance::ConvertTo<ai_real>(
bool haveNormal = false; GetProperty(instElement->alProperties, aiNormal[1]).avList.front(), aiNormalTypes[1]);
if (0xFFFFFFFF != aiNormal[0]) haveNormal = true;
{ }
nOut.x = PLY::PropertyInstance::ConvertTo<ai_real>(
GetProperty(instElement->alProperties, aiNormal[0]).avList.front(), aiNormalTypes[0]);
haveNormal = true;
}
if (0xFFFFFFFF != aiNormal[1]) if (0xFFFFFFFF != aiNormal[2]) {
{ nOut.z = PLY::PropertyInstance::ConvertTo<ai_real>(
nOut.y = PLY::PropertyInstance::ConvertTo<ai_real>( GetProperty(instElement->alProperties, aiNormal[2]).avList.front(), aiNormalTypes[2]);
GetProperty(instElement->alProperties, aiNormal[1]).avList.front(), aiNormalTypes[1]); haveNormal = true;
haveNormal = true; }
}
if (0xFFFFFFFF != aiNormal[2]) //Colors
{ aiColor4D cOut;
nOut.z = PLY::PropertyInstance::ConvertTo<ai_real>( bool haveColor = false;
GetProperty(instElement->alProperties, aiNormal[2]).avList.front(), aiNormalTypes[2]); if (0xFFFFFFFF != aiColors[0]) {
haveNormal = true; cOut.r = NormalizeColorValue(GetProperty(instElement->alProperties,
} aiColors[0]).avList.front(), aiColorsTypes[0]);
haveColor = true;
}
//Colors if (0xFFFFFFFF != aiColors[1]) {
aiColor4D cOut; cOut.g = NormalizeColorValue(GetProperty(instElement->alProperties,
bool haveColor = false; aiColors[1]).avList.front(), aiColorsTypes[1]);
if (0xFFFFFFFF != aiColors[0]) haveColor = true;
{ }
cOut.r = NormalizeColorValue(GetProperty(instElement->alProperties,
aiColors[0]).avList.front(), aiColorsTypes[0]);
haveColor = true;
}
if (0xFFFFFFFF != aiColors[1]) if (0xFFFFFFFF != aiColors[2]) {
{ cOut.b = NormalizeColorValue(GetProperty(instElement->alProperties,
cOut.g = NormalizeColorValue(GetProperty(instElement->alProperties, aiColors[2]).avList.front(), aiColorsTypes[2]);
aiColors[1]).avList.front(), aiColorsTypes[1]); haveColor = true;
haveColor = true; }
}
if (0xFFFFFFFF != aiColors[2]) // assume 1.0 for the alpha channel ifit is not set
{ if (0xFFFFFFFF == aiColors[3]) {
cOut.b = NormalizeColorValue(GetProperty(instElement->alProperties, cOut.a = 1.0;
aiColors[2]).avList.front(), aiColorsTypes[2]); } else {
haveColor = true; cOut.a = NormalizeColorValue(GetProperty(instElement->alProperties,
} aiColors[3]).avList.front(), aiColorsTypes[3]);
// assume 1.0 for the alpha channel ifit is not set haveColor = true;
if (0xFFFFFFFF == aiColors[3]) }
{
cOut.a = 1.0;
}
else
{
cOut.a = NormalizeColorValue(GetProperty(instElement->alProperties,
aiColors[3]).avList.front(), aiColorsTypes[3]);
haveColor = true; //Texture coordinates
} aiVector3D tOut;
tOut.z = 0;
bool haveTextureCoords = false;
if (0xFFFFFFFF != aiTexcoord[0]) {
tOut.x = PLY::PropertyInstance::ConvertTo<ai_real>(
GetProperty(instElement->alProperties, aiTexcoord[0]).avList.front(), aiTexcoordTypes[0]);
haveTextureCoords = true;
}
//Texture coordinates if (0xFFFFFFFF != aiTexcoord[1]) {
aiVector3D tOut; tOut.y = PLY::PropertyInstance::ConvertTo<ai_real>(
tOut.z = 0; GetProperty(instElement->alProperties, aiTexcoord[1]).avList.front(), aiTexcoordTypes[1]);
bool haveTextureCoords = false; haveTextureCoords = true;
if (0xFFFFFFFF != aiTexcoord[0]) }
{
tOut.x = PLY::PropertyInstance::ConvertTo<ai_real>(
GetProperty(instElement->alProperties, aiTexcoord[0]).avList.front(), aiTexcoordTypes[0]);
haveTextureCoords = true;
}
if (0xFFFFFFFF != aiTexcoord[1]) //create aiMesh if needed
{ if ( nullptr == mGeneratedMesh ) {
tOut.y = PLY::PropertyInstance::ConvertTo<ai_real>( mGeneratedMesh = new aiMesh();
GetProperty(instElement->alProperties, aiTexcoord[1]).avList.front(), aiTexcoordTypes[1]); mGeneratedMesh->mMaterialIndex = 0;
haveTextureCoords = true; }
}
//create aiMesh if needed if (nullptr == mGeneratedMesh->mVertices) {
if (mGeneratedMesh == NULL) mGeneratedMesh->mNumVertices = pcElement->NumOccur;
{ mGeneratedMesh->mVertices = new aiVector3D[mGeneratedMesh->mNumVertices];
mGeneratedMesh = new aiMesh(); }
mGeneratedMesh->mMaterialIndex = 0;
}
if (mGeneratedMesh->HasPositions() ) mGeneratedMesh->mVertices[pos] = vOut;
{
mGeneratedMesh->mNumVertices = pcElement->NumOccur;
mGeneratedMesh->mVertices = new aiVector3D[mGeneratedMesh->mNumVertices];
}
mGeneratedMesh->mVertices[pos] = vOut; if (haveNormal) {
if (nullptr == mGeneratedMesh->mNormals)
mGeneratedMesh->mNormals = new aiVector3D[mGeneratedMesh->mNumVertices];
mGeneratedMesh->mNormals[pos] = nOut;
}
if (haveNormal) if (haveColor) {
{ if (nullptr == mGeneratedMesh->mColors[0])
if (mGeneratedMesh->HasNormals() ) mGeneratedMesh->mColors[0] = new aiColor4D[mGeneratedMesh->mNumVertices];
mGeneratedMesh->mNormals = new aiVector3D[mGeneratedMesh->mNumVertices]; mGeneratedMesh->mColors[0][pos] = cOut;
mGeneratedMesh->mNormals[pos] = nOut; }
}
if (haveColor) if (haveTextureCoords) {
{ if (nullptr == mGeneratedMesh->mTextureCoords[0]) {
if (mGeneratedMesh->HasVertexColors( 0 ) ) mGeneratedMesh->mNumUVComponents[0] = 2;
mGeneratedMesh->mColors[0] = new aiColor4D[mGeneratedMesh->mNumVertices]; mGeneratedMesh->mTextureCoords[0] = new aiVector3D[mGeneratedMesh->mNumVertices];
mGeneratedMesh->mColors[0][pos] = cOut; }
mGeneratedMesh->mTextureCoords[0][pos] = tOut;
}
} }
if (haveTextureCoords)
{
if (mGeneratedMesh->HasTextureCoords(0))
{
mGeneratedMesh->mNumUVComponents[0] = 2;
mGeneratedMesh->mTextureCoords[0] = new aiVector3D[mGeneratedMesh->mNumVertices];
}
mGeneratedMesh->mTextureCoords[0][pos] = tOut;
}
}
} }

View File

@ -42,6 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
#include <assimp/Exporter.hpp> #include <assimp/Exporter.hpp>
#include <assimp/scene.h>
#include "AbstractImportExportBase.h" #include "AbstractImportExportBase.h"
using namespace ::Assimp; using namespace ::Assimp;
@ -51,6 +52,11 @@ public:
virtual bool importerTest() { virtual bool importerTest() {
Assimp::Importer importer; Assimp::Importer importer;
const aiScene *scene = importer.ReadFile( ASSIMP_TEST_MODELS_DIR "/PLY/cube.ply", 0 ); const aiScene *scene = importer.ReadFile( ASSIMP_TEST_MODELS_DIR "/PLY/cube.ply", 0 );
EXPECT_EQ( 1u, scene->mNumMeshes );
EXPECT_NE( nullptr, scene->mMeshes[0] );
EXPECT_EQ( 8u, scene->mMeshes[0]->mNumVertices );
EXPECT_EQ( 6u, scene->mMeshes[0]->mNumFaces );
return nullptr != scene; return nullptr != scene;
} }