
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) then
  gx_camera.update(camera, dt)
end  







if (run_sim == 1) then

  local num_new_boxes = math.floor(box_rate * dt)
  num_new_boxes_excess = num_new_boxes_excess + (box_rate * dt) - num_new_boxes
  if (num_new_boxes_excess >= 1) then
    num_new_boxes = num_new_boxes + math.floor(num_new_boxes_excess)
    num_new_boxes_excess = num_new_boxes_excess - math.floor(num_new_boxes_excess)
  end  


  while (num_new_boxes > 0) do
      
    local box = all_px_boxes[box_index]
    
    box_index = box_index+1
    if (box_index > num_boxes) then
      box_index = 1
    end
    
    box.render = 1
    gh_physx4.rb_set_actor_render_state(aaid, box_index-1, box.render)

    local x = gxl.random(-1.0, 1.0)
    local y = 6.0
    local z = gxl.random(-1.0, 1.0)
    gh_physx4.actor_set_position(box.px_actor_id, x, y, z)
    x = gxl.random(-30.0, 30.0)
    y = gxl.random(-30.0, 30.0)
    z = gxl.random(-30.0, 30.0)
    gh_physx4.actor_set_euler_angles(box.px_actor_id, x, y, z)
    gh_physx4.actor_set_linear_velocity(box.px_actor_id, 0, 0, 0)
    gh_physx4.actor_set_angular_velocity(box.px_actor_id, 0, 0, 0)
    
    num_new_boxes = num_new_boxes-1
    
    if (num_boxes_render < num_boxes) then
      num_boxes_render = num_boxes_render + 1
    end

  end

end





if ((run_sim == 1) and (can_run_sim == 1)) then
  -- PhysX4 physics simulation. 
  --gh_physx4.run_simulation_step(px_scene, dt)
  --gh_physx4.fetch_results(px_scene)

  fixed_time_step  = 1.0/60.0
  gh_physx4.run_simulation(px_scene, dt, fixed_time_step)
end  












gh_renderer.set_depth_test_state(1)
gh_renderer.clear_color_depth_buffers(0.3, 0.3, 0.3, 1, 1.0)


gh_camera.bind(camera)


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






gh_gpu_program.bind(gi_texture_prog)
gh_texture.bind(box_tex, 0)

gh_physx4.rb_instancing_apply_transform(aaid, mesh_box)

gh_mesh.instancing_set_num_instances_to_render(mesh_box, num_boxes_render)
gh_object.render(mesh_box)

  














---[[
  libfont_clear()

  local y_offset = 40
  
  libfont_print(20, y_offset, 1, 0.5, 0, 1, ">> PhysX 4 (v" .. physx4_ver .. ") <<")
  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_WINDOW_BG_COLOR, 0.1, 0.1, 0.1, 0.6)

  local is_open = imgui_window_begin_no_collapse("Control panel", 250, 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_rgba("PhysX 4 - SDK version: " .. physx4_ver, 1.0, 1.0, 0.0, 1.0)

    gh_imgui.text("Press [ESC] to quit the demo")

    imgui_vertical_space()
    imgui_vertical_space()
    imgui_vertical_space()

    gh_imgui.text("# boxes: " .. num_boxes_render)

  
   
    imgui_vertical_space()
    imgui_vertical_space()
    imgui_vertical_space()
    
    if (run_sim == 0) then
      if (gh_imgui.button("Run simultation", 200, 20) == 1) then
        run_sim = 1
      end
    else
      if (gh_imgui.button("Pause simultation", 200, 20) == 1) then
        run_sim = 0
      end
    end

  end 

  imgui_window_end()

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


