/// LSU EE 4702-1 (Fall 2022), GPU Programming
//

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

#extension GL_GOOGLE_include_directive : enable

// 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;
layout (location = LOC_IN_COLOR) in vec4 in_color;
//
// The CPU must provide these inputs.


 /// 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;
};

 /// 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 ) );
  color = uni_light.color * in_color * phase / dist_to_light;
}
#endif

#ifdef _FRAGMENT_SHADER_

layout (location = 0) in Data_VF
{
  vec4 color;
};

// 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.
  //
  frag_color = gl_FrontFacing ? color : vec4(1,0,0,1);
  //
  // On important thing fragment shaders do--but not this one--is
  // apply textures.
}
#endif