1.求出顶点的法向量。通过叉乘计算
2.在顶点世界坐标空间 法向量与光向量进行点积(需要单位向量)
3.通过视向量与法线向量点积的结果 是否开启背面剔除
1.向量类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _3DTransform
{
class Vector4
{
public double x, y, z, w;
public Vector4() { }
public Vector4(double x, double y, double z, double w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public Vector4(Vector4 v)
{
this.x = v.x;
this.y = v.y;
this.z = v.z;
this.w = v.w;
}
public static Vector4 operator -(Vector4 a, Vector4 b)
{
return new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
}
public Vector4 Cross(Vector4 v)
{
return new Vector4(this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * v.y - this.y * v.x, 0);
}
public float Dot(Vector4 v)
{
return (float)(this.x * v.x + this.y + v.y + this.z + v.z+this.w+v.w);
}
public Vector4 Normalized
{
get
{
double Mod = Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
return new Vector4(x / Mod, y / Mod, z / Mod, w / Mod);
}
}
}
}
2.矩阵
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _3DTransform
{
class matrix4X4
{
public double[,] pts;
public matrix4X4()
{
pts = new double[4, 4];
}
/// <summary>
/// 访问器便于获取矩阵中的元素
/// </summary>
///