在openGL。如何将一部分纹理复制到另一个纹理 [英] In openGL. How do you copy part of a texture to another texture

查看:175
本文介绍了在openGL。如何将一部分纹理复制到另一个纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个空白(白色没有alpha)纹理,我可以加载其他纹理和写入它们的一部分。我试图只需要得到一部分的纹理,并使用glTexSubImage2D把它放在那里似乎无法正常工作。任何人都有任何想法如何做到这一点?我做错了什么?

I am trying to create a blank (white with no alpha) texture where I can load other textures and write part of them. I tried just having to get a portion of the texture, and using glTexSubImage2D to put it there it doesn't seem to work right. Anyone have any idea how to do this? What am i doing wrong?

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或ARB_copy_image扩展(或其旧表哥,NV_copy_image),那么您可以使用 glCopyImageSubData

If you have access to OpenGL 4.3 or the ARB_copy_image extension (or its older cousin, NV_copy_image), then you can use glCopyImageSubData.

否则, FBO blitting。您将源贴图附加到 FBO ,将目标贴图附加到FBO(可能是同一个贴图),但如果是这样,那么显然不会到相同的附加点),请设置读取缓冲区绘制缓冲区,以便FBO从源附件读取并绘制到目标附件,然后按 blit从一个framebuffer到另一个

Otherwise, you could use FBO blitting. You attach the source texture to an FBO, attach the destination texture to an FBO (possibly the same one, but if so, then obviously not to the same attachment point), set the read buffers and draw buffers for the FBOs to read from the source attachment and draw to the destination attachment, and then blit from one framebuffer to the other.

你试图做的伎俩不工作,BTW ,因为您从未为新纹理分配存储 >该图像的存储已分配。这可以通过调用 glTexImage 或许多其他函数。SubImage函数都是用于上传到现有存储,而不是用于创建新存储。

The trick you're trying to do doesn't work, BTW, because you never allocated storage for your new texture. glTexSubImage cannot be called for an image in a texture unless storage for that image has been allocated. This can be by a call to glTexImage or one of a number of other functions. The "SubImage" functions are all for uploading to existing storage, not for creating new storage.

这篇关于在openGL。如何将一部分纹理复制到另一个纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆