【发布时间】:2013-06-19 10:05:44
【问题描述】:
我在我的引擎中实现了一个新的渲染管道,现在渲染已损坏。当我直接将 G-Buffer 的纹理绘制到屏幕上时,它会正确显示。所以G-Buffer很好。但不知何故,照明通过会带来麻烦。即使我不使用生成的纹理,而是尝试在光照通过后从 G-Buffer 显示反照率,它也会显示为纯灰色。
我无法解释这种行为,奇怪的是在任何时候都没有 OpenGL 错误。
顶点着色器绘制全屏四边形。
#version 330
in vec4 vertex;
out vec2 coord;
void main()
{
coord = vertex.xy;
gl_Position = vertex * 2.0 - 1.0;
}
用于照明的片段着色器。
#version 330
in vec2 coord;
out vec3 image;
uniform int type = 0;
uniform sampler2D positions;
uniform sampler2D normals;
uniform vec3 light;
uniform vec3 color;
uniform float radius;
uniform float intensity = 1.0;
void main()
{
if(type == 0) // directional light
{
vec3 normal = texture2D(normals, coord).xyz;
float fraction = max(dot(normalize(light), normal) / 2.0 + 0.5, 0);
image = intensity * color * fraction;
}
else if(type == 1) // point light
{
vec3 pixel = texture2D(positions, coord).xyz;
vec3 normal = texture2D(normals, coord).xyz;
float dist = max(distance(pixel, light), 1);
float magnitude = 1 / pow(dist / radius + 1, 2);
float cutoff = 0.4;
float attenuation = clamp((magnitude - cutoff) / (1 - cutoff), 0, 1);
float fraction = clamp(dot(normalize(light - pixel), normal), -1, 1);
image = intensity * color * attenuation * max(fraction, 0.2);
}
}
照明通道的目标和采样器。纹理 ID 分别映射到附件着色器位置。
unordered_map<GLenum, GLuint> targets;
targets.insert(make_pair(GL_COLOR_ATTACHMENT2, ...)); // light
targets.insert(make_pair(GL_DEPTH_STENCIL_ATTACHMENT, ...)); // depth and stencil
unordered_map<string, GLuint> samplers;
samplers.insert(make_pair("positions", ...)); // positions from G-Buffer
samplers.insert(make_pair("normals", ...)); // normals from G-Buffer
光照通道的绘制函数。
void DrawLights(unordered_map<string, GLuint> Samplers, GLuint Program)
{
auto lis = Entity->Get<Light>();
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glUseProgram(Program);
int n = 0; for(auto i : Samplers)
{
glActiveTexture(GL_TEXTURE0 + n);
glBindTexture(GL_TEXTURE_2D, i.second);
glUniform1i(glGetUniformLocation(Program, i.first.c_str()), n);
n++;
}
mat4 view = Entity->Get<Camera>(*Global->Get<unsigned int>("camera"))->View;
for(auto i : lis)
{
int type = i.second->Type == Light::DIRECTIONAL ? 0 : 1;
vec3 pos = vec3(view * vec4(Entity->Get<Form>(i.first)->Position(), !type ? 0 : 1));
glUniform1i(glGetUniformLocation(Program, "type"), type);
glUniform3f(glGetUniformLocation(Program, "light"), pos.x, pos.y, pos.z);
glUniform3f(glGetUniformLocation(Program, "color"), i.second->Color.x, i.second->Color.y, i.second->Color.z);
glUniform1f(glGetUniformLocation(Program, "radius"), i.second->Radius);
glUniform1f(glGetUniformLocation(Program, "intensity"), i.second->Intensity);
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(1, 0);
glVertex2i(1, 1);
glVertex2i(0, 1);
glEnd();
}
glDisable(GL_BLEND);
glActiveTexture(GL_TEXTURE0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
【问题讨论】:
-
您在此处调用
glDrawBuffers,那么您如何知道要渲染到哪个附件?为您的片段着色器发布输出部分。另外,纹理附件的确切内部格式、格式和类型是什么? -
片段着色器使用
layout(location = 0)等写入附加到GL_COLOR_ATTACHMENT0等的目标。对于 G-Buffer 通道,有GL_RGB16FGL_RGBGL_FLOAT类型的三个纹理和GL_DEPTH24_STENCIL8GL_DEPTH_STENCILGL_UNSIGNED_INT_24_8类型之一。我可以说深度和模板工作,但不是其他三个目标。根据glGetFramebufferAttachmentParameteriv,所有目标都正确附加。 -
好吧,鉴于写入 G-Buffer 的是着色器,我想可能需要发布它们以获得更多信息。
-
@danijar “片段着色器使用
layout(location = 0)等写入附加到GL_COLOR_ATTACHMENT0等的目标。” - 这不太正确尽管。location指定要写入的绘图缓冲区(因此location=0将是数组中提供给glDrawBuffers的第一个附件)并且如果您的目标不包含GL_COLOR_ATTACHMENTi连续没有间隙,那么这两件事就赢了不一样。也许这就是你的错误的原因? -
如何检查反照率和法线纹理是否为零?您是否有可能正在对尚未生成的 mipmap 级别进行采样?
标签: c++ opengl rendering framebuffer