2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "wireframe-visual.h"
23 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
24 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
25 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
26 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
27 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
40 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
41 const char * const INDEX_NAME("indices");
43 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
44 attribute mediump vec2 aPosition;\n
45 uniform mediump mat4 uMvpMatrix;\n
46 uniform mediump vec3 uSize;\n
49 //Visual size and offset
50 uniform mediump vec2 offset;\n
51 uniform mediump vec2 size;\n
52 uniform mediump vec4 offsetSizeMode;\n
53 uniform mediump vec2 origin;\n
54 uniform mediump vec2 anchorPoint;\n
56 vec4 ComputeVertexPosition()\n
58 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
59 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
60 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
65 gl_Position = uMvpMatrix * ComputeVertexPosition();\n
69 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(\n
70 uniform lowp vec4 uColor;\n
71 uniform lowp vec4 mixColor;\n
75 gl_FragColor = uColor*mixColor;\n
81 WireframeVisualPtr WireframeVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
83 Visual::BasePtr emtptyVisual;
85 return New(factoryCache, emtptyVisual, properties);
88 WireframeVisualPtr WireframeVisual::New( VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual )
90 return new WireframeVisual( factoryCache, actualVisual );
93 WireframeVisualPtr WireframeVisual::New( VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual, const Property::Map& properties )
95 WireframeVisualPtr wireframeVisual( new WireframeVisual( factoryCache, actualVisual ) );
97 // Instead of calling SetProperties, looking for the only valid property 'transform'
98 Property::Value* transformValue = properties.Find( DevelVisual::Property::TRANSFORM, TRANSFORM );
99 Property::Map transformMap;
100 if( transformValue && transformValue->Get( transformMap ) )
102 wireframeVisual->SetTransformAndSize( transformMap, Vector2::ZERO );
105 return wireframeVisual;
108 WireframeVisual::WireframeVisual( VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual )
109 : Visual::Base( factoryCache ),
110 mActualVisual( actualVisual )
114 WireframeVisual::~WireframeVisual()
118 float WireframeVisual::GetHeightForWidth( float width )
122 return mActualVisual->GetHeightForWidth( width );
126 return Visual::Base::GetHeightForWidth( width );
130 void WireframeVisual::GetNaturalSize( Vector2& naturalSize )
134 mActualVisual->GetNaturalSize( naturalSize );
138 Visual::Base::GetNaturalSize( naturalSize );
142 void WireframeVisual::DoCreatePropertyMap( Property::Map& map ) const
146 mActualVisual->CreatePropertyMap( map );
151 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::WIREFRAME );
155 void WireframeVisual::DoSetProperties( const Property::Map& propertyMap )
157 Property::Value* mixValue = propertyMap.Find( Toolkit::DevelVisual::Property::MIX_COLOR, MIX_COLOR );
161 mixValue->Get( mixColor );
162 SetMixColor( mixColor );
166 void WireframeVisual::DoSetOnStage( Actor& actor )
168 InitializeRenderer();
170 actor.AddRenderer( mImpl->mRenderer );
173 void WireframeVisual::InitializeRenderer()
175 Shader shader = mFactoryCache.GetShader( VisualFactoryCache::WIREFRAME_SHADER );
178 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
179 mFactoryCache.SaveShader( VisualFactoryCache::WIREFRAME_SHADER, shader );
182 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::WIREFRAME_GEOMETRY );
185 geometry = CreateQuadWireframeGeometry();
186 mFactoryCache.SaveGeometry( VisualFactoryCache::WIREFRAME_GEOMETRY, geometry );
189 //Create the renderer
190 mImpl->mRenderer = Renderer::New( geometry, shader);
192 //Register transform properties
193 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
196 Geometry WireframeVisual::CreateQuadWireframeGeometry()
198 const float halfWidth = 0.5f;
199 const float halfHeight = 0.5f;
200 struct QuadVertex { Vector2 position;};
201 QuadVertex quadVertexData[4] =
203 { Vector2(-halfWidth, -halfHeight) },
204 { Vector2( halfWidth, -halfHeight) },
205 { Vector2( halfWidth, halfHeight) },
206 { Vector2(-halfWidth, halfHeight) }
209 Property::Map quadVertexFormat;
210 quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
211 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
212 quadVertices.SetData( quadVertexData, 4 );
215 unsigned short indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 0 };
217 // Create the geometry object
218 Geometry geometry = Geometry::New();
219 geometry.AddVertexBuffer( quadVertices );
220 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
221 geometry.SetType( Geometry::LINES );
226 void WireframeVisual::OnSetTransform()
228 if( mImpl->mRenderer )
230 //Register transform properties
231 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
235 } // namespace Internal
237 } // namespace Toolkit