Merge base & optional
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / shader-effects / image-region-effect.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
18 #include <dali-toolkit/public-api/shader-effects/image-region-effect.h>
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace
27 {
28
29 const std::string TOP_LEFT_PROPERTY_NAME( "uTopLeft" );
30 const std::string BOTTOM_RIGHT_PROPERTY_NAME( "uBottomRight" );
31
32 } // namespace
33
34 ImageRegionEffect::ImageRegionEffect()
35 {
36 }
37
38 //Call the Parent copy constructor to add reference to the implementation for this object
39 ImageRegionEffect::ImageRegionEffect(ShaderEffect handle)
40 :ShaderEffect(handle)
41 {
42 }
43
44 ImageRegionEffect::~ImageRegionEffect()
45 {
46 }
47
48
49 ImageRegionEffect ImageRegionEffect::New()
50 {
51   std::string vertexShader(
52     "uniform mediump vec2 uTopLeft;\n"
53     "uniform mediump vec2 uBottomRight;\n"
54     "void main()\n"
55     "{\n"
56     "  mediump vec4 position = vec4(aPosition,1.0);\n"
57     "  gl_Position = uMvpMatrix * position;\n"
58     // The line below is doing the same as the following commented lines:
59     //"  vec2 imageSize = sTextureRect.zw - sTextureRect.xy;\n"
60     //"  vec2 topLeft = sTextureRect.xy + uTopLeft * imageSize;\n"
61     //"  vec2 bottomRight = sTextureRect.xy + uBottomRight * imageSize;\n"
62     //"  vec2 texCoord = (aTexCoord - sTextureRect.xy) / imageSize;\n"
63     //"  vTexCoord = topLeft + texCoord * ( bottomRight - topLeft );\n"
64     "  vTexCoord = sTextureRect.xy + uTopLeft * ( sTextureRect.zw - sTextureRect.xy ) + ( aTexCoord - sTextureRect.xy ) * ( uBottomRight - uTopLeft );\n"
65     "}\n"
66   );
67
68   Dali::ShaderEffect shaderEffectCustom = Dali::ShaderEffect::New( vertexShader, "" );
69
70   Dali::Toolkit::ImageRegionEffect handle( shaderEffectCustom );
71
72   handle.SetUniform( TOP_LEFT_PROPERTY_NAME, Vector2( 0.f, 0.f ) );
73   handle.SetUniform( BOTTOM_RIGHT_PROPERTY_NAME, Vector2( 1.f, 1.f ) );
74
75   return handle;
76 }
77
78 void ImageRegionEffect::SetTopLeft(const Vector2& point)
79 {
80   SetUniform( TOP_LEFT_PROPERTY_NAME, point );
81 }
82
83 void ImageRegionEffect::SetBottomRight(const Vector2& point)
84 {
85   SetUniform( BOTTOM_RIGHT_PROPERTY_NAME, point );
86 }
87
88 const std::string& ImageRegionEffect::GetTopLeftPropertyName() const
89 {
90   return TOP_LEFT_PROPERTY_NAME;
91 }
92
93 const std::string& ImageRegionEffect::GetBottomRightPropertyName() const
94 {
95   return BOTTOM_RIGHT_PROPERTY_NAME;
96 }
97
98 } // namespace Toolkit
99
100 } // namespace Dali