PDA

View Full Version : iOS Rendering a object to a texture



poppinmc
07-26-2016, 04:56 PM
I'm currently using OpenGL ES 2.0 and trying to draw a triangle into a texture and then paste that texture onto the face of another triangle. I was wondering what the steps might be to do this? The process I have right now is to configure shaders and setup my main framebuffer/renderbuffer, then setup my texture, and then try to draw that all together at the end. I cleared my texture-framebuffer to a green color and tried to draw on it, but at the final result I only have a green surface to my object and not a green surface plus another smaller triangle in the face of the larger triangle that I'm applying the texture to. Any possible leads?

john_connor
07-27-2016, 05:24 AM
I'm currently using OpenGL ES 2.0 and trying to draw a triangle into a texture and then paste that texture onto the face of another triangle. I was wondering what the steps might be to do this?

i think you cant simply use the texture framebuffer to read from and at the same time write into
i would use 2 framebuffers, both with 1 texture
first render anything into framebuffer 1
then bind framebuffer 2, bind the texture from framebuffer 1 so that you can sample from it in you fragment shader
then render your triangle into framebuffer 2
finally, bind framebuffer 0 (default framebuffer) and copy the content from your 2nd framebuffer into the default framebuffer

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);// specify the framebuffer to draw into
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer2);// specify the framebuffer to read from
glReadBuffer(GL_COLOR_ATTACHMENT0);// specify the color buffer to read from
glBlitFramebuffer(...);// copy framebuffer content

poppinmc
07-27-2016, 04:53 PM
i think you cant simply use the texture framebuffer to read from and at the same time write into
i would use 2 framebuffers, both with 1 texture
first render anything into framebuffer 1
then bind framebuffer 2, bind the texture from framebuffer 1 so that you can sample from it in you fragment shader
then render your triangle into framebuffer 2
finally, bind framebuffer 0 (default framebuffer) and copy the content from your 2nd framebuffer into the default framebuffer

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);// specify the framebuffer to draw into
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer2);// specify the framebuffer to read from
glReadBuffer(GL_COLOR_ATTACHMENT0);// specify the color buffer to read from
glBlitFramebuffer(...);// copy framebuffer content

Hmm sorry I think I might've misworded my approach. So currently I'm using two different framebuffers, one for rendering to a texture, and one for rendering to a renderbuffer (which I'll refer to from here on as texture-framebuffer and render-framebuffer respectively). So what I'm trying to do is draw a triangle into the texture-framebuffer, and also draw a another different triangle into the render-framebuffer. I want to use the texture as an application onto the render-framebuffer, but I haven't been able to get anything to appear on that framebuffer except for a green color which I used to clear the texture-framebuffer with. Also in OpenGL ES 2.0, I don't have access to glBlitFramebuffer(), so I'm not really sure what other approaches there might be to do this. Ultimately I'm trying to divide the rendering process so that I can draw my object of interest into the texture-framebuffer on a thread, and then render that texture onto some surface in another thread to make drawing a less hefty process (as a personal project).