#version 460
#extension GL_GOOGLE_include_directive : enable
#include <light.h>
#include "shader-common-generic-lighting.h"
#include "links-shdr-common.h"
layout ( binding = BIND_UNI_COMMON ) uniform Common_Uniform
{
Shdr_Uni_Common com;
};
bool opt_tryout1 = com.tryout.x != 0;
bool opt_tryout2 = com.tryout.y != 0;
bool opt_tryout3 = com.tryout.z != 0;
float opt_tryoutf = com.tryoutf.x;
bool opt_hw04_stel = com.opt_hw04.x != 0;
bool opt_hw04_dot = com.opt_hw04.y != 0;
bool opt_hw04_tex = com.opt_hw04.z != 0;
float opt_hw04_height_stel = com.tryoutf.y;
layout ( binding = BIND_TRANSFORM ) uniform Uni_Transform
{
mat4 eye_from_object; mat4 clip_from_eye; mat4 clip_from_object; mat4 object_from_eye; mat4 eye_from_clip; mat4 object_from_clip; };
#ifdef HW4_TRI
#ifdef _VERTEX_SHADER_
layout ( location = LOC_IN_POS ) in vec4 in_vertex;
layout ( location = LOC_IN_COLOR ) in vec4 in_color;
layout ( location = LOC_IN_MAT4 ) in mat4 in_mat_object_from_local;
layout (location = 0) out VG
{
vec4 vertex_e;
vec4 color;
} Out;
void
vs_main()
{
gl_Position = clip_from_object * in_mat_object_from_local * in_vertex;
Out.vertex_e = eye_from_object * in_mat_object_from_local * in_vertex;
Out.color = in_color;
}
#endif
#ifdef _GEOMETRY_SHADER_
layout ( triangles ) in;
layout ( triangle_strip, max_vertices = 6 ) out;
layout (location = 0) in VG
{
vec4 vertex_e;
vec4 color;
} In[3];
layout (location = 0) out GF
{
vec4 vertex_e;
vec4 color;
vec3 normal_e;
vec2 tcoor;
} Out;
void
gs_main()
{
vec3 v12 = In[2].vertex_e.xyz - In[1].vertex_e.xyz;
vec3 v10 = In[0].vertex_e.xyz - In[1].vertex_e.xyz;
vec3 tn = normalize( cross(v12,v10) );
float edge_len = length(v12);
const float pi = radians(180); vec3 v12o = cross(tn,v12) * float( 1.0 / ( 2 * tan( 2 * pi / 10 ) ) );
vec4 ctr_pentagon_e = vec4( In[1].vertex_e.xyz + 0.5f * v12 + v12o, 1 );
float ri_pent = length( v12o );
vec3 ax = normalize( In[0].vertex_e.xyz - ctr_pentagon_e.xyz );
vec3 ay = cross(tn,ax);
vec3 vx = ax / ri_pent;
vec3 vy = ay / ri_pent;
mat4 tr = mat4(1.0); tr[3] = -ctr_pentagon_e;
mat4 rot = mat4( transpose( mat3( vx, vy, vec3(0) ) ) );
mat4 m = rot * tr;
mat4x2 m2 = mat4x2(m);
vec4 ctr_stel_e =
opt_hw04_stel
? vec4( ctr_pentagon_e.xyz + tn * opt_hw04_height_stel * edge_len, 1 )
: ctr_pentagon_e;
for ( int i=0; i<3; i++ )
{
vec4 pt_i_e = In[i].vertex_e;
vec4 pt_n_e = In[(i+1)%3].vertex_e;
bool is_pentagon_edge = distance(pt_i_e,pt_n_e) < edge_len * 1.1f;
if ( opt_tryout3 )
{
vec3 to_ctr = vec3( ctr_pentagon_e - In[i].vertex_e );
vec4 px_e = vec4( In[i].vertex_e.xyz + 0.1 * to_ctr, 1 );
gl_Position = clip_from_eye * px_e;
Out.vertex_e = px_e;
Out.color = In[ i ].color;
Out.normal_e = tn;
Out.tcoor = m2 * Out.vertex_e;
EmitVertex();
continue;
}
if ( opt_hw04_stel )
{
if ( !is_pentagon_edge ) continue;
vec3 n = normalize
( cross( vec3( pt_i_e-ctr_stel_e ), vec3( pt_n_e-ctr_stel_e ) ) );
vec4 pts[3] = { pt_i_e, pt_n_e, ctr_stel_e };
for ( int j=0; j<3; j++ )
{
Out.vertex_e = pts[j];
gl_Position = clip_from_eye * Out.vertex_e;
Out.color = In[ i ].color;
Out.normal_e = n;
bool use_dot_products = false;
if ( use_dot_products )
{
vec3 vctr = Out.vertex_e.xyz - ctr_pentagon_e.xyz;
Out.tcoor = vec2( dot(vctr,vx), dot(vctr,vy) );
}
else
{
Out.tcoor = m2 * Out.vertex_e;
}
EmitVertex();
}
EndPrimitive();
continue;
}
gl_Position = gl_in[i].gl_Position;
Out.vertex_e = In[i].vertex_e;
Out.color = In[ i ].color;
Out.normal_e = tn;
Out.tcoor = m2 * pt_i_e;
EmitVertex();
}
EndPrimitive(); }
#endif
#ifdef _FRAGMENT_SHADER_
layout ( location = 0 ) in GF
{
vec4 vertex_e;
vec4 color;
vec3 normal_e;
vec2 tcoor;
} In;
layout ( binding = BIND_TEXUNIT ) uniform sampler2D tex_unit_0;
layout ( location = 0 ) out vec4 out_frag_color;
void
fs_main()
{
vec4 our_color = gl_FrontFacing ? In.color : vec4(1,0,0,1);
if ( opt_hw04_dot && length(In.tcoor) < 0.5 ) our_color = vec4(1,1,0,1);
vec4 lighted_color = generic_lighting( In.vertex_e, our_color, In.normal_e );
if ( opt_hw04_tex )
{
float a = pow(2,-0.5);
vec2 tc = ( In.tcoor + vec2(a,a) ) * vec2(a,a);
tc.y = 1 - tc.y;
bool out_of_range = any( greaterThan(tc,vec2(1)) )
|| any( lessThan(tc,vec2(0)) );
if ( !out_of_range )
lighted_color = lighted_color * texture(tex_unit_0,tc);
}
out_frag_color = lighted_color;
}
#endif
#endif
#ifdef HW4_CLEAN
#ifdef _VERTEX_SHADER_
layout ( location = LOC_IN_POS ) in vec4 in_vertex;
layout ( location = LOC_IN_COLOR ) in vec4 in_color;
layout ( location = LOC_IN_MAT4 ) in mat4 in_mat_object_from_local;
layout (location = 0) out VG
{
vec4 vertex_e;
vec4 color;
} Out;
void
vs_main_clean()
{
gl_Position = clip_from_object * in_mat_object_from_local * in_vertex;
Out.vertex_e = eye_from_object * in_mat_object_from_local * in_vertex;
Out.color = in_color;
}
#endif
#ifdef _GEOMETRY_SHADER_
layout ( triangles ) in;
layout ( triangle_strip, max_vertices = 3 ) out;
layout (location = 0) in VG
{
vec4 vertex_e;
vec4 color;
} In[3];
layout (location = 0) out GF
{
vec4 vertex_e;
vec4 color;
vec3 normal_e;
} Out;
void
gs_main_clean()
{
vec3 v12 = In[2].vertex_e.xyz - In[1].vertex_e.xyz;
vec3 v10 = In[0].vertex_e.xyz - In[1].vertex_e.xyz;
vec3 tn = normalize( cross(v12,v10) );
float edge_len = length(v12);
const float pi = radians(180); vec3 v12o = cross(tn,v12) * float( 1.0 / ( 2 * tan( 2 * pi / 10 ) ) );
vec4 ctr_pentagon_e = vec4( In[1].vertex_e.xyz + 0.5f * v12 + v12o, 1 );
for ( int i=0; i<3; i++ )
{
vec4 pt_i_e = In[ i ].vertex_e;
vec4 pt_n_e = In[ (i+1)%3 ].vertex_e;
bool is_pentagon_edge = distance(pt_i_e,pt_n_e) < edge_len * 1.1f;
gl_Position = gl_in[i].gl_Position;
Out.vertex_e = In[i].vertex_e;
Out.color = In[i].color;
Out.normal_e = tn;
EmitVertex();
}
EndPrimitive();
}
#endif
#ifdef _FRAGMENT_SHADER_
layout ( location = 0 ) in GF
{
vec4 vertex_e;
vec4 color;
vec3 normal_e;
} In;
layout ( binding = BIND_TEXUNIT ) uniform sampler2D tex_unit_0;
layout ( location = 0 ) out vec4 out_frag_color;
void
fs_main_clean()
{
vec2 tc = vec2(0.5,0.5);
vec4 texel = texture(tex_unit_0,tc);
vec4 our_color = gl_FrontFacing ? In.color : vec4(1,0,0,1);
vec4 lighted_color = generic_lighting( In.vertex_e, our_color, In.normal_e );
out_frag_color = lighted_color;
}
#endif
#endif