Exemple #1
0
 public Vektor(Vektor anfang, Vektor ende)
 {
     X = ende.X - anfang.X;
     Y = ende.Y - anfang.Y;
     Z = ende.Z - anfang.Z;
     W = 1;
 }
Exemple #2
0
        public static double[,] lookAt(Vektor pos, Vektor target, Vektor up)
        {
            Vektor zaxis = pos - target;

            zaxis = zaxis.Normalise();

            Vektor xaxis = Vektor.CrossProduct(up, zaxis);

            xaxis = xaxis.Normalise();

            Vektor yaxis = Vektor.CrossProduct(zaxis, xaxis);

            double[,] orientation =
            {
                { xaxis.X, yaxis.X, zaxis.X, 0 },
                { xaxis.Y, yaxis.Y, zaxis.Y, 0 },
                { xaxis.Z, yaxis.Z, zaxis.Z, 0 },
                {       0,       0,       0, 1 }
            };
            double[,] translation =
            {
                { 1, 0, 0, -pos.X },
                { 0, 1, 0, -pos.Y },
                { 0, 0, 1, -pos.Z },
                { 0, 0, 0,      1 }
            };

            return(Matrix.MultiplyMatrix(orientation, translation));
        }
Exemple #3
0
        public static Vektor CrossProduct(Vektor v1, Vektor v2)
        {
            Vektor v = new Vektor(
                x: v1.Y * v2.Z - v1.Z * v2.Y,
                y: v1.Z * v2.X - v1.X * v2.Z,
                z: v1.X * v2.Y - v1.Y * v2.X);

            return(v);
        }
Exemple #4
0
        public static Vektor CalcNormals(Vektor v1, Vektor v2)
        {
            Vektor v = CrossProduct(v1, v2);

            v = v.Normalise();
            return(new Vektor(
                       x: v.X,
                       y: v.Y,
                       z: v.Z));
        }
Exemple #5
0
 public static double[,] ToMatrix(Vektor v)
 {
     double[,] matrix =
     {
         { v.X },
         { v.Y },
         { v.Z },
         { v.W }
     };
     return(matrix);
 }
Exemple #6
0
        static Vektor VektorIntersectPlane(Vektor planeP, Vektor planeN, Vektor lineStart, Vektor lineEnd)
        {
            planeN = planeN.Normalise();
            double planeD          = -Vektor.DotProduct(planeN, planeP);
            double ad              = Vektor.DotProduct(lineStart, planeN);
            double bd              = Vektor.DotProduct(lineEnd, planeN);
            double t               = (-planeD - ad) / (bd - ad);
            Vektor lineStartToEnd  = lineEnd - lineStart;
            Vektor lineToIntersect = lineStartToEnd * new Vektor(t, t, t);

            return(lineStart + lineToIntersect);
        }
Exemple #7
0
        public static Vektor MultiplyVektor(double[,] a, Vektor v)
        {
            double[,] b =
            {
                { v.X },
                { v.Y },
                { v.Z },
                { v.W }
            };

            return(ToVektor(MultiplyMatrix(a, b)));
        }
Exemple #8
0
        private Vektor PerspectiveProjectionMatrix(Vektor input)
        {
            // ReSharper disable once PossibleLossOfFraction
            double aspectRation = 1920 / 1080; // auflösung Bildschirm

            Vektor ergebnis = Matrix.MultiplyVektor(Matrix.Projecton(fov, aspectRation, far, near), input);
            double bx       = ergebnis.X;
            double by       = ergebnis.Y;

            if (ergebnis.W != 0)
            {
                bx = ergebnis.X / ergebnis.W;
                by = ergebnis.Y / ergebnis.W;
            }
            return(new Vektor(bx, by, 0));
        }
Exemple #9
0
        public static double[,] WorldMatrix(Vektor angle, Vektor abstand)
        {
            double[,] rotateX = RotateX(angle.X);
            double[,] rotateY = RotateY(angle.Y);
            //double[,] rotateZ = Matrix.RotateZ(angle);

            double[,] translate = Translation(abstand.X, abstand.Y, abstand.Z);

            double[,] worldMatrix = Identety();
            worldMatrix           = MultiplyMatrix(worldMatrix, translate);
            worldMatrix           = MultiplyMatrix(worldMatrix, rotateX);
            worldMatrix           = MultiplyMatrix(worldMatrix, rotateY);
            //worldMatrix = Matrix.MultiplyMatrix(worldMatrix, rotateZ);

            return(worldMatrix);
        }
Exemple #10
0
        public static double[,] FPSCamera(Vektor eye, double pitch, double yaw)
        {
            double cosPitch = Math.Cos(pitch);
            double sinPitch = Math.Sin(pitch);
            double cosYaw   = Math.Cos(yaw);
            double sinYaw   = Math.Sin(yaw);

            Vektor xaxis = new Vektor(cosYaw, 0, -sinYaw);
            Vektor yaxis = new Vektor(sinYaw * sinPitch, cosPitch, cosYaw * sinPitch);
            Vektor zaxis = new Vektor(sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw);

            double[,] matrix =
            {
                { xaxis.X, yaxis.X, zaxis.X, -Vektor.DotProduct(xaxis, eye) },
                { xaxis.Y, yaxis.Y, zaxis.Y, -Vektor.DotProduct(yaxis, eye) },
                { xaxis.Z, yaxis.Z, zaxis.Z, -Vektor.DotProduct(zaxis, eye) },
                {       0,       0,       0, 1 }
            };

            return(matrix);
        }
Exemple #11
0
        public static double[,] PointAt(Vektor pos, Vektor target, Vektor up)
        {
            Vektor zaxis = pos - target;

            zaxis = zaxis.Normalise();

            Vektor xaxis = Vektor.CrossProduct(up, zaxis);

            xaxis = xaxis.Normalise();

            Vektor yaxis = Vektor.CrossProduct(zaxis, xaxis);

            double[,] matrix =
            {
                { xaxis.X, xaxis.Y, xaxis.Z, pos.X },
                { yaxis.X, yaxis.Y, yaxis.Z, pos.Y },
                { zaxis.X, zaxis.Y, zaxis.Z, pos.Z },
                {       0,       0,       0,     1 }
            };

            return(matrix);
        }
Exemple #12
0
        void Project(Import import, double angle)
        {
            List <Vektor>   translatedVerts    = new List <Vektor>();
            List <Triangle> projectedTriangles = new List <Triangle>();
            List <Color>    projectedTriColor  = new List <Color>();

            Vektor target = new Vektor(0, 0, 1);

            double[,] matCameraRotY = Matrix.RotateY(Yaw);
            double[,] matCameraRotX = Matrix.RotateX(Pitch);
            _lookDirCamera          = Matrix.MultiplyVektor(matCameraRotY, target);
            target = _camera + _lookDirCamera;

            double[,] matView = Matrix.lookAt(_camera, target, _up);

            for (int j = 0; j < import.Verts.Count; j++)
            {
                Vektor element = import.Verts[j];

                Vektor erg = Matrix.MultiplyVektor(Matrix.WorldMatrix(new Vektor(angle, angle, angle), new Vektor(0, 0, abstand)), element);

                translatedVerts.Add(erg);
            }

            List <Triangle> sortedTriangles    = new List <Triangle>();
            List <Triangle> sortedTrianglesF   = new List <Triangle>();
            Dictionary <Triangle, double> sort = new Dictionary <Triangle, double>();

            foreach (var triangle in import.CreateTriangles(translatedVerts))
            {
                sort.Add(triangle, triangle.Tp1.Z + triangle.Tp2.Z + triangle.Tp3.Z / 3);
            }

            foreach (KeyValuePair <Triangle, double> author in sort.OrderBy(key => key.Value))
            {
                sortedTriangles.Add(author.Key);
            }
            for (int t = 0; t < sortedTriangles.Count; t++)
            {
                sortedTrianglesF.Add(sortedTriangles[sortedTriangles.Count - 1 - t]);
            }

            foreach (var triangle in sortedTrianglesF)
            {
                Vektor line1 = new Vektor(triangle.Tp2, triangle.Tp1);
                Vektor line2 = new Vektor(triangle.Tp2, triangle.Tp3);

                Vektor normal = Vektor.CalcNormals(line1, line2);

                Vektor pointToCamera = _camera - triangle.Tp1;

                if (Vektor.DotProduct(pointToCamera, normal) < 0)
                {
                    Vektor lightDirection = _camera + new Vektor(0, 0, -1);
                    lightDirection = lightDirection.Normalise();

                    double dp        = Vektor.DotProduct(normal, lightDirection);
                    var    grayValue = Convert.ToByte(Math.Abs(dp * Byte.MaxValue));
                    var    col       = Color.FromArgb(250, grayValue, grayValue, grayValue);

                    var tp1 = Matrix.MultiplyVektor(matView, triangle.Tp1);
                    var tp2 = Matrix.MultiplyVektor(matView, triangle.Tp2);
                    var tp3 = Matrix.MultiplyVektor(matView, triangle.Tp3);

                    Triangle triViewed = new Triangle(tp1, tp2, tp3);

                    int        clippedTriangles = 0;
                    Triangle[] clipped          = new Triangle[4];

                    clippedTriangles += Clipping.Triangle_ClipAgainstPlane(new Vektor(0, 0, -near), new Vektor(0, 0, -1), triViewed);
                    clipped[0]        = Clipping.outTri1;
                    clipped[1]        = Clipping.outTri2;

                    for (int n = 0; n < clippedTriangles; n++)
                    {
                        Vektor projectedPoint1 = PerspectiveProjectionMatrix(clipped[n].Tp1);
                        Vektor projectedPoint2 = PerspectiveProjectionMatrix(clipped[n].Tp2);
                        Vektor projectedPoint3 = PerspectiveProjectionMatrix(clipped[n].Tp3);

                        projectedTriangles.Add(new Triangle(projectedPoint1, projectedPoint2, projectedPoint3));
                        projectedTriColor.Add(col);
                    }
                }
            }

            foreach (Triangle triToRaster in projectedTriangles)
            {
                int             c             = 0;
                Triangle[]      clipped       = new Triangle[2];
                List <Triangle> listTriangles = new List <Triangle>();
                Color           col           = projectedTriColor[c];
                listTriangles.Add(triToRaster);
                int newTriangles = 1;

                for (int p = 0; p < 4; p++)
                {
                    int TrisToAdd = 0;
                    while (newTriangles > 0)
                    {
                        Triangle test = listTriangles[0];
                        listTriangles.RemoveAt(0);
                        newTriangles--;

                        switch (p)
                        {
                        case 0:
                            TrisToAdd  = Clipping.Triangle_ClipAgainstPlane(new Vektor(0, (-_drawingSurface.Surface.ActualHeight / 2) / 50, 0), new Vektor(0, 1, 0), test);
                            clipped[0] = Clipping.outTri1;
                            clipped[1] = Clipping.outTri2;
                            break;

                        case 1:
                            TrisToAdd  = Clipping.Triangle_ClipAgainstPlane(new Vektor(0, (_drawingSurface.Surface.ActualHeight / 2) / 50, 0), new Vektor(0, -1, 0), test);
                            clipped[0] = Clipping.outTri1;
                            clipped[1] = Clipping.outTri2;
                            break;

                        case 2:
                            TrisToAdd  = Clipping.Triangle_ClipAgainstPlane(new Vektor((-_drawingSurface.Surface.ActualWidth / 2) / 50, 0, 0), new Vektor(1, 0, 0), test);
                            clipped[0] = Clipping.outTri1;
                            clipped[1] = Clipping.outTri2;
                            break;

                        case 3:
                            TrisToAdd  = Clipping.Triangle_ClipAgainstPlane(new Vektor((_drawingSurface.Surface.ActualWidth / 2) / 50, 0, 0), new Vektor(-1, 0, 0), test);
                            clipped[0] = Clipping.outTri1;
                            clipped[1] = Clipping.outTri2;
                            break;
                        }

                        for (int w = 0; w < TrisToAdd; w++)
                        {
                            listTriangles.Add(clipped[w]);
                        }
                    }
                    newTriangles = listTriangles.Count;
                }

                for (int i = 0; i < listTriangles.Count; i++)
                {
                    _drawingSurface.Triangle(listTriangles[i], col);
                }
                c++;
            }
        }
Exemple #13
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.P)
            {
                _timer.IsEnabled ^= true;
                e.Handled         = true;
            }

            Vektor Forward = _lookDirCamera * new Vektor(.1, .1, -.1);

            if (e.Key == Key.Space)
            {
                _camera   = new Vektor(x: _camera.X, y: _camera.Y - .1, z: _camera.Z);
                e.Handled = true;
            }
            if (e.Key == Key.LeftCtrl)
            {
                _camera   = new Vektor(x: _camera.X, y: _camera.Y + .1, z: _camera.Z);
                e.Handled = true;
            }


            if (e.Key == Key.A)
            {
                _camera  += Matrix.MultiplyVektor(Matrix.RotateY(90), _lookDirCamera * new Vektor(.1, .1, -.1));
                e.Handled = true;
            }
            if (e.Key == Key.D)
            {
                _camera  -= Matrix.MultiplyVektor(Matrix.RotateY(90), _lookDirCamera * new Vektor(.1, .1, -.1));
                e.Handled = true;
            }

            if (e.Key == Key.W)
            {
                _camera  -= Forward;
                e.Handled = true;
            }
            if (e.Key == Key.S)
            {
                _camera  += Forward;
                e.Handled = true;
            }

            if (e.Key == Key.Left)
            {
                Yaw      += 1;
                e.Handled = true;
            }
            if (e.Key == Key.Right)
            {
                Yaw      -= 1;
                e.Handled = true;
            }

            if (e.Key == Key.Up)
            {
                Pitch    += 1;
                e.Handled = true;
            }
            if (e.Key == Key.Down)
            {
                Pitch    -= 1;
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }
Exemple #14
0
        public static int Triangle_ClipAgainstPlane(Vektor planeP, Vektor planeN, Triangle inTri)
        {
            outTri1 = new Triangle();
            outTri2 = new Triangle();

            planeN = planeN.Normalise();

            Vektor[] insidePoints      = new Vektor[3];
            Vektor[] outsidePoints     = new Vektor[3];
            int      InsidePointCount  = 0;
            int      OutsidePointCount = 0;

            double d0 = calcdis(inTri.Tp1, planeP, planeN);
            double d1 = calcdis(inTri.Tp2, planeP, planeN);
            double d2 = calcdis(inTri.Tp3, planeP, planeN);

            if (d0 > 0)
            {
                insidePoints[InsidePointCount++] = inTri.Tp1;
            }
            else
            {
                outsidePoints[OutsidePointCount++] = inTri.Tp1;
            }

            if (d1 > 0)
            {
                insidePoints[InsidePointCount++] = inTri.Tp2;
            }
            else
            {
                outsidePoints[OutsidePointCount++] = inTri.Tp2;
            }

            if (d2 > 0)
            {
                insidePoints[InsidePointCount++] = inTri.Tp3;
            }
            else
            {
                outsidePoints[OutsidePointCount++] = inTri.Tp3;
            }

            if (InsidePointCount == 0)
            {
                return(0);
            }

            if (InsidePointCount == 3)
            {
                outTri1 = inTri;
                return(1);
            }

            if (InsidePointCount == 1 && OutsidePointCount == 2)
            {
                outTri1.Tp1 = insidePoints[0];

                outTri1.Tp2 = VektorIntersectPlane(planeP, planeN, insidePoints[0], outsidePoints[0]);
                outTri1.Tp3 = VektorIntersectPlane(planeP, planeN, insidePoints[0], outsidePoints[1]);

                return(1);
            }

            if (InsidePointCount == 2 && OutsidePointCount == 1)
            {
                outTri1.Tp1 = insidePoints[0];
                outTri1.Tp2 = insidePoints[1];
                outTri1.Tp3 = VektorIntersectPlane(planeP, planeN, insidePoints[0], outsidePoints[0]);

                outTri2.Tp1 = insidePoints[1];
                outTri2.Tp2 = outTri1.Tp3;
                outTri2.Tp3 = VektorIntersectPlane(planeP, planeN, insidePoints[1], outsidePoints[0]);

                return(2);
            }

            return(1);
        }
Exemple #15
0
        static double calcdis(Vektor p, Vektor planeP, Vektor planeN)
        {
            Vektor erg = planeN * (p - planeP);

            return(erg.X + erg.Y + erg.Z);
        }
Exemple #16
0
 public static double DotProduct(Vektor v1, Vektor v2)
 {
     return(v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z);
 }