[Quick3D] Vertex coloring
Unsolved
QML and Qt Quick
-
I've created custom
QQuick3DGeometry
class. Is it possible to set color to vertex from a code to make some heights different?
Here's how it works:... for (int y = 0; y < MATRIX_SIZE; ++y) { for (int x = 0; x < MATRIX_SIZE; ++x) { QVector3D vertex(x * MATRIX_SCALE, y * MATRIX_SCALE, m_testMatrix[x][y] * MATRIX_SCALE); *p++ = vertex.x(); *p++ = vertex.y(); *p++ = vertex.z(); vertex.setY((y + 1) * MATRIX_SCALE); vertex.setZ(m_testMatrix[x][y + 1] * MATRIX_SCALE); *p++ = vertex.x(); *p++ = vertex.y(); *p++ = vertex.z(); } } setVertexData(vertexData); setPrimitiveType(QQuick3DGeometry::PrimitiveType::LineStrip); setStride(stride); addAttribute(QQuick3DGeometry::Attribute::PositionSemantic, 0, QQuick3DGeometry::Attribute::F32Type); addAttribute(QQuick3DGeometry::Attribute::ColorSemantic, 4, QQuick3DGeometry::Attribute::U32Type);
I found that I can add color attribute, but I don't understand how to add color data to vertex. If it possible to make this without shaders, will be awesome
-
Just like in official tutorial with custom point cloud
#include "heightmapgeometry.h" HeightMapGeometry::HeightMapGeometry() { m_pFrameTimer = new QTimer(this); connect(m_pFrameTimer, &QTimer::timeout, this, &HeightMapGeometry::m_update); m_pFrameTimer->start(120); m_generateTestMatrix(); m_updateGeometry(); } void HeightMapGeometry::m_updateGeometry() { clear(); int stride = 3 * sizeof(float); QByteArray vertexData(3 * stride, Qt::Initialization::Uninitialized); vertexData.resize(stride * (MATRIX_SIZE * MATRIX_SIZE * 2)); float *p = reinterpret_cast<float *>(vertexData.data()); for (int y = 0; y < MATRIX_SIZE; ++y) { for (int x = 0; x < MATRIX_SIZE; ++x) { QVector3D vertex(x * MATRIX_SCALE, y * MATRIX_SCALE, m_testMatrix[x][y] * MATRIX_SCALE); *p++ = vertex.x(); *p++ = vertex.y(); *p++ = vertex.z(); vertex.setY((y + 1) * MATRIX_SCALE); vertex.setZ(m_testMatrix[x][y + 1] * MATRIX_SCALE); *p++ = vertex.x(); *p++ = vertex.y(); *p++ = vertex.z(); } } setVertexData(vertexData); setPrimitiveType(QQuick3DGeometry::PrimitiveType::LineStrip); setStride(stride); addAttribute(QQuick3DGeometry::Attribute::PositionSemantic, 0, QQuick3DGeometry::Attribute::F32Type); } float HeightMapGeometry::m_generateTestMatrix() { for (int i = 0; i < MATRIX_SIZE; ++i) { for (int j = 0; j < MATRIX_SIZE; ++j) { float num = QRandomGenerator::global()->generateDouble() * (1.f - 0.f) + 0.f; m_testMatrix[i][j] = num; } } } void HeightMapGeometry::m_update() { m_generateTestMatrix(); m_updateGeometry(); update(); }