コード例 #1
0
        private static async Task LoadModel()
        {
            try
            {
                Console.WriteLine("Select a file (located in ./Models/) :");

                // List files.
                foreach (var file in Directory.GetFiles("./Models/"))
                {
                    Console.WriteLine(Path.GetFileName(file));
                }
                Console.WriteLine();
                Console.Write("File (e.g. foobar.txt) : ");
                var filename = Console.ReadLine();

                var fileContent = File.ReadAllText($"./Models/{filename}");

                _model         = ModelData.Deserialize(fileContent);
                _originalModel = _model.Clone();
                await _renderer.RenderAwaitableAsync(_originalModel);

                WriteSuccess("Model loaded.");
            }
            catch (Exception e)
            {
                WriteError($"Error loading model. Error : {e.Message}");
            }
        }
コード例 #2
0
        private static async void StartInspectionAnimation(InspectionAnimations inspectionAnimation, CancellationToken cancellationToken)
        {
            // Save model state
            var savedModel = _model.Clone();

            _renderer.LoggingEnabled = false;
            await Task.Run(async() =>
            {
                while (true)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        _model = savedModel;
                        await _renderer.RenderAwaitableAsync(_model);
                        _renderer.LoggingEnabled = true;
                        break;
                    }

                    double DegreesToRadians(double degrees) => degrees * (Math.PI / 180);

                    Transformation transformation = null;
                    switch (inspectionAnimation)
                    {
                    case InspectionAnimations.RotateAlongXAxis:
                        transformation = new Transformation()
                        {
                            TransformName = "RotateX",
                            theta         = DegreesToRadians(0.5)
                        };
                        break;

                    case InspectionAnimations.RotateAlongYAxis:
                        transformation = new Transformation()
                        {
                            TransformName = "RotateY",
                            theta         = DegreesToRadians(0.5)
                        };
                        break;

                    case InspectionAnimations.RotateAlongZAxis:
                        transformation = new Transformation()
                        {
                            TransformName = "RotateZ",
                            theta         = DegreesToRadians(0.5)
                        };
                        break;
                    }

                    _model.Points = Transformation.Transform((List <Point3D>)_model.Points, new List <Transformation>()
                    {
                        transformation
                    });

                    _renderer.Render(_model);

                    await Task.Delay(5);
                }
            });
        }
コード例 #3
0
        async static Task Main(string[] args)
        {
            try
            {
                ConsoleHelper.Initialize();

                // Print identity.
                Console.WriteLine("Tugas Kelompok Teknik Visualisasi Grafis - Transformasi 3D");
                Console.WriteLine("Identitas Anggota Kelompok :");
                Console.WriteLine("Daffa Bil Nadzary                19/439811/TK/48541");
                Console.WriteLine("Firdaus Bisma Suryakusuma        19/444051/TK/49247");
                Console.WriteLine("Hafizh Aradhana Harimurti        19/444053/TK/49249");
                Console.WriteLine("Please wait a while.");
                Console.WriteLine();
                await Task.Delay(3000);

                // Start up renderer
                _renderer = new RendererServer();

                Console.WriteLine();

                // Load default model
                Console.WriteLine("Loading default model(cube)...");
                var modelFileContent = File.ReadAllText($"./Models/default-cube.txt");
                _model         = ModelData.Deserialize(modelFileContent);
                _originalModel = _model.Clone();
                await _renderer.RenderAwaitableAsync(_originalModel);

                // Load default scene
                Console.WriteLine("Loading default scene...");
                var sceneFileContent = File.ReadAllText($"./Scenes/default-scene.txt");
                _scene = SceneData.Deserialize(sceneFileContent);
                await _renderer.SetSceneAwaitableAsync(_scene);

                Console.WriteLine();

                // Prompt action
                while (true)
                {
                    Console.WriteLine("Select an action :");
                    Console.WriteLine("[0] Load model");
                    Console.WriteLine("[1] Load scene");
                    Console.WriteLine("[2] Save model");
                    Console.WriteLine("[3] Save scene");
                    Console.WriteLine("[4] Perform a single transformation");
                    Console.WriteLine("[5] Chain multiple transformations");
                    Console.WriteLine("[6] Inspect model");
                    Console.WriteLine("[7] Stop inspecting model");
                    Console.WriteLine("[8] Reset model");
                    Console.WriteLine("[9] Exit");
                    Console.Write("Select : ");
                    var ans = Console.ReadLine();
                    Console.WriteLine();

                    if (ans == "9")
                    {
                        break;
                    }

                    switch (ans)
                    {
                    case "0":
                        if (_inspectionCancellation != null)
                        {
                            _inspectionCancellation.Cancel();
                        }
                        await LoadModel();

                        break;

                    case "1":
                        if (_inspectionCancellation != null)
                        {
                            _inspectionCancellation.Cancel();
                        }
                        await LoadScene();

                        break;

                    case "2":
                        if (_inspectionCancellation != null)
                        {
                            _inspectionCancellation.Cancel();
                        }
                        _model.Serialize();
                        Console.Write("Input desired model name without .txt extension : ");
                        var _modelName = Console.ReadLine();
                        SaveModel($"./Models/{_modelName}.txt", _model);
                        break;

                    case "3":
                        if (_inspectionCancellation != null)
                        {
                            _inspectionCancellation.Cancel();
                        }
                        _scene.Serialize();
                        Console.WriteLine("Input desired scene name without .txt extension : ");
                        var _sceneName = Console.ReadLine();
                        SaveScene($"./Models/{_sceneName}.txt", _scene);
                        break;

                    case "4":
                        if (_inspectionCancellation != null)
                        {
                            _inspectionCancellation.Cancel();
                        }
                        _preTransformedModel = _model.Clone();
                        BeginTransformation(false);
                        await _renderer.RenderAwaitableAsync(_model);

                        break;

                    case "5":
                        if (_inspectionCancellation != null)
                        {
                            _inspectionCancellation.Cancel();
                        }
                        _preTransformedModel = _model.Clone();
                        BeginTransformation(true);
                        await _renderer.RenderAwaitableAsync(_model);

                        break;

                    case "6":
                        if (_inspectionCancellation != null)
                        {
                            _inspectionCancellation.Cancel();
                        }
                        _inspectionCancellation = new CancellationTokenSource();
                        InspectModel(_inspectionCancellation.Token);
                        break;

                    case "7":
                        _inspectionCancellation.Cancel();
                        _inspectionCancellation = null;
                        break;

                    case "8":
                        _model = _originalModel.Clone();
                        await _renderer.RenderAwaitableAsync(_model);

                        if (_inspectionCancellation != null)
                        {
                            _inspectionCancellation.Cancel();
                        }
                        break;

                    default:
                        if (_inspectionCancellation != null)
                        {
                            _inspectionCancellation.Cancel();
                        }
                        WriteError("Action not recognized.");
                        break;
                    }

                    Console.WriteLine();
                }

                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();

                _renderer.Close();
            }
            catch (Exception e)
            {
                WriteError(e.Message);
            }
        }