#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);
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;
layout ( binding = BIND_LIGHT_SIMPLE ) uniform Uni_Light
{
vec4 position;
vec4 color;
} uni_light;
const int ncolors = 10;
layout ( binding = BIND_HW04 ) uniform Uni_Colors
{
vec4 front[ncolors], back[ncolors];
} uc;
#ifdef _VERTEX_SHADER_
layout (location = LOC_IN_POS) in vec4 in_vertex_o;
#ifdef LOC_IN_NORMAL
layout (location = LOC_IN_NORMAL) in vec3 in_normal_o;
#endif
#ifdef LOC_IN_INT1
layout (location = LOC_IN_INT1) in int in_color_idx;
#endif
layout (location = 0) out Data_VG
{
vec4 vertex_c;
vec4 vertex_o, vertex_e;
int c_idx;
};
void
vs_main_hw04()
{
c_idx = int(in_vertex_o.w);
vertex_o = vec4(in_vertex_o.xyz,1);
vertex_c = ut.clip_from_object * vertex_o;
vertex_e = ut.eye_from_object * vertex_o;
}
#endif
#ifdef _GEOMETRY_SHADER_
layout (location = 0) in Data_VG
{
vec4 vertex_c;
vec4 vertex_o;
vec4 vertex_e;
int c_idx;
} In[];
layout ( triangles ) in;
layout (location = 0) out Data_GF
{
vec4 color;
vec4 color_back;
};
layout ( triangle_strip, max_vertices = 3 ) out;
void
gs_main_hw04()
{
vec3 normal_e =
normalize( cross( In[1].vertex_e.xyz - In[0].vertex_e.xyz,
In[2].vertex_e.xyz - In[0].vertex_e.xyz ) );
int c_idx = In[0].c_idx;
for ( int i=0; i<3; i++ )
{
gl_Position = In[i].vertex_c;
vec4 vertex_e = In[i].vertex_e;
vec3 vec_vl = uni_light.position.xyz - vertex_e.xyz;
float dist_to_light = length( vec_vl );
float phase = abs( dot( normal_e, vec_vl ) / dist_to_light );
vec4 illum = uni_light.color * max( 0.025, phase / dist_to_light );
color = illum * uc.front[c_idx];
color_back = illum * uc.back[c_idx];
EmitVertex();
}
EndPrimitive();
}
#endif
#ifdef _FRAGMENT_SHADER_
layout (location = 0) in Data_GF
{
vec4 color;
vec4 color_back;
};
layout (location = 0) out vec4 frag_color;
void
fs_main_hw04()
{
frag_color = gl_FrontFacing ? color : color_back;
}
#endif