Today I am trying to write a few lines to read the a webCam using OpenCV and output frames as textures using OpenGL. Start with a simple and standard structure of OpenGL program [gl_init() for initialize the GL parameters, onDraw() to draw things, which is to be called by glutDisplayFunc()] in main loop. Then read the camera input using cvQueryFrame(capture) and mapped to a texture. Finally output int real-time in a window.
The problem here is cvQueryFrame(capture) cannot be called in the onDraw(), by doing which the output stream freezes at the first frame. Here the magic function glutIdleFunc(idleFunc) comes to play. glutIdleFunc(void *func(void)) sets the global idle callback to be func so a GLUT program can perform background processing tasks or continuous animation when window system events are not being received. If enabled, the idle callback is continuously called when events are not being received. The cvQueryFrame(capture) then should be called in the idleFunc, which is handled by glutIdleFunc() in main loop.
Note: check the existence of the captured image before processing otherwise may counter seg fault.