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. QOpenGLWidget drawing a line. Matrix problem?
Forum Updated to NodeBB v4.3 + New Features

QOpenGLWidget drawing a line. Matrix problem?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 175 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.
  • J Offline
    J Offline
    jacubus
    wrote on last edited by
    #1

    Hello i have a some code which is supposed to draw a white line in QOpenGlWidget which is part of my app. I just want to create 3D space inside the app where you'll be able make some changes.
    When i added matrixes and camera position the line is not visible (Previously has been displaying incorrectly but at least something has been visible, so i found out that i need to set up camera view properly and draw line basing on camera and projection)..
    Camera is displaying a scene from +z and looking at 0z when the line is in -z so it should be clearly visible. Here is code

    In class variables:

    private:
        QOpenGLShaderProgram program;
        QOpenGLBuffer vbo;
        QOpenGLVertexArrayObject vao;
        int am = 0;
    
    void CubeMainWidget::initializeGL()
    {
        // Create a Vertex Array Object
               initializeOpenGLFunctions();
    
               vao.create();
               vao.bind();
    
               // Create a Vertex Buffer Object and copy data to it
               vbo.create();
               vbo.bind();
               am = 2;
               float vertices[] = {
                   -1.0f, 0.0f, -3.0f,
                   1.0f, 0.0f, -3.0f
    
               };
               vbo.allocate(vertices, sizeof(vertices));
    
               // Create a Shader Program
               program.addShaderFromSourceCode(QOpenGLShader::Vertex,
                   "#version 330\n"
                   "in vec3 position;\n"
                   // |2|
                   "uniform mat4 projection;\n"
                   "uniform mat4 view;\n"
                   // -|2|
                   "void main()\n"
                   "{\n"
                   "    gl_Position = projection * view * vec4(position, 1.0);\n"
                   "}\n");
               program.addShaderFromSourceCode(QOpenGLShader::Fragment,
                   "#version 330\n"
                   "out vec4 color;\n"
                   "void main()\n"
                   "{\n"
                   "    color = vec4(1.0, 1.0, 1.0, 1.0);\n"
                   "}\n");
               program.link();
    
               // Set the Vertex Array Object as the current one
               vao.bind();
               
               //! [2]
               // Enable depth buffer
               glEnable(GL_DEPTH_TEST);
    
               // Enable back face culling
               glEnable(GL_CULL_FACE);
               //! [2]
    
               // Enable attribute arrays
               program.enableAttributeArray("position");
    
               // Set the attribute arrays
               program.setAttributeArray("position", GL_FLOAT, 0, 3);
               
               // Release all
               vao.release();
               vbo.release();
               program.release();
    }
    void CubeMainWidget::resizeGL(int w, int h)
    {
        // |2|
        qreal aspect = qreal(w) / qreal(h ? h : 1);
        qDebug() << qreal(w) << qreal(h) << aspect;
        QMatrix4x4 projection;
        projection.perspective(45.f, aspect, 0.01f, 100.f);
        program.setUniformValue("projection", projection);
        QMatrix4x4 view;
        view.lookAt(QVector3D(0.0f, 0.0f, 5.0f),
                    QVector3D(0.0f, 0.0f, 0.0f),
                    QVector3D(0.0f, 1.0f, 0.0f));
        program.setUniformValue("view", view);
    }
    void CubeMainWidget::paintGL()
    {
        // Clear the screen
              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
              // Use the Shader Program
              program.bind();
    
    
              // Use the Vertex Array Object
              vao.bind();
    
              // Draw the line
              glDrawArrays(GL_LINES, 0, am);
    
              // Release all
              vao.release();
              program.release();
    }
    
    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