Using OPENCV to read the image as an OpenGL texture image of the implementation of the method __filter

Source: Internet
Author: User

The first step in implementing the texture mapping of three-dimensional objects in OpenGL is to read the picture and then specify the picture as a texture picture. The first project I took over was to read the texture picture by a special function to read the BMP picture. This function has three disadvantages:

1, can only read BMP pictures, can not read jpg,ppm and other types of pictures, to use other types of pictures, they must be converted into BMP pictures;

2, can only read 2 power and 2 power to the size of the picture, otherwise the function can not correctly read the picture content;

3, can only read 24-bit BMP pictures.

The above three shortcomings make our procedure have the very big limitation. To that end, after learning the OPENCV, I used the cvloadimage () function in OpenCV to solve the problem.

Our solution is as follows:

First, use the following line of code to read the picture into memory:

iplimage * iface = Cvloadimage (szfilename);

Then use the following code to open a memory space in memory that is the same size as the read image:

m_new_face_bmpbytes = (unsigned char *) malloc (sizeof (unsigned char) *iface->imagesize);

if we read 24-bit RGB color pictures, then iface->imagesize several pictures of the pixel size times 3, such as our picture is 512*512 size so iface->imagesize is 512*512*3.

We then use the following code to copy the actual data portion of the picture in Iface to the memory space we have set:

memcpy (m_new_face_bmpbytes,iface->imagedata,sizeof (unsigned char) *iface->imagesize);

Iface->imagedata is the first address of the space for storing picture pixel data

Then we save the size of the picture:

M_new_face_width = iface->width;
M_new_face_height = iface->height;

Finally, we set the image data pointed to m_new_face_bmpbytes as the texture image in OpenGL:

Glgentextures (1, &m_texname); 1 represents the number of textures to be generated, M_texname is used to store the indexes that generate textures, which are used when binding textures

Gltexenvi (gl_texture_env, Gl_texture_env_mode, gl_modulate);
Gldisable (gl_lighting);
Glenable (gl_texture_2d);

Glbindtexture (gl_texture_2d, m_texname);
Gltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_linear);
Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_linear);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, gl_repeat);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, gl_repeat);
Glteximage2d (gl_texture_2d, 0, Gl_rgb8, m_new_face_width, m_new_face_height, 0,
Gl_bgr_ext, Gl_unsigned_byte, m_new_face_bmpbytes);

This completes the setting of the texture image that we have read into as OpenGL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.