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.

Downloads

The executables require OpenGL.

Description

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 =
  Fill.radial
    (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.)]] |>
  ContourGeometry.make

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

let styles =
  [ Fill.red, Style.Interior `Odd;
    highlight, Style.Interior `Odd;
    Fill.Flat Fill.blue, 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.

Compilation

This example program may be compiled using our library with:

$ ocamlopt -I +lablGL lablgl.cmxa lablglut.cmxa unix.cmxa graphics2D.cmxa demo1.ml -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