
#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

// ------- UNIFORMS (SHADER CONSTS) -------

cbuffer PMV
{
	mat4	hu_mPMVMatrix;
};

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

#if defined(GEONORMALMAP)
mat4		chu_mLocalToGeoMatrix;
#endif

#if defined(GEONORMALMAP09)
mat4		hu_mMMatrix;
#endif

#if defined(TEXTURE0) && defined(TEXTURE_MATRIX)
mat4		chu_mTextureMatrix;
#endif

#ifdef CONST_COLOR
vec4		chu_vColor;
#endif

// ------- ATTRIBUTES (VERTEX BUFFER DATA) -------

struct VIn
{
	vec4	position: POSITION;
#if defined(VERTEX_COLOR)
	vec4	color: COLOR;
#endif
#if defined(TEXTURE0)
	vec2	uv: TEXCOORD;
#endif
};

// ------- INTERPOLATORS (SHADER OUTPUTS) -------
struct PIn
{
    vec4	position: SV_POSITION;
#if defined(VERTEX_COLOR)
	vec4	color: COLOR;
#endif
#if defined(TEXTURE0)
	vec2	uv: TEXCOORD;
#endif
#if defined(GEONORMALMAP) || defined(GEONORMALMAP09)
	vec2	geouv: TEXCOORD1;
#endif
};

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

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

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

	return v;
}
#endif

PIn main(VIn input)
{
	PIn output;
    output.position = mul(hu_mPMVMatrix, input.position);

#if defined(GEONORMALMAP)
	vec4 vGeoVertex = mul(chu_mLocalToGeoMatrix, input.position);
	output.geouv = vec2(vGeoVertex.x - chu_vGeoNormalmapInfo.x, vGeoVertex.z + chu_vGeoNormalmapInfo.y)*chu_vGeoNormalmapInfo.z;
#endif

#if defined(GEONORMALMAP09)
	vec4 vSphere = mul(hu_mMMatrix, input.position);
	vec2 vGeoVertex = P3dToGeo(vSphere.xyz);
	output.geouv = vec2(vGeoVertex.x - chu_vGeoNormalmapInfo.x, -vGeoVertex.y + chu_vGeoNormalmapInfo.y)*chu_vGeoNormalmapInfo.z;
#endif

#if defined(TEXTURE0)
#if defined(TEXTURE_MATRIX)
	vec4 vTransUV = mul(chu_mTextureMatrix, vec4(input.uv, 1.0, 1.0));
	output.uv = vTransUV.xy;
#else
	output.uv = input.uv;
#endif
#endif

#if defined(VERTEX_COLOR)
	vec4 vColor = input.color;
#endif

#if defined(CONST_COLOR)
	vColor *= chu_vColor;
#endif

#if defined(VERTEX_COLOR)
	output.color = vColor;
#endif

	return output;
}
