

#ifndef __TskmdViewer_h__
#define __TskmdViewer_h__


#ifndef __XGLVIEW_H__
#include <XOpenGLView.h>
#endif

#ifndef __myth_defs_h__
#include "myth_defs.h"
#endif

#ifndef __vec3_h__
#include "vec3.h"
#endif

class vec3;
struct skmd_complex_collision_tri;
struct skmd_complex_collision_box;


struct skmdViewerCylinder
{
	float rgba[4];
	vec3 base, axis;
	float r, h0, h1;
	int segments;
};


class TskmdViewer: public XGOpenGLView {
	public:
							TskmdViewer( XGView *parent, XGArgStream &s );
		virtual				~TskmdViewer();
		
		enum {
			kClassID = 'skmd'
		};
		
		enum {
			shadeWire,
			shadeFlat,
			shadeSmooth
		};
		
		virtual void		DoDrawView( XRect );
		virtual bool		DoMouseDown( XPoint pt, short a );
		virtual void		DoMouseMove( XPoint pt, short a );
		
		void				SetData( const float pos[][4], const float norm[][4], const float uv[][2], int vcount, const unsigned short tri[][3], int tcount );
		void				SetShading( int shadeMode );
		void				ShowNormals( bool show );
		void				SetTwoSided( bool twosided );
		
		void				SetCollision( skmd_complex_collision_box *boxes, int count, world_distance radius, world_distance height0, world_distance height1, float scale );
		void				SetHilight( world_distance radius, world_distance height0, world_distance height1, float scale );
		void				SetClick( world_distance radius, world_distance height0, world_distance height1, float scale );
		
		void				SetShadowCylinders( skmdViewerCylinder *cyls, int count );
		
		float				GetHeight();
		
	private:
		int shadeMode;
		bool normalsShown;
		
		vec3 *pos;
		vec3 *norm;
		float (*uv)[2];
		unsigned short (*tri)[3];
		int vcount;
		int tcount;
		
		float modelRadius;
		float modelLowerBound;
		float modelUpperBound;
		
		float hilightRadius;
		float hilightUpperBound;
		float hilightLowerBound;
		
		float clickRadius;
		float clickUpperBound;
		float clickLowerBound;
		
		float collisionRadius;
		float collisionUpperBound;
		float collisionLowerBound;
		
		float collisionArrayRadius;
		float collisionArrayUpperBound;
		float collisionArrayLowerBound;
		
		float shadowArrayRadius;
		float shadowArrayUpperBound;
		float shadowArrayLowerBound;
		
		
		bool twosided;
		
		XPoint spinAnchor;
		float xRot, yRot;
		
		vec3 *collision_pos;
		vec3 *collision_norm;
		int *collision_indices;
		int collision_vertex_count;
		int collision_index_count;
		
		skmdViewerCylinder *shadow_cylinders;
		int shadow_cylinder_count;
		
		
		void free_model();
		void draw_cylinder( vec3 base, vec3 axis, float r, float h0, float h1, int segments );
};



#endif
