PDA

View Full Version : OpenGL question



^NyAw^
5th February 2008, 20:52
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


glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glFlush();


"showImage": Function that show the image


glClear(GL_COLOR_BUFFER_BIT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
glPixelStorei(GL_UNPACK_SKIP_ROWS,0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS,0);
glTexImage2D(GL_TEXTURE_2D,0,1,iWidth,iHeight,0,GL _LUMINANCE,GL_UNSIGNED_BYTE,pcData);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 0.0f);

glTexCoord2f(1.0f, 1.0f);
glVertex2f(float(width()), 0.0f);

glTexCoord2f(1.0f, 0.0f);
glVertex2f(float(width()), float(height()));

glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, float(height()));
glEnd();
glFlush();
updateGL();


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,

jacek
5th February 2008, 22:22
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().

^NyAw^
5th February 2008, 23:19
Hi,


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).

^NyAw^
5th February 2008, 23:27
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,

jacek
6th February 2008, 00:46
Do you use queued connections between threads and QGLWidget or do you invoke showImage() directly?

^NyAw^
6th February 2008, 08:59
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,

jacek
6th February 2008, 20:22
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?

^NyAw^
7th February 2008, 18:19
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,