//==========================================================================//
// File:	 gosFX_Beam.hpp										//
// Project:  gosFX					                                        //
// Contents: Base Beam Profile									//
//--------------------------------------------------------------------------//
// Date      Who    Modification                                            //
// 09/28/98  JTR    Created													//
//																			//
//--------------------------------------------------------------------------//
// Copyright (C) 1997-1998, Virtual World Entertainment, Inc.               //
// All Rights reserved worldwide                                            //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL              //
//==========================================================================//
//
#pragma once

#include "gosFX.hpp"
#include "Effect.hpp"

namespace MidLevelRenderer {class MLRIndexedTriangleCloud;}

namespace gosFX
{
	//############################################################################
	//########################  Beam__Specification  #############################
	//############################################################################

	class Beam__Specification:
		public Effect__Specification
	{
	//----------------------------------------------------------------------
	// Constructors/Destructors
	//
	protected:
		Beam__Specification(
			Stuff::MemoryStream *stream,
			int gfx_version
		);

	public:
		typedef Effect__Specification BaseClass;

		Beam__Specification();
		~Beam__Specification();

		static Beam__Specification*
			Make(
				Stuff::MemoryStream *stream,
				int gfx_version
			);

		void
			Copy(Beam__Specification *spec);

		void
			Save(Stuff::MemoryStream *stream);
		
		void 
			BuildDefaults();

		bool 
			IsDataValid(bool fix_data=false);

	//-------------------------------------------------------------------------
	// FCurves
	//
	public:
		ConstantCurve
			m_profilesPerMeter,
			m_minimumDeviation,
			m_maximumDeviation;
		SeededCurveOf<ComplexCurve, SplineCurve, Curve::e_ComplexSplineType>
			m_pDisplacement;
		SeededCurveOf<ComplexCurve, ComplexCurve, Curve::e_ComplexComplexType>
			m_pScale,
			m_pRed,
			m_pGreen,
			m_pBlue,
			m_pAlpha;
		ComplexCurve
			m_pUOffset,
			m_pVOffset,
			m_pUSize,
			m_pVSize;

	//----------------------------------------------------------------------
	// Data
	//
	public:
		int
			m_maxProfileCount;

		enum ProfileType {
			e_Ribbon,
			e_AlignedRibbon,
			e_Triangle,
			e_Square,
			e_Cross,
			e_Pentagon,
			e_Hexagon,
			e_VerticalRibbon
		}
			m_profileType;
		bool
			m_insideOut,
			m_randomDirections;

		Stuff::DynamicArrayOf<Stuff::Point3D>
			m_vertices;
		Stuff::DynamicArrayOf<Stuff::Vector2DOf<Stuff::Scalar> >
			m_uvs;

		void
			BuildTemplate();
	};

	//############################################################################
	//########################  Beam__Profile  #############################
	//############################################################################

	class Beam__Profile
	{
	public:
		Stuff::LinearMatrix4D
			m_profileToWorld;
		Stuff::UnitVector3D
			m_direction;
		Stuff::Scalar
			m_seed;

		void
			TestInstance() const
				{}
	};

	//############################################################################
	//##############################  Beam  #############################
	//############################################################################

	class Beam:
		public Effect
	{
	public:
		static void
			InitializeClass();
		static void
			TerminateClass();

		typedef Effect BaseClass;

		static ClassData
			*DefaultData;

		typedef Beam__Specification Specification;
		typedef Beam__Profile Profile;

		struct ExecuteInfo:
			public BaseClass::ExecuteInfo
		{
			const Stuff::Point3D
				*m_endPoint;

			ExecuteInfo(
				Stuff::Time time,
				const Stuff::LinearMatrix4D *parent_to_world,
				const Stuff::Point3D *end_point,
				Stuff::OBB *bounds,
				Stuff::Scalar seed = -1.0f
			):
				BaseClass::ExecuteInfo(time, parent_to_world, bounds, seed)
					{m_endPoint = end_point;}

		private:
			ExecuteInfo(
				Stuff::Scalar time,
				const Stuff::LinearMatrix4D *parent_to_world,
				const Stuff::Point3D *end_point,
				Stuff::OBB *bounds,
				Stuff::Scalar seed = -1.0f
			);
		};

	protected:
		int
			m_activeProfileCount,
			m_triangleCount,
			m_vertexCount;

		Stuff::DynamicArrayOf<Profile>
			m_profiles;
		Stuff::DynamicArrayOf<char>
			m_data;
		MidLevelRenderer::MLRIndexedTriangleCloud
			*m_mesh;
		Stuff::Point3D
			*m_P_vertices;
		Stuff::RGBAColor
			*m_P_colors;
		Stuff::Vector2DOf<Stuff::Scalar>
			*m_P_uvs;

		void
			BuildMesh(unsigned short *indices);

		Beam(
			Specification *spec,
			unsigned flags
		);

	//----------------------------------------------------------------------------
	// Class Data Support
	//
	public:
		~Beam();

		static Beam*
			Make(
				Specification *spec,
				unsigned flags
			);

		Specification*
			GetSpecification()
				{
					Check_Object(this);
					return
						Cast_Object(Specification*, m_specification);
				}
		Profile*
			GetProfile(unsigned index)
				{
					Check_Object(this); Check_Object(GetSpecification());
					return &m_profiles[index];
				}

	//----------------------------------------------------------------------------
	// Testing
	//
	public:
		void
			TestInstance() const;

	//----------------------------------------------------------------------------
	// API
	//
	protected:
		void
			AnimateProfile(
				unsigned profile,
				const Stuff::LinearMatrix4D &world_to_new_local,
				Stuff::Time till,
				Stuff::Sphere *sphere
			);
		void
			CreateNewProfile(unsigned index);
		void
			ComputeDirection(unsigned index);

	public:
		void
			Start(Effect::ExecuteInfo *info);
		bool
			Execute(ExecuteInfo *info);
		void
			Kill();
		void
			Draw(DrawInfo *info);
	};
}
