Results 1 to 8 of 8

Thread: OpenGL question

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default OpenGL question

    Hi,

    I've created a OpenGL widget class and I create two objects of this class.
    I'm using this code to show the images captured from two cameras. When a image is captured, it is sended to the main thread that have the two openGL widgets and tell them to show the image.

    "initializeGL" function
    Qt Code:
    1. glClearColor(0.0f,0.0f,0.0f,1.0f);
    2. glClear(GL_COLOR_BUFFER_BIT);
    3. glEnable(GL_TEXTURE_2D);
    4. glFlush();
    To copy to clipboard, switch view to plain text mode 

    "showImage": Function that show the image
    Qt Code:
    1. glClear(GL_COLOR_BUFFER_BIT);
    2. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    3. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    4. glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    5. glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
    6. glPixelStorei(GL_UNPACK_SKIP_ROWS,0);
    7. glPixelStorei(GL_UNPACK_SKIP_PIXELS,0);
    8. glTexImage2D(GL_TEXTURE_2D,0,1,iWidth,iHeight,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,pcData);
    9.  
    10. glBegin(GL_QUADS);
    11. glTexCoord2f(0.0f, 1.0f);
    12. glVertex2f(0.0f, 0.0f);
    13.  
    14. glTexCoord2f(1.0f, 1.0f);
    15. glVertex2f(float(width()), 0.0f);
    16.  
    17. glTexCoord2f(1.0f, 0.0f);
    18. glVertex2f(float(width()), float(height()));
    19.  
    20. glTexCoord2f(0.0f, 0.0f);
    21. glVertex2f(0.0f, float(height()));
    22. glEnd();
    23. glFlush();
    24. updateGL();
    To copy to clipboard, switch view to plain text mode 

    The problem is that sometimes the image from camera1 is shown on widget2 and sometimes the image from the camera2 is shown on the widget1. Note that stopping one camera, the other one is displaying at its widget without problem, so I think that I have a OpenGL problem.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL question

    Do I understand correctly that you first try to draw something using OpenGL commands and then you call updateGL()? Shouldn't you store the image data somewhere and call updateGL, so that painGL() can draw everything?

    Read this: QGLWidget::makeCurrent().

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL question

    Hi,

    Quote Originally Posted by jacek View Post
    Do I understand correctly that you first try to draw something using OpenGL commands and then you call updateGL()? Shouldn't you store the image data somewhere and call updateGL, so that painGL() can draw everything?

    When the camera takes me an image, I call the OpenGL widget to draw it, so I pass it the char pointer that points the memory buffer. Inside this function I create a rectangle and aplly a texture on it. The texture will be the image created using the pointer to the buffer.
    So, as the image have to be painted ouside the "paintGL" function, I have to force it to paint using "updateGL" (as I readed on QGLWidget).
    Òscar Llarch i Galán

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL question

    Hi,

    Now using "makeCurrent" seems to work a little more. The images are shown in the correct widget but sometimes the program crash.
    Should I use a lock to paint to the graphics card? Maybe one thread has called "makeCurrent", then the other thread enters to the CPU and calls "makeCurrent", do its job and the CPU control returns to the first thread. Here the context is not its context and it is trying to do something. Could this be the problem?

    Thanks,
    Òscar Llarch i Galán

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL question

    Do you use queued connections between threads and QGLWidget or do you invoke showImage() directly?

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL question

    Hi,

    I use Queued connections. I connect the signal of the Threads to the main Thread. When the main thread recives the signal it calls "showImage" from the QGLWidgets.

    I think that I found the problem. Showing images is now working well(Thanks), but, the Thread also uses image algorithms to do some jobs. Here is the problem. The libraries could not be thread safe and so if I'm calling the same function on two different threads it could crash.

    For the algorithm of showing images, thanks another time. I'm new to OpenGL and this is the only way that I found to show images. If there is a better way to do this, please tell me how to do this.

    Thanks,
    Òscar Llarch i Galán

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL question

    Quote Originally Posted by ^NyAw^ View Post
    I'm new to OpenGL and this is the only way that I found to show images. If there is a better way to do this, please tell me how to do this.
    I would do all of the drawing in paintGL(), because you can't tell when you need the repaint. For example the user might move some other window over your one.

    showImage() should only to store the updated image data in some member variable and schedule the update using updateGL(). This way you have clear separation of concerns.

    What do you do exactly with the image in your threads? Do you use OpenGL there?

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL question

    Hi,

    I would do all of the drawing in paintGL(), because you can't tell when you need the repaint. For example the user might move some other window over your one.
    Yes, but this is no problem. Think that when an image is captured by the camera it have to be displayed. It's what I'm doing(my purpose)

    What do you do exactly with the image in your threads? Do you use OpenGL there?
    The Threads are how capture the image from the camera. One thread per camera. I don't use OpenGL there. I use a QueuedConnection to tell the Main Thread that can display an image and it do it with the OpenGL widget.

    That's all. It's now working ok. I have looked at raster images on OpenGL but I think that mapping textures will be faster.

    Thanks,
    Òscar Llarch i Galán

Similar Threads

  1. OpenGL and Qt Question
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2009, 18:04
  2. OpenGL question
    By ^NyAw^ in forum General Programming
    Replies: 2
    Last Post: 4th February 2008, 09:39
  3. Qtopia Core & OpenGL ES?
    By zelko in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 28th May 2007, 07:21
  4. OpenGL ES, Qt/11 - Qtopia Core?
    By zelko in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2007, 10:56
  5. Qt's optimized OpenGL context switching
    By sverhoff in forum Qt Programming
    Replies: 0
    Last Post: 28th March 2006, 16:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.