OpenGL ES 2.0 笔记 - 着色语言的内置函数

来源:互联网 发布:cocos2d-js webgl 编辑:程序博客网 时间:2024/06/06 01:00

着色语言的内置函数

  • 简单的数学函数
    abs(求模) floor(取整)

角度转换与三角函数

内置函数签名 说明 genType radians(genType degrees) 此函数功能将角度转换为弧度 genType degrees(genType radians) 此函数功能将弧度转换为角度 genType sin(genType angle) 此函数为标准的正弦函数,返回值范围[-1, 1],单位:弧度 genType cos(genType angle) 此函数为标准的余弦函数,返回值范围[-1,1],单位:弧度 genType tan(genType angle) 此函数为标准的正切函数,返回值单位:弧度 genType asin(genType x) 此函数为反正弦函数,返回值是[-π2,π2], x的取值范围[-1,1] genType acos(genType x) 此函数为标准反余弦函数,返回值范围[-π,π], x的取值范围[-1,1] genType atan(genType y, genType x) 此函数为标准的反正切函数,其返回值是[-π,π] genType atan(genType y_over_x) 此函数为反正切函数,返回值范围[-π2,π2]

特别说明:上述表格中genType代表的数据类型有float,vec2,vec3以及vec4.float指的是浮点数标量.

指数函数

内置函数签名 说明 genType pow(genType x, genType y) 此函数返回x的y次方xy genType exp(genType x) 此函数返回e(数学常数,值近似2.718281828)的x次方,即ex genType log(genType x) 返回logex genType exp2(genType x) 返回2x genType log2(genType x) 返回log2x genType sqrt(genType x) 返回x的平方根,x genType inversesqrt(genType x) 返回x正平方根的倒数,即1x

常见函数

内置函数签名 说明 genType abs(genType x) 返回x绝对值 genType sign(genType x) sign(x)=1.0,0,1.0,if x > 0if x = 0if x < 0 genType floor(genType x) 返回小于或等于x的最大的整数值 genType ceil(genType x) 返回大于或等于x的最小整数值 genType fract(genType x) 返回xfloor(x)的值 genType mod(genType x, float y) 取模运算 genType mod(genType x, genType y) 取模运算 genType min(genType x, genType y) 取x,y间最小值 genType max(genType x, genType y) 取x,y间最大值 genType clamp(genType x, genType minVal, genType maxVal) 返回min(max(x,minVal),maxVal) genType mix(genType x,genType y, genType a) 功能使用因子a对x与y执行线性混合,即返回x*(1-a)+y*a genType step(genType edge, genType x) 如果x < edge,返回0.0,否则返回1.0 genType smoothstep(genType edge0, genType edge1, genType x) 待研究

几何函数

内置函数签名 说明 float distance(genType p0, genType p1) 返回p0与p1之间的距离. float dot(genType x, genType y) 返回向量x,y的点积即ni=0xiyi vec3 cross(vec3 x, vec3 y) 此函数返回向量x,y的叉积x[1]y[2]y[1]x[2]x[2]y[0]y[2]x[0]x[0]y[1]y[0]x[1] genType normalize(genType x) 此函数返回与向量x方向相同,并且长度为1的向量. genType faceforward(genType N, genType I, genType Nref) 待研究 genType reflect(genType I, genType N) 根据入射向量I,表面法向量N,返回反射方向的向量 genType refract(genType I, genType N, float eta) 根据入射向量I,表面法向量N,以及折射系数eta,返回折射向量

- cross函数
此函数的功能返回两个向量x与y的叉积,两个向量叉积的绝对值即为这两个向量所在四边形的面积.

矩阵函数

内置函数签名 说明 mat matrixCompMult(mat x, mat y) 按各个部分将矩阵x与矩阵y相乘,即返回值矩阵中result[i][j]=x[i][j]*y[i][j]

向量关系函数

内置函数签名 说明 bvec lessThan(vec x, vec y)
bvec lessThan(ivec x, ivec y) 此函数返回向量x与y中,各个分量执行x < y的结果 bvec lessThanEqual(vec x, vec y)
bvec lessThanEqual(ivec x, ivec y) 此函数返回向量x,y各个分量执行x <= y的结果 bvec greaterThan(vec x, vec y)
bvec greaterThan(ivec x, ivec y) 此函数返回向量x,y各个分量执行x > y的结果 bvec greaterThanEqual(vec x, vec y)
bvec greaterThanEqual(ivec x, ivec y) 此函数功能返回向量x,y中各个分量执行x >= y的结果 bvec equal(vec x, vec y)
bvec equal(ivec x, ivec y)
bvec equal(bvec x, bvec y) 此函数返回向量x与y中各个分量执行x == y的结果 bvec notEqual(vec x, vec y)
bvec notEqual(ivec x, ivec y)
bvec notEqual(bvec x, bvec y) 此函数返回向量x,y各个分量x != y 的结果 bool any(bvec x) 如果x中任何一个分量为true,则返回true bool all(bvec x) x中所有分量都为true,则返回true bvec not(bvec x) 对于x的各个分量执行的逻辑非运算

说明:上述表格中的vec代表的数据类型有vec2,vec3,vec4; ivec代表的数据类型有ivec2,ivec3,ivec4.以及bvec代表的数据类型bvec2,bvec3,bvec4.

纹理采样函数

内置函数签名 说明 vec4 texture2D(sampler2D sampler, vec2 coord, [float bias])
vec4 texture2DProj(sampler2D sampler, vec3 coord, [float bias]
vec4 texture2DProj(sampler2D sampler, vec4 coord, [float bias]
vec4 texture2DLod(sampler2D sampler, vec2 coord, float lod)
vec4 texture2DProjLod(sampler2D sampler, vec3 coord, float lod)
vec4 texture2DProjLod(sampler2D sampler, vec4 coord, float lod) 此系列函数功能为使用二维纹理坐标coord在由sampler参数指定的2D纹理中执行纹理采样.对于带有Proj后缀的函数,会将纹理坐标(coord.s, coord.t)除以coord的最后一个部分(coord.q).对于类型为vec4的coord.忽略其第三部分(coord.p) vec4 textureCube(samplerCube sampler, vec3 coord)
vec4 textureCube(samplerCube sampler, vec3 coord, float bias)
vec4 textureCubeLod(samplerCube sampler, vec3 coord, float lod) 此函数功能为使用纹理坐标coord在由sampler参数指定的立方图纹理中执行纹理采样.此函数中coord不再是直接的纹理坐标,而是一个表示方向的向量,通过此向量搜首先确定要采样的面,再确定面里面的纹理坐标 vec4 texture3D(sampler3D sampler, vec3 coord, [float bias]) OpenGL ES 2.0 未支持

1. bias参数
含有bias参数的纹理采样函数只能在片元着色器中调用,且此参数仅对sampler为mipmap类型的纹理时才有意义.
2. Lod后缀系列
带有”Lod”后缀的纹理采样函数仅适用于顶点着色器.此系列函数仅对sampler为mipmap类型的纹理有意义.
3. texture3D系列纹理采样函数在标准的OpenGL ES 2.0 中还没有支持.

微分函数

内置函数签名 说明 genType dFdx(genType p) 此函数返回参数p在x方向的偏导数 genType dFdy(genType p) 此函数返回参数p在y方向的偏导数 genType fwidth(genType p) 此函数返回参数p在x,y方向偏导数的绝对值之和

开发人员可以根据需要选择偏重高渲染质量还是高渲染速度

GLES20.glHint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, XXXXX);//XXXXX 代表某选项的参数的值
0 0
原创粉丝点击