Beispiel #1
0
        //public void UpdateCameraPosition(float _x, float _y, float _z)
        //{
        //    vector_t eye = new vector_t(_x, _y, _z, 1);
        //    vector_t at = new vector_t(0, 0, 0, 1);
        //    vector_t up = new vector_t(0, 0, 1, 1);
        //    SetLookat(eye, at, up);
        //}

        // 设置摄像机
        void SetLookat(vector_t eye, vector_t at, vector_t up)
        {
            vector_t xaxis, yaxis, zaxis;

            zaxis = Tools.vector_sub(at, eye);
            zaxis = Tools.vector_normalize(zaxis);

            xaxis = Tools.vector_crossproduct(up, zaxis);
            xaxis = Tools.vector_normalize(xaxis);

            yaxis = Tools.vector_crossproduct(zaxis, xaxis);

            mTransformer.view.m[0, 0] = xaxis.x;
            mTransformer.view.m[1, 0] = xaxis.y;
            mTransformer.view.m[2, 0] = xaxis.z;
            mTransformer.view.m[3, 0] = -Tools.vector_dotproduct(xaxis, eye);

            mTransformer.view.m[0, 1] = yaxis.x;
            mTransformer.view.m[1, 1] = yaxis.y;
            mTransformer.view.m[2, 1] = yaxis.z;
            mTransformer.view.m[3, 1] = -Tools.vector_dotproduct(yaxis, eye);

            mTransformer.view.m[0, 2] = zaxis.x;
            mTransformer.view.m[1, 2] = zaxis.y;
            mTransformer.view.m[2, 2] = zaxis.z;
            mTransformer.view.m[3, 2] = -Tools.vector_dotproduct(zaxis, eye);

            mTransformer.view.m[0, 3] = mTransformer.view.m[1, 3] = mTransformer.view.m[2, 3] = 0.0f;
            mTransformer.view.m[3, 3] = 1.0f;
        }
Beispiel #2
0
        void CameraInit(float x, float y, float z)
        {
            mCameraVector = new vector_t(x, y, z, 1);
            vector_t at = new vector_t(0, 0, 0, 1);
            vector_t up = new vector_t(0, 0, 1, 1);

            SetLookat(mCameraVector, at, up);
            mTransformer.UpdateTransform();
        }
Beispiel #3
0
        public static float Triangle_Acreage_2D(point_t p1, point_t p2, point_t p3)
        {
            float    _tmp    = 0f;
            vector_t _vec1_2 = new vector_t(p2.x - p1.x, p2.y - p1.y, 0, 0);
            vector_t _vec1_3 = new vector_t(p3.x - p1.x, p3.y - p1.y, 0, 0);

            _tmp = Tools.vector_length(Tools.vector_crossproduct(_vec1_2, _vec1_3)) / 2;
            return(Math.Abs(_tmp));
        }
Beispiel #4
0
        private void buttonRight_Click(object sender, EventArgs e)
        {
            vector_t _newRotate = new vector_t(mDevice.mRotateVector.x, mDevice.mRotateVector.y, Tools.CMID_F(mDevice.mRotateVector.z - 0.1f, -1f, 1f), mDevice.mRotateVector.w);

            mDevice.mRotateVector = _newRotate;
            mDevice.ResetFrameBuffer();
            mDevice.DrawBox();
            DrawCall();
        }
Beispiel #5
0
        // 矢量归一化
        public static vector_t vector_normalize(vector_t v)
        {
            float length = vector_length(v);

            if (length != 0.0f)
            {
                float inv = 1.0f / length;
                return(new vector_t(v.x * inv, v.y * inv, v.z * inv, v.w));
            }
            return(v);
        }
Beispiel #6
0
        // 归一化,得到屏幕坐标
        public static vector_t Homogenize(vector_t x)
        {
            vector_t _tmp = new vector_t();
            float    rhw  = 1.0f / x.w;

            _tmp.x = (x.x * rhw + 1.0f) * Def.DeviceWidth * 0.5f;
            _tmp.y = (1.0f - x.y * rhw) * Def.DeviceHeight * 0.5f;
            _tmp.z = x.z * rhw;
            _tmp.w = 1.0f;
            return(_tmp);
        }
Beispiel #7
0
        public Device()
        {
            mTransformer = new Transformer();
            mTransformer.Init();
            frameBuffer = new Bitmap(Def.DeviceWidth, Def.DeviceHeight);

            //TestDevice();
            CameraInit(3, 0, 0);

            Def.InitMesh_Box();

            mRotateVector = new vector_t(0.1f, 0.7f, 0.5f, 1f);
            //mRotateVector = new vector_t(0f, 0f, 0f, 1f);
            DrawBox();
        }
Beispiel #8
0
        //是否可剔除
        bool TriangleCullingJudge_A(vector_t v1, vector_t v2, vector_t v3)
        {
            vector_t _v1_2   = Tools.vector_normalize(Tools.vector_sub(v2, v1));
            vector_t _v1_3   = Tools.vector_normalize(Tools.vector_sub(v3, v1));
            vector_t _normal = Tools.vector_crossproduct(_v1_2, _v1_3);

            vector_t _cameraVector = Tools.matrix_apply(mCameraVector, mTransformer.transform);

            float _val = Tools.vector_dotproduct(_normal, _cameraVector);

            if (_val > 0f)
            {
                return(false);
            }
            return(true);
        }
Beispiel #9
0
        // v(vector_t) * m(matrix_t) 这里默认矩阵row = 4
        public static vector_t matrix_apply(vector_t v, matrix_t m)
        {
            vector_t _tmp = v;

            if (m.row == 4)
            {
                float[] _val = new float[4];
                for (int i = 0; i < 4; ++i)
                {
                    _val[i] = v.x * m.m[0, i] + v.y * m.m[1, i] + v.z * m.m[2, i] + v.w * m.m[3, i];
                }
                _tmp.x = _val[0]; _tmp.y = _val[1]; _tmp.z = _val[2]; _tmp.w = _val[3];
            }
            else
            {
                System.Diagnostics.Debug.Assert(false, "matrix_t Operation Error : matrix_apply");
            }
            return(_tmp);
        }
Beispiel #10
0
        public void SetRotate(float x, float y, float z, float theta)
        {
            float    qsin = (float)Math.Sin(theta * 0.5f);
            float    qcos = (float)Math.Cos(theta * 0.5f);
            vector_t vec  = new vector_t(x, y, z, 1f);
            float    w    = qcos;

            Tools.vector_normalize(vec);
            x       = vec.x * qsin;
            y       = vec.y * qsin;
            z       = vec.z * qsin;
            m[0, 0] = 1 - 2 * y * y - 2 * z * z;
            m[1, 0] = 2 * x * y - 2 * w * z;
            m[2, 0] = 2 * x * z + 2 * w * y;
            m[0, 1] = 2 * x * y + 2 * w * z;
            m[1, 1] = 1 - 2 * x * x - 2 * z * z;
            m[2, 1] = 2 * y * z - 2 * w * x;
            m[0, 2] = 2 * x * z - 2 * w * y;
            m[1, 2] = 2 * y * z + 2 * w * x;
            m[2, 2] = 1 - 2 * x * x - 2 * y * y;
            m[0, 3] = m[1, 3] = m[2, 3] = 0f;
            m[3, 0] = m[3, 1] = m[3, 2] = 0f;
            m[3, 3] = 1f;
        }
Beispiel #11
0
 // 矢量插值,t取值 [0, 1]
 public static vector_t vector_interp(vector_t v1, vector_t v2, float t)
 {
     return(new vector_t(interp(v1.x, v2.x, t), interp(v1.y, v2.y, t), interp(v1.z, v2.z, t), 1));
 }
Beispiel #12
0
 // 矢量叉乘
 public static vector_t vector_crossproduct(vector_t v1, vector_t v2)
 {
     return(new vector_t(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x, 1f));
 }
Beispiel #13
0
 // 矢量点乘
 public static float vector_dotproduct(vector_t v1, vector_t v2)
 {
     return(v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
 }
Beispiel #14
0
 // v1 - v2
 public static vector_t vector_sub(vector_t v1, vector_t v2)
 {
     return(new vector_t(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, 1f));
 }
Beispiel #15
0
 // v1 + v2
 public static vector_t vector_add(vector_t v1, vector_t v2)
 {
     return(new vector_t(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, 1f));
 }
Beispiel #16
0
 // | v |
 public static float vector_length(vector_t v)
 {
     return((float)Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z));
 }
Beispiel #17
0
 // 旋转矩阵
 public void SetRotate(vector_t _rotateV)
 {
     SetRotate(_rotateV.x, _rotateV.y, _rotateV.z, _rotateV.w);
 }