18bb1c3cef93b33b0745871a27a37d76fadf57a2
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / public-api / shader-effects / image-region-effect.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <dali-toolkit/public-api/shader-effects/image-region-effect.h>
18
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 namespace
26 {
27
28 const std::string TOP_LEFT_PROPERTY_NAME( "uTopLeft" );
29 const std::string BOTTOM_RIGHT_PROPERTY_NAME( "uBottomRight" );
30
31 } // namespace
32
33 ImageRegionEffect::ImageRegionEffect()
34 {
35 }
36
37 //Call the Parent copy constructor to add reference to the implementation for this object
38 ImageRegionEffect::ImageRegionEffect(ShaderEffect handle)
39 :ShaderEffect(handle)
40 {
41 }
42
43 ImageRegionEffect::~ImageRegionEffect()
44 {
45 }
46
47
48 ImageRegionEffect ImageRegionEffect::New()
49 {
50   std::string vertexShader(
51     "uniform vec2 uTopLeft;\n"
52     "uniform vec2 uBottomRight;\n"
53     "void main()\n"
54     "{\n"
55     "  vec4 position = vec4(aPosition,1.0);\n"
56     "  gl_Position = uMvpMatrix * position;\n"
57     // The line below is doing the same as the following commented lines:
58     //"  vec2 imageSize = sTextureRect.zw - sTextureRect.xy;\n"
59     //"  vec2 topLeft = sTextureRect.xy + uTopLeft * imageSize;\n"
60     //"  vec2 bottomRight = sTextureRect.xy + uBottomRight * imageSize;\n"
61     //"  vec2 texCoord = (aTexCoord - sTextureRect.xy) / imageSize;\n"
62     //"  vTexCoord = topLeft + texCoord * ( bottomRight - topLeft );\n"
63     "  vTexCoord = sTextureRect.xy + uTopLeft * ( sTextureRect.zw - sTextureRect.xy ) + ( aTexCoord - sTextureRect.xy ) * ( uBottomRight - uTopLeft );\n"
64     "}\n"
65   );
66
67   Dali::ShaderEffect shaderEffectCustom = Dali::ShaderEffect::New( vertexShader, "" );
68
69   Dali::Toolkit::ImageRegionEffect handle( shaderEffectCustom );
70
71   handle.SetUniform( TOP_LEFT_PROPERTY_NAME, Vector2( 0.f, 0.f ) );
72   handle.SetUniform( BOTTOM_RIGHT_PROPERTY_NAME, Vector2( 1.f, 1.f ) );
73
74   return handle;
75 }
76
77 void ImageRegionEffect::SetTopLeft(const Vector2& point)
78 {
79   SetUniform( TOP_LEFT_PROPERTY_NAME, point );
80 }
81
82 void ImageRegionEffect::SetBottomRight(const Vector2& point)
83 {
84   SetUniform( BOTTOM_RIGHT_PROPERTY_NAME, point );
85 }
86
87 const std::string& ImageRegionEffect::GetTopLeftPropertyName() const
88 {
89   return TOP_LEFT_PROPERTY_NAME;
90 }
91
92 const std::string& ImageRegionEffect::GetBottomRightPropertyName() const
93 {
94   return BOTTOM_RIGHT_PROPERTY_NAME;
95 }
96
97 } // namespace Toolkit
98
99 } // namespace Dali
100