/// LSU EE 4702-1 (Fall 2022), GPU Programming
//
 /// Homework 3 -- SOLUTION
//
//   Modify this file AND hw03.cc.
//   Assignment: https://www.ece.lsu.edu/koppel/gpup/2022/hw03.pdf
//   Solution:   https://www.ece.lsu.edu/koppel/gpup/2022/hw03_sol.pdf

// Specify version of OpenGL Shading Language.
//
#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);


// Declare variables for communication between vertex shader
// and fragment shader.
//
#ifdef _VERTEX_SHADER_

 /// Vertex Shader Inputs
//
layout (location = LOC_IN_POS) in vec4 in_vertex_o;
layout (location = LOC_IN_NORMAL) in vec3 in_normal_o;
#ifdef LOC_IN_COLOR
layout (location = LOC_IN_COLOR) in vec4 in_color;
#endif
#ifdef LOC_IN_INT1
layout (location = LOC_IN_INT1) in int in_color_idx;
#endif
//
// The CPU must provide these inputs.


 /// SOLUTION -- Problem 1
const int ncolors = 10;
layout ( binding = BIND_HW03 ) uniform I_can_forget_this_name_no_problem
{
  vec4 front[ncolors], back[ncolors];
} uc;


 /// Vertex Shader Uniforms
//
layout ( binding = BIND_LIGHT_SIMPLE ) uniform Uni_Light
{
  vec4 position;
  vec4 color;
} uni_light;


layout ( binding = BIND_TRANSFORM ) uniform Uni_Transform
{
  // Transformation Matrices
  mat4 eye_from_object, clip_from_eye, clip_from_object;
  mat4 object_from_eye, eye_from_clip;
} ut;


 /// Vertex Shader Output
//
layout (location = 0) out Data_VF
{
  vec4 color;
  /// SOLUTION -- Problem 1
  vec4 color_back;
};

 /// The Vertex Shader
void
vs_main()
{
  // Transform the vertex object-space coordinate to clip space.
  //
  gl_Position = ut.clip_from_object * in_vertex_o;
  //
  // gl_Position is predefined (by OGSL) and must be written with
  // the clip-space coordinates of the vertex.

  // Compute eye-space coordinates for vertex and normal.
  //
  vec4 vertex_e = ut.eye_from_object * in_vertex_o;
  vec3 normal_e = normalize( mat3(ut.eye_from_object) * in_normal_o );

  // Compute lighted color.
  //
  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 ) );

  /// SOLUTION -- Problem 1
  color = uni_light.color * uc.front[in_color_idx] * phase / dist_to_light;

  /// SOLUTION -- Problem 1
  color_back = uni_light.color * uc.back[in_color_idx] * phase / dist_to_light;
}
#endif

#ifdef _FRAGMENT_SHADER_

layout (location = 0) in Data_VF
{
  vec4 color;
  /// SOLUTION -- Problem 1
  vec4 color_back;
};

// Fragment Shader Output
//
layout (location = 0) out vec4 frag_color;
//
// This will get written to frame buffer if tests pass, such as the
// depth (z-buffer) test.

void
fs_main()
{
  // All this fragment shader does is substitute red for the color if
  // the user is seeing the back of the triangle.
  //
  /// SOLUTION -- Problem 1
  frag_color = gl_FrontFacing ? color : color_back;
  //
  // On important thing fragment shaders do--but not this one--is
  // apply textures.
}
#endif