OpenGL着色器:聚光灯和定向光 [英] OpenGL shader: a spotlight and a directional light

查看:341
本文介绍了OpenGL着色器:聚光灯和定向光的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要两个光源:定向光源和聚光灯.我似乎无法理解我做错了什么-可能不了解着色器的工作原理!我得到的第一个光源很好,但是没有第二个效果的迹象(又称聚光灯).这是我想出的碎片着色器:

I want to have two light sources: a directional one and a spotlight. I cannot seem to get what I am doing wrong -- probably not understanding how shaders work! I get the first light fine but no sign of the effects of the second one (aka spotlight). Here is the fragement shader that I came up with:

varying vec4 diffuse,ambientGlobal, ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;


void main()
{
    vec3 n, halfV, viewV, ldir;
    float NdotL, NdotHV;
    vec4 color = ambientGlobal;
    float att, spotEffect;
    n = normalize(normal);

    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);

        spotEffect = dot(normalize(gl_LightSource[1].spotDirection), normalize(-lightDir));
        if (spotEffect > gl_LightSource[1].spotCosCutoff) {
            spotEffect = pow(spotEffect, gl_LightSource[1].spotExponent);
            att = spotEffect / (gl_LightSource[1].constantAttenuation +
                    gl_LightSource[1].linearAttenuation * dist +
                    gl_LightSource[1].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[1].specular * pow(NdotHV,gl_FrontMaterial.shininess);
        }

    }

    gl_FragColor = color;
}

PS :当然,这是一个已解决的问题....有人吗?

PS: Surely this is a problem that has been solved.... Anyone?

推荐答案

这是我想出的:

顶点着色器:

varying vec3 N;
varying vec3 v;
void main(void)  
{     
   v = vec3(gl_ModelViewMatrix * gl_Vertex);       
   N = normalize(gl_NormalMatrix * gl_Normal);
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
}

还有片段着色器:

varying vec3 N;
varying vec3 v;
#define MAX_LIGHTS 2 
void main (void)  
{  
   vec4 finalColour;

   for (int i=0; i<MAX_LIGHTS; i++)
   {
       vec3 L = normalize(gl_LightSource[i].position.xyz - v);   
       vec3 E = normalize(-v);
       vec3 R = normalize(-reflect(L,N));  
       vec4 Iamb = gl_FrontLightProduct[i].ambient;    
       vec4 Idiff = gl_FrontLightProduct[i].diffuse * max(dot(N,L), 0.0);
       Idiff = clamp(Idiff, 0.0, 1.0);     
       vec4 Ispec = gl_FrontLightProduct[i].specular 
                    * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
       Ispec = clamp(Ispec, 0.0, 1.0);
       finalColour += Iamb + Idiff + Ispec;
   }
   gl_FragColor = gl_FrontLightModelProduct.sceneColor + finalColour;
}

提供这张图片:

这篇关于OpenGL着色器:聚光灯和定向光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆