在OpenGL中使用FBO渲染到纹理时如何禁用或配置裁剪? [英] How to disable or configure clipping when rendering to texture using FBO in OpenGL?

查看:331
本文介绍了在OpenGL中使用FBO渲染到纹理时如何禁用或配置裁剪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对FBO剪辑可见场景的方式有疑问. 所有这些都发生在iPhone的OpenGL ES中.

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

我正在2D渲染,并在FBO中使用渲染到纹理".

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

我发现,当场景的任何部分通过屏幕尺寸矩形(480 * 320)的矩形的任何一侧时,它看起来都是从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?

我已尝试通过

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);

它无法按需工作.与其将渲染区域扩展到所需的512 * 512,倒不如将其裁剪为480 * 320,然后将其拉伸到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

所有这些均未带来所需的结果.

在我的代码中: 我创建纹理

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);`

创建FBO并为其添加纹理

Create FBO and attach texture to it

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

// create framebuffer
glGenFramebuffersOES(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);

然后,当我在FBO上绘制一些东西并使用此纹理渲染正方形时,

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);

我认为我应该看到广场上充满了我吸引到FBO的所有物品.而我只看到其中的一部分-左下角的大小为480x320的裁剪后的矩形. 所以,问题是钢铁的实际情况

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);

编辑: 并且不要忘记在渲染到FBO之后将其切换回其正常值.

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]);

这篇关于在OpenGL中使用FBO渲染到纹理时如何禁用或配置裁剪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆