X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fshadow-view%2Fshadow-view-impl.cpp;h=c19f3c718a0e7849206681357ca2206617fc4273;hp=fbc2e1c7b5e0cf91edc4efe5c83b9f6de3fc228e;hb=11ed6421771d05113ae1a6510167d8c2557ac20e;hpb=e2eda444afbe82e9591fe198eef339227f90a616 diff --git a/dali-toolkit/internal/controls/shadow-view/shadow-view-impl.cpp b/dali-toolkit/internal/controls/shadow-view/shadow-view-impl.cpp index fbc2e1c..c19f3c7 100644 --- a/dali-toolkit/internal/controls/shadow-view/shadow-view-impl.cpp +++ b/dali-toolkit/internal/controls/shadow-view/shadow-view-impl.cpp @@ -1,18 +1,19 @@ -// -// Copyright (c) 2014 Samsung Electronics Co., Ltd. -// -// Licensed under the Flora License, Version 1.0 (the License); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://floralicense.org/license/ -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an AS IS BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +/* + * Copyright (c) 2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ // CLASS HEADER #include "shadow-view-impl.h" @@ -20,17 +21,23 @@ // EXTERNAL INCLUDES #include #include +#include +#include +#include +#include +#include +#include +#include // INTERNAL INCLUDES +#include #include #include -#include // TODO: // pixel format / size - set from JSON // aspect ratio property needs to be able to be constrained also for cameras. (now do-able) // default near clip value -// mChildrenRoot Add()/Remove() overloads - better solution ///////////////////////////////////////////////////////// @@ -63,8 +70,8 @@ BaseHandle Create() return Toolkit::ShadowView::New(); } -TypeRegistration mType( typeid(Toolkit::ShadowView), typeid(Toolkit::Control), Create ); - +DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ShadowView, Toolkit::Control, Create ) +DALI_TYPE_REGISTRATION_END() const float BLUR_STRENGTH_DEFAULT = 1.0f; @@ -73,26 +80,37 @@ const float DEFAULT_FIELD_OF_VIEW_RADIANS = Math::PI / 4.0f; // 45 degrees const Vector4 DEFAULT_SHADOW_COLOR = Vector4(0.2f, 0.2f, 0.2f, 0.8f); -const std::string SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME( "uLightCameraProjectionMatrix" ); -const std::string SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME( "uLightCameraViewMatrix" ); -const std::string SHADER_SHADOW_COLOR_PROPERTY_NAME( "uShadowColor" ); - -const std::string BLUR_STRENGTH_PROPERTY_NAME( "BlurStrengthProperty" ); -const std::string SHADOW_COLOR_PROPERTY_NAME( "ShadowColorProperty" ); +const char* const SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME = "uLightCameraProjectionMatrix"; +const char* const SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME = "uLightCameraViewMatrix"; +const char* const SHADER_SHADOW_COLOR_PROPERTY_NAME = "uShadowColor"; +const char* const BLUR_STRENGTH_PROPERTY_NAME = "BlurStrengthProperty"; +const char* const SHADOW_COLOR_PROPERTY_NAME = "ShadowColorProperty"; const char* const RENDER_SHADOW_VERTEX_SOURCE = + + " attribute mediump vec2 aPosition;\n" + " uniform mediump mat4 uMvpMatrix;\n" + " uniform mediump mat4 uModelMatrix;\n" + " uniform vec3 uSize;\n" + " varying vec2 vTexCoord;\n" + " uniform mediump mat4 uLightCameraProjectionMatrix;\n" " uniform mediump mat4 uLightCameraViewMatrix;\n" "\n" "void main()\n" "{\n" - " gl_Position = uProjection * uModelView * vec4(aPosition,1.0);\n" - " vec4 textureCoords = uLightCameraProjectionMatrix * uLightCameraViewMatrix * uModelMatrix * vec4(aPosition,1.0);\n" + " mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n" + " vertexPosition.xyz *= uSize;\n" + " gl_Position = uMvpMatrix * vertexPosition;\n" + " vec4 textureCoords = uLightCameraProjectionMatrix * uLightCameraViewMatrix * uModelMatrix * vertexPosition;\n" " vTexCoord = 0.5 + 0.5 * (textureCoords.xy/textureCoords.w);\n" "}\n"; const char* const RENDER_SHADOW_FRAGMENT_SOURCE = + "varying mediump vec2 vTexCoord;\n" "uniform lowp vec4 uShadowColor;\n" + "uniform sampler2D sTexture;\n" + "void main()\n" "{\n" " lowp float alpha;\n" @@ -100,23 +118,10 @@ const char* const RENDER_SHADOW_FRAGMENT_SOURCE = " gl_FragColor = vec4(uShadowColor.rgb, uShadowColor.a * alpha);\n" "}\n"; -// TODO: Add this to dali-core constraints.h -/** - * EqualToConstraintMatrix - * - * f(current, property) = property - */ -struct EqualToConstraintMatrix -{ - EqualToConstraintMatrix(){} - - Dali::Matrix operator()(const Dali::Matrix& current, const PropertyInput& property) {return property.GetMatrix();} -}; - } // namespace ShadowView::ShadowView( float downsampleWidthScale, float downsampleHeightScale ) -: ControlImpl( false ), // doesn't require touch events +: Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ), mChildrenRoot(Actor::New()), mCachedShadowColor(DEFAULT_SHADOW_COLOR), mCachedBackgroundColor(DEFAULT_SHADOW_COLOR.r, DEFAULT_SHADOW_COLOR.g, DEFAULT_SHADOW_COLOR.b, 0.0f), @@ -144,29 +149,17 @@ Toolkit::ShadowView ShadowView::New(float downsampleWidthScale, float downsample return handle; } -///////////////////////////////////////////////////////////// -// for creating a subtree for all user added child actors. -// TODO: overloading Actor::Add()/Remove() not nice since breaks polymorphism. Need another method to pass ownership of added child actors to our internal actor root. -void ShadowView::Add(Actor child) -{ - mChildrenRoot.Add(child); -} - -void ShadowView::Remove(Actor child) -{ - mChildrenRoot.Remove(child); -} - -void ShadowView::SetShadowPlane(Actor shadowPlane) +void ShadowView::SetShadowPlaneBackground(Actor shadowPlaneBackground) { - mShadowPlaneBg = shadowPlane; + mShadowPlaneBg = shadowPlaneBackground; - mShadowPlane = ImageActor::New(); + mShadowPlane = Toolkit::ImageView::New( mOutputImage ); + mShadowPlane.SetName( "SHADOW_PLANE" ); mShadowPlane.SetParentOrigin(ParentOrigin::CENTER); mShadowPlane.SetAnchorPoint(AnchorPoint::CENTER); - mShadowPlane.SetImage(mOutputImage); - mShadowPlane.SetShaderEffect(mShadowRenderShader); + mShadowPlane.SetProperty( Toolkit::ImageView::Property::IMAGE, mShadowVisualMap ); + SetShaderConstants(); // Rather than parent the shadow plane drawable and have constraints to move it to the same // position, instead parent the shadow plane drawable on the shadow plane passed in. @@ -176,9 +169,9 @@ void ShadowView::SetShadowPlane(Actor shadowPlane) ConstrainCamera(); - mShadowPlane.ApplyConstraint( Constraint::New( Actor::SIZE, Source( mShadowPlaneBg, Actor::SIZE ), EqualToConstraint() ) ); + mShadowPlane.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); - mBlurRootActor.ApplyConstraint( Constraint::New( Actor::SIZE, Source( mShadowPlane, Actor::SIZE ), EqualToConstraint() ) ); + mBlurRootActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); } void ShadowView::SetPointLight(Actor pointLight) @@ -200,7 +193,10 @@ void ShadowView::SetShadowColor(Vector4 color) mCachedBackgroundColor.g = color.g; mCachedBackgroundColor.b = color.b; - Self().SetProperty( mShadowColorPropertyIndex, mCachedShadowColor ); + if( mShadowPlane ) + { + mShadowPlane.SetProperty( mShadowColorPropertyIndex, mCachedShadowColor ); + } if(mRenderSceneTask) { mRenderSceneTask.SetClearColor( mCachedBackgroundColor ); @@ -232,22 +228,31 @@ void ShadowView::Deactivate() void ShadowView::OnInitialize() { // root actor to parent all user added actors. Used as source actor for shadow render task. - mChildrenRoot.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION ); - mChildrenRoot.ApplyConstraint(Constraint::New( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() )); + mChildrenRoot.SetParentOrigin( ParentOrigin::CENTER ); + mChildrenRoot.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); Vector2 stageSize = Stage::GetCurrent().GetSize(); mCameraActor = CameraActor::New(stageSize); + mCameraActor.SetParentOrigin( ParentOrigin::CENTER ); // Target is constrained to point at the shadow plane origin mCameraActor.SetNearClippingPlane( 1.0f ); mCameraActor.SetType( Dali::Camera::FREE_LOOK ); // Camera orientation constrained to point at shadow plane world position - mCameraActor.SetInvertYAxis(false); + mCameraActor.SetOrientation(Radian(Degree(180)), Vector3::YAXIS); mCameraActor.SetPosition(DEFAULT_LIGHT_POSITION); - mShadowRenderShader = ShaderEffect::New( RENDER_SHADOW_VERTEX_SOURCE, RENDER_SHADOW_FRAGMENT_SOURCE, - Dali::GeometryType( GEOMETRY_TYPE_IMAGE ), - ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING )); + + Property::Map customShader; + customShader[ Toolkit::Visual::Shader::Property::VERTEX_SHADER ] = RENDER_SHADOW_VERTEX_SOURCE; + customShader[ Toolkit::Visual::Shader::Property::FRAGMENT_SHADER ] = RENDER_SHADOW_FRAGMENT_SOURCE; + + customShader[ Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_X ] = 20; + customShader[ Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_Y ] = 20; + + customShader[ Toolkit::Visual::Shader::Property::HINTS ] = Shader::Hint::OUTPUT_IS_TRANSPARENT; + + mShadowVisualMap[ Toolkit::Visual::Property::SHADER ] = customShader; // Create render targets needed for rendering from light's point of view mSceneFromLightRenderTarget = FrameBufferImage::New( stageSize.width, stageSize.height, Pixel::RGBA8888 ); @@ -267,10 +272,12 @@ void ShadowView::OnInitialize() mBlurFilter.SetPixelFormat(Pixel::RGBA8888); mBlurRootActor = Actor::New(); + mBlurRootActor.SetName( "BLUR_ROOT_ACTOR" ); // Turn off inheritance to ensure filter renders properly - mBlurRootActor.SetPositionInheritanceMode(USE_PARENT_POSITION); - mBlurRootActor.SetInheritRotation(false); + mBlurRootActor.SetParentOrigin( ParentOrigin::CENTER ); + mBlurRootActor.SetInheritPosition(false); + mBlurRootActor.SetInheritOrientation(false); mBlurRootActor.SetInheritScale(false); mBlurRootActor.SetColorMode(USE_OWN_COLOR); @@ -279,25 +286,30 @@ void ShadowView::OnInitialize() mBlurFilter.SetRootActor(mBlurRootActor); mBlurFilter.SetBackgroundColor(Vector4::ZERO); - SetShaderConstants(); -} + CustomActor self = Self(); + // Register a property that the user can use to control the blur in the internal object + mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT); -void ShadowView::OnSizeSet(const Vector3& targetSize) -{ + Constraint blurStrengthConstraint = Constraint::New( mBlurFilter.GetHandleForAnimateBlurStrength(), mBlurFilter.GetBlurStrengthPropertyIndex(), EqualToConstraint() ); + blurStrengthConstraint.AddSource( Source( self, mBlurStrengthPropertyIndex) ); + blurStrengthConstraint.Apply(); } -void ShadowView::OnStageConnection() +void ShadowView::OnChildAdd( Actor& child ) { - // TODO: can't call this here, since SetImage() calls fail to connect images to stage, since parent chain not fully on stage yet - // Need to fix the stage connection so this callback can be used arbitrarily. At that point we can simplify the API by removing the need for Activate() / Deactivate() - //Activate(); + Control::OnChildAdd( child ); + + if( child != mChildrenRoot && child != mBlurRootActor) + { + mChildrenRoot.Add( child ); + } } -void ShadowView::OnStageDisconnection() +void ShadowView::OnChildRemove( Actor& child ) { - // TODO: can't call this here, since SetImage() calls fails similarly to above - // Need to fix the stage connection so this callback can be used arbitrarily. At that point we can simplify the API by removing the need for Activate() / Deactivate() - //Deactivate(); + mChildrenRoot.Remove( child ); + + Control::OnChildRemove( child ); } void ShadowView::ConstrainCamera() @@ -307,18 +319,15 @@ void ShadowView::ConstrainCamera() // Constrain camera to look directly at center of shadow plane. (mPointLight position // is under control of application, can't use transform inheritance) - Constraint cameraOrientationConstraint = - Constraint::New ( Actor::ROTATION, - Source( mShadowPlane, Actor::WORLD_POSITION ), - Source( mPointLight, Actor::WORLD_POSITION ), - Source( mShadowPlane, Actor::WORLD_ROTATION ), - &LookAt ); + Constraint cameraOrientationConstraint = Constraint::New ( mCameraActor, Actor::Property::ORIENTATION, &LookAt ); + cameraOrientationConstraint.AddSource( Source( mShadowPlane, Actor::Property::WORLD_POSITION ) ); + cameraOrientationConstraint.AddSource( Source( mPointLight, Actor::Property::WORLD_POSITION ) ); + cameraOrientationConstraint.AddSource( Source( mShadowPlane, Actor::Property::WORLD_ORIENTATION ) ); + cameraOrientationConstraint.Apply(); - mCameraActor.ApplyConstraint( cameraOrientationConstraint ); - - Constraint pointLightPositionConstraint = Constraint::New( Actor::POSITION, Source( mPointLight, Actor::WORLD_POSITION ), EqualToConstraint() ); - - mCameraActor.ApplyConstraint( pointLightPositionConstraint ); + Constraint pointLightPositionConstraint = Constraint::New( mCameraActor, Actor::Property::POSITION, EqualToConstraint() ); + pointLightPositionConstraint.AddSource( Source( mPointLight, Actor::Property::WORLD_POSITION ) ); + pointLightPositionConstraint.Apply(); } } @@ -355,32 +364,17 @@ void ShadowView::RemoveRenderTasks() void ShadowView::SetShaderConstants() { - CustomActor self = Self(); - - mShadowRenderShader.SetUniform( SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME, Matrix::IDENTITY ); - mShadowRenderShader.SetUniform( SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME, Matrix::IDENTITY ); - mShadowRenderShader.SetUniform( SHADER_SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor ); - - Property::Index lightCameraProjectionMatrixPropertyIndex = mShadowRenderShader.GetPropertyIndex(SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME); - Property::Index lightCameraViewMatrixPropertyIndex = mShadowRenderShader.GetPropertyIndex(SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME); - - Constraint projectionMatrixConstraint = Constraint::New( lightCameraProjectionMatrixPropertyIndex, Source( mCameraActor, CameraActor::PROJECTION_MATRIX ), EqualToConstraintMatrix()); - Constraint viewMatrixConstraint = Constraint::New( lightCameraViewMatrixPropertyIndex, Source( mCameraActor, CameraActor::VIEW_MATRIX ), EqualToConstraintMatrix()); - - mShadowRenderShader.ApplyConstraint(projectionMatrixConstraint); - mShadowRenderShader.ApplyConstraint(viewMatrixConstraint); - - // Register a property that the user can use to control the blur in the internal object - mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT); - mBlurFilter.GetHandleForAnimateBlurStrength().ApplyConstraint( Constraint::New( mBlurFilter.GetBlurStrengthPropertyIndex() , - Source( self, mBlurStrengthPropertyIndex), - EqualToConstraint()) ); + Property::Index lightCameraProjectionMatrixPropertyIndex = mShadowPlane.RegisterProperty( SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME, Matrix::IDENTITY ); + Constraint projectionMatrixConstraint = Constraint::New( mShadowPlane, lightCameraProjectionMatrixPropertyIndex, EqualToConstraint() ); + projectionMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::PROJECTION_MATRIX ) ); + projectionMatrixConstraint.Apply(); - // Register a property that the user can use to control the color of the shadow. - Property::Index index = mShadowRenderShader.GetPropertyIndex(SHADER_SHADOW_COLOR_PROPERTY_NAME); - mShadowColorPropertyIndex = self.RegisterProperty(SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor); + Property::Index lightCameraViewMatrixPropertyIndex = mShadowPlane.RegisterProperty( SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME, Matrix::IDENTITY ); + Constraint viewMatrixConstraint = Constraint::New( mShadowPlane, lightCameraViewMatrixPropertyIndex, EqualToConstraint() ); + viewMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::VIEW_MATRIX ) ); + viewMatrixConstraint.Apply(); - mShadowRenderShader.ApplyConstraint(Constraint::New( index, Source( self, mShadowColorPropertyIndex ), EqualToConstraint()) ); + mShadowColorPropertyIndex = mShadowPlane.RegisterProperty( SHADER_SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor ); } } // namespace Internal