using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Drawing;

namespace DirectXEksempel07A
{
    class Airplane : ModelComponent
    {
        private Mesh mesh;
        private Material[] meshMaterials; // Materials for the mesh
        private Texture[] meshTextures; // Textures for the mesh
        Device theDevice;

        public Airplane(Device aDevice, Vector3 aPosition)
            : base(aPosition)
        {
            theDevice = aDevice;
            CreateMesh();
            Yaw = (float)(2F * Math.PI / 360F) * 90F;
            Pitch = (float)(2F * Math.PI / 360F) * 10F;
        }

        public void CreateMesh()
        {
            ExtendedMaterial[] materials = null;
            String path = @"..\..\";
            mesh = Mesh.FromFile(path + "triplane.x",
                MeshFlags.SystemMemory, theDevice, out materials);
            if (meshTextures == null)
            {
                // Egenskaber for materialer og texture udtrękkes 
                meshTextures = new Texture[materials.Length];
                meshMaterials = new Material[materials.Length];
                for (int i = 0; i < materials.Length; i++)
                {
                    meshMaterials[i] = materials[i].Material3D;
                    // Set the ambient color for the material (D3DX does not do this)
                    meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
                    if (materials[i].TextureFilename != null)
                    {
                        // Create the texture
                        meshTextures[i] = TextureLoader.FromFile(theDevice, path + materials[i].TextureFilename);
                    }
                }
            }
        }

        public void Render()
        {
            Color color = Color.LightSteelBlue;
            Material meshMaterial = new Material();
            meshMaterial.AmbientColor = new ColorValue(color.R, color.G, color.B);
            meshMaterial.DiffuseColor = new ColorValue(color.R, color.G, color.B);
            theDevice.RenderState.Lighting = true;
            theDevice.Transform.World = Matrix.RotationYawPitchRoll(Yaw, Pitch, Roll) *
                Matrix.Translation(Position);
            theDevice.Material = meshMaterial;
            theDevice.SetTexture(0, null);
            mesh.DrawSubset(0);
        }
    }
}
