如何在Qt3D中将objectpicker和Camera添加到我的实体中?

时间:2019-10-15 02:08:32

标签: qt3d

我需要渲染一些线和点来保存3d场景中的某些数据。

需要用鼠标拾取点,然后获取该点的数据。

我首先尝试定义一个从QQuickFramebufferObject继承的类,但是我发现很难进行鼠标拾取。

我发现它在Qt3D模块中具有ObjectPicker,因此我想使用Qt3D处理我的工作。

我的测试代码如下。我定义了我的GeometryRenderer对象,并设置了顶点数据(只需绘制两个三角形)。像这样:

GeometryRenderer {
    id: geometry
    geometry: Geometry {
        boundingVolumePositionAttribute: position
        Attribute {
            id: position
            attributeType: Attribute.VertexAttribute
            vertexBaseType: Attribute.Float
            vertexSize: 3
            count: 4
            byteOffset: 0
            byteStride: 6 * 4
            name: "position"
            buffer: vertexBuffer
        }
        Attribute {
            id: color
            attributeType: Attribute.VertexAttribute
            vertexBaseType: Attribute.Float
            vertexSize: 3
            count: 4
            byteOffset: 3 * 4
            byteStride: 6 * 4
            name: "color"
            buffer: vertexBuffer
        }

        Attribute {
            attributeType: Attribute.IndexAttribute
            vertexBaseType: Attribute.UnsignedShort
            vertexSize: 1
            count: 6
            buffer: indexBuffer
        }
    }
    Buffer {
        id: vertexBuffer
        type: Buffer.VertexBuffer
        data: new Float32Array(...)
    }
    Buffer {
        id: indexBuffer
        type: Buffer.IndexBuffer
        data: new Uint16Array(...)
    }
}

然后定义这样的材料对象:

Material {
    id: material
    effect: Effect {
        techniques: Technique {
            graphicsApiFilter {
                profile: GraphicsApiFilter.CoreProfile
            }
            renderPasses: RenderPass {
                shaderProgram: ShaderProgram {
                    vertexShaderCode: loadSource("qrc:/shader/hellotriangle.vert")
                    fragmentShaderCode: loadSource("qrc:/shader/hellotriangle.frag")
                }
            }
        }
    }
}

然后在我的根实体中


Entity {
    id: root
    components: [
        RenderSettings {
            activeFrameGraph: colorBuffer
            pickingSettings.pickMethod: PickingSettings.TrianglePicking
            pickingSettings.pickResultMode: PickingSettings.NearestPick
        },
        InputSettings { }
    ]
    ClearBuffers {
        id: colorBuffer
        clearColor: Qt.rgba(0.8, 0.8, 0.8, 0.6)
        buffers: ClearBuffers.ColorDepthBuffer
        RenderSurfaceSelector {
            RenderStateSet {
                renderStates: DepthTest {
                    depthFunction: DepthTest.Less
                }
            }
        }
    }
}

它起作用,渲染两个三角形。我想在场景中添加Qt3D摄像机和ObjectPicker,我该怎么做?

我发现如果我使用ForwardRenderer(但是我找不到一种方法来渲染自己的线/点顶点)而不是ClearBuffers,那么Camera和ObjectPicker都可以工作

0 个答案:

没有答案