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

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





if (reset_simulation == 1):
  reset_simulation = 0

  gh_physx4.actor_clear_forces(px_actor_box)
  gh_physx4.actor_set_position(px_actor_box, 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_physx4.actor_set_angular_velocity(px_actor_box, vx, vy, vz)
  
  gh_physx4.actor_clear_forces(px_actor_sphere)
  gh_physx4.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_physx4.actor_set_angular_velocity(px_actor_sphere, vx, vy, vz)








if (physx_ok == 1):
  # 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)


  # Read back PhysX4 actors and updates 3D objects transformation.
  gh_physx4.actor_apply_transform(px_actor_box, box)
  gh_physx4.actor_apply_transform(px_actor_sphere, sphere)







if (win_hovered == 0):
  gx_camera.update(camera, dt)

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)








libfont_clear()

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(10, y_offset, 1, 1, 0, 1, "FPS: " + str(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)

is_open = imgui_window_begin_no_collapse("Control panel", 250, 200, 10, 200)
if (is_open == 1):

  win_hovered = gh_imgui.is_window_hovered()
  
  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()
  
  if (gh_imgui.button("Reset simultation", 200, 20) == 1):
    reset_simulation = 1



imgui_window_end()
imgui_frame_end()


