How to draw lines opengl.



  • This is example code with bit of modification ,problem is drawing wont show lines, what aspect to look into? , are matrix and other comands correct?

    @
    void TriangleWindow::render()
    {
    const qreal retinaScale = devicePixelRatio();
    glViewport( 0 , 0 ,width() * retinaScale,height() * retinaScale);

    glClear(GL_COLOR_BUFFER_BIT);

    m_program->bind();
    
    
    QMatrix4x4 matrix;
    matrix.perspective(60, 4.0f/3.0f, 0.1f, 100.0f);
    matrix.translate(move_LEFT_RIGHT/500, 0, -2+move_UP_DOWN/200);
    
    m_program->setUniformValue(m_matrixUniform, matrix);
    

    // Set the line width
    glLineWidth(6);
    // Draw the line
    glColor3f( 1.0f, 1.0f, 1.0f);

    glBegin(GL_LINE_STRIP);
    glVertex2f(0.0f,2.0f);// eelmine
    glVertex2f(0.0f, -1.0f);
    glEnd();
    /*
    GLfloat vertices[] = {
    0.0f, 0.707f,
    -0.5f, -0.5f,
    0.5f, -0.5f
    };

    GLfloat colors[] = {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f
    };
    
    glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
    
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    
    glDrawArrays(GL_TRIANGLES, 0, 3);
    
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
    

    */
    m_program->release();

    ++m_frame;
    

    }@



  • Which OpenGl profile are you using? if you are not in compatible mode with version post 3.2 your code won't run, because this functions
    @
    glBegin(GL_LINE_STRIP);
    glVertex2f(0.0f,2.0f);// eelmine
    glVertex2f(0.0f, -1.0f);
    glEnd();@
    are deprecated, you should set

    @QSurfaceFormat fmt;
    fmt.setDepthBufferSize(24);
    fmt.setVersion(1,2); // or 4,3, but compatibility profile
    fmt.setProfile(QSurfaceFormat::CoreProfile);

    QSurfaceFormat::setDefaultFormat(fmt);@



  • i solved it by drawing 2 triangles that created a square like line.



  • Have problem with code below , it allows to draw about 30 trianagles but if declare more they fail.
    How to make code correct to allow drawing more than ~30 triangles to be drawn?


    void TriangleWindow::render()//rend
    {
    const qreal retinaScale = devicePixelRatio();
    glViewport( 0 , 0 ,width() * retinaScale,height() * retinaScale);

    glClear(GL_COLOR_BUFFER_BIT);

    m_program->bind();
    
    
    QMatrix4x4 matrix;
    matrix.perspective(60, 4.0f/3.0f, 0.1f, 100.0f);
    
    if(zoom > 594){zoom = 594;}
    
    
    matrix.translate(move_LEFT_RIGHT/100-shift_for_stabilisation, -3+move_UP_DOWN/100, -10+(zoom/60));
    

    m_program->setUniformValue(m_matrixUniform, matrix);

    GLfloat kastID[] = {
    
    
        //kast alu
    
        kast_lopp ,  kasti_sisenemis_vaartus    ,
        kasti_koht , kasti_sisenemis_vaartus-kast_korgus ,
        kast_lopp , kasti_sisenemis_vaartus-kast_korgus ,
    
        kast_lopp ,   kasti_sisenemis_vaartus    ,
        kasti_koht  , kasti_sisenemis_vaartus-kast_korgus  ,
        kasti_koht  ,  kasti_sisenemis_vaartus      ,
    
        //kast üle
    
        kast_lopp ,  kasti_sisenemis_vaartus+kast_korgus    ,
        kasti_koht , kasti_sisenemis_vaartus ,
        kast_lopp , kasti_sisenemis_vaartus ,
    
        kast_lopp ,   kasti_sisenemis_vaartus+kast_korgus    ,
        kasti_koht  , kasti_sisenemis_vaartus  ,
        kasti_koht  ,  kasti_sisenemis_vaartus+kast_korgus
    
    
    };
    
    
    
    GLfloat vertices[] = {
    

    //lauis , kõrgus

        //jooned kolmunukrkadest
        SHIFTright + laius ,  TASE   + korgus2  ,
        eelmineAEG - laius , eelmineTASE ,
        eelmineAEG + laius , eelmineTASE +korgus2 ,
    
        SHIFTright + laius ,   TASE  + korgus2  ,
        eelmineAEG - laius ,  eelmineTASE  ,
        SHIFTright - laius ,  TASE      ,
    
    
    
         SHIFTright + laius ,  TASE    ,
         eelmineAEG - laius , eelmineTASE ,
         eelmineAEG + laius , eelmineTASE ,
    
         SHIFTright + laius ,   TASE    ,
         eelmineAEG - laius ,  eelmineTASE  ,
         SHIFTright - laius ,  TASE,
    
    
    
        -50000.0f ,  sl+laius   ,
        -50000.0f ,  sl-laius ,
         50000.0f , sl-laius ,
    
        //rg
        -50000.0f ,kastist_valjumise_vaartus+laius,
        -50000.0f ,kastist_valjumise_vaartus-laius,
         50000.0f ,kastist_valjumise_vaartus-laius,
    
    
    };
    
    
    
    GLfloat KASTcolors[] = {
    
    
        //kast alumine
    
                   0.3f, 0.0f, 0.0f,
                   0.3f, 0.0f, 0.0f,
                   0.3f, 0.0f, 0.0f,
    
                   0.3f, 0.0f, 0.0f,
                   0.3f, 0.0f, 0.0f,
                   0.3f, 0.0f, 0.0f,
    

    //kast ülemine

           0.0f, 0.3f, 0.0f,
           0.0f, 0.3f, 0.0f,
           0.0f, 0.3f, 0.0f,
    
           0.0f, 0.3f, 0.0f,
           0.0f, 0.3f, 0.0f,
           0.0f, 0.3f, 0.0f,
    
    
    
    };
    
    
    
    GLfloat colors[] = {
    

    //valged jooned
    1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f,

        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
    
        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
    
        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
    
    
        //sl värvid
    
                   0.0f, 0.0f, 1.0f,
                   0.0f, 0.0f, 1.0f,
                   0.0f, 0.0f, 1.0f,
    
        //rg värvid
        RED_for_line, GREEN_for_line, 0.0f,
        RED_for_line, GREEN_for_line, 0.0f,
        RED_for_line, GREEN_for_line, 0.0f,
    
    
    };
    
    
    
    glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
    
    
    glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, kastID);
    glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, KASTcolors);
    
    
    
    
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    
    
    glDrawArrays(GL_TRIANGLES, 0, 30);
    
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
    
    
    
    eelmineAEG = SHIFTright;
     eelmineTASE = TASE;
    

    }//for end

      TriangleWindow::setTitle("Tulemus="+QString::number(tulemus)+"          K="+QString::number(KASTkorgus_plus_minus)+"        L="+
                               QString::number(KASTlaius_plus_minus)+"        Sl="+QString::number(sl_multiplier)+"        len="+
                               QString::number(lugemis_tagasi_max));
    

    }//if end +++++++++++++++++++++++++++++++++++

    m_program->release();
    
    
    
    ++m_frame;
    

    }


Log in to reply
 

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