Code snippets (Lua or Python) for GeeXLab demos.
Language: Lua
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)
Language: Lua
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)
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)
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)
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)
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
Language: Lua
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)
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
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)
Language: 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)
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
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)
Language: Lua
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)