
local elapsed_time = gh_utils.get_elapsed_time()
local dt = gh_utils.get_time_step()


frames = frames+1
fps_time = fps_time + dt
if (fps_time >= 1.0) then
  fps_time = 0
  fps = frames
  frames = 0
end  







gh_audio.update() -- Important!!!






--gh_renderer.clear_color_depth_buffers(bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a, 1.0)
gh_renderer.clear_color_depth_stencil_buffers(0, 0, 0, 1, 1, 0)





gh_renderer.set_depth_test_state(0)

gh_camera.bind(camera_ortho)
gh_gpu_program.bind(colormap_prog)
gh_gpu_program.uniform4f(colormap_prog, "color", bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a)
gh_object.render(fullscreen_quad)








gh_renderer.set_depth_test_state(1)






camera_fov = 60.0
gh_camera.set_fov(camera, camera_fov)
keyboard_speed = 50.0
gx_camera.set_keyboard_speed(keyboard_speed)

if (imgui_hovered == false) then
  gx_camera.update(camera, dt)
end  
gh_camera.bind(camera)





   


gh_gpu_program.bind(wave_prog)
gh_gpu_program.uniform1f(wave_prog, "time", elapsed_time*4.0)
gh_gpu_program.uniform4f(wave_prog, "color", water_mesh_color.r, water_mesh_color.g, water_mesh_color.b, water_mesh_color.a)
gh_gpu_program.uniform4f(wave_prog, "wave1", wave1.dirx, wave1.diry, wave1.steepness, wave1.wavelength)
gh_gpu_program.uniform4f(wave_prog, "wave2", wave2.dirx, wave2.diry, wave2.steepness, wave2.wavelength)
gh_gpu_program.uniform4f(wave_prog, "wave3", wave3.dirx, wave3.diry, wave3.steepness, wave3.wavelength)
gh_renderer.wireframe()
gh_object.render(water_mesh)
gh_renderer.solid()


















imgui_frame_begin()

  gh_imgui.set_color(IMGUI_WINDOW_BG_COLOR, 0.1, 0.1, 0.1, 0.75)

  local is_open = imgui_window_begin_pos_size_always("Control panel", 400, winW, 0, 0)
  if (is_open == 1) then
 
    gh_imgui.text_rgba("Gerstner Waves", 1.0, 1.0, 0.0, 1.0)

    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.spacing()


    gh_imgui.text("Background color")
    bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a = gh_imgui.color_edit_rgba("##colorpicker-bgcolor", bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a)


    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.spacing()
    

    gh_imgui.text("Water mesh color")
    water_mesh_color.r, water_mesh_color.g, water_mesh_color.b, water_mesh_color.a = gh_imgui.color_edit_rgba("##colorpicker-water_mesh_color", water_mesh_color.r, water_mesh_color.g, water_mesh_color.b, water_mesh_color.a)

    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.spacing()

    

    gh_imgui.text("Wave1 parameters")
    wave1.dirx, wave1.diry = gh_imgui.slider_2f("dir##wave1_dir", wave1.dirx, wave1.diry, -1.0, 1.0, 1.0)
    wave1.steepness = gh_imgui.slider_1f("steepness##wave1_steepness", wave1.steepness, 0.0, 2.0, 1.0)
    wave1.wavelength = gh_imgui.slider_1f("wavelength##wave1_wavelength", wave1.wavelength, 1.0, 1000.0, 1.0)
   
    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.text("Wave2 parameters")
    wave2.dirx, wave2.diry = gh_imgui.slider_2f("dir##wave2_dir", wave2.dirx, wave2.diry, -1.0, 1.0, 1.0)
    wave2.steepness = gh_imgui.slider_1f("steepness##wave2_steepness", wave2.steepness, 0.0, 2.0, 1.0)
    wave2.wavelength = gh_imgui.slider_1f("wavelength##wave2_wavelength", wave2.wavelength, 1.0, 1000.0, 1.0)

    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.text("Wave3 parameters")
    wave3.dirx, wave3.diry = gh_imgui.slider_2f("dir##wave3_dir", wave3.dirx, wave3.diry, -1.0, 1.0, 1.0)
    wave3.steepness = gh_imgui.slider_1f("steepness##wave3_steepness", wave3.steepness, 0.0, 2.0, 1.0)
    wave3.wavelength = gh_imgui.slider_1f("wavelength##wave3_wavelength", wave3.wavelength, 1.0, 1000.0, 1.0)


    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.separator()
    gh_imgui.spacing()
    gh_imgui.spacing()
  
    gh_imgui.text(string.format("FPS: %.0f", fps))
    gh_imgui.text(string.format("GL_RENDERER: %s", gl_renderer))
    gh_imgui.text(string.format("GL_VERSION: %s", gl_version))
    


    imgui_hovered = false
    if (gh_imgui.is_any_window_hovered() == 1) then
      imgui_hovered = true
    end

    if (gh_imgui.is_any_item_hovered() == 1) then
      imgui_hovered = true
    end


  end 
  imgui_window_end()
  imgui_frame_end()
