56 return m_heightMapSource;
61 if (m_heightMapSource == newSource)
63 m_heightMapSource = newSource;
73 return m_smoothShading;
78 if (m_smoothShading == smooth)
80 m_smoothShading = smooth;
95 m_extentsSetExplicitly =
true;
96 if (m_extents == newExtents)
98 m_extents = newExtents;
112void HeightFieldGeometry::updateData()
123 QImage heightMap(qmlSource);
124 int numRows = heightMap.
height();
125 int numCols = heightMap.
width();
127 if (numRows < 2 || numCols < 2)
130 const int numVertices = numRows * numCols;
132 if (!m_extentsSetExplicitly) {
133 auto prevExt = m_extents;
134 if (numRows == numCols) {
135 m_extents = {100, 100, 100};
136 }
else if (numRows < numCols) {
137 float f = float(numRows) / float(numCols);
138 m_extents = {100.f, 100.f, 100.f *
f};
140 float f = float(numCols) / float(numRows);
141 m_extents = {100.f *
f, 100.f, 100.f};
143 if (m_extents != prevExt) {
151 const float rowF = m_extents.
z() / (numRows - 1);
152 const float rowOffs = -m_extents.
z() / 2;
153 const float colF = m_extents.
x() / (numCols - 1);
154 const float colOffs = -m_extents.
x() / 2;
155 for (
int x = 0;
x < numCols;
x++) {
156 for (
int y = 0;
y < numRows;
y++) {
161 vertex.
uv =
QVector2D(
float(
x) / (numCols - 1), 1.f -
float(
y) / (numRows - 1));
167 for (
int ix = 0; ix < numCols - 1; ++ix) {
168 for (
int iy = 0; iy < numRows - 1; ++iy) {
169 const int idx = iy + ix * numRows;
171 const auto tri0 = std::array<int, 3> { idx + numRows + 1, idx + numRows, idx };
172 const auto tri1 = std::array<int, 3> { idx + 1, idx + numRows + 1, idx };
174 for (
const auto [i0, i1, i2] : { tri0, tri1 }) {
179 if (m_smoothShading) {
181 const QVector3D e0 = vertices[i1].position - vertices[i0].position;
182 const QVector3D e1 = vertices[i2].position - vertices[i0].position;
186 vertices[i0].normal += normal;
187 vertices[i1].normal += normal;
188 vertices[i2].normal += normal;
194 if (m_smoothShading) {
196 for (
auto &vertex : vertices)
204 for (
const auto &vertex : vertices) {
void setSource(const QUrl &newSource)
void setExtents(const QVector3D &newExtents)
void smoothShadingChanged()
HeightFieldGeometry()
\qmltype HeightFieldGeometry \inqmlmodule QtQuick3D.Helpers \inherits Geometry
void setSmoothShading(bool smooth)
float valueF() const noexcept
Returns the value color component of this color.
int width() const
Returns the width of the image.
int height() const
Returns the height of the image.
QColor pixelColor(int x, int y) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
qsizetype size() const noexcept
void push_back(parameter_type t)
void reserve(qsizetype size)
The QQmlContext class defines a context within a QML engine.
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to QFile.
void setPrimitiveType(PrimitiveType type)
Sets the primitive type used for rendering to type.
void setStride(int stride)
Sets the stride of the vertex buffer to stride, measured in bytes.
void addAttribute(Attribute::Semantic semantic, int offset, Attribute::ComponentType componentType)
Adds vertex attribute description.
void setVertexData(const QByteArray &data)
Sets the vertex buffer data.
int const QVector3D const QVector3D & boundsMax
Returns the maximum coordinate of the bounding volume.
int const QVector3D & boundsMin
Returns the minimum coordinate of the bounding volume.
void clear()
Resets the geometry to its initial state, clearing previously set vertex and index data as well as at...
void setBounds(const QVector3D &min, const QVector3D &max)
Sets the bounding volume of the geometry to the cube defined by the points min and max.
void setIndexData(const QByteArray &data)
Sets the index buffer to data.
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
The QVector2D class represents a vector or vertex in 2D space.
The QVector3D class represents a vector or vertex in 3D space.
QVector3D normalized() const noexcept
Returns the normalized unit vector form of this vector.
constexpr float y() const noexcept
Returns the y coordinate of this point.
constexpr float x() const noexcept
Returns the x coordinate of this point.
static constexpr QVector3D crossProduct(QVector3D v1, QVector3D v2) noexcept
Returns the cross-product of vectors v1 and v2, which is normal to the plane spanned by v1 and v2.
constexpr float z() const noexcept
Returns the z coordinate of this point.
constexpr const T & qMin(const T &a, const T &b)
constexpr const T & qMax(const T &a, const T &b)
GLint GLint GLint GLint GLint x
[0]
GLsizei GLenum const void * indices
static void normalize(double &x, double &y)
QQmlContext * qmlContext(const QObject *obj)
static QUrl resolvedUrl(const QUrl &url, const QQmlRefPointer< QQmlContextData > &context)