| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <QtTest/QTest> |
| 30 | #include <Qt3DRender/private/uniform_p.h> |
| 31 | |
| 32 | using namespace Qt3DRender; |
| 33 | using namespace Qt3DRender::Render; |
| 34 | |
| 35 | class tst_Uniform : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | private Q_SLOTS: |
| 39 | |
| 40 | void checkInitialState() |
| 41 | { |
| 42 | // GIVEN |
| 43 | UniformValue v; |
| 44 | // THEN |
| 45 | QVERIFY(v.constData<float>()[0] == 0.0f); |
| 46 | QVERIFY(v.constData<float>()[1] == 0.0f); |
| 47 | QVERIFY(v.constData<float>()[2] == 0.0f); |
| 48 | QVERIFY(v.constData<float>()[3] == 0.0f); |
| 49 | } |
| 50 | |
| 51 | void checkDefaultCTors() |
| 52 | { |
| 53 | { |
| 54 | // GIVEN |
| 55 | UniformValue v(883); |
| 56 | // THEN |
| 57 | QCOMPARE(v.constData<int>()[0], 883); |
| 58 | QCOMPARE(v.constData<int>()[1], 0); |
| 59 | QCOMPARE(v.constData<int>()[2], 0); |
| 60 | QCOMPARE(v.constData<int>()[3], 0); |
| 61 | } |
| 62 | { |
| 63 | // GIVEN |
| 64 | UniformValue v(1584U); |
| 65 | // THEN |
| 66 | QCOMPARE(v.constData<uint>()[0], 1584U); |
| 67 | QCOMPARE(v.constData<uint>()[1], 0U); |
| 68 | QCOMPARE(v.constData<uint>()[2], 0U); |
| 69 | QCOMPARE(v.constData<uint>()[3], 0U); |
| 70 | } |
| 71 | { |
| 72 | // GIVEN |
| 73 | UniformValue v(454.0f); |
| 74 | // THEN |
| 75 | QCOMPARE(v.constData<float>()[0], 454.0f); |
| 76 | QCOMPARE(v.constData<float>()[1], 0.0f); |
| 77 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 78 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 79 | } |
| 80 | { |
| 81 | // GIVEN |
| 82 | UniformValue v(350.0); |
| 83 | // THEN |
| 84 | // Note: Uniform value does a double -> float conversion |
| 85 | QCOMPARE(v.constData<float>()[0], 350.0f); |
| 86 | QCOMPARE(v.constData<float>()[1], 0.0f); |
| 87 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 88 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 89 | } |
| 90 | { |
| 91 | // GIVEN |
| 92 | UniformValue v(true); |
| 93 | // THEN |
| 94 | QCOMPARE(v.constData<bool>()[0], true); |
| 95 | QCOMPARE(v.constData<bool>()[1], false); |
| 96 | QCOMPARE(v.constData<bool>()[2], false); |
| 97 | QCOMPARE(v.constData<bool>()[3], false); |
| 98 | } |
| 99 | { |
| 100 | // GIVEN |
| 101 | UniformValue v(QVector2D(355.0f, 383.0f)); |
| 102 | // THEN |
| 103 | QCOMPARE(v.constData<float>()[0], 355.0f); |
| 104 | QCOMPARE(v.constData<float>()[1], 383.0f); |
| 105 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 106 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 107 | } |
| 108 | { |
| 109 | // GIVEN |
| 110 | UniformValue v(Vector3D(572.0f, 355.0f, 383.0f)); |
| 111 | // THEN |
| 112 | QCOMPARE(v.constData<float>()[0], 572.0f); |
| 113 | QCOMPARE(v.constData<float>()[1], 355.0f); |
| 114 | QCOMPARE(v.constData<float>()[2], 383.0f); |
| 115 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 116 | } |
| 117 | { |
| 118 | // GIVEN |
| 119 | UniformValue v(Vector4D(355.0f, 383.0f, 1340.0f, 1603.0f)); |
| 120 | // THEN |
| 121 | QCOMPARE(v.constData<float>()[0], 355.0f); |
| 122 | QCOMPARE(v.constData<float>()[1], 383.0f); |
| 123 | QCOMPARE(v.constData<float>()[2], 1340.0f); |
| 124 | QCOMPARE(v.constData<float>()[3], 1603.0f); |
| 125 | } |
| 126 | { |
| 127 | // GIVEN |
| 128 | const QMatrix4x4 m1; |
| 129 | QMatrix4x4 m2; |
| 130 | m2.rotate(angle: 90.0f, x: 1.0f, y: 0.0f, z: 0.0f); |
| 131 | QMatrix4x4 m3; |
| 132 | m3.scale(factor: 2.5f); |
| 133 | QMatrix4x4 m4; |
| 134 | m4.translate(x: 1.0f, y: 2.0f, z: 3.0f); |
| 135 | |
| 136 | const QVector<QMatrix4x4> matrices = (QVector<QMatrix4x4>() << m1 << m2 << m3 << m4); |
| 137 | UniformValue v(matrices); |
| 138 | |
| 139 | // THEN |
| 140 | for (int j = 0; j < matrices.size(); ++j) { |
| 141 | for (int i = 0; i < 16; ++i) { |
| 142 | QCOMPARE(v.constData<float>()[16 * j + i], matrices[j].constData()[i]); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | { |
| 147 | // GIVEN |
| 148 | const Qt3DCore::QNodeId nodeId = Qt3DCore::QNodeId::createId(); |
| 149 | UniformValue v(nodeId); |
| 150 | |
| 151 | // THEN |
| 152 | QCOMPARE(uint(v.byteSize()), sizeof(Qt3DCore::QNodeId)); |
| 153 | QCOMPARE(v.constData<Qt3DCore::QNodeId>()[0], nodeId); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void checkFromVariant() |
| 158 | { |
| 159 | { |
| 160 | // GIVEN |
| 161 | UniformValue v = UniformValue::fromVariant(variant: QVariant(883)); |
| 162 | // THEN |
| 163 | QCOMPARE(v.constData<int>()[0], 883); |
| 164 | QCOMPARE(v.constData<int>()[1], 0); |
| 165 | QCOMPARE(v.constData<int>()[2], 0); |
| 166 | QCOMPARE(v.constData<int>()[3], 0); |
| 167 | } |
| 168 | { |
| 169 | // GIVEN |
| 170 | UniformValue v = UniformValue::fromVariant(variant: QVariant(1584U)); |
| 171 | // THEN |
| 172 | QCOMPARE(v.constData<uint>()[0], 1584U); |
| 173 | QCOMPARE(v.constData<uint>()[1], 0U); |
| 174 | QCOMPARE(v.constData<uint>()[2], 0U); |
| 175 | QCOMPARE(v.constData<uint>()[3], 0U); |
| 176 | } |
| 177 | { |
| 178 | // GIVEN |
| 179 | UniformValue v = UniformValue::fromVariant(variant: QVariant(454.0f)); |
| 180 | // THEN |
| 181 | QCOMPARE(v.constData<float>()[0], 454.0f); |
| 182 | QCOMPARE(v.constData<float>()[1], 0.0f); |
| 183 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 184 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 185 | } |
| 186 | { |
| 187 | // GIVEN |
| 188 | UniformValue v = UniformValue::fromVariant(variant: QVariant(350.0)); |
| 189 | // THEN |
| 190 | // Note: Uniform value does a double -> float conversion |
| 191 | QCOMPARE(v.constData<float>()[0], 350.0f); |
| 192 | QCOMPARE(v.constData<float>()[1], 0.0f); |
| 193 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 194 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 195 | } |
| 196 | { |
| 197 | // GIVEN |
| 198 | UniformValue v = UniformValue::fromVariant(variant: QVariant(true)); |
| 199 | // THEN |
| 200 | QCOMPARE(v.constData<bool>()[0], true); |
| 201 | QCOMPARE(v.constData<bool>()[1], false); |
| 202 | QCOMPARE(v.constData<bool>()[2], false); |
| 203 | QCOMPARE(v.constData<bool>()[3], false); |
| 204 | } |
| 205 | { |
| 206 | // GIVEN |
| 207 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QVector2D(355.0f, 383.0f))); |
| 208 | // THEN |
| 209 | QCOMPARE(v.constData<float>()[0], 355.0f); |
| 210 | QCOMPARE(v.constData<float>()[1], 383.0f); |
| 211 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 212 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 213 | } |
| 214 | { |
| 215 | // GIVEN |
| 216 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QVector3D(572.0f, 355.0f, 383.0f))); |
| 217 | // THEN |
| 218 | QCOMPARE(v.constData<float>()[0], 572.0f); |
| 219 | QCOMPARE(v.constData<float>()[1], 355.0f); |
| 220 | QCOMPARE(v.constData<float>()[2], 383.0f); |
| 221 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 222 | } |
| 223 | { |
| 224 | // GIVEN |
| 225 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QVector4D(355.0f, 383.0f, 1340.0f, 1603.0f))); |
| 226 | // THEN |
| 227 | QCOMPARE(v.constData<float>()[0], 355.0f); |
| 228 | QCOMPARE(v.constData<float>()[1], 383.0f); |
| 229 | QCOMPARE(v.constData<float>()[2], 1340.0f); |
| 230 | QCOMPARE(v.constData<float>()[3], 1603.0f); |
| 231 | } |
| 232 | { |
| 233 | // GIVEN |
| 234 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QPoint(427, 396))); |
| 235 | // THEN |
| 236 | QCOMPARE(v.constData<int>()[0], 427); |
| 237 | QCOMPARE(v.constData<int>()[1], 396); |
| 238 | QCOMPARE(v.constData<int>()[2], 0); |
| 239 | QCOMPARE(v.constData<int>()[3], 0); |
| 240 | } |
| 241 | { |
| 242 | // GIVEN |
| 243 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QSize(427, 396))); |
| 244 | // THEN |
| 245 | QCOMPARE(v.constData<int>()[0], 427); |
| 246 | QCOMPARE(v.constData<int>()[1], 396); |
| 247 | QCOMPARE(v.constData<int>()[2], 0); |
| 248 | QCOMPARE(v.constData<int>()[3], 0); |
| 249 | } |
| 250 | { |
| 251 | // GIVEN |
| 252 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QRect(427, 396, 454, 1584))); |
| 253 | // THEN |
| 254 | QCOMPARE(v.constData<int>()[0], 427); |
| 255 | QCOMPARE(v.constData<int>()[1], 396); |
| 256 | QCOMPARE(v.constData<int>()[2], 454); |
| 257 | QCOMPARE(v.constData<int>()[3], 1584); |
| 258 | } |
| 259 | { |
| 260 | // GIVEN |
| 261 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QPointF(427, 396))); |
| 262 | // THEN |
| 263 | QCOMPARE(v.constData<float>()[0], 427.0f); |
| 264 | QCOMPARE(v.constData<float>()[1], 396.0f); |
| 265 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 266 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 267 | } |
| 268 | { |
| 269 | // GIVEN |
| 270 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QSizeF(427, 396))); |
| 271 | // THEN |
| 272 | QCOMPARE(v.constData<float>()[0], 427.0f); |
| 273 | QCOMPARE(v.constData<float>()[1], 396.0f); |
| 274 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 275 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 276 | } |
| 277 | { |
| 278 | // GIVEN |
| 279 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QRectF(427, 396, 454, 1584))); |
| 280 | // THEN |
| 281 | QCOMPARE(v.constData<float>()[0], 427.0f); |
| 282 | QCOMPARE(v.constData<float>()[1], 396.0f); |
| 283 | QCOMPARE(v.constData<float>()[2], 454.0f); |
| 284 | QCOMPARE(v.constData<float>()[3], 1584.0f); |
| 285 | } |
| 286 | { |
| 287 | // GIVEN |
| 288 | UniformValue v = UniformValue::fromVariant(variant: QVariant::fromValue(value: QMatrix4x4())); |
| 289 | // THEN |
| 290 | QCOMPARE(v.constData<float>()[ 0], 1.0f); |
| 291 | QCOMPARE(v.constData<float>()[ 1], 0.0f); |
| 292 | QCOMPARE(v.constData<float>()[ 2], 0.0f); |
| 293 | QCOMPARE(v.constData<float>()[ 3], 0.0f); |
| 294 | |
| 295 | QCOMPARE(v.constData<float>()[ 4], 0.0f); |
| 296 | QCOMPARE(v.constData<float>()[ 5], 1.0f); |
| 297 | QCOMPARE(v.constData<float>()[ 6], 0.0f); |
| 298 | QCOMPARE(v.constData<float>()[ 7], 0.0f); |
| 299 | |
| 300 | QCOMPARE(v.constData<float>()[ 8], 0.0f); |
| 301 | QCOMPARE(v.constData<float>()[ 9], 0.0f); |
| 302 | QCOMPARE(v.constData<float>()[10], 1.0f); |
| 303 | QCOMPARE(v.constData<float>()[11], 0.0f); |
| 304 | |
| 305 | QCOMPARE(v.constData<float>()[12], 0.0f); |
| 306 | QCOMPARE(v.constData<float>()[13], 0.0f); |
| 307 | QCOMPARE(v.constData<float>()[14], 0.0f); |
| 308 | QCOMPARE(v.constData<float>()[15], 1.0f); |
| 309 | } |
| 310 | { |
| 311 | // GIVEN |
| 312 | QVariant variants = QVariantList() << QVariant(427.0f) << QVariant(454.0f) << QVariant(883.0f) << QVariant(1340.0f); |
| 313 | UniformValue v = UniformValue::fromVariant(variant: variants); |
| 314 | |
| 315 | // THEN |
| 316 | QCOMPARE(v.constData<float>()[0], 427.0f); |
| 317 | QCOMPARE(v.constData<float>()[1], 454.0f); |
| 318 | QCOMPARE(v.constData<float>()[2], 883.0f); |
| 319 | QCOMPARE(v.constData<float>()[3], 1340.0f); |
| 320 | } |
| 321 | { |
| 322 | // GIVEN |
| 323 | QVariant variants = QVariantList() << QVariant::fromValue(value: QVector4D(2.0f, 16.0f, 8.0f, 4.0f)) << QVariant(QVector4D(3.0f, 24.0f, 12.0f, 6.0f)); |
| 324 | UniformValue v = UniformValue::fromVariant(variant: variants); |
| 325 | |
| 326 | // THEN |
| 327 | QCOMPARE(v.constData<float>()[0], 2.0f); |
| 328 | QCOMPARE(v.constData<float>()[1], 16.0f); |
| 329 | QCOMPARE(v.constData<float>()[2], 8.0f); |
| 330 | QCOMPARE(v.constData<float>()[3], 4.0f); |
| 331 | QCOMPARE(v.constData<float>()[4], 3.0f); |
| 332 | QCOMPARE(v.constData<float>()[5], 24.0f); |
| 333 | QCOMPARE(v.constData<float>()[6], 12.0f); |
| 334 | QCOMPARE(v.constData<float>()[7], 6.0f); |
| 335 | } |
| 336 | { |
| 337 | // GIVEN |
| 338 | QVariant variants = QVariantList() << QVariant(427) << QVariant(454) << QVariant(883) << QVariant(1340); |
| 339 | UniformValue v = UniformValue::fromVariant(variant: variants); |
| 340 | |
| 341 | // THEN |
| 342 | QCOMPARE(v.constData<int>()[0], 427); |
| 343 | QCOMPARE(v.constData<int>()[1], 454); |
| 344 | QCOMPARE(v.constData<int>()[2], 883); |
| 345 | QCOMPARE(v.constData<int>()[3], 1340); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void checkComparison() |
| 350 | { |
| 351 | // GIVEN |
| 352 | const UniformValue v1(Vector3D(454.0f, 883.0f, 572.0f)); |
| 353 | UniformValue v2(454.0f); |
| 354 | |
| 355 | // THEN |
| 356 | QVERIFY(!(v1 == v2)); |
| 357 | QVERIFY(v1 != v2); |
| 358 | |
| 359 | // WHEN |
| 360 | v2 = UniformValue::fromVariant(variant: QVariant::fromValue(value: Vector3D(454.0f, 883.0f, 572.0f))); |
| 361 | // THEN |
| 362 | QVERIFY(v1 == v2); |
| 363 | QVERIFY(!(v1 != v2)); |
| 364 | |
| 365 | // WHEN |
| 366 | v2 = UniformValue::fromVariant(variant: QVariant::fromValue(value: Vector3D(454.0f, 883.0f, 572.0f))); |
| 367 | // THEN |
| 368 | QVERIFY(v1 == v2); |
| 369 | QVERIFY(!(v1 != v2)); |
| 370 | |
| 371 | // WHEN |
| 372 | v2 = UniformValue::fromVariant(variant: 454.0f); |
| 373 | // THEN |
| 374 | QVERIFY(!(v1 == v2)); |
| 375 | QVERIFY(v1 != v2); |
| 376 | } |
| 377 | |
| 378 | void checkSetData() |
| 379 | { |
| 380 | // GIVEN |
| 381 | const QMatrix4x4 m1; |
| 382 | QMatrix4x4 m2; |
| 383 | m2.rotate(angle: 90.0f, x: 1.0f, y: 0.0f, z: 0.0f); |
| 384 | QMatrix4x4 m3; |
| 385 | m3.scale(factor: 2.5f); |
| 386 | QMatrix4x4 m4; |
| 387 | m4.translate(x: 1.0f, y: 2.0f, z: 3.0f); |
| 388 | |
| 389 | const QVector<QMatrix4x4> matrices1 = (QVector<QMatrix4x4>() << m1 << m2 << m3 << m4); |
| 390 | UniformValue v(matrices1); |
| 391 | |
| 392 | // WHEN |
| 393 | const QVector<QMatrix4x4> matrices2 = (QVector<QMatrix4x4>() << m4 << m3 << m2 << m1 << m4); |
| 394 | v.setData(matrices2); |
| 395 | |
| 396 | // THEN |
| 397 | for (int j = 0; j < matrices2.size(); ++j) { |
| 398 | for (int i = 0; i < 16; ++i) { |
| 399 | QCOMPARE(v.constData<float>()[16 * j + i], matrices2[j].constData()[i]); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // GIVEN |
| 404 | const int positionCount = 10; |
| 405 | QVector<QVector3D> positions(positionCount); |
| 406 | for (int i = 0; i < positionCount; ++i) { |
| 407 | const QVector3D p(float(i), 10.0f * i, 100.0f * i); |
| 408 | positions[i] = p; |
| 409 | } |
| 410 | |
| 411 | UniformValue positionsUniform; |
| 412 | |
| 413 | // WHEN |
| 414 | positionsUniform.setData(positions); |
| 415 | |
| 416 | // THEN |
| 417 | const QVector3D *data = positionsUniform.constData<QVector3D>(); |
| 418 | for (int i = 0; i < positionCount; ++i) { |
| 419 | QCOMPARE(*(data + i), positions[i]); |
| 420 | } |
| 421 | } |
| 422 | }; |
| 423 | |
| 424 | |
| 425 | QTEST_APPLESS_MAIN(tst_Uniform) |
| 426 | |
| 427 | #include "tst_uniform.moc" |
| 428 | |