X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Frenderers%2Fmesh%2Fmesh-renderer.cpp;h=2ac1dd667d5765810b45fce90f6945b5b20121df;hp=8b3256de33cc71525023ca8e8f6f3bd91dedd4b4;hb=9b1791de7d9e29435943464acc7cd896c44015a1;hpb=283c6129b00dfdbc1badbf809b4257784820bb30 diff --git a/dali-toolkit/internal/controls/renderers/mesh/mesh-renderer.cpp b/dali-toolkit/internal/controls/renderers/mesh/mesh-renderer.cpp index 8b3256d..2ac1dd6 100644 --- a/dali-toolkit/internal/controls/renderers/mesh/mesh-renderer.cpp +++ b/dali-toolkit/internal/controls/renderers/mesh/mesh-renderer.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -34,6 +35,35 @@ 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; }