Modifed mesh-renderer to use new texture API
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / model3d-view / obj-loader.cpp
index 04153d2..2a34f59 100644 (file)
@@ -19,6 +19,7 @@
 #include "obj-loader.h"
 
 // EXTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
 #include <string>
 #include <sstream>
 #include <string.h>
@@ -32,12 +33,20 @@ namespace Toolkit
 namespace Internal
 {
 
+namespace
+{
+  const int MAX_POINT_INDICES = 4;
+}
 using namespace Dali;
 
 ObjLoader::ObjLoader()
 {
   mSceneLoaded = false;
   mMaterialLoaded = false;
+  mHasTexturePoints = false;
+  mHasDiffuseMap = false;
+  mHasNormalMap = false;
+  mHasSpecularMap = false;
   mSceneAABB.Init();
 }
 
@@ -63,9 +72,6 @@ void ObjLoader::CalculateTangentArray(const Dali::Vector<Vector3>& vertex,
                                       Dali::Vector<Vector3>& normal,
                                       Dali::Vector<Vector3>& tangent)
 {
-  normal.Clear();
-  normal.Resize(vertex.Size());
-
   Dali::Vector<Vector3> tangents;
   tangents.Resize( vertex.Size() );
 
@@ -166,13 +172,18 @@ void ObjLoader::CreateGeometryArray(Dali::Vector<Vertex> & 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() );
+    mNormals.Clear();
+
+    mNormals.Resize( mPoints.Size() );
+    mTangents.Resize( mPoints.Size() );
+    mBiTangents.Resize( mPoints.Size() );
+
     CalculateTangentArray( mPoints, mTextures, mTriangles, mNormals, mTangents );
+
     for ( unsigned int ui = 0 ; ui < mNormals.Size() ; ++ui )
     {
       mBiTangents[ui] = mNormals[ui].Cross(mTangents[ui]);
@@ -181,7 +192,7 @@ void ObjLoader::CreateGeometryArray(Dali::Vector<Vertex> & 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 +218,7 @@ void ObjLoader::CreateGeometryArray(Dali::Vector<Vertex> & vertices,
       vertex.position = mPoints[ui];
       vertices[ui] = vertex;
 
-      if ( mHasTexture )
+      if ( mHasTexturePoints )
       {
         textures[ui] = Vector2();
         verticesExt[ui] = VertexExt();
@@ -226,13 +237,9 @@ void ObjLoader::CreateGeometryArray(Dali::Vector<Vertex> & 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 +265,14 @@ void ObjLoader::CreateGeometryArray(Dali::Vector<Vertex> & 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++;
@@ -277,19 +281,18 @@ void ObjLoader::CreateGeometryArray(Dali::Vector<Vertex> & vertices,
   }
 }
 
-bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& materialFile )
+bool ObjLoader::LoadObject( char* objBuffer, std::streampos fileSize )
 {
   Vector3 point;
   Vector2 texture;
-  std::string vet[4], name;
-  int ptIdx[4];
-  int nrmIdx[4];
-  int texIdx[4];
+  std::string vet[MAX_POINT_INDICES], name;
+  int ptIdx[MAX_POINT_INDICES];
+  int nrmIdx[MAX_POINT_INDICES];
+  int texIdx[MAX_POINT_INDICES];
   TriIndex triangle,triangle2;
   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 +340,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 +348,6 @@ bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& mat
       isline >> point.z;
 
       mBiTangents.PushBack( point );
-      hasNormalMap = true;
     }
     else if ( tag == "vt" )
     {
@@ -377,33 +378,53 @@ bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& mat
       }
 
       int numIndices = 0;
-      while( isline >> vet[numIndices] )
+      while( isline >> vet[numIndices] && numIndices < MAX_POINT_INDICES )
       {
         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++ )
         {
@@ -461,26 +482,21 @@ bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& mat
     {
       isline >> name;
     }
-    else
-    {
-    }
   }
 
   if ( iniObj )
   {
     CenterAndScale( true, mPoints );
     mSceneLoaded = true;
-    mHasTexture = hasTexture;
-    mHasNormalMap = hasNormalMap;
+    mHasTexturePoints = hasTexture;
     return true;
   }
 
   return false;
-
 }
 
-void ObjLoader::LoadMaterial( char* objBuffer, std::streampos fileSize, std::string& texture0Url,
-                              std::string& texture1Url, std::string& texture2Url )
+void ObjLoader::LoadMaterial( char* objBuffer, std::streampos fileSize, std::string& diffuseTextureUrl,
+                              std::string& normalTextureUrl, std::string& glossTextureUrl )
 {
   float fR,fG,fB;
 
@@ -504,11 +520,15 @@ void ObjLoader::LoadMaterial( char* objBuffer, std::streampos fileSize, std::str
     {
       isline >> info;
     }
+    else if ( tag == "Ka" ) //ambient color
+    {
+      isline >> fR >> fG >> fB;
+    }
     else if ( tag == "Kd" ) //diffuse color
     {
       isline >> fR >> fG >> fB;
     }
-    else if ( tag == "Kd" ) //Ambient color
+    else if ( tag == "Ks" ) //specular color
     {
       isline >> fR >> fG >> fB;
     }
@@ -521,26 +541,30 @@ void ObjLoader::LoadMaterial( char* objBuffer, std::streampos fileSize, std::str
     else if ( tag == "map_Kd" )
     {
       isline >> info;
-      texture0Url = info;
+      diffuseTextureUrl = info;
+      mHasDiffuseMap = true;
     }
     else if ( tag == "bump" )
     {
       isline >> info;
-      texture1Url = info;
+      normalTextureUrl = info;
       mHasNormalMap = true;
     }
     else if ( tag == "map_Ks" )
     {
       isline >> info;
-      texture2Url = info;
+      glossTextureUrl = info;
+      mHasSpecularMap = true;
     }
   }
 
   mMaterialLoaded = true;
 }
 
-Geometry ObjLoader::CreateGeometry( Toolkit::Model3dView::IlluminationType illuminationType )
+Geometry ObjLoader::CreateGeometry( int objectProperties )
 {
+  Geometry surface = Geometry::New();
+
   Dali::Vector<Vertex> vertices;
   Dali::Vector<Vector2> textures;
   Dali::Vector<VertexExt> verticesExt;
@@ -554,13 +578,10 @@ 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 )
+  if( ( objectProperties & TEXTURE_COORDINATES ) && mHasTexturePoints && mHasDiffuseMap )
   {
     Property::Map textureFormat;
     textureFormat["aTexCoord"] = Property::VECTOR2;
@@ -571,7 +592,7 @@ Geometry ObjLoader::CreateGeometry( Toolkit::Model3dView::IlluminationType illum
   }
 
   //Some need tangent and bitangent
-  if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP && mHasNormalMap && mHasTexture )
+  if( ( objectProperties & TANGENTS ) && ( objectProperties & BINORMALS ) && mHasTexturePoints )
   {
     Property::Map vertexExtFormat;
     vertexExtFormat["aTangent"] = Property::VECTOR3;
@@ -582,15 +603,12 @@ Geometry ObjLoader::CreateGeometry( Toolkit::Model3dView::IlluminationType illum
     surface.AddVertexBuffer( extraVertices );
   }
 
+  //If indices are required, we set them.
   if ( indices.Size() )
   {
     surface.SetIndexBuffer ( &indices[0], indices.Size() );
   }
 
-  vertices.Clear();
-  verticesExt.Clear();
-  indices.Clear();
-
   return surface;
 }
 
@@ -621,7 +639,12 @@ void ObjLoader::ClearArrays()
 
 bool ObjLoader::IsTexturePresent()
 {
-  return mHasTexture;
+  return mHasTexturePoints;
+}
+
+bool ObjLoader::IsDiffuseMapPresent()
+{
+  return mHasDiffuseMap;
 }
 
 bool ObjLoader::IsNormalMapPresent()
@@ -629,6 +652,11 @@ bool ObjLoader::IsNormalMapPresent()
   return mHasNormalMap;
 }
 
+bool ObjLoader::IsSpecularMapPresent()
+{
+  return mHasSpecularMap;
+}
+
 } // namespace Internal
 } // namespace Toolkit
 } // namespace Dali