我想創建一個空白(沒有alpha的白色)紋理,我可以加載其他紋理並寫入其中的一部分。我試着只需要獲得紋理的一部分,並使用glTexSubImage2D來放置它,但它似乎並不正確。任何人有任何想法如何做到這一點?我究竟做錯了什麼?在openGL中。你如何複製紋理的一部分到另一個紋理
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);
嘗試使用glCopyTexSubImage2D:http://www.opengl.org/sdk/docs/man/xhtml/glCopyTexSubImage2D.xml –
但是,使用glCopyTexSubImage2D,您需要確保您正在讀取紋理,而不是backbuffer,我相信使用glFramebufferTexture2D。 –
我回答了OpenGL的GPU內部拷貝的這個問題,但是StackOverflow變得很愚蠢,因爲我的非常精確的問題被標記爲一個糟糕的非法的副本,而另一個高級用戶甚至改變了我的問題,使其不再有任何意義。現在,我希望每個人都可以自己找到解決問題的方法。 – thewhiteambit