我想製作一個自定義淺色着色器,隨着時間的推移嘗試了很多不同的東西。 我發現的一些解決方案效果更好,其他更差。對於這個問題,我使用迄今爲止效果最好的解決方案。爲什麼GLSL照明代碼會使用相機移動光點?
我的問題是,如果我移動「相機」,燈光位置似乎也會移動。這種解決方案有非常輕微但明顯的運動,燈光位置似乎高於應有的位置。
默認OpenGL照明(沒有任何着色器)可以正常工作(穩定的光照位置),但是我需要用於多紋理的着色器,並且我計劃在照明效果工作後使用它的一部分。
頂點來源:
varying vec3 vlp, vn;
void main(void)
{
gl_Position = ftransform();
vn = normalize(gl_NormalMatrix * -gl_Normal);
vlp = normalize(vec3(gl_LightSource[0].position.xyz) - vec3(gl_ModelViewMatrix * -gl_Vertex));
gl_TexCoord[0] = gl_MultiTexCoord0;
}
片段來源:
uniform sampler2D baseTexture;
uniform sampler2D teamTexture;
uniform vec4 teamColor;
varying vec3 vlp, vn;
void main(void)
{
vec4 newColor = texture2D(teamTexture, vec2(gl_TexCoord[0]));
newColor = newColor * teamColor;
float teamBlend = newColor.a;
// mixing the textures and colorizing them. this works, I tested it w/o lighting!
vec4 outColor = mix(texture2D(baseTexture, vec2(gl_TexCoord[0])), newColor, teamBlend);
// apply lighting
outColor *= max(dot(vn, vlp), 0.0);
outColor.a = texture2D(baseTexture, vec2(gl_TexCoord[0])).a;
gl_FragColor = outColor;
}
我在做什麼錯?
幾張圖片甚至是一個Youtube視頻的鏈接都可以幫助我們診斷問題。 –