2014-02-13 21 views
1

我嘗試添加凹凸貼圖功能,我的應用程序,但我已經非常多面模型:如何實現平滑切線空間法線?

enter image description here

它發生的原因是因爲我計算每對切線,副法線和正常並且完全忽略了我從模型文件中獲得的法線。

該計算當前使用三角形的兩個邊和紋理空間矢量來得到正切和副法線,然後用於通過叉積來計算法線。一旦模型加載完成,所有這些都在CPU上完成,然後將值存儲爲模型幾何體的一部分。

vector1 = vertex2.coords - vertex1.coords;  
    vector2 = vertex3.coords - vertex1.coords; 

    tuVector = vertex2.texcoords - vertex1.texcoords; 
    tvVector = vertex3.texcoords - vertex1.texcoords; 

    float den = 1.0f/(tuVector.x * tvVector.y - tuVector.y * tvVector.x); 

    tangent.x = (tvVector.y * vector1.x - tvVector.x * vector2.x) * den; 
    tangent.y = (tvVector.y * vector1.y - tvVector.x * vector2.y) * den; 
    tangent.z = (tvVector.y * vector1.z - tvVector.x * vector2.z) * den; 

    binormal.x = (tuVector.x * vector2.x - tuVector.y * vector1.x) * den; 
    binormal.y = (tuVector.x * vector2.y - tuVector.y * vector1.y) * den; 
    binormal.z = (tuVector.x * vector2.z - tuVector.y * vector1.z) * den; 

    D3DXVec3Normalize(&tangent, &tangent); 
    D3DXVec3Normalize(&binormal, &binormal); 

    D3DXVec3Cross(&normal, &tangent, &binormal);  
    D3DXVec3Normalize(&normal, &normal); 

有沒有辦法,要麼計算每頂點基礎上這些值可能使用與模型或將它們以某種方式理順因此模型不會出現刻面提供的正常?

+0

我們展示着色器。 – Drop

回答

0

對於光滑的表面(沒有邊緣)餘像這樣做:

  1. 爲每個頂點

    double N[3]; //normal 
    int cnt; 
    
  2. 每個頂點的init創建空間

    N={0.0,0.0,0.0} 
    cnt=0; 
    
  3. 按面計算正常

    正常值必須標準化長度= 1.0 !!!添加此Normal在臉上使用的所有頂點,並增加cnt在臉上使用的所有頂點

  4. 每個頂點正常化

    N/=cnt; // N = average normal from all vertex - neighbour faces 
    

    知道cnt=0未使用的頂點(被零除)

  5. 每個頂點N包含你想要的正常

    現在計算T,B向量TBN(每個頂點)矩陣因爲你現在做

  6. 輸出圖像流暢

    我的地球預覽(大氣散射,凹凸映射和更多... )爲here

希望它有助於