Simple SlideShow Demo

Here is a simple slideshow demo. You load several images using the LoadTexture() (INIT script line 20) function and each texture is displayed during duration time (FRAME script lines 3 and 7).

This demo will be available in the next version of the code sample pack.

 

INIT script in Lua

winW, winH = gh_window.getsize()
   
font_a = gh_utils.font_create("Tahoma", 14)

-- An orthographic camera for 2D visualization
--
camera_ortho = gh_camera.create_ortho(-winW/2, winW/2, -winH/2, winH/2, 1.0, 10.0)
gh_camera.set_viewport(camera_ortho, 0, 0, winW, winH)
gh_camera.set_position(camera_ortho, 0, 0, 4)


-- The textures
--
local abs_path = 0
all_textures = {}
num_textures = 0

-- A helper function to load a texture and add it to the texture array.
--
function LoadTexture(filename)
  local tex = gh_texture.create_from_file(filename, 0, abs_path)
  if (tex > 0) then
    num_textures = num_textures + 1
    all_textures[num_textures] = tex
  end
end

-- Load all textures
--
LoadTexture("data/i01.jpg")
LoadTexture("data/i02.jpg")
LoadTexture("data/i03.jpg")
LoadTexture("data/i04.jpg")


-- The GPU program used to display a texture.
--
texture_prog = gh_node.getid("texture_prog")
gh_gpu_program.uniform1i(texture_prog, "tex0", 0)


-- The quad to display the texture.
--
mesh_quad = gh_mesh.create_quad(winW, winH)


gh_renderer.set_vsync(1)
gh_renderer.set_scissor_state(0)

local elapsed_time = gh_utils.get_elapsed_time()
last_time = elapsed_time
frame = 0
last_texture = 0

 

FRAME script in Lua

-- Each texture is displayed during "duration" seconds.
--
local duration = 1.0 -- in seconds    
    
local elapsed_time = gh_utils.get_elapsed_time()
local diff_time = elapsed_time - last_time
if (diff_time > duration) then
  last_time = elapsed_time
  frame = frame + 1
  if (frame >= num_textures) then
    frame = 0
  end
end


-- Texture rendering
--
gh_camera.bind(camera_ortho)

gh_renderer.set_depth_test_state(0)
gh_renderer.clear_color_depth_buffers(0.2, 0.2, 0.2, 1.0, 1.0)

gh_gpu_program.bind(texture_prog)

local tex = all_textures[frame+1]
-- A small test that avoind to bind the same texture every frame.
--
if (last_texture ~= tex) then 
  gh_texture.bind(tex, 0)
  last_texture = tex
end  

gh_object.set_position(mesh_quad, 0, 0, 0)
gh_object.set_euler_angles(mesh_quad, 0, 0, 0)
gh_object.render(mesh_quad)


-- Some info...
--
gh_utils.font_render(font_a, 10, 20, 1.0, 1.0, 0.0, 1.0, "GLSL Hacker - SlideShow demo")
gh_utils.font_render(font_a, 10, 40, 1.0, 1.0, 0.0, 1.0, "Image: " .. (frame+1))

 

SIZE script in Lua

winW, winH = gh_window.getsize(0)

gh_camera.update_ortho(camera_ortho, -winW/2, winW/2, -winH/2, winH/2, 1.0, 10.0)
gh_camera.set_viewport(camera_ortho, 0, 0, winW, winH)

gh_utils.font_set_viewport_info(font_a, 0, 0, winW, winH)

gh_mesh.update_quad_size(mesh_quad, winW, winH)

And to terminate, the texturing GLSL program:


	
  
	






Leave a Comment

Your email address will not be published. Required fields are marked *