Phong光照模型漫放射光强计算函数

功能: 实现Phong光照模型,求给定点的漫放射光强;

参数含义:

  • Pt[3] : 点的坐标,
  • Vertices:点Pt所在的四边形的四个顶点坐标,顶点按逆时针顺序存放
  • Normals :四个顶点的法向量,和Vertices按同样的顺序存放;
  • lightPos:光源的位置
  • Ip :漫放射光源的强度
  • Kd :漫放射系数

返回值: 点Pt的漫放射光强;

可调用函数:

bool   BilinearInterpolation(int Vertices[4][3], int P[3], double uv[4]);

功能: 在凸四边形中实现二次线性插值

参数含义:

  • Vertices : 四边形的四个顶点,按逆时针存储;
  • P : 点的坐标
  • uv : 用于返回插值系数;

返回值: True-插值成功,Flase-插值失败(如果Pt不在四边形内部,则失败)

double CIlluminationView::Phong(int    Pt[3],
								int    Vertices[4][3],
								double Normals[4][3],
								int    lightPos[3],
								double Ip,
								double Kd)
{
    double I = 0;   //itensity;

    //计算各个顶点的光强
    double I_vertices[4];
    for (int i = 0; i < 4; i++)
    {
        double L[3] = { lightPos[0] - Vertices[i][0], lightPos[1] - Vertices[i][1], lightPos[2] - Vertices[i][2] };   //光线向量
        double N[3] = { Normals[i][0], Normals[i][1], Normals[i][2] };   //法向量
        double cos_theta = DotProduct(L, N) / (VectorLength(L) * VectorLength(N));   //计算cos(theta)
        I_vertices[i] = Ip * Kd * cos_theta;   //计算顶点的光强
    }

    //计算插值系数
    double uv[4];
    bool bSuccess = BilinearInterpolation(Vertices, Pt, uv);
    if (!bSuccess)   //如果插值失败,则返回0
    {
        return 0;
    }

    //计算点Pt的光强
    for (int i = 0; i < 4; i++)
    {
        I += I_vertices[i] * uv[i];   //根据差值系数计算点Pt的光强
    }

    return I;
}
Phong光照模型漫放射光强计算函数

原文地址: https://www.cveoy.top/t/topic/lUXh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录