使用的着色器:http://www.gamerendering.com/2008/12/20/radial-blur-filter/
我的问题是:整个场景只占据屏幕空间的四分之一(这是这些坐标的矩形:宽度/ 2,0宽度,高度/ 2),它似乎已被字面修剪(没有缩放)。它也用Y反转呈现它(但是我能够通过从第9行的-
中删除gl_Position.y
符号来解决这个问题。
我使用this tutorial进行后处理,但使用基本glBegin(GL_QUADS)
代替顶点数组除外。
编辑2 :我尝试过使用其他着色器(http://www.geeks3d.com/20110428/shader-library-swirl-post-processing-filter-in-glsl/),但效果非常好。没有裁剪。
编辑:以下是着色器的来源(我做了一些小编辑):
片段着色器:
#version 140
// This texture should hold the image to blur.
// This can perhaps be a previous rendered scene.
uniform sampler2D tex;
// texture coordinates passed from the vertex shader
varying vec2 uv;
// some const, tweak for best look
const float sampleDist = 1.0;
const float sampleStrength = 20.2;
void main(void)
{
// some sample positions
float samples[10] =
float[](-0.08,-0.05,-0.03,-0.02,-0.01,0.01,0.02,0.03,0.05,0.08);
// 0.5,0.5 is the center of the screen
// so substracting uv from it will result in
// a vector pointing to the middle of the screen
vec2 dir = 0.5 - uv;
// calculate the distance to the center of the screen
float dist = sqrt(dir.x*dir.x + dir.y*dir.y);
// normalize the direction (reuse the distance)
//dir = dir/dist;
dir /= dist;
// this is the original colour of this fragment
// using only this would result in a nonblurred version
vec4 color = texture2D(tex,uv);
vec4 sum = color;
// take 10 additional blur samples in the direction towards
// the center of the screen
for (int i = 0; i < 10; i++)
{
sum += texture2D( tex, uv + dir * samples[i] * sampleDist );
}
// we have taken eleven samples
sum *= 1.0/11.0;
// weighten the blur effect with the distance to the
// center of the screen ( further out is blurred more)
float t = dist * sampleStrength;
t = clamp( t ,0.0,1.0); //0 <= t <= 1
//Blend the original color with the averaged pixels
gl_FragColor = mix( color, sum, t );
}
顶点着色器:
#version 140
varying vec2 uv;
// this vertex shader is from AMD RenderMonkey
void main(void)
{
gl_Position = vec4( gl_Vertex.xy, 0.0, 1.0 );
gl_Position = sign( gl_Position );
// Texture coordinate for screen aligned (in correct range):
uv = (vec2( gl_Position.x, gl_Position.y ) + vec2(1.0 )) / vec2( 2.0 );
}
发表处理器:
#include "std/opengl.h"
#include "postprocess.h"
Post::Post(Pos2D res) {
this->res = res;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &this->texture);
glBindTexture(GL_TEXTURE_2D, this->texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, res.x, res.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glGenRenderbuffers(1, &this->rbo);
glBindRenderbuffer(GL_RENDERBUFFER, this->rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, res.x, res.y);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glGenFramebuffers(1, &this->fbo);
glBindFramebuffer(GL_FRAMEBUFFER, this->fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->texture, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, this->rbo);
GLenum status;
if ((status = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) {
fprintf(stderr, "Framebuffer error! %i", status);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Post::~Post() {
glDeleteRenderbuffers(1, &this->rbo);
glDeleteTextures(1, &this->texture);
glDeleteFramebuffers(1, &this->fbo);
}
void Post::update(Pos2D res) {
this->res = res;
glBindTexture(GL_TEXTURE_2D, this->texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, res.x, res.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glBindRenderbuffer(GL_RENDERBUFFER, this->rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, res.x, res.y);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
void Post::bind() {
glBindFramebuffer(GL_FRAMEBUFFER, this->fbo);
}
void Post::unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Post::render(Shader* shader) {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
shader->run();
auto tex = glGetUniformLocation(shader->program_handle, "tex");
glBindTexture(GL_TEXTURE_2D, this->texture);
glUniform1i(tex, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(0, 0);
glTexCoord2f(0.0, 1.0);
glVertex2f(0, this->res.y);
glTexCoord2f(1.0, 1.0);
glVertex2f(this->res.x, this->res.y);
glTexCoord2f(1.0, 0.0);
glVertex2f(this->res.x, 0);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
}
问题的屏幕截图(如您所见,它已被切断):
我禁用着色器时的屏幕截图(如您所见,它可以渲染到整个窗口,它没有被剪裁):
答案 0 :(得分:4)
问题来自你定义顶点坐标的方式:
glTexCoord2f(0.0, 0.0);
glVertex2f(0, 0);
glTexCoord2f(0.0, 1.0);
glVertex2f(0, this->res.y);
glTexCoord2f(1.0, 1.0);
glVertex2f(this->res.x, this->res.y);
glTexCoord2f(1.0, 0.0);
glVertex2f(this->res.x, 0);
而不是使用窗口分辨率作为顶点坐标,尝试使用以下顶点绘制四边形:( - 1,-1),( - 1,1),(1,1)和(1,-1) - 这将导致opengl的原生坐标系统中的全屏四边形。在你的顶点着色器中,它看起来像你试图使用sign函数将顶点坐标转换为这个坐标系:
gl_Position = vec4( gl_Vertex.xy, 0.0, 1.0 );
gl_Position = sign( gl_Position );
但是当然你会得到一个带有角点(0,0),(0,1),(1,1)和(1,0)的四边形,这就解释了为什么它只填充部分屏幕。 因此,您在着色器中使用此行计算的纹理坐标:
uv = (vec2( gl_Position.x, gl_Position.y ) + vec2(1.0 )) / vec2( 2.0 );
也是错误的,导致你正在经历的“剪裁”外观 - 你的texcoords范围从0.5到1,而不是0到1.顺便说一句,你明确地用glTexCoord2f定义了texcoords,并且那些是正确的 - 所以不需要在着色器中再次计算它们,只需使用内置的gl_MultiTexcoord。