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/visual-factory/devel-visual-properties.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
74 gl_FragColor = uColor;\n
80 WireframeVisualPtr WireframeVisual::New( VisualFactoryCache& factoryCache )
82 return new WireframeVisual( factoryCache );
85 WireframeVisual::WireframeVisual( VisualFactoryCache& factoryCache )
86 : Visual::Base( factoryCache )
90 WireframeVisual::~WireframeVisual()
94 void WireframeVisual::DoSetProperties( const Property::Map& propertyMap )
96 // no properties supported at the moment
99 void WireframeVisual::DoSetOnStage( Actor& actor )
101 InitializeRenderer();
103 actor.AddRenderer( mImpl->mRenderer );
106 void WireframeVisual::DoCreatePropertyMap( Property::Map& map ) const
109 map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::WIREFRAME );
112 void WireframeVisual::InitializeRenderer()
114 Shader shader = mFactoryCache.GetShader( VisualFactoryCache::WIREFRAME_SHADER );
117 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
118 mFactoryCache.SaveShader( VisualFactoryCache::WIREFRAME_SHADER, shader );
121 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::WIREFRAME_GEOMETRY );
124 geometry = CreateQuadWireframeGeometry();
125 mFactoryCache.SaveGeometry( VisualFactoryCache::WIREFRAME_GEOMETRY, geometry );
128 //Create the renderer
129 mImpl->mRenderer = Renderer::New( geometry, shader);
131 //Register transform properties
132 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
135 Geometry WireframeVisual::CreateQuadWireframeGeometry()
137 const float halfWidth = 0.5f;
138 const float halfHeight = 0.5f;
139 struct QuadVertex { Vector2 position;};
140 QuadVertex quadVertexData[4] =
142 { Vector2(-halfWidth, -halfHeight) },
143 { Vector2( halfWidth, -halfHeight) },
144 { Vector2( halfWidth, halfHeight) },
145 { Vector2(-halfWidth, halfHeight) }
148 Property::Map quadVertexFormat;
149 quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
150 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
151 quadVertices.SetData( quadVertexData, 4 );
154 unsigned short indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 0 };
156 // Create the geometry object
157 Geometry geometry = Geometry::New();
158 geometry.AddVertexBuffer( quadVertices );
159 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
160 geometry.SetType( Geometry::LINES );
165 void WireframeVisual::OnSetTransform()
167 if( mImpl->mRenderer )
169 //Register transform properties
170 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
174 } // namespace Internal
176 } // namespace Toolkit