此像素着色器中的漫反射和镜面反射

发布于 2024-10-20 11:19:23 字数 1396 浏览 2 评论 0原文

#version 150
uniform float shade;
in vec3 cshade;
in vec3 v_o;
in vec3 locallight_o;
in vec3 n;
in float shadescale_o;
out vec4 pixelcolour;
void main( )
{
    float shadescale;
    shadescale=shadescale_o;
    vec3 vn,l,ln,nn,h,hh;
    vec3 ambient = vec3(0.4,0.4,0.4);       // ambient light    

   vn=normalize(v_o);
    ln=normalize(locallight_o);
    if (dot(ln,n)<0)
    {
        h=vn-ln;//light direction shines from source to object
        hh=h;
        h=normalize(h);
        nn=normalize(n);
        shadescale= dot(h,nn);//inward normal
        if (shadescale<0)
            shadescale=0;
        shadescale*=shadescale;
        shadescale*=shadescale;
    }
    else
    shadescale=0;

    // Get pixel colour from input shade
    if (shade>=0.5)
    {       
        pixelcolour = vec4( (cshade * shadescale) + (ambient * cshade), 1.0 );  // ambient lighting     

        //pixelcolour = vec4( (cshade*ambient)+ambient, 1.0 );
        //pixelcolour += vec4(ambient, 1.0);
    }
    else
    {
        pixelcolour = vec4( (cshade * shadescale_o) + (ambient * cshade), 1.0 );

        //pixelcolour = vec4( (cshade*ambient)+ambient, 1.0 );      
        //pixelcolour += vec4(ambient, 1.0);

    }               

}

上面的代码是一个直接的像素着色器,在显示立方体的 openGL 框架中使用。目前环境照明已经实现,但是我如何向此代码添加漫反射和镜面反射(当然不是同时!)?我知道我需要一些额外的制服,即 vec3 的漫反射和镜面反射,但我应该执行哪些具体操作?

#version 150
uniform float shade;
in vec3 cshade;
in vec3 v_o;
in vec3 locallight_o;
in vec3 n;
in float shadescale_o;
out vec4 pixelcolour;
void main( )
{
    float shadescale;
    shadescale=shadescale_o;
    vec3 vn,l,ln,nn,h,hh;
    vec3 ambient = vec3(0.4,0.4,0.4);       // ambient light    

   vn=normalize(v_o);
    ln=normalize(locallight_o);
    if (dot(ln,n)<0)
    {
        h=vn-ln;//light direction shines from source to object
        hh=h;
        h=normalize(h);
        nn=normalize(n);
        shadescale= dot(h,nn);//inward normal
        if (shadescale<0)
            shadescale=0;
        shadescale*=shadescale;
        shadescale*=shadescale;
    }
    else
    shadescale=0;

    // Get pixel colour from input shade
    if (shade>=0.5)
    {       
        pixelcolour = vec4( (cshade * shadescale) + (ambient * cshade), 1.0 );  // ambient lighting     

        //pixelcolour = vec4( (cshade*ambient)+ambient, 1.0 );
        //pixelcolour += vec4(ambient, 1.0);
    }
    else
    {
        pixelcolour = vec4( (cshade * shadescale_o) + (ambient * cshade), 1.0 );

        //pixelcolour = vec4( (cshade*ambient)+ambient, 1.0 );      
        //pixelcolour += vec4(ambient, 1.0);

    }               

}

The above code is a straight forward pixel shader, used in an openGL framework that shows a cube. Currently ambient lighting has been implemented, but how do I go about adding diffuse and specular reflection (not simultaneously of course!) to this code? I understand I will need some extra uniforms i.e. vec3's called diffuse and specular, but what exact operations should I perform?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2024-10-27 11:19:23

我不会将代码粘贴到此处,但您所有问题的答案是 http://www.lighthouse3d.com/opengl/glsl/index.php?lights

简而言之,漫反射 = -dot(normal, lightDir)。
为什么?点积评估两个向量的“相同性”,如果它们相同,则结果为 1;如果它们成直角,则结果为 0;如果方向相反,则结果为 -1。如果面的法线直接指向光线(法线和 lightDir 相反),则它应该取最大值。如果光线以一定角度照射,则点积返回的值更接近 0,从而形成最终的照明值。

需要注意的是lightDir和normal必须进行归一化。

I'm not going to paste the code into here, but the answer to all your questions is http://www.lighthouse3d.com/opengl/glsl/index.php?lights.

In a nutshell, diffuse = -dot(normal, lightDir).
Why? Well the dot product evaluates the "sameness" of two vectors, yeilding 1 if they are the same, 0 if they are at right angles, and -1 if they are opposite. If the normal of the face is pointing directly into the light(normal and lightDir are opposite), it should take on the maximum value. If the light is shining in on an angle, the dot product returns a value closer to 0, making the final lighing value next.

It should be noted that lightDir and normal must be normalized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文