#
// test less and equal zero
// x小于,等于0返回0,x大于0返回1
// if x <= 0 return 0 and x > 0 return 1.
float LQZ(x){
return max(0, sign(x));
// return ceil(clamp(0.,1.,x));
}
// if x <= 0 return a and x > 0 return b
// x小于,等于0返回a,x大于0返回b
float v = mix(a, b, LQZ(x));
// if x is odd number (0~1,2~3,4~5,6~7) return a else return b
// 奇數(shù)段(0~1,2~3,4~5,...)返回a,偶數(shù)段(1~2,3~4)返回b
float v = mix(a, b, LQZ(mod(x,2.0) - 1.0));
float4x4 LookAtMatrix(float3 eyePosition, float3 lookAt, float3 up)
{
float3 zaxis = normalize(lookAt - eyePosition);
float3 yaxis = normalize(up);
float3 xaxis = normalize(cross(yaxis, zaxis));
yaxis = normalize(cross(zaxis, xaxis));
float4x4 result = float4x4(xaxis.x, yaxis.x, zaxis.x, 0.0,
xaxis.y, yaxis.y, zaxis.y, 0.0,
xaxis.z, yaxis.z, zaxis.z, 0.0,
-dot(xaxis, eyePosition), -dot(yaxis, eyePosition), -dot(zaxis, eyePosition), 1.0);
return result;
}
#define MATH_PI_DIV_180 0.0174532925
float4x4 DxPorjMatrix(float fov_y, float aspect, float zNear, float zFar)
{
// Left Hand
float f_n = 1.0f / (zFar - zNear);
float yScale = 1.0 / tan(MATH_PI_DIV_180 * (fov_y) * 0.5f);
float xScale = yScale / aspect;
float4x4 result = float4x4(xScale, 0.0, 0.0, 0.0,
0.0, yScale, 0.0, 0.0,
0.0, 0.0, zFar*f_n, 1.0,
0.0, 0.0, -zNear * zFar * f_n, 1.0);
return result;
第二章 物理中的基礎(chǔ)概念
在這一章中我們回顧一些物理的基礎(chǔ)概念,這些概念關(guān)系到(relevant)運(yùn)動(dòng)分析(analysis)和對(duì)剛體的影響。包含質(zhì)量的類型區(qū)域被歸類于(classified)剛體。2.1,2.2節(jié)中的主題介紹了粒子的在absence力量下,在二維或者三維所呈現(xiàn)的曲線路徑。這些主題與運(yùn)動(dòng)學(xué)(kinematics)有關(guān).而這一節(jié)的主題介紹了物理概念里的位置,速度,加速度。很多應(yīng)用為更好的處理問題來選擇適合的坐標(biāo)系。笛卡爾坐標(biāo)系統(tǒng)通常很方便,但是我們還需極坐標(biāo)系(polar coordinates),圓柱坐標(biāo)系(cylindrical coordinates), 球坐標(biāo)系(spherical coordinates)。還有單個(gè)粒子的運(yùn)動(dòng)學(xué),還有粒子系統(tǒng)和實(shí)體系統(tǒng)的運(yùn)動(dòng)學(xué)。這些物理材質(zhì)都屬于物理引擎,在第六章討論。
這一章的其他剩余部分將介紹一些標(biāo)準(zhǔn)的物理概念。一條主線是從2.3節(jié)開始介紹牛頓運(yùn)動(dòng)法則,討論力的概論在2.4中,其書中具體例子涉及到重力,彈力,摩擦力。力矩和平衡也在這節(jié)出現(xiàn)。各種測(cè)量距在2.5節(jié)討論,包含了線性和角度距,一階距,這些與物體的質(zhì)心,距,慣性息息相關(guān)。2.5的最后一部分展示了怎樣計(jì)算質(zhì)量不變的實(shí)體多邊形的質(zhì)心和慣性張量。在第6章中討論的物理引擎中需要實(shí)現(xiàn)的東西。功和能是本章最后討論的。在開發(fā)拉格朗日動(dòng)態(tài)模型中,動(dòng)能是重要的量。在處理保守力中勢(shì)能也是非常重要的量,如重力就是保守力。
2.1 剛體類型
剛體的特征(characterized)是質(zhì)量存在于區(qū)域中。最簡(jiǎn)單的剛體是一個(gè)質(zhì)量為m的單粒子它處于位置x處。p是一個(gè)含有碰撞的有限數(shù)量的粒子所組成的粒子系統(tǒng),第i個(gè)粒子的質(zhì)量M i處于位置X i,l <= i <= p. 單粒子和粒子系統(tǒng)是粒子數(shù)目有限的松散材質(zhì)的例子。一個(gè)粒子系統(tǒng)把各種物理量累加之和的標(biāo)準(zhǔn)樣子是:
p
Q =
S Qi total
i=1
Qi是一些物理量
按某中心為原點(diǎn)的縮放公式為.
p' = o + (p - o) * s;
p 為需要變換的點(diǎn).
o 為縮放中心點(diǎn).
s 縮放的值. (s >= 0)
p' 為結(jié)果.
http://www.iquilezles.org/blog/?p=2828
I think size matters. However, unlike in real life, when programming the smaller the better. Generally. Also, the less branches the better, at least when programming for parallel systems. And also, the more compact and regular, the prettier (but this is my personal opinion only).
Related to this, in the last 6 months I have pointed out / proposed this same optimization to at least five different people. Basically, it seems most people make this same “mistake” over and over again, which is to write this horrifying thing
vec3 color = vec3(0.0);
if (theta < 1.0) {
color.r = 1.0;
color.g = theta;
}
else if (theta < 2.0) {
color.r = 2.0 - theta;
color.g = 1.0;
}
else if (theta < 3.0) {
color.g = 1.0;
color.b = theta - 2.0;
}
else if (theta < 4.0) {
color.g = 4.0 - theta;
color.b = 1.0;
}
else if (theta < 5.0) {
color.r = theta - 4.0;
color.b = 1.0;
}
else {
color.r = 1.0;
color.b = 6.0 - theta;
}
return color;
instead of this equivalent line:
vec3 color = clamp( abs(mod(theta+vec3(0.,4.,2.),6.)-3.)-1., 0., 1. );
http://www.iquilezles.org/blog/?p=2848
Yet another example of code simplification that people don’t seem to want to do. It must be the 5th ot 6th time I ask people to do this change when programming a point-to-line distance computation: please, replace this ugly
float sdLine( vec2 a, vec2 b, vec2 p )
{
vec2 ba = b - a;
vec2 pa = p - a;
float dist = (ba.x*pa.y - ba.y*pa.x) / distance(a, b);
if( dot(a-b,p-b) < 0.0 )
return distance(b, p);
if( dot(b-a,p-a) < 0.0 )
return distance(a, p);
return abs(dist);
}
by the much more beautiful:
float sdLine( vec2 a, vec2 b, vec2 p )
{
vec2 pa = p - a;
vec2 ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
Do it for the karma or something.
1
2 // 繪制點(diǎn)
3 float point(vec2 p, vec2 center, float radius)
4 {
5 vec2 c2p = p-center;
6 float d = dot(c2p,c2p); // c2p的長(zhǎng)度平方
7 if(d < radius * radius)
8 return 1.0;
9
10 return 0.0;
11 }
12
13
14 // 繪制直線
15 float line(vec2 p, vec2 a, vec2 b, float w)
16 {
17 vec2 a2b = b - a;
18 vec2 a2p = p - a;
19
20
21 // 因?yàn)橥队?nbsp;proj(a2p,a2b) 等于 dot(a2p,a2b) / length(a2b)
22 // 在把投影的值和a2b向量做比例關(guān)系。
23 // 所以proj(a2p,a2b)/length(a2b) 等于 dot(a2p,a2b)/dot(a2b,a2b)
24
25 float h = clamp( dot(a2p,a2b)/dot(a2b,a2b), 0.0, 1.0 );
26 vec2 p1 = mix( a, b, h );
27 if( length( p1 - p ) <= w )
28 return 1.0;
29
30 return 0.0;
31 }
32
33 void main(void)
34 {
35 vec2 uv = gl_FragCoord.xy / iResolution.y;
36 uv.x = uv.x + (1.0 - iResolution.x/iResolution.y)*0.5;
37
38 vec3 col = vec3(1.0,0,0) * point(uv, vec2(0.5,0.5), 0.05)
39 + vec3(0,0,1.0) * line(uv, vec2(0.0,0.25), vec2(1.0,0.55), 0.05);
40
41 gl_FragColor = vec4(col,1.0);
42 }
光照類型
環(huán)境光(ambient): 沒有確切來源。各種光經(jīng)過反射后的得到的結(jié)果。
表示式 = 環(huán)境光強(qiáng)度 * 環(huán)境反射的顏色;
散射光(diffuse): 在真實(shí)世界中,我們經(jīng)常見到的光線都是由某些固定的光源發(fā)出的,它們總是從某一個(gè)方向照射到物體上,而不像我們?cè)谟懻摥h(huán)境光時(shí)那樣不用考慮光線的方向。所以他需要方向。
表達(dá)式 = 散射光強(qiáng)度 *(射出角度 點(diǎn)乘 面法線向量)* 散射的顏色
鏡面反射光(specular):在真實(shí)世界中,不僅要使用漫反射模型,同時(shí)還要接觸大量鏡面反射的情況。通過鏡面反射,可以看到物體表面的高光,或者是光源在光亮物體表面上的反射。
表達(dá)式 = 反射系數(shù) * 光照強(qiáng)度 * (cosA的反射指數(shù) 次方)
發(fā)射光(emission):看起來發(fā)光,但不是光源,不能照亮環(huán)境。
表達(dá)式 = 發(fā)射光
光源的類型
定向光:屬性:光照強(qiáng)度不雖距離的而變化。
表達(dá)式 = 光源初始強(qiáng)度 * 光源的顏色
點(diǎn)光源:屬性:光源強(qiáng)度雖距光源的距離而衰減。
表達(dá)式 = 光源初始強(qiáng)度*光源的顏色 / 三個(gè)因子 其中三個(gè)因子為:常量因子,線性因子,二次因子
聚光燈 :屬性:具有內(nèi)錐(本影)和外錐(半影)。
表達(dá)式分為內(nèi)錐和外錐兩個(gè)。不在內(nèi)錐和外錐范圍內(nèi)不受光照影響。