【问题标题】:Per-pixel lighting black spots/weird artifacts每像素照明黑点/奇怪的伪影
【发布时间】:2012-12-10 20:12:31
【问题描述】:

好吧,几天来我一直在努力实现每像素照明,这基本上是我通常最终得到的“结果”。

我的网格中都有这些硬黑点,还有那些不平衡的黑点。蓝色阴影“有点”工作正常,除了它在整个网格中应用自身,并且它似乎随机应用自身,如图所示。当我将灯光绑定到我的相机时,灯光确实“穿过”网格,尽管大部分时间都很奇怪。我不知道为什么会这样;据我所知,我的网格数据的法线很好(在 MilkShape、3DS、Lightwave、Blender、Maya 等中没有颜色/平滑问题)。

这是我的设置/灯光代码:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
float lpos[4] = {get_cam_pos().x,get_cam_pos().y,get_cam_pos().z,1};
float lamb[4] = {0,0,1,1};
float ldiff[4] = {1,1,0,1};
float lspec[4] = {1,1,0.5,1};
GLfloat lin[4] = {5.0f,5.0f,5.0f,1};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glLightfv(GL_LIGHT0, GL_AMBIENT, lamb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, ldiff);
glLightfv(GL_LIGHT0, GL_SPECULAR, lspec);
    <from here camera matrix is then loaded and scene is rendered>

这是我的垂直着色器,来自 Lighthouse3D 每像素光教程:

varying vec4 diffuse,ambientGlobal,ambient, ecPos;
varying vec3 normal,halfVector;
varying float dist;

void main()
{   
    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

    /* first transform the normal into eye space and normalize the result */
    normal = normalize(gl_NormalMatrix * gl_Normal);

    /* compute the vertex position  in camera space. */
    ecPos = gl_ModelViewMatrix * gl_Vertex;

    /* Normalize the halfVector to pass it to the fragment shader */
    halfVector = gl_LightSource[0].halfVector.xyz;

    /* Compute the diffuse, ambient and globalAmbient terms */
    diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
    ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
    ambientGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient;


    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
} 

...还有片段着色器,同样来自 Lighthouse3D 教程:

uniform sampler2D color_texture;
varying vec4 diffuse,ambientGlobal, ambient, ecPos;
varying vec3 normal,halfVector;
varying float dist;

void main()
{
    vec4 tex0Color = vec4(texture2D(color_texture,gl_TexCoord[0].st));
    vec3 n,halfV,viewV,lightDir;
    float NdotL,NdotHV;
    vec4 color = ambientGlobal;
    float att;

    /* a fragment shader can't write a verying variable, hence we need
    a new variable to store the normalized interpolated normal */
    n = normalize(normal);

    // Compute the ligt direction
    lightDir = vec3(gl_LightSource[0].position-ecPos);

    /* compute the distance to the light source to a varying variable*/
    dist = length(lightDir);


    /* compute the dot product between normal and ldir */
    NdotL = max(dot(n,normalize(lightDir)),0.0);

    if (NdotL > 0.0) {

        att = 1.0 / (gl_LightSource[0].constantAttenuation +
            gl_LightSource[0].linearAttenuation * dist +
            gl_LightSource[0].quadraticAttenuation * dist * dist);
                               color += att * (diffuse * NdotL + ambient);


        halfV = normalize(halfVector);
        NdotHV = max(dot(n,halfV),0.0);
        color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular 
                               * pow(NdotHV,gl_FrontMaterial.shininess);
     }

     gl_FragColor = tex0Color*color;
 }

【问题讨论】:

  • 看起来你的法线对我来说是错误的......
  • 为什么dist 是一个可变变量,因为它没有在顶点着色器中使用?我认为这与您的问题无关,只是想知道。
  • 我同意@Goz - 将您的法线渲染为线条并检查它们是否正常。
  • 作为将法线渲染为线条的替代方法,更改着色器中的最终颜色分配以可视化每个像素的法线或着色器中的任何其他值。
  • 啊哈!那成功了。显然我的法线正在爆炸并且完全不正确。我想我实际上必须计算它们,然后看看会发生什么。

标签: c++ opengl glsl


【解决方案1】:

你的法线对我来说看起来不对...

;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 2021-06-11
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 2020-11-10
    相关资源
    最近更新 更多