从顶点着色器到片段着色器的光

时间:2012-12-04 17:24:57

标签: java opengl jogl fragment-shader vertex-shader

需要在片段着色器中创建聚光灯以点亮球体。目前我在顶点着色器中有一个点光源,完全不知道如何咬它。

 #version 140
in  vec4 vPosition;

in  vec4 vNormal;

out vec4 color;

// implicit definition: out vec4 gl_Position;

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

uniform vec4 lPosition;

uniform vec4 lAmbient;

uniform vec4 lDiffuse;

uniform vec4 lSpecular;

uniform float Shininess;

uniform vec4 matColor;

void main() 
{
    vec4 ambient, diffuse, specular;
    vec3 N,L,E,H;

    gl_Position = projection * view * model * vPosition;
    ambient = matColor*lAmbient;

    N = normalize((view*model*vNormal).xyz);
    L = normalize((view*model*lPosition).xyz - (view*model*vPosition).xyz);
    diffuse = max(dot(L,N), 0.0)*(matColor*lDiffuse);

    E = -normalize((view * model * vPosition).xyz);
    H = normalize(L + E);
    specular = max(pow(max(dot(N, H), 0.0), Shininess)
                *lSpecular, 0.0);

    color = vec4((ambient+diffuse+specular).rgb, 1.0);
 }

据我所知,聚光灯与点光源不同,它的衰减包含恒定,线性和二次衰减+截止参数?


就我而言:

计算spottlightEffect:

spottEffect = pow(max(dot(spotdirection, gl_Position), 0.0), spotexponent);

计算衰减:

att = (1 / (const_attenuation + linear_attenuation * (view*model*lPosition) + quadratic_attenuation * (view*model*lPosition) * (view*model*lPosition))).xyz;

它应该是正确的,因为我理解正确的文章,但不知道接下来该做什么...添加if语句用于截止?

0 个答案:

没有答案