/**

  • 功 能:实现Gouraud光照模型,求给定点的漫放射光强;
  • 参数含义:
  •      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不在四边形内部,则失败)
    
  •  ===========================================================================
    
  • 日 期: 2009-10-20
  • 作 者: 罗月童 */ /*double CIlluminationView::Gouraud (int Pt[3], int Vertices[4][3], double Normals[4][3], int lightPos[3], double Ip, double Kd) { double I = 0; //itensity; //学生添加代码 return I;

}*/

/**

  • 计算给定点在四边形中的漫反射光照强度

  • @param Pt 给定点的坐标

  • @param Vertices 四边形的四个顶点坐标

  • @param Normals 四个顶点的法向量

  • @param lightPos 光源的位置

  • @param Ip 漫放射光源的强度

  • @param Kd 漫放射系数

  • @return 给定点的漫反射光照强度 */ double CIlluminationView::Gouraud(int Pt[3], int Vertices[4][3], double Normals[4][3], int lightPos[3], double Ip, double Kd) { double I = 0; //itensity;

    // 计算点Pt在四边形中的重心坐标系数uv double uv[4]; if (!BilinearInterpolation(Vertices, Pt, uv)) return 0;

    // 计算点Pt的法向量 double normal[3] = {0}; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { normal[j] += uv[i] * Normals[i][j]; } }

    // 计算光线向量 double lightVector[3] = {0}; for (int i = 0; i < 3; i++) { lightVector[i] = lightPos[i] - Pt[i]; }

    // 计算光线向量和法向量的夹角余弦值 double cosTheta = 0; double normalLength = sqrt(pow(normal[0], 2) + pow(normal[1], 2) + pow(normal[2], 2)); double lightVectorLength = sqrt(pow(lightVector[0], 2) + pow(lightVector[1], 2) + pow(lightVector[2], 2)); for (int i = 0; i < 3; i++) { cosTheta += normal[i] * lightVector[i] / (normalLength * lightVectorLength); }

    // 计算漫反射光照强度 I = Ip * Kd * cosTheta;

    return I; }

Gouraud 光照模型:计算漫反射光照强度

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

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