3

我想在我的游戏中做图像效果,比如屏幕模糊,但我有一个问题,它只是让屏幕稍微变暗。

这是FBO的课程

GLuint m_fbo;
GLuint m_fboTexture;
GLuint m_fboDepthBuffer;

void FBO::Initialize(float width, float height)
{
    glGenFramebuffersEXT(1, &m_fbo);
    glGenTextures(1, &m_fboTexture);
    glGenRenderbuffersEXT(1, &m_fboDepthBuffer);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, m_fboTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glGenerateMipmapEXT(GL_TEXTURE_2D);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, (GLuint)width, (GLuint)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_fboTexture, 0);

    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_fboDepthBuffer);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, (GLuint)width, (GLuint)height);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_fboDepthBuffer);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

    glDisable(GL_TEXTURE_2D);
}

void FBO::Bind()
{
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
}

void FBO::UnBind()
{
   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

void FBO::Clear()
{
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

这就是我在主课中设置它的方式。

FBO m_fbo;//create an object

//initialize size
m_fbo.Initiliazie(screen_width, screen_height);

在我的渲染功能中,我这样做

void Render()
{
   SetShaderProgram();//start shader program

   m_fbo.Bind();

   SetShaderImageSampler("texture", 0, m_fbo.m_fboTexture);
   SetShaderUniformVec2("size", (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT);
   SetShaderUniformInt("kernel_size", ksize);

   //draws my texture
   m_spriteBatch->Draw(RecTangle(0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT));

   m_fbo.UnBind();

   EndShaderProgram();//end shader program
}

这就是 SetShaderImageSampler 函数的作用:

void SetShaderImageSampler(char *sSamplerName, U32 nSlot, GLuint nHandle)
{
    glEnable(GL_TEXTURE_2D);
    glActiveTexture(GL_TEXTURE0 + nSlot);
    glBindTexture(GL_TEXTURE_2D, nHandle);
    U32 nSamplerLoc = glGetUniformLocation(m_shaderProgram, sSamplerName);
    glUniform1iARB(nSamplerLoc, nSlot);
}

这是我的 spritebatch 抽奖

void SpriteBatch::Draw(RecTangle rect)
{
    //draws it depending on its center
    float left = -(rect.right/2.0f);
    float right = left + (rect.right);
    float top = -(rect.bottom/2.0f);
    float bottom = top + (rect.bottom);

    //vertices: Vertex(x, y, z, u, v) coordinates
    m_vertex[0] = Vertex(left,  top,    0, 0, 0);
    m_vertex[1] = Vertex(right, top,    0, 1, 0);
    m_vertex[2] = Vertex(right, bottom, 0, 1, 1);
    m_vertex[3] = Vertex(left,  bottom, 0, 0, 1);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &m_vertex[0].m_fX);

    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &m_vertex[0].m_fU);

    glDrawArrays(GL_QUADS, 0, 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    glColor4f(0.5f, 0.5f, 0.5f, 0.5f);
}

这是我的碎片着色器

uniform sampler2D texture;
uniform vec2 size;
uniform int kernel_size;

void main (void)  
{ 
    vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
    vec2 st = gl_TexCoord[0].st;
    vec2 texel = 1.0/size;

    int sum = 0;
    for(int i=-kernel_size; i <= kernel_size; i++) 
    {
            vec4 value = texture2D(texture, st + vec2(0.0, texel.y * i));
            int factor = kernel_size+1 - abs((float)i);
            color += value * factor;
            sum += factor;
    }
    color /= sum;

    gl_FragColor = color;
    gl_FragColor.a = 1.0;
}

它工作得不太好。我尝试更改着色器来做其他事情,但无论如何它只会稍微变暗。

4

4 回答 4

2

解绑应该是

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
于 2012-10-08T10:47:11.413 回答
0

没关系,我现在可以正常工作了。我在错误的地方调用了 Bind 和 UnBind。

于 2012-10-12T01:25:29.373 回答
0

好的,通常要进行后期处理效果,您会在帧缓冲区处于活动状态时渲染场景。然后在激活后处理(模糊)着色器并将帧缓冲区纹理绑定为采样器的情况下渲染一个全屏四边形。

于 2012-10-10T18:05:51.723 回答
0

也许您没有包含足够的代码,但我没有看到您在 Render 方法中实际渲染任何内容。

你有一些调用,我假设第一个调用是上下文,然后绑定帧缓冲区并链接着色器变量,但接下来我希望调用以某种方式渲染四边形/网格(即使用 glBegin/glEnd 部分或顶点缓冲区绑定和渲染。)

于 2012-10-08T23:05:05.803 回答