此像素着色器中的漫反射和镜面反射
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会将代码粘贴到此处,但您所有问题的答案是 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.