Modifed mesh-renderer to use new texture API 36/77836/5
authorFerran Sole <ferran.sole@samsung.com>
Fri, 1 Jul 2016 08:29:49 +0000 (09:29 +0100)
committerFerran Sole <ferran.sole@samsung.com>
Tue, 5 Jul 2016 15:02:50 +0000 (16:02 +0100)
* mesh-renderer is now using the new texture API
* Added a property to specify whether to use mipmapping on the textures

Change-Id: Ic654279a97dfb0efaad2833bac0d497160faaf53

automated-tests/src/dali-toolkit/dali-toolkit-test-utils/test-gl-abstraction.h
dali-toolkit/internal/controls/model3d-view/model3d-view-impl.cpp
dali-toolkit/internal/controls/model3d-view/obj-loader.cpp
dali-toolkit/internal/controls/model3d-view/obj-loader.h
dali-toolkit/internal/controls/renderers/mesh/mesh-renderer.cpp
dali-toolkit/internal/controls/renderers/mesh/mesh-renderer.h
dali-toolkit/internal/controls/renderers/renderer-string-constants.cpp
dali-toolkit/internal/controls/renderers/renderer-string-constants.h

index e445084..da94586 100644 (file)
@@ -662,6 +662,11 @@ public:
         *type = GL_SAMPLER_2D;
         *size = 1;
         break;
         *type = GL_SAMPLER_2D;
         *size = 1;
         break;
+      case 2:
+        *length = snprintf(name, bufsize, "sGloss");
+        *type = GL_SAMPLER_2D;
+        *size = 1;
+        break;
       default:
         break;
     }
       default:
         break;
     }
@@ -878,9 +883,10 @@ public:
     namedParams["program"] = ToString(program);
     mShaderTrace.PushCall("LinkProgram", out.str(), namedParams);
 
     namedParams["program"] = ToString(program);
     mShaderTrace.PushCall("LinkProgram", out.str(), namedParams);
 
-    mNumberOfActiveUniforms=2;
+    mNumberOfActiveUniforms=3;
     GetUniformLocation(program, "sTexture");
     GetUniformLocation(program, "sEffect");
     GetUniformLocation(program, "sTexture");
     GetUniformLocation(program, "sEffect");
+    GetUniformLocation(program, "sGloss");
   }
 
   inline void PixelStorei(GLenum pname, GLint param)
   }
 
   inline void PixelStorei(GLenum pname, GLint param)
index eac2d89..fc75b7a 100644 (file)
@@ -639,7 +639,7 @@ int Model3dView::GetShaderProperties( Toolkit::Model3dView::IlluminationType ill
 
   if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
   {
 
   if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
   {
-    objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINOMIALS;
+    objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
   }
 
   return objectProperties;
   }
 
   return objectProperties;
index aaf7c60..2a34f59 100644 (file)
@@ -592,7 +592,7 @@ Geometry ObjLoader::CreateGeometry( int objectProperties )
   }
 
   //Some need tangent and bitangent
   }
 
   //Some need tangent and bitangent
-  if( ( objectProperties & TANGENTS ) && ( objectProperties & BINOMIALS ) && mHasTexturePoints )
+  if( ( objectProperties & TANGENTS ) && ( objectProperties & BINORMALS ) && mHasTexturePoints )
   {
     Property::Map vertexExtFormat;
     vertexExtFormat["aTangent"] = Property::VECTOR3;
   {
     Property::Map vertexExtFormat;
     vertexExtFormat["aTangent"] = Property::VECTOR3;
index 0336323..a877bb9 100644 (file)
@@ -97,7 +97,7 @@ public:
   {
     TEXTURE_COORDINATES = 1 << 0,
     TANGENTS = 1 << 1,
   {
     TEXTURE_COORDINATES = 1 << 0,
     TANGENTS = 1 << 1,
-    BINOMIALS = 1 << 2
+    BINORMALS = 1 << 2
   };
 
   ObjLoader();
   };
 
   ObjLoader();
index 8b3256d..2ac1dd6 100644 (file)
@@ -22,6 +22,7 @@
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/images/resource-image.h>
 #include <dali/public-api/common/stage.h>
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/images/resource-image.h>
 #include <dali/public-api/common/stage.h>
+#include <dali/devel-api/adaptor-framework/bitmap-loader.h>
 #include <dali/devel-api/adaptor-framework/file-loader.h>
 #include <fstream>
 
 #include <dali/devel-api/adaptor-framework/file-loader.h>
 #include <fstream>
 
 namespace Dali
 {
 
 namespace Dali
 {
 
+namespace
+{
+  /**
+   * @brief Loads a texture from a file
+   * @param[in] imageUrl The url of the file
+   * @param[in] generateMipmaps Indicates whether to generate mipmaps for the texture
+   * @return A texture if loading succeeds, an empty handle otherwise
+   */
+  Texture LoadTexture( const char* imageUrl, bool generateMipmaps )
+  {
+    Texture texture;
+    Dali::BitmapLoader loader = Dali::BitmapLoader::New( imageUrl );
+    loader.Load();
+    PixelData pixelData = loader.GetPixelData();
+    if( pixelData )
+    {
+      texture = Texture::New( TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight() );
+      texture.Upload( pixelData );
+
+      if( generateMipmaps )
+      {
+        texture.GenerateMipmaps();
+      }
+    }
+
+    return texture;
+  }
+}// unnamed namespace
+
 namespace Toolkit
 {
 
 namespace Toolkit
 {
 
@@ -244,7 +274,8 @@ const char* NORMAL_MAP_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
 MeshRenderer::MeshRenderer( RendererFactoryCache& factoryCache )
 : ControlRenderer( factoryCache ),
   mShaderType( ALL_TEXTURES ),
 MeshRenderer::MeshRenderer( RendererFactoryCache& factoryCache )
 : ControlRenderer( factoryCache ),
   mShaderType( ALL_TEXTURES ),
-  mUseTexture( true )
+  mUseTexture( true ),
+  mUseMipmapping( true )
 {
 }
 
 {
 }
 
@@ -275,6 +306,12 @@ void MeshRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap
     mTexturesPath.clear();
   }
 
     mTexturesPath.clear();
   }
 
+  Property::Value* useMipmapping = propertyMap.Find( USE_MIPMAPPING );
+  if( useMipmapping )
+  {
+    useMipmapping->Get( mUseMipmapping );
+  }
+
   Property::Value* shaderType = propertyMap.Find( SHADER_TYPE );
   if( shaderType && shaderType->Get( mShaderTypeString ) )
   {
   Property::Value* shaderType = propertyMap.Find( SHADER_TYPE );
   if( shaderType && shaderType->Get( mShaderTypeString ) )
   {
@@ -413,16 +450,14 @@ void MeshRenderer::CreateShader()
 bool MeshRenderer::CreateGeometry()
 {
   //Determine if we need to use a simpler shader to handle the provided data
 bool MeshRenderer::CreateGeometry()
 {
   //Determine if we need to use a simpler shader to handle the provided data
-  if( mShaderType == ALL_TEXTURES )
+  if( !mUseTexture || !mObjLoader.IsDiffuseMapPresent() )
   {
   {
-    if( !mObjLoader.IsNormalMapPresent() || !mObjLoader.IsSpecularMapPresent() )
-    {
-      mShaderType = DIFFUSE_TEXTURE;
-    }
+    mUseTexture = false;
+    mShaderType = TEXTURELESS;
   }
   }
-  if( !mObjLoader.IsTexturePresent() || !mObjLoader.IsDiffuseMapPresent() || !mUseTexture )
+  else if( mShaderType == ALL_TEXTURES && (!mObjLoader.IsNormalMapPresent() || !mObjLoader.IsSpecularMapPresent()) )
   {
   {
-    mShaderType = TEXTURELESS;
+    mShaderType = DIFFUSE_TEXTURE;
   }
 
   int objectProperties = 0;
   }
 
   int objectProperties = 0;
@@ -435,7 +470,7 @@ bool MeshRenderer::CreateGeometry()
 
   if( mShaderType == ALL_TEXTURES )
   {
 
   if( mShaderType == ALL_TEXTURES )
   {
-    objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINOMIALS;
+    objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
   }
 
   //Create geometry with attributes required by shader.
   }
 
   //Create geometry with attributes required by shader.
@@ -492,57 +527,68 @@ bool MeshRenderer::LoadTextures()
 {
   mTextureSet = TextureSet::New();
 
 {
   mTextureSet = TextureSet::New();
 
-  if( !mDiffuseTextureUrl.empty() )
+  if( mUseTexture )
   {
   {
-    std::string imageUrl = mTexturesPath + mDiffuseTextureUrl;
-
-    //Load textures
-    Image diffuseTexture = ResourceImage::New( imageUrl );
-    if( diffuseTexture )
-    {
-      mTextureSet.SetImage( DIFFUSE_INDEX, diffuseTexture );
-    }
-    else
+    Sampler sampler = Sampler::New();
+    if( mUseMipmapping )
     {
     {
-      DALI_LOG_ERROR( "Failed to load diffuse map texture in mesh renderer.\n");
-      return false;
+      sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR );
     }
     }
-  }
-
-  if( !mNormalTextureUrl.empty() && ( mShaderType == ALL_TEXTURES ) )
-  {
-    std::string imageUrl = mTexturesPath + mNormalTextureUrl;
 
 
-    //Load textures
-    Image normalTexture = ResourceImage::New( imageUrl );
-    if( normalTexture )
+    if( !mDiffuseTextureUrl.empty() )
     {
     {
-      mTextureSet.SetImage( NORMAL_INDEX, normalTexture );
+      std::string imageUrl = mTexturesPath + mDiffuseTextureUrl;
+
+      //Load textures
+      Texture diffuseTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
+      if( diffuseTexture )
+      {
+        mTextureSet.SetTexture( DIFFUSE_INDEX, diffuseTexture );
+        mTextureSet.SetSampler( DIFFUSE_INDEX, sampler );
+      }
+      else
+      {
+        DALI_LOG_ERROR( "Failed to load diffuse map texture in mesh renderer.\n");
+        return false;
+      }
     }
     }
-    else
-    {
-      DALI_LOG_ERROR( "Failed to load normal map texture in mesh renderer.\n");
-      return false;
-    }
-  }
-
-  if( !mGlossTextureUrl.empty() && ( mShaderType == ALL_TEXTURES ) )
-  {
-    std::string imageUrl = mTexturesPath + mGlossTextureUrl;
 
 
-    //Load textures
-    Image glossTexture = ResourceImage::New( imageUrl );
-    if( glossTexture )
+    if( !mNormalTextureUrl.empty() && ( mShaderType == ALL_TEXTURES ) )
     {
     {
-      mTextureSet.SetImage( GLOSS_INDEX, glossTexture );
+      std::string imageUrl = mTexturesPath + mNormalTextureUrl;
+
+      //Load textures
+      Texture normalTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
+      if( normalTexture )
+      {
+        mTextureSet.SetTexture( NORMAL_INDEX, normalTexture );
+        mTextureSet.SetSampler( NORMAL_INDEX, sampler );
+      }
+      else
+      {
+        DALI_LOG_ERROR( "Failed to load normal map texture in mesh renderer.\n");
+        return false;
+      }
     }
     }
-    else
+
+    if( !mGlossTextureUrl.empty() && ( mShaderType == ALL_TEXTURES ) )
     {
     {
-      DALI_LOG_ERROR( "Failed to load gloss map texture in mesh renderer.\n");
-      return false;
+      std::string imageUrl = mTexturesPath + mGlossTextureUrl;
+
+      //Load textures
+      Texture glossTexture = LoadTexture( imageUrl.c_str(), mUseMipmapping );
+      if( glossTexture )
+      {
+        mTextureSet.SetTexture( GLOSS_INDEX, glossTexture );
+        mTextureSet.SetSampler( GLOSS_INDEX, sampler );
+      }
+      else
+      {
+        DALI_LOG_ERROR( "Failed to load gloss map texture in mesh renderer.\n");
+        return false;
+      }
     }
   }
     }
   }
-
   return true;
 }
 
   return true;
 }
 
index ede12fb..e5aaee5 100644 (file)
@@ -196,6 +196,7 @@ private:
   ShaderType mShaderType;
 
   bool mUseTexture;
   ShaderType mShaderType;
 
   bool mUseTexture;
+  bool mUseMipmapping;
 };
 
 } // namespace Internal
 };
 
 } // namespace Internal
index a5eb87a..41fd6a7 100644 (file)
@@ -43,6 +43,7 @@ const char * const OBJECT_URL( "objectUrl" );
 const char * const MATERIAL_URL( "materialUrl" );
 const char * const TEXTURES_PATH( "texturesPath" );
 const char * const SHADER_TYPE( "shaderType" );
 const char * const MATERIAL_URL( "materialUrl" );
 const char * const TEXTURES_PATH( "texturesPath" );
 const char * const SHADER_TYPE( "shaderType" );
+const char * const USE_MIPMAPPING( "useMipmapping" );
 
 } // namespace Internal
 
 
 } // namespace Internal
 
index 51e13f0..0bb7d45 100644 (file)
@@ -43,6 +43,7 @@ extern const char * const OBJECT_URL;
 extern const char * const MATERIAL_URL;
 extern const char * const TEXTURES_PATH;
 extern const char * const SHADER_TYPE;
 extern const char * const MATERIAL_URL;
 extern const char * const TEXTURES_PATH;
 extern const char * const SHADER_TYPE;
+extern const char * const USE_MIPMAPPING;
 
 } // namespace Internal
 
 
 } // namespace Internal