3D数学基础实例---C#矩阵变换与三维渲染1

1、将3x3矩阵变成4x4矩阵,统一地去处理几次变换,包括从模型到世界,从世界到摄像机,从摄像机到投影变换,最终利用小孔成像真正地把3d的点映射到屏幕上,


2、

①新建项目:文件-->新建-->项目-->windows窗体应用-->项目名称-->确定

②Triangle3D的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3DTransform
{
    class Triangle3D
    {
        public Vector4 A, B, C;
        public Triangle3D() { }
        public Triangle3D(Vector4 a,Vector4 b,Vector4 c) {
            //得到了三个顶点的数据
            this.A = new Vector4(a);
            this.B = new Vector4(b);
            this.C = new Vector4(c);
        } 
    }
}

③Matrix4x4的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3DTransform
{
    //4x4的矩阵
    class Matrix4x4
    {
        private double[,] pts;
        public Matrix4x4()
        {
            //创建数组
            pts = new double[4,4];
        }

        //参数为行和列
        public double this[int i,int j]
        {
            get {
                return pts[i-1,j-1];
            }
            set {
                pts[i - 1, j - 1] = value;
            }

        }

        //将4x4的矩阵和其它4x4的矩阵相乘,再返回一个4x4的矩阵,顺序不能错,用自己放在左边乘以右边的矩阵m
        public Matrix4x4 Mul(Matrix4x4 m)
        {
            //新的矩阵
            Matrix4x4 newM = new Matrix4x4();
            //w表示行
            for (int w=1; w<=4;w++)
                //h表示列
                for(int h = 1; h <= 4; h++)
                    for(int n = 1; n <= 4; n++)
                    {
                        //新矩阵的w行h列等于自己的w行的n列的元素乘以新矩阵的n行的h列的元素
                        newM[w, h] += this[w, n] * m[n,h];
                    }
            return newM;
        }

        //用这个4维矩阵变换一个4维向量,返回一个变换后的向量
        public Vector4 Mul(Vector4 v)
        {
            Vector4 newV = new Vector4();
            newV.x = v.x * this[1, 1] + v.y * this[2, 1] + v.z * this[3, 1] + v.w * this[4,1];
            newV.y = v.x * this[1, 2] + v.y * this[2, 2] + v.z * this[3, 2] + v.w * this[4, 2];
            newV.z = v.x * this[1, 3] + v.y * this[2, 3] + v.z * this[3, 3] + v.w * this[4, 3];
            newV.w = v.x * this[1, 4] + v.y * this[2, 4] + v.z * this[3, 4] + v.w * this[4, 4];

            return newV;
        }
    }

}


④Vector4的代码

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;
        private Vector4 a;

        public Vector4() { }

        public Vector4(Vector4 a)
        {
            this.a = a;
        }

        public Vector4(double x,double y,double z,double w)
        {
            this.x = x;
            this.y = y;
            this.z = z;
            this.w = w;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值