#version 460
#extension GL_GOOGLE_include_directive : enable
layout ( binding = BIND_MISC ) uniform Uni_Misc
{
ivec4 uni_misc;
};
bool opt_tryout1 = bool(uni_misc.x);
bool opt_tryout2 = bool(uni_misc.y);
const int ncolors = 10;
layout ( binding = BIND_HW03 ) uniform I_can_forget_this_name_no_problem
{
vec4 front[ncolors], back[ncolors];
} uc;
layout ( binding = BIND_LIGHT_SIMPLE ) uniform Uni_Light
{
vec4 position;
vec4 color;
} uni_light;
layout ( binding = BIND_TRANSFORM ) uniform Uni_Transform
{
mat4 eye_from_object, clip_from_eye, clip_from_object;
mat4 object_from_eye, eye_from_clip;
} ut;
#ifdef _VERTEX_SHADER_
layout (location = LOC_IN_POS) in vec4 in_vertex_o;
layout (location = LOC_IN_NORMAL) in vec3 in_normal_o;
#ifdef LOC_IN_INT1
layout (location = LOC_IN_INT1) in int in_color_idx;
#endif
layout (location = 0) out Data_VG
{
vec4 vertex_c, vertex_e;
vec3 normal_e;
int color_idx;
};
void
vs_main()
{
vertex_c = ut.clip_from_object * in_vertex_o;
vertex_e = ut.eye_from_object * in_vertex_o;
normal_e = normalize( mat3(ut.eye_from_object) * in_normal_o );
color_idx = in_color_idx;
}
#endif
#ifdef _GEOMETRY_SHADER_
layout ( triangles ) in;
layout ( triangle_strip, max_vertices = 3 ) out;
layout (location = 0) in Data_VG
{
vec4 vertex_c, vertex_e;
vec3 normal_e;
int color_idx;
} In[];
layout (location = 0) out Data_GF
{
vec4 vertex_e;
vec3 normal_e;
int color_idx;
} Out;
void
gs_main()
{
int i_last = 2 - gl_PrimitiveIDIn % 2;
if ( !opt_tryout2 && In[i_last].color_idx == 0 ) return;
for ( int i=0; i<3; i++ )
{
gl_Position = In[i].vertex_c;
Out.color_idx = In[i_last].color_idx;
Out.vertex_e = In[i].vertex_e;
Out.normal_e = In[i].normal_e;
EmitVertex();
}
EndPrimitive();
}
#endif
#ifdef _FRAGMENT_SHADER_
layout (location = 0) in Data_VF
{
vec4 vertex_e;
vec3 normal_e;
flat int color_idx;
};
layout (location = 0) out vec4 frag_color;
void
fs_main()
{
vec3 vec_vl = uni_light.position.xyz - vertex_e.xyz;
float dist_sq = dot( vec_vl, vec_vl );
bool lit_side = dot( normal_e, vec_vl )>0 == dot( normal_e, -vertex_e.xyz )>0;
float attenuation = abs( dot( normal_e, vec_vl ) / dist_sq );
vec4 mat_color = gl_FrontFacing ? uc.front[color_idx] : uc.back[color_idx];
if ( !lit_side )
{
mat_color = ( uc.front[color_idx] + uc.back[color_idx] ) * 0.25;
}
frag_color = mat_color * uni_light.color * attenuation;
}
#endif