
#if defined(CONST_COLOR) && !defined(VERTEX_COLOR)
// when "vertex color" not present there is no need to interpolate "const color", send it to pixel shader instead
#undef CONST_COLOR
#endif

// ------- ATTRIBUTES (VERTEX BUFFER DATA) -------
attribute vec4		a_vPosition0;

#if defined(TEXTURE0)
attribute vec4		a_vTexCoord0;
#endif

#ifdef VERTEX_COLOR
attribute vec4		a_vColor0;
#endif

// ------- UNIFORMS (SHADER CONSTS) -------
uniform mat4		hu_mPMVMatrix;

#if defined(GEONORMALMAP) || defined(GEONORMALMAP09)
uniform vec4		chu_vGeoNormalmapInfo;
#endif

#ifdef GEONORMALMAP
uniform mat4		chu_mLocalToGeoMatrix;
#endif

#ifdef GEONORMALMAP09
uniform mat4		hu_mMMatrix;
#endif

#ifdef TEXTURE0
#ifdef TEXTURE_MATRIX
uniform mat4		chu_mTextureMatrix;
#endif
#endif

#ifdef CONST_COLOR
uniform	vec4		chu_vColor;
#endif

//#ifdef PRIMITIVE_POINTS
//uniform	float		chu_fPointSize;
//#endif

// ------- INTERPOLATORS (SHADER OUTPUTS) -------
#if defined(GEONORMALMAP) || defined(GEONORMALMAP09)
varying vec2		v_vHeightmapUV;
#endif

#if defined(TEXTURE0)
varying vec2		v_vTexCoord;
#endif

#ifdef VERTEX_COLOR
varying vec4		v_vColor;
#endif

// ------- FUNCTIONS ------- 
#if defined(GEONORMALMAP09)
const float MATH_PI = 3.141592;
const float RAD2DEG = 57.295779;

vec2 P3dToGeo(vec3 p)
{
	vec2 v;
	
	v.x = MATH_PI*0.5 - atan(p.z, p.x);
	if (v.x > MATH_PI*1.0) v.x -= MATH_PI*2.0;
	
	v.y = MATH_PI*0.5 - atan(length(vec2(p.x, p.z)), p.y);

	v.x *= RAD2DEG*100000.0;
	v.y *= RAD2DEG*100000.0;

	return v;
}
#endif

void main(void)
{
	gl_Position = hu_mPMVMatrix*a_vPosition0;

#ifdef GEONORMALMAP
	vec3 vGeoVertex = vec3(chu_mLocalToGeoMatrix*a_vPosition0);
	v_vHeightmapUV = vec2(vGeoVertex.x - chu_vGeoNormalmapInfo.x, vGeoVertex.z + chu_vGeoNormalmapInfo.y)*chu_vGeoNormalmapInfo.z;
#endif

#ifdef GEONORMALMAP09
	vec2 vGeoVertex = P3dToGeo(vec3(hu_mMMatrix*a_vPosition0));
	v_vHeightmapUV = vec2(vGeoVertex.x - chu_vGeoNormalmapInfo.x, -vGeoVertex.y + chu_vGeoNormalmapInfo.y)*chu_vGeoNormalmapInfo.z;
#endif

#if defined(TEXTURE0)
#ifdef TEXTURE_MATRIX
	vec4 vTransUV = chu_mTextureMatrix*a_vTexCoord0;
	v_vTexCoord = vTransUV.st;
#else
	v_vTexCoord = a_vTexCoord0.st;
#endif
#endif

#ifdef VERTEX_COLOR
	vec4 vColor = a_vColor0;
#endif

#ifdef CONST_COLOR
	vColor *= chu_vColor;
#endif

#ifdef VERTEX_COLOR
	v_vColor = vColor;
#endif

//#ifdef PRIMITIVE_POINTS
//	gl_PointSize = chu_fPointSize;
//#endif
}
