Click here to Skip to main content
Click here to Skip to main content

Video Texture in OpenGL

, 25 Mar 2009
Rate this:
Please Sign up or sign in to vote.
This article describes how a texture can be created in OpenGL from a live video stream from a web cam or a video file.

Introduction

This short program shows how a live video stream from a web cam (or from a video file) can be rendered in OpenGL as a texture. The live video stream is captured using the Open Source Computer Vision library (OpenCV). The program also shows how an OpenCV image can be converted into OpenGL texture. This code can be used as the first step in the development of an Augmented Reality application using OpenCV and OpenGL.

Understanding the Code

The program renders an OpenGL textured quad which shows a live video stream. The code does not contain any additional functionality, and is kept very simple for easy understanding.

The OpenGL texture is continuously created in the OnIdle callback function. The next available frame in the video stream is captured first:

IplImage *image = cvQueryFrame(g_Capture);

The image is stored in the OpenCV data structure IplImage. Please see the OpenCV documentation for details. The image captured by OpenCV is stored as a BGR. It is first converted to RGB using the OpenCV function cvCvtColor.

cvCvtColor(image, image, CV_BGR2RGB);

Then, the following magic call creates a 2D OpenGL texture from the OpenCV image:

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image->width, image->height, 
                  GL_RGB, GL_UNSIGNED_BYTE, image->imageData);

The texture is loaded into memory and is available for rendering.

Compiling and Running

The code is compiled and tested using Microsoft Visual Studio 2008. However, it can be compiled using any C++ compiler on any platform. The program uses the OpenGL, GLUT, and OpenCV libraries. Please make sure that you have installed them and paths to the include and lib directories are set. OpenCV can be downloaded from here.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Arsalan Malik
Software Developer (Senior)
Pakistan Pakistan
No Biography provided

Comments and Discussions

 
Questionhow to play a video PinmemberSweety Khan13-Oct-11 21:25 
GeneralVideo textures in opengl Pinmembernavyenzo23-Nov-10 15:58 
GeneralMy vote of 1 PinmemberBig D21-Mar-10 21:11 
GeneralMy vote of 1 PinmemberJim Crafton14-Jan-10 9:58 
GeneralThank you very much [modified] PinmemberMember 443275517-Jun-09 1:07 
GeneralSome efficiency notes Pinmemberm.e.fathy30-Apr-09 23:34 
You do not have to build mipmaps (which doubles the texture loading time). So, you do not have to use gluBuild2DMipmaps(). Instead, consider using
 
glTexImage2D(
GL_TEXTURE_2D, //target
0, //mip-map level 0
GL_RGB, //internal format: the format OpenGL will use to store the image
image->width,
image->height,
0, //no border
GL_RGB, //src format is RGB. You can save more time by removing cvCvtColor and set the src format here to GL_BGR
GL_UNSIGNED_BYTE, //type
image->imageData);
 
I think we could even speed the loading process up by using fast asynchronous PBO (Pixel Buffer Object) DMA transfers as described in http://www.songho.ca/opengl/gl_pbo.html[^].
 
One more possible issue, I think you need to flip the image vertically before loading it onto OpenGL, or otherwise adjust the rendering code so that the vertical texture coordinates t become 1 - t.
GeneralRe: Some efficiency notes Pinmemberaquawicket25-Apr-12 11:31 
General[My vote of 1] Check Nehe SDK Pinmembersrsr25-Mar-09 21:06 
GeneralRe: [My vote of 1] Check Nehe SDK PinmemberArsalan Malik26-Mar-09 4:12 
GeneralRe: [My vote of 1] Check Nehe SDK Pinmembersrsr26-Mar-09 4:24 
GeneralRe: [My vote of 1] Check Nehe SDK PinmemberBen_King14-Oct-09 22:37 
GeneralMy vote of 1 PinmemberAdrian Pasik25-Mar-09 10:51 
GeneralRe: My vote of 1 PinmemberArsalan Malik26-Mar-09 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 25 Mar 2009
Article Copyright 2009 by Arsalan Malik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid