/*********************************************************************//**
*	\brief Jednotlive elementy sceny.
*       Zde je implementovan balik nekolika struktur, ktere se pouzivaji
*	misto definovanych bsp struktur. Tyto objekty obsahuji vsechny informace,
*	ktere jsou potreba k funkcnosti aplikace.
*                                                                        
*
*	\author Michal Jirous
*	\date 4.2.2009
*	\file level_structs.h
**********************************************************************/  

#ifndef __LEVEL_STRUCTS_H__
#define __LEVEL_STRUCTS_H__

#include "algebraic.h"
#include "textureslib.h"
#include "level_bspfile_def.h"

typedef byte sessionkey_t;	/*!< @brief Typ pro definovani aktualniho cisla cyklu. */


/** @brief Hrana dana dvema body. */
struct Edge
{
	Point	*A,	/*!< @brief Pocatecni bod. */
			*B;	/*!< @brief Koncovy bod. */
	sessionkey_t session_key;	/*!< @brief Cislo posledniho pouziti. */
};


/** @brief Uzel BSP stromu. */
struct BSPNode
{
	int			planenum;		/*!< @brief Cislo roviny v seznamu rovin. */
	short		children[2];	/*!< @brief Potomci tohoto uzlu (zaporne cislo znamena list = -(leafs+1)). */
	BoundingBox bounds;			/*!< @brief BoundingBox tohoto uzlu. */
	unsigned short	firstface;	/*!< @brief ??? .*/
	unsigned short	numfaces;	/*!< @brief ??? counting both sides. */
	void create( const dnode_t &node );	/*!< @brief Funkce nastavi atributy toho objektu dle parametru node. */
};



/** @brief List BSP stromu a take cluster pro visibility check. */
struct BSPLeaf
{
	int contents;	/*!< @brief Not identified yet. */
	unsigned short *visible_leafs;	/*!< @brief Seznam indexu viditelnych clusteru. */
	unsigned short visible_leafs_count;	/*!< @brief Pocet viditelnych clusteru. */
	BoundingBox bounds;	/*!< @brief BoundingBox vsech facu listu. */
	unsigned short first_mark_surface;	/*!< @brief Prvni pozice v poli mark surfaces. */
	unsigned short mark_surfaces_count;	/*!< @brief Pocet pozic v poli mark surfaces = pocet facu k vykresleni. */
	byte ambient_level[NUM_AMBIENTS];	/*!< @brief Not identified yet. */
	
	void create( const dleaf_t &bspleaf );	/*!< @brief Funkce nastavi atributy tohoto listu dle listu v parametru. */

	void destroy();	/*!< @brief Funkci uvolni alokovanou pamet. */
};

/** @brief Texturovaci souradnice. */
struct TexCoord2f_t
{
	float	u,	/*!< @brief Souradnice U (S). */
			v;	/*!< @brief Souradnice V (T). */
};

#define FACE_NO_LIGHTMAP 1
#define FACE_SIMPLE_COLLISION 2
#define FACE_USES_STYLES	4
#define FACE_FOG_COORDS	8

/** @brief Plocha, polygon, k vykresleni, detekci kolizi. */
struct Face
{
	short planenum;	/*!< @brief Cislo roviny. */
	short side;		/*!< @brief 1 Rika, ze rovina ma opacny smer. */

	int first_surfedge;		/*!< @brief Prvni pozice v surf edges. */
	unsigned short elements_count;	/*!< @brief Pocet elementu (bodu, hran a texturovacich souradnic). */

	TexturePooled *pTexture;	/*!< @brief Objekt hlavni textury. */
	
	size_t firstvertex;		/*!< @brief Prvni pozice ve vertex array, tex. coord array a lightmap coords array. */
	
	sessionkey_t session_key;	/*!< @brief Posledni cislo pouziti. */
	int flags;	/*!< @brief Vlastnosti textury kdyz se nerovna 0, tak se nevytvari lightmapa. */

	GLuint lightmaps[MAXLIGHTMAPS];	/*!< @brief Identifikacni cisla textur lightmap. */
	byte styles[MAXLIGHTMAPS];	/*!< @brief Styly lightmap. */
	GLfloat average_face_color[3];	/*!< @brief Prumerna hodnota barvy prvni lightmapy. */

	void create( const dface_t &face );	/*!< @brief Funkce zkopiruje nektere informace z dface_t v parametru. */
	void destroy();	/*!< @brief Uvolni alokovanou pamet. */
	
	BoundingBox m_Bounds;	/*!< @brief AABB tohoto facu. */

	int intersectByLine( const Vector &direction, const Point &position, Point &result ) const;	/*!< @brief Prunik poloprimky s timtopolygonem. */
	int intersectByLine( const Line &line, Point &result ) const;	/*!< @brief Prunik poloprimky s timtopolygonem. */
	int isPointIn( const Point &point ) const;	/*!< @brief Funkce zjisti, zda zadany bod lezi v prostoru tohoto polygonu. */
	bool collideWith( CollisionData &collData ); /*!< @brief Funkce zjisti, zda tento polygon koliduje se zadanym dynamickym modelem. */	
	void getLightColor( GLfloat *values3 );	/*!< @brief Funkce ulozi do parametru soucet vsech specifickych barev aktivnich lightmap. */
	int getMaterial();
private:
	bool collisionPhase1( CollisionData &collData, const Plane &plane ) const;
	bool collisionPhase2( CollisionData &collData ) const;
	bool collisionPhase3( CollisionData &collData ) const;
};





#endif /*__LEVEL_STRUCTS_H__*/

