utils.cpp
5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "utils.h"
using namespace Dali;
Vector3 ToHueSaturationLightness(Vector3 rgb)
{
float min = std::min(rgb.r, std::min(rgb.g, rgb.b));
float max = std::max(rgb.r, std::max(rgb.g, rgb.b));
Vector3 hsl(max - min, 0.f, (max + min) * .5f);
if (hsl.x * hsl.x > .0f)
{
hsl.y = hsl.x / max;
if (max == rgb.r)
{
hsl.x = (rgb.g - rgb.b) / hsl.x;
}
else if(max == rgb.g)
{
hsl.x = 2.f + (rgb.b - rgb.r) / hsl.x;
}
else
{
hsl.x = 4.f + (rgb.r - rgb.g) / hsl.x;
}
hsl.x *= 60.f;
if (hsl.x < 0.f)
{
hsl.x += 360.f;
}
}
else
{
hsl.y = 0.f;
}
return hsl;
}
Vector3 FromHueSaturationLightness(Vector3 hsl)
{
Vector3 rgb;
if (hsl.y * hsl.y > 0.f)
{
if(hsl.x >= 360.f)
{
hsl.x -= 360.f;
}
hsl.x /= 60.f;
int i = FastFloor(hsl.x);
float ff = hsl.x - i;
float p = hsl.z * (1.0 - hsl.y);
float q = hsl.z * (1.0 - (hsl.y * ff));
float t = hsl.z * (1.0 - (hsl.y * (1.f - ff)));
switch (i)
{
case 0:
rgb.r = hsl.z;
rgb.g = t;
rgb.b = p;
break;
case 1:
rgb.r = q;
rgb.g = hsl.z;
rgb.b = p;
break;
case 2:
rgb.r = p;
rgb.g = hsl.z;
rgb.b = t;
break;
case 3:
rgb.r = p;
rgb.g = q;
rgb.b = hsl.z;
break;
case 4:
rgb.r = t;
rgb.g = p;
rgb.b = hsl.z;
break;
case 5:
default:
rgb.r = hsl.z;
rgb.g = p;
rgb.b = q;
break;
}
}
else
{
rgb = Vector3::ONE * hsl.z;
}
return rgb;
}
Geometry CreateCuboidWireframeGeometry()
{
//
// 2---3
// |- |-
// | 6---7
// | | | |
// 0-|-1 |
// -| -|
// 4---5
//
Vector3 vertexData[] = {
Vector3(-.5, -.5, -.5),
Vector3( .5, -.5, -.5),
Vector3(-.5, .5, -.5),
Vector3( .5, .5, -.5),
Vector3(-.5, -.5, .5),
Vector3( .5, -.5, .5),
Vector3(-.5, .5, .5),
Vector3( .5, .5, .5),
};
uint16_t indices[] = {
0, 1, 1, 3, 3, 2, 2, 0,
0, 4, 1, 5, 3, 7, 2, 6,
4, 5, 5, 7, 7, 6, 6, 4
};
VertexBuffer vertexBuffer = VertexBuffer::New( Property::Map()
.Add( "aPosition", Property::VECTOR3));
vertexBuffer.SetData(vertexData, std::extent<decltype(vertexData)>::value );
Geometry geometry = Geometry::New();
geometry.AddVertexBuffer( vertexBuffer );
geometry.SetIndexBuffer( indices, std::extent<decltype(indices)>::value );
geometry.SetType(Geometry::LINES);
return geometry;
}
Renderer CreateRenderer(TextureSet textures, Geometry geometry, Shader shader, uint32_t options)
{
Renderer renderer = Renderer::New(geometry, shader);
renderer.SetProperty(Renderer::Property::BLEND_MODE,
(options & OPTION_BLEND) ? BlendMode::ON : BlendMode::OFF);
renderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE,
(options & OPTION_DEPTH_TEST) ? DepthTestMode::ON : DepthTestMode::OFF);
renderer.SetProperty(Renderer::Property::DEPTH_WRITE_MODE,
(options & OPTION_DEPTH_WRITE) ? DepthWriteMode::ON : DepthWriteMode::OFF);
renderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
if (!textures)
{
textures = TextureSet::New();
}
renderer.SetTextures(textures);
return renderer;
}
void CenterActor(Actor actor)
{
actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
}
Actor CreateActor()
{
auto actor = Actor::New();
CenterActor(actor);
return actor;
}
Renderer CloneRenderer(Renderer original)
{
Geometry geom = original.GetGeometry();
Shader shader = original.GetShader();
Renderer clone = Renderer::New(geom, shader);
// Copy properties.
Property::IndexContainer indices;
original.GetPropertyIndices(indices);
for (auto& i: indices)
{
auto actualIndex = PropertyRanges::DEFAULT_RENDERER_PROPERTY_START_INDEX + i;
clone.SetProperty(actualIndex, original.GetProperty(actualIndex));
}
// Copy texture references (and create TextureSet, if there's any textures).
TextureSet ts = original.GetTextures();
clone.SetTextures(ts);
return clone;
}
Actor CloneActor(Actor original)
{
using namespace Dali;
auto clone = Actor::New();
clone.SetProperty(Actor::Property::NAME, original.GetProperty(Actor::Property::NAME));
// Copy properties.
// Don't copy every single one of them.
// Definitely don't copy resize policy related things, which will internally enable
// relayout, which in turn will result in losing the ability to set Z size.
for (auto i : {
Actor::Property::PARENT_ORIGIN,
Actor::Property::ANCHOR_POINT,
Actor::Property::SIZE,
Actor::Property::POSITION,
Actor::Property::ORIENTATION,
Actor::Property::SCALE,
Actor::Property::VISIBLE,
Actor::Property::COLOR,
Actor::Property::NAME,
})
{
clone.SetProperty(i, original.GetProperty(i));
}
// Clone renderers.
for(unsigned int i = 0; i < original.GetRendererCount(); ++i)
{
auto rClone = CloneRenderer(original.GetRendererAt(i));
clone.AddRenderer(rClone);
}
// Recurse into children.
for(unsigned int i = 0; i < original.GetChildCount(); ++i)
{
Actor newChild = CloneActor(original.GetChildAt(i));
clone.Add(newChild);
}
return clone;
}