1

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

我的网格中都有这些硬黑点,还有那些不平衡的黑点。蓝色阴影“有点”工作正常,除了它在整个网格中应用自身并且它似乎随机应用自身,如图所示。当我将灯光绑定到我的相机时,灯光确实“穿过”网格,尽管大部分时间都很奇怪。我不知道为什么会这样;据我所知,我的网格数据的法线很好(在 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 per-pixel-light 教程:

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;
 }
4

1 回答 1

2

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

;-)

于 2012-12-10T22:53:46.770 回答