gpt4 book ai didi

c++ - 高斯模糊永远无法正常工作

转载 作者:行者123 更新时间:2023-11-30 05:00:22 28 4
gpt4 key购买 nike

Image of the problem

出于某种原因,每次我做模糊处理时,结果都是这样,我真的很困惑为什么会这样。对于绽放,模糊都是像素化的,而且每次我做模糊时都会发生这种情况。我真的不知道问题出在哪里。

这是您需要的用于模糊方差阴影贴图的所有代码。

GaussianBlurVertical 片段着色器..

#version 330 core

out vec4 FragColor; //fragment shader output

in vec2 _texcoord; //input interpolated texture coordinate

uniform sampler2D textureMap; //the input image to blur

//constant kernel values for Gaussian smoothing
const float kernel[]=float[21] (0.000272337, 0.00089296, 0.002583865, 0.00659813, 0.014869116,
0.029570767, 0.051898313, 0.080381679, 0.109868729, 0.132526984,
0.14107424, 0.132526984, 0.109868729, 0.080381679, 0.051898313,
0.029570767, 0.014869116, 0.00659813, 0.002583865, 0.00089296, 0.000272337);

void main()
{
//get the inverse of texture size
vec2 delta = 1.0 / textureSize(textureMap,0);
vec4 color = vec4(0);
int index = 20;

//go through all neighbors and multiply the kernel value with the obtained
//colour from the input image
for(int i =- 10; i <= 10; i++) {
color += kernel[index--] * texture(textureMap, _texcoord + (vec2(0, i * delta.y)));
}

//return the filtered colour as fragment output
FragColor = vec4(color.xy,0,0);
}

GuassianBlurHorizo​​ntal 片段着色器..

#version 330 core

out vec4 FragColor; //fragment shader output

in vec2 _texcoord; //input interpolated texture coordinate

//uniform
uniform sampler2D textureMap; //the input image to blur

//constant kernel values for Gaussian smoothing
const float kernel[]=float[21] (0.000272337, 0.00089296, 0.002583865, 0.00659813, 0.014869116,
0.029570767, 0.051898313, 0.080381679, 0.109868729, 0.132526984,
0.14107424, 0.132526984, 0.109868729, 0.080381679, 0.051898313,
0.029570767, 0.014869116, 0.00659813, 0.002583865, 0.00089296, 0.000272337);

void main()
{
//get the inverse of texture size
vec2 delta = 1.0 / textureSize(textureMap, 0);
vec4 color = vec4(0);
int index = 20;

//go through all neighbors and multiply the kernel value with the obtained
//colour from the input image
for(int i =- 10; i <= 10; i++) {
color += kernel[index--] * texture(textureMap, _texcoord + (vec2(i * delta.x, 0)));
}

//return the filtered colour as fragment output
FragColor = vec4(color.xy, 0, 0);
}

模糊阶段..

glBindFramebuffer(GL_FRAMEBUFFER, _filteredshadowmap_fbo);

glUseProgram(_program_blurH);
glUniform1i(_u_texture_map_h, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _shadowmap);
renderQuad();

glUseProgram(_program_blurV);
glUniform1i(_u_texture_map_v, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _shadowmap);
renderQuad();

glBindFramebuffer(GL_FRAMEBUFFER, 0);

初始化模糊 fbo..

// shadowmap blurring
glGenFramebuffers(1, &_filteredshadowmap_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, _filteredshadowmap_fbo);

glGenTextures(1, &_filtered_shadowmap);

glBindTexture(GL_TEXTURE_2D, _filtered_shadowmap);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, SHADOW_QUALITY, SHADOW_QUALITY, 0, GL_RGBA, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _filtered_shadowmap, 0);

GLenum status2 = glCheckFramebufferStatus(GL_FRAMEBUFFER);
status2 != GL_FRAMEBUFFER_COMPLETE ? std::cerr << "Unable to create FBO" << std::endl : std::cout << "FBOs Were successful" << std::endl;

glBindFramebuffer(GL_FRAMEBUFFER, 0);

最佳答案

一般来说,您的高斯模糊着色器可以工作,但您使用它的方式不对。

您问题中的高斯模糊着色器是一个 2 pass 着色器。第 1 遍模糊垂直,第 2 遍模糊水平。
为此,需要 2 个目标纹理。垂直模糊 channel 读取源纹理并写入“中间”纹理。水平模糊读取“中间”纹理并写入目标纹理。

创建 2 个帧缓冲区和 2 个纹理附件,方法与您的问题相同。当然,也可以使用带有 2 个纹理附件的 1 个帧缓冲区,并在 channel 之间切换纹理附件。事实上,一个帧缓冲区的纹理附件也可以在 channel 之间改变。
在我的回答中,我决定创建 2 个帧缓冲区,因为你的问题的代码可以完全重用。

像为 _filteredshadowmap_fbo_filtered_shadowmap 一样创建一个帧缓冲区对象 _intermediateshadowmap_fbo 和一个附加的纹理对象 _intermediate_shadowmap在问题中:

glGenFramebuffers(1, &_intermediateshadowmap_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, _intermediateshadowmap_fbo);

glGenTextures(1, &_intermediate_shadowmap);

.....

并创建第二个 frambuffer 对象 _filteredshadowmap_fbo 和第二个纹理对象 _filtered_shadowmap:

glGenFramebuffers(1, &_filteredshadowmap_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, _filteredshadowmap_fbo);

glGenTextures(1, &_filtered_shadowmap);

.....

要创建模糊纹理,必须完成以下操作:

  • 设置纹理的视口(viewport)大小
  • 绑定(bind) frambuffer _intermediateshadowmap_fbo
  • 使用源纹理 _shadowmap 进行垂直模糊处理
  • 绑定(bind) frambuffer _filteredshadowmap_fbo
  • 使用源纹理 _intermediate_shadowmap 进行垂直模糊处理
  • 绑定(bind)默认帧缓冲区
  • 设置默认帧缓冲区(窗口)的视口(viewport)大小


glViewport( 0, 0, SHADOW_QUALITY, SHADOW_QUALITY );
glBindFramebuffer(GL_FRAMEBUFFER, _intermediateshadowmap_fbo);

glUseProgram(_program_blurH);
glUniform1i(_u_texture_map_h, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _shadowmap);
renderQuad();

glBindFramebuffer(GL_FRAMEBUFFER, _filteredshadowmap_fbo);

glUseProgram(_program_blurV);
glUniform1i(_u_texture_map_v, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _intermediate_shadowmap);
renderQuad();

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, window_width, window_height);

关于c++ - 高斯模糊永远无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50841270/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com