
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  


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




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




if (show_plane == 1) then
  gh_renderer.wireframe()
  gh_gpu_program.bind(color_prog)
  gh_gpu_program.uniform4f(color_prog, "color", plane_color.r, plane_color.g, plane_color.b, plane_color.a)
  gh_object.render(mesh_plane)
  gh_renderer.solid()
end










--local BLEND_FACTOR_ZERO = 0
local BLEND_FACTOR_ONE = 1
--local BLEND_FACTOR_SRC_ALPHA = 2
--local BLEND_FACTOR_ONE_MINUS_DST_ALPHA = 3
--local BLEND_FACTOR_ONE_MINUS_DST_COLOR = 4
--local BLEND_FACTOR_ONE_MINUS_SRC_ALPHA = 5
--local BLEND_FACTOR_DST_COLOR = 6
--local BLEND_FACTOR_DST_ALPHA = 7
--local BLEND_FACTOR_SRC_COLOR = 8
--local BLEND_FACTOR_ONE_MINUS_SRC_COLOR = 9

gh_renderer.set_blending_state(1)
gh_renderer.set_blending_factors(1, 1)

gh_renderer.set_depth_test_state(0)


gh_gpu_program.bind(particle_prog)

gh_texture.bind(particle_tex, 0)


gh_gpu_program.uniform1f(particle_prog, "particle_size", particle_size)
gh_object.set_euler_angles(vp, elapsed_time*3 * speed_factor, elapsed_time*5 * speed_factor, elapsed_time*7 * speed_factor)
gh_vertex_pool.render(vp, 0, num_particles)

gh_renderer.set_depth_test_state(1)
gh_renderer.set_blending_state(0)






--gx_camera.draw_ref_grid(20, 20, 20, 20)
gx_camera.draw_tripod(camera)






---[[
libfont_clear()

local y_offset = 40

libfont_print(20, y_offset, 1, 0.5, 0, 1, string.format("Vertex pool (%d particles)", num_particles))
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.6)


  local pos_size_flag_always = 1 -- Always set the pos and/or size
  local pos_size_flag_once = 2 -- Set the pos and/or size once per runtime session (only the first call with succeed)
  local pos_size_flag_first_use_ever = 4  -- Set the pos and/or size if the window has no saved data (if doesn't exist in the .ini file)
  local pos_size_flag_appearing = 8  -- Set the pos and/or size if the window is appearing after being hidden/inactive (or the first time)

  local window_flags = ImGuiWindowFlags_NoResize

  --local is_open = gh_imgui.window_begin("Control panel", 300, 200, 10, 200, window_flags, pos_size_flag_once, pos_size_flag_once)
  local is_open = imgui_window_begin_v1("Control panel", 300, 200, 200, 20)
  --local is_open = imgui_window_begin_pos_size_always("Control panel", 360, 200, 10, 200)
  if (is_open == 1) then
  
    

    win_hovered = gh_imgui.is_window_hovered()
            
  
    local window_w = gh_imgui.get_content_region_available_width()

    local widget_width = window_w * 1.0
    
    
    gh_imgui.text("Press [ESC] to quit the demo")
    
    ---[[

    gh_imgui.spacing()
    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.text("Plane color")
    plane_color.r, plane_color.g, plane_color.b, plane_color.a = gh_imgui.color_edit_rgba("##planecolor", plane_color.r, plane_color.g, plane_color.b, plane_color.a)


    show_plane = gh_imgui.checkbox("show plane", show_plane)

    --[[
    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.spacing()
    gh_imgui.spacing()
    

    gh_imgui.text("particle_size")
    local min_value = 1.0
    local max_value = 200.0
    local power = 1.0
    particle_size = gh_imgui.slider_1f("##particle_size", particle_size,   min_value, max_value,   power)
    
    gh_imgui.spacing()
    gh_imgui.spacing()

    gh_imgui.text("rotation speed_factor")
    min_value = 1.0
    max_value = 10.0
    speed_factor = gh_imgui.slider_1f("##speed_factor", speed_factor,   min_value, max_value,   power)

    gh_imgui.spacing()






    win_hovered = 0
    if (gh_imgui.is_any_window_hovered() == 1) then
      win_hovered = 1
    end

    item_hovered = 0
    if (gh_imgui.is_any_item_hovered() == 1) then
      item_hovered = 1
    end
  

  end 

  imgui_window_end()

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