shader三种变量类型(uniform,attribute和varying)
原创
©著作权归作者所有:来自51CTO博客作者妙为的原创作品,请联系作者获取转载授权,否则将追究法律责任
学习目标:
uniform变量在vertex和fragment两者之间声明方式完全一样,则它可以在vertex和fragment共享使用。(相当于一个被vertex和fragment shader共享的全局变量)
uniform变量一般用来表示:变换矩阵,材质,光照参数和颜色等信息。
以下是例子:
uniform mat4 viewProjMatrix; //投影+视图矩阵
uniform mat4 viewMatrix; //视图矩阵
uniform vec3 lightPosition; //光源位置
2.attribute变量
attribute变量是只能在vertex shader中使用的变量。(它不能在fragment shader中声明attribute变量,也不能被fragment shader中使用)
一般用attribute变量来表示一些顶点的数据,如:顶点坐标,法线,纹理坐标,顶点颜色等。
在application中,一般用函数glBindAttribLocation()来绑定每个attribute变量的位置,然后用函数glVertexAttribPointer()为每个attribute变量赋值。
以下是例子:
uniform mat4 u_matViewProjection;
attribute vec4 a_position;
attribute vec2 a_texCoord0;
varying vec2 v_texCoord;
void main(void)
{
gl_Position = u_matViewProjection * a_position;
v_texCoord = a_texCoord0;
}
3.varying变量
varying变量是vertex和fragment shader之间做数据传递用的。一般vertex shader修改varying变量的值,然后fragment shader使用该varying变量的值。因此varying变量在vertex和fragment shader二者之间的声明必须是一致的。application不能使用此变量。
以下是例子:
// Vertex shader
uniform mat4 u_matViewProjection;
attribute vec4 a_position;
attribute vec2 a_texCoord0;
varying vec2 v_texCoord; // Varying in vertex shader
void main(void)
{
gl_Position = u_matViewProjection * a_position;
v_texCoord = a_texCoord0;
}
// Fragment shader
precision mediump float;
varying vec2 v_texCoord; // Varying in fragment shader
uniform sampler2D s_baseMap;
uniform sampler2D s_lightMap;
void main()
{
vec4 baseColor;
vec4 lightColor;
baseColor = texture2D(s_baseMap, v_texCoord);
lightColor = texture2D(s_lightMap, v_texCoord);
gl_FragColor = baseColor * (lightColor + 0.25);
}
multilight.vs
#version 430
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
out vec3 Color;
struct LightInfo {
vec4 Position; // Light position in eye coords.
vec3 Intensity; // Light intesity (amb., diff., and spec.)
};
uniform LightInfo lights[5];
// Material parameters
uniform vec3 Kd; // Diffuse reflectivity
uniform vec3 Ka; // Ambient reflectivity
uniform vec3 Ks; // Specular reflectivity
uniform float Shininess; // Specular shininess factor
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 MVP;
vec3 ads(int lightIndex, vec4 position, vec3 normal)
{
vec3 s = normalize(vec3(lights[lightIndex].Position - position));
vec3 v = normalize(vec3(-position));
vec3 r = reflect(-s, normal);
vec3 I = lights[lightIndex].Intensity;
return I * (Ka +
Kd * max(dot(s, normal), 0.0f) +
Ks * pow(max(dot(r, v), 0.0f), Shininess));
}
void main()
{
vec3 eyeNorm = normalize(NormalMatrix * VertexNormal);
vec4 eyePosition = ModelViewMatrix * vec4(VertexPosition, 1.0f);
// Evaluate the lighting equation, for each light
Color = vec3(0.0f);
for(int i=0; i<5; i++) {
Color += ads(i, eyePosition, eyeNorm);
}
gl_Position = MVP * vec4(VertexPosition, 1.0f);
}
multilight.fs
#version 430
in vec3 Color;
layout (location = 0) out vec4 FragColor;
void main()
{
FragColor = vec4(Color, 1.0f);
}
参考
softimagewht