
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  




camera_fov = 50.0
gh_camera.set_fov(camera, camera_fov)

if ((win_hovered == 0) and (item_hovered == 0)) then
  gx_camera.update(camera, dt)
end  
gh_camera.bind(camera)


gh_renderer.set_depth_test_state(1)
gh_renderer.clear_color_depth_buffers(bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a, 1.0)


gh_gpu_program.bind(matcap_prog)
--gh_gpu_program.bind(matcap2_prog)
--gh_gpu_program.uniform4f(matcap_prog, "uvtiling", 2, 1, 0, 0)
--local cx, cy, cz = gh_camera.get_position(camera)
--gh_gpu_program.uniform4f(matcap_prog, "camera_pos", cx, cy, cz, 1.0)

gh_texture.bind(matcap01, 0)

gh_object.set_position(mesh1, 9, -4, 0)
gh_object.set_euler_angles(mesh1, 20, 0, 0)
gh_object.render(mesh1)

gh_object.set_position(mesh2, -8, 0, 0)
gh_object.set_euler_angles(mesh2, 0, 0, 0)
gh_object.render(mesh2)


gh_object.set_scale(mesh3, 4.0, 4.0, 4.0)
gh_object.set_position(mesh3, 9, 0.5, 0.0)
--gh_object.set_euler_angles(mesh3, -90, 0, 0)
gh_object.set_euler_angles(mesh3, 0, 0, 0)
gh_object.render(mesh3)



gh_object.set_scale(model_brainstem, 10.0, 10.0, 10.0)
gh_object.set_position(model_brainstem, 0, -8, 0.0)
gh_object.set_euler_angles(model_brainstem, 0, 0, 0)
gh_object.render(model_brainstem)






--[[
libfont_clear()

local y_offset = 40

libfont_print(20, y_offset, 1, 0.5, 0, 1, "3D template")
y_offset = y_offset + 20

y_offset = y_offset + 20
libfont_print(20, y_offset, 1, 1, 0, 1, "FPS: " .. fps)
y_offset = y_offset + 20

y_offset = y_offset + 20
libfont_print(20, y_offset, 1, 1, 0, 1, "GL_RENDERER")
y_offset = y_offset + 20
libfont_print(20, y_offset, 1, 1, 1, 1, "> " .. gl_renderer)
y_offset = y_offset + 20

libfont_print(20, y_offset, 1, 1, 0, 1, "GL_VERSION")
y_offset = y_offset + 20
libfont_print(20, y_offset, 1, 1, 1, 1, "> " .. gl_version)
y_offset = y_offset + 20


libfont_render()
--]]











  imgui_frame_begin()

  --gh_imgui.set_color(IMGUI_TITLE_BG_COLOR, 0.4, 0.4, 0.4, 0.90)

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

  local is_open = imgui_window_begin_pos_size_always("Control panel", 300, winH, 0, 0)
  if (is_open == 1) then
  
 
    
    
    ---[[
    gh_imgui.text("Drag and drop a matcap file.")

    gh_imgui.spacing()
    gh_imgui.spacing()

    gh_imgui.text("Matcap:")
    gh_imgui.text_wrapped(matcap_filename)


    gh_imgui.dummy(10, 150)
    gh_imgui.spacing()
    gh_imgui.separator()
    gh_imgui.spacing()

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



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

    gh_imgui.text("GL_RENDERER: " .. gl_renderer)
    gh_imgui.text("GL_VERSION: " .. gl_version)


    --[[
    gh_imgui.text("Simulation speed factor")
    local min_value = 1.0
    local max_value = 200.0
    local power = 1.0
    g_simulation_speed_factor = gh_imgui.slider_1f("##simspeed", g_simulation_speed_factor,   min_value, max_value,   power)

    imgui_vertical_space()
    imgui_vertical_space()
    --]]

    
    --[[
    
    imgui_vertical_space()
    imgui_vertical_space()

    local dummy_a = 1.0
    

    imgui_vertical_space()
    imgui_vertical_space()

    gh_imgui.text("Box fill color")
    box_color.r, box_color.g, box_color.b, dummy_a = gh_imgui.color_edit_rgba("##colorpicker-fill-box", box_color.r, box_color.g, box_color.b, dummy_a)
    
    gh_imgui.text("Ground fill color")
    ground_color.r, ground_color.g, ground_color.b, dummy_a = gh_imgui.color_edit_rgba("##colorpicker-fill-ground", ground_color.r, ground_color.g, ground_color.b, dummy_a)
    
    

    gh_imgui.text("vX")
    local min_value = 1.0
    local max_value = 100.0
    local power = 1.0
    vX = gh_imgui.slider_1f("##vX", vX,   min_value, max_value,   power)

    imgui_vertical_space()


    --]]


    win_hovered = 0
    if (gh_imgui.is_any_window_hovered() == 1) then
      win_hovered = 1
    end
    --gh_imgui.text("win_hovered => " .. win_hovered)
   

    item_hovered = 0
    if (gh_imgui.is_any_item_hovered() == 1) then
      item_hovered = 1
    end
    --gh_imgui.text("item_hovered => " .. item_hovered)
  



  else
    win_hovered = 0
    item_hovered = 0

  end 

  imgui_window_end()

  imgui_frame_end()
  --
  -- ImGui end ---------------------------------------------------------------
  ----------------------------------------------------------------------------
  --]]


  if (is_open == 1) then
    gh_renderer.set_depth_test_state(0)
    gh_camera.bind(camera_ortho)
    gh_texture.bind(matcap01, 0)
    gh_gpu_program.bind(tex_prog)
    gh_object.set_position(matcap_quad, -winW/2 + 150, winH/2-160, 0)
    gh_object.render(matcap_quad)
  end  