• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            副法線和正切是什么?

            法線,切線和副法線構(gòu)成了所謂的切線空間(tangnet space),在Bump Mapping中,法線紋理中存儲的法線值就是在切線空間內(nèi)的。

            從網(wǎng)上找了一段求切線和副法線的代碼.
            根據(jù)三個頂點的位置坐標(biāo)和紋理坐標(biāo)求面的副法線和正切
             1//let P = v1 - v0
             2D3DXVECTOR3 P = v1.pos - v0.pos;
             3//let Q = v2 - v0
             4D3DXVECTOR3 Q = v2.pos - v0.pos;
             5float s1 = v1.s - v0.s;
             6float t1 = v1.t - v0.t;
             7float s2 = v2.s - v0.s;
             8float t2 = v2.t - v0.t; 
             9
            10//we need to solve the equation
            11// P = s1*T + t1*B
            12// Q = s2*T + t2*B
            13// for T and B
            14
            15
            16//this is a linear system with six unknowns and six equatinos, for TxTyTz BxByBz
            17//[px,py,pz] = [s1,t1] * [Tx,Ty,Tz]
            18// qx,qy,qz     s2,t2     Bx,By,Bz
            19
            20//multiplying both sides by the inverse of the s,t matrix gives
            21//[Tx,Ty,Tz] = 1/(s1t2-s2t1) *  [t2,-t1] * [px,py,pz]
            22// Bx,By,Bz                      -s2,s1     qx,qy,qz  
            23
            24//solve this for the unormalized T and B to get from tangent to object space
            25
            26
            27float tmp = 0.0f;
            28if(fabsf(s1*t2 - s2*t1) <= 0.0001f)
            29{
            30    tmp = 1.0f;
            31}

            32else
            33{
            34    tmp = 1.0f/(s1*t2 - s2*t1 );
            35}

            36
            37tangent.x = (t2*P.x - t1*Q.x);
            38tangent.y = (t2*P.y - t1*Q.y);
            39tangent.z  = (t2*P.z - t1*Q.z);
            40
            41tangent = tmp * tangent;
            42
            43binormal.x = (s1*Q.x - s2*P.x);
            44binormal.y = (s1*Q.y - s2*P.y);
            45binormal.z = (s1*Q.z - s2*P.z);
            46
            47binormal = tmp * binormal;

            根據(jù)Maya里面的資料寫了一個求取tangent的函數(shù),輸入為3個頂點的位置,法線和紋理坐標(biāo),輸出是切線值,副法線可以由切線和法線叉乘得到。
            inline bool floatEqual(float a, float b)
            {
                
            return abs(a-b) < 0.00001f;
            }


            HRESULT ComputerTangent(D3DXVECTOR3 position[
            3], D3DXVECTOR3 normal[3], D3DXVECTOR2 texcoord[3],D3DXVECTOR3 oTangent[3])
            {
                D3DXVECTOR3 edge1;
                D3DXVECTOR3 edge2;
                D3DXVECTOR3 crossP;

                 
            //==============================================
                
            // x, s, t
                
            // S & T vectors get used several times in this vector,
                
            // but are only computed once.
                
            //==============================================
                edge1.x = position[1].x - position[0].x;
                edge1.y 
            = texcoord[1].x - texcoord[0].x;// s-vector - don't need to compute this multiple times
                edge1.z = texcoord[1].y - texcoord[0].y;// t-vector

                edge2.x 
            = position[2].x - position[0].x;
                edge2.y 
            = texcoord[2].x - texcoord[0].x;// another s-vector
                edge2.z = texcoord[2].y - texcoord[0].y;// another t-vector

                D3DXVec3Cross(
            &crossP,&edge1,&edge2);
                D3DXVec3Normalize(
            &crossP,&crossP);

                
            bool degnerateUVTangentPlane = floatEqual(crossP.x, 0.0f);
                
            if(degnerateUVTangentPlane)
                    crossP.x 
            = 1.0f;

                
            float tanX = -crossP.y / crossP.x;

                oTangent[
            0].x = tanX;
                oTangent[
            1].x = tanX;
                oTangent[
            2].x = tanX;

                
            //--------------------------------------------------------
                
            // y, s, t
                
            //--------------------------------------------------------
                edge1.x = position[1].y - position[0].y;

                edge2.x 
            = position[2].y - position[0].y;
                edge2.y 
            = texcoord[2].x - texcoord[0].x;// another s-vector
                edge2.z = texcoord[2].y - texcoord[0].y;// another t-vector

                D3DXVec3Cross(
            &crossP,&edge1,&edge2);
                D3DXVec3Normalize(
            &crossP,&crossP);

                degnerateUVTangentPlane 
            = floatEqual(crossP.x, 0.0f);
                
            if(degnerateUVTangentPlane)
                    crossP.x 
            = 1.0f;

                
            float tanY = -crossP.y / crossP.x;

                oTangent[
            0].y = tanY;
                oTangent[
            1].y = tanY;
                oTangent[
            2].y = tanY;

                 
            //------------------------------------------------------
                
            // z, s, t
                
            //------------------------------------------------------
                edge1.x = position[1].z - position[0].z;

                edge2.x 
            = position[2].z - position[0].z;
                edge2.y 
            = texcoord[2].x - texcoord[0].x;// another s-vector
                edge2.z = texcoord[2].y - texcoord[0].y;// another t-vector

                D3DXVec3Cross(
            &crossP,&edge1,&edge2);
                D3DXVec3Normalize(
            &crossP,&crossP);

                degnerateUVTangentPlane 
            = floatEqual(crossP.x, 0.0f);
                
            if(degnerateUVTangentPlane)
                    crossP.x 
            = 1.0f;

                
            float tanZ = -crossP.y / crossP.x;

                oTangent[
            0].z = tanZ;
                oTangent[
            1].z = tanZ;
                oTangent[
            2].z = tanZ;

                
            //------------------------------------------------------
                forint i = 0; i < 3; i++)
                
            {
                    
            // Ortho-normalize to normal
                    float dot = D3DXVec3Dot(&oTangent[i],&normal[i]);
                    oTangent[i] 
            -= normal[i] * dot;

                    
            // Normalize tangents
                    D3DXVec3Normalize(&oTangent[i],&oTangent[i]);
                }


                
            return S_OK;
            }

            posted on 2007-06-11 16:09 隨便寫寫 閱讀(2867) 評論(0)  編輯 收藏 引用 所屬分類: 圖形學(xué)

            導(dǎo)航

            <2007年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類(30)

            隨筆檔案(16)

            文章分類(18)

            文章檔案(9)

            鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            欧美激情精品久久久久| 久久青青色综合| 伊人久久综合无码成人网| 久久精品一区二区影院| 热99re久久国超精品首页| 色综合久久久久| 久久免费国产精品一区二区| 国产精品久久99| 亚洲国产精品久久66| 久久综合中文字幕| 精品人妻伦九区久久AAA片69| 青青草国产精品久久久久| 丁香五月综合久久激情| 国产巨作麻豆欧美亚洲综合久久| 91性高湖久久久久| 欧美性大战久久久久久| 久久久久久国产精品无码下载| 久久久精品国产免大香伊| 久久精品国产亚洲av高清漫画 | 久久国产精品无码HDAV| 久久精品国产亚洲AV大全| 国产999精品久久久久久| 久久久久久久免费视频| 亚洲AV日韩精品久久久久久久| 精品久久无码中文字幕| 久久久受www免费人成| 亚洲午夜久久久久妓女影院| 狠色狠色狠狠色综合久久| 久久精品国产色蜜蜜麻豆| 久久强奷乱码老熟女网站| 久久亚洲高清观看| 久久99久久99精品免视看动漫| 国产精品久久久久AV福利动漫 | 亚洲精品乱码久久久久久蜜桃不卡| 欧洲人妻丰满av无码久久不卡 | 久久精品无码专区免费| 久久亚洲中文字幕精品一区| 久久99国产精品久久99果冻传媒 | 久久午夜无码鲁丝片午夜精品| 亚洲精品无码久久久久| 久久国产视频网|