gltexsubimage2d functionProvides the ability to modify an image function, because modifying a texture is much less expensive than creating a texture, for some video capture programs can first store the video image in a larger initial image (the image size requirement is 2, OGL 2.0 does not have this limit), create a rendering texture, Then repeatedly call Gltexsubimage2d (the modified image area does not have to be 2 of the secondary) function from the image video image area to read the data into the rendered texture image. The texture image used for rendering needs to be created only once. Function prototypes: gltexsubimage2d (glenum target, glint level, glint xoffset, Glint yoffset, Glsizei width, glsizei height, glenum for Mat, Glenum type, const glvoid *pixels); The target parameter must be a value that corresponds to the target in glcopyteximage2d. Level is mipmap. Xoffset,yoffset is the lower-left offset of the image to be modified. Width,height is the image width high pixel to be modified. The scope of the modification is not affected outside the original image. Format,type describes the format and type of the image data, consistent with format, type glteximage2d. Pixels is the texture data for the sub-image, replaced by the value. Sub-images are also affected by the mode set by glpixelstore* () and glpixeltransfer* () and other pixel transfer operations.
Example: Glbindtexture (gl_texture_2d, texname); int OffsetX = 0;//12; int OffsetY = 0;//44; Gltexsubimage2d (gl_texture_2d, 0, OffsetX, OffsetY, Subimagewidth, Subimageheight, Gl_rgba, Gl_unsigned_byte, SubImage );
glcopytexsubimage2d functionReads a pixel rectangle from the frame buffer Gl_read_buffer and replaces part or all of an existing texture array, which is equivalent to calling the Glcopyteximage2d and GLTEXSUBIMAGE2D functions. Prototype: glcopytexsubimage2d (glenum target, glint level, glint xoffset, Glint Yoffset, Glint x, glint y, Glsizei width, glsize I height); Glcopytexsubimage2d-copy a two-dimensional texture subimage target specifies the target texture. Must be gl_texture_2d, gl_texture_cube_map_positive_x, gl_texture_cube_map_negative_x, GL_TEXTURE_CUBE_MAP_POSITIVE _y, gl_texture_cube_map_negative_y, gl_texture_cube_map_positive_z, or gl_texture_cube_map_negative_z. The pixel is read from the current gl_read_buffer, and it processes the process like calling Glcopypixels, but the pixel data is not placed in the frame buffer, but in the texture memory. The settings for glpixeltransfer* and other pixel transfer operations will also work for it. In OGL 3.0 and later versions, you should be able to render directly to the texture memory using the Framebuffer object, thus efficiently performing and glcopytexsubimage2d the same operation.
Rectangle TextureOGL3.1 adds texture cell locations instead of normalized texture coordinates to locate textures, which are specified by the target of the gl_texture_ractangle and can be mapped directly to pixels at render time using the texture unit. There are also restrictions on using texture rectangles, which cannot be based on mipmap filtering or compressed textures. Here's how to modify the rectangle texture.
Gltexsubimage2d Instance Code:
#include <GL/glut.h> #include <stdlib.h> #include <stdio.h> #ifdef gl_version_1_1/* Create Checkerboa
RD Textures */#define CHECKIMAGEWIDTH #define CHECKIMAGEHEIGHT #define SUBIMAGEWIDTH #define Subimageheight 16
Static Glubyte checkimage[checkimageheight][checkimagewidth][4];
Static Glubyte subimage[subimageheight][subimagewidth][4];
Static Gluint Texname;
void Makecheckimages (void) {int I, j, C; for (i = 0; i < checkimageheight; i++) {for (j = 0; J < Checkimagewidth; J + +) {c = ((((i&0x8) = =
0) ^ ((j&0x8)) ==0)) *255;
Checkimage[i][j][0] = (glubyte) C;
CHECKIMAGE[I][J][1] = (glubyte) C;
CHECKIMAGE[I][J][2] = (glubyte) C;
CHECKIMAGE[I][J][3] = (glubyte) 255; }} for (i = 0; i < subimageheight; i++) {for (j = 0; J < Subimagewidth; J + +) {c = ((((i&
0x4) ==0) ^ ((j&0x4)) ==0)) *255;
Subimage[i][j][0] = (glubyte) 0; subimage[i][j][1] = (glubyte) 0;
SUBIMAGE[I][J][2] = (glubyte) C;
SUBIMAGE[I][J][3] = (glubyte) 255;
}}} void init (void) {Glclearcolor (0.0, 0.0, 0.0, 0.0);
Glshademodel (Gl_flat);
Glenable (gl_depth_test);
Makecheckimages ();
Glpixelstorei (gl_unpack_alignment, 1);
Glgentextures (1, &texname);
Glbindtexture (gl_texture_2d, texname);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, gl_repeat);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, gl_repeat);
Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_nearest);
Gltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_nearest); Glteximage2d (gl_texture_2d, 0, Gl_rgba, checkimagewidth, checkimageheight, 0, Gl_rgba, Gl_unsigned_byte,
Checkimage); } void Display (void) {Glclear (Gl_color_buffer_bit |
Gl_depth_buffer_bit);
Glenable (gl_texture_2d);
GLTEXENVF (gl_texture_env, Gl_texture_env_mode, gl_decal);
Glbindtexture (gl_texture_2d, texname); GLbegin (gl_quads); GLTEXCOORD2F (0.0, 0.0);
glvertex3f (-2.0,-1.0, 0.0); GLTEXCOORD2F (0.0, 1.0);
glvertex3f (-2.0, 1.0, 0.0); GLTEXCOORD2F (1.0, 1.0);
glvertex3f (0.0, 1.0, 0.0); GLTEXCOORD2F (1.0, 0.0);
glvertex3f (0.0,-1.0, 0.0); GLTEXCOORD2F (0.0, 0.0);
glvertex3f (1.0,-1.0, 0.0); GLTEXCOORD2F (0.0, 1.0);
glvertex3f (1.0, 1.0, 0.0); GLTEXCOORD2F (1.0, 1.0);
glvertex3f (2.41421, 1.0,-1.41421); GLTEXCOORD2F (1.0, 0.0);
glvertex3f (2.41421,-1.0,-1.41421);
Glend ();
Glflush ();
Gldisable (gl_texture_2d);
} void Reshape (int w, int h) {glviewport (0, 0, (Glsizei) W, (Glsizei) h);
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (60.0, (glfloat) w/(Glfloat) H, 1.0, 30.0);
Glmatrixmode (Gl_modelview);
Glloadidentity ();
Gltranslatef (0.0, 0.0,-3.6); } void keyboard (unsigned char key, int x, int y) {switch (key) {case ' s ': Case ' s ': Glbindtext
Ure (gl_texture_2d, texname);
int OffsetX = 0;//12; int OffsetY= 0;//44;
Gltexsubimage2d (gl_texture_2d, 0, OffsetX, OffsetY, Subimagewidth, Subimageheight, Gl_rgba,
Gl_unsigned_byte, Subimage);
Glutpostredisplay ();
/* glcopytexsubimage2d*/break;
Case ' R ': Case ' R ': Glbindtexture (gl_texture_2d, texname);
Glteximage2d (gl_texture_2d, 0, Gl_rgba, checkimagewidth, checkimageheight, 0, Gl_rgba,
Gl_unsigned_byte, Checkimage);
Glutpostredisplay ();
Break
Case 27:exit (0);
Break
Default:break;
}} int main (int argc, char** argv) {glutinit (&ARGC, argv); Glutinitdisplaymode (Glut_single | Glut_rgb |
Glut_depth);
Glutinitwindowsize (250, 250);
Glutinitwindowposition (100, 100);
Glutcreatewindow (Argv[0]);
Init ();
Glutdisplayfunc (display);
Glutreshapefunc (reshape);
Glutkeyboardfunc (keyboard);
Glutmainloop (); return 0;
} #else int main (int argc, char** argv) {fprintf (stderr, "This program demonstrates a feature which are not in OpenG
L Version 1.0.\n ");
fprintf (stderr, "If your implementation of OpenGL Version 1.0 have the right extensions,\n");
fprintf (stderr, "your may is able to modify this program to make it run.\n");
return 0; } #endif