OpenGL window inside of a mainwindow?



  • I've taken over someone else's project and need to change the way images are displayed onto the screen. I decided to use OpenGL for this task, but I have no idea how to get a window to display inside of the main window that is already there. The only thing that I can get is to have a 2nd window pop up.

    I have been looking into this for the past two days and I have no clue how to accomplish this. All the tutorials I've found online have the OpenGL as the main window, but the key here is that I already have buttons and sliders and other junk in the window and just need to put the image display on there as well. Right now it's a QGraphicsScene, but that's just too slow for what I need. Or at least, I'm hoping OpenGL will do it faster. What I'm trying to do is display a greyscale image onto the screen. Nothing fancy. I grab a frame from the camera, adjust the contrast (I have my own way to do it), and then spit out each pixel individually on the screen. I want to use glDrawPixels.

    Anyway, how do I get the window to appear along side things like sliders and buttons?



  • Create a QGLWidget and put it in the main window layout along with the other controls.

    I recommend using a texture rather than glDrawPixels. The performance should be better.



  • Okay, I got it semi-working. I now have a different issue. I have a resizeGL function, an paintGL function, and an initializeGL function.

    If I run my code, I get paintGL to run somehow. I never call it, but it runs. If I try to pass something to paintGL and call it manually, nothing happens. For example, I want a greyscale image and whatever I pass is the greylevel I want to display. But it doesn't change.

    @
    void GLWidget::paintGL(int j) {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1,0,0);
    int max = 512;
    unsigned short image[555555];
    for(int i = 0; i < max
    max; i++)
    {
    image[i]= j;
    }
    glDrawPixels(max,max,GL_LUMINANCE,GL_UNSIGNED_SHORT,image);
    }
    @

    Elsewhere in the code I hook up a button to paintGL. Every time I press the button, I both call paintGL and at the same time increment 'j' by 10 (and make sure it's between 0 and 255). I'd expect to see the screen get gradually brighter as I press my button. But nothing happens. So I'm confused. When and how do I call paintGL?

    [EDIT: code formatting, please wrap in @-tags, not in code-tags, Volker]



  • "void paintGL()" is the virtual function is called whenever the widget needs to be painted. You don't call it. The QGLWidget base implementation calls it. If you want to change a parameter used in paintGL(), do something like the following:

    @
    void GLWidget::setGreyLevel(int j)
    {
    greyLevel = j;
    update(); // Cause QGLWidget to repaint, and hence call paintGL()
    }
    @

    with a paintGL() function of:
    @
    void GLWidget::paintGL()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1,0,0);
    int max = 512;
    unsigned short image[555555];
    for(int i = 0; i < max
    max; ++i)
    image[i]= greyLevel;
    glDrawPixels(max,max,GL_LUMINANCE,GL_UNSIGNED_SHORT,image);
    }
    @



  • Oh I see. Thank you for your help!



  • Alright, now that works, I'd like to ask about textures vs. glDrawPixels.

    What I do right now is get a 16-bit greyscale image from a framegrabber, run it through a contrast adjuster, then display that on the screen.

    The contrast adjust works by taking in a minimum value to display and a maximum value. Anything below the minimum gets set to 0, anything above the maximum gets set to 255, and the rest gets spread linearly between 0 and 255. This is actually a look-up table that just gets recalculated when the user changes the input sliders.

    It is my understanding that a texture works directly through the video card, whereas glDrawPixels uses the CPU. If I need to push the data through the LUT anyway, would it really help at all to use a texture instead of glDrawPixels?

    Especially since the video card on the target compute for this program has 512MB of RAM, but our image stacks can easily get to 1GB at a time.

    Basically, my layout for this is: get an image, stick it in the image stack as a 16-bit unsigned integer array. To display an image, I pull it out of the image stack, push it through the LUT, and display the result. So the way I see it, most of it stays in system memory. I know I don't know much about graphics, though, so I could be totally wrong. I just want to make sure what I do is likely to yield some good results before I invest all the time into doing it.


Log in to reply
 

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