2012-12-10 23 views
1

嗯,我一直在摔跤實施每像素照明幾天,這實際上是我通常結果的「結果」。 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

看起來你的法線對我來說是錯誤的... – Goz

+0

爲什麼'dist'是一個變量,因爲它沒有用在頂點着色器中?我不認爲這與你的問題有關,只是想知道。 – JWWalker

+0

我同意@Goz - 將您的法線渲染爲線條並檢查它們是否正常。 – Ani

回答

1

您的法線看起來不對我...

;-)