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
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "morphgeometry.h"
#include <qmath.h>
//! [vertex struct]
struct Vertex {
QVector3D position;
QVector3D normal;
QVector4D color;
};
//! [vertex struct]
//! [constructor]
MorphGeometry::MorphGeometry(QQuick3DObject *parent)
: QQuick3DGeometry(parent)
{
updateData();
}
//! [constructor]
//! [property]
void MorphGeometry::setGridSize(int gridSize)
{
if (m_gridSize == gridSize)
return;
m_gridSize = gridSize;
emit gridSizeChanged();
updateData();
update();
}
//! [property]
//! [updateData]
void MorphGeometry::updateData()
{
clear();
calculateGeometry();
addAttribute(QQuick3DGeometry::Attribute::PositionSemantic, 0,
QQuick3DGeometry::Attribute::ComponentType::F32Type);
addAttribute(QQuick3DGeometry::Attribute::NormalSemantic, 3 * sizeof(float),
QQuick3DGeometry::Attribute::ComponentType::F32Type);
addAttribute(QQuick3DGeometry::Attribute::ColorSemantic, 6 * sizeof(float),
QQuick3DGeometry::Attribute::ComponentType::F32Type);
addTargetAttribute(0, QQuick3DGeometry::Attribute::PositionSemantic, 0);
addTargetAttribute(0, QQuick3DGeometry::Attribute::NormalSemantic, m_targetPositions.size() * sizeof(float) * 3);
addTargetAttribute(0, QQuick3DGeometry::Attribute::ColorSemantic,
m_targetPositions.size() * sizeof(float) * 3 + m_targetNormals.size() * sizeof(float) * 3);
addAttribute(QQuick3DGeometry::Attribute::IndexSemantic, 0,
QQuick3DGeometry::Attribute::ComponentType::U32Type);
const int numVertexes = m_positions.size();
m_vertexBuffer.resize(numVertexes * sizeof(Vertex));
Vertex *vert = reinterpret_cast<Vertex *>(m_vertexBuffer.data());
for (int i = 0; i < numVertexes; ++i) {
Vertex &v = vert[i];
v.position = m_positions[i];
v.normal = m_normals[i];
v.color = m_colors[i];
}
m_targetBuffer.append(QByteArray(reinterpret_cast<char *>(m_targetPositions.data()), m_targetPositions.size() * sizeof(QVector3D)));
m_targetBuffer.append(QByteArray(reinterpret_cast<char *>(m_targetNormals.data()), m_targetNormals.size() * sizeof(QVector3D)));
m_targetBuffer.append(QByteArray(reinterpret_cast<char *>(m_targetColors.data()), m_targetColors.size() * sizeof(QVector4D)));
setStride(sizeof(Vertex));
setVertexData(m_vertexBuffer);
setTargetData(m_targetBuffer);
setPrimitiveType(QQuick3DGeometry::PrimitiveType::Triangles);
setBounds(boundsMin, boundsMax);
m_indexBuffer = QByteArray(reinterpret_cast<char *>(m_indexes.data()), m_indexes.size() * sizeof(quint32));
setIndexData(m_indexBuffer);
}
//! [updateData]
void MorphGeometry::calculateGeometry()
{
float dw = 10.0; // width/2
float dh = 10.0;
int iw = m_gridSize;
int ih = m_gridSize;
float wf = dw * 2 / iw; //factor grid coord => position
float hf = dh * 2 / ih;
m_positions.clear();
m_indexes.clear();
m_targetPositions.clear();
m_targetNormals.clear();
m_targetColors.clear();
constexpr float maxFloat = std::numeric_limits<float>::max();
boundsMin = QVector3D(maxFloat, maxFloat, maxFloat);
boundsMax = QVector3D(-maxFloat, -maxFloat, -maxFloat);
// We construct a rectangular grid of iw times ih vertices;
// ix and iy are indices into the grid. x, y, and z are the spatial
// coordinates we calculate for each vertex. tx, ty, and tz are the
// coordinates for the morph target.
for (int iy = 0; iy < ih; ++iy) {
for (int ix = 0; ix < iw; ++ix) {
float x = ix * wf - dw;
float z = iy * hf - dh;
// The base shape is a cosine wave, and the corresponding normal
// vectors are calculated from the partial derivatives.
float y = 2 * qCos(x) + 3.0;
QVector3D normal{2 * qSin(x), 2, 0};
float tx = x * 1.2;
float tz = z * 1.2;
constexpr float R = 16;
QVector3D targetPosition;
QVector3D targetNormal;
// The morph target shape is a hemisphere. Therefore we don't have
// to do complex math to calculate the normal vector, since all vectors
// from the center are normal to the surface of a sphere.
if (tx*tx + tz*tz < R*R) {
float ty = 0.4f * qSqrt(R*R - tx*tx - tz*tz);
targetPosition = {tx, ty, tz};
targetNormal = targetPosition;
} else {
targetPosition = {tx, -3, tz};
targetNormal = {0, 1, 0};
}
// Finally, we make the outside edges of the shapes vertical, so they look nicer.
if (ix == 0 || iy == 0 || ix == iw-1 || iy == ih-1) {
int iix = qMin(iw-2, qMax(1, ix));
int iiy = qMin(ih-2, qMax(1, iy));
x = iix * wf - dw;
z = iiy * hf - dh;
y = -3.0;
targetPosition.setY(-3);
}
if (iy >= ih-2)
normal = {0, 0, 1};
else if (iy <= 1)
normal = {0, 0, -1};
m_positions.append({x, y, z});
m_normals.append(normal.normalized());
m_targetPositions.append(targetPosition);
m_targetNormals.append(targetNormal.normalized());
// Set custom colors for the vertices:
const float tmp = (y + 5.0f) / 10.0f;
m_colors.append({1.0, tmp, tmp, 1.0f});
const float ttmp = (ix % (iw / 8)) < 3 ? 0.5 : 1.0;
m_targetColors.append({ttmp, ttmp, ttmp, 1.0f});
// Note: We only use the bounds of the target positions since they are strictly
// bigger than the original
boundsMin.setX(std::min(boundsMin.x(), targetPosition.x()));
boundsMin.setY(std::min(boundsMin.y(), targetPosition.y()));
boundsMin.setZ(std::min(boundsMin.z(), targetPosition.z()));
boundsMax.setX(std::max(boundsMax.x(), targetPosition.x()));
boundsMax.setY(std::max(boundsMax.y(), targetPosition.y()));
boundsMax.setZ(std::max(boundsMax.z(), targetPosition.z()));
}
}
for (int ix = 0; ix < iw - 1; ++ix) {
for (int iy = 0; iy < ih - 1; ++iy) {
int idx = ix + iy * ih;
m_indexes << idx << idx + iw << idx + iw + 1
<< idx << idx + iw + 1 << idx + 1;
}
}
}
|