X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fmodel3d-view%2Fmodel3d-view-impl.cpp;h=a4f927a4ff481a17a64edca6bfe054ae12bab434;hp=eac2d8934bfe48e0b60a5263a9f31a3ff0089314;hb=b1e8521ad77e7b4e62b59613b2edef64429130e9;hpb=b00bd1f5f0925dcdf424567116cc208efc48d769 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 eac2d89..a4f927a 100644 --- a/dali-toolkit/internal/controls/model3d-view/model3d-view-impl.cpp +++ b/dali-toolkit/internal/controls/model3d-view/model3d-view-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,8 +25,8 @@ #include #include #include -#include #include +#include // INTERNAL INCLUDES #include @@ -43,6 +43,34 @@ namespace Internal namespace { +// Texture indices are constants. +enum TextureIndex +{ + DIFFUSE_TEXTURE_INDEX, + NORMAL_TEXTURE_INDEX, + GLOSS_TEXTURE_INDEX +}; + +/** + * @brief Loads a texture from a file. + * @param[in] imageUrl The URL of the file + * @return A texture if loading succeeds, an empty handle otherwise + */ +Texture LoadTexture( const char* imageUrl ) +{ + Texture texture; + Devel::PixelBuffer pixelBuffer = LoadImageFromFile( imageUrl ); + if( pixelBuffer ) + { + texture = Texture::New( TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight() ); + PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer ); + texture.Upload( pixelData ); + texture.GenerateMipmaps(); + } + + return texture; +} + // Type registration BaseHandle Create() { @@ -247,28 +275,9 @@ const char* NRMMAP_FRAGMENT_SHADER = MAKE_SHADER( using namespace Dali; -void LookAt(Matrix& result, const Vector3& eye, const Vector3& target, const Vector3& up) -{ - Vector3 vZ = target - eye; - vZ.Normalize(); - - Vector3 vX = up.Cross(vZ); - vX.Normalize(); - - Vector3 vY = vZ.Cross(vX); - vY.Normalize(); - - result.SetInverseTransformComponents(vX, vY, vZ, eye); -} - - Model3dView::Model3dView() - : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ) +: Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ) { - mTexture0Url = ""; - mTexture1Url = ""; - mTexture2Url = ""; - mIlluminationType = Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP; mCameraFOV = Math::PI_OVER_180 * 45.f; @@ -317,6 +326,7 @@ void Model3dView::SetProperty( BaseObject* object, Property::Index index, const { impl.LoadMaterial(); impl.CreateMaterial(); + impl.LoadTextures(); } break; } @@ -414,16 +424,14 @@ Property::Value Model3dView::GetProperty( BaseObject* object, Property::Index in ///////////////////////////////////////////////////////////// -void Model3dView::OnStageConnection( int depth ) +void Model3dView::OnSceneConnection( int depth ) { - Control::OnStageConnection( depth ); - CustomActor self = Self(); self.AddRenderer( mRenderer ); if( mObjLoader.IsSceneLoaded() ) { - mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ) ); + mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ), true ); CreateMaterial(); LoadTextures(); @@ -437,6 +445,8 @@ void Model3dView::OnStageConnection( int depth ) constraint.AddSource( Source( self, Toolkit::Model3dView::Property::LIGHT_POSITION ) ); constraint.Apply(); } + + Control::OnSceneConnection( depth ); } /////////////////////////////////////////////////////////// @@ -518,12 +528,13 @@ void Model3dView::CreateGeometry() { if( mObjLoader.IsSceneLoaded() ) { - mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ) ); + mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ), true ); if( mRenderer ) { mRenderer.SetGeometry( mMesh ); mRenderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON ); + mRenderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON ); } } } @@ -588,41 +599,54 @@ void Model3dView::CreateMaterial() void Model3dView::LoadTextures() { if( !mTextureSet ) - return ; - - if( (mTexture0Url != "") && (mIlluminationType != Toolkit::Model3dView::DIFFUSE) ) { - std::string imgUrl = mImagesUrl + mTexture0Url; - - //Load textures - Image tex0 = ResourceImage::New( imgUrl ); - if( tex0 ) - { - mTextureSet.SetImage( 0u, tex0 ); - } + return; } - if( (mTexture1Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) ) + Sampler sampler = Sampler::New(); + sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR ); + + // Setup diffuse texture. + if( !mTexture0Url.empty() && ( mIlluminationType != Toolkit::Model3dView::DIFFUSE ) ) { - std::string imgUrl = mImagesUrl + mTexture1Url; + std::string imageUrl = mImagesUrl + mTexture0Url; //Load textures - Image tex1 = ResourceImage::New( imgUrl ); - if (tex1) + Texture diffuseTexture = LoadTexture( imageUrl.c_str() ); + if( diffuseTexture ) { - mTextureSet.SetImage( 1u, tex1 ); + mTextureSet.SetTexture( DIFFUSE_TEXTURE_INDEX, diffuseTexture ); + mTextureSet.SetSampler( DIFFUSE_TEXTURE_INDEX, sampler ); } } - if( (mTexture2Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) ) + if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP ) { - std::string imgUrl = mImagesUrl + mTexture2Url; + // Setup normal map texture. + if( !mTexture1Url.empty() ) + { + std::string imageUrl = mImagesUrl + mTexture1Url; - //Load textures - Image tex2 = ResourceImage::New( imgUrl ); - if( tex2 ) + //Load textures + Texture normalTexture = LoadTexture( imageUrl.c_str() ); + if( normalTexture ) + { + mTextureSet.SetTexture( NORMAL_TEXTURE_INDEX, normalTexture ); + mTextureSet.SetSampler( NORMAL_TEXTURE_INDEX, sampler ); + } + } + if( !mTexture2Url.empty() ) { - mTextureSet.SetImage( 2u, tex2 ); + // Setup gloss map texture. + std::string imageUrl = mImagesUrl + mTexture2Url; + + //Load textures + Texture glossTexture = LoadTexture( imageUrl.c_str() ); + if( glossTexture ) + { + mTextureSet.SetTexture( GLOSS_TEXTURE_INDEX, glossTexture ); + mTextureSet.SetSampler( GLOSS_TEXTURE_INDEX, sampler ); + } } } } @@ -639,7 +663,7 @@ int Model3dView::GetShaderProperties( Toolkit::Model3dView::IlluminationType ill if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP ) { - objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINOMIALS; + objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS; } return objectProperties;