using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirectXEksempel02
{
    public partial class Form1 : Form
    {
        // Global variables for this project
        private Device device = null; // Rendering device
        CustomVertex.PositionColored[] vertices1 = new CustomVertex.PositionColored[3];
        CustomVertex.PositionColored[] vertices2 = new CustomVertex.PositionColored[3];
        CustomVertex.PositionColored[] vb = new CustomVertex.PositionColored[3];
        private float angle1 = 0F;
        private float angle2 = 0F;

        public Form1() // Constructor
        {
            // Set the initial size of the form
            this.ClientSize = new System.Drawing.Size(800, 600);
            // And its caption
            this.Text = "DirectX Eksempel 2";
        }

        public void CreateVertices()
        {
            int i = 0;
            vertices1[i++] = new CustomVertex.PositionColored(100f, 100f, 10f, System.Drawing.Color.Red.ToArgb());
            vertices1[i++] = new CustomVertex.PositionColored(400f, 600f, 400f, System.Drawing.Color.Green.ToArgb());
            vertices1[i++] = new CustomVertex.PositionColored(700f, 100f, 10f, System.Drawing.Color.Yellow.ToArgb());
            i = 0;
            vertices2[i++] = new CustomVertex.PositionColored(0f, -200f, 0f, System.Drawing.Color.Cyan.ToArgb());
            vertices2[i++] = new CustomVertex.PositionColored(100f, 500f, 100f, System.Drawing.Color.Magenta.ToArgb());
            vertices2[i++] = new CustomVertex.PositionColored(200f, -300f, 100f, System.Drawing.Color.Yellow.ToArgb());
            i = 0;
            vb[i++] = new CustomVertex.PositionColored(395f, 0f, 0f, System.Drawing.Color.White.ToArgb());
            vb[i++] = new CustomVertex.PositionColored(400f, 5f, 0f, System.Drawing.Color.White.ToArgb());
            vb[i++] = new CustomVertex.PositionColored(405f, 0f, 0f, System.Drawing.Color.White.ToArgb());
        }

        public void Render()
        {
            if (device == null)
                return;

            //Clear the backbuffer to a blue color 
            device.Clear(ClearFlags.Target, System.Drawing.Color.MidnightBlue, 1.0f, 0);
            //Begin the scene
            device.BeginScene();
            device.VertexFormat = CustomVertex.PositionColored.Format;
            // Trekant 1: Rotation om en Y-akse, der går gennem X=400
            device.Transform.World = Matrix.Translation(-400, 0, 0) *
                Matrix.RotationY(angle1) * Matrix.Translation(400, 0, 0);
            device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices1);
            // Trekant 2: Rotation om en Z-aksen
            device.Transform.World = Matrix.RotationZ(angle2);
            device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices2);
            // Prikken
            device.Transform.World = Matrix.Identity;
            device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vb);
            //End the scene
            device.EndScene();
            device.Present();
            angle1 += 0.01f;
            angle2 -= 0.02f;
        }

        public void InitializeGraphics()
        {
            // Definering af device til DirectX Rendering
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            presentParams.PresentationInterval = PresentInterval.Default;
            device = new Device(0, DeviceType.Hardware, this,
                CreateFlags.SoftwareVertexProcessing, presentParams);
            //Placering af kameraet
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 
                this.Width / this.Height, 0f, 50f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(400, 100, -1500), 
                new Vector3(400, 100, 0), new Vector3(0, 1, 0));
            device.RenderState.CullMode = Cull.None;
            device.RenderState.Lighting = false;
            // Opbygning af vertex tabel
            CreateVertices();
        }

        static void Main() // Entry point
        {
            using (Form1 engine = new Form1())
            {
                engine.InitializeGraphics();
                engine.Show();

                // While the form is still valid, render and process messages
                while (engine.Created)
                {
                    engine.Render();
                    Application.DoEvents();
                }
            }
        }
    }
}