为什么在OpenGL 2.0中使用GLSL时我的灯不能正常工作?

首先,这是一个单一的任务,但讲师也难倒。

光(附在鼠标上)只有在物体内部(在这种情况下是一个球体)时才起作用。 我试着把球体的法线乘以-1,光线与球体的距离也乘以-1。 我不知道这是什么原因。

这里是片段着色器:

const float lightWeighting = 0.75; const float textureWeighting = 0.25; const float PI = 3.14; const float lightIntensity = 1.0; const float constFudge = 0.025; const float linearFudge = 0.025; const float quadraticFudge = 0.05; float rend = 250.0; float rstart = 50.0; uniform float Intensity; uniform sampler2D grabTexture; //varying sampler2D HeatValues varying vec3 Normal; varying vec3 Vertex; varying vec2 texCoord; void main(void) { vec3 pigPos = Vertex; vec3 normPigPos = normalize(pigPos); vec3 lightPos = vec3(gl_LightSource[0].position.xyz); vec3 normLightPos = normalize(lightPos); float effectiveIntesity; vec3 normNormal = normalize(vec3(Normal.xyz)); vec3 t = vec3(pigPos - lightPos); float distToLight = length(t); //I belive this is where it is going wrong float d = dot(normNormal.xyz, normLightPos.xyz); if(d > 0.0)//facing the light { effectiveIntesity = d*lightIntensity * (1.0/(constFudge + (linearFudge*distToLight) +(quadraticFudge*distToLight*distToLight))); } else { effectiveIntesity = 0.0; } //TODO: change the colour of the pixel vec4 lightingColour = vec4(vec4( 1.0 ) * effectiveIntesity); //TODO: improve gl_FragColor = lightingColour; } 

顶点着色器按要求:

 //I am surprisingly important uniform float Intensity; varying vec3 Normal; varying vec3 Vertex; varying vec2 texCoord; void main(void) { gl_Position = ftransform(); texCoord = gl_MultiTexCoord0.xy; Normal = vec3(gl_Normal.xyz); Vertex = vec3(gl_Vertex.xyz); } 

法线是从用搅拌器生产的obj文件加载的

从光计算的距离是问题。

我仍然从对象的内部获得照明,但是我并没有为此而烦恼。

这里是片段着色器的变化:

 const float lightWeighting = 0.75; const float textureWeighting = 0.25; const float PI = 3.14; const float lightIntensity = 1.0; const float constFudge = 0.025; const float linearFudge = 0.025; const float quadraticFudge = 0.05; float rend = 250.0; float rstart = 50.0; uniform float Intensity; uniform sampler2D grabTexture; //varying sampler2D HeatValues varying vec3 Normal; varying vec3 Vertex; varying vec2 texCoord; void main(void) { vec3 pigPos = Vertex; vec3 normPigPos = normalize(pigPos); vec3 lightPos = vec3(gl_LightSource[0].position.xyz); vec3 normLightPos = normalize(lightPos); float effectiveIntesity; vec3 normNormal = normalize(vec3(Normal.xyz)); vec3 t = vec3(pigPos - lightPos); //////////////////////////// // THE FIX //////////////////////////// float distToLight = length(t)/100; //the divide by 100 fixed it (I probably need to make the light brighter) //////////////////////////// // END OF FIX //////////////////////////// float d = dot(normNormal.xyz, normLightPos.xyz); if(d > 0.0)//facing the light { effectiveIntesity = d*lightIntensity * (1.0/(constFudge + (linearFudge*distToLight) +(quadraticFudge*distToLight*distToLight))); } else { effectiveIntesity = 0.0; } //TODO: change the colour of the pixel vec4 lightingColour = vec4(vec4( 1.0 ) * effectiveIntesity); //TODO: improve gl_FragColor = lightingColour; } 

这可能不是全部问题,但我确实看到一些问题

 float d = dot(normNormal.xyz, normLightPos.xyz); 

normLightPos对我来说看起来毫无意义 – 它是来自世界中心的光的方向

你真正想要的(我认为)是

 float d = dot(normNormal.xyz, normalize(t)); 

比较表面法线与光线的角度。

我也强烈建议尝试与距离/衰减检查隔离 – 如直接使用“d”作为像素的亮度。

当你从这个角度有良好的照明,增加线性亮度衰减 – 然后升级到更复杂的衰减,你正在尝试

这可能有助于testing有多个球体散落在现场,所以你可以看到照明对附近和远处物体的影响