OpenGL学习- 12.着色器内建变量、常量、函数

1,573 阅读3分钟

12.着色器内建变量、常量、函数

顶点着色器内建变量

内建变量作用精度
gl_VertexID当前被处理的顶点的索引highp
gl_InstanceID当前被渲染的实例编号highp
gl_Position输出顶点位置(如果在 vertex shader 中没有写入 gl_Position,那么它的值是未定义的)highp
gl_PointSize指定栅格化点的直径highp
gl_FrontFacing内建只读变量,根据顶点位置和图元类型自动生成,如果属于一个当前图元,那么这个值就为true

顶点着色器内建Uniform

深度范围

struct gl_DepthRangeParameters {
highp float near; //near z 
highp float far; //near far 
highp float diff; //far - near
}
uniform gl_DepthRangeParameters gl_DepthRange;

顶点着色器内建常量

//顶点数据的最大同时输入数量
const mediump int gl_MaxVertexAttribs = 16;
//最多使用的Uniform数量
const mediump int gl_MaxVertexUniformVectors = 256; 
//最大同时输出顶点数据数量
const mediump int gl_MaxVertexOutputVectors = 16;
//最大纹理单元数量
const mediump int gl_MaxVertexTextureImageUnits = 16; 
//顶点加片元着色器中可用纹理总和的最大值
const mediump int gl_MaxCombinedTextureImageUnits = 32;

片元着色器内建变量

内建变量作用
gl_FragCoord内建只读变量,它保存了片元相对窗口的坐标位置:x, y, z, 1/w处理的顶点的索引
gl_FrontFacing内建只读变量,如果当前片段是正向面的一部分那么就是true,否则就是false
gl_PointCoord内建只读变量,它的值是当前片元所在点图元的二维坐标
gl_FragDepth内建,深度值

片元着色器内建常量

//片元最大输入顶点数
const mediump int gl_MaxFragmentInputVectors = 15; 
//最大纹理数量
const mediump int gl_MaxTextureImageUnits = 16;
//最大Uniform数量
const mediump int gl_MaxFragmentUniformVectors = 224; 
//多重渲染最大值
const mediump int gl_MaxDrawBuffers = 4;

多纹理渲染

客户端代码: 将各个纹理理对象绑定到纹理理单元0和1,为采样器器设置数 值,将采集器器绑定到对应的纹理理单元

glActiveTexutre(GL_TEXTURE0); 
glBindTeture(GL_TEXTURE_2D ,baseMapTexId); 
glUniformli(baseMapTexId,0);
glActiveTexutre(GL_TEXTURE1); 
glBindTeture(GL_TEXTURE_2D ,secondMapTexId);
glUniformli(secondMapTexId,1);

片元着色器代码:

attribute vec2 v_texCoord;
uniform sampler2D s_baseMap; 
uniform sampler2D s_SecondMap; 
void main()
{
  vec4 baseColor;
  vec4 secondColor;
  baseColor = texture(s_baseMap ,v_texCoord); 
  secondColor = texture(s_SecondMap ,v_texCoord);
  gl_FragColor = baseColor * secondColor; 
}

内建函数

角度和三角函数

 radians函数是将角度转换为弧度,degrees函数是将弧度转换为角度。sin, cos, tan都是标准的三角函数。asin, acos, atan是反三角函数。

指数函数

genType pow (genType x, genType y)x的y次方。如果x小于0,结果是未定义的。同样,如果x=0并且y<=0,结果也是未定义的。使用时应特别注意。
genType exp (genType x)e的x次方
genType log (genType x)计算满足x等于e的y次方的y的值。如果x的值小于0,结果是未定义的。
genType exp2 (genType x)计算2的x次方
genType log2 (genType x)计算满足x等于2的y次方的y的值。如果x的值小于0,结果是未定义的。
genType sqrt (genType x) 计算x的开方。如果x小于0,结果是未定义的。
genType inversesqrt (genType x)计算x的开方之一的值,如果x小于等于0,结果是未定义的。

常用函数 15888208227412.png 15888208339800.png

genType abs (genType x)返回x的绝对值
genType sign (genType x)如果x>0,返回1.0;如果x=0,返回0,如果x<0,返回-1.0
genType floor (genType x)返回小于等于x的最大整数值
genType ceil (genType x)返回大于等于x的最小整数值
genType fract (genType x)返回x-floor(x),即返回x的小数部分
genType mod (genType x, float y)、genType mod (genType x, genType y)返回x – y * floor (x/y),即求模计算%
genType min (genType x, genType y),genType min (genType x, float y)返回x和y的值较小的那个值。
genType max (genType x, genType y),genType max (genType x, float y)返回x和y的值较大的那个值。
genType clamp (genType x, genType minVal, genType maxVal)、genType clamp (genType x, float minVal, float maxVal)获取x和minVal之间较大的那个值,然后再拿较大的那个值和最后那个最大的值进行比较然后获取较小的那个,clamp实际上是获得三个参数中大小处在中间的那个值。函数有个说明:如果minVal > minMax的话,函数返回的结果是未定的。也就是说x的值大小没有限制,但是minval的值必须比maxVal小。
genType mix (genType x, genType y, genType a)、genType mix (genType x, genType y, float a)返回线性混合的x和y,如:x⋅(1−a)+y⋅a
genType step (genType edge, genType x),genType step (float edge, genType x)如果x < edge,返回0.0,否则返回1.0
genType smoothstep (genType edge0,genType edge1,genType x),genType smoothstep (float edge0,float edge1,genType x)如果x <= edge0,返回0.0 ;如果x >= edge1 返回1.0;如果edge0 < x < edge1,则执行0~1之间的平滑埃尔米特差值。如果edge0 >= edge1,结果是未定义的。

几何函数 15888211400490.png

float length (genType x)返回向量x的长度
float distance (genType p0, genType p1)计算向量p0,p1之间的距离
float dot (genType x, genType y)向量x,y之间的点乘
vec3 cross (vec3 x, vec3 y)叉乘
genType normalize (genType x)标准化向量,返回一个方向和x相同但长度为1的向量
genType faceforward(genType N, genType I, genType Nref)如果Nref和I的点积小于0,返回N;否则,返回-N
genType reflect (genType I, genType N)返回反射向量
genType refract(genType I, genType N,float eta)返回折射向量

矩阵函数 15888212647196.png

mat matrixCompMult (mat x, mat y)矩阵x乘以y,result[i][j]是 x[i][j] 和 y[i][j] 的标量积。注意,要获取线性代数矩阵的乘法,使用乘法操作符(*)。

向量相关函数 15888213332805.png

lessThan:比较x < y
lessThanEqual:比较x<=y
greaterThan:比较x>y
greaterThanEqual:比较x>=y
equal:比较x==y
notEqual:比较x!=y
bool any(bvec x):如果向量x的任何组件为true,则结果返回true
bool all(bvec x):如果向量x的所有组件均为true,则结果返回true
bvec not(bvec x):返回向量x的互补矩阵