简体   繁体   中英

OpenGL FrameBuffer and glViewport

I am going through a render pass onto a FrameBufferObject such that I can use the result as a texture in the next rendering pass. Looking at tutorials, I need to call:

glBindFramebuffer(GL_TEXTURE2D, mFrameBufferObjectID);
glViewport(0, 0, mWidth, mHeight);

where mWidth and mHeight are the width and height of the frame buffer object. It seems without this glViewport call, nothing gets drawn correctly. What's strange is that upon starting the next frame, I need to call:

glViewport(0, 0, window_width, window_height);

so that I can go back to my previous width/height of the window; but calling it seems to only render my stuff at half of the original window size. So I physically only see a quarter of my screen gets stuff rendered onto (yes the entire scene is on it). I tried putting a break point and looking at the width and heigh values, they are the original values (1024, 640). Why is this happening? I tried doubling those and it correctly draws on my entire window.

I'm doing this on Mac via xcode and with glew.

The viewport settings are stored in the global state. So if you change it, it stays at the new values until you call glViewport again. As you already noted, you'll have to set the size whenever you want to render to a FBO (or backbuffer) that has a different size then the previously bound one.

Try adjusting the scissor box as well as the viewport if you have the scissor test enabled using

glEnable(GL_SCISSOR_TEST);

To fix your problem write

glViewport(0, 0, mWidth, mHeight);
glScissor(0, 0, mWidth, mHeight);

and

glViewport(0, 0, window_width, window_height);
glScissor(0, 0, window_width, window_height);

everytime when you switch to a new framebuffer with a different size as the previous one, or always just to be safe.

See this link of the reference glScissor Or this other post explaining the purpose of it https://gamedev.stackexchange.com/questions/40704/what-is-the-purpose-of-glscissor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM