[dali_1.2.10] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / wireframe / wireframe-visual.cpp
1 /*
2  * Copyright (c) 2016 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
19 // CLASS HEADER
20 #include "wireframe-visual.h"
21
22 // INTERNAL INCLUDES
23 #include <dali-toolkit/public-api/visuals/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>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
41 const char * const INDEX_NAME("indices");
42
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
47 \n
48 void main()\n
49 {\n
50   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
51   vertexPosition.xyz *= uSize;\n
52   gl_Position = uMvpMatrix * vertexPosition;\n
53 }\n
54 );
55
56 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(\n
57 uniform lowp vec4 uColor;\n
58 \n
59 void main()\n
60 {\n
61   gl_FragColor = uColor;\n
62 }\n
63 );
64
65 }
66
67
68 WireframeVisual::WireframeVisual( VisualFactoryCache& factoryCache )
69 : Visual::Base( factoryCache )
70 {
71 }
72
73 WireframeVisual::~WireframeVisual()
74 {}
75
76 void WireframeVisual::DoSetOnStage( Actor& actor )
77 {
78   InitializeRenderer();
79
80   actor.AddRenderer( mImpl->mRenderer );
81 }
82
83 void WireframeVisual::DoCreatePropertyMap( Property::Map& map ) const
84 {
85   map.Clear();
86   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::WIREFRAME );
87 }
88
89 void WireframeVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
90 {
91   // TODO
92 }
93
94 Dali::Property::Value WireframeVisual::DoGetProperty( Dali::Property::Index index )
95 {
96   // TODO
97   return Dali::Property::Value();
98 }
99
100 void WireframeVisual::InitializeRenderer()
101 {
102   mImpl->mRenderer = mFactoryCache.GetWireframeRenderer();
103   if( !mImpl->mRenderer )
104   {
105     Geometry geometry = CreateQuadWireframeGeometry();
106     Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
107
108     mImpl->mRenderer = Renderer::New( geometry, shader);
109     mFactoryCache.CacheWireframeRenderer( mImpl->mRenderer );
110   }
111 }
112
113 Geometry WireframeVisual::CreateQuadWireframeGeometry()
114 {
115   const float halfWidth = 0.5f;
116   const float halfHeight = 0.5f;
117   struct QuadVertex { Vector2 position;};
118   QuadVertex quadVertexData[4] =
119   {
120       { Vector2(-halfWidth, -halfHeight) },
121       { Vector2( halfWidth, -halfHeight) },
122       { Vector2( halfWidth,  halfHeight) },
123       { Vector2(-halfWidth,  halfHeight) }
124   };
125
126   Property::Map quadVertexFormat;
127   quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
128   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
129   quadVertices.SetData( quadVertexData, 4 );
130
131   // Create indices
132   unsigned short indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 0 };
133
134   // Create the geometry object
135   Geometry geometry = Geometry::New();
136   geometry.AddVertexBuffer( quadVertices );
137   geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
138   geometry.SetType( Geometry::LINES );
139
140   return geometry;
141 }
142
143 } // namespace Internal
144
145 } // namespace Toolkit
146
147 } // namespace Dali