X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fscene3d-view%2Fscene3d-view-impl.cpp;h=6231b64d776e51cbc9144e8f8c92af1eb00972b5;hp=2f7bd6b2e0c6511fb8ffeee970e37d3c59c6611b;hb=bee78157f19fa54ae961081782a2f6537cc8dd5a;hpb=1bcd0bb90e1614c3688026e6b6f181610d0ede83 diff --git a/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp b/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp index 2f7bd6b..6231b64 100644 --- a/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp +++ b/dali-toolkit/internal/controls/scene3d-view/scene3d-view-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * Copyright (c) 2021 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. @@ -21,39 +21,41 @@ // EXTERNAL INCLUDES #include +// INTERNAL INCLUDES +#include + namespace Dali { - namespace Toolkit { - namespace Internal { - namespace { +const char* const IMAGE_BRDF_FILE_NAME = "brdfLUT.png"; // glTF file extension -const std::string GLTF_EXT( ".gltf" ); +const std::string GLTF_EXT(".gltf"); /** * cube map face index */ -const uint32_t CUBEMAP_INDEX_X[2][6] = { { 2, 0, 1, 1, 1, 3 }, { 0, 1, 2, 3, 4, 5 } }; -const uint32_t CUBEMAP_INDEX_Y[2][6] = { { 1, 1, 0, 2, 1, 1 }, { 0, 0, 0, 0, 0, 0 } }; +const uint32_t CUBEMAP_INDEX_X[2][6] = {{2, 0, 1, 1, 1, 3}, {0, 1, 2, 3, 4, 5}}; +const uint32_t CUBEMAP_INDEX_Y[2][6] = {{1, 1, 0, 2, 1, 1}, {0, 0, 0, 0, 0, 0}}; -}//namespace +} //namespace Scene3dView::Scene3dView() - : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ), - mRoot( Actor::New() ), +: Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)), + mRoot(Actor::New()), mShaderArray(), mCameraActorArray(), - mDefaultCamera( CameraActor::New() ), + mDefaultCamera(CameraActor::New()), mAnimationArray(), - mLightType( Toolkit::Scene3dView::LightType::NONE ), - mLightVector( Vector3::ONE ), - mLightColor( Vector3::ONE ) + mLightType(Toolkit::Scene3dView::LightType::NONE), + mLightVector(Vector3::ONE), + mLightColor(Vector3::ONE), + mUseIBL(false) { } @@ -61,11 +63,11 @@ Scene3dView::~Scene3dView() { } -Toolkit::Scene3dView Scene3dView::New( const std::string& filePath ) +Toolkit::Scene3dView Scene3dView::New(const std::string& filePath) { Scene3dView* impl = new Scene3dView(); - Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView( *impl ); + Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView(*impl); // Second-phase init of the implementation // This can only be done after the CustomActor connection has been made... @@ -75,16 +77,16 @@ Toolkit::Scene3dView Scene3dView::New( const std::string& filePath ) return handle; } -Toolkit::Scene3dView Scene3dView::New( const std::string& filePath, const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor ) +Toolkit::Scene3dView Scene3dView::New(const std::string& filePath, const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor) { Scene3dView* impl = new Scene3dView(); - Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView( *impl ); + Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView(*impl); // Second-phase init of the implementation // This can only be done after the CustomActor connection has been made... impl->mFilePath = filePath; - impl->SetCubeMap( diffuseTexturePath, specularTexturePath, scaleFactor ); + impl->SetCubeMap(diffuseTexturePath, specularTexturePath, scaleFactor); impl->Initialize(); return handle; @@ -92,10 +94,10 @@ Toolkit::Scene3dView Scene3dView::New( const std::string& filePath, const std::s bool Scene3dView::CreateScene() { - if( std::string::npos != mFilePath.rfind( GLTF_EXT ) ) + if(std::string::npos != mFilePath.rfind(GLTF_EXT)) { Internal::Gltf::Loader gltfloader; - return( gltfloader.LoadScene( mFilePath, *this ) ); + return (gltfloader.LoadScene(mFilePath, *this)); } return false; @@ -106,9 +108,9 @@ uint32_t Scene3dView::GetAnimationCount() return mAnimationArray.size(); } -bool Scene3dView::PlayAnimation( uint32_t index ) +bool Scene3dView::PlayAnimation(uint32_t index) { - if( GetAnimationCount() <= index ) + if(GetAnimationCount() <= index) { return false; } @@ -119,7 +121,7 @@ bool Scene3dView::PlayAnimation( uint32_t index ) bool Scene3dView::PlayAnimations() { - for( auto&& animation : mAnimationArray ) + for(auto&& animation : mAnimationArray) { animation.Play(); } @@ -127,43 +129,37 @@ bool Scene3dView::PlayAnimations() return true; } -bool Scene3dView::SetLight( Toolkit::Scene3dView::LightType type, Vector3 lightVector, Vector3 lightColor ) +bool Scene3dView::SetLight(Toolkit::Scene3dView::LightType type, Vector3 lightVector, Vector3 lightColor) { - if( type > Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT ) - { - return false; - } - - mLightType = static_cast( - ( mLightType >= Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT ) ? - Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT + type : - type ); - + mLightType = type; mLightVector = lightVector; - mLightColor = lightColor; + mLightColor = lightColor; - for( auto&& shader : mShaderArray ) + for(auto&& shader : mShaderArray) { - shader.RegisterProperty( "uLightType", ( GetLightType() & ~Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT ) ); - shader.RegisterProperty( "uLightVector", lightVector ); - shader.RegisterProperty( "uLightColor", lightColor ); + float hasLightSource = static_cast(!!(GetLightType() & (Toolkit::Scene3dView::LightType::POINT_LIGHT | Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT))); + float isPointLight = static_cast(!!(GetLightType() & Toolkit::Scene3dView::LightType::POINT_LIGHT)); + shader.RegisterProperty("uHasLightSource", hasLightSource); + shader.RegisterProperty("uIsPointLight", isPointLight); + shader.RegisterProperty("uLightVector", lightVector); + shader.RegisterProperty("uLightColor", lightColor); } return true; } -uint8_t* Scene3dView::GetCroppedBuffer( uint8_t* sourceBuffer, uint32_t bytesPerPixel, uint32_t width, uint32_t height, uint32_t xOffset, uint32_t yOffset, uint32_t xFaceSize, uint32_t yFaceSize ) +uint8_t* Scene3dView::GetCroppedBuffer(uint8_t* sourceBuffer, uint32_t bytesPerPixel, uint32_t width, uint32_t height, uint32_t xOffset, uint32_t yOffset, uint32_t xFaceSize, uint32_t yFaceSize) { - uint32_t byteSize = bytesPerPixel * xFaceSize * yFaceSize; - uint8_t* destBuffer = reinterpret_cast( malloc( byteSize + 4u ) ); + uint32_t byteSize = bytesPerPixel * xFaceSize * yFaceSize; + uint8_t* destBuffer = reinterpret_cast(malloc(byteSize + 4u)); - int32_t srcStride = width * bytesPerPixel; + int32_t srcStride = width * bytesPerPixel; int32_t destStride = xFaceSize * bytesPerPixel; - int32_t srcOffset = xOffset * bytesPerPixel + yOffset * srcStride; + int32_t srcOffset = xOffset * bytesPerPixel + yOffset * srcStride; int32_t destOffset = 0; - for( uint16_t row = yOffset; row < yOffset + yFaceSize; ++row ) + for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row) { - memcpy( destBuffer + destOffset, sourceBuffer + srcOffset, destStride ); + memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride); srcOffset += srcStride; destOffset += destStride; } @@ -171,22 +167,21 @@ uint8_t* Scene3dView::GetCroppedBuffer( uint8_t* sourceBuffer, uint32_t bytesPer return destBuffer; } -void Scene3dView::UploadTextureFace( Texture& texture, Devel::PixelBuffer pixelBuffer, uint32_t faceIndex ) +void Scene3dView::UploadTextureFace(Texture& texture, Devel::PixelBuffer pixelBuffer, uint32_t faceIndex) { - uint8_t* imageBuffer = pixelBuffer.GetBuffer(); - uint32_t bytesPerPixel = Pixel::GetBytesPerPixel( pixelBuffer.GetPixelFormat() ); - uint32_t imageWidth = pixelBuffer.GetWidth(); - uint32_t imageHeight = pixelBuffer.GetHeight(); + uint8_t* imageBuffer = pixelBuffer.GetBuffer(); + uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(pixelBuffer.GetPixelFormat()); + uint32_t imageWidth = pixelBuffer.GetWidth(); + uint32_t imageHeight = pixelBuffer.GetHeight(); - CubeType cubeType = ( imageWidth / 4 == imageHeight / 3 ) ? CROSS_HORIZONTAL : - ( ( imageWidth / 6 == imageHeight ) ? ARRAY_HORIZONTAL : NONE ); + CubeType cubeType = (imageWidth / 4 == imageHeight / 3) ? CROSS_HORIZONTAL : ((imageWidth / 6 == imageHeight) ? ARRAY_HORIZONTAL : NONE); uint32_t faceSize = 0; - if( cubeType == CROSS_HORIZONTAL ) + if(cubeType == CROSS_HORIZONTAL) { faceSize = imageWidth / 4; } - else if( cubeType == ARRAY_HORIZONTAL ) + else if(cubeType == ARRAY_HORIZONTAL) { faceSize = imageWidth / 6; } @@ -198,69 +193,69 @@ void Scene3dView::UploadTextureFace( Texture& texture, Devel::PixelBuffer pixelB uint32_t xOffset = CUBEMAP_INDEX_X[cubeType][faceIndex] * faceSize; uint32_t yOffset = CUBEMAP_INDEX_Y[cubeType][faceIndex] * faceSize; - uint8_t* tempImageBuffer = GetCroppedBuffer( imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, faceSize, faceSize ); - PixelData pixelData = PixelData::New( tempImageBuffer, faceSize * faceSize * bytesPerPixel, faceSize, faceSize, pixelBuffer.GetPixelFormat(), PixelData::FREE ); - texture.Upload( pixelData, CubeMapLayer::POSITIVE_X + faceIndex, 0, 0, 0, faceSize, faceSize ); + uint8_t* tempImageBuffer = GetCroppedBuffer(imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, faceSize, faceSize); + PixelData pixelData = PixelData::New(tempImageBuffer, faceSize * faceSize * bytesPerPixel, faceSize, faceSize, pixelBuffer.GetPixelFormat(), PixelData::FREE); + texture.Upload(pixelData, CubeMapLayer::POSITIVE_X + faceIndex, 0, 0, 0, faceSize, faceSize); } -void Scene3dView::SetCubeMap( const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor ) +void Scene3dView::SetCubeMap(const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor) { - mLightType = Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT; - // BRDF texture - std::string imageBrdfUrl = DALI_IMAGE_DIR "brdfLUT.png"; - mBRDFTexture = LoadTexture( imageBrdfUrl.c_str(), true ); - if( !mBRDFTexture ) + const std::string imageDirPath = AssetManager::GetDaliImagePath(); + const std::string imageBrdfUrl = imageDirPath + IMAGE_BRDF_FILE_NAME; + mBRDFTexture = LoadTexture(imageBrdfUrl.c_str(), true); + if(!mBRDFTexture) { return; } // Diffuse Cube Map - Devel::PixelBuffer diffusePixelBuffer = LoadImageFromFile( diffuseTexturePath ); - uint32_t diffuseFaceSize = diffusePixelBuffer.GetWidth() / 4; - mDiffuseTexture = Texture::New( TextureType::TEXTURE_CUBE, diffusePixelBuffer.GetPixelFormat(), diffuseFaceSize, diffuseFaceSize ); - for( uint32_t i = 0; i < 6; ++i ) + Devel::PixelBuffer diffusePixelBuffer = LoadImageFromFile(diffuseTexturePath); + uint32_t diffuseFaceSize = diffusePixelBuffer.GetWidth() / 4; + mDiffuseTexture = Texture::New(TextureType::TEXTURE_CUBE, diffusePixelBuffer.GetPixelFormat(), diffuseFaceSize, diffuseFaceSize); + for(uint32_t i = 0; i < 6; ++i) { - UploadTextureFace( mDiffuseTexture, diffusePixelBuffer, i ); + UploadTextureFace(mDiffuseTexture, diffusePixelBuffer, i); } mDiffuseTexture.GenerateMipmaps(); // Specular Cube Map - Devel::PixelBuffer specularPixelBuffer = LoadImageFromFile( specularTexturePath ); - uint32_t specularFaceSize = specularPixelBuffer.GetWidth() / 4; - mSpecularTexture = Texture::New( TextureType::TEXTURE_CUBE, specularPixelBuffer.GetPixelFormat(), specularFaceSize, specularFaceSize ); - for( uint32_t i = 0; i < 6; ++i ) + Devel::PixelBuffer specularPixelBuffer = LoadImageFromFile(specularTexturePath); + uint32_t specularFaceSize = specularPixelBuffer.GetWidth() / 4; + mSpecularTexture = Texture::New(TextureType::TEXTURE_CUBE, specularPixelBuffer.GetPixelFormat(), specularFaceSize, specularFaceSize); + for(uint32_t i = 0; i < 6; ++i) { - UploadTextureFace( mSpecularTexture, specularPixelBuffer, i ); + UploadTextureFace(mSpecularTexture, specularPixelBuffer, i); } mSpecularTexture.GenerateMipmaps(); mIBLScaleFactor = scaleFactor; + mUseIBL = true; } -bool Scene3dView::SetDefaultCamera( const Dali::Camera::Type type, const float nearPlane, const Vector3 cameraPosition ) +bool Scene3dView::SetDefaultCamera(const Dali::Camera::Type type, const float nearPlane, const Vector3 cameraPosition) { - mDefaultCamera.SetParentOrigin( ParentOrigin::CENTER ); - mDefaultCamera.SetAnchorPoint( AnchorPoint::CENTER ); - mDefaultCamera.SetType( type ); - mDefaultCamera.SetNearClippingPlane( nearPlane ); - mDefaultCamera.SetPosition( cameraPosition ); + mDefaultCamera.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); + mDefaultCamera.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); + mDefaultCamera.SetType(type); + mDefaultCamera.SetNearClippingPlane(nearPlane); + mDefaultCamera.SetProperty(Actor::Property::POSITION, cameraPosition); return true; } -void Scene3dView::AddCamera( CameraActor cameraActor ) +void Scene3dView::AddCamera(CameraActor cameraActor) { - mCameraActorArray.push_back( cameraActor ); + mCameraActorArray.push_back(cameraActor); } -void Scene3dView::AddAnimation( Animation animation ) +void Scene3dView::AddAnimation(Animation animation) { - mAnimationArray.push_back( animation ); + mAnimationArray.push_back(animation); } -void Scene3dView::AddShader( Shader shader ) +void Scene3dView::AddShader(Shader shader) { - mShaderArray.push_back( shader ); + mShaderArray.push_back(shader); } Actor Scene3dView::GetRoot() @@ -278,10 +273,10 @@ uint32_t Scene3dView::GetCameraCount() return mCameraActorArray.size(); } -CameraActor Scene3dView::GetCamera( uint32_t cameraIndex ) +CameraActor Scene3dView::GetCamera(uint32_t cameraIndex) { CameraActor cameraActor; - if( cameraIndex >= mCameraActorArray.size() ) + if(cameraIndex >= mCameraActorArray.size()) { return cameraActor; } @@ -319,23 +314,28 @@ Texture Scene3dView::GetSpecularTexture() return mSpecularTexture; } +bool Scene3dView::HasImageBasedLighting() +{ + return mUseIBL; +} + Texture Scene3dView::GetDiffuseTexture() { return mDiffuseTexture; } -Texture Scene3dView::LoadTexture( const char *imageUrl, bool generateMipmaps ) +Texture Scene3dView::LoadTexture(const char* imageUrl, bool generateMipmaps) { Texture texture; - Devel::PixelBuffer pixelBuffer = LoadImageFromFile( imageUrl ); - if( pixelBuffer ) + 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 = Texture::New(TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight()); + PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer); + texture.Upload(pixelData); - if( generateMipmaps ) + if(generateMipmaps) { texture.GenerateMipmaps(); } @@ -346,27 +346,26 @@ Texture Scene3dView::LoadTexture( const char *imageUrl, bool generateMipmaps ) void Scene3dView::OnInitialize() { - mRoot.SetParentOrigin( ParentOrigin::CENTER ); - mRoot.SetAnchorPoint( AnchorPoint::CENTER ); + mRoot.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); + mRoot.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); Layer layer = Layer::New(); - layer.SetBehavior( Layer::LAYER_3D ); - layer.SetParentOrigin( ParentOrigin::CENTER ); - layer.SetAnchorPoint( AnchorPoint::CENTER ); - layer.Add( mRoot ); + layer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D); + layer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); + layer.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); + layer.Add(mRoot); Actor self = Self(); // Apply some default resizing rules. - self.SetParentOrigin( ParentOrigin::CENTER ); - self.SetAnchorPoint( AnchorPoint::CENTER ); - self.Add( layer ); + self.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); + self.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); + self.Add(layer); CreateScene(); } -}//namespace Internal - -}//namespace Toolkit +} //namespace Internal -}//namespace Dali +} //namespace Toolkit +} //namespace Dali