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. Incorrect Position of viewport in OpenGL + Qt
QtWS25 Last Chance

Incorrect Position of viewport in OpenGL + Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
openglqopenglwidget
2 Posts 2 Posters 899 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.
  • S Offline
    S Offline
    sckorpio
    wrote on 8 Mar 2019, 13:20 last edited by
    #1

    Hello,
    I am working on a CAD type application,where i am using multiple QOpenglWidgets. I am trying to render a SAMPLE Triangle but the viewport position is incorrect.

    //This the resize function-------------------------------------------------

    void resizeEvent(QResizeEvent * event)
    {
    unsigned int width=event->size().width();
    unsigned int height=event->size().height();

    if (height == 0) height = 1;
    GLfloat aspect = (GLfloat)width / (GLfloat)height;
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if (width >= height)
    {
    // aspect >= 1, set the height from -1 to 1, with larger width
    gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
    }
    else
    {
    // aspect < 1, set the width to -1 to 1, with larger height
    gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
    }
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    // And from PaintEvent i am calling this-------------------

    void SAMPLE_OPENGL(float Ty)
    {

    glClearColor(1,1,0,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    qDebug("~TRIANGLE~~~~");

    glMatrixMode(GL_MODELVIEW); // To operate on Model-View matrix
    glLoadIdentity(); // Reset the model-view matrix

    glBegin(GL_LINES); // sample X,Y axis (in immediate mode) (Just to check)
    glColor3f(1,0,0); //X
    glVertex2f(-1.0f, 0.0f);
    glVertex2f( 3.0f, 0.0f);
    glColor3f(0,1,0); //Y
    glVertex2f( 0.0f, 1.0f);
    glVertex2f( 0.0f, -1.0f);
    glEnd();

    glTranslatef(0, Ty, 0.0f); // Translate left and up
    glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle
    glColor3f(0.0f, 0.0f, 1.0f); // Blue
    glVertex2f(0.2f, 0.2f);
    glColor3f(1.0f, 0.0f, 0.0f); // Red
    glVertex2f(0.8f, 0.2f);
    glColor3f(0.0f, 1.0f, 0.0f); // Green
    glVertex2f(0.5f, 0.6f);
    glEnd();
    glPopMatrix();

    }

    //Note there are multiple QOpenglWidgets
    //Here is the screenshot of output0_1552051164238_Screenshot from 2019-03-08 18-37-37.png

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Mar 2019, 21:21 last edited by
      #2

      Hi and welcome to devnet,

      From the looks of it you are using both glu and the fixed pipeline.

      Since you are starting a new project, may a suggest taking a look at the Qt OpenGL examples and their use of current technologies like shaders ?

      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

      1/2

      8 Mar 2019, 13:20

      • Login

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