2 * Copyright (c) 2019 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.
18 #include <dali-toolkit/internal/controls/control/control-renderers.h>
27 Geometry CreateGridGeometry( Uint16Pair gridSize )
29 uint16_t gridWidth = gridSize.GetWidth();
30 uint16_t gridHeight = gridSize.GetHeight();
33 Vector< Vector2 > vertices;
34 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
36 for( int y = 0; y < gridHeight + 1; ++y )
38 for( int x = 0; x < gridWidth + 1; ++x )
40 vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight - 0.5f) );
45 Vector< unsigned short > indices;
46 indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
48 for( unsigned int row = 0u; row < gridHeight; ++row )
50 unsigned int rowStartIndex = row*(gridWidth+1u);
51 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
53 if( row != 0u ) // degenerate index on non-first row
55 indices.PushBack( rowStartIndex );
58 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
60 indices.PushBack( rowStartIndex + column);
61 indices.PushBack( nextRowStartIndex + column);
64 if( row != gridHeight-1u ) // degenerate index on non-last row
66 indices.PushBack( nextRowStartIndex + gridWidth );
70 Property::Map vertexFormat;
71 vertexFormat[ "aPosition" ] = Property::VECTOR2;
72 VertexBuffer vertexBuffer = VertexBuffer::New( vertexFormat );
73 if( vertices.Size() > 0 )
75 vertexBuffer.SetData( &vertices[ 0 ], vertices.Size() );
78 // Create the geometry object
79 Geometry geometry = Geometry::New();
80 geometry.AddVertexBuffer( vertexBuffer );
81 if( indices.Size() > 0 )
83 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
86 geometry.SetType( Geometry::TRIANGLE_STRIP );
91 Dali::Renderer CreateRenderer( std::string_view vertexSrc, std::string_view fragmentSrc )
93 Dali::Shader shader = Dali::Shader::New( vertexSrc, fragmentSrc );
95 Dali::Geometry texturedQuadGeometry = Dali::Geometry::New();
97 struct VertexPosition { Dali::Vector2 position; };
98 struct VertexTexture { Dali::Vector2 texture; };
100 VertexPosition positionArray[] =
102 { Dali::Vector2( -0.5f, -0.5f ) },
103 { Dali::Vector2( 0.5f, -0.5f ) },
104 { Dali::Vector2( -0.5f, 0.5f ) },
105 { Dali::Vector2( 0.5f, 0.5f ) }
107 uint32_t numberOfVertices = sizeof(positionArray)/sizeof(VertexPosition);
109 Dali::Property::Map positionVertexFormat;
110 positionVertexFormat["aPosition"] = Dali::Property::VECTOR2;
111 Dali::VertexBuffer positionVertices = Dali::VertexBuffer::New( positionVertexFormat );
112 positionVertices.SetData( positionArray, numberOfVertices );
113 texturedQuadGeometry.AddVertexBuffer( positionVertices );
115 const uint16_t indices[] = { 0, 3, 1, 0, 2, 3 };
116 texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) );
118 Dali::Renderer renderer = Dali::Renderer::New( texturedQuadGeometry, shader );
120 Dali::TextureSet textureSet = Dali::TextureSet::New();
121 renderer.SetTextures( textureSet );
126 Dali::Renderer CreateRenderer( std::string_view vertexSrc, std::string_view fragmentSrc, Dali::Shader::Hint::Value hints, Uint16Pair gridSize )
128 Dali::Shader shader = Dali::Shader::New( vertexSrc, fragmentSrc, hints );
130 Dali::Geometry gridGeometry = CreateGridGeometry( gridSize );
132 Dali::Renderer renderer = Dali::Renderer::New( gridGeometry, shader );
134 Dali::TextureSet textureSet = Dali::TextureSet::New();
135 renderer.SetTextures( textureSet );
140 void SetRendererTexture( Dali::Renderer renderer, Dali::Texture texture )
144 Dali::TextureSet textureSet = renderer.GetTextures();
145 textureSet.SetTexture( 0u, texture );
149 void SetRendererTexture( Dali::Renderer renderer, Dali::FrameBuffer frameBuffer )
153 Dali::Texture texture = frameBuffer.GetColorTexture();
154 SetRendererTexture( renderer, texture );
158 } // namespace Internal
159 } // namespace Toolkit