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 doesn't draw lines
Forum Updated to NodeBB v4.3 + New Features

OpenGL doesn't draw lines

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 4.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.
  • M Offline
    M Offline
    mikecurl91
    wrote on last edited by
    #1

    HI,
    I've a QGLWidget that draws a grid of lines, and it works very well.
    The problem is after the grid drawing process has finished:
    infact, in my app, I'm listening for keyboard events and I've reimplemented the QGLWidget has showned below
    @void MyGLWidget::keyPressEvent(QKeyEvent * e)
    {
    switch (e->key())
    {
    case Qt::Key_Escape:
    qDebug()<<"Escape pressed!";
    glLineWidth(6.0);
    glColor3f (1,0,0);
    glBegin (GL_LINES);
    glVertex3f (0,0,0);
    glVertex3f (6,6,6);
    glEnd();
    updateGL();
    break;
    default:
    e->ignore();
    }
    }@

    but, on the console I can see that the escape press event is intercepted, but the new line that I want to draw doesn't appear! The drawing space shows only the grid, but no new lines!
    What is the problem? Why the scene is not updated?
    I'll show the QGLWidget code for a complete documentation of the problem:
    @#include "myglwidget.h"

    /*

    • Sets up the OpenGL rendering context, defines display lists, etc.
    • Gets called once before the first time resizeGL() or paintGL() is called.
      */

    void MyGLWidget::initializeGL()
    {
    //tutti i valori sono tra 0 e 1
    glClearColor(0,0,0,0); //specifica il colore quando chiamerò glClear (nero)
    angle_x=0;
    angle_y=0;
    angle_z=0;
    scale=3;
    }

    void MyGLWidget::resizeGL (int w, int h)
    {
    qDebug()<<"resizeGL called";
    qDebug()<<"w:"<<w<<"h"<<h;

    double aspect;
    
    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width)
    if(h == 0)//altura
        h = 1;
    
     // Set the viewport to be the entire window
     glViewport(0,0 , w, h);
    
     aspect=(double)w/(double)h;
     //Reset projection matrix stack
     glMatrixMode(GL_PROJECTION);
     // Reset the coordinate system before modifying
     glLoadIdentity();
    
     double range = 100;
     if (w <= h)
         glOrtho(-(range),range,-range/aspect,range/aspect,-(range*2*scale),range*2*scale);
     else
         glOrtho(-(range*aspect),range*aspect,-range,range,-(range*2*scale),range*2*scale);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
    

    }
    void MyGLWidget::keyPressEvent(QKeyEvent * e)
    {
    switch (e->key())
    {
    case Qt::Key_Escape:
    qDebug()<<"Escape pressed!";
    glLineWidth(6.0);
    glColor3f (1,0,0);
    glBegin (GL_LINES);
    glVertex3f (0,0,0);
    glVertex3f (6,6,6);
    glEnd();
    updateGL();
    break;
    default:
    e->ignore();
    }
    }

    void MyGLWidget::paintGL()
    {
    qDebug()<<"paintGL called";
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    //glTranslatef(0,0,-6); //traslo tutti gli assi all'interno del viewing volume
    //ruota la camera di 30° rispetto agli assi x,y,z
    glRotatef( angle_x, 1.0f, 0.0f, 0.0f );
    glRotatef( angle_y, 0.0f, 1.0f, 0.0f );
    glRotatef( angle_z, 0.0f, 0.0f, 1.0f );
    //prova scalatura
    glScalef(scale, scale, scale); // Make the shape half as wide, the same height and twice as deep

        drawAxis();  //disegno gli assi
    
        glLineWidth(5.0);
        glColor3f(0.0,0.0,0.0);
    
        glBegin(GL_QUADS);
            glVertex3f( 0,0, 0);
            glVertex3f( 0,100,0);
            glVertex3f(100,100,0);
            glVertex3f(100,0, 0);
        glEnd();
    
        glLineWidth(1.0);
        glColor3f(0.0,1.0,0.0);
        glBegin(GL_LINES);
        for(int i=0;i<=100;i++)
        {
            glVertex3f(i,0,0);
            glVertex3f(i,100,0);
            glVertex3f(0,i,0);
            glVertex3f(100,i,0);
        };
        glEnd();
    
         //swapBuffers();  //scambia il buffer di backend, con quello di frontend (a vista)
    

    }

    void MyGLWidget::drawAxis()
    {
    float ORG[3] = {0,0,0};

    float XP[3] = {2,0,0},
    YP[3] = {0,2,0},
    ZP[3] = {0,0,2};
    
    glLineWidth(6.0);
    
    glBegin (GL_LINES);
        glColor3f (1,0,0);  glVertex3fv (ORG);  glVertex3fv (XP);    // X axis is red.
        glColor3f (0,1,0);  glVertex3fv (ORG);  glVertex3fv (YP);    // Y axis is green.
        glColor3f (0,0,1);  glVertex3fv (ORG);  glVertex3fv (ZP);    // z axis is blue.
    glEnd();
    

    }
    @

    1 Reply Last reply
    0
    • 8Observer88 Offline
      8Observer88 Offline
      8Observer8
      wrote on last edited by
      #2

      See this: http://8observer8.somee.com/QtLine.aspx

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

        Hi,

        AFAIK, calling gl code outside of xxxGL will not work. You should do all rendering inside paintGL.

        What you could do is to update a list of pair of points (i.e QList<QPair< QPoint, QPoint>> and draw them in your paintGL function.

        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
        • M Offline
          M Offline
          mikecurl91
          wrote on last edited by
          #4

          Thank you so much, I've resolve the problem.
          I've a question for you:
          since I've put in the paintGL() method a Qlist<QVector3d> for store the vertex coords, if I have a lot a points in my drawing (like 50000), I'm going to use a lot of memory (ram)... but if I see the memory usage, it is constantly about 20 mb, notwithstanding there are a lot of points in QList<Qvector3d> list! How is possible? Has OpenGL a particulare storing method to prevent (i think so) the excessive memory usage?
          my fear is also that having big, very big drawing, memory could crash...
          [quote author="8Observer8" date="1378055998"]See this: http://8observer8.somee.com/QtLine.aspx[/quote]

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mikecurl91
            wrote on last edited by
            #5

            Thank you so mushììch, I've also premeditate to use a Qlist in order to storing vertexs, but my fear is about the memory usage... could it be excessive?
            [quote author="SGaist" date="1378070317"]Hi,

            AFAIK, calling gl code outside of xxxGL will not work. You should do all rendering inside paintGL.

            What you could do is to update a list of pair of points (i.e QList<QPair< QPoint, QPoint>> and draw them in your paintGL function.

            Hope it helps[/quote]

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

              Then you should have a look at the QGLBuffer class to store your vertices and draw them

              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
              • M Offline
                M Offline
                mikecurl91
                wrote on last edited by
                #7

                Thank for the advice, but as I just read, this class does the same job of the QList: so, where is the advantage?

                [quote author="SGaist" date="1378117757"]Then you should have a look at the QGLBuffer class to store your vertices and draw them[/quote]

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

                  It's a bit more complex than that. But it can indeed contain the same data as a QList in your case, but the idea is to have the all the vertices stored on the GPU side and let the GPU handle the drawing "in mass" rather than one point after the other.

                  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

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved