Beispiel #1
0
 public void Transform(Matriax4x4 m)
 {
     foreach (Triangle3D item in triangles)
     {
         item.Tranform(m);
     }
 }
Beispiel #2
0
 public void CalculateLighting(Matriax4x4 _Object2World, Vector4 L)
 {
     foreach (Triangle3D item in triangles)
     {
         item.CalculateLighting(_Object2World, L);
     }
 }
Beispiel #3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            Vector4 a = new Vector4(0, 0.5f, 0, 1);
            Vector4 b = new Vector4(0.5f, -0.5f, 0, 1);
            Vector4 c = new Vector4(-0.5f, -0.5f, 0, 1);

            t = new Triangle3D(a, b, c);

            m_view       = new Matriax4x4();
            m_view[1, 1] = 1;
            m_view[2, 2] = 1;
            m_view[3, 3] = 1;
            m_view[4, 3] = 250;
            m_view[4, 4] = 1;

            m_projection       = new Matriax4x4();
            m_projection[1, 1] = 1;
            m_projection[2, 2] = 1;
            m_projection[3, 3] = 1;
            m_projection[3, 4] = 1.0 / 250;
            m_projection[4, 4] = 0;

            m_rotationX = new Matriax4x4();
            m_rotationY = new Matriax4x4();
            m_rotationZ = new Matriax4x4();
        }
Beispiel #4
0
        public Form1()
        {
            InitializeComponent();
            m_scale       = new Matriax4x4();
            m_scale[1, 1] = 250;
            m_scale[2, 2] = 250;
            m_scale[3, 3] = 250;
            m_scale[4, 4] = 1;

            cube = new Cube();
        }
Beispiel #5
0
        public Matriax4x4 Transpose()
        {
            Matriax4x4 t = new Matriax4x4();

            for (int i = 1; i <= 4; i++)
            {
                for (int j = 1; j <= 4; j++)
                {
                    t[i, j] = this[j, i];
                }
            }

            return(t);
        }
Beispiel #6
0
        public Matriax4x4 Mul(Matriax4x4 m)
        {
            Matriax4x4 newM = new Matriax4x4();

            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);
        }
Beispiel #7
0
        //计算法向量
        public void CalculateLighting(Matriax4x4 _Object2World, Vector4 L)
        {
            this.Tranform(_Object2World);

            Vector4 U      = this.b - this.a;
            Vector4 V      = this.c - this.a;
            Vector4 normal = U.Cross(V);

            dot = normal.Normalized.Dot(L.Normalized);
            dot = Math.Max(0, dot);  //将点积限定到0-1的范围

            //摄像机(视线)向量
            Vector4 eye = new Vector4(0, 0, -1, 0);

            //判断是否剔除背面
            culBack = normal.Normalized.Dot(eye) < 0 ? true : false;
        }
Beispiel #8
0
 //利用矩阵乘法进行变换
 public void Tranform(Matriax4x4 m)
 {
     this.a = m.Mul(this.A);
     this.b = m.Mul(this.B);
     this.c = m.Mul(this.C);
 }
Beispiel #9
0
        private void Timer1_Tick(object sender, EventArgs e)
        {
            degree++;
            double radian = Math.PI * degree / 180;

            // ---------------- X -----------------------
            m_rotationX[1, 1] = 1;
            m_rotationX[2, 2] = Math.Cos(radian);
            m_rotationX[2, 3] = Math.Sin(radian);
            m_rotationX[3, 2] = -Math.Sin(radian);
            m_rotationX[3, 3] = Math.Cos(radian);
            m_rotationX[4, 4] = 1;
            // ---------------- Y -----------------------
            m_rotationY[1, 1] = Math.Cos(radian);
            m_rotationY[1, 3] = Math.Sin(radian);
            m_rotationY[2, 2] = 1;
            m_rotationY[3, 1] = -Math.Sin(radian);
            m_rotationY[3, 3] = Math.Cos(radian);
            m_rotationY[4, 4] = 1;
            // ---------------- Z -----------------------
            m_rotationZ[1, 1] = Math.Cos(radian);
            m_rotationZ[1, 2] = Math.Sin(radian);
            m_rotationZ[2, 1] = -Math.Sin(radian);
            m_rotationZ[2, 2] = Math.Cos(radian);
            m_rotationZ[3, 3] = 1;
            m_rotationZ[4, 4] = 1;



            if (this.checkBox1.Checked)
            {
                Matriax4x4 tX = m_rotationX.Transpose();
                m_rotationX = m_rotationX.Mul(tX);
            }

            if (this.checkBox2.Checked)
            {
                Matriax4x4 tY = m_rotationY.Transpose();
                m_rotationY = m_rotationY.Mul(tY);
            }

            if (this.checkBox3.Checked)
            {
                Matriax4x4 tZ = m_rotationZ.Transpose();
                m_rotationZ = m_rotationZ.Mul(tZ);
            }

            Matriax4x4 mAll = m_rotationX.Mul(m_rotationY.Mul(m_rotationZ));
            Matriax4x4 m    = m_scale.Mul(mAll);

            //计算光照              光照向量
            // t.CalculateLighting(m, new Vector4(-1, 1, -1, 0));

            cube.CalculateLighting(m, new Vector4(-1, 1, -1, 0));

            Matriax4x4 mv  = m.Mul(m_view);
            Matriax4x4 mvp = mv.Mul(m_projection);

            //t.Tranform(mvp);
            cube.Transform(mvp);
            this.Invalidate();
        }