How to render to texture in iphone opengl

Hi,

 I am trying to render a scene to an offscreen texture first and then draw this texture to the render buffer. I am using opengl ES 1.1 on XCode 3.1.4 for iphones.. This is what Have I done so far...

In my ES1renderer class declaration in ES1renderer.h

GLuint defaultFramebuffer,bigFrameBuffer, bigColorRenderBuffer,colorRenderbuffer,depthRenderbuffer;
GLuint offscreentexture;

In my init function in ES1Renderer.m

glGenFramebuffersOES(1, &defaultFramebuffer);
glGenRenderbuffersOES(1, &colorRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
		glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
		
		
		
glGenRenderbuffersOES(1, &bigColorRenderBuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, bigColorRenderBuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES,640, 960);
		
		
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT24_OES, 640, 960);
		
		
glGenFramebuffersOES(1,&bigFrameBuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, bigFrameBuffer);
		glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,GL_RENDERBUFFER_OES,bigColorRenderBuffer);
		glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

		
	
glGenTextures(1, &offscreentexture);
glBindTexture(GL_TEXTURE_2D, offscreentexture);
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 640,960, 0,
					 GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,GL_TEXTURE_2D,offscreentexture, 0);
		
GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
		if (status != GL_FRAMEBUFFER_COMPLETE_OES) {
			NSLog(@"Incomplete FBO");
			exit(1);
		}
		
		

And in my render function

[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, bigFrameBuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, bigColorRenderBuffer);
glViewport(0, 0, backingWidth, backingHeight);

//Then I draw my thing here...
glDrawElements(GL_TRIANGLES,countInd, GL_UNSIGNED_SHORT, waterIndices);
I believe whatever scene I have drawn is drawn to the offscreen  texture...Is there anyway to check whether it is indeed drawn into the texture..Plus now I want to render the screen to a render buffer half the size of the buffer allocated above which is 640 * 960 (backingWidth and backingHeight in the above program) .. How can I achieve that...

 Also this is the document which help me get thus far
http://iphone-3d-programming.labs.oreilly.com/ch06.html#ch06_id36002707

 I think whatever I have done upto now is correct...(again is there any way of checking?)..But I am at a roadblock here..I somehow want to render the scene to a texture and then from texture to the screen..What is next think I got to do...

Sure, if you rendered to a texture attached to an FBO, all you need to do is map that texture to a QUAD constructed from a 4 element triangle strip, and draw the QUAD to the screen.

Thanks scratt for the quick reply…I tried to render the texture to a quad on screen but nothing is rendered… Then I did some more research to know the basic of render to texture…And from this tutorial I got some information…They are creating only a frame buffer and a texture is attaching to it and no render buffer.


http://developer.apple.com/iphone/library/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/WorkingwithEAGLContexts/WorkingwithEAGLContexts.html
I opened a new project and try to draw a simple sphere on to the texture using the method given in above link...This is what I have done in my init method..
glGenFramebuffersOES(1, &textureFrameBuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);
		
glGenTextures(1,&frameTexture);
glBindTexture(GL_TEXTURE_2D, frameTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA8_OES, GL_UNSIGNED_BYTE, NULL);
		
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, frameTexture, 0);
		
GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
if(status != GL_FRAMEBUFFER_COMPLETE_OES)
{
    NSLog(@"Failed to make complete frame buffer object for texture frame buffer : %x " , status);
}

But when I check the frame buffer status it is always showing a failure with status value : 8CD6

Error status 8CD6 means : FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT 0x8CD6

courtesy : http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt

I dont know why I am getting this error…Any comments

There is a full example here :

https://devforums.apple.com/message/23282#23282

Thanks scratt…Nice link you gave me…Thanks

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.