我正在尝试创建一个空白(没有 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);
最佳答案
如果您有权访问 OpenGL 4.3 or the ARB_copy_image extension (或者它的表亲 NV_copy_image),那么你可以 use glCopyImageSubData
.
否则,您可以使用 FBO block 传输。您将源纹理附加到 an FBO ,将目标纹理附加到 FBO(可能是同一个,但如果是,则显然不是同一个附加点),设置 read buffers和 draw buffers让 FBO 从源附件读取并绘制到目标附件,然后是 blit from one framebuffer to the other .
顺便说一句,您尝试的技巧不起作用,因为您从未为新纹理分配存储。 glTexSubImage
除非storage for that image,否则不能为纹理中的图像调用已分配。这可以是 call to glTexImage
or one of a number of other functions. "SubImage"函数都是用于上传到现有存储,而不是用于创建新存储。
关于c++ - 在 OpenGL 中。如何将纹理的一部分复制到另一个纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17682799/