/// LSU EE 4702-1 (Fall 2025), GPU Programming
//
 /// Homework 4 (EE 4702 Fall 2025)
 //
 //  Assignment: https://www.ece.lsu.edu/gpup/2025/hw04.pdf
 //
 //  Place your solution in routines vs_main, gs_main, and vs_main and
 //  surrounding code.
 //
 //  DO NOT put solution in vs_main_clean, gs_main_clean, and vs_main_clean.
 //  Either leave them untouched, or lightly modify them. They will
 //  not be graded.

// Specify version of OpenGL Shading Language.
//
#version 460

#extension GL_GOOGLE_include_directive : enable
#include <light.h>
#include "shader-common-generic-lighting.h"
#include "links-shdr-common.h"

///
 /// Shader Uniforms
///

layout ( binding = BIND_UNI_COMMON ) uniform Common_Uniform
{
  Shdr_Uni_Common com;
};

// Convenience Variables
//
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;

// Note: this uniform is also defined in transform.h.
layout ( binding = BIND_TRANSFORM ) uniform Uni_Transform
{
  //   Course Name           OpenGL Name
  mat4 eye_from_object;   // gl_ModelViewMatrix
  mat4 clip_from_eye;     // gl_ProjectionMatrix
  mat4 clip_from_object;  // gl_ModelViewProjectionMatrix
  mat4 object_from_eye;   // gl_ModelViewMatrixInverse
  mat4 eye_from_clip;     // gl_ProjectionMatrixInverse
  mat4 object_from_clip;  // gl_ModelViewProjectionMatrixInverse
};


#ifdef HW4_TRI
#ifdef _VERTEX_SHADER_

 /// Vertex Shader Inputs
///
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()
{
  // Homework 4 - Put solution here and in surrounding code.

  // Transform to clip space ..
  //
  gl_Position = clip_from_object * in_mat_object_from_local * in_vertex;
  //
  // .. and eye-space.
  //
  Out.vertex_e = eye_from_object * in_mat_object_from_local * in_vertex;

  // Pass color unchanged.
  //
  Out.color = in_color;
}

#endif

#ifdef _GEOMETRY_SHADER_

// Indicate type of input primitive expected by 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()
{
  // Homework 4 - Put solution here and in surrounding code.

  /// Geometry Shader
  //
  // Notes: 
  //
  //   - In[0] is the same vertex on all three triangles used to
  //     render a dodecahedron face.

  // Compute Triangle Normal
  //
  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);

  // Compute Center of Pentagon
  //
  const float pi = radians(180);  // No, pi is not a built in. Nor tau.
  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 );
  //
  // The calculation above is correct when In[1] and In[2] are an edge
  // of the pentagon.

  // Emit one triangle.
  //
  for ( int i=0; i<3; i++ )
    {
      // For convenience, write this vertex and next one in to variables. 
      //
      vec4 pt_i_e = In[ i ].vertex_e;
      vec4 pt_n_e = In[ (i+1)%3 ].vertex_e;

      // Check whether pt_i_e and pt_n_e form an edge of the pentagon.
      //
      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(); // Triangle Strip Re-Start
}

#endif

#ifdef _FRAGMENT_SHADER_

 /// Fragment Shader Inputs
//
layout ( location = 0 ) in GF
{
  vec4 vertex_e;
  vec4 color;
  vec3 normal_e;
} In;

layout ( binding = BIND_TEXUNIT ) uniform sampler2D tex_unit_0;

 /// Fragment Shader Output
//
layout ( location = 0 ) out vec4 out_frag_color;

void
fs_main()
{
  // Homework 4 - Put solution here and in surrounding code.

  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 // _FRAGMENT_SHADER_

#endif // HW4_TRI


/// Homework 4:
//
 ///  Code below should not be part of the solution.
 //   Use it for experimentation or keep it unmodified, as a reference.

#ifdef HW4_CLEAN
#ifdef _VERTEX_SHADER_

 /// Vertex Shader Inputs
///
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()
{
  /// Not part of solution. Modify vs_main instead.

  // Transform to clip space ..
  //
  gl_Position = clip_from_object * in_mat_object_from_local * in_vertex;
  //
  // .. and eye-space.
  //
  Out.vertex_e = eye_from_object * in_mat_object_from_local * in_vertex;

  // Pass color unchanged.
  //
  Out.color = in_color;

  /// Not part of solution. Modify vs_main instead.
}

#endif

#ifdef _GEOMETRY_SHADER_

// Indicate type of input primitive expected by 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()
{
  /// Not part of solution. Modify gs_main instead.

  /// Geometry Shader
  //
  // Notes: 
  //
  //   - In[0] is the same vertex on all three triangles used to
  //     render a dodecahedron face.

  // Compute Triangle Normal
  //
  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);

  // Compute Center of Pentagon
  //
  const float pi = radians(180);  // No, pi is not a built in. Nor tau.
  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 );
  //
  // The calculation above is correct when In[1] and In[2] are an edge
  // of the pentagon.

  /// Not part of solution. Modify gs_main instead.

  // Emit one triangle.
  //
  for ( int i=0; i<3; i++ )
    {
      // For convenience, write this vertex and next one in to variables. 
      //
      vec4 pt_i_e = In[ i ].vertex_e;
      vec4 pt_n_e = In[ (i+1)%3 ].vertex_e;

      // Check whether pt_i_e and pt_n_e form an edge of the pentagon.
      //
      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(); // Triangle Strip Re-Start

  /// Not part of solution. Modify gs_main instead.
}

#endif

#ifdef _FRAGMENT_SHADER_

 /// Fragment Shader Inputs
//
layout ( location = 0 ) in GF
{
  vec4 vertex_e;
  vec4 color;
  vec3 normal_e;
} In;

layout ( binding = BIND_TEXUNIT ) uniform sampler2D tex_unit_0;

 /// Fragment Shader Output
//
layout ( location = 0 ) out vec4 out_frag_color;

void
fs_main_clean()
{
  /// Not part of solution. Modify fs_main instead.

  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 // _FRAGMENT_SHADER_

#endif // HW4_CLEAN