Пример #1
0
        public void UpdateCameraRotation(float r)
        {
            CameraRotation += r;

            CameraForward = CustomMath.SimpleRotateVectorByRadian(CameraForward, r);
            CameraLeft    = CustomMath.SimpleRotateVectorByDegree(CameraForward, 90);
            CameraRight   = CustomMath.SimpleRotateVectorByDegree(CameraForward, -90);
        }
Пример #2
0
        public void UpdateVisibleFaces(List <Model> batch)
        {
            foreach (var model in batch)
            {
                var mesh          = model.Mesh;
                var modelPosition = model.Position;
                var yRot          = model.RotationY;
                var xRot          = model.RotationX;

                var modelMatrix = Matrix.RotationMatrixY(yRot) * Matrix.RotationMatrixX(xRot) *
                                  Matrix.Translation(modelPosition);

                var angle2 = CustomMath.ConvertRadiansToDegrees(CustomMath.SimpleAngleBetweenTwoVectors(CameraForward, modelPosition - CameraPosition));

                if (angle2 > 90 || angle2 < -90)
                {
                    break;
                }

                for (int i = 0; i < mesh.Triangles.Length; i++)
                {
                    Triangle tri = mesh.Triangles[i];

                    Triangle transformed = tri.MatMul(modelMatrix);
                    transformed = transformed.MatMul(viewMatrix);
                    transformed = transformed.MatMul(projectionMatrix);

                    Vector3 normal, line1, line2;

                    line1 = transformed.p[1] - transformed.p[0];
                    line2 = transformed.p[2] - transformed.p[0];

                    normal = Vector3.Cross(line1, line2);
                    normal = Vector3.Normalize(normal);

                    if (Vector3.Dot(normal, transformed.p[0] - CameraPosition) < 0.0f)
                    {
                        Triangle projected  = transformed;
                        Vector3  offsetView = new Vector3(1, 1, 0);

                        projected.p[0] += offsetView;
                        projected.p[1] += offsetView;
                        projected.p[2] += offsetView;

                        projected.p[0].X *= 0.5f * consoleWidth; projected.p[0].Y *= 0.5f * consoleHeight;
                        projected.p[1].X *= 0.5f * consoleWidth; projected.p[1].Y *= 0.5f * consoleHeight;
                        projected.p[2].X *= 0.5f * consoleWidth; projected.p[2].Y *= 0.5f * consoleHeight;

                        float            l = Vector3.Dot(lightDirection, normal);
                        ConsoleCharacter character;
                        if (l > 1)
                        {
                            character = ConsoleCharacter.Full;
                        }
                        else if (l > 0.7)
                        {
                            character = ConsoleCharacter.Dark;
                        }
                        else if (l > 0.4)
                        {
                            character = ConsoleCharacter.Medium;
                        }
                        else
                        {
                            character = ConsoleCharacter.Light;
                        }
                        projected.c = character;

                        trianglesToRaster.Add(projected);
                    }
                }
            }

            // sortowanie
            trianglesToRaster.Sort((t1, t2) => ((t2.p[0].Z + t2.p[1].Z + t2.p[2].Z).CompareTo((t1.p[0].Z + t1.p[1].Z + t1.p[2].Z))));
        }