Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Quick3D] Vertex coloring
Forum Updated to NodeBB v4.3 + New Features

[Quick3D] Vertex coloring

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 341 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    0x5FB5
    wrote on last edited by
    #1

    I've created custom QQuick3DGeometry class. Is it possible to set color to vertex from a code to make some heights different?
    Screenshot from 2022-07-06 11-02-27.png
    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      djdan
      wrote on last edited by
      #2

      Can you provide full code?

      1 Reply Last reply
      0
      • 0 Offline
        0 Offline
        0x5FB5
        wrote on last edited by
        #3

        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();
        }
        
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved