Tiger Demo

Interactive demo illustrating the use of feedback to zoom the view into a selected vector shape.

Downloads

The executables require OpenGL.

Description

This demo renders a static predefined vector scene but registers a callback for the on_click event that changes the target view to the bound of the selected vector shape. The on_paint callback zooms the current view towards that of the target.

The result is a staggeringly impressive interactive demonstration of vector graphics that can be implemented in only 21 lines of OCaml code using our library. Moreover, performance far exceeds that of any other vector graphics renderers thanks to the use of advanced multiresolution techniques in the renderer.

The entire program is as follows:

open Graphics2D
let portal = {view=None; scene=Tiger.scene}

let target = ref (bound_of portal.scene)

Our library includes the definition of a useful function dt that returns a function giving the time since it was last invoked (in seconds). This can be used to incrementally-update a GUI, providing fluid animation with minimal effort:

let dt = dt()

The paint function will be registered as a handler for the on_paint event. This function zooms toward the target view:

let paint() =
  match portal.view, !target with
  | Some (ol, ou), Some (nl, nu) ->
      let p = 0.1 ** dt() in
      portal.view <-
        Some (p *| ol +| (1. -. p) *| nl, p *| ou +| (1. -. p) *| nu)
  | None, target -> portal.view <- target
  | _ -> ()

The click function will be registered as a handler for the on_click event. This function simply replaces the current target with the bound of the vector shape that was selected, or the bound of the whole scene if the background was selected.

let click = function
  | Some(i::_) -> target := bound_of(get [i] portal.scene)
  | _ -> target := bound_of portal.scene

The main program simply registers the callbacks and spawns a GUI:

let () =
  let on_paint = Event.create() in
  Event.add paint on_paint;
  let on_click = Event.create() in
  Event.add click on_click;
  GUI.spawn ~on_paint ~on_click portal

Compilation

This example program may be compiled using our library with:

$ ocamlopt -I +lablGL lablgl.cmxa lablglut.cmxa unix.cmxa graphics2D.cmxa tiger.ml -o tiger

(Note: We are not VAT registered and, therefore, cannot provide VAT receipts)

Mastercard VISA VISA Delta VISA Electron VISA Purchasing JCB Solo Switch