常用Cg函数

数学函数

abs(x):绝对值

// float类型的实现
float abs(float x) {
return max(-a, a);
}

sin(x):正弦,输入为弧度

// float类型的实现
float sin(float a) {
/* C simulation gives a max absolute error of less than 1.8e-7 */
float4 c0 = float4( 0.0, 0.5,
1.0, 0.0 );
float4 c1 = float4( 0.25, -9.0,
0.75, 0.159154943091 );
float4 c2 = float4( 24.9808039603, -24.9808039603,
-60.1458091736, 60.1458091736 );
float4 c3 = float4( 85.4537887573, -85.4537887573,
-64.9393539429, 64.9393539429 );
float4 c4 = float4( 19.7392082214, -19.7392082214,
-1.0, 1.0 ); /* r0.x = sin(a) */
float3 r0, r1, r2; r1.x = c1.w * a - c1.x; // only difference from cos!
r1.y = frac( r1.x ); // and extract fraction
r2.x = (float) ( r1.y < c1.x ); // range check: 0.0 to 0.25
r2.yz = (float2) ( r1.yy >= c1.yz ); // range check: 0.75 to 1.0
r2.y = dot( r2, c4.zwz ); // range check: 0.25 to 0.75
r0 = c0.xyz - r1.yyy; // range centering
r0 = r0 * r0;
r1 = c2.xyx * r0 + c2.zwz; // start power series
r1 = r1 * r0 + c3.xyx;
r1 = r1 * r0 + c3.zwz;
r1 = r1 * r0 + c4.xyx;
r1 = r1 * r0 + c4.zwz;
r0.x = dot( r1, -r2 ); // range extract return r0.x;
}

cos(x):正弦,输入为弧度

// float类型的实现
float cos(float a) {
/* C simulation gives a max absolute error of less than 1.8e-7 */
const float4 c0 = float4( 0.0, 0.5,
1.0, 0.0 );
const float4 c1 = float4( 0.25, -9.0,
0.75, 0.159154943091 );
const float4 c2 = float4( 24.9808039603, -24.9808039603,
-60.1458091736, 60.1458091736 );
const float4 c3 = float4( 85.4537887573, -85.4537887573,
-64.9393539429, 64.9393539429 );
const float4 c4 = float4( 19.7392082214, -19.7392082214,
-1.0, 1.0 ); /* r0.x = cos(a) */
float3 r0, r1, r2; r1.x = c1.w * a; // normalize input
r1.y = frac( r1.x ); // and extract fraction
r2.x = (float) ( r1.y < c1.x ); // range check: 0.0 to 0.25
r2.yz = (float2) ( r1.yy >= c1.yz ); // range check: 0.75 to 1.0
r2.y = dot( r2, c4.zwz ); // range check: 0.25 to 0.75
r0 = c0.xyz - r1.yyy; // range centering
r0 = r0 * r0;
r1 = c2.xyx * r0 + c2.zwz; // start power series
r1 = r1 * r0 + c3.xyx;
r1 = r1 * r0 + c3.zwz;
r1 = r1 * r0 + c4.xyx;
r1 = r1 * r0 + c4.zwz;
r0.x = dot( r1, -r2 ); // range extract return r0.x;
}

sincos(x,out s,out c):s=sin(x),c=cos(x)

ceil(x):向上取整

floor(x):向下取整(floor(-1.3)= -2)

round(x):四舍五入

frac(x):取x的小数部分

clamp(x,a,b):把x截取到[a,b]

saturate(x):把x截取到[0,1]

lerp(a,b,f):(1-f) * a + f * b

step(a,x):返回x>=a

smoothstep(min,max,x):x=min时返回0,x=max时返回1;否则返回下式的值

\[-2*\left(x-min\over max-min\right)^3+3*\left(x-min\over max-min\right)^2
\]

pow(x,y):计算x的y次方

sqrt(x):计算x的算术平方根

noise(x):返回根据x生成的伪随机数,范围[0,1]

min(a,b):取最小

max(a,b):取最大

normalize(x):把x化为单位向量

length(x):返回向量x的模

distance(x,y):计算x,y的欧氏距离

dot(a,b):点积

cross(a,b):叉积

mul(a,b):乘法

光照函数

光照函数的输入向量都必须归一化,入射光方向均指从外指向顶点的方向

reflect(I,N):反射函数,I为入射光向量,N为反射表面的法向量,返回反射光向量

float3 reflect(float3 i, float3 n) {
return i - 2.0 * n * dot(n, i);
}

refract(I,N,eta):折射函数,I为入射光向量,N为反射表面的法向量,eta是介质折射率,返回折射光向量

float3 refract(float3 i, float3 n, float eta) {
float cosi = dot(-i, n);
float cost2 = 1.0f - eta * eta * (1.0f - cosi * cosi);
float3 t = eta * i + ((eta * cosi - sqrt(abs(cost2))) * n);
return t * (float3)(cost2 > 0);
}

纹理采样函数

tex2D(sampler2D samp,float2 s):samp是待采样的纹理,s是纹理坐标

Extra:Cg Math Functions的更多相关文章

  1. NeHe OpenGL教程 第四十七课:CG顶点脚本

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  2. 转载:使用Math.floor和Math.random取随机整数

    Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...

  3. Java-API-Package:java.math

    ylbtech-Java-API-Package:java.math 1.返回顶部 1. Package java.math Provides classes for performing arbit ...

  4. Java-API:java.math.BigDecimal

    ylbtech-Java-API:java.math.BigDecimal 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://docs.ora ...

  5. Java报错:java.math.BigDecimal cannot be cast to java.lang.String

    从数据库取数字,转为string,报错: java.math.BigDecimal cannot be cast to java.lang.String 错误代码 Integer.parseInt(( ...

  6. 浏览器端-W3School-JavaScript:JavaScript Math 对象

    ylbtech-浏览器端-W3School-JavaScript:JavaScript Math 对象 1.返回顶部 1. Math 对象 Math 对象用于执行数学任务. 使用 Math 的属性和方 ...

  7. 《理解 ES6》阅读整理:函数(Functions)(七)Block-Level Functions

    块级函数(Block-Level Functions) 在ES3及以前,在块内声明一个函数会报语法错误,但是所有的浏览器都支持块级函数.不幸的是,每个浏览器在支持块级函数方面都有一些细微的不同的行为. ...

  8. 《理解 ES6》阅读整理:函数(Functions)(六)Purpose of Functions

    明确函数的双重作用(Clarifying the Dual Purpose of Functions) 在ES5及更早的ES版本中,函数调用时是否使用new会有不同的作用.当使用new时,函数内的th ...

  9. 《理解 ES6》阅读整理:函数(Functions)(四)Arrow Functions

    箭头函数(Arrow Functions) 就像名字所说那样,箭头函数使用箭头(=>)来定义函数.与传统函数相比,箭头函数在多个地方表现不一样. 箭头函数语法(Arrow Function Sy ...

随机推荐

  1. mvc5 源码解析1:UrlRoutingModule

    注册在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG \webconfig中 在该module源码中 我们可以看出注册了application ...

  2. Python基础18

    “为什么有列表,还要元组?” 1. 元组可看成是简单的对象组合,而列表是随时间改变的数据集合. 2. 元组的不可变特性提供了某种完整性,确保元组不会被另一个引用来修改.类似于其它语言中的常数声明.

  3. Error: Opening Robot Framework log failed on mac jenkins

    For resolve your problem you must : Connect on your jenkins url (http://[IP]:8080/) Click on Manage ...

  4. elasticsearch 多字段聚合或者对字段子串聚合

    以下是字段子串聚合,截取 'your_field' 前八位进行聚合的 Script script = new Script("doc['your_field'].getValue().sub ...

  5. [TCP/IP] TCP报文长度是由什么确定的

    MTU:最大传输单元,以太网的MTU为1500Bytes MSS:最大分解大小,为每次TCP数据包每次传输的最大数据的分段大小,由发送端通知接收端,发送大于MTU就会被分片 TCP最小数据长度为146 ...

  6. NLP中的预训练语言模型(五)—— ELECTRA

    这是一篇还在双盲审的论文,不过看了之后感觉作者真的是很有创新能力,ELECTRA可以看作是开辟了一条新的预训练的道路,模型不但提高了计算效率,加快模型的收敛速度,而且在参数很小也表现的非常好. 论文: ...

  7. 笔记13:Python 和 Elasticsearch 构建简易搜索

    Python 和 Elasticsearch 构建简易搜索 1 ES基本介绍 概念介绍 Elasticsearch是一个基于Lucene库的搜索引擎.它提供了一个分布式.支持多租户的全文搜索引擎,它可 ...

  8. Rust中的测试用例的写法

    有点类似 #[derive(Debug)] pub struct Rectangle { length: u32, width: u32, } impl Rectangle { pub fn can_ ...

  9. C语言的常量

    #include<stdio.h> int main(void) { ; //定义一个常量,不能被修改,可以赋初值:常量的标识符建议使用大写字母 ; //初始化 printf(" ...

  10. re 模块详解

    1.re 模块 regex 1.查找 :findall 意思"匹配所有,每一项都是列表的组成元素" 有返回值 import re ret=re.findall("\d+& ...