如何实现平滑的切线空间法线? [英] How to achieve smooth tangent space normals?

查看:781
本文介绍了如何实现平滑的切线空间法线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加凹凸贴图功能,我的应用程序,但我已经非常多面模型:

I'm trying to add bump mapping functionality to my application but I'm getting very faceted models:

它发生的原因是因为我计算切线,副法线和正常的每个面基础上,完全无视我从模型文件中获取的法线。

The reason it is happening is because I'm calculating tangent, binormal and normal on per face basis and completely ignoring the normals I'm getting from the model file.

计算目前使用的三角形,纹理空间矢量的两个边缘来获得切线和副法线,然后将其用于计算正常通过交产物。它是在CPU上都做只要加载模型和值随后存储为模型的几何结构的一部分。

The calculation currently uses two edges of the triangle and texture space vectors to get tangent and binormal, which are then used to calculate normal by cross product. It is all done on the CPU as soon as the model loads and the values are then stored as a part of model's geometry.

    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);

有没有办法在每个顶点的基础上无论是计算这些值,可能使用的模型或提供的正常顺利出来莫名其妙因此模型不会出现多面?

Is there a way to either calculate these values on per vertex basis, perhaps using the normal supplied with the model or to smooth them out somehow so the model doesn't appear faceted?

推荐答案

有关光滑的表面(无毛边),我这样做是这样的:

For smooth surfaces (no edges) I do it like this:

  1. 对于每个顶点的创造空间。

  1. create space for per vertex

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

  • 每个顶点的init

  • per vertex init

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

  • 计算每个面正常

  • compute per face normal

    • 正常必须正常化的长度!
    • 在此正常添加到脸部使用的所有顶点
    • 增量CNT在脸上使用的所有顶点

    每个顶点的标准化

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

    • 意识到CNT = 0未使用顶点(被零除)
    • 每个顶点N包含一个你想要的正常

      per vertex N contains the Normal you want

      • 现在,计算T,用于TBN矩阵B向量(每顶点),因为你现在做

      输出图像流畅

      • 在我的地上preVIEW(大气散射,凹凸贴图等...)
      • 是在这里:<一href="http://stackoverflow.com/a/19659648/2521214">http://stackoverflow.com/a/19659648/2521214

      希望它能帮助

      这篇关于如何实现平滑的切线空间法线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆