//BSD License  
//DXGfx® - http://www.eteractions.com
//Copyright (c) 2005
//by Guillaume Randon
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software
//and associated documentation files (the "Software"), to deal in the Software without restriction,
//including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
//and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
//subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial
//portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
//AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
//DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.Samples.DirectX.UtilityToolkit;
using System.Drawing;

namespace DXGfxLib
{
    /// <summary>
    /// This isn't really a nice impostor. I should add alpha testing for this class to become really of interest. This is yet to
    /// be done.
    /// </summary>
    public class Impostor : Batch
    {
        private Texture texture = null;

        /// <summary>
        /// Will build a 8 faces impostor from a material and a texture.
        /// </summary>
        /// <param name="material"></param>
        /// <param name="texture"></param>
        public void Attach(Material material, Texture texture)
        {
            this.texture = texture;

            CustomVertex.PositionNormalTextured[] vertices = new CustomVertex.PositionNormalTextured[4];

            vertices[0].X = -0.5f;
            vertices[0].Y = 0;
            vertices[0].Z = 0;

            vertices[0].Nx = 0;
            vertices[0].Ny = 1.0f;
            vertices[0].Nz = 0;

            vertices[0].Tu = 0;
            vertices[0].Tv = 0;

            vertices[1].X = 0.5f;
            vertices[1].Y = 0;
            vertices[1].Z = 0;

            vertices[1].Nx = 0;
            vertices[1].Ny = 1.0f;
            vertices[1].Nz = 0;

            vertices[1].Tu = 1;
            vertices[1].Tv = 0;

            vertices[2].X = -0.5f;
            vertices[2].Y = 1;
            vertices[2].Z = 0;

            vertices[2].Nx = 0;
            vertices[2].Ny = 1.0f;
            vertices[2].Nz = 0;

            vertices[2].Tu = 0;
            vertices[2].Tv = 1;

            vertices[3].X = 0.5f;
            vertices[3].Y = 1;
            vertices[3].Z = 0;

            vertices[3].Nx = 0;
            vertices[3].Ny = 1.0f;
            vertices[3].Nz = 0;

            vertices[3].Tu = 1;
            vertices[3].Tv = 1;

            Int16[] indices = new Int16[] { 0, 1, 2, 2, 1, 3 };

            Mesh oneface = new Mesh(2, 4, MeshFlags.IbSystemMem, CustomVertex.PositionNormalTextured.Format, texture.Device);

            GraphicsStream vertexStm = oneface.LockVertexBuffer(LockFlags.None);
            vertexStm.Write(vertices);
            oneface.UnlockVertexBuffer();

            GraphicsStream indicesStm = oneface.LockIndexBuffer(LockFlags.None);
            indicesStm.Write(indices);
            oneface.UnlockIndexBuffer();

            Matrix[] matrices = new Matrix[4];

            matrices[0] = Matrix.Identity;
            matrices[1] = Matrix.RotationY((float)(0.25f * Math.PI));
            matrices[2] = Matrix.RotationY((float)(0.5f * Math.PI));
            matrices[3] = Matrix.RotationY((float)(0.75f * Math.PI));

            Material[] materials = new Material[1];
            Texture[] textures = new Texture[1];

            materials[0] = new Material();
            materials[0].AmbientColor = ColorValue.FromColor(Color.WhiteSmoke);
            materials[0].DiffuseColor = ColorValue.FromColor(Color.WhiteSmoke);
            materials[0].Specular = Color.White;

            textures[0] = texture;

            Populate(oneface, materials, textures, matrices);
        }

        /// <summary>
        /// Using the previous method, this will build a 8 faces impostor from a FrameworkMesh, this method uses a render to
        /// texture to perform that.
        /// </summary>
        /// <param name="fMesh"></param>
        public void Attach(FrameworkMesh fMesh)
        {
            Material mat = fMesh.GetMaterial(0);

            Device d3ddevice = fMesh.SystemMesh.Device;

            if (d3ddevice != null)
            {
                Texture tex = new Texture(d3ddevice, 120, 120, 1, Usage.None, Format.X8R8G8B8, Pool.Managed);

                Surface currentTarget = d3ddevice.GetRenderTarget(0);

                d3ddevice.SetRenderTarget(0, tex.GetSurfaceLevel(0));
                d3ddevice.Clear(ClearFlags.ZBuffer | ClearFlags.Target, Color.Blue, 1.0f, 0);

                d3ddevice.BeginScene();

                fMesh.Render(fMesh.SystemMesh.Device);

                d3ddevice.EndScene();

                d3ddevice.SetRenderTarget(0, currentTarget);

                Attach(mat, tex);
            }
        }
    }
}
