加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何实现平滑切线空间法线?

发布时间:2020-12-16 09:13:33 所属栏目:百科 来源:网络整理
导读:我正在尝试将凹凸贴图功能添加到我的应用程序中,但我正在获得非常多面的模型: 它发生的原因是因为我在每个面上计算正切,正常和正常,并完全忽略我从模型文件中得到的法线. 计算当前使用三角形和纹理空间矢量的两个边来获得切线和副法线,然后用于通过叉积计算
我正在尝试将凹凸贴图功能添加到我的应用程序中,但我正在获得非常多面的模型:

它发生的原因是因为我在每个面上计算正切,正常和正常,并完全忽略我从模型文件中得到的法线.

计算当前使用三角形和纹理空间矢量的两个边来获得切线和副法线,然后用于通过叉积计算法线.一旦模型加载,这一切都在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);

有没有办法在每个顶点的基础上计算这些值,也许使用模型提供的法线或以某种方式平滑它们,以便模型不会出现刻面?

解决方法

对于光滑的表面(没有边缘),我这样做:

>为每个顶点创建空间

double N[3]; //normal
int cnt;

>每个顶点init

N={0.0,0.0,0.0}
cnt=0;

>按面部计算正常

normal必须标准化长度= 1.0 !!!将此Normal添加到face中使用的所有顶点,并将cnt增加到face中使用的所有顶点
>每个顶点标准化

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

注意未使用顶点的cnt = 0(除以零)
>每个顶点N包含您想要的法线

现在,像现在一样计算TBN矩阵(每个顶点)的T,B向量
>输出图像流畅

我的地球预览(大气散射,凹凸贴图等等)是here

希望能帮助到你

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读