五、GLSL 常用内建函数

OpenGL + OpenGL ES +Metal 系列文章汇总

常用函数

常用函数 说明
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) 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,结果是未定义的。

几何函数

几何函数 说明
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) 向量x,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) 返回折射向量

指数函数

指数函数 说明
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,结果是未定义的。

猜你喜欢

转载自blog.csdn.net/lin1109221208/article/details/108901540
今日推荐