- Home /
Rendering to texture using an FBO on iOS
I'm trying to render to texture but I just haven't been able to get anything to show. Any hints?
In a script attached to a camera I have this:
void Start() {
tex = new Texture2D (512, 512);
NativeCallCreateFBO( tex.GetNativeTextureID() );
}
void OnPostRender () {
NativeCallToRenderToTexture();
Graphics.Blit (tex, texMat, 0);
}
If tex is a plain old Texture2D it displays correctly.
The native code to create the FBO looks like this:
glGenFramebuffers(1, &textureFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, textureFramebuffer);
// attach renderbuffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);
GLuint framebufferCreationStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
NSAssert(framebufferCreationStatus == GL_FRAMEBUFFER_COMPLETE, @"Failure with display framebuffer generation for display" );
// unbind frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
And the code to render to texture looks like this:
glBindFramebuffer(GL_FRAMEBUFFER, textureFramebuffer);
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, inputTextureForDisplay);
glUniform1i(displayInputTextureUniform, 4);
glVertexAttribPointer(displayPositionAttribute, 2, GL_FLOAT, 0, 0, imageVertices);
glVertexAttribPointer(displayTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, noRotationTextureCoordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindRenderbuffer(GL_FRAMEBUFFER, 0);
Of course this is missing all the shader setup, etc. It's a bit too long to post.
Is the glClear (without calling the other render calls) enough to determine if this is working? Is OnPostRender the right place to call the rendering code? Is this approach even viable?
The background story is I have a CVOpenGLESTextureRef that I want to render in Unity. Of course Unity will only render textures it creates. This is my attempt to solve that issue.
Your answer
Follow this Question
Related Questions
Compatibility between ARKit plugin and iVidCapPro 2 Answers
Extremely slow texture upload in plugin when a model is being drawn 1 Answer
Saving Unity Texture2D updated with OpenGL FBO via custom plugin 2 Answers
renderplugin example not working on ios 1 Answer
Issue with glDrawArrays in plugin 2 Answers