我需要一个简单聚光灯着色器的帮助。 锥体内的所有顶点都应为黄色,锥体外的所有顶点应为黑色。 我只是不能得到它的工作。我认为它与从世界到眼坐标的转变有关。简单GLSL聚光灯着色器
顶点着色器:
uniform vec4 lightPositionOC; // in object coordinates
uniform vec3 spotDirectionOC; // in object coordinates
uniform float spotCutoff; // in degrees
void main(void)
{
vec3 lightPosition;
vec3 spotDirection;
vec3 lightDirection;
float angle;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
// Transforms light position and direction into eye coordinates
lightPosition = (lightPositionOC * gl_ModelViewMatrix).xyz;
spotDirection = normalize(spotDirectionOC * gl_NormalMatrix);
// Calculates the light vector (vector from light position to vertex)
vec4 vertex = gl_ModelViewMatrix * gl_Vertex;
lightDirection = normalize(vertex.xyz - lightPosition.xyz);
// Calculates the angle between the spot light direction vector and the light vector
angle = dot(normalize(spotDirection),
-normalize(lightDirection));
angle = max(angle,0);
// Test whether vertex is located in the cone
if(angle > radians(spotCutoff))
gl_FrontColor = vec4(1,1,0,1); // lit (yellow)
else
gl_FrontColor = vec4(0,0,0,1); // unlit(black)
}
片段着色器:
void main(void)
{
gl_FragColor = gl_Color;
}
编辑:
蒂姆是正确的。这
if(angle > radians(spotCutoff))
应该是:
if(acos(angle) < radians(spotCutoff))
新问题:
的光似乎并没有留在现场的固定位置,而似乎是相对于移动当我向前或向后移动时,作为锥体的相机变得更小或更大。
哪里是你的'#version'指令? – genpfault
而不是设置gl_FrontColor我只是将一个颜色值作为变量传递给片段着色器。再次,当我写GLSL 1.20时,通常只是为了向后兼容GLSL 3.30+着色器,并且我以同样的方式做事。我仍然认为它与gl_FrontColor有关,但看看这个问题:http://stackoverflow.com/questions/6430154/what-is-the-relationship-between-gl-color-and-gl-frontcolor-in-both -vertex-and -f –