将部分纹理复制到另一个纹理OpenGL 4.6 C++

zphenhs4  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(152)

如果我必须纹理ID
SrcIDDestID
如何将srcID的一部分复制到DestId
给定要复制的部分的标准化坐标,src和dest的宽度和高度

  • normailsed是指介于0和1之间 *
deyfvvtc

deyfvvtc1#

您可以使用glCopyImageSubData执行此操作。
glCopyImageSubData

glCopyImageSubData(SrcID, GL_TEXTURE_2D, 0, 0, 0, 0,
                   DestID, GL_TEXTURE_2D, 0, 0, 0, 0,
                   width, height, 1);
lrpiutwd

lrpiutwd2#

你可以把纹理附加到一个帧缓冲区,然后使用glBlitFramebuffer。我不能测试下面的代码,但它应该是正确的。你只需要指定像素的坐标,但它很容易计算,通过乘以归一化坐标的纹理尺寸。

GLuint FboID;
glGenFramebuffers(1, &FboID);
glBindFramebuffer(GL_FRAMEBUFFER, FboID);

// Attach the textures to atttachment slots
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, SrcID, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, DestID, 0);

// Prepare to read from first attachment (src texture)
// and draw into second attachment (dst texture)
glReadBuffer(GL_COLOR_ATTACHMENT0);
GLenum[] DstBuffer = { GL_COLOR_ATTACHMENT1 };
glDrawBuffers(1, &DstBuffer);

// Fill those pixel coordinates from your normalized coordinates 
// (e.g. srcX0 = 0.2f * srcWidth) 
GLint srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1;
glBlitFramebuffer(
    srcX0, srcY0, 
    srcX1, srcY1, 
    dstX0, dstY0, 
    dstX1, dstY1
    GL_COLOR_BUFFER_BIT, 
    GL_LINEAR);

glBindFramebuffer(GL_FRAMEBUFFER, 0); // disable FBO
// optionally delete FBO, if you won't need it anymore
// glDeleteFramebuffers(1, &FboID);

相关问题