
-- <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>


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 (reset_simulation == 1) then
  reset_simulation = 0

  gh_physx5.actor_clear_forces(px_actor_box)
  gh_physx5.actor_set_position(px_actor_box, 1.5, 3, 0)
  local vx = gxl.random(-1.0, 1.0)
  local vy = gxl.random(-1.0, 1.0)
  local vz = gxl.random(-1.0, 1.0)
  gh_physx5.actor_set_angular_velocity(px_actor_box, vx, vy, vz)
  
  gh_physx5.actor_clear_forces(px_actor_sphere)
  gh_physx5.actor_set_position(px_actor_sphere, -1.5, 3, 0)
  vx = gxl.random(-1.0, 1.0)
  vy = gxl.random(-1.0, 1.0)
  vz = gxl.random(-1.0, 1.0)
  gh_physx5.actor_set_angular_velocity(px_actor_sphere, vx, vy, vz)
end








if (physx_ok == 1) then
  -- PhysX5 physics simulation. 

  local fixed_time_step  = 1.0/60.0
  gh_physx5.run_simulation(px_scene, dt, fixed_time_step)

  -- Read back PhysX5 actors and updates 3D objects transformation.
  --gh_physx5.actor_apply_transform(px_actor_box, box)
  --gh_physx5.actor_apply_transform(px_actor_sphere, sphere)

  gh_physx5.scene_sync_3d_objects_v2(px_scene)

end  

--]]




if (imgui_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(0.3, 0.3, 0.3, 1, 1.0)



gh_gpu_program.bind(texture_prog)


gh_gpu_program.uniform4f(texture_prog, "uv_tiling", 6, 6, 0, 0)
gh_texture.bind(ground_tex, 0)
gh_object.render(ground)

gh_gpu_program.uniform4f(texture_prog, "uv_tiling", 1, 1, 0, 0)
gh_texture.bind(box_tex, 0)
gh_object.render(box)

gh_gpu_program.uniform4f(texture_prog, "uv_tiling", 2, 2, 0, 0)
gh_texture.bind(sphere_tex, 0)
gh_object.render(sphere)




  

  ----------------------------------------------------------------------------
  -- ImGui start ---------------------------------------------------------------
  --
  imgui_frame_begin()

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

  local y_offset = 0
  
  y_offset = 50 -- place for PhysX Visual Indicator

  local is_open = imgui_window_begin_no_collapse("Control panel", 300, winW-y_offset, 0, y_offset)
  if (is_open == 1) then

    imgui_hovered = gh_imgui.is_window_hovered()
    
    gh_imgui.text_rgba("PhysX 5 - SDK version: " .. physx5_ver, 1.0, 1.0, 0.0, 1.0)

    gh_imgui.spacing()
    gh_imgui.spacing()
    if (dynamic_sphere) then
      gh_imgui.text("Sphere is a mesh actor (dynamic)")
    else
      gh_imgui.text("Sphere is a mesh actor (static)")
    end

    gh_imgui.spacing()
    gh_imgui.spacing()
    gh_imgui.text("Press [ESC] to quit the demo")
   
    gh_imgui.spacing()
    gh_imgui.spacing()
    
    if (gh_imgui.button("Reset simultation", 200, 20) == 1) then
      reset_simulation = 1
    end

  end 
  imgui_window_end()


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


