blTexture — Load an IplImage into an opengl texture and create webcam and video textures using the blImageAPI

Introduction

Today I introduce a simple class that will help us load IplImage images into opengl textures. This simple class, as I will continue to show you in later posts, provides us with a very easy and powerful method to intermix images, videos and 3d graphics. In the end, you’ll be able to do the following:

  1. How do I load an image from a file into an opengl texture?
  2. How do I load an IplImage into an opengl texture?
  3. How do I create video textures?
  4. How do I create webcam textures?
  5. How do I save a texture into an IplImage?
  6. How do I save a texture to file?
  7. How do I save whatever I’m painting with OpenGL in a file or IplImage?

To get your feet wet, the following snippet loads a couple of pictures into opengl textures, and shows them as spheres in an sfml window. The snippet uses the sfml library to create the window:

Snippet (Click to see code…)

The resulting output is the following:

Dependencies

  1. blImage
    1. Used to handle opencv IplImage

Texture functions list

  1. CreateEmptyTexture
    1. Creates an empty texture of the specified width and height
  2. LoadImageToTexture
    1. Loads a blImage into an opengl texture
  3. LoadImageFromFileToTexture<blType>
    1. Loads an image from file to an opengl texture. blType specifies the depth you want for the image you want to load into the opengl texture.
  4. SaveTextureToImage
    1. Saves the current texture into a blImage.  The image is saved depending on the type of the blImage (could be color, grayscale or whatever the blImage format is).
  5. SaveTextureToFile
    1. Saves the current texture to file.  The image is saved either in color or grayscale depending on the template parameter specified by the programmer.
  6. CopyBufferToThisTexture
  1. Used to render to a texture.  It basically copies the current back buffer to the texture object.

The code

The class is called blTexture and is saved in a file called blTexture.hpp, to which I will be adding more functions dealing with textures in the future. I have put blTexture inside the blImageAPI namespace, but to use it, you have to define the macro USE_BL_TEXTURE, so that if you don’t want to use opengl, it won’t give you errors.

blTexture.hpp (Click to see code…)

Usage

This collection of functions makes it really easy to create some stunning visual effects with images or videos in a 3d environment.  Just to wet your appetite, the following snippet shows a “naive” way to use a video as an opengl texture.  In posts blVideoThread and blVideoThread2, I actually present ways to take advantage of parallel programming to avoid slowing down your main graphics routine.

Snippet (Click to see code…)

The resulting output is a video being shown on two spheres, the following shows a picture:

Downloads

I have put all the files into a zip file which can be downloaded here. All you have to do is extract it somewhere, let’s say in a directory called blImageAPI, and then include the blImageAPI.hpp file as follows:

// To use the blTexture class we define the following macro
#define USE_BL_TEXTURE

#include "blImageAPI.hpp"
using namespace blImageAPI;

Note: Everything is declared in the namespace blImageAPI, and such you would use it as: blImageAPI::blImage

Note2: To use the blTexture, you have to declare the macro USE_BL_TEXTURE before including the blImageAPI.hpp file
[download#1]

Updates

  1. Nov/16/2010 — To use the blTexture functionality, you need the opengl library. You also need the glext.h file, which I have provided here: [download#4]. I have also provided a function which I got from NeHe lesson #45 to search for an opengl extension. Just download it and include the files as such (before including the blImageAPI.hpp file):
    // A file and some variables and functions
    // I got from NeHe lesson #45 to check for
    // opengl extensions
    #include "GLext/glext.h"
    #include "GLext/blCheckForOPENGLExtensions.hpp"
    
  2. May/16/2011 — Added more functions.
    1. Added two functions to save the texture image to file or to a blImage.
    2. Added a simple function to copy the current back buffer to the texture object.

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

Leave a Reply

Your email address will not be published. Required fields are marked *