您的位置:首页 > 编程语言

GLSL编程之GLSL(七)——逐像素光照

2014-08-15 10:24 239 查看
下边第二部分:逐像素点光,我试验出了修改方法,但是至于为什么要这样修改,我并不清楚,希望有大神可以帮忙解答,小女感激不尽!!!

本文参考/article/1945460.html

但经过实验,修正其中部分问题

第一部分:逐像素方向光

由原本代码运行得到的结果为:



主要原因是其片段着色器并未将所有颜色加在一起,最后加上这条语句即可:

color = globalAmbient + NdotL * diffuse + ambient;


以下修改代码也有修改其他成分——

修改其中的顶点着色器为:

varying vec4 diffuse,ambient,globalAmbient ;
varying vec3 normal,lightDir,halfVector;

void main()
{

normal = normalize(gl_NormalMatrix * gl_Normal);
lightDir = normalize(vec3(gl_LightSource[0].position));
halfVector = normalize(gl_LightSource[0].halfVector.xyz);

diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;

gl_Position = ftransform();

}


片段着色器为:

varying vec4 diffuse,ambient,globalAmbient;
varying vec3 normal,lightDir,halfVector;

void main()
{
vec3 n,halfV,viewV,ldir;
float NdotL,NdotHV;
vec4 color ;

/* a fragment shader can't write a verying variable, hence we need
a new variable to store the normalized interpolated normal */
n = normalize(normal);

/* compute the dot product between normal and ldir */
NdotL = max(dot(n,lightDir),0.0);

color = globalAmbient + NdotL * diffuse + ambient;

gl_FragColor = color;
}


修改之后的结果为:



第二部分:逐像素点光

原图:



添加语句即可修改

color = globalAmbient + NdotL * diffuse + ambient;


修改之后的图:



第三部分:逐像素聚光
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: