2 * Copyright (c) 2021 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>
26 Geometry CreateGridGeometry(Uint16Pair gridSize)
28 uint16_t gridWidth = gridSize.GetWidth();
29 uint16_t gridHeight = gridSize.GetHeight();
32 Vector<Vector2> vertices;
33 vertices.Reserve((gridWidth + 1) * (gridHeight + 1));
35 for(int y = 0; y < gridHeight + 1; ++y)
37 for(int x = 0; x < gridWidth + 1; ++x)
39 vertices.PushBack(Vector2((float)x / gridWidth - 0.5f, (float)y / gridHeight - 0.5f));
44 Vector<unsigned short> indices;
45 indices.Reserve((gridWidth + 2) * gridHeight * 2 - 2);
47 for(unsigned int row = 0u; row < gridHeight; ++row)
49 unsigned int rowStartIndex = row * (gridWidth + 1u);
50 unsigned int nextRowStartIndex = rowStartIndex + gridWidth + 1u;
52 if(row != 0u) // degenerate index on non-first row
54 indices.PushBack(rowStartIndex);
57 for(unsigned int column = 0u; column < gridWidth + 1u; column++) // main strip
59 indices.PushBack(rowStartIndex + column);
60 indices.PushBack(nextRowStartIndex + column);
63 if(row != gridHeight - 1u) // degenerate index on non-last row
65 indices.PushBack(nextRowStartIndex + gridWidth);
69 Property::Map vertexFormat;
70 vertexFormat["aPosition"] = Property::VECTOR2;
71 VertexBuffer vertexBuffer = VertexBuffer::New(vertexFormat);
72 if(vertices.Size() > 0)
74 vertexBuffer.SetData(&vertices[0], vertices.Size());
77 // Create the geometry object
78 Geometry geometry = Geometry::New();
79 geometry.AddVertexBuffer(vertexBuffer);
80 if(indices.Size() > 0)
82 geometry.SetIndexBuffer(&indices[0], indices.Size());
85 geometry.SetType(Geometry::TRIANGLE_STRIP);
90 Dali::Renderer CreateRenderer(std::string_view vertexSrc, std::string_view fragmentSrc)
92 Dali::Shader shader = Dali::Shader::New(vertexSrc, fragmentSrc);
94 Dali::Geometry texturedQuadGeometry = Dali::Geometry::New();
98 Dali::Vector2 position;
102 Dali::Vector2 texture;
105 VertexPosition positionArray[] =
107 {Dali::Vector2(-0.5f, -0.5f)},
108 {Dali::Vector2(0.5f, -0.5f)},
109 {Dali::Vector2(-0.5f, 0.5f)},
110 {Dali::Vector2(0.5f, 0.5f)}};
111 uint32_t numberOfVertices = sizeof(positionArray) / sizeof(VertexPosition);
113 Dali::Property::Map positionVertexFormat;
114 positionVertexFormat["aPosition"] = Dali::Property::VECTOR2;
115 Dali::VertexBuffer positionVertices = Dali::VertexBuffer::New(positionVertexFormat);
116 positionVertices.SetData(positionArray, numberOfVertices);
117 texturedQuadGeometry.AddVertexBuffer(positionVertices);
119 const uint16_t indices[] = {0, 3, 1, 0, 2, 3};
120 texturedQuadGeometry.SetIndexBuffer(&indices[0], sizeof(indices) / sizeof(indices[0]));
122 Dali::Renderer renderer = Dali::Renderer::New(texturedQuadGeometry, shader);
124 Dali::TextureSet textureSet = Dali::TextureSet::New();
125 renderer.SetTextures(textureSet);
130 Dali::Renderer CreateRenderer(std::string_view vertexSrc, std::string_view fragmentSrc, Dali::Shader::Hint::Value hints, Uint16Pair gridSize)
132 Dali::Shader shader = Dali::Shader::New(vertexSrc, fragmentSrc, hints);
134 Dali::Geometry gridGeometry = CreateGridGeometry(gridSize);
136 Dali::Renderer renderer = Dali::Renderer::New(gridGeometry, shader);
138 Dali::TextureSet textureSet = Dali::TextureSet::New();
139 renderer.SetTextures(textureSet);
144 void SetRendererTexture(Dali::Renderer renderer, Dali::Texture texture)
148 Dali::TextureSet textureSet = renderer.GetTextures();
149 textureSet.SetTexture(0u, texture);
153 void SetRendererTexture(Dali::Renderer renderer, Dali::FrameBuffer frameBuffer)
157 Dali::Texture texture = frameBuffer.GetColorTexture();
158 SetRendererTexture(renderer, texture);
162 } // namespace Internal
163 } // namespace Toolkit