#version 460
#extension GL_GOOGLE_include_directive : enable
#include <light.h>
#include "shader-common-generic-lighting.h"
#include "links-shdr-common.h"
#ifdef BIND_UNI_COMMON
layout ( binding = BIND_UNI_COMMON ) uniform Common_Uniform
{
Shdr_Uni_Common com;
};
#endif
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_hw05_stel = com.opt_hw05.x != 0;
bool opt_hw05_dot = com.opt_hw05.y != 0;
bool opt_hw05_tex = com.opt_hw05.z != 0;
float opt_hw05_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 BIND_HW5_COLOR
layout ( binding = BIND_HW5_COLOR ) buffer cb1 { vec4 buf_hw05_color[]; };
#endif
#ifdef BIND_HW5_MATRIX
layout ( binding = BIND_HW5_MATRIX ) buffer cb2 { mat4 buf_hw05_matrix[]; };
#endif
#ifdef BIND_DOD_COORS_L
layout ( binding = BIND_DOD_COORS_L ) buffer cb3 { vec4 buf_dod_coors_l[]; };
#endif
#ifdef HW5_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;
layout (location = 0) out VG
{
vec4 vertex_e;
vec4 color;
} Out;
void
vs_main()
{
gl_Position = clip_from_object * in_mat * in_vertex;
Out.vertex_e = eye_from_object * in_mat * in_vertex;
Out.color = in_color;
}
#endif
#ifdef _GEOMETRY_SHADER_
layout ( triangles ) in;
layout ( triangle_strip, max_vertices = 9 ) 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 rc_pent = length( v12o );
vec3 ax = normalize( In[0].vertex_e.xyz - ctr_pentagon_e.xyz );
vec3 ay = cross(tn,ax);
vec3 vx = ax / rc_pent, vy = ay / rc_pent;
mat4 tr = mat4(1.0); tr[3] = -ctr_pentagon_e;
mat4 rot = mat4( transpose( mat3( vx, vy, vec3(0) ) ) );
mat4x2 m2 = mat4x2( rot * tr );
vec4 ctr_stel_e =
opt_hw05_stel
? vec4( ctr_pentagon_e.xyz + tn * opt_hw05_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;
Out.tcoor = m2 * pt_i_e;
if ( opt_hw05_stel )
{
if ( !is_pentagon_edge ) continue;
vec4 pts[3] = { pt_i_e, pt_n_e, ctr_stel_e };
vec3 n = normalize
( cross( vec3( pt_i_e-ctr_stel_e ), vec3( pt_n_e-ctr_stel_e ) ) );
for ( int j=0; j<3; j++ )
{
Out.vertex_e = pts[j];
gl_Position = clip_from_eye * pts[j];
Out.color = In[i].color;
Out.normal_e = n;
Out.tcoor = m2 * pts[j];
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;
EmitVertex();
}
EndPrimitive(); }
#endif
#endif
#ifdef HW5_PTS_PROB_1
#ifdef _VERTEX_SHADER_
#ifdef LOC_IN_POS
layout ( location = LOC_IN_POS ) in vec4 in_vertex;
#endif
#ifdef LOC_IN_INT1
layout ( location = LOC_IN_INT1 ) in int in_color_idx;
#endif
#ifdef LOC_IN_MAT4
layout ( location = LOC_IN_MAT4 ) in mat4 in_mat;
#endif
#ifdef LOC_IN_NORMAL
layout ( location = LOC_IN_NORMAL ) in vec3 in_normal;
#endif
void
vs_main_pts_prob_1()
{
}
#endif
#ifdef _GEOMETRY_SHADER_
layout ( points ) in;
layout ( triangle_strip, max_vertices = 15 ) out;
layout (location = 0) out GF
{
vec4 vertex_e;
flat vec4 color;
flat vec3 normal_e;
vec2 tcoor;
} Out;
void
gs_main_pts_prob_1()
{
const float pi = radians(180);
const float delta_th = 2 * pi / 5;
const int prim_id = gl_PrimitiveIDIn;
int dod_idx = gl_PrimitiveIDIn / 12;
int face_idx = gl_PrimitiveIDIn % 12;
vec4 color = buf_hw05_color[dod_idx];
mat4 eye_from_local = eye_from_object * buf_hw05_matrix[dod_idx];
vec4 dod_ctr_e = eye_from_local * vec4(0,0,0,1);
int pt0_idx = face_idx * 5;
vec4 pent_pts_e[5];
for ( int i=0; i<5; i++ )
pent_pts_e[i] = eye_from_local * buf_dod_coors_l[pt0_idx+i];
vec3 az =
normalize( cross( vec3( pent_pts_e[2] - pent_pts_e[1] ),
vec3( pent_pts_e[0] - pent_pts_e[1] ) ) );
float edge_len = distance( pent_pts_e[1], pent_pts_e[2] );
float ri_e = edge_len * 1.11351636441;
vec4 pent_ctr_e = dod_ctr_e + vec4(ri_e * az,0);
vec3 vx = vec3( pent_pts_e[0] - pent_ctr_e );
vec3 vy = cross( az, vx );
mat4 tr = mat4(1.0); tr[3] = -pent_ctr_e;
mat4 scale = mat4( 1 / ( cos( 2 * pi / 10 ) * dot( vx, vx ) ) );
mat4 rot = mat4( transpose( mat3( vx, vy, vec3(0) ) ) );
mat4x2 m2 = mat4x2( scale * rot * tr );
vec4 ctr_stel_e =
opt_hw05_stel
? vec4( pent_ctr_e.xyz + az * opt_hw05_height_stel * edge_len, 1 )
: pent_ctr_e;
for ( int i=0; i<5; i++ )
{
vec4 pts[3] = { ctr_stel_e, pent_pts_e[i], pent_pts_e[(i+1)%5] };
vec3 n = normalize
( cross( vec3(pts[0] - pts[1]), vec3(pts[0] - pts[2]) ) );
for ( int j=0; j<3; j++ )
{
Out.vertex_e = pts[j];
gl_Position = clip_from_eye * Out.vertex_e;
Out.color = color;
Out.normal_e = n;
Out.tcoor = m2 * pts[j];
EmitVertex();
}
EndPrimitive();
}
}
#endif
#endif
#ifdef HW5_INST_PROB_2
#ifdef _VERTEX_SHADER_
layout ( location = LOC_IN_POS ) in vec4 in_vertex;
layout ( location = LOC_IN_TCOOR ) in vec2 in_tcoor;
layout (location = 0) out VG
{
vec4 vertex_e;
vec4 color;
vec2 tcoor;
} Out;
void
vs_main_inst_prob_2()
{
const int instance_id = gl_InstanceIndex;
const int vertex_id = gl_VertexIndex;
mat4 object_from_local = buf_hw05_matrix[gl_InstanceIndex];
gl_Position = clip_from_object * object_from_local * in_vertex;
Out.vertex_e = eye_from_object * object_from_local * in_vertex;
Out.color = buf_hw05_color[gl_InstanceIndex];
Out.tcoor = in_tcoor;
}
#endif
#ifdef _GEOMETRY_SHADER_
layout ( triangles ) in;
layout ( triangle_strip, max_vertices = 3 ) out;
layout (location = 0) in VG
{
vec4 vertex_e;
vec4 color;
vec2 tcoor;
} In[3];
layout (location = 0) out GF
{
vec4 vertex_e;
vec4 color;
vec3 normal_e;
vec2 tcoor;
} Out;
void
gs_main_inst_prob_2()
{
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);
vec4 ctr_pentagon_e = In[0].vertex_e;
vec4 ctr_stel_e =
opt_hw05_stel
? vec4( ctr_pentagon_e.xyz + tn * opt_hw05_height_stel * edge_len, 1 )
: ctr_pentagon_e;
if ( opt_hw05_stel )
{
vec3 v10s = ctr_stel_e.xyz - In[1].vertex_e.xyz;
tn = normalize( cross(v12, v10s ) );
}
for ( int i=0; i<3; i++ )
{
if ( i == 0 && opt_hw05_stel )
{
Out.vertex_e = ctr_stel_e;
gl_Position = clip_from_eye * Out.vertex_e;
}
else
{
Out.vertex_e = In[i].vertex_e;
gl_Position = gl_in[i].gl_Position;
}
Out.color = In[i].color;
Out.tcoor = In[i].tcoor;
Out.normal_e = tn;
EmitVertex();
}
EndPrimitive(); }
#endif
#endif
#ifdef HW5_PTS_CLEAN
#ifdef _VERTEX_SHADER_
layout ( location = LOC_IN_POS ) in vec4 in_vertex;
layout ( location = LOC_IN_INT1 ) in int in_color_idx;
layout ( location = LOC_IN_MAT4 ) in mat4 in_mat;
layout ( location = LOC_IN_NORMAL ) in vec3 in_normal;
layout (location = 0) out VG
{
mat4 obj_from_local;
vec4 pent_pt0_l;
vec3 dod_to_pctr_l;
int color_idx;
} Out;
void
vs_main_pts_clean()
{
Out.obj_from_local = in_mat;
Out.pent_pt0_l = in_vertex;
Out.dod_to_pctr_l = in_normal;
Out.color_idx = in_color_idx;
}
#endif
#ifdef _GEOMETRY_SHADER_
layout ( points ) in;
layout ( triangle_strip, max_vertices = 15 ) out;
layout (location = 0) in VG
{
mat4 obj_from_local;
vec4 pent_pt0_l;
vec3 dod_to_pctr_l;
int color_idx;
} In[1];
layout (location = 0) out GF
{
vec4 vertex_e;
flat vec4 color;
flat vec3 normal_e;
vec2 tcoor;
} Out;
void
gs_main_pts_clean()
{
const float pi = radians(180);
const float delta_th = 2 * pi / 5;
mat4 eye_from_local = eye_from_object * In[0].obj_from_local;
vec4 dod_ctr_e = eye_from_local * vec4(0,0,0,1);
vec4 pent_ctr_l = vec4( In[0].dod_to_pctr_l, 1 );
vec4 pent_ctr_e = eye_from_local * pent_ctr_l;
vec4 pt0_l = In[0].pent_pt0_l;
vec4 pt0_e = eye_from_local * pt0_l;
vec3 az = normalize( pent_ctr_e.xyz - dod_ctr_e.xyz );
vec3 vx = pt0_e.xyz - pent_ctr_e.xyz;
vec3 vy = cross( az, vx );
mat4 tr = mat4(1.0); tr[3] = -pent_ctr_e;
mat4 scale = mat4( 1 / ( cos( 2 * pi / 10 ) * dot( vx, vx ) ) );
mat4 rot = mat4( transpose( mat3( vx, vy, vec3(0) ) ) );
mat4x2 m2 = mat4x2( scale * rot * tr );
vec4 pent_pts_e[5];
pent_pts_e[0] = pt0_e;
vec4 color = buf_hw05_color[In[0].color_idx];
for ( int i=1; i<5; i++ )
pent_pts_e[i] = pent_ctr_e
+ vec4( cos(i*delta_th) * vx + sin(i*delta_th) * vy, 0 );
float edge_len = distance(pent_pts_e[1],pent_pts_e[2]);
vec4 ctr_stel_e =
opt_hw05_stel
? vec4( pent_ctr_e.xyz + az * opt_hw05_height_stel * edge_len, 1 )
: pent_ctr_e;
for ( int i=0; i<5; i++ )
{
vec4 pts[3] = { ctr_stel_e, pent_pts_e[i], pent_pts_e[(i+1)%5] };
vec3 n = normalize
( cross( pts[0].xyz - pts[1].xyz, pts[0].xyz - pts[2].xyz ) );
for ( int j=0; j<3; j++ )
{
Out.vertex_e = pts[j];
gl_Position = clip_from_eye * Out.vertex_e;
Out.color = color;
Out.normal_e = n;
Out.tcoor = m2 * pts[j];
EmitVertex();
}
EndPrimitive();
}
}
#endif
#endif
#ifdef _FRAGMENT_SHADER_
layout ( location = 0 ) in GF
{
vec4 vertex_e;
vec4 color;
vec3 normal_e;
vec2 tcoor;
} In;
#ifdef BIND_TEXUNIT
layout ( binding = BIND_TEXUNIT ) uniform sampler2D tex_unit_0;
#endif
layout ( location = 0 ) out vec4 out_frag_color;
void
fs_main()
{
vec4 our_color = gl_FrontFacing ? In.color : vec4(1,0,0,1);
float cd = length(In.tcoor);
if ( opt_hw05_dot && cd < 0.5 ) our_color = vec4(1,1,0,1);
float a = pow(2,-0.5);
vec2 tc = ( In.tcoor + vec2(a,a) ) * vec2(a,a);
tc.y = 1 - tc.y;
vec4 texel = texture(tex_unit_0,tc);
bool out_of_range = any( greaterThan(tc,vec2(1)) )
|| any( lessThan(tc,vec2(0)) );
vec4 lighted_color = generic_lighting( In.vertex_e, our_color, In.normal_e );
if ( opt_hw05_tex && !out_of_range )
lighted_color = lighted_color * texel;
out_frag_color = lighted_color;
}
#endif