脚踏实地

心 勿噪

#

shader 中避免if else

// 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
// 奇数段(0~1,2~3,4~5,...)返回a,偶数段(1~2,3~4)返回b
float v = mix(a, b, LQZ(mod(x,2.0) - 1.0));

posted @ 2016-09-04 14:32 LSH| 编辑 收藏

hlsl的 投影和view矩阵

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;
}

biliner 插值

posted @ 2015-10-24 10:38 LSH| 编辑 收藏

game physics chapter 2.

第二章 物理中的基础概念
在这一章中我们回顾一些物理的基础概念,这些概念关系到(relevant)运动分析(analysis)和对刚体的影响。包含质量的类型区域被归类于(classified)刚体。2.1,2.2节中的主题介绍了粒子的在absence力量下,在二维或者三维所呈现的曲线路径。这些主题与运动学(kinematics)有关.而这一节的主题介绍了物理概念里的位置,速度,加速度。很多应用为更好的处理问题来选择适合的坐标系。笛卡尔坐标系统通常很方便,但是我们还需极坐标系(polar coordinates),圆柱坐标系(cylindrical coordinates), 球坐标系(spherical coordinates)。还有单个粒子的运动学,还有粒子系统和实体系统的运动学。这些物理材质都属于物理引擎,在第六章讨论。
这一章的其他剩余部分将介绍一些标准的物理概念。一条主线是从2.3节开始介绍牛顿运动法则,讨论力的概论在2.4中,其书中具体例子涉及到重力,弹力,摩擦力。力矩和平衡也在这节出现。各种测量距在2.5节讨论,包含了线性和角度距,一阶距,这些与物体的质心,距,惯性息息相关。2.5的最后一部分展示了怎样计算质量不变的实体多边形的质心和惯性张量。在第6章中讨论的物理引擎中需要实现的东西。功和能是本章最后讨论的。在开发拉格朗日动态模型中,动能是重要的量。在处理保守力中势能也是非常重要的量,如重力就是保守力。
2.1 刚体类型
刚体的特征(characterized)是质量存在于区域中。最简单的刚体是一个质量为m的单粒子它处于位置x处。p是一个含有碰撞的有限数量的粒子所组成的粒子系统,第i个粒子的质量M i处于位置X i,l <= i <= p. 单粒子和粒子系统是粒子数目有限的松散材质的例子。一个粒子系统把各种物理量累加之和的标准样子是:
                 p
Q
       = S Qi
     total    
                i=1
Qi是一些物理量

posted @ 2015-08-09 11:29 LSH| 编辑 收藏

中心缩放公式

按某中心为原点的缩放公式为.
p' = o + (p - o) * s;
p 为需要变换的点.
o 为缩放中心点.
s 缩放的值. (s >= 0)
p' 为结果.

posted @ 2015-05-19 17:33 LSH 阅读(418) | 评论 (0)编辑 收藏

Never call virtual function during construction or destruction.(用远不要在构造函数或者析构函数中调用虚函数或纯虚函数)

the title is all about.

posted @ 2015-05-05 16:33 LSH| 编辑 收藏

please please please, make it smaller!

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. );

posted @ 2015-01-05 14:22 LSH 阅读(233) | 评论 (0)编辑 收藏

please simplify me (again…)

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.  

posted @ 2015-01-05 14:04 LSH 阅读(217) | 评论 (0)编辑 收藏

矢量直线,矢量点

 1 
 2 // 绘制点
 3 float point(vec2 p, vec2 center, float radius)
 4 {
 5     vec2 c2p = p-center;
 6     float d = dot(c2p,c2p); // c2p的长度平方
 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     // 因为投影 proj(a2p,a2b) 等于 dot(a2p,a2b) / length(a2b)
22     // 在把投影的值和a2b向量做比例关系。
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 }

posted @ 2015-01-05 02:21 LSH 阅读(262) | 评论 (0)编辑 收藏

光照模型

光照类型

环境光(ambient): 没有确切来源。各种光经过反射后的得到的结果。
表示式 = 环境光强度 * 环境反射的颜色;

散射光(diffuse): 在真实世界中,我们经常见到的光线都是由某些固定的光源发出的,它们总是从某一个方向照射到物体上,而不像我们在讨论环境光时那样不用考虑光线的方向。所以他需要方向。
表达式 = 散射光强度  *(射出角度 点乘 面法线向量)* 散射的颜色  

镜面反射光(specular):在真实世界中,不仅要使用漫反射模型,同时还要接触大量镜面反射的情况。通过镜面反射,可以看到物体表面的高光,或者是光源在光亮物体表面上的反射。
表达式 = 反射系数 *  光照强度 * (cosA的反射指数 次方)

发射光(emission):看起来发光,但不是光源,不能照亮环境。
表达式 = 发射光

光源的类型

定向光:属性:光照强度不虽距离的而变化。
表达式 = 光源初始强度 * 光源的颜色

点光源:属性:光源强度虽距光源的距离而衰减。
表达式 = 光源初始强度*光源的颜色 / 三个因子      其中三个因子为:常量因子,线性因子,二次因子

聚光灯 :属性:具有内锥(本影)和外锥(半影)。
表达式分为内锥和外锥两个。不在内锥和外锥范围内不受光照影响。 

posted @ 2010-05-17 02:23 LSH 阅读(378) | 评论 (0)编辑 收藏

仅列出标题
共2页: 1 2