//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;

namespace DXGfxLib
{
    public class FrameworkMeshObject : MeshObjectWithLOD
    {
        /// <summary>
        /// Most of the time we are going to attach full framework Mesh to our scene object so we keep a possible ref to that 
        /// in the base SceneObject class. This might be moved to a derive class in the future but it does suit my current needs.
        /// </summary>
        protected FrameworkMesh frameworkMesh = null;

        /// <summary>
        /// This method is used to associate a FrameworkMesh instance to the SceneObject.
        /// </summary>
        /// <param name="meshToAttach"></param>
        public void Attach(FrameworkMesh meshToAttach)
        {
            frameworkMesh = meshToAttach;
            base.Attach(meshToAttach.LocalMesh);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="d3ddevice"></param>
        /// <param name="meshToAttach"></param>
        /// <param name="adjacencyBuffer"></param>
        /// <param name="materials"></param>
        /// <param name="effects"></param>
        public virtual void AttachWithLOD(Device d3ddevice, FrameworkMesh meshToAttach)
        {
            int[] generatedAdjacency = new int[3*meshToAttach.LocalMesh.NumberFaces];
            meshToAttach.LocalMesh.GenerateAdjacency(0.01f, generatedAdjacency);

            ExtendedMaterial[] generatedMaterials = new ExtendedMaterial[meshToAttach.NumberMaterials];
            for (int i = 0; i < meshToAttach.NumberMaterials; i++)
            {
                generatedMaterials[i] = new ExtendedMaterial();
                Material mat = meshToAttach.GetMaterial(i);

                generatedMaterials[i].Material3D = mat;
            }
            base.AttachWithLOD(d3ddevice, meshToAttach.LocalMesh, generatedAdjacency, generatedMaterials, null);
        }

        /// <summary>
        /// In its draw method the scene will call the Draw method on all scene object (or derived class instances) that
        /// have registered to be rendered in the AttachForRendering method.
        /// </summary>
        /// <param name="device"></param>
        public override void Draw(Device device)
        {
            device.SetTransform(TransformType.World, worldMat);
            frameworkMesh.Render(device);
        }
    }
}
