using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Drawing3d.Math;
using Drawing3d.Devices;
using Drawing3d;
namespace WindowsFormsApplication1
{
    
    public partial class Form1 : Form
    {
        public OpenGlDevice Device = new OpenGlDevice();  
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Device.WinControl = this;
            Device.OnPaint += new EventHandler(Device_OnPaint);
            Device.Navigate = NavigateKind.ZoomRotateTrans;
            Device.BackColor = Color.DarkGray;
            Device.Lights[0].Position = new xyz(-10, -5, 3);
           
           // Setzen des Blickwinkels : Perspektive
            Device.FieldOfView = 30;
            // Looks to the Point 0, 0, 2 form -20, -10, 2
            Device.Camera.LookAt(new xyz(-20, -10, 2), new xyz(0, 0, 2), new xyz(0, 0, 1)); 
            Device.Camera.SetRelativSystem();
            // Sets the focus. 
            Device.Camera.Focus = new xyz(-20, -10, 2).dist(new xyz(0, 0, 2));
            // now 0, 0, 2 is the Center of rotations
            
            // for bigger reflections 
            Device.Shininess = 1;
            KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left:
                    Device.Camera.Yaw = Device.Camera.Yaw - 0.1;
                    Device.Refresh();
                    break;

                case Keys.Right:
                    Device.Camera.Yaw = Device.Camera.Yaw + 0.1;
                    Device.Refresh();
                    break;

                case Keys.Up:
                    Device.Camera.Pitch = Device.Camera.Pitch + 0.1;
                    Device.Refresh();
                    break;

                case Keys.Down:
                    Device.Camera.Pitch = Device.Camera.Pitch - 0.1;
                    Device.Refresh();
                    break;

                case Keys.R: // Roll
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.Roll = Device.Camera.Roll - 0.1;
                    else
                        Device.Camera.Roll = Device.Camera.Roll + 0.1;
                    Device.Refresh();
                    break;

                case Keys.W:// Walk in Z-Axis 0.5
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.Walk(-0.5);
                    else
                        Device.Camera.Walk(0.5);
                    Device.Refresh();
                    break;

                case Keys.X: // X-Panning
                    {
                        Point Panning = Device.Camera.Panning;
                        if (Form.ModifierKeys == Keys.Shift)
                            Device.Camera.Panning = new Point(Panning.X - 5, Panning.Y);
                        else
                            Device.Camera.Panning = new Point(Panning.X + 5, Panning.Y);
                        Device.Refresh();
                    }
                    break;

                case Keys.Y: // Y-Panning
                    {
                        Point Panning = Device.Camera.Panning;
                        if (Form.ModifierKeys == Keys.Shift)
                            Device.Camera.Panning = new Point(Panning.X, Panning.Y - 5);
                        else
                            Device.Camera.Panning = new Point(Panning.X, Panning.Y + 5);
                        Device.Refresh();
                    }
                    break;

                case Keys.N:// Walk in XY- Plane of camera in x-direction
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.Walk(new xy(-0.2, 0));
                    else
                        Device.Camera.Walk(new xy(0.2, 0));
                    Device.Refresh();

                    break;

                case Keys.M:// Walk in XY- Plane of camera in y-direction
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.Walk(new xy(0, -0.2));
                    else
                        Device.Camera.Walk(new xy(0, 0.2));
                    Device.Refresh();

                    break;

                case Keys.Z: // Zooming
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.ZoomFactor -= 0.1 * Device.Camera.ZoomFactor;
                    else
                        Device.Camera.ZoomFactor += 0.1 * Device.Camera.ZoomFactor;
                    Device.Refresh();
                    break;
            }
        }
        void Device_OnPaint(object sender, EventArgs e)
        {// Draw a lightgray grid
            Device.PenColor = Color.LightGray;
            int Count = 100;
            double Distance = 2;
            for (int j = 0; j < Count; j++)
                Device.drawLine(new xyz(-Count / 2 * Distance, j * Distance - Count / 2 * Distance, 0), new xyz(Count / 2 * Distance, j * Distance - Count / 2 * Distance, 0));
            for (int j = 0; j < Count; j++)
                Device.drawLine(new xyz(j * Distance - Count / 2 * Distance, -Count / 2 * Distance, 0), new xyz(j * Distance - Count / 2 * Distance, Count / 2 * Distance, 0));
            // save the ambient material
            Color SaveAmbient = Device.Ambient;

            // and draw four Boxes

            Device.Ambient = Color.Red;
            Tools.drawBoxAt(Device, new xyz(-4, 1, 0), new xyz(3, 4, 2));

            Device.Ambient = Color.Green;
            Tools.drawBoxAt(Device, new xyz(-2, 8, 0), new xyz(4, 2, 5));

            Device.Ambient = Color.Yellow;
            Tools.drawBoxAt(Device, new xyz(7, 5, 0), new xyz(3, 4, 4));

            Device.Translucent = 0.6;
            Device.Ambient = Color.Blue;
            Tools.drawBoxAt(Device, new xyz(1, 1, 0), new xyz(4, 3, 6));
            Device.Translucent = 1;

            Device.Ambient = SaveAmbient;

        }
    }
}