From f0fe614904172a9f8beae160d11eec25af18681b Mon Sep 17 00:00:00 2001 From: Andrew Poor Date: Mon, 16 May 2016 12:41:19 +0100 Subject: [PATCH] Fix for object loader not handling flags correctly. Change-Id: I287d1156e04c804ef0eeb1830f0b1c970739be3c --- .../controls/model3d-view/model3d-view-impl.cpp | 9 +- .../internal/controls/model3d-view/obj-loader.cpp | 98 ++++++++++++++-------- .../internal/controls/model3d-view/obj-loader.h | 8 +- 3 files changed, 74 insertions(+), 41 deletions(-) diff --git a/dali-toolkit/internal/controls/model3d-view/model3d-view-impl.cpp b/dali-toolkit/internal/controls/model3d-view/model3d-view-impl.cpp index f3e7fb2..cf38437 100644 --- a/dali-toolkit/internal/controls/model3d-view/model3d-view-impl.cpp +++ b/dali-toolkit/internal/controls/model3d-view/model3d-view-impl.cpp @@ -423,7 +423,7 @@ void Model3dView::OnStageConnection( int depth ) if( mObjLoader.IsSceneLoaded() ) { - mMesh = mObjLoader.CreateGeometry(mIlluminationType); + mMesh = mObjLoader.CreateGeometry( mIlluminationType ); CreateMaterial(); LoadTextures(); @@ -520,7 +520,7 @@ void Model3dView::CreateGeometry() { if( mObjLoader.IsSceneLoaded() ) { - mMesh = mObjLoader.CreateGeometry(mIlluminationType); + mMesh = mObjLoader.CreateGeometry( mIlluminationType ); if( mRenderer ) { @@ -556,11 +556,12 @@ void Model3dView::CreateMaterial() { if( mObjLoader.IsMaterialLoaded() && (mTexture0Url != "") && mObjLoader.IsTexturePresent() ) { - if( (mTexture2Url != "") && (mTexture1Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) && mObjLoader.IsNormalMapPresent() ) + if( (mTexture2Url != "") && (mTexture1Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) ) { mShader = Shader::New( NRMMAP_VERTEX_SHADER, NRMMAP_FRAGMENT_SHADER ); } - else if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ) + else if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE || + mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP ) { mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ); } diff --git a/dali-toolkit/internal/controls/model3d-view/obj-loader.cpp b/dali-toolkit/internal/controls/model3d-view/obj-loader.cpp index 04153d2..bc4e174 100644 --- a/dali-toolkit/internal/controls/model3d-view/obj-loader.cpp +++ b/dali-toolkit/internal/controls/model3d-view/obj-loader.cpp @@ -38,6 +38,10 @@ ObjLoader::ObjLoader() { mSceneLoaded = false; mMaterialLoaded = false; + mHasTexturePoints = false; + mHasNormalMap = false; + mHasDiffuseMap = false; + mHasSpecularMap = false; mSceneAABB.Init(); } @@ -166,9 +170,10 @@ void ObjLoader::CreateGeometryArray(Dali::Vector & vertices, { //If we don't have tangents, calculate them //we need to recalculate the normals too, because we need just one normal,tangent, bitangent per vertex - //In the case of a textureless object, we don't need tangents and so we skip this step + //In the case of a textureless object, we don't need tangents for our shader and so we skip this step //TODO: Use a better function to calculate tangents - if( mTangents.Size() == 0 && mHasTexture && mHasNormalMap ) + + if( mTangents.Size() == 0 && mHasTexturePoints ) { mTangents.Resize( mNormals.Size() ); mBiTangents.Resize( mNormals.Size() ); @@ -181,7 +186,7 @@ void ObjLoader::CreateGeometryArray(Dali::Vector & vertices, bool mapsCorrespond; //True if the sizes of the arrays necessary for the object agree. - if ( mHasTexture ) + if ( mHasTexturePoints ) { mapsCorrespond = ( mPoints.Size() == mTextures.Size() ) && ( mTextures.Size() == mNormals.Size() ); } @@ -207,7 +212,7 @@ void ObjLoader::CreateGeometryArray(Dali::Vector & vertices, vertex.position = mPoints[ui]; vertices[ui] = vertex; - if ( mHasTexture ) + if ( mHasTexturePoints ) { textures[ui] = Vector2(); verticesExt[ui] = VertexExt(); @@ -226,13 +231,9 @@ void ObjLoader::CreateGeometryArray(Dali::Vector & vertices, vertices[mTriangles[ui].pntIndex[j]].normal = mNormals[mTriangles[ui].nrmIndex[j]]; - if ( mHasTexture ) + if ( mHasTexturePoints ) { textures[mTriangles[ui].pntIndex[j]] = mTextures[mTriangles[ui].texIndex[j]]; - } - - if ( mHasNormalMap && mHasTexture ) - { verticesExt[mTriangles[ui].pntIndex[j]].tangent = mTangents[mTriangles[ui].nrmIndex[j]]; verticesExt[mTriangles[ui].pntIndex[j]].bitangent = mBiTangents[mTriangles[ui].nrmIndex[j]]; } @@ -258,17 +259,14 @@ void ObjLoader::CreateGeometryArray(Dali::Vector & vertices, vertex.normal = mNormals[mTriangles[ui].nrmIndex[j]]; vertices[index] = vertex; - if ( mHasTexture ) + if ( mHasTexturePoints ) { textures[index] = mTextures[mTriangles[ui].texIndex[j]]; - } - - if ( mHasNormalMap && mHasTexture ) - { VertexExt vertexExt; vertexExt.tangent = mTangents[mTriangles[ui].nrmIndex[j]]; vertexExt.bitangent = mBiTangents[mTriangles[ui].nrmIndex[j]]; verticesExt[index] = vertexExt; + } index++; @@ -289,7 +287,6 @@ bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& mat int pntAcum = 0, texAcum = 0, nrmAcum = 0; bool iniObj = false; bool hasTexture = false; - bool hasNormalMap = false; int face = 0; //Init AABB for the file @@ -337,7 +334,6 @@ bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& mat isline >> point.z; mTangents.PushBack( point ); - hasNormalMap = true; } else if ( tag == "#_#binormal" ) { @@ -346,7 +342,6 @@ bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& mat isline >> point.z; mBiTangents.PushBack( point ); - hasNormalMap = true; } else if ( tag == "vt" ) { @@ -382,28 +377,48 @@ bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& mat numIndices++; } + //Hold slashes that separate attributes of the same point. char separator; char separator2; - if ( strstr( vet[0].c_str(),"//" ) ) //No texture coordinates. + const char * subString; //A pointer to the position in the string as we move through it. + + subString = strstr( vet[0].c_str(),"/" ); //Search for the first '/' + + if( subString ) { - for( int i = 0 ; i < numIndices; i++) + if( subString[1] == '/' ) // Of the form A//C, so has points and normals but no texture coordinates. { - std::istringstream isindex( vet[i] ); - isindex >> ptIdx[i] >> separator >> separator2 >> nrmIdx[i]; - texIdx[i] = 0; + for( int i = 0 ; i < numIndices; i++) + { + std::istringstream isindex( vet[i] ); + isindex >> ptIdx[i] >> separator >> separator2 >> nrmIdx[i]; + texIdx[i] = 0; + } } - } - else if ( strstr( vet[0].c_str(),"/" ) ) //Has texture coordinates, and possibly also normals. - { - for( int i = 0 ; i < numIndices; i++ ) + else if( strstr( subString, "/" ) ) // Of the form A/B/C, so has points, textures and normals. { - std::istringstream isindex( vet[i] ); - isindex >> ptIdx[i] >> separator >> texIdx[i] >> separator2 >> nrmIdx[i]; + for( int i = 0 ; i < numIndices; i++ ) + { + std::istringstream isindex( vet[i] ); + isindex >> ptIdx[i] >> separator >> texIdx[i] >> separator2 >> nrmIdx[i]; + } + + hasTexture = true; + } + else // Of the form A/B, so has points and textures but no normals. + { + for( int i = 0 ; i < numIndices; i++ ) + { + std::istringstream isindex( vet[i] ); + isindex >> ptIdx[i] >> separator >> texIdx[i]; + nrmIdx[i] = 0; + } + hasTexture = true; } } - else //Has just points. + else // Simply of the form A, as in, point indices only. { for( int i = 0 ; i < numIndices; i++ ) { @@ -470,8 +485,7 @@ bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& mat { CenterAndScale( true, mPoints ); mSceneLoaded = true; - mHasTexture = hasTexture; - mHasNormalMap = hasNormalMap; + mHasTexturePoints = hasTexture; return true; } @@ -522,6 +536,7 @@ void ObjLoader::LoadMaterial( char* objBuffer, std::streampos fileSize, std::str { isline >> info; texture0Url = info; + mHasDiffuseMap = true; } else if ( tag == "bump" ) { @@ -533,6 +548,7 @@ void ObjLoader::LoadMaterial( char* objBuffer, std::streampos fileSize, std::str { isline >> info; texture2Url = info; + mHasSpecularMap = true; } } @@ -541,6 +557,8 @@ void ObjLoader::LoadMaterial( char* objBuffer, std::streampos fileSize, std::str Geometry ObjLoader::CreateGeometry( Toolkit::Model3dView::IlluminationType illuminationType ) { + Geometry surface = Geometry::New(); + Dali::Vector vertices; Dali::Vector textures; Dali::Vector verticesExt; @@ -554,13 +572,11 @@ Geometry ObjLoader::CreateGeometry( Toolkit::Model3dView::IlluminationType illum vertexFormat["aNormal"] = Property::VECTOR3; PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat ); surfaceVertices.SetData( &vertices[0], vertices.Size() ); - - Geometry surface = Geometry::New(); surface.AddVertexBuffer( surfaceVertices ); //Some need texture coordinates if( ( (illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP ) || - (illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ) ) && mHasTexture ) + (illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ) ) && mHasTexturePoints && mHasDiffuseMap ) { Property::Map textureFormat; textureFormat["aTexCoord"] = Property::VECTOR2; @@ -571,7 +587,7 @@ Geometry ObjLoader::CreateGeometry( Toolkit::Model3dView::IlluminationType illum } //Some need tangent and bitangent - if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP && mHasNormalMap && mHasTexture ) + if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP && mHasTexturePoints ) { Property::Map vertexExtFormat; vertexExtFormat["aTangent"] = Property::VECTOR3; @@ -621,7 +637,12 @@ void ObjLoader::ClearArrays() bool ObjLoader::IsTexturePresent() { - return mHasTexture; + return mHasTexturePoints; +} + +bool ObjLoader::IsDiffuseMapPresent() +{ + return mHasDiffuseMap; } bool ObjLoader::IsNormalMapPresent() @@ -629,6 +650,11 @@ bool ObjLoader::IsNormalMapPresent() return mHasNormalMap; } +bool ObjLoader::IsSpecularMapPresent() +{ + return mHasSpecularMap; +} + } // namespace Internal } // namespace Toolkit } // namespace Dali diff --git a/dali-toolkit/internal/controls/model3d-view/obj-loader.h b/dali-toolkit/internal/controls/model3d-view/obj-loader.h index 72967a6..0a9ea25 100644 --- a/dali-toolkit/internal/controls/model3d-view/obj-loader.h +++ b/dali-toolkit/internal/controls/model3d-view/obj-loader.h @@ -113,7 +113,9 @@ public: void ClearArrays(); bool IsTexturePresent(); + bool IsDiffuseMapPresent(); bool IsNormalMapPresent(); + bool IsSpecularMapPresent(); private: @@ -121,8 +123,12 @@ private: bool mSceneLoaded; bool mMaterialLoaded; - bool mHasTexture; + bool mHasTexturePoints; + + //Material file properties. + bool mHasDiffuseMap; bool mHasNormalMap; + bool mHasSpecularMap; Dali::Vector mPoints; Dali::Vector mTextures; -- 2.7.4