render to texture in OpenGL ES
The key is glFramebufferTexture2D in creating FBO. The image is rendered to the bound image buffer through the attachment point.
/* texId[0] = texture from resource file texId[3] = texture on the fly in Frame Buffer 1 */ ///////////////////////// void initRenderTexFbo(){ // FBO glGenFramebuffers(1, fboId); glBindFramebuffer(GL_FRAMEBUFFER, fboId); // texture glGenTextures(1, &texId[3]); glBindTexture(GL_TEXTURE_2D, texId[3]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fboWidth, fboHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // Framebuffer Texture glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId[3], 0); GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; glDrawBuffers(1, DrawBuffers); // RBO glGenRenderbuffers(1, &rboId); glBindRenderbuffer(GL_RENDERBUFFER, rboId); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, fboWidth, fboHeight); // back to default glBindFramebuffer(GL_FRAMEBUFFER, 0); } ///////////////////////// void render_to_FrameBuffer1(){ glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pFooGlInfo->fboId); glActiveTexture(GL_TEXTURE0 + texUnit); glBindTexture(GL_TEXTURE_2D, pFooGlInfo->texId[0]); glBindVertexArray(id_vao); glUseProgram(shaderProgramId); glClearColor(1.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // ... glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idbIdx); glDrawElements(type, idx_n, GL_UNSIGNED_SHORT, NULL); glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBindVertexArray(0); } ///////////////////////// void render_from_FrameBuffer1(){ glBindVertexArray(id_vao); glUseProgram(shaderProgramId); glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glActiveTexture(GL_TEXTURE0 + texUnit); glBindTexture(GL_TEXTURE_2D, texId[3]); // ... glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idbIdx); glDrawElements(type, idx_n, GL_UNSIGNED_SHORT, NULL); glBindVertexArray(0); }