Opengl 如何调整我的GLSL代码以添加其他光源?
在下面的代码中,我尝试为Phong实现一个片段着色器程序:Opengl 如何调整我的GLSL代码以添加其他光源?,opengl,glsl,shader,fragment-shader,light,Opengl,Glsl,Shader,Fragment Shader,Light,在下面的代码中,我尝试为Phong实现一个片段着色器程序: // Inputs from application. // Generally, "in" like the position and normal vectors for things that change frequently, // and "uniform" for things that change less often (think scene versus vertices).
// Inputs from application.
// Generally, "in" like the position and normal vectors for things that change frequently,
// and "uniform" for things that change less often (think scene versus vertices).
in vec3 position_cam, normal_cam;
uniform mat4 view_mat;
// This light setup would usually be passed in from the application.
vec3 light_position_world = vec3 (10.0, 25.0, 10.0);
vec3 Ls = vec3 (1.0, 1.0, 1.0); // neutral, full specular color of light
vec3 Ld = vec3 (0.8, 0.8, 0.8); // neutral, lessened diffuse light color of light
vec3 La = vec3 (0.12, 0.12, 0.12); // ambient color of light - just a bit more than dk gray bg
// Surface reflectance properties for Phong model below.
vec3 Ks = vec3 (1.0, 1.0, 1.0); // fully reflect specular light
vec3 Kd = vec3 (0.32, 0.18, 0.5); // purple diffuse surface reflectance
vec3 Ka = vec3 (1.0, 1.0, 1.0); // fully reflect ambient light
float specular_exponent = 400.0; // specular 'power' -- controls "roll-off"
// Shader programs can also designate outputs.
out vec4 fragment_color; // color of surface to draw in this case
void main ()
{
fragment_color = vec4 (Kd, 1.0);
}
我有两个问题:
在glsl中,可以使用数组和结构。定义光源阵列。见和:
const int no_of_lights=2;
结构TLightSource
{
vec3-lightPos;
vec3ls;
vec3-Ld;
vec3-La;
浮光;
};
t光源光源[无灯]=t光源[无灯](
t光源(vec3(10.0,25.0,10.0),vec3(1.0,1.0,1.0),vec3(0.8,0.8,0.8),vec3(0.12,0.12,0.12),10.0),
t光源(vec3(-10.0,25.0,10.0),vec3(1.0,0.0,0.0),vec3(0.8,0.0,0.0),vec3(0.12,0.0,0.0),10.0)
);
用户循环遍历光源,并总结环境光、漫反射光和镜面反射光的灯光颜色(例如:
void main()
{
vec3-normalInterp;
vec3-vertPos;
vec3 normal=标准化(normalInterp);
vec3颜色=vec3(0.0);
对于(int i=0;i0.0)
{
vec3 viewDir=标准化(-vertPos);
vec3 reflectDir=反射(-lightDir,正常);
浮点RdotV=最大值(点(反射方向,视图方向),0.0);
浮光镜面反射=pow(RdotV,光源[i].光泽度/4.0);
颜色+=镜面反射*光源[i].Ls;
}
}
frag_color=vec4(颜色,1.0);
}
在glsl中,可以使用数组和结构。定义光源阵列。见和:
const int no_of_lights=2;
结构TLightSource
{
vec3-lightPos;
vec3ls;
vec3-Ld;
vec3-La;
浮光;
};
t光源光源[无灯]=t光源[无灯](
t光源(vec3(10.0,25.0,10.0),vec3(1.0,1.0,1.0),vec3(0.8,0.8,0.8),vec3(0.12,0.12,0.12),10.0),
t光源(vec3(-10.0,25.0,10.0),vec3(1.0,0.0,0.0),vec3(0.8,0.0,0.0),vec3(0.12,0.0,0.0),10.0)
);
用户循环遍历光源,并总结环境光、漫反射光和镜面反射光的灯光颜色(例如:
void main()
{
vec3-normalInterp;
vec3-vertPos;
vec3 normal=标准化(normalInterp);
vec3颜色=vec3(0.0);
对于(int i=0;i0.0)
{
vec3 viewDir=标准化(-vertPos);
vec3 reflectDir=反射(-lightDir,正常);
浮点RdotV=最大值(点(反射方向,视图方向),0.0);
浮光镜面反射=pow(RdotV,光源[i].光泽度/4.0);
颜色+=镜面反射*光源[i].Ls;
}
}
frag_color=vec4(颜色,1.0);
}
在glsl中,可以使用数组和循环。将数据存储在一个数据库中。但这对我有什么帮助?我不太明白。如果我要添加更多光源,难道我不需要简单地添加更多的光变量,比如灯光位置世界吗?是的,但是使用一个结构数组。ssbo可以更容易地更新变量。请举例说明如何实现该功能?在glsl中,可以使用数组和循环。将数据存储在一个数据库中。但这对我有什么帮助?我不太明白。如果我要添加更多光源,难道我不需要简单地添加更多的光变量,比如灯光位置世界吗?是的,但是使用一个结构数组。ssbo使更新variablesCan变得更容易。请举例说明如何实现?