脚踏实地

心 勿噪

2021年3月8日 #

fixed function pipeline

//******************************************************************
//
// OpenGL ES 2.0 vertex shader that implements the following
// OpenGL ES 1.1 fixed function pipeline
//
// - compute lighting equation for up to eight directional/point/
// - spot lights
// - transform position to clip coordinates
// - texture coordinate transforms for up to two texture coordinates
// - compute fog factor
// - compute user clip plane dot product (stored as v_ucp_factor)
//
//******************************************************************
#define NUM_TEXTURES 2
#define GLI_FOG_MODE_LINEAR 0
#define GLI_FOG_MODE_EXP 1
#define GLI_FOG_MODE_EXP2 2
struct light {
    vec4 position;  // light position for a point/spot light or
                    // normalized dir. for a directional light
    vec4 ambient_color;
    vec4 diffuse_color;
    vec4 specular_color;
    vec3 spot_direction;
    vec3 attenuation_factors;
    float spot_exponent;
    float spot_cutoff_angle;
    bool compute_distance_attenuation;
};
struct material {
    vec4 ambient_color;
    vec4 diffuse_color;
    vec4 specular_color;
    vec4 emissive_color;
    float specular_exponent;
};
const float c_zero = 0.0;
const float c_one = 1.0;
const int indx_zero = 0;
const int indx_one = 1;
uniform mat4 mvp_matrix; // combined model-view + projection matrix
uniform mat4 modelview_matrix; // model view matrix
uniform mat3 inv_modelview_matrix; // inverse model-view
// matrix used
// to transform normal
uniform mat4 tex_matrix[NUM_TEXTURES]; // texture matrices
uniform bool enable_tex[NUM_TEXTURES]; // texture enables
uniform bool enable_tex_matrix[NUM_TEXTURES]; // texture matrix enables
uniform material material_state;
uniform vec4 ambient_scene_color;
uniform light light_state[8];
uniform bool light_enable_state[8]; // booleans to indicate which of eight
                                    // lights are enabled
uniform int num_lights; // number of lights enabled = sum of
                        // light_enable_state bools set to TRUE
uniform bool enable_lighting; // is lighting enabled
uniform bool light_model_two_sided; // is two-sided lighting enabled
uniform bool enable_color_material; // is color material enabled
uniform bool enable_fog; // is fog enabled
uniform float fog_density;
uniform float fog_start, fog_end;
uniform int fog_mode; // fog mode - linear, exp, or exp2
uniform bool xform_eye_p; // xform_eye_p is set if we need
                          // Peye for user clip plane,
                          // lighting, or fog
uniform bool rescale_normal; // is rescale normal enabled
uniform bool normalize_normal; // is normalize normal enabled
uniform float rescale_normal_factor; // rescale normal factor if
                                     // glEnable(GL_RESCALE_NORMAL)
uniform vec4 ucp_eqn; // user clip plane equation –
                      // - one user clip plane specified
uniform bool enable_ucp; // is user clip plane enabled
//******************************************************
// vertex attributes - not all of them may be passed in
//******************************************************
attribute vec4 a_position; // this attribute is always specified
attribute vec4 a_texcoord0;// available if enable_tex[0] is true
attribute vec4 a_texcoord1;// available if enable_tex[1] is true
attribute vec4 a_color; // available if !enable_lighting or
                        // (enable_lighting && enable_color_material)
attribute vec3 a_normal; // available if xform_normal is set
                         // (required for lighting)
//************************************************
// varying variables output by the vertex shader
//************************************************
varying vec4 v_texcoord[NUM_TEXTURES];
varying vec4 v_front_color;
varying vec4 v_back_color;
varying float v_fog_factor;
varying float v_ucp_factor;
//************************************************
// temporary variables used by the vertex shader
//************************************************
vec4 p_eye;
vec3 n;
vec4 mat_ambient_color;
vec4 mat_diffuse_color;
vec4
lighting_equation(int i)
{
    vec4 computed_color = vec4(c_zero, c_zero, c_zero, c_zero);
    vec3 h_vec;
    float ndotl, ndoth;
    float att_factor;
    att_factor = c_one;
    if(light_state[i].position.w != c_zero)
    {
        float spot_factor;
        vec3 att_dist;
        vec3 VPpli;
        // this is a point or spot light
        // we assume "w" values for PPli and V are the same
        VPpli = light_state[i].position.xyz - p_eye.xyz;
        if(light_state[i].compute_distance_attenuation)
        {
            // compute distance attenuation
            att_dist.x = c_one;
            att_dist.z = dot(VPpli, VPpli);
            att_dist.y = sqrt(att_dist.z);
            att_factor = c_one / dot(att_dist,
            light_state[i].attenuation_factors);
        }
        VPpli = normalize(VPpli);
        if(light_state[i].spot_cutoff_angle < 180.0)
        {
            // compute spot factor
            spot_factor = dot(-VPpli, light_state[i].spot_direction);
            if(spot_factor >= cos(radians(light_state[i].spot_cutoff_angle)))
            {
                spot_factor = pow(spot_factor,light_state[i].spot_exponent);
            }
            else{
                spot_factor = c_zero;
            }
            att_factor *= spot_factor;
        }
    }
    else
    {
        // directional light
        VPpli = light_state[i].position.xyz;
    }
    if(att_factor > c_zero)
    {
        // process lighting equation --> compute the light color
        computed_color += (light_state[i].ambient_color * mat_ambient_color);
        ndotl = max(c_zero, dot(n, VPpli));
        computed_color += (ndotl * light_state[i].diffuse_color * mat_diffuse_color);
        h_vec = normalize(VPpli + vec3(c_zero, c_zero, c_one));
        ndoth = dot(n, h_vec);
        if (ndoth > c_zero)
        {
            computed_color += (pow(ndoth,material_state.specular_exponent) *
                               material_state.specular_color *
                               light_state[i].specular_color);
        }
        computed_color *= att_factor; // multiply color with
                                      // computed attenuation factor
                                      // * computed spot factor
    }
    return computed_color;
}
float compute_fog()
{
    float f;
    
    // use eye Z as approximation
    if(fog_mode == GLI_FOG_MODE_LINEAR)
    {
        f = (fog_end - p_eye.z) / (fog_end - fog_start);
    }
    else if(fog_mode == GLI_FOG_MODE_EXP)
    {
        f = exp(-(p_eye.z * fog_density));
    }
    else
    {
        f = (p_eye.z * fog_density);
        f = exp(-(f * f));
    }
    f = clamp(f, c_zero, c_one);
    return f;
}
vec4 do_lighting()
{
    vec4 vtx_color;
    int i, j;
    
    vtx_color = material_state.emissive_color +
                (mat_ambient_color * ambient_scene_color);
    j = (int)c_zero;
    for (i=(int)c_zero; i<8; i++)
    {
        if(j >= num_lights)
            break;
            
        if (light_enable_state[i])
        {
            j++;
            vtx_color += lighting_equation(i);
        }
    }
    vtx_color.a = mat_diffuse_color.a;
    return vtx_color;
}
void main(void)
{
    int i, j;
    // do we need to transform P
    if(xform_eye_p)
        p_eye = modelview_matrix * a_position;
        
    if(enable_lighting)
    {
        n = inv_modelview_matrix * a_normal;
        if(rescale_normal)
            n = rescale_normal_factor * n;
        if (normalize_normal)
            n = normalize(n);
        mat_ambient_color = enable_color_material ? a_color
                                                  : material_state.ambient_color;
        mat_diffuse_color = enable_color_material ? a_color
                                                  : material_state.diffuse_color;
        v_front_color = do_lighting();
        v_back_color = v_front_color;
        
        // do 2-sided lighting
        if(light_model_two_sided)
        {
            n = -n;
            v_back_color = do_lighting();
        }
    }
    else
    {
        // set the default output color to be the per-vertex /
        // per-primitive color
        v_front_color = a_color;
        v_back_color = a_color;
    }
    // do texture xforms
    v_texcoord[indx_zero] = vec4(c_zero, c_zero, c_zero, c_one);
    if(enable_tex[indx_zero])
    {
        if(enable_tex_matrix[indx_zero])
            v_texcoord[indx_zero] = tex_matrix[indx_zero] * a_texcoord0;
        else
            v_texcoord[indx_zero] = a_texcoord0;
    }
    v_texcoord[indx_one] = vec4(c_zero, c_zero, c_zero, c_one);
    if(enable_tex[indx_one])
    {
        if(enable_tex_matrix[indx_one])
            v_texcoord[indx_one] = tex_matrix[indx_one] * a_texcoord1;
        else
            v_texcoord[indx_one] = a_texcoord1;
    }
    v_ucp_factor = enable_ucp ? dot(p_eye, ucp_eqn) : c_zero;
    v_fog_factor = enable_fog ? compute_fog() : c_one;
    gl_Position = mvp_matrix * a_position;
}

posted @ 2021-03-08 22:05 LSH 阅读(162) | 评论 (0)编辑 收藏

2019年12月8日 #

rayIntersect 重新修改


----------------------------------
一段光线求交的场景!
----------------------------------


posted @ 2019-12-08 00:59 LSH 阅读(611) | 评论 (0)编辑 收藏

2019年12月7日 #

350行路径追踪渲染器online demo

这是一个简单的路径追踪demo
移动视角:左键按下+鼠标移动
全屏查看:右键按下


posted @ 2019-12-07 15:21 LSH 阅读(552) | 评论 (0)编辑 收藏

2019年11月3日 #

关于向量的叉乘操作

在三维中常常需要重算正交的基向量组,
由于叉乘操作是有序的. 一般来说 : UxV不等于VxU, 
所有往往记不住到底是哪个左向量乘哪个右向量求出
第三个向量,由于吃了一些亏所以做了总结.
i,j,k三个基向量, 如果你使用的图形引擎Z往屏幕外面,
右手边X和上方向Y规定为正方向的一组正交向量,如果
你使用的模型的基向量组和它相同,那么放心用.
ixj=k, kxi=j, jxk=i 
但是你可能不总是那么幸运.也许你打算使用Z往屏幕里面,
右手边X和上方向Y规定为正方向的一组正交向量,这时你就
需要改变叉乘方式了
jxi=k, ixk=j, kxj=i 
也就是统统反过来使用就可以了.
但是如果你想使用Z往屏幕里面,右手边X和下方向Y规定
为正方向的一组正交向量时这时你又需要怎么弄呢?
其实还是:
ixj=k, kxi=j, jxk=i 
如果你想使用Z往屏幕里面,左手边X和下方向Y规定
为正方向的一组正交向量时这时你又需要怎么弄呢?
这时又是:
jxi=k, ixk=j, kxj=i 
也是统统反过来使用.
这时怎么得到得结论?
其实就是通过计算得到的
以下都假设x右为正方向,y上为正方向,z往屏幕外为正方向设备的环境
测试.

var vec3 = glMatrix.vec3;
console.log("-------------------->z轴往屏幕里为正的坐标系");
var u = vec3.fromValues(1,0,0)
var v = vec3.fromValues(0,1,0)
var w = vec3.fromValues(0,0,-1)

console.log(vec3.cross(vec3.create(), w,v));
console.log(vec3.cross(vec3.create(), u,w));
console.log(vec3.cross(vec3.create(), v,u));
console.log("-------------------->y轴向下为正的坐标系");
var u = vec3.fromValues(1,0,0)
var v = vec3.fromValues(0,-1,0)
var w = vec3.fromValues(0,0,1)

console.log(vec3.cross(vec3.create(), w,v));
console.log(vec3.cross(vec3.create(), u,w));
console.log(vec3.cross(vec3.create(), v,u));
console.log("-------------------->x轴向左为正的坐标系");
var u = vec3.fromValues(-1,0,0)
var v = vec3.fromValues(0,1,0)
var w = vec3.fromValues(0,0,1)

console.log(vec3.cross(vec3.create(), w,v));
console.log(vec3.cross(vec3.create(), u,w));
console.log(vec3.cross(vec3.create(), v,u));
console.log("-------------------->全部反为正坐标系");
var u = vec3.fromValues(-1,0,0)
var v = vec3.fromValues(0,-1,0)
var w = vec3.fromValues(0,0,-1)
console.log(vec3.cross(vec3.create(), w,v));
console.log(vec3.cross(vec3.create(), u,w));
console.log(vec3.cross(vec3.create(), v,u));

以上都能得到正确的向量组

console.log("-------------------->z轴往屏幕外为正坐标系");
var u = vec3.fromValues(1,0,0)
var v = vec3.fromValues(0,1,0)
var w = vec3.fromValues(0,0,1)
console.log(vec3.cross(vec3.create(), v,w));
console.log(vec3.cross(vec3.create(), w,u));
console.log(vec3.cross(vec3.create(), u,v));
console.log("-------------------->任意两个是为负数的坐标系");
var u = vec3.fromValues(-1,0,0)
var v = vec3.fromValues(0,1,0)
var w = vec3.fromValues(0,0,-1)
console.log(vec3.cross(vec3.create(), v,w));
console.log(vec3.cross(vec3.create(), w,u));
console.log(vec3.cross(vec3.create(), u,v));

以上也都能得到正确的向量组.
结论就是如果偶数相反就正常使用,如果是奇数相反就
用反过来用.

posted @ 2019-11-03 23:34 LSH 阅读(720) | 评论 (0)编辑 收藏

2019年6月26日 #

排列组合

// 排列:正数n的全排列
// n 正数n
// return 数值
function A(n) {
if (n <= 0) return n;
var sum = 1;
for (var i = n; i > 0; --i) {
sum *= i;
}
return sum;
}

// 组合:从n个中选择m个来组合
// n 正数n
// m 正数m
// return 数值
function C(n, m) {
return A(n) / (A(m) * A(n - m));
}

// 数组组合: 从array中选择n个元素来组合
// array 数组
// n 正数n
// return 多少种组合
function ArrayComb(array, n) {
var result = [], t = [], e;

function Recursion(index, array, n, t, result) {
if (t.length === n) { result.push(t.slice()); return };

for (var i = index; i < array.length; ++i) {
e = array[i];
t.push(e);
Recursion(i + 1, array, n, t, result);
t.pop();
}
}

Recursion(0, array, n, t, result);
return result;
}

posted @ 2019-06-26 12:57 LSH 阅读(228) | 评论 (0)编辑 收藏

2018年3月23日 #

rayIntersect

     摘要: ---------------------------------- 一段光线求交的场景! ---------------------------------- 点我看源码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --...  阅读全文

posted @ 2018-03-23 00:27 LSH 阅读(307) | 评论 (0)编辑 收藏

2017年1月19日 #

矩阵计算器

     摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><html><head><title>矩阵计算器 (1.0)</title><meta charset="utf-8">&l...  阅读全文

posted @ 2017-01-19 23:36 LSH 阅读(507) | 评论 (0)编辑 收藏

2016年12月16日 #

Quine program

c/c++
//>this is a Quine program implement by c language.
//>reference http://www.madore.org/~david/computers/quine.html
#include <stdio.h>
int main(void){
  char n='\n'; char g='\\'; char q='"'; char s=';';
  char*s1="//>this is a Quine program implement by c language.%c//>reference http://www.madore.org/~david/computers/quine.html%c#include <stdio.h>%cint main(void){%c  char n='%cn'; char g='%c%c'; char q='%c'; char s=';';%c  char*s1=%c%s%c;%c  printf(s1,n,n,n,n,g,g,g,q,n,q,s1,q,n,s,n,s,n)%c%c  return 0%c%c}";
  printf(s1,n,n,n,n,g,g,g,q,n,q,s1,q,n,s,n,s,n);
  return 0;
}
javascript
var c1='"'; var c2='\n'; var c3='\\'; var c4=';';
var s1="var c1='%c1'; var c2='%c3n'; var c3='%c3%c3'; var c4=';';%c2var s1=%c1%s1%c1%c4%c2console.log((((((((((s1.replace('%c1', c1)).replace('%c1', c1)).replace('%c1', c1)).replace('%c2', c2)).replace('%c2', c2)).replace('%c3', c3)).replace('%c3', c3)).replace('%c3', c3)).replace('%c4', c4)).replace('%s1', s1))";
console.log((((((((((s1.replace('%c1', c1)).replace('%c1', c1)).replace('%c1', c1)).replace('%c2', c2)).replace('%c2', c2)).replace('%c3', c3)).replace('%c3', c3)).replace('%c3', c3)).replace('%c4', c4)).replace('%s1', s1))

posted @ 2016-12-16 16:44 LSH 阅读(391) | 评论 (0)编辑 收藏

2016年10月2日 #

js模块编程

<script type="text/javascript">
 
        void function(global)
        {
            var mapping = {}, cache = {};
            global.define = function(id, func){
                mapping[id] = func;
            };
            
            global.require = function(id){
                if(cache[id])
                    return cache[id];
                else
                    return cache[id] = mapping[id]({});
            };
        }(this);
        
        define("moduleA", function(exports)
        {
            function ClassA(){
            }
            
            ClassA.prototype.print = function(){
                alert("moduleA.ClassA")
            }
            
            exports.New = function(){
                return new ClassA();
            }
        
            return exports;
            
        });
        
        define("moduleB", function(exports)
        {
            function ClassB(){
            }
        
            ClassB.prototype.print = function(){
                alert("moduleB.ClassB")
            }
            
            exports.New = function(){
                return new ClassB();
            }
            
            return exports;
        });
        
        define("moduleC", function(exports)
        {
            function ClassC(){
            }
        
            ClassC.prototype.print = function(){
                var classA = require("moduleA").New();
                classA.print();
                    
                var classB = require("moduleB").New();
                classB.print();
                    
                alert("moduleC.ClassC")
            }
            
            exports.New = function(){
                return new ClassC();
            }
            
            return exports;
        });
        
        var classC = require("moduleC").New();
        classC.print();
        
      </script>

posted @ 2016-10-02 20:33 LSH 阅读(200) | 评论 (0)编辑 收藏

2016年9月19日 #

trace.bat

@echo off
:Main
setlocal EnableDelayedExpansion
call :ShowInputIP
call :CheckIP
if %errorlevel% == 1 (
    call :TrackIP !IP! 1
)
setlocal DisableDelayedExpansion
goto :Main
::---------------------------------------------------------------
:TrackIP
ping %1 -n 2 -i %2 >rs.txt
set /a c=%2+1
if %c% geq 65 (
    echo 超出TTL限制[65]
    ping %1 -n 1
    goto :eof
)
for /f "tokens=1-5* delims= " %%i in (rs.txt) do (
    if "%%i" == "来自" (
        echo    追踪到IP[%%j] TTL=%2
        if %%j == !IP! (
            echo 追踪完成!!! 
        ) else (
            call :TrackIP %1 %c%
        )
        goto :eof
    ) else (
        if "%%i" == "请求超时。" ( 
            echo 跳跃TTL  [TTL=%2%] 
            call :TrackIP %1 %c% 
            goto :eof
        )
    )
)
goto :eof
::---------------------------------------------------------------
:ShowInputIP
echo 请输入要跟踪 ip/域名 地址:
set /p IP=
goto :eof
::---------------------------------------------------------------
:CheckIP
ping %IP% -n 1 >temp.txt
set context=
for /f "tokens=1-5* delims= " %%i in (temp.txt) do (
    if "%%m" == "具有" (
        set context=%%l
        set IP=!context:~1,-1!
        echo 解析域名 [%IP%] → IP [!IP!]
        goto :CheckEnd
    )
)
:CheckEnd
del temp.txt
exit /b 1

posted @ 2016-09-19 03:07 LSH 阅读(251) | 评论 (0)编辑 收藏

仅列出标题  下一页