开发者

How to disable or configure clipping when rendering to texture using FBO in OpenGL?

I have a question about the way the FBO clips the visible scene. All this happens in iPhone's OpenGL ES.

I'm rendering in 2D and using render-to-texture with FBO.

I find that when any part of the scene passes any side of the rectangle of screen size (480*320) that it appears clipped from the FBO generated texture.

Why does it do that? How do I disable or configure it?

I've tried disabling scissor test by

lDisable(GL_SCISSOR_TEST);

I've tried to set up needed section by

`glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, 100, 100);`

I've tried to adjust Viewport by

glViewport(0,0, 512,512);

it doesn't work as needed. Instead of extending the render area to needed 512*512 it looks like it still clips the area to 480*320 and stretches it afterwards to 512*512

All these didn't bring needed result.

In my code: I create the texture

GLuint textureID;   
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, nil);`

Create FBO and attach texture to it

GLuint textureFrameBuffer;
GLint prevFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &prevFBO);

// create framebuffer
glGe开发者_开发技巧nFramebuffersOES(1, &textureFrameBuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer);

// attach renderbuffer
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, textureID, 0);

if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
    NSLog(@"ERROR - WHILE CREATING FBO");

// unbind frame buffer
glBindFramebufferOES(GL_FRAMEBUFFER_OES, prevFBO);

And then, when I draw some stuff to FBO and render the square with this texture,

static const float textCoords[] = 
{
    0, 1,
    1, 1,
    0, 0,
    1, 0,
};

static const float quadVerts[] = 
{
    0, 320,
    320, 320,
    0, 0,
    320, 0,
};
glEnable(GL_TEXTURE_2D);    
glBindTexture(GL_TEXTURE_2D, textureID);    
glVertexPointer(2, GL_FLOAT, 0, quadVerts);
glTexCoordPointer(2, GL_FLOAT, 0, textCoords);      
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

To my opinion I should see the square filled with all the stuff I had drawn to my FBO. Whereas, I only see a part of it – clipped rectangle of size 480x320 at lower left corner. So, the question is steel actual


You need to change your viewport doing:

glViewport(0,0,textureWidth,textureHeight);

EDIT: And don't forget to switch it back to its normal value after rendering onto your FBO.

/// save viewport
GLint vp[4];
glGetIntegerv(GL_VIEWPORT, vp);

/// restore viewport
glViewport(vp[0], vp[1], vp[2], vp[3]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜