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
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "examplegeometry.h"
#include <QRandomGenerator>
#include <QVector3D>
ExampleTriangleGeometry::ExampleTriangleGeometry()
{
updateData();
}
void ExampleTriangleGeometry::setNormals(bool enable)
{
if (m_hasNormals == enable)
return;
m_hasNormals = enable;
emit normalsChanged();
updateData();
update();
}
void ExampleTriangleGeometry::setNormalXY(float xy)
{
if (m_normalXY == xy)
return;
m_normalXY = xy;
emit normalXYChanged();
updateData();
update();
}
void ExampleTriangleGeometry::setUV(bool enable)
{
if (m_hasUV == enable)
return;
m_hasUV = enable;
emit uvChanged();
updateData();
update();
}
void ExampleTriangleGeometry::setUVAdjust(float f)
{
if (m_uvAdjust == f)
return;
m_uvAdjust = f;
emit uvAdjustChanged();
updateData();
update();
}
//! [update data]
void ExampleTriangleGeometry::updateData()
{
clear();
int stride = 3 * sizeof(float);
if (m_hasNormals)
stride += 3 * sizeof(float);
if (m_hasUV)
stride += 2 * sizeof(float);
QByteArray vertexData(3 * stride, Qt::Initialization::Uninitialized);
float *p = reinterpret_cast<float *>(vertexData.data());
// a triangle, front face = counter-clockwise
*p++ = -1.0f; *p++ = -1.0f; *p++ = 0.0f;
if (m_hasNormals) {
*p++ = m_normalXY; *p++ = m_normalXY; *p++ = 1.0f;
}
if (m_hasUV) {
*p++ = 0.0f + m_uvAdjust; *p++ = 0.0f + m_uvAdjust;
}
*p++ = 1.0f; *p++ = -1.0f; *p++ = 0.0f;
if (m_hasNormals) {
*p++ = m_normalXY; *p++ = m_normalXY; *p++ = 1.0f;
}
if (m_hasUV) {
*p++ = 1.0f - m_uvAdjust; *p++ = 0.0f + m_uvAdjust;
}
*p++ = 0.0f; *p++ = 1.0f; *p++ = 0.0f;
if (m_hasNormals) {
*p++ = m_normalXY; *p++ = m_normalXY; *p++ = 1.0f;
}
if (m_hasUV) {
*p++ = 1.0f - m_uvAdjust; *p++ = 1.0f - m_uvAdjust;
}
setVertexData(vertexData);
setStride(stride);
setBounds(QVector3D(-1.0f, -1.0f, 0.0f), QVector3D(+1.0f, +1.0f, 0.0f));
setPrimitiveType(QQuick3DGeometry::PrimitiveType::Triangles);
addAttribute(QQuick3DGeometry::Attribute::PositionSemantic,
0,
QQuick3DGeometry::Attribute::F32Type);
if (m_hasNormals) {
addAttribute(QQuick3DGeometry::Attribute::NormalSemantic,
3 * sizeof(float),
QQuick3DGeometry::Attribute::F32Type);
}
if (m_hasUV) {
addAttribute(QQuick3DGeometry::Attribute::TexCoordSemantic,
m_hasNormals ? 6 * sizeof(float) : 3 * sizeof(float),
QQuick3DGeometry::Attribute::F32Type);
}
}
//! [update data]
ExamplePointGeometry::ExamplePointGeometry()
{
updateData();
}
void ExamplePointGeometry::updateData()
{
clear();
constexpr auto randomFloat = [](const float lowest, const float highest) -> float {
return lowest + QRandomGenerator::global()->generateDouble() * (highest - lowest);
};
constexpr int NUM_POINTS = 2000;
constexpr int stride = 3 * sizeof(float);
QByteArray vertexData;
vertexData.resize(NUM_POINTS * stride);
float *p = reinterpret_cast<float *>(vertexData.data());
for (int i = 0; i < NUM_POINTS; ++i) {
*p++ = randomFloat(-5.0f, +5.0f);
*p++ = randomFloat(-5.0f, +5.0f);
*p++ = 0.0f;
}
setVertexData(vertexData);
setStride(stride);
setBounds(QVector3D(-5.0f, -5.0f, 0.0f), QVector3D(+5.0f, +5.0f, 0.0f));
setPrimitiveType(QQuick3DGeometry::PrimitiveType::Points);
addAttribute(QQuick3DGeometry::Attribute::PositionSemantic,
0,
QQuick3DGeometry::Attribute::F32Type);
}
|