在我的OpenGL程序中,我注意到黑色像素出現在正向渲染算法中光線混合在一起的地方。起初,我忽略它,直到我將Window API(SDL轉換爲GLFW)轉換爲視覺上有問題時爲止。爲什麼在向前渲染燈時有Z-格鬥?
當我禁用GL_DEPTH_TEST時,黑色的人工製品消失了,但事物變得對它們後面的燈光透明,所以它不是解決方案。 (這是我如何發現潛在的問題)
我不確定問題是什麼,它可能是深度緩衝區,但切換窗口API增強工件,任何幫助?
看來黑線/點被每個三角形內分離
在此處,我混合燈
// Note: I wrapped OpenGL calls into wrapper functions but the naming convenction is still the same
gl::Clear(gl::e_ColorBufferBit | gl::e_DepthBufferBit);
mesh->Render(m_forward_ambient);
gl::Enable(gl::e_Blend);
gl::SetBlendFunc(gl::e_One, gl::e_One); // Additive blending
gl::SetDepthMask(false); // No need to write to depth buffer
gl::SetDepthFunc(gl::e_Equal); // Only draw fragments of equal depths (ignore fragments behind basically)
{
for (word i = 0; i < m_lights.Length(); ++i)
{
m_active_light = m_lights[i];
mesh->Render(m_active_light->shader); // Shaders use active light
}
}
gl::SetDepthFunc(gl::e_Lequal); // Restore default state
gl::SetDepthMask(true);
gl::Disable(gl::e_Blend);
着色器代碼(具有所有代碼的着色器,其他實現更少)
// Vertex
void main()
{
// pv_matrix is projection and camera, ml_matrix is model transform
gl_Position = pv_matrix * ml_matrix * vec4(pos, 1);
f_pos = (ml_matrix * vec4(pos, 1)).xyz;
f_nrm = (ml_matrix * vec4(normalize(nrm), 0)).xyz;
f_txc = txc;
}
// Fragment
struct Light
{
vec3 color;
float intensity;
};
struct Attenuation
{
float constant;
float linear;
float exponent;
};
struct PointLight
{
Light light;
Attenuation atten;
vec3 position;
float range;
};
struct SpotLight
{
PointLight plight;
vec3 direction;
float cutoff;
};
vec4 CalculateLight(Light light, vec3 direction, vec3 normal)
{
float diffuse_factor = dot(normal, -direction);
vec4 diffuse_color = vec4(0, 0, 0, 0);
vec4 specular_color = vec4(0, 0, 0, 0);
if (diffuse_factor > 0)
diffuse_color = vec4(light.color, 1) * light.intensity * diffuse_factor;
vec3 direction_to_eye = normalize(eye_pos - f_pos); // eye_pos is uniform for camera pos, f_pos is (position) attribute sent from vertex shader
vec3 reflect_direction = normalize(reflect(direction, normal));
float specular_factor = dot(reflect_direction, direction_to_eye); // specular calculations
if (specular_factor > 0)
{
specular_factor = pow(specular_factor, specular_power);
specular_color = vec4(light.color, 1) * specular_intensity * specular_factor;
}
return diffuse_color + specular_color;
}
vec4 CalculatePointLight(PointLight plight, vec3 normal)
{
vec3 light_direction = f_pos - plight.position;
float distance = length(light_direction);
if (distance > plight.range)
return vec4(0, 0, 0, 0);
light_direction = normalize(light_direction);
vec4 color = CalculateLight(plight.light, light_direction, normal);
float a = plight.atten.constant + (plight.atten.linear * distance) + (plight.atten.exponent * (distance * distance)) + 0.0001;
return color/a;
}
vec4 CalculateSpotLight(SpotLight slight, vec3 normal)
{
vec3 light_direction = normalize(f_pos - slight.plight.position);
float spot_factor = dot(light_direction, slight.direction);
vec4 color = vec4(0, 0, 0, 0);
if (spot_factor > slight.cutoff)
color = CalculatePointLight(slight.plight, normal) * (1.0 - ((1.0 - spot_factor)/(1.0 - slight.cutoff)));
return color;
}
uniform SpotLight spot_light;
void main()
{
FragColor = CalculateSpotLight(spot_light, f_nrm); // f_nrm is a (normal) attribute sent from vertex shader
}
請提供適當的[MCVE](https://stackoverflow.com/help/mcve)。這仍然是很多未知的事情,尤其是所有與頂點轉換有關的拍打 - 在我看來,這似乎是這個問題最可能出現的地方。 – derhass