Notes when loading textures and using glgentextures (solving memory consumption)

Source: Internet
Author: User
Notes when loading textures and using glgentextures (solving memory consumption) [go]

Glgentextures

Glgentextures (glsizei N, gluint * textures) Function Description

N: Number of textures to generate

Textures: used to store texture Indexes

The glgentextures function returns n texture indexes based on texture parameters. The texture name set does not need to be a continuous Integer Set.

(Glgentextures is used to produce the index of the texture object you want to operate on. For example, you tell OpenGL that I need five texture objects, it returns five of the unused Integers to you)

Glbindtexture actually changes the state of OpenGL. It tells OpenGL that any operations on Textures are bound to texture objects, such as glbindtexture (gl_texture_2d, 1) tell OpenGL that any settings of 2D textures in the following code are for the texture with index 1.

The texture generation function assumes that the area of the target texture is restricted by the glbindtexture function. The texture index set generated by calling glgentextures is not obtained by the glgentextures called later, unless they are first deleted by gldeletetextures. You cannot include glgentextures in the display list.

Void glgentextures (glsizei N, Gluint *Texture);

This function is used to generate a texture name. Texture nameGluint *TextureIt is an integer, so it can be understood that this function specifies n different IDs for the n textures.

Texture is very common when rendering with GL. Before using a texture, you must execute this command to assign an ID to your texture, bind the texture, and load the texture image. Then, the texture can be used. The code for loading a texture is as follows:

Bool loadtextures (iplimage * pimage, gluint * ptexture)
{
Int status = false;
If (pimage! = NULL)
{
Status = true;

Glgentextures (1, & ptexture [0]); // pay attention to this
Glbindtexture (gl_texture_2d, ptexture [0]);
Glteximage2d (gl_texture_2d, 0, 3,
Pimage-> width, pimage-> height,
0, gl_bgr, gl_unsigned_byte, (unsigned char *) pimage-> imagedata );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear );
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear );
}
Return status;
}

Be careful when using the above function. This function can only be used outside the loop! If you want to reuse this texture [0] in a loop and load different textures for it (for example, you want to display a sequence image in a window ), if you call this function within a loop, your computer will become very slow and even cause a crash when the program loops many times. The reason is that glgentextures (1, & ptexture [0]) is called repeatedly. I am not clear about the mechanism of this problem, but I have encountered it today.

Therefore, the above function is usually placed outside the loop. During window initialization, it is used to load textures for the background. So what should I do if I have to render sequence frames in a loop? We can add a slight change to the above function, as shown below:

Bool loadtextures (iplimage * pimage, gluint texture)
{
Int status = false;
If (pimage! = NULL)
{
Status = true;
Glbindtexture (gl_texture_2d, texture );
Glteximage2d (gl_texture_2d, 0, 3,
Pimage-> width, pimage-> height,
0, gl_bgr, gl_unsigned_byte, (unsigned char *) pimage-> imagedata );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear );
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear );
}
Return status;
}

Run the following command during window initialization:

Glgentextures (1, & texture [0]);

Then you can call it within your loop:

Iplimage * videoframe = cvqueryframe (capture );

Loadtextures (videoframe, texture [0]);

In this way, the image frame will not be displayed, and the computer will not be slow. In short, do not repeatedly allocate IDs to a texture.

The loadtextures function provided by myself provides the image buffer interface, which can read Video Frames from outside and pass them to this function, bind the texture, and use it flexibly.

Category:
OpenGL ES

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.