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. OpenGL line width/thickness
Forum Updated to NodeBB v4.3 + New Features

OpenGL line width/thickness

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 8.7k Views 1 Watching
  • 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.
  • thompsonxT Offline
    thompsonxT Offline
    thompsonx
    wrote on last edited by
    #1

    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();
    }
    
    
    m.sueM 1 Reply Last reply
    0
    • thompsonxT thompsonx

      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();
      }
      
      
      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by m.sue
      #2

      Hi @thompsonx

      Maybe your OpenGL card does not support it: https://stackoverflow.com/questions/34866964/opengl-gllinewidth-doesnt-change-size-of-lines

      -Michael.

      1 Reply Last reply
      0
      • thompsonxT Offline
        thompsonxT Offline
        thompsonx
        wrote on last edited by thompsonx
        #3

        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).

        m.sueM 1 Reply Last reply
        0
        • thompsonxT thompsonx

          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).

          m.sueM Offline
          m.sueM Offline
          m.sue
          wrote on last edited by
          #4

          Hi @thompsonx

          Maybe you will need glEnable(GL_LINE_SMOOTH);?

          -Michael

          thompsonxT 1 Reply Last reply
          0
          • m.sueM m.sue

            Hi @thompsonx

            Maybe you will need glEnable(GL_LINE_SMOOTH);?

            -Michael

            thompsonxT Offline
            thompsonxT Offline
            thompsonx
            wrote on last edited by
            #5

            @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);
                }
            
            
            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              YuCrazing
              wrote on last edited by YuCrazing
              #6

              Anyone solved this problem? I have the same problem, the range of supported line widths is:
              GL_ALIASED_LINE_WIDTH_RANGE: (1.0, 10.0)
              GL_SMOOTH_LINE_WIDTH_RANGE: (1.0, 1.0)

              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