副法线和正切是什么?

1649 篇文章 12 订阅
1623 篇文章 23 订阅
法线,切线和副法线构成了所谓的切线空间(tangnet space),在Bump Mapping中,法线纹理中存储的法线值就是在切线空间内的。

从网上找了一段求切线和副法线的代码.
根据三个顶点的位置坐标和纹理坐标求面的副法线和正切
 1 // let P = v1 - v0
 2 D3DXVECTOR3 P  =  v1.pos  -  v0.pos;
 3 // let Q = v2 - v0
 4 D3DXVECTOR3 Q  =  v2.pos  -  v0.pos;
 5 float  s1  =  v1.s  -  v0.s;
 6 float  t1  =  v1.t  -  v0.t;
 7 float  s2  =  v2.s  -  v0.s;
 8 float  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
27 float  tmp  =   0.0f ;
28 if (fabsf(s1 * t2  -  s2 * t1)  <=   0.0001f )
29 {
30    tmp = 1.0f;
31}

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

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

根据Maya里面的资料写了一个求取tangent的函数,输入为3个顶点的位置,法线和纹理坐标,输出是切线值,副法线可以由切线和法线叉乘得到。
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;
}
好的,我可以帮您用Plotly画一张这样的图。以下是示例代码: ```python import plotly.graph_objects as go import numpy as np # 生成曲线上的点 t = np.linspace(0, 2 * np.pi, 1000) x = np.sin(t) y = np.cos(t) z = t # 计算切向量、主法向量和法向量 dx_dt = np.cos(t) dy_dt = -np.sin(t) dz_dt = np.ones_like(t) dr_dt = np.array([dx_dt, dy_dt, dz_dt]) dr_dt_norm = np.linalg.norm(dr_dt, axis=0) tangent_vector = dr_dt / dr_dt_norm d2x_dt2 = -np.sin(t) d2y_dt2 = -np.cos(t) d2z_dt2 = np.zeros_like(t) d2r_dt2 = np.array([d2x_dt2, d2y_dt2, d2z_dt2]) d2r_dt2_norm = np.linalg.norm(d2r_dt2, axis=0) normal_vector = d2r_dt2 / d2r_dt2_norm binormal_vector = np.cross(tangent_vector, normal_vector) # 绘制曲线和向量 fig = go.Figure() fig.add_trace(go.Scatter3d( x=x, y=y, z=z, mode='lines', name='Curve', )) point_of_interest = 500 # 选取曲线上的一个点 fig.add_trace(go.Scatter3d( x=[x[point_of_interest]], y=[y[point_of_interest]], z=[z[point_of_interest]], mode='markers', name='Point of Interest', )) # 在该点处绘制切向量、主法向量和法向量 vector_scale = 0.25 # 向量缩放比例 vector_color = ['red', 'green', 'blue'] # 向量颜色 for vector, color, name in zip([tangent_vector[:, point_of_interest], normal_vector[:, point_of_interest], binormal_vector[:, point_of_interest]], vector_color, ['Tangent Vector', 'Normal Vector', 'Binormal Vector']): fig.add_trace(go.Cone( x=[x[point_of_interest]], y=[y[point_of_interest]], z=[z[point_of_interest]], u=[vector[0] * vector_scale], v=[vector[1] * vector_scale], w=[vector[2] * vector_scale], sizemode='absolute', sizeref=0.1, anchor='tail', showscale=False, colorscale=[[0, color], [1, color]], name=name, )) fig.update_layout( scene=dict( xaxis=dict(title='X'), yaxis=dict(title='Y'), zaxis=dict(title='Z'), aspectratio=dict(x=1, y=1, z=0.3), camera=dict( eye=dict(x=-1.7, y=-1.7, z=0.5), up=dict(x=0, y=0, z=1), ), ), margin=dict(l=0, r=0, t=0, b=0), ) fig.show() ``` 这个代码会生成一个空间曲线,以及在曲线上的某个点处标注出其切向量、主法向量和法向量。您可以根据需要修改点的位置、曲线的形状等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值