在OpenGL ES 2.0中从一个非2功率纹理复制到另一个纹理

时间:2016-08-25 14:46:50

标签: ios opengl-es render-to-texture

我在iOS上使用OpenGL ES 2.0。

我有以下代码从较小的纹理复制到较大的纹理内;一切都经过,没有glGetError()。然而,当我读回像素时,似乎没有任何东西被写入并且原始纹理未被修改。

所以我想知道是否可以使用Texture Unit 0进行复制;或当我要求它使用纹理作为其颜色渲染缓冲区时,是否使用纹理单元0隐式地使用帧缓冲区?换句话说glFramebufferTexture2D绑定纹理单元0?我是否需要使用纹理单元1代替fromTexture?

或者代码是否有其他问题可能导致它跳闸?

glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, toTextureImage.name );

// Set the texture parameters for a non power of 2;
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

glBindTexture( GL_TEXTURE_2D, 0 );

// Generate a new FBO. It will contain the texture as the color render buffer (color attachment).
GLuint offscreenFrameBuffer;

glGenFramebuffers( 1, &offscreenFrameBuffer );
glBindFramebuffer( GL_FRAMEBUFFER, offscreenFrameBuffer );

// Bind the texture to the FBO; Question ?  With this use the texture unit 0?
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, toTextureImage.texture.name, 0 );

// Test if everything failed
GLenum status = glCheckFramebufferStatus( GL_FRAMEBUFFER );

if( status != GL_FRAMEBUFFER_COMPLETE )
{
    DebugLog( @"failed to make complete framebuffer object %x", status );
    return ;
}

// Bind the FBO; it is already bound;
// glBindFramebuffer( GL_FRAMEBUFFER, offscreenFrameBuffer );

glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, fromTextureImage.name );

BoundingBox uvBounds;

uvBounds.xmin = 0.0;
uvBounds.xmax = 1.0;
uvBounds.ymin = 0.0;
uvBounds.ymax = 1.0;

Vector2 renderUVs[6];
quadsFromBoundingBox( &uvBounds, renderUVs );

BoundingBox verticesBounds;

verticesBounds.xmin = toPos->v[0];
verticesBounds.xmax = toPos->v[0] + fromTextureImage.imageSize->v[0];
verticesBounds.ymin = toPos->v[1];
verticesBounds.ymax = toPos->v[1] + fromTextureImage.imageSize->v[1];

Vector2 renderVertices[6];
quadVerticesFromBoundingBox( &verticesBounds, renderVertices );

[ self prepareToDraw ];

// Tell the shader what the UVs are ...
[ self setTexture0UVs:renderUVs ];

// use linear filtering for non power of 2
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

// set the default color to use
[ self setColor4b:&colorRGB_White ];

// Tell the shader what the vertices are;
[ self setVerticesVector2s:renderVertices ];

glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO);

glDrawArrays( GL_TRIANGLES, 0, 6 );

glDisable(GL_BLEND);

// Unbind the framebuffer;
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
glDeleteFramebuffers( 1, &offscreenFrameBuffer );

1 个答案:

答案 0 :(得分:1)

Framebuffer附件绑定与纹理单元无关;它们是API中完全独立的概念。