Name
glTexSubImage2D, glTextureSubImage2D — specify a two-dimensional texture subimage
C Specification
void glTexSubImage2D( GLenum target,
GLint level,
GLint xoffset,
GLint yoffset,
GLsizei width,
GLsizei height,
GLenum format,
GLenum type,
const void * pixels);
void glTextureSubImage2D( GLuint texture,
GLint level,
GLint xoffset,
GLint yoffset,
GLsizei width,
GLsizei height,
GLenum format,
GLenum type,
const void *pixels);
Parameters
target
Specifies the target to which the texture is bound for glTexSubImage2D. 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, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, or GL_TEXTURE_1D_ARRAY.
texture
Specifies the texture object name for glTextureSubImage2D. The effective target of texture must be one of the valid target values above.
level
Specifies the level-of-detail number. Level 0 is the base image level. Level n is the nth mipmap reduction image.
xoffset
Specifies a texel offset in the x direction within the texture array.
yoffset
Specifies a texel offset in the y direction within the texture array.
width
Specifies the width of the texture subimage.
height
Specifies the height of the texture subimage.
format
Specifies the format of the pixel data. The following symbolic values are accepted: GL_RED, GL_RG, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA, GL_DEPTH_COMPONENT, and GL_STENCIL_INDEX.
type
Specifies the data type of the pixel data. The following symbolic values are accepted: GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT, GL_INT, GL_FLOAT, GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_UNSIGNED_INT_8_8_8_8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, and GL_UNSIGNED_INT_2_10_10_10_REV.
pixels
Specifies a pointer to the image data in memory.
Description
Texturing maps a portion of a specified texture image onto each graphical primitive for which texturing is enabled.
glTexSubImage2D and glTextureSubImage2D redefine a contiguous subregion of an existing two-dimensional or one-dimensional array texture image. The texels referenced by pixels replace the portion of the existing texture array with x indices xoffset and xoffset+width−1, inclusive, and y indices yoffset and yoffset+height−1, inclusive. This region may not include any texels outside the range of the texture array as it was originally specified. It is not an error to specify a subtexture with zero width or height, but such a specification has no effect.
If a non-zero named buffer object is bound to the GL_PIXEL_UNPACK_BUFFER target (see glBindBuffer) while a texture image is specified, pixels is treated as a byte offset into the buffer object’s data store.
Notes
glPixelStore modes affect texture images.
glTexSubImage2D and glTextureSubImage3D specify a two-dimensional subtexture for the current texture unit, specified with glActiveTexture.
GL_STENCIL_INDEX is accepted for format only if the GL version is 4.4 or higher.
Errors
GL_INVALID_ENUM is generated if target or the effective target of texture is not 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, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, or GL_TEXTURE_1D_ARRAY.
GL_INVALID_OPERATION is generated by glTextureSubImage2D if texture is not the name of an existing texture object.
GL_INVALID_ENUM is generated if format is not an accepted format constant.
GL_INVALID_ENUM is generated if type is not a type constant.
GL_INVALID_VALUE is generated if level is less than 0.
GL_INVALID_VALUE may be generated if level is greater than log2 max, where max is the returned value of GL_MAX_TEXTURE_SIZE.
GL_INVALID_VALUE is generated if xoffset<−b, (xoffset+width)>(w−b), yoffset<−b, or (yoffset+height)>(h−b), where w is the GL_TEXTURE_WIDTH, h is the GL_TEXTURE_HEIGHT, and b is the border width of the texture image being modified. Note that w and h include twice the border width.
GL_INVALID_VALUE is generated if width or height is less than 0.
GL_INVALID_OPERATION is generated if the texture array has not been defined by a previous glTexImage2D operation.
GL_INVALID_OPERATION is generated if type is one of GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV, GL_UNSIGNED_SHORT_5_6_5, or GL_UNSIGNED_SHORT_5_6_5_REV and format is not GL_RGB.
GL_INVALID_OPERATION is generated if type is one of GL_UNSIGNED_SHORT_4_4_4_4, GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_UNSIGNED_INT_8_8_8_8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_UNSIGNED_INT_10_10_10_2, or GL_UNSIGNED_INT_2_10_10_10_REV and format is neither GL_RGBA nor GL_BGRA.
GL_INVALID_OPERATION is generated if format is GL_STENCIL_INDEX and the base internal format is not GL_STENCIL_INDEX.
GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER target and the buffer object’s data store is currently mapped.
GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER target and the data would be unpacked from the buffer object such that the memory reads required would exceed the data store size.
GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to the GL_PIXEL_UNPACK_BUFFER target and pixels is not evenly divisible into the number of bytes needed to store in memory a datum indicated by type.
Associated Gets
glGetTexImage
glGet with argument GL_PIXEL_UNPACK_BUFFER_BINDING
Version Support
OpenGL Version
Function / Feature Name 2.0 2.1 3.0 3.1 3.2 3.3 4.0 4.1 4.2 4.3 4.4 4.5
glTexSubImage2D ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
glTextureSubImage2D - - - - - - - - - - - ✔
See Also
glActiveTexture, glCopyTexImage1D, glCopyTexImage2D, glCopyTexSubImage1D, glCopyTexSubImage2D, glCopyTexSubImage3D, glPixelStore, glTexImage1D, glTexImage2D, glTexImage3D, glTexSubImage1D, glTexSubImage3D, glTexParameter
Copyright
Copyright © 1991-2006 Silicon Graphics, Inc. Copyright © 2010-2014 Khronos Group. This document is licensed under the SGI Free Software B License. For details, see http://oss.sgi.com/projects/FreeB/.
中文翻译:
名称
glTexSubImage2D - 指定二维纹理子图像
C规范
void glTexSubImage2D(GLenum target,
GLint level,
GLint xoffset,
GLint yoffset,
GLsizei width,
GLsizei height,
GLenum format,
GLenum type,
const GLvoid * data);
参数
target
指定活动纹理单元的目标纹理。 必须是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或GL_TEXTURE_CUBE_MAP_NEGATIVE_Z。
level
指定详细级别编号。 0级是基本图像级别。 级别n是第n个mipmap缩小图像。
xoffset
指定纹理数组中x方向的纹素偏移。
yoffset
指定纹理数组中y方向的纹素偏移。
width
指定纹理子图像的宽度。
height
指定纹理子图像的高度。
format
指定像素数据的格式。 接受以下符号值:GL_ALPHA,GL_RGB,GL_RGBA,GL_LUMINANCE和GL_LUMINANCE_ALPHA。
type
指定像素数据的数据类型。 接受以下符号值:GL_UNSIGNED_BYTE,GL_UNSIGNED_SHORT_5_6_5,GL_UNSIGNED_SHORT_4_4_4_4和GL_UNSIGNED_SHORT_5_5_5_1。
data
指定指向内存中图像数据的指针。
描述
纹理将指定纹理图像的一部分映射到纹理处于活动状态的每个图形基元上。 当前片段着色器或顶点着色器使用内置纹理查找功能时,纹理处于活动状态。
glTexSubImage2D重新定义了现有二维纹理图像的连续子区域。data引用的纹素将现有纹理数组的部分替换为x索引xoffset和xoffset + width - 1(含),y索引yoffset和yoffset + height - 1(含)。该区域可能不包括纹理数组范围之外的任何最初指定的那样的纹素。指定宽度或高度为零的子纹理不是错误的,但这样的规范是没有效果的。
注意
由glPixelStorei设置的存储参数GL_UNPACK_ALIGNMENT会影响从客户端内存中读取数据的方式。 有关说明,请参阅glPixelStorei。
glTexSubImage2D为当前纹理单元指定二维或立方体贴图子纹理,由glActiveTexture指定。
错误
GL_INVALID_ENUM :如果target不是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或GL_TEXTURE_CUBE_MAP_NEGATIVE_Z。
GL_INVALID_ENUM :如果format或type不是可接受的值。
GL_INVALID_VALUE :level<0
GL_INVALID_VALUE :level>log以2为底,max的对数,其中max为GL_MAX_TEXTURE_SIZE返回的值
GL_INVALID_VALUE :xoffset < 0 , xoffset + width > w , yoffset < 0 , or yoffset + height > h,其中w是宽度,h是要修改的纹理图像的高度。
GL_INVALID_VALUE :如果width或height小于0。
GL_INVALID_OPERATION :如果纹理数组尚未由之前的glTexImage2D或glCopyTexImage2D操作定义(其内部格式与glTexSubImage2D的格式匹配)。
GL_INVALID_OPERATION:如果type为GL_UNSIGNED_SHORT_5_6_5且格式不是GL_RGB。
GL_INVALID_OPERATION :如果type为GL_UNSIGNED_SHORT_4_4_4_4或GL_UNSIGNED_SHORT_5_5_5_1且格式不是GL_RGBA。
相关Gets
glGet 参数GL_MAX_TEXTURE_SIZE 或GL_MAX_CUBE_MAP_TEXTURE_SIZE
另见
glActiveTexture,glCompressedTexImage2D,glCompressedTexSubImage2D,glCopyTexImage2D,glCopyTexSubImage2D,glPixelStorei,glTexImage2D,glTexParameter
版权
https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glTexSubImage2D.xml
https://blog.csdn.net/flycatdeng
Copyright © 1991-2006 Silicon Graphics, Inc.本文档的许可是根据SGI Free Software B License.详见http://oss.sgi.com/projects/FreeB/.
注意!!!
因为纹理坐标的(0,0)点是左下角,所以我们修改纹理数据的时候是从左下角开始绘制的。
顺序先沿着X轴从左至右,再沿着Y轴从下至上,即一行一行像素的绘制。
示例代码如下:
virtual unsigned loadTexture(const char* fileName)
{
unsigned textureId = 0;
//1 获取图片格式
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(fileName, 0);
//2 加载图片
FIBITMAP *dib = FreeImage_Load(fifmt, fileName,0);
//3 转化为rgb 24色
dib = FreeImage_ConvertTo24Bits(dib);
//4 获取数据指针
BYTE *pixels = (BYTE*)FreeImage_GetBits(dib);
int width = FreeImage_GetWidth(dib);
int height = FreeImage_GetHeight(dib);
//windows是BGR模式
for (int i =0;i<width*height*3;)
{
float temp = pixels[i+2];
pixels[i + 2] = pixels[i];
pixels[i] = temp;
i += 3;
}
/**
* 产生一个纹理Id,可以认为是纹理句柄,后面的操作将书用这个纹理id
*/
glGenTextures( 1, &textureId );
/**
* 使用这个纹理id,或者叫绑定(关联)
*/
glBindTexture( GL_TEXTURE_2D, textureId );
/**
* 指定纹理的放大,缩小滤波,使用线性方式,即当图片放大的时候插值方式
*/
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
/**
* 将图片的rgb数据上传给opengl.
*/
glTexImage2D(
GL_TEXTURE_2D, //! 指定是二维图片
0, //! 指定为第一级别,纹理可以做mipmap,即lod,离近的就采用级别大的,远则使用较小的纹理
GL_RGB, //! 纹理的使用的存储格式
width, //! 宽度,老一点的显卡,不支持不规则的纹理,即宽度和高度不是2^n。
height, //! 宽度,老一点的显卡,不支持不规则的纹理,即宽度和高度不是2^n。
0, //! 是否的边
GL_RGB, //! 数据的格式,bmp中,windows,操作系统中存储的数据是bgr格式
GL_UNSIGNED_BYTE, //! 数据是8bit数据
pixels
);
char subData[100 * 100 * 3];
memset(subData, 255, sizeof(subData));
for (int i = 0; i<150;)
{
subData[i] = 0;
subData[++i] = 0;
subData[++i] = 255;
}
glTexSubImage2D(GL_TEXTURE_2D, 0, 100, 50, 100, 100, GL_RGB, GL_UNSIGNED_BYTE, subData);
/**
* 释放内存
*/
FreeImage_Unload(dib);
return textureId;
}
图片修改结果如下:
二、在freetype中应用的时候要注意了!!!
代码如下
int _yStart = 0;
int _xStart = 0;
char* readFile(const char* fileName, unsigned& length)
{
FILE* pFile = fopen(fileName, "rb");
if (pFile)
{
fseek(pFile, 0, SEEK_END);
length = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
char*buffer = new char[length + 1];
fread(buffer, 1, length, pFile);
buffer[length] = 0;
fclose(pFile);
return buffer;
}
else
{
char buffer[1024];
sprintf_s(buffer, "read %s failed", fileName);
assert(pFile != 0 && buffer);
}
return 0;
}
void buildSystemFont(const char* font,int fontSize)
{
_uiProgram.initialize();
unsigned length = 0;
_fontBuffer = readFile(font, length);
/**
* 保存字体的大小
*/
_fontSize = fontSize;
/**
* 已经创建了字体则销毁
* 支持多次调用
*/
if (_face)
{
FT_Done_Face(FT_Face(_face));
_xStart = 0;
_yStart = 0;
memset(_character, 0, sizeof(_character));
}
/**
* 销毁字体
*/
if (_sysFontTexture != -1)
{
glDeleteTextures(1, &_sysFontTexture);
}
glGenTextures(1, &_sysFontTexture);
/**
* 使用这个纹理id,或者叫绑定(关联)
*/
glBindTexture(GL_TEXTURE_2D, _sysFontTexture);
/**
* 指定纹理的放大,缩小滤波,使用线性方式,即当图片放大的时候插值方式
*/
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_ALPHA,
_textureWidth,
_textureHeight,
0,
GL_ALPHA,
GL_UNSIGNED_BYTE,
0
);
FT_Error error = FT_New_Memory_Face((FT_Library)_library,(const FT_Byte*)_fontBuffer,length,0,&(FT_Face)_face);
if (error != 0)
{
return;
}
FT_Face ftFace = (FT_Face)_face;
FT_Select_Charmap(ftFace, FT_ENCODING_UNICODE);
FT_F26Dot6 ftSize = (FT_F26Dot6)(fontSize * (1 << 6));
FT_Set_Char_Size((FT_Face)_face, ftSize, 0, 72, 72);
assert(_face != 0);
}
Character getCharacter(wchar_t ch)
{
if (_character[ch].x0 == 0 &&
_character[ch].x0 == 0 &&
_character[ch].x1 == 0 &&
_character[ch].y1 == 0
)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if (_xStart + max(_fontPixelX, _fontSize) > _textureWidth)
{
/**
* 写满一行,从新开始
*/
_xStart = 0;
/**
* y开始位置要增加
*/
_yStart += max(_fontPixelY, _fontSize);
}
FT_Load_Glyph(_face, FT_Get_Char_Index(_face, ch), FT_LOAD_DEFAULT);
FT_Glyph glyph;
FT_Get_Glyph(FT_Face(_face)->glyph, &glyph);
/**
* 根据字体的大小决定是否使用反锯齿绘制模式
* 当字体比较小的是说建议使用ft_render_mode_mono
* 当字体比较大的情况下12以上,建议使用ft_render_mode_normal模式
*/
if (!(ch >= L'0' && ch <= L'9'))
{
FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1);
}
else
{
FT_Glyph_To_Bitmap(&glyph, ft_render_mode_mono, 0, 1);
}
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
FT_Bitmap& bitmap = bitmap_glyph->bitmap;
FT_Bitmap ftBitmap;
FT_Bitmap_New(&ftBitmap);
if (bitmap.pixel_mode == FT_PIXEL_MODE_MONO)
{
if (FT_Bitmap_Convert((FT_Library)_library, &bitmap, &ftBitmap, 1) == 0)
{
/**
* Go through the bitmap and convert all of the nonzero values to 0xFF (white).
*/
for (unsigned char* p = ftBitmap.buffer, *endP = p + ftBitmap.width * ftBitmap.rows; p != endP; ++p)
*p ^= -*p ^ *p;
bitmap = ftBitmap;
}
}
/**
* 如果没有数据,则不写,直接过去
*/
if (bitmap.width == 0 || bitmap.rows == 0)
{
char mem[1024 * 32];
memset(mem, 0, sizeof(mem));
_character[ch].x0 = _xStart;
_character[ch].y0 = _yStart;
_character[ch].x1 = _xStart + _fontSize / 2;
_character[ch].y1 = _yStart + _fontSize - 1;
_character[ch].offsetY = _fontSize - 1;
_character[ch].offsetX = 0;
glBindTexture(GL_TEXTURE_2D, _sysFontTexture);
glTexSubImage2D(
GL_TEXTURE_2D,
0,
_xStart,
_yStart,
_fontSize / 2,
_fontSize,
GL_ALPHA,
GL_UNSIGNED_BYTE,
mem
);
_xStart += _fontSize / 2;
}
else
{
glBindTexture(GL_TEXTURE_2D, _sysFontTexture);
_character[ch].x0 = _xStart;
_character[ch].y0 = _yStart;
_character[ch].x1 = _xStart + bitmap.width;
_character[ch].y1 = _yStart + bitmap.rows;
_character[ch].offsetY = bitmap_glyph->top;
_character[ch].offsetX = bitmap_glyph->left;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(
GL_TEXTURE_2D,
0,
_xStart,
_yStart,
max(1, bitmap.width),
max(1, bitmap.rows),
GL_ALPHA,
GL_UNSIGNED_BYTE,
bitmap.buffer
);
_xStart += (bitmap.width + 1);
_fontPixelY = max(_fontPixelY, bitmap.rows);
_fontPixelX = max(_fontPixelX, bitmap.width);
}
FT_Bitmap_Done((FT_Library)_library, &ftBitmap);
}
return _character[ch];
}
主要的内容在getCharacter函数,将字库里的数据,通过glTexSubImage2D贴到我们自己的纹理上。
但由于glTexSubImage2D绘制的特性(从下往上),所以我们的字是倒过来的。
我们再使用该纹理写字的时候务必注意倒过来
参考:
https://www.khronos.org/registry/OpenGL-Refpages/gl4/
https://blog.csdn.net/flycatdeng/article/details/82667353
https://blog.csdn.net/Qinhaifu/article/details/102994102