Я хочу сделать так: Быстрое копирование между двумя FBO или из FBO в текстуру в OpenGL-ES 1.1 .
Но glCopyTexSubImage2D слишком медленный, и мне нужно найти эффективную функцию. Я закончил шаг «рендеринга в FBO». Но это sh в drawArray из EXC_BAD_ACCESS. Мой кодекс взорван. Надеюсь, кто-нибудь поможет мне. Спасибо
EAGLContext *oldContext = EAGLContext.currentContext;
[EAGLContext setCurrentContext:mEglContext];
// 创建一个离屏fbo
if (!_haveFbo) {
glGenFramebuffers(1, &fbo);
_haveFbo = YES;
}
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, bindTarget, target.textureId, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
NSLog(@"ERROR:GL_FRAMEBUFFER");
}
GLint posHandle = -1;
GLint uvHandle = -1;
GLint flipHandle = -1;
GLint tranHandle = -1;
if (TextureFlipYProgram < 0) {
TextureFlipYProgram = glCreateProgram();
EJGLProgram2D* program2D = [EJGLProgram2D new];
GLuint vertexShader = [EJGLProgram2D compileShaderSource:TEXFlipY_VERTEX type:GL_VERTEX_SHADER];
// program2D->compileShaderSource(NSString::create(TEXFlipY_VERTEX), GL_VERTEX_SHADER);
GLuint fragmentShader = [EJGLProgram2D compileShaderSource:TEXFlipY_FRAGMENT type:GL_FRAGMENT_SHADER];
glAttachShader(TextureFlipYProgram, vertexShader);
glAttachShader(TextureFlipYProgram, fragmentShader);
[EJGLProgram2D linkProgram:TextureFlipYProgram];
glDetachShader(TextureFlipYProgram, vertexShader);
glDeleteShader(vertexShader);
glDetachShader(TextureFlipYProgram, fragmentShader);
glDeleteShader(fragmentShader);
posHandle = glGetAttribLocation(TextureFlipYProgram, "pos");
uvHandle = glGetAttribLocation(TextureFlipYProgram, "uv");
flipHandle = glGetUniformLocation(TextureFlipYProgram,"flip");
tranHandle = glGetUniformLocation(TextureFlipYProgram,"trans");
}
glUseProgram(TextureFlipYProgram);
GLfloat coor[] = {
//顶点坐标 纹理坐标
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(coor), coor, GL_STATIC_DRAW);
glEnableVertexAttribArray(posHandle);
glVertexAttribPointer(posHandle, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GL_FLOAT), (GLvoid*)0);
glEnableVertexAttribArray(uvHandle);
glVertexAttribPointer(uvHandle, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat)));
// sourceTexture永远是2D Texture,用于shader着色
// 为什么source->bindTarget(GL_TEXTURE_2D)不行??
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,target.textureId);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
BOOL flipY = NO;
// 是否翻转
glUniform2f(flipHandle,1.0f,flipY ? -1.0f:1.0f);
// 是否预乘
if (preMultiplyAlpha){
glUniform3f(tranHandle,1.0f,0.0f,0.0f);
} else if (unPreMultiplyAlpha){
glUniform3f(tranHandle,0.0f,1.0f,0.0f);
}else{
glUniform3f(tranHandle,0.0f,0.0f,1.0f);
}
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// draw
glViewport(0, 0, target->width, target->height);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//glBindTexture(GL_TEXTURE_2D, boundTexture2D);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// delete
glDeleteBuffers(1, &vbo);
// 恢复
[EAGLContext setCurrentContext:oldContext];