Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

USE opengl load stl file problem(qml)



  • Hello! I'm trying to load stl file by qtquick & opengl.
    Now I can load a triangle. But how can I load complete .stl file by this framework?
    I have tried add more points in vertices. Changed the setAttributeBuffer() function's value. But I still can't get more triangles.

    void TriangleRender::initializeTriangle()
    {
        VertexData vertices[] =
        {
            { QVector3D(-0.5f, -0.5f, 0.0f), QVector3D(1.0, 0.0, 0.0) },
            { QVector3D(0.0f, 0.5f, 0.0f),   QVector3D(0.0, 1.0, 0.0) },
            { QVector3D(0.5f, -0.5f, 0.0f),  QVector3D(0.0, 0.0, 1.0) }
        };
    
        m_vbo.create();
    
        m_vbo.bind();
        m_vbo.allocate(vertices, 3 * sizeof (VertexData));
    }
    
    void TriangleRender::render()
    {
        static float angle = 0.0;
    
        glClearColor(0.2, 0.3, 0.3, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
    
        angle += 1.0f;
        QMatrix4x4 modelMatrix;
        modelMatrix.translate(0.0, 0.0, -5.0);
        modelMatrix.rotate(angle, 0.0f, 1.0f, 0.0f);
    
        m_program.bind();
        m_program.setUniformValue("mvp",  m_projection * modelMatrix);
    
        m_vbo.bind();
    
    #ifdef Q_OS_WIN
        int location = 0;
        //position
        m_program.enableAttributeArray(location);
        m_program.setAttributeBuffer(location, GL_FLOAT, 0, 3, sizeof(VertexData));
        //color
        m_program.enableAttributeArray(location + 1);
        m_program.setAttributeBuffer(location + 1, GL_FLOAT, sizeof(QVector3D), 3, sizeof(VertexData));
    
    #else
        int vertexLocation = m_program.attributeLocation("position");
        m_program.enableAttributeArray(vertexLocation);
        m_program.setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(VertexData));
    
        int colorLocation = m_program.attributeLocation("color0");
        m_program.enableAttributeArray(colorLocation);
        m_program.setAttributeBuffer(colorLocation, GL_FLOAT, sizeof(QVector3D), 3, sizeof(VertexData));
    #endif
    
        glDrawArrays(GL_TRIANGLES, 0, 3);
    }
    

    GLSL:

    #version 330 core
    
    layout (location = 0) in vec4 position;
    layout (location = 1) in vec3 color0;
    
    out vec3 color;
    uniform mat4 mvp;
    
    void main(void)
    {
        gl_Position = mvp * position;
        color = color0;
    }
    

Log in to reply