Modifed mesh-renderer to use new texture API
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / mesh / mesh-renderer.cpp
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/devel-api/adaptor-framework/bitmap-loader.h>
 #include <dali/devel-api/adaptor-framework/file-loader.h>
 #include <fstream>
 
 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
 {
 
@@ -244,7 +274,8 @@ const char* NORMAL_MAP_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
 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();
   }
 
+  Property::Value* useMipmapping = propertyMap.Find( USE_MIPMAPPING );
+  if( useMipmapping )
+  {
+    useMipmapping->Get( mUseMipmapping );
+  }
+
   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
-  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;
@@ -435,7 +470,7 @@ bool MeshRenderer::CreateGeometry()
 
   if( mShaderType == ALL_TEXTURES )
   {
-    objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINOMIALS;
+    objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
   }
 
   //Create geometry with attributes required by shader.
@@ -492,57 +527,68 @@ bool MeshRenderer::LoadTextures()
 {
   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;
 }