Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Render STL with Qt buffers

Render STL with Qt buffers

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 3.8k Views
  • 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.
  • PythonimusP Offline
    PythonimusP Offline
    Pythonimus
    wrote on last edited by Pythonimus
    #1

    Hi,

    I want to render a STL file with Qt Buffers. Using Qt version 5.4.2.
    The problem is, this code does not render the STL object but just crashes with a core dump (memory read error)
    Does igl::readSTL returns what I think it does, or did I messed up with the buffers?
    Here is my code:

    #include "movablesphere.h"
    
    #include <QMouseEvent>
    #include <QDebug>
    #include <QMatrix>
    #include <QRect>
    #include <Eigen/Geometry>
    #include <igl/readSTL.h>
    #include <QFile>
    #include <QTemporaryDir>
    #include "mesh.h"
    
    #include <QOpenGLBuffer>
    #include <QOpenGLShaderProgram>
    
    QOpenGLBuffer* verticesBuffer;
    QOpenGLBuffer* trianglesBuffer;
    QOpenGLBuffer* normalsBuffer;
    
    QOpenGLShaderProgram *program;
    int triangleSize;
    
    movableSphere::movableSphere(){
        setWheelSensitivity(0);
        setRotationSensitivity(0);
    
        QTemporaryDir tempDir;
        if(tempDir.isValid()){
    
            QFile::copy(":/halfpipe_gyroscope_noncon.stl", tempDir.path() + QString("/halfpipe_gyroscope_noncon.stl"));
    
            Eigen::MatrixX3d vertices, normals;
            Eigen::MatrixXi triangles;
    
            igl::readSTL(tempDir.path().toStdString() + "/halfpipe_gyroscope_noncon.stl", vertices, triangles, normals);
    
            //compile shaders
            program = new QOpenGLShaderProgram();
            program->addShaderFromSourceCode(QOpenGLShader::Vertex, "attribute highp vec4 vertex;\n"
                "void main()\n"
                "{\n"
                "  gl_Position = vertex;\n"
                "}"
            );
            program->addShaderFromSourceCode(QOpenGLShader::Fragment, "void main()\n"
                "{\n"
                "  gl_FragColor = vec4(0.8,0.4,0.0,1.0);\n"
                "}"
            );
            //program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vertex_shader.glsl");
            //program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fragment_shader.glsl");
            program->link();
            program->bind();
    
            //cast libigl matrix to opengl matrix
            Eigen::MatrixXf c_vertices, c_normals;
            Eigen::Matrix<uint, Eigen::Dynamic, Eigen::Dynamic> c_triangles;
            c_vertices = vertices.transpose().cast<float>();
            c_triangles = triangles.transpose().cast<uint>();
            c_normals = normals.transpose().cast<float>();
    
            std::cout << "Liquido: Triangle Size: " << c_triangles.cols() << std::endl;
            std::cout << "Liquido: Vertices Size: " << c_vertices.cols() << std::endl;
            triangleSize = c_triangles.size();
    
            //Create Buffers
            QOpenGLBuffer* verticesBuffer = new QOpenGLBuffer();
            QOpenGLBuffer* trianglesBuffer = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
            QOpenGLBuffer* normalsBuffer = new QOpenGLBuffer();
    
            verticesBuffer->create();
            trianglesBuffer->create();
            normalsBuffer->create();
    
            //binding triangles
            trianglesBuffer->bind();
            trianglesBuffer->allocate(c_triangles.data(), static_cast<int>(c_triangles.size()*sizeof(unsigned int)));
            trianglesBuffer->release();
    
            //binding vertices buffer/array
            verticesBuffer->bind();
            verticesBuffer->allocate(c_vertices.data(), static_cast<int>(c_vertices.size()*sizeof(float)));
            verticesBuffer->release();
    
            //binding normals buffer/array
            normalsBuffer->bind();
            normalsBuffer->allocate(c_normals.data(), static_cast<int>(c_normals.size()*sizeof(float)));
            normalsBuffer->release();
    
            program->release();
    
        }else{
            std::cerr << "Cannot create temporary directory! Quitting." << std::endl;
            exit(0);
        }
    }
    
    void movableSphere::draw(QOpenGLFunctions_2_1* gl){
        program->bind();
    
        //Vertex Buffer
        verticesBuffer->bind();
        int vertexLocation = program->attributeLocation("vertex");
        program->enableAttributeArray(vertexLocation);
        program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3);
        trianglesBuffer->bind();
    
        gl->glDrawElements(GL_TRIANGLES, triangleSize, GL_UNSIGNED_INT, 0);
    
        verticesBuffer->release();
        trianglesBuffer->release();
        program->release();
    }
    

    Thank you!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The Qt3D module might be of interest for your use case.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Hi,

        is there a minimal/easy to understand AssimpParser example?
        I just need to render a STL object in a qglviewer::ManipulatedFrame, so I can move it with the mouse.
        Or should I use something else to manipulate the object?

        Thanks!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Do you mean something like the assimp example ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • PythonimusP Offline
            PythonimusP Offline
            Pythonimus
            wrote on last edited by
            #5

            Thanks!
            But how can I render from inside a "plugin" of a main program?
            I got only an QOpenGLFunctions_2_1* gl pointer.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you describe your setup ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • PythonimusP Offline
                PythonimusP Offline
                Pythonimus
                wrote on last edited by Pythonimus
                #7

                The program uses Qt5 and libqglviewer.
                The main program creates a QGLViewer (QGLWidget) instance and renders in it with QOpenGLBuffers and glDrawElements.
                This program has plugins, which have a draw function: void draw3D(QOpenGLFunctions_2_1* gl);
                The example plugin just created a new qglviewer::ManipulatedFrame and rendered with gluSphere(foo...);.
                My question is now: What is the easiest way to render STL data in this plugin?

                EDIT: Edited the start post, program crashes with memory read error now

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Do you have any control on that main application ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • PythonimusP Offline
                    PythonimusP Offline
                    Pythonimus
                    wrote on last edited by
                    #9

                    It would be great not to modify it.

                    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