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

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;
} }