我正在尝试创建一个空白(没有 alpha 的白色)纹理,我可以在其中加载其他纹理并写入其中的一部分。我尝试只需要获取纹理的一部分,然后使用 glTexSubImage2D 将它放在那里似乎无法正常工作。任何人都知道如何做到这一点?我究竟做错了什么?
int sourceTextWidth;
int sourceTextHeight;
int sourceFormat;
int formatOffset = 0;
//bind the texture
glBindTexture( GL_TEXTURE_2D, textureID );
//get its params
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &sourceFormat);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &sourceTextWidth);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &sourceTextHeight);
switch (sourceFormat)
{
case GL_RGB:
{
formatOffset = 3;
}
break;
case GL_RGBA:
{
formatOffset = 4;
}
break;
}
if (formatOffset == 0)
{
return false;
}
unsigned char * sourceData = (unsigned char *)malloc(sourceTextWidth * sourceTextHeight * formatOffset);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, sourceData);
glBindTexture( GL_TEXTURE_2D, m_currentTextureId );
unsigned char * destData = (unsigned char *)malloc(width * height * 3);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, sourceData);
for (int i = 0; i < height; ++i)
{
memcpy(&destData[(i * width * 3) ], &sourceData[((i + y) * sourceTextWidth * 3) + (x * 3)], width * 3);
}
//glDeleteTextures (1, m_currentTextureId);
glBindTexture( GL_TEXTURE_2D, m_currentTextureId );
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB,GL_UNSIGNED_BYTE, destData );
free(destData);
free(sourceData);