#version 460
#extension GL_GOOGLE_include_directive : enable
layout ( binding = BIND_MISC ) uniform Uni_Misc
{
ivec4 uni_misc;
vec4 opt_tryoutf4;
float radius_inner;
float p1, p2, p3;
mat4 rot;
};
bool opt_tryout1 = bool(uni_misc.x);
bool opt_tryout2 = bool(uni_misc.y);
float opt_tryoutf = opt_tryoutf4.x;
const int ncolors = 10;
layout ( binding = BIND_HW05 ) 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;
layout (location = LOC_IN_INT1) in int in_color_idx;
#endif
#ifdef HW05_PROB1
#ifdef _VERTEX_SHADER_
layout (location = 0) out Data_VG
{
vec4 vertex_c;
vec3 vertex_e, normal_e;
int color_idx;
vec3 inner_e; vec4 inner_c;
int vtx_idx; };
void
vs_main_prob1()
{
vertex_c = ut.clip_from_object * in_vertex_o;
vertex_e = vec3( ut.eye_from_object * in_vertex_o );
normal_e = normalize( mat3(ut.eye_from_object) * in_normal_o );
color_idx = in_color_idx;
vec4 ctr_o = vec4(0,0,in_vertex_o.z,1);
vec4 ctr_e = ut.eye_from_object * vec4(0,0,in_vertex_o.z,1);
vec3 v = normalize( vertex_e - ctr_e.xyz );
inner_e = ctr_e.xyz + v * radius_inner;
inner_c = ut.clip_from_eye * vec4(inner_e,1);
vtx_idx = gl_VertexIndex;
}
#endif
#ifdef _GEOMETRY_SHADER_
layout (location = 0) in Data_VG
{
vec4 vertex_c;
vec3 vertex_e, normal_e;
int color_idx;
vec3 inner_e; vec4 inner_c;
int vtx_idx; } In[];
layout (location = 0) out Data_GF
{
vec3 vertex_e;
vec3 normal_e;
flat int color_idx;
};
layout ( triangles ) in;
layout ( triangle_strip, max_vertices = 9 ) out;
void
gs_main_prob1()
{
for ( int i=0; i<3; i++ )
{
gl_Position = In[i].vertex_c;
vertex_e = In[i].vertex_e;
normal_e = In[i].normal_e;
color_idx = In[i].color_idx;
EmitVertex();
}
EndPrimitive();
int idx_skip = In[1].vtx_idx - In[0].vtx_idx;
vec3 normal_side_o = vec3(0,0,1);
vec3 normal_side_e = mat3(ut.eye_from_object) * normal_side_o;
for ( int i=0; i<3; i++ )
if ( i != idx_skip )
{
gl_Position = In[i].vertex_c;
vertex_e = In[i].vertex_e;
color_idx = 6;
normal_e = normal_side_e;
EmitVertex();
}
for ( int i=0; i<3; i++ )
if ( i != idx_skip )
{
gl_Position = In[i].inner_c;
vertex_e = In[i].inner_e.xyz;
color_idx = 6;
normal_e = normal_side_e;
EmitVertex();
}
EndPrimitive();
}
#endif
#endif
#ifdef HW05_PROB2
layout ( binding = BIND_XF ) buffer Gears { mat4 gears_xform[]; };
#ifdef _VERTEX_SHADER_
layout (location = 0) out Data_VG
{
vec4 vertex_c;
vec3 vertex_e, normal_e;
int color_idx;
};
void
vs_main_prob2()
{
int inst = gl_InstanceIndex;
mat4 g_from_l = gears_xform[inst] * rot;
vec4 vertex_g = g_from_l * in_vertex_o;
vertex_c = ut.clip_from_object * vertex_g;
vertex_e = vec3( ut.eye_from_object * vertex_g );
normal_e =
normalize( mat3(ut.eye_from_object) * mat3(g_from_l) * in_normal_o );
color_idx = in_color_idx;
}
#endif
#ifdef _GEOMETRY_SHADER_
layout (location = 0) in Data_VG
{
vec4 vertex_c;
vec3 vertex_e, normal_e;
int color_idx;
} In[];
layout (location = 0) out Data_GF
{
vec3 vertex_e;
vec3 normal_e;
flat int color_idx;
};
layout ( triangles ) in;
layout ( triangle_strip, max_vertices = 3 ) out;
void
gs_main_prob2()
{
for ( int i=0; i<3; i++ )
{
gl_Position = In[i].vertex_c;
vertex_e = In[i].vertex_e;
normal_e = In[i].normal_e;
color_idx = In[i].color_idx;
EmitVertex();
}
EndPrimitive();
}
#endif
#endif
#ifdef _FRAGMENT_SHADER_
layout (location = 0) in Data_GF
{
vec3 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;
float dist_to_light = length( vec_vl );
bool lit_side = dot( normal_e, vec_vl ) > 0 == dot( normal_e, -vertex_e ) > 0;
float phase = lit_side ? abs( dot( normal_e, vec_vl ) / dist_to_light ) : 0;
vec4 illum = uni_light.color * max( 0.015, phase / dist_to_light );
vec4 color = gl_FrontFacing ? uc.front[color_idx] : uc.back[color_idx];
frag_color = illum * color;
}
#endif