// vert //

flat out uint object_flags;
out float distance;
out vec4 pos;

const vec3 luma = vec3(0.299, 0.587, 0.114);

void main(void) {
	object_flags = qgl_ObjectFlags;
	if (bitfieldExtract(object_flags, 2, 1) == 0)
		return;
	qgl_MaterialIndex = qgl_Material;
	qgl_FragTexture = qgl_getFragTexture();
	float height = dot(qgl_materialTexture(QGL_MAP_RELIEF, qgl_FragTexture, vec4(0.)).rgb, luma);
	pos = qgl_ViewProjMatrix * (qgl_ModelMatrix * (vec4(qgl_Vertex + qgl_Normal * height, 1.)));//qgl_ftransform();
	gl_Position = pos;
}


// frag //

flat in uint object_flags;
in float distance;
in vec4 pos;

float z_near = 0.1f;
const float _pe = 2.4e-7;

void main(void) {
	if (bitfieldExtract(object_flags, 2, 1) == 0)
		discard;
	vec4 diffuse = qgl_materialTexture(QGL_MAP_DIFFUSE, qgl_FragTexture.xy, vec4(0.f));
	if(diffuse.a < 0.5f)
		discard;

	//float z = gl_FragCoord.z;
	//z = z + z - 1;
	//z = ((_pe - 2.) * z_near) / (z + z + _pe - 2.); // infinite depth
	
#ifdef OMNI
	float z = length(pos.xyz);
#else
	float z = pos.z;
#endif
	z = 1.f - 1.f / (z - z_near + 1.f);
	gl_FragDepth = z;
	qgl_FragData[0].r = z;
	//qgl_FragData[0].r = length(vec3(pos.xy, z));//1/gl_FragCoord.w;
}
