GLSL —— 三种变量类型(uniform,attribute和varying)

1、uniform变量

uniform变量是外部程序传递给(vertex和fragment)shader的变量。因此它是application通过函数glUniform**()函数赋值的。在(vertex和fragment)shader程序内部,uniform变量就像是C语言里面的常量(const ),它不能被shader程序修改。(shader只能用,不能改

如果uniform变量在vertex和fragment两者之间声明方式完全一样,则它可以在vertex和fragment共享使用。(相当于一个被vertex和fragment shader共享的全局变量)

uniform变量一般用来表示:变换矩阵,材质,光照参数和颜色等信息。

以下是例子:

uniform mat4 viewProjMatrix; //投影+视图矩阵

uniform mat4 viewMatrix;        //视图矩阵

uniform vec3 lightPosition;     //光源位置

uniform float lumaThreshold;

uniform float chromaThreshold;

uniform sampler2D SamplerY;

uniform sampler2D SamplerUV;

uniform mat3 colorConversionMatrix;

2、attribute变量

attribute变量是只能在vertex shader中使用的变量。(它不能在fragment shader中声明attribute变量,也不能被fragment shader中使用)

一般用attribute变量来表示一些顶点的数据,如:顶点坐标,法线,纹理坐标,顶点颜色等。

在application中,一般用函数glBindAttribLocation()来绑定每个attribute变量的位置,然后用函数glVertexAttribPointer()为每个attribute变量赋值。

以下是例子:

attribute vec4 position;

attribute vec2 texCoord;

uniform float preferredRotation;

varying vec2 texCoordVarying;

void main()

{

mat4 rotationMatrix = mat4( cos(preferredRotation), -sin(preferredRotation), 0.0, 0.0,

sin(preferredRotation),  cos(preferredRotation), 0.0, 0.0,

0.0,     0.0, 1.0, 0.0,

0.0,     0.0, 0.0, 1.0);

gl_Position = position * rotationMatrix;

texCoordVarying = texCoord;

}

3、varying变量

varying变量是vertex和fragment shader之间做数据传递用的。一般vertex shader修改varying变量的值,然后fragment shader使用该varying变量的值。因此varying变量在vertex和fragment shader二者之间的声明必须是一致的。application不能使用此变量。

以下是例子:

// Vertex shader  

attribute vec4 position;

attribute vec2 texCoord;

uniform float preferredRotation;

varying vec2 texCoordVarying;   // Varying in vertex shader

void main()

{

mat4 rotationMatrix = mat4( cos(preferredRotation), -sin(preferredRotation), 0.0, 0.0,

sin(preferredRotation),  cos(preferredRotation), 0.0, 0.0,

0.0,     0.0, 1.0, 0.0,

0.0,     0.0, 0.0, 1.0);

gl_Position = position * rotationMatrix;

texCoordVarying = texCoord;

}

// Fragment shader

varying highp vec2 texCoordVarying;  // Varying in fragment shader

precision mediump float;

uniform float lumaThreshold;

uniform float chromaThreshold;

uniform sampler2D SamplerY;

uniform sampler2D SamplerUV;

uniform mat3 colorConversionMatrix;

void main()

{

mediump vec3 yuv;

lowp vec3 rgb;

// Subtract constants to map the video range start at 0

yuv.x = (texture2D(SamplerY, texCoordVarying).r - (16.0/255.0))* lumaThreshold;

yuv.yz = (texture2D(SamplerUV, texCoordVarying).rg - vec2(0.5, 0.5))* chromaThreshold;

rgb = colorConversionMatrix * yuv;

gl_FragColor = vec4(rgb,1);

}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

summer_du

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值