Exemple #1
0
        private async void PageLoaded(object sender, RoutedEventArgs e)
        {
            var bmp = new WriteableBitmap(1024, 768);
            imageControl.Source = bmp;

            device = new RenderDevice(bmp);
            camera = new Camera
            {
                Position = new Vector3(0, 0, 10.0f),
                Target = Vector3.Zero
            };

            var importer = new BabylonImporter();
            var importedMeshes = await importer.LoadFileAsync("model.babylon");

            meshes = new List<Mesh>();
            foreach(var mesh in importedMeshes)
                meshes.Add(mesh);
            foreach(var mesh in importedMeshes)
            {
                var newMesh = new Mesh("MonkeyClone", mesh.Vertices, mesh.Faces);
                newMesh.Position = new Vector3(2.65f, 0, 0);
                meshes.Add(newMesh);
            }

            CompositionTarget.Rendering += Render;
        }
Exemple #2
0
        public void Render(Camera camera, List<Mesh> meshes)
        {
            var viewMatrix = Matrix.LookAtLH(camera.Position, camera.Target, Vector3.UnitY);
            var projectionMatrix = Matrix.PerspectiveFovLH(0.78f, (float)renderWidth / renderHeight, 0.01f, 1.0f);

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

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

                    var vertexA = mesh.Vertices[face.V0];
                    var vertexB = mesh.Vertices[face.V1];
                    var vertexC = mesh.Vertices[face.V2];

                    var pix0 = Project(ref vertexA, ref transformMatrix, ref worldMatrix);
                    var pix1 = Project(ref vertexB, ref transformMatrix, ref worldMatrix);
                    var pix2 = Project(ref vertexC, ref transformMatrix, ref worldMatrix);

                    DrawTriangle(ref pix0, ref pix1, ref pix2, new Color4(1.0f));
                });
            }
        }