分享

SL的一些有用的基础信息

 Tech-d 2013-05-31

==============================================================================
全局变量 和 局部变量
一些在sufrace或者displacement shaders中的全局变量,除非有标注,否则它们是read-only的:
point                                    你正在shading的点的位置。改变这个值会导致表面的displacement;

Normal N                                在P处的表面shading的法线。改变N会导致bump mapping;

normal  Ng                              在P处真正的表面法线。Ng跟N是不一样的,N可能被很多中方法覆盖,
                                                 比如bump mapping以及由user提供vertex normals,但是Ng永远都是真正的表面法线;

vector   I                                  入射向量,从viewing的位置到正在shading的P位置

color Cs, Os                            默认的初始表面颜色和透明度。

float u, v                                   P的2D 参数坐标(parametric coordinate)。

float s, t                                    P的2D 材质坐标(texturing coordinate),这俩值的初始值可能等于u和v,
                                                 但是有很多的机制可以覆盖初始值。

vector dPdu, dPdv                 表面两个方向在P处的偏导数
                                               (不同轴向上的切线斜率,函数P(u, v)中,u和v同时为变量)

float du, dv                              当从sample到sample时,对于表面参数u和v的改变量的估计
                                              (变化率,斜率,即导数,理解做df/du, df/dv, 注意跟偏导数的意义的区别在于一次只有一个变量)

vector L, color Cl                    光到表面的距离 L,光的颜色Cl这些变量包括从光源过来的信息,
                                                 只能通过内部的illuminance循环获取

time                                         当前 shading sample的时间

color Ci, Oi                             在点P处的最终的表面颜色和透明度。
                                                设置这两个参数值就是shader的最终和主要目的                                     

这些全局变量包含一份渲染器用来shader一个点的某个基本信息,例如 位置,表面朝向,默认表面颜色等。你不需要声明这些变量,直接用就可以了。局部变量则需要你自己声明了,在SL文件中普通局部变量公式是:
[class] type variablename [ = initializer];
class: uniform 或 varying,默认为varying。数组的定义方式为:
         type variablename[ arraylen] [= {init0, init1, init 2, ...}];

而在rib文件中:
Declare "Kd" "float"
Declare "stripecolor" "color"
Surface "pitted" "Kd" [0.8] "stripecolor" [0.2, 0.3, 0.8]

================================================================
Statements:
vector a = vector (0, 1, 1);
vector b = vector (1, 0, 0);
vector c = a ^ b;
float d = a.b;

“ ^ ”是叉乘, “ . ”是点乘这两个符号只对vector或者normal有效。
“ * ” 和 “ / ” 是matrix唯二能用的操作符。

==============================================================
循环:
while 和 for两种

string functions:
printf(string template, ...) 
%f, %c, %m, %p, %s:  float, color, point-like, matrix, string

string concat(string s1, ..., sn)
连接s1到sn,返回连接后的string

float match(string pattern, subject)
查找pattern是否存在于subject中,如果存在,返回1;否则返回0

matrix functions:
float determinant(matrix m)
返回m的行列式,即| m |

matrix translate(matrix m; point t)
matrix rotate(matrix m; float angle; vector axis)
matrix scale(uniform matrix m; uniform point t)
返回对m做了简单转换之后生成的矩阵。这些函数跟rib中的translate,Rotate, Scale命令相似,除了rotate中接收的是弧度值而不是角度值。


Geometric Functions:
float xcomp(ptype p)
float ycomp(ptype p)
float zcomp(ptype p)
float comp(ptype p, i)
返回x,y,z或者第i个元素的值。

void setxcomp(output ptype p; float x)
void setycomp(output ptype p; float y)
void setzcomp(output ptype p; float z)
void setcomp(output ptype p; float i, x)
设置x,y,z或者第i个元素的值

float length(vector V / normal V)

float distance(point P0, P1)

float ptlined(Point P0, P1, Q)
返回从Q到由P0P1组成的线上最近的一点的距离

vector reflect(vector I, N)
对于入射角I以及表面法线N,返回一个反射方向R = I - 2 * (N.I) * N
注意,N必须做过标准化了。

vector refract(vector I, N; float eta)
index of refraction:折射率
对于入射角I以及表面发现N,返回一个用Snell's law计算出来的折射方向。eta指的是一个比例,即发出光线I的物质的折射率 / 被射入的物质的折射率

=================================================
SL functions 的关键字:
e.g.
float myfunc(float f; output float g;)
{
...
}
output是一个关键字,一般的参数你不能改变它的值,但是加了output的参数则可以,注意这一点与某些语言是不一样的。

float x, y;
float myfunc(float f)
{
       float x;                        /* local hides the one in the outer scope*/
       extern float y;             /* refers to the y in the outer scope*/
       extern point P;           /* refers to the global P*/
}
如上,想要local functions能够调用非local的参数或者变量,需要用到extern 关键字。



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多