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
50 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
51 vertexPosition.xyz *= uSize;\n
52 gl_Position = uMvpMatrix * vertexPosition;\n
56 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(\n
57 uniform lowp vec4 uColor;\n
61 gl_FragColor = uColor;\n
67 WireframeVisualPtr WireframeVisual::New( VisualFactoryCache& factoryCache )
69 return new WireframeVisual( factoryCache );
72 WireframeVisual::WireframeVisual( VisualFactoryCache& factoryCache )
73 : Visual::Base( factoryCache )
77 WireframeVisual::~WireframeVisual()
81 void WireframeVisual::DoSetProperties( const Property::Map& propertyMap )
83 // no properties supported at the moment
86 void WireframeVisual::DoSetOnStage( Actor& actor )
90 actor.AddRenderer( mImpl->mRenderer );
93 void WireframeVisual::DoCreatePropertyMap( Property::Map& map ) const
96 map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::WIREFRAME );
99 void WireframeVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
104 Dali::Property::Value WireframeVisual::DoGetProperty( Dali::Property::Index index )
107 return Dali::Property::Value();
110 void WireframeVisual::InitializeRenderer()
112 mImpl->mRenderer = mFactoryCache.GetWireframeRenderer();
113 if( !mImpl->mRenderer )
115 Geometry geometry = CreateQuadWireframeGeometry();
116 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
118 mImpl->mRenderer = Renderer::New( geometry, shader);
119 mFactoryCache.CacheWireframeRenderer( mImpl->mRenderer );
123 Geometry WireframeVisual::CreateQuadWireframeGeometry()
125 const float halfWidth = 0.5f;
126 const float halfHeight = 0.5f;
127 struct QuadVertex { Vector2 position;};
128 QuadVertex quadVertexData[4] =
130 { Vector2(-halfWidth, -halfHeight) },
131 { Vector2( halfWidth, -halfHeight) },
132 { Vector2( halfWidth, halfHeight) },
133 { Vector2(-halfWidth, halfHeight) }
136 Property::Map quadVertexFormat;
137 quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
138 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
139 quadVertices.SetData( quadVertexData, 4 );
142 unsigned short indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 0 };
144 // Create the geometry object
145 Geometry geometry = Geometry::New();
146 geometry.AddVertexBuffer( quadVertices );
147 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
148 geometry.SetType( Geometry::LINES );
153 } // namespace Internal
155 } // namespace Toolkit