<GeeXLab HowTo/>

Code Snippets



Code snippets (Lua or Python) for GeeXLab demos.

  1. How to create and resize a 3D camera
  2. How to create and resize a 2D camera
  3. How to create a reference grid
  4. How to load a 2D texture
  5. How to load a 2D texture with ImageMagick plugin
  6. How to load a 3D model with ASSIMP plugin
  7. How to create and use an orbit 3D camera
  8. How to generate a random number
  9. How to do color blending
  10. How to do create a GPU program from Lua buffers
  11. How to set the vertices color of a quad
  12. How to rotate an object with quaternions
  13. How to create and use a render target



How to create and resize a 3D camera

Language: Lua


INIT script:
local aspect = winW / winH
fov = 60.0
znear = 1.0
zfar = 1000.0
camera = gh_camera.create_persp(fov, aspect, znear, zfar)
gh_camera.set_viewport(camera, 0, 0, winW, winH)
gh_camera.set_position(camera, 0, 50, 150)
gh_camera.setlookat(camera, 0, 0, 0, 1)
gh_camera.setupvec(camera, 0, 1, 0, 0)
SIZE script:
winW, winH = gh_window.getsize(0)
local aspect = 1.333
if (winH > 0) then
  aspect = winW / winH
end  
gh_camera.update_persp(camera, fov, aspect, znear, zfar)
gh_camera.set_viewport(camera, 0, 0, winW, winH)


How to create and resize a 2D camera

Language: Lua


INIT script:
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)
SIZE script:
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)


How to create a reference grid

Language: Lua


grid = gh_utils.grid_create()
gh_utils.grid_set_geometry_params(grid, 100, 100, 25, 25)
gh_utils.grid_set_lines_color(grid, 0.7, 0.7, 0.7, 1.0)
gh_utils.grid_set_main_lines_color(grid, 1.0, 1.0, 0.0, 1.0)
gh_utils.grid_set_main_x_axis_color(grid, 1.0, 0.0, 0.0, 1.0)
gh_utils.grid_set_main_z_axis_color(grid, 0.0, 0.0, 1.0, 1.0)
local display_main_lines = 1
local display_lines = 1
gh_utils.grid_set_display_lines_options(grid, display_main_lines, display_lines)


How to load a 2D texture

Language: Lua


local demo_dir = gh_utils.get_demo_dir()
local filename = demo_dir .. "textures/image.jpg"
local PF_U8_RGB = 1
local PF_U8_RGBA = 3
local PF_F32_RGBA = 6
local pixel_format = PF_U8_RGBA
local gen_mipmaps = 1
local compressed_texture = 0
tex = gh_texture.create_from_file_v6(filename, pixel_format, gen_mipmaps, compressed_texture)


How to load a 2D texture with ImageMagick plugin

Language: Lua


local demo_dir = gh_utils.get_demo_dir()
local filename = demo_dir .. "textures/image.jpg"
local PF_U8_RGB = 1
local PF_U8_RGBA = 3
local PF_F32_RGBA = 6
local pixel_format = PF_U8_RGBA
local gen_mipmaps = 1
local compressed_texture = 0
local free_cpu_memory = 1
local upload_to_gpu = 1
tex = gh_imagemagick.texture_create_from_file(filename, pixel_format, gen_mipmaps, free_cpu_memory, upload_to_gpu)


How to load a 3D model with ASSIMP plugin

Language: Lua


local demo_dir = gh_utils.get_demo_dir()
local model_directory = demo_dir .. "/models/" 
local resource_directory = demo_dir .. "/models/" 
local model_filename = "Space Ship.3DS"
num_vertices_model = 0
num_faces_model = 0
model = gh_model.create_from_file_loader_assimp_v2(model_filename, model_directory, resource_directory, "")
if (model > 0) then
  num_vertices_model = gh_object.get_num_vertices_v2(model)
  num_faces_model = gh_object.get_num_faces_v2(model)
end  


How to create and use an orbit 3D camera

Language: Lua


INIT script:
local lib_dir = gh_utils.get_scripting_libs_dir()     
dofile(lib_dir .. "lua/gx_cam_lib_v1.lua")
winW, winH = gh_window.getsize(0)
local camera_lookat_x = 0
local camera_lookat_y = 0
local camera_lookat_z = 0
fov = 60.0
znear = 1.0
zfar = 1000.0
x = 0
y = 0
camera = gx_camera.create_perspective(fov, 1, x, y, winW, winH, znear, zfar)
gh_camera.set_position(camera, 0, 0, 80.0)
gx_camera.init_orientation(camera, camera_lookat_x, camera_lookat_y, camera_lookat_z, 20, 90)
gx_camera.set_mode_orbit()
gx_camera.set_keyboard_speed(4.0)
FRAME script:
local dt = gh_utils.get_time_step()
gx_camera.update(camera, dt)
gh_camera.bind(camera)
SIZE script:
winW, winH = gh_window.getsize(0)
gx_camera.update_perspective(camera, fov, 1, x, y, winW, winH, znear, zfar)


How to generate a random number

Language: Lua


function random(a, b)
	if (a > b) then
		local c = b
		b = a
		a = c
	end
	local delta = b-a
	return (a + math.random()*delta)
end


How to do color blending

Language: Lua


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

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

-- Render objects here

gh_renderer.set_blending_state(0)


How to do create a GPU program from Lua buffers

Language: Lua


In many GeeXLab demos, GPU programs are defined in the XML file using the gpu_program XML element. But it's also possible to create a GPU program in a Lua or Python script. Here is how to create a simple OpenGL 2.0 GPU program in Lua:
local vs="\
#version 120\
void main()\
{\
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\
	gl_TexCoord[0] = gl_MultiTexCoord0;\
}"

local ps="\
#version 120\
uniform sampler2D tex0;\
void main(void)\
{\
  vec2 uv = gl_TexCoord[0].xy;
  uv.y *= -1.; // Y inversion 
  gl_FragColor = texture2D(tex0, uv);
}"

prog = gh_gpu_program.create_v2("texture_prog", vs, ps)


How to set the vertices color of a quad

Language: Lua


quad = gh_mesh.create_quad(10, 10)
gh_mesh.set_vertex_color(quad, 0, 1.0, 0.0, 0.0, 1.0) -- bottom left
gh_mesh.set_vertex_color(quad, 1, 0.0, 1.0, 0.0, 1.0) -- top left
gh_mesh.set_vertex_color(quad, 2, 0.0, 0.1, 1.0, 1.0) -- top right
gh_mesh.set_vertex_color(quad, 3, 1.0, 1.0, 1.0, 1.0) -- bottom right


How to rotate an object with quaternions

Language: Lua


local lib_dir = gh_utils.get_scripting_libs_dir() 		
dofile(lib_dir .. "lua/quaternion.lua")
qRx = gx_quaternion.new()
qRy = gx_quaternion.new()
qRz = gx_quaternion.new()
qR = gx_quaternion.new()

local angx = 90
local angy = 10
local angz = 90
gx_quaternion.from_angle_axis_v2(qRx, angx, 1, 0, 0)
gx_quaternion.from_angle_axis_v2(qRy, angy, 0, 1, 0)
gx_quaternion.from_angle_axis_v2(qRz, angz, 0, 0, 1)
gx_quaternion.mul2(qRx, qRy, qR)
gx_quaternion.mul2(qR, qRz, qR)
gx_quaternion.normalise(qR)

gh_object.set_orientation(torus, qR.x, qR.y, qR.z, qR.w)


How to create and use a render target

Language: Lua


INIT script:
winW, winH = gh_window.getsize(0)
local PF_U8_RGB = 1
local PF_U8_RGBA = 3
local PF_F32_RGBA = 6
local num_color_targets = 1
local pf = PF_U8_RGBA
local linear_filtering = 1
local clamp_addressing = 1
local samples = 0
local create_depth_texture = 1
rt01 = gh_render_target.create_ex_v4(winW, winH, num_color_targets, pf, linear_filtering, clamp_addressing, samples, create_depth_texture);
FRAME script:
gh_render_target.bind(rt01)
--
-- render scene
--
gh_render_target.unbind(rt01)


-- binds the first color texture of the 
-- render target on the texture unit 0
local texture_unit = 0
local color_target_index = 0
gh_texture.rt_color_bind_v2(rt01, color_target_index, texture_unit)

-- binds the depth texture of the 
-- render target on the texture unit 1
texture_unit = 1
gh_texture.rt_depth_bind(rt01, texture_unit)


gh_gpu_program.bind(postfx_prog)
...
SIZE script:
winW, winH = gh_window.getsize(0)
gh_render_target.resize(rt01, winW, winH)



Last update: 2018.04.04 @ 08:09am




GeeXLab Rootard Guide | Downloads | Contact