Qt / Qml 3D如何从顶点数组渲染网格

时间:2018-09-06 17:46:59

标签: qt qml qt3d

尽管我一直坚持画一个简单的立方体,但我在运行时正在生成网格。为什么屏幕上什么都没出现?

如何将浮点数组(三角形顶点)转换为3D网格?

为满足所需的描述长度,我有第二个相关问题。当多维数据集已经被渲染时,如何在其上放置纹理?

   class Master : public QObject
    {
        Q_OBJECT
    public:
        explicit Master(QObject *parent = nullptr) : QObject(parent)
        {
            float vertexArray[] = {
                -1.0f,-1.0f,-1.0f,
                -1.0f,-1.0f, 1.0f,
                -1.0f, 1.0f, 1.0f,
                1.0f, 1.0f,-1.0f,
                -1.0f,-1.0f,-1.0f,
                -1.0f, 1.0f,-1.0f,
                1.0f,-1.0f, 1.0f,
                -1.0f,-1.0f,-1.0f,
                1.0f,-1.0f,-1.0f,
                1.0f, 1.0f,-1.0f,
                1.0f,-1.0f,-1.0f,
                -1.0f,-1.0f,-1.0f,
                -1.0f,-1.0f,-1.0f,
               -1.0f, 1.0f, 1.0f,
                -1.0f, 1.0f,-1.0f,
                1.0f,-1.0f, 1.0f,
                -1.0f,-1.0f, 1.0f,
                -1.0f,-1.0f,-1.0f,
                -1.0f, 1.0f, 1.0f,
                -1.0f,-1.0f, 1.0f,
                1.0f,-1.0f, 1.0f,
                1.0f, 1.0f, 1.0f,
                1.0f,-1.0f,-1.0f,
                1.0f, 1.0f,-1.0f,
                1.0f,-1.0f,-1.0f,
                1.0f, 1.0f, 1.0f,
                1.0f,-1.0f, 1.0f,
                1.0f, 1.0f, 1.0f,
                1.0f, 1.0f,-1.0f,
                -1.0f, 1.0f,-1.0f,
                1.0f, 1.0f, 1.0f,
                -1.0f, 1.0f,-1.0f,
                -1.0f, 1.0f, 1.0f,
                1.0f, 1.0f, 1.0f,
                -1.0f, 1.0f, 1.0f,
                1.0f,-1.0f, 1.0f
            };

            m_buffer.append(reinterpret_cast<const char*>(vertexArray), sizeof(vertexArray));
        }

        Q_PROPERTY(QByteArray buffer READ buffer CONSTANT)
        QByteArray buffer() const { return m_buffer; }

    private:
         QByteArray m_buffer;

    };

和Scene3D中的GeometryRenderer:

  Entity{
            Material{
                id: material
                parameters: [
                    Parameter { name: "color"; value: "green" }
                ]
            }

            GeometryRenderer{
                id: geometryRenderer

                instanceCount: 0
                primitiveType: GeometryRenderer.Triangles

                geometry: Geometry{
                    Attribute{
                        attributeType: Attribute.VertexAttribute
                        vertexBaseType: Attribute.Float
                        vertexSize: 3
                        byteOffset: 0
                        byteStride: 3 * 4
                        count: 12
                        buffer : Buffer{
                            type: Buffer.VertexBuffer
                            data: Master.buffer
                        }
                    }
                }
            }
            components: [material, geometryRenderer]
        }

0 个答案:

没有答案