예제 #1
0
        private void DrawButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                textureEnabled(false);
                if (model != null)
                {
                    WriteableBitmap source = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
                    Bgr24Bitmap     bitmap = new Bgr24Bitmap(source);

                    ModelParams modelParams = GetModelsParams();
                    Model       modelMain   = model.Clone() as Model;

                    CoordTransformations.TransformFromWorldToView(modelMain, modelParams);

                    if (modelMain.CheckSize(width, height))
                    {
                        Color   color    = Color.FromRgb(byte.Parse(colorRTextBox.Text), byte.Parse(colorGTextBox.Text), byte.Parse(colorBTextBox.Text));
                        Vector3 lighting = new Vector3(int.Parse(lightVectorXTextBox.Text), int.Parse(lightVectorYTextBox.Text), -int.Parse(lightVectorZTextBox.Text));

                        if (bresenhamRadioButton.IsChecked == true)
                        {
                            // lab 1-2
                            BresenhamAlg bresenham = new BresenhamAlg(bitmap, modelMain);
                            bresenham.DrawModel(color);
                        }
                        else if (plainShadingRadioButton.IsChecked == true)
                        {
                            // lab 3
                            PlaneShading shader = new PlaneShading(bitmap, modelMain, new LambertLighting(lighting));
                            shader.DrawModel(color);
                        }

                        else if (phongShadingRadioButton.IsChecked == true)
                        {
                            textureEnabled(true);
                            // затенение фонга
                            Vector3 viewVector = new Vector3(int.Parse(colorRTextBox_View.Text), int.Parse(colorGTextBox_View.Text), int.Parse(colorBTextBox_View.Text));
                            Vector3 koef_a = new Vector3(float.Parse(colorRTextBox_A.Text), float.Parse(colorGTextBox_A.Text), float.Parse(colorBTextBox_A.Text));
                            Vector3 koef_d = new Vector3(float.Parse(colorRTextBox_D.Text), float.Parse(colorGTextBox_D.Text), float.Parse(colorBTextBox_D.Text));
                            Vector3 koef_s = new Vector3(float.Parse(colorRTextBox_S.Text), float.Parse(colorGTextBox_S.Text), float.Parse(colorBTextBox_S.Text));
                            Vector3 ambientColor = new Vector3(int.Parse(colorRTextBox_Ambient.Text), int.Parse(colorGTextBox_Ambient.Text), int.Parse(colorBTextBox_Ambient.Text));
                            Vector3 reflectionColor = new Vector3(int.Parse(colorRTextBox_Reflection.Text), int.Parse(colorGTextBox_Reflecion.Text), int.Parse(colorBTextBox_Reflection.Text));
                            float   shiness = float.Parse(shinessBox.Text);
                            bool    d = false, n = false, s = false;
                            if ((diffuseCheckBox != null && (bool)diffuseCheckBox.IsChecked))
                            {
                                d = true;
                            }
                            if ((normalCheckBox != null && (bool)normalCheckBox.IsChecked))
                            {
                                n = true;
                            }
                            if ((mirrorCheckBox != null && (bool)mirrorCheckBox.IsChecked))
                            {
                                s = true;
                            }

                            var light = new PhongLighting(lighting, viewVector, koef_a, koef_d, koef_s, ambientColor, reflectionColor, shiness, d, n, s);
                            //var light = new LambertLighting(lighting);
                            PhongShading shader = new PhongShading(bitmap, modelMain, light, d, n, s);
                            shader.DrawModel(color);
                        }


                        screenPictureBox.Source = bitmap.Source;
                    }
                }
                else
                {
                    MessageBox.Show("Load an object");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Произошла ошибка! " + ex);
            }
        }