简体   繁体   English

在openGL中。 如何将纹理的一部分复制到另一个纹理

[英]In openGL. How do you copy part of a texture to another texture

I am trying to create a blank (white with no alpha) texture where I can load other textures and write part of them. 我试图创建一个空白(没有alpha的白色)纹理,可以在其中加载其他纹理并编写其中的一部分。 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. 我试图只获取一部分纹理,然后使用glTexSubImage2D将其放置在其中似乎无法正常工作。 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);

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 . 如果可以访问OpenGL 4.3或ARB_copy_image扩展名 (或其较旧的表亲NV_copy_image),则可以使用glCopyImageSubData

Otherwise, you could use FBO blitting. 否则,您可以使用FBO发条。 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 . 您将源纹理附加到FBO ,将目标纹理附加到FBO(可能是相同的,但如果是,则显然不是相同的附加点),设置FBO的读取缓冲区绘制缓冲区以从中读取源附件并绘制到目标附件,然后从一个帧缓冲区到另一帧缓冲

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. 除非已为该图像的存储分配了存储空间,否则无法为纹理中的图像调用 glTexSubImage This can be by a call to glTexImage or one of a number of other functions. 这可以通过调用glTexImage或其他许多功能之一来实现。 The "SubImage" functions are all for uploading to existing storage, not for creating new storage. “ SubImage”功能全部用于上载到现有存储,而不用于创建新存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM