OpenGL line width/thickness



  • Hi,
    I am trying to draw a skeleton of a cube (i.e. edges) in OpenGL. I have an array of all edges (lines) which are specified by two vertices. Everything is ok. I am able to render a wireframed cube. Nevertheless, I would like to have thicker lines and I tried to do it by glLineWidth but it does not work. Have you any idea what I am doing wrong?

    thx

    #include "model3.h"
    
    #include <QDebug>
    #include <QtMath>
    
    Model3::Model3(QWidget* parent) : QOpenGLWidget(parent)
    {
    
    }
    
    Model3::~Model3()
    {
        delete m_program;
    }
    
    
    void Model3::initializeGL()
    {
        this->initializeOpenGLFunctions();
    
        const char* clickVertexShader =
                "#version 330 core\n"
                "uniform mat4 projection;\n"
                "uniform mat4 view;\n"
                "uniform mat4 model;\n"
                "in vec4 position;\n"
                "void main()\n"
                "{\n"
                "gl_Position = projection * view * model * position;"
                "}\n";
        const char* clickFragmentShader =
                "#version 330\n"
                "out vec4 outputF;\n"
                "void main()\n"
                "{\n"
                "outputF = vec4(0, 0, 0, 0);"
                "}\n";
    
        this->m_program = new QOpenGLShaderProgram(this);
        m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, clickVertexShader);
        m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, clickFragmentShader);
        m_program->link();
        m_aPos = m_program->attributeLocation("position");
    
        glEnable(GL_DEPTH_TEST);
    
        this->m_lastX = width() / 2;
        this->m_lastY = height() / 2;
    }
    
    
    void Model3::resizeGL(int, int)
    {
    
    }
    
    
    void Model3::paintGL()
    {
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        this->m_program->bind();
    
        float lines[] = {
            -0.5f, -0.5f,  0.5f,  0.5f, -0.5f,  0.5f,
    
            -0.5f, -0.5f,  0.5f, -0.5f,  0.5f,  0.5f,
    
            -0.5f, -0.5f,  0.5f, -0.5f, -0.5f, -0.5f,
    
             0.5f, -0.5f,  0.5f,  0.5f,  0.5f,  0.5f,
    
             0.5f, -0.5f,  0.5f,  0.5f, -0.5f, -0.5f,
    
             0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
    
             0.5f, -0.5f, -0.5f,  0.5f,  0.5f, -0.5f,
    
            -0.5f, -0.5f, -0.5f, -0.5f,  0.5f, -0.5f,
    
            -0.5f,  0.5f,  0.5f,  0.5f,  0.5f,  0.5f,
    
            -0.5f,  0.5f,  0.5f, -0.5f,  0.5f, -0.5f,
    
             0.5f,  0.5f,  0.5f,  0.5f,  0.5f, -0.5f,
    
             0.5f,  0.5f, -0.5f, -0.5f,  0.5f, -0.5f
        };
    
        QVector<QVector3D> cubePositions;
        cubePositions << QVector3D(0.0f,  0.0f,  0.0f);
    
        QMatrix4x4 view;
        view.translate(0.0f, 0.0f, -3.0f);
        view.rotate(45.0f, 1.0f, 0.0f, 0.0f);
        view.rotate(45.0f, 0.0f, 1.0f, 0.0f);
    
        QMatrix4x4 projection;
        projection.perspective(m_fov, (float)width() / (float)height(), 0.1f, 100.0f);
    
        m_program->setUniformValue("projection", projection);
    
        QOpenGLBuffer vbo(QOpenGLBuffer::VertexBuffer);
        vbo.create();
        vbo.bind();
        vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
        vbo.allocate(lines, sizeof(lines));
    
        m_program->setUniformValue("view", view);
    
        QOpenGLVertexArrayObject cubeVAO(this);
        cubeVAO.create();
        cubeVAO.bind();
    
        glVertexAttribPointer(m_aPos, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
    
        QMatrix4x4 model;
        model.translate(cubePositions[0]);
        m_program->setUniformValue("model", model);
    
        glLineWidth(5.0f);
    
        for (int l = 0; l < 24; l += 2)
        {
            glDrawArrays(GL_LINES, l, 2);
        }
    
        glDisableVertexAttribArray(0);
    
        cubeVAO.release();
    
        vbo.release();
    
        m_program->release();
    }
    
    
    void Model3::wheelEvent(QWheelEvent *event)
    {
        float sign = (event->angleDelta().y() > 0) ? 1.0f : -1.0f;
    
        if (m_fov >= 1.0f && m_fov <= 45.0f)
            m_fov -= sign * 3;
    
        if (m_fov <= 1.0f)
            m_fov = 1.0f;
    
        if (m_fov >= 45.0f)
            m_fov = 45.0f;
    
        this->update();
    }
    
    




  • thx @m-sue for reply

    It might not be the problem. I have tried to print the GL_ALIASED_LINE_WIDTH_RANGE variable and it shows interval (1, 7.375).



  • Hi @thompsonx

    Maybe you will need glEnable(GL_LINE_SMOOTH);?

    -Michael



  • @m.sue

    No effect

     glEnable(GL_LINE_SMOOTH);
        glLineWidth(5.0f);
        GLfloat lineWidthRange[2] = {0.0f, 0.0f};
        glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, lineWidthRange);
        qInfo() << lineWidthRange[0] << lineWidthRange[1];
    
        for (int l = 0; l < 24; l += 2)
        {
            m_program->setUniformValue("code", l);
            glDrawArrays(GL_LINES, l, 2);
        }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.