简体   繁体   English

OpenGL FrameBuffer和glViewport

[英]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. 我正在通过渲染传递到FrameBufferObject,以便我可以在下一个渲染过程中将结果用作纹理。 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. 其中mWidth和mHeight是帧缓冲区对象的宽度和高度。 It seems without this glViewport call, nothing gets drawn correctly. 似乎没有这个glViewport调用,没有任何东西被正确绘制。 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). 我尝试设置断点并查看宽度和高度值,它们是原始值(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. 我是通过xcode和glew在Mac上做的。

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. 因此,如果您更改它,它将保持新值,直到您再次调用glViewport 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. 正如您已经指出的那样,每当您想要渲染到FBO(或后备缓冲区)时,您必须设置大小,该FBO的大小与先前绑定的大小不同。

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 请参阅glScissor的参考链接或其他帖子解释其目的https://gamedev.stackexchange.com/questions/40704/what-is-the-purpose-of-glscissor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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