Minimal Demo

Minimal demo illustrating a shape composed of lines and Bezier curves with a red interior fill, a gradient fill giving a highlight and a blue stroke with rounded line joins.


The executables require OpenGL.


The entire program is only 20 lines of code. Moreover, performance is excellent from both native-code and interpreted byte-code because much of the time is spent in OpenGL and GLU.

All definitions related to 2D vector graphics are in the Graphics2D module of our library:

open Graphics2D

Define a radial highlight that interpolates from white to transparent:

let highlight =
    (vec2 0.4 0.5) (vec2 0.6 0.7) (vec2 0.1 0.8) Fill.white Fill.clear

Define a closed geometry from two Bezier curves and two straight lines:

let geo =
  [Contour.Closed, [curve_to (-0.8) 0. (-0.8) 0. (-1.) 1.; line_to 1. 1.;
                    curve_to 0.8 0. 0.8 0. 1. (-1.); line_to (-1.) (-1.)]] |>

Define the styles for our shape, starting with a red interior fill, then the white highlight and finally a blue stroke:

let styles =
  [, Style.Interior `Odd;
    highlight, Style.Interior `Odd;
    Fill.Flat, Style.stroke ~joins:`Round 0.2 ]

A "portal" defines the visible area and scene:

let portal =
  {view = Some(vec2 (-1.6) (-1.6), vec2 1.6 1.6); scene = Group [||]}
let paint() = portal.scene <- Transform(rot(time()), Shape(styles, geo))

The view and scene fields of a portal are mutable and may be updated at any time, affecting the next redraw.

Finally, the main program registers our paint function as a callback for the on_paint event and spawns the GUI.

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

This program demonstrates the construction and visualization of a simple scene graph composed of a transform and a vector graphic shape. The scene is updated as a function of time every time the on_paint event is invoked, allowing animations to be represented quickly and easily.


This example program may be compiled using our library with:

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

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

Mastercard VISA VISA Delta VISA Electron VISA Purchasing JCB Solo Switch