每像素照明黑点/奇怪的人工制品

时间:2012-12-10 20:12:31

标签: c++ opengl glsl

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

我的网眼中有这些坚硬的黑点,还有那些不合时宜的黑点。蓝色阴影“种类”的工作正常,除了它在整个网格中应用自身,它似乎随机应用,如图中所示。当我将灯光束缚在我的相机上时,光线会在网格上“移动”,尽管大部分时间都是奇怪的。我不知道为什么会这样;我的网格数据有法线,据我所知,它很好(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>

这是我的vert着色器,来自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;
 }

1 个答案:

答案 0 :(得分:2)

你的法线对我来说错了......

;-)