Ejemplo n.º 1
0
        public void Render(Camera camera, params Mesh[] meshes)
        {
            var viewMatrix       = Matrix.LookAtLH(camera.Position, camera.Target, camera.Up);
            var projectionMatrix = Matrix.PerspectiveFovLH(0.78f, (float)width / height, 0.01f, 1.0f);

            foreach (var mesh in meshes)
            {
                var worldMatrix = Matrix.RotationYawPitchRoll(mesh.Rotation.Y, mesh.Rotation.X, mesh.Rotation.Z) *
                                  Matrix.Translation(mesh.Position);

                var worldView       = worldMatrix * viewMatrix;
                var transformMatrix = worldView * projectionMatrix;

                Parallel.For(0, mesh.Faces.Length, faceIndex =>
                {
                    var face = mesh.Faces[faceIndex];

                    // Face-back culling
                    //var transformedNormal = Vector3.TransformNormal(face.Normal, worldView);

                    //if (transformedNormal.Z >= 0)
                    //{
                    //    return;
                    //}

                    // Render this face
                    var vertexA = mesh.Vertices[face.A];
                    var vertexB = mesh.Vertices[face.B];
                    var vertexC = mesh.Vertices[face.C];

                    var pixelA = Project(vertexA, transformMatrix, worldMatrix);
                    var pixelB = Project(vertexB, transformMatrix, worldMatrix);
                    var pixelC = Project(vertexC, transformMatrix, worldMatrix);

                    //var color = 0.25f + (faceIndex % mesh.Faces.Length) * 0.75f / mesh.Faces.Length;
                    var color = 1.0f;


                    if (Wireframe)
                    {
                        Algorithm.Line(pixelA.Coordinates.X, pixelA.Coordinates.Y, pixelB.Coordinates.X,
                                       pixelB.Coordinates.Y, Color.White, DrawPoint);
                        Algorithm.Line(pixelB.Coordinates.X, pixelB.Coordinates.Y, pixelC.Coordinates.X,
                                       pixelC.Coordinates.Y, Color.White, DrawPoint);
                        Algorithm.Line(pixelC.Coordinates.X, pixelC.Coordinates.Y, pixelA.Coordinates.X,
                                       pixelA.Coordinates.Y, Color.White, DrawPoint);
                    }
                    else
                    {
                        DrawTriangle(pixelA, pixelB, pixelC, new Color4(color, color, color, 1), mesh.Texture);
                    }
                });
            }
        }
Ejemplo n.º 2
0
        public void Update()
        {
            //Calculate deltaTime
            var passedTime = (float)(DateTime.Now - _prevFrameTime).Milliseconds / 1000;

            //Determine direction of travel
            var moveDirection = Vector2.Zero;

            moveDirection.X = Keyboard.IsKeyDown(Key.D) ? 1.0f : 0.0f;
            if (moveDirection.X.Equals(0.0f))
            {
                moveDirection.X = -(Keyboard.IsKeyDown(Key.A) ? 1.0f : 0.0f);
            }

            moveDirection.Y = Keyboard.IsKeyDown(Key.W) ? 1.0f : 0.0f;
            if (moveDirection.Y.Equals(0.0f))
            {
                moveDirection.Y = -(Keyboard.IsKeyDown(Key.S) ? 1.0f : 0.0f);
            }

            var currPos = Mouse.GetPosition(Application.Current.MainWindow);

            //Mouse Rotation
            var mouseLook = Vector2.Zero;

            if (Mouse.RightButton == MouseButtonState.Pressed)
            {
                var mouseMovement = currPos - _prevMousePos;

                //Debug.Log(LogLevel.Info,mouseMovement.ToString());
                //mouseLook.X = (float) (mouseMovement.X/Math.Abs(mouseMovement.X));
                //mouseLook.Y = (float)(mouseMovement.Y / Math.Abs(mouseMovement.Y));

                mouseLook.X = (float)(mouseMovement.X);
                mouseLook.Y = (float)(mouseMovement.Y);
            }

            //Calculate rotation
            _totalYaw   += mouseLook.X * ROTATION_SPEED * passedTime;
            _totalPitch += mouseLook.Y * ROTATION_SPEED * passedTime;
            Rotating     = Matrix.RotationYawPitchRoll(_totalYaw, _totalPitch, 0);

            Translation = Matrix.Translation(EyePosition);
            Scale       = Matrix.Scaling(1.0f);

            //Update Forward,Right and up based on the current rotation of the Camera
            Forward     = Vector3.TransformCoordinate(Vector3.ForwardLH, Rotating);
            Right       = Vector3.TransformCoordinate(Vector3.Right, Rotating);
            UpDirection = Vector3.Cross(Forward, Right);

            EyePosition += moveDirection.X * Right * MOVE_SPEED * passedTime;
            EyePosition += moveDirection.Y * Forward * MOVE_SPEED * passedTime;


            var viewDirection = (Vector3)Vector3.Transform(Vector3.ForwardLH, Rotating);

            viewDirection.Normalize();
            TargetPosition = EyePosition + viewDirection;

            _prevFrameTime = DateTime.Now;
            _prevMousePos  = currPos;

            //Debug.Log(LogLevel.Info,EyePosition.ToString());
        }