/// LSU EE 4702-1 (Fall 2023), GPU Programming
//
 /// Simple Demo of Vulkan. Relatively Simple

/// Purpose
//
//   Demonstrate simple Vulkan


/// What Code Does

// Shows a sphere and a triangle.

#if 0
/// Background
//

 /// References
//
// :vl13:  Vulkan Specification 1.3
//         https://www.khronos.org/registry/vulkan/specs/1.3-extensions/html/


/// Primitive Topologies
//
 // :vl13: Section 21.1
 //
 // :Def:  Primitive Topology (in Vulkan)
 //       A specification to two things:
 //         - Topology Class
 //           This is the shape of the primitive. 
 //         - How vertices are grouped into those primitives.
 //
 // List of Vulkan 1.3 Primitive Topologies
 //
      vk::PrimitiveTopology::ePointList
      vk::PrimitiveTopology::eLineList
      vk::PrimitiveTopology::eLineStrip
      vk::PrimitiveTopology::eTriangleList
      vk::PrimitiveTopology::eTriangleStrip
      vk::PrimitiveTopology::eTriangleFan
      vk::PrimitiveTopology::eLineListWithAdjacency
      vk::PrimitiveTopology::eLineStripWithAdjacency
      vk::PrimitiveTopology::eTriangleListWithAdjacency
      vk::PrimitiveTopology::eTriangleStripWithAdjacency
      vk::PrimitiveTopology::ePatchList
 //
 /// Shape of Primitive (Topology Class)
 //  :vl13: 21.1.1
 //
 //  - Triangle
 //    Most common. (In this course, anyway.)
 //    Consists of three vertices, of course.
 //
 //  - Line
 //    Consists of two vertices.
 //
 //  - Point
 //    Consists of one vertices.
 //
 //  - Patch
 //    Consists of several vertices.
 //
 /// Grouping of Vertices
 //
 //  Consider vertex shader input:   v1 v2 v3 v4 v5 v6 ...
 //
 //  Grouping determines:
 //
 //  - How vertices grouped into primitives.
 //  - Nearby vertices seen by geometry shader.
 //
 /// eTriangleList
 //
 //  v1 v2 v3  v4 v5 v6  v7 v8 v9 ...
 //  ---T1---  ---T2---  ---T3---
 //
 //  For T triangles, need 3T vertices.
 //
 /// eTriangleStrip
 //
 //  v1 v2 v3 v4 v5 v6  v7 v8 v9 ...
 //  ---T1---
 //     ---T2---
 //        ---T3---
 //           ---T4---
 //
 //  For T triangles need T+2 vertices.
 //
 /// eTriangleFan
 //
 //  v1 v2 v3 v4 v5 v6  v7 v8 v9 ...
 //  ---T1---
 //  --    -T2--
 //  --       -T3--
 //  --          -T4--
 //
 //  Note: vertex v1 is in every triangle.
 //  For T triangles need T+2 vertices.
 //



/// Vertex Attributes
//
//  :Def: Vertex Attribute
//
//  Data associated with a vertex. Also called associated data.
//
//  Attributes include:
//
//    - Vertex coordinate.
        bset << pCoor(1,2,3);        // EE 4702 Vulkan Helper
        glVertex3fv( pCoor(1,2,3) ); // OpenGL

//
//    - Vertex color. (In the Compatibility Profile)
        bset << color_orange;     // EE 4702 Vulkan Helper
        glColor3fv(color_orange); // OpenGL
//
//    - Vertex "normal". In the Compatibility Profile)
        bset << pVect(1,0,0);     // EE 4702 Vulkan Helper
        glNormal(1,0,0);          // OpenGL
//

 /// Attribute Values
//
//   Historically, a vertex attribute was a four-element vector
//   of single-precision (32b) floating point numbers.
//
//   Such a vector could easily represent two important quantities:
//
//     - A homogeneous coordinate: (x,y,z,w)
//     - A color with transparency:  (r,g,b,a)
//
//     Coordinates and colors make up a large fraction of the data
//     needed for 3D graphics.
//
//   Vectors
//
//     Yes, these only need three floats ...
//     ... but they still occupy a "slot" that can hold four floats ...
//     ... because avoiding the wasted space (one float) ...
//     ... is not worth the trouble of having two attribute sizes ...
//     ... especially if one of those sizes is not a power of 2.
//
//  Attribute Values in the year 2019
//
//     Performance is still best when attributes are 4-element vectors.
//     ( In fact, other kinds of values should also have a power-of-2 size.)


 /// Vertex Attributes
 //
 //  :lindholm01: Description of an early GPU, showing origin of attrib limits.
 //  https://www.ece.lsu.edu/koppel/gp/srefs/p149-lindholm.pdf
 //
 //  Older versions of OpenGL defined 16 attributes ..
 //  .. each with a specific purpose.
 //
 //  This limit of 16 attributes is based on hardware constraints.
 //    Note: 16 * 4 * sizeof(float) = 256 B.
 //    So hardware had to move 256 B of data for each vertex,
 //      and have at least that much storage in "shader" performing
 //      vertex calculations.
 //
 //  Current versions of OpenGL define only a few attributes ..
 //  .. such as coordinates.
 //  The user is free to define others (to be used by user-written code).
 //
 //  The maximum number of vertex attributes that can be defined is
 //  the value of:
       GL_MAX_VERTEX_ATTRIBS;




#endif


///  Keyboard Commands
 //
 /// Object (Eye, Light, Ball) Location or Push
 //   Arrows (←,→,↑,↓) Page Up, Page Down
 //        Move object or push ball, depending on mode.
 //        Shift + KEY: motion is 5x faster.
 //        Ctrl + KEY : motion is 5x slower.
 //   'e': Move eye.
 //   'l': Move light.
 //   'b': Move sphere.
 //
 /// Eye Direction and GUI Options
 //
 //   'Home', 'End', 'Delete', 'Insert'
 //         Turn the eye direction. Home should rotate eye direction
 //         up, End should rotate eye down, Delete should rotate eye
 //         left, Insert should rotate eye right. The eye direction
 //         vector is displayed in the upper left.
 //  'C-='  (Ctrl =) Increase green text size.
 //  'C--'  (Ctrl -) Decrease green text size.
 //  'F12'  Write screenshot to PNG file.

 /// Simulation Options
 //  (Also see variables below.)
 //
 //  'F12'  Write screenshot to file.

 /// Variables
 //   Selected program variables can be modified using the keyboard.
 //   Use "Tab" to cycle through the variable to be modified, the
 //   name of the variable is displayed next to "VAR" on the bottom
 //   line of green text.

 //  'Tab' Cycle to next variable.
 //  '`'   Cycle to previous variable.
 //  '+'   Increase variable value.
 //  '-'   Decrease variable value.
 //
 //  VAR Light Intensity - The light intensity.


// Include files provided for this course.
//
#define MAIN_INCLUDE
#include <vhelper.h>

#include <vstroke.h>
#include <gp/coord.h>
#include <gp/pstring.h>
#include <gp/misc.h>
#include <gp/colors.h>

#include <vutil-texture.h>
#include <vutil-pipeline.h>
#include "shapes.h"


array tops
  { vk::PrimitiveTopology::eTriangleList,
    vk::PrimitiveTopology::eTriangleStrip,
    vk::PrimitiveTopology::eLineStrip,
    vk::PrimitiveTopology::eLineList
  };

class World {
public:
  World(pVulkan_Helper &vh)
    :vh(vh),ff_state(vh.qs),frame_timer(vh.frame_timer),shapes(ff_state),
     transform(vh.qs){}
  void setup_and_run();
  void render(vk::CommandBuffer& cb);
  void cb_keyboard();

  // Class providing utilities, such as showing text.
  //
  pVulkan_Helper& vh;
  VFixed_Function_State_Manager ff_state;

  // Class for showing frame timing.
  //
  pFrame_Timer& frame_timer;

  Shapes shapes;

  // Class for easy keyboard control of variables.
  //
  pVariable_Control variable_control;

  size_t opt_primitive;

  pCoor light_location;
  float opt_light_intensity;
  enum { MI_Eye, MI_Light, MI_Ball, MI_Ball_V, MI_COUNT } opt_move_item;
  bool opt_2_color;

  int slices; // Level of detail for sphere.
  pCoor sphere_location;
  float sphere_size;

  pCoor eye_location;
  pVect eye_direction;

  bool global_transform_stale;

  VTransform transform;
  VBufferV<Uni_Lighting> uni_light;
  VPipeline pipe_lonely;
  VPipeline pipe_sphere;
  VVertex_Buffer_Set bset_lonely, bset_sphere;
};

void
World::setup_and_run()
{
  vh.init(); // inst, phys, logical dev, win, surf, render_pass

  vh.display_cb_set([&](){});
  vh.cbs_cmd_record.push_back( [&](vk::CommandBuffer& cb){ render(cb); });

  eye_location = pCoor(1,0.5,3);
  eye_location = pCoor(1,.5,10.2);
  eye_direction = pVect(0,0,-1);

  opt_light_intensity = 4.3;
  light_location = pCoor(6.2,0.0,3.7);
  opt_2_color = false;

  sphere_location = pCoor(0,0,-0.5);
  sphere_size = 2;

  slices = 20;
  variable_control.insert(slices,"Slices in Sphere",1,2);

  variable_control.insert(opt_light_intensity,"Light Intensity");
  variable_control.insert(sphere_size,"Sphere Size");

  opt_move_item = MI_Eye;

  opt_primitive = 0;

  uni_light.init(vh.dev_phys,vh.dev,vk::BufferUsageFlagBits::eUniformBuffer);
  pColor black(0,0,0,0);
  uni_light->cgl_LightModel.ambient = pColor(.4,.4,.4,1);
  uni_light->cgl_LightSource[0].position = light_location;
  uni_light->cgl_LightSource[0].diffuse = pColor(1,1,1,1);
  uni_light->cgl_LightSource[0].ambient = black;
  uni_light->cgl_LightSource[0].specular = black;
  uni_light->cgl_LightSource[0].constantAttenuation = .5;
  uni_light->cgl_LightSource[0].linearAttenuation = 1.0;
  uni_light->cgl_LightSource[0].quadraticAttenuation = 0;

  // Start the graphics. The function below does not return until the
  // user exits by closing the window.
  //
  vh.message_loop_spin();
  //
  // At this point the user exited and so it's time to
  // clean up.

  uni_light.destroy();
  pipe_lonely.destroy();
  pipe_sphere.destroy();
  bset_lonely.destroy();
  bset_sphere.destroy();
  shapes.destroy();
  transform.destroy();

  vh.finish();
}

void
World::render(vk::CommandBuffer& cb)
{
  // This routine called whenever window needs to be updated.

  // Get any waiting keyboard commands.
  //
  cb_keyboard();

  // Start a timer object used for tuning this code.
  //

  /// Frame Buffer Informational Messages
  //
  //  Print messages using utility functions provided for this course.
  //

  vh.fbprintf("%s\n",frame_timer.frame_rate_text_get());

  vh.fbprintf
    ("Eye location: [%5.1f, %5.1f, %5.1f]  "
     "Eye direction: [%+.2f, %+.2f, %+.2f]\n",
     eye_location.x, eye_location.y, eye_location.z,
     eye_direction.x, eye_direction.y, eye_direction.z);

  vh.fbprintf
    ("Light location: [%5.1f, %5.1f, %5.1f]  Render Using: ('n') %s  "
     "Use %s colors ('c')\n",
     light_location.x, light_location.y, light_location.z,
     vk::to_string(tops[opt_primitive]).c_str(),
     opt_2_color ? "TWO" : "ONE" );

  pVariable_Control_Elt* const cvar = variable_control.current;
  vh.fbprintf("VAR %s = %.5f  (TAB or '`' to change, +/- to adjust)\n",
                      cvar->name,cvar->get_val());


  // -------------------------------------------------------------------------
  ///
  /// Specification of Transformation Matrices
  ///

  /// Setup Modelview Transformation:  Object Space -> Eye Space

  transform.eye_from_global_set
    ( pMatrix_Rotation(eye_direction,pVect(0,0,-1))
      * pMatrix_Translate(-eye_location) );

  /// Setup Projection Transformation:  Eye Space -> Clip Space
  //

  const int win_width = vh.s_extent.width;
  const int win_height = vh.s_extent.height;
  const float aspect = float(win_width) / win_height;
  const float n_dist = 0.01;
  const float xr = .8 * n_dist;

  // Frustum: left, right, bottom, top, near, far
  transform.clip_from_eye_set
    ( pMatrix_Frustum( -xr, xr,                // left, right
                       -xr/aspect, xr/aspect,  // bottom, top
                       n_dist, 5000            // near, far 
                       ) );

  /// Viewport Transformation
  //
  //  This is not set explicitly under ordinary circumstances.
  //  The transformation is automatically set based on the window size.


  /// Lighting
  //
  uni_light->cgl_LightSource[0].diffuse = color_white * opt_light_intensity;
  uni_light->cgl_LightSource[0].position =
    transform.eye_from_global * light_location;
  uni_light.to_dev();

  //
  // -------------------------------------------------------------------------


  ///
  /// Paint Single Triangle.
  ///

  if ( !pipe_lonely )
    pipe_lonely
      .init( vh.qs )
      .ds_follow( transform )
      .ds_use( uni_light )
      .ds_set_material_back( color_red )
      .shader_lighting_on()
      .topology_set( vk::PrimitiveTopology::eTriangleList )
      .create();

  bset_lonely.reset(pipe_lonely);


  // Object-Space Coordinates
  //
  pCoor p0 = { 0, 0,  0 };
  pCoor p1 = { 9, 6, -9 };
  pCoor p2 = { 0, 5, -5 };

  /// Specify normal for triangle.
  //
  // Use cross product function (in coord.h) to find normal.
  //
  pNorm tri_norm = cross( p0, p1, p2 );
  pColor color_tri( .470, .553, .965 ); // Red, Green, Blue

  // Insert vertex coordinates into buffer set (bset_lonely).
  //
  bset_lonely << p0 << p1 << p2;

  // Insert vertex colors and normals.
  //
  bset_lonely << color_tri << color_tri << color_tri;
  bset_lonely << tri_norm << tri_norm << tri_norm;
  //
  // All three vertices here are the same color and have the same
  // normal.

  bset_lonely.to_dev();
  pipe_lonely.record_draw(cb, bset_lonely);


  ///
  /// Paint a Sphere
  ///

  auto prim_want = tops[opt_primitive];

  if ( pipe_sphere.p_in_asm_ci.topology != prim_want && vh.frame_serial_need() )
    pipe_sphere.destroy();

  if ( !pipe_sphere )
    pipe_sphere
      .init( vh.qs )
      .ds_use( uni_light )
      .ds_set_material_back( color_red )
      .shader_lighting_on()
      .topology_set( prim_want )
      .create();

  // Construct color objects using hex RGB codes. See coord.h and colors.h.
  //
  const pColor lsu_spirit_purple(0x580da6);
  const pColor lsu_spirit_gold(0xf9b237);

  /// Populate bset_sphere With Sphere's Triangles
  //

  // Colors for outside of sphere.
  //
  pColor color = lsu_spirit_gold;
  pColor color2 = opt_2_color ? lsu_spirit_purple : color;
  //
  // If opt_2_color (something that can be set by the UI) is true use
  // two colors to emphasize how the sphere is constructed.

  const float delta_eta = M_PI / slices;
  const float delta_theta = delta_eta;

  // Outer (slice, eta) Loop: Iterate over longitude (north-to-south).
  // Inner (j, theta) Loop: Iterate over latitude (east-to-west)

  // Get rid of any triangles previously stored in bset_sphere.
  //
  bset_sphere.reset(pipe_sphere);
  //
  // This is wasteful in cases where we are going to re-fill bset_sphere
  // with the exact same triangles. Waste is bad.

  switch ( prim_want ) {
  case vk::PrimitiveTopology::eTriangleList: // Code below is written for this.
  case vk::PrimitiveTopology::eLineList:     // Not this.
  case vk::PrimitiveTopology::eLineStrip:    // Nor this.
    for ( int slice = 0; slice < slices; slice++ )
      {
        const float eta0 = slice * delta_eta,  eta1 = eta0 + delta_eta;
        const float y0 = cosf(eta0),           y1 = cosf(eta1);
        const float slice_r0 = sinf(eta0),     slice_r1 = sinf(eta1);

        for ( int j=0; j<=2*slices; j++ )
          {
            const float theta0 = j * delta_theta;
            const float theta1 = theta0 + delta_theta;

            bset_sphere

              /// Triangle I

              // Vertex 1
              << color
              << pNorm( slice_r1 * cosf(theta0), y1, slice_r1 * sinf(theta0) )
              << pCoor( slice_r1 * cosf(theta0), y1, slice_r1 * sinf(theta0) )

              // Vertex 2
              << color
              << pNorm( slice_r0 * cosf(theta0), y0, slice_r0 * sinf(theta0) )
              << pCoor( slice_r0 * cosf(theta0), y0, slice_r0 * sinf(theta0) )

              // Vertex 3      want
              << color
              << pNorm( slice_r1 * cosf(theta1), y1, slice_r1 * sinf(theta1) )
              << pCoor( slice_r1 * cosf(theta1), y1, slice_r1 * sinf(theta1) )

              /// Triangle II

              // Vertex 3      
              << color2
              << pNorm( slice_r1 * cosf(theta1), y1, slice_r1 * sinf(theta1) )
              << pCoor( slice_r1 * cosf(theta1), y1, slice_r1 * sinf(theta1) )

              // Vertex 2
              << color2
              << pNorm( slice_r0 * cosf(theta0), y0, slice_r0 * sinf(theta0) )
              << pCoor( slice_r0 * cosf(theta0), y0, slice_r0 * sinf(theta0) )

              // Vertex 4
              << color2
              << pNorm( slice_r0 * cosf(theta1), y0, slice_r0 * sinf(theta1) )
              << pCoor( slice_r0 * cosf(theta1), y0, slice_r0 * sinf(theta1) );
          }
      }
    break;
 case vk::PrimitiveTopology::eTriangleStrip:
    for ( int i=0; i<slices; i++ )
      {
        const float eta0 = i * delta_eta;
        const float eta1 = eta0 + delta_eta;
        const float y0 = cosf(eta0),        y1 = cosf(eta1);
        const float slice_r0 = sinf(eta0),  slice_r1 = max(0.0f,sinf(eta1));
        const float delta_theta = delta_eta;

        for ( int j=0; j <= 2*slices; j++ )
          {
            const float theta = j * delta_theta;
            const float theta1 = theta + delta_theta;

            // Vertex 0  (Used for three triangles.)
            //
            pVect n0( slice_r0 * sinf(theta1), y0, slice_r0 * cosf(theta1) );
            bset_sphere << n0 << pCoor(n0) << color;

            // Vertex 1  (Used for three triangles.)
            //
            pVect n1( slice_r1 * sinf(theta1), y1, slice_r1 * cosf(theta1) );
            bset_sphere << n1 << pCoor(n1) << color2;
          }
      }
    break;
  default: assert( false );
  }

  bset_sphere.to_dev();

  // In sphere's coordinate space the sphere center is at the origin
  // and its radius is one. Therefore we need to adjust the modelview
  // matrix so that sphere is at the value of coordinate
  // sphere_location and its radius is sphere_size.
  //
  pipe_sphere
    .ds_set( transform
             * pMatrix_Translate( sphere_location )
             * pMatrix_Scale(sphere_size) )
    .record_draw(cb, bset_sphere);

  // Render Marker for Light Source
  //
  shapes.record_tetrahedron(cb,transform,light_location,0.2);

}



void
World::cb_keyboard()
{
  const int key = vh.keyboard_key_get();
  if ( !key ) return;
  pVect adjustment(0,0,0);
  pVect user_rot_axis(0,0,0);
  const bool kb_mod_s = vh.keyboard_shift;
  const bool kb_mod_c = vh.keyboard_control;
  const float move_amt = kb_mod_s ? 2.0 : kb_mod_c ? 0.08 : 0.4;

  switch ( key ) {
  case FB_KEY_LEFT: adjustment.x = -move_amt; break;
  case FB_KEY_RIGHT: adjustment.x = move_amt; break;
  case FB_KEY_PAGE_UP: adjustment.y = move_amt; break;
  case FB_KEY_PAGE_DOWN: adjustment.y = -move_amt; break;
  case FB_KEY_DOWN: adjustment.z = move_amt; break;
  case FB_KEY_UP: adjustment.z = -move_amt; break;
  case FB_KEY_DELETE: user_rot_axis.y = 1; break;
  case FB_KEY_INSERT: user_rot_axis.y =  -1; break;
  case FB_KEY_HOME: user_rot_axis.x = 1; break;
  case FB_KEY_END: user_rot_axis.x = -1; break;
  case 'b': case 'B': opt_move_item = MI_Ball; break;
  case 'c': case 'C': opt_2_color = !opt_2_color; break;
  case 'e': case 'E': opt_move_item = MI_Eye; break;
  case 'l': case 'L': opt_move_item = MI_Light; break;
  case 'n':
    opt_primitive++;
    if ( opt_primitive == tops.size() ) opt_primitive = 0;
    break;
  case 'N':
    if ( !opt_primitive ) opt_primitive = tops.size();
    opt_primitive--;
    break;
  case FB_KEY_TAB:
    if ( !kb_mod_s ) { variable_control.switch_var_right(); break; }
  case 96: variable_control.switch_var_left(); break;
  case '-':case '_': variable_control.adjust_lower(); break;
  case '+':case '=': variable_control.adjust_higher(); break;
  default: printf("Unknown key, %d\n",key); break;
  }

  // Update eye_direction based on keyboard command.
  //
  if ( user_rot_axis.x || user_rot_axis.y )
    {
      pMatrix_Rotation rotall(eye_direction,pVect(0,0,-1));
      user_rot_axis *= invert(rotall);
      eye_direction *= pMatrix_Rotation(user_rot_axis, M_PI * 0.03);
    }

  // Update eye_location based on keyboard command.
  //
  if ( adjustment.x || adjustment.y || adjustment.z )
    {
      if ( opt_move_item != MI_Ball && opt_move_item != MI_Ball_V )
        global_transform_stale = true;
      const double angle =
        fabs(eye_direction.y) > 0.99
        ? 0 : atan2(eye_direction.x,-eye_direction.z);
      pMatrix_Rotation rotall(pVect(0,1,0),-angle);
      adjustment *= rotall;

      switch ( opt_move_item ){
      case MI_Light: light_location += adjustment; break;
      case MI_Eye: eye_location += adjustment; break;
      case MI_Ball: sphere_location += adjustment; break;
      default: break;
      }
    }
}


int
main(int argv, char **argc)
{
  pVulkan_Helper pvulkan_helper(argv,argc);
  World world(pvulkan_helper);

  world.setup_and_run();

  return 0;
}