構建基礎的C#3D工程

1.創建Vector4 來表示頂點數據類

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

namespace _3DTemple
{
    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 vets) {
            this.x = vets.x;
            this.y = vets.y;
            this.z = vets.z;
            this.w = vets.w;
        }
    }
}


2.創建一個基礎的矩陣類

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

namespace _3DTemple
{
    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;
            }
        }

        //矩陣 X 矩陣
        //|a1,a2,a3|     |f1,f2,f3|   | a1 * f1 + a2 * g1 + a3 * h1 , a1 * f2 + a2 * g2 + a3 * h2,a1 * f3 + a2 * g3 + a3 * h3|
        //|b1,b2,b3|     |g1,g2,g3|   | b1 * f1 + b2 * g1 + b3 * h1,  b1 * f2 + b2 * g2 + b3 * h2,b1 * f3 + b2 * g3 + b3 * h3|
        //|c1,c2,c3|   x |h1,h2,h3| = | c1 * f1 + c2 * g1 + c3 * h1,  c1 * f2 + c2 * g2 + c3 * h2,c1 * f3 + c2 * g3 + c3 * h3|

        //矩陣 X N
        //|a1,a2,a3|             |Na1,Na2,Na3|     
        //|b1,b2,b3|             |Nb1,Nb2,Nb3|
        //|c1,c2,c3|   x  N =    |Nb1,Nb2,Nb3|

         //矩陣 和矩陣 相乘的函數
        public Matrix4x4 Mul(Matrix4x4 m) {
            Matrix4x4 newM = new Matrix4x4();
            for (int W = 1; W<=4;W++) {
                for (int h = 1;h<=4;h++) {
                    for (int n = 1; n <=4;n++) {
                        newM[W, h] += this[W, n] * m[n, h];
                    }
                }
            }
 
            return newM;
        }
        //矩陣和向量的乘法
        //|a1,a2,a3|     |f1|   | a1 * f1 + b1 * g1 + c1 * h1   |
        //|b1,b2,b3|     |g1|   | a2 * f1 + b2 * g1 + c2 * g1   |
        //|c1,c2,c3|   x |h1| = | a3 * f1 + b3 * g1 + c3 * h1   |
        public Vector4 Mul(Vector4 vets) {
            Vector4 newVets = new Vector4();
            newVets.x = vets.x * this[1, 1] + vets.y * this[2, 1] + vets.z * this[3, 1] + vets.w * this[4, 1];
            newVets.y = vets.x * this[1, 2] + vets.y * this[2, 2] + vets.z * this[3, 2] + vets.w * this[4, 2];
            newVets.z = vets.x * this[1, 3] + vets.y * this[2, 3] + vets.z * this[3, 3] + vets.w * this[4, 3];
            newVets.w = vets.x * this[1, 4] + vets.y * this[2, 4] + vets.z * this[3, 4] + vets.w * this[4, 4];

            return newVets;
        }
    }
}


3。創建一個基礎的三角形類

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

namespace MaterixTreanform
{
    class Tringle
    {
        PointF A, B, C;

        public Tringle(PointF A,PointF B,PointF C)
        {
            this.A = A;
            this.B = B;
            this.C = C;
        }

        public void Draw(Graphics g)
        {
            Pen pen = new Pen(Color.Red,2);
            g.DrawLine(pen,A,B);
            g.DrawLine(pen, B, C);
            g.DrawLine(pen, C, A);

        }

        public void Rotate(int degress)
        {
            float angle = (float)(degress/360.0f * Math.PI);

            //旋轉矩陣後的各個座標系
            float newX = (float)(A.X * Math.Cos(angle) - A.Y * Math.Sin(angle));
            float newY = (float)(A.X * Math.Sin(angle) + A.Y * Math.Cos(angle));
            A.X = newX;
            A.Y = newY;

            newX = (float)(B.X * Math.Cos(angle) - B.Y * Math.Sin(angle));
            newY = (float)(B.X * Math.Sin(angle) + B.Y * Math.Cos(angle));
            B.X = newX;
            B.Y = newY;

            newX = (float)(C.X * Math.Cos(angle) - C.Y * Math.Sin(angle));
            newY = (float)(C.X * Math.Sin(angle) + C.Y * Math.Cos(angle));
            C.X = newX;
            C.Y = newY;
        }
    }
}


4.創建一個3D的三角形類

 

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

namespace _3DTemple
{
    class Tringle3D
    {
        private Vector4 a, b, c;
        public Vector4 A, B, C;

        public Tringle3D() { }
        public Tringle3D(Vector4 a,Vector4 b,Vector4 c) {
            this.A = this.a =  new Vector4(a);
            this.B = this.b = new Vector4(b);
            this.C = this.c =  new Vector4(c);
        }
        //三角形利用矩陣的乘法進行變換
        public void Transform(Matrix4x4 m) {
            this.a = m.Mul(this.A);
            this.b = m.Mul(this.B);
            this.c = m.Mul(this.C);
        }
        //繪製三角形
        public void Draw(Graphics g) {
            g.TranslateTransform(300.0f,300.0f);
            g.DrawLines(new Pen(Color.Red,2),get2DPointArray());
        }

        public PointF[] get2DPointArray() {
            PointF[] arr = new PointF[4];
            arr[0] = Get2DPointF(this.a);
            arr[1] = Get2DPointF(this.b);
            arr[2] = Get2DPointF(this.c);
            arr[3] = arr[0];
            return arr;
        }
        //3d 的座標轉換成2D的 做透視除法
        private PointF Get2DPointF(Vector4 v)
        {
            PointF p = new PointF();
            p.X = (float)(v.x / v.w);
            p.Y = (float)(v.y / v.w);
            return p;
        }
    }
}

5。基礎的表現形式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章