C# 模拟光照运算以及背面剔除

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>
        ///
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值