Mixing OpenGL and OpenCV

Standard

In this example we mix OpenGL (a computer graphics library) and OpenCV. We will play a video on a 3d rotating cube. (Yes, it will be animated!) We mix the code for video playing with code to render a 3d cube with PyOpenGL. If you do not have PyOpenGL installed install that first:

If you are totally unfamiliar with OpenGL, I recommend reading these tutorials.  If you are familiar with OpenGL, the code should be straight forward.

The program will grab frames from the video using OpenCVs  cv2.VideoCapture().  These frames will be converted to OpenGL texture data structure. OpenGL will render the cube like it would render any textured 3d cube. The difference being, that texture are now frames of a video stream. The conversion of the OpenCV Mat datastructure format to the OpenGL texture format is:

Finally the full code:

Output: