[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.2 Simple viewer

Module: gl.simple.viewer

This module packages common operations to allow users to view 3D objects, controlling the camera by the mouse, and have some interactions with the keyboard.

A typical way of using the viewer is like the following:

 
(use gl)
(use gl.glut)
(use gl.simple.viewer)

(define (main args)
  (glut-init args)
  (simple-viewer-display <your-display-thunk>)
  (simple-viewer-set-key! <key> <handler> ...)
  (simple-viewer-window :title "test")
  (simple-viewer-run) ; loop forever.  type ESC to exit.
  0)

The viewer handles mouse drag (to move the camera), and also draws reference grid and axes by default for your convenience. You have to provide a thunk, which must draw your 3D object.

For the keyboard events, you can use a convenient API to associate handler to the key (character for normal keys, and constants like GL_LEFT_ARROW for special keys).

The reshape event is handled implicitly, though you can override it.

If you call simple-viewer-run, it enters the event loop and never returns. If you wish to keep REPL and/or other parts of your application run concurrently, the convenient way is to run simple-viewer-run in a separate thread.

 
(use gauche.threads)

(define (run-viewer)
  (thread-start! (make-thread simple-viewer-run #f)))

See also the code under ‘examples/simple’ directory of the source tree for more examples.

Function: simple-viewer-display &optional display-thunk

Gets/sets the display thunk, which is called every time the GL window is redrawn. You can change the display thunk any time, even while the viewer thread is running.

If no argument is given, returns the current display thunk. It can be #f if no display thunk is set.

When the display thunk is called, the matrix mode is MODELVIEW and the camera transformation is already applied. The grid and axes are also drawn, unless you've customized them.

In the display thunk you can just write your model in the world coordinate system. It is guaranteed that the current color is white and line width is 1.0, but the states of other GL contexts are undefined, so you have to set them explicitly.

Function: simple-viewer-reshape &optional reshape-proc

Gets/sets the reshape procedure which is called every time the GL window configuration is changed. (It is also called when the GL window is shown first time.) You can change the reshape procedure any time, even while the viewer thread is running.

If no argument is given, returns the current reshape proc.

A reshape procedure is called with two arguments, the width and the height (in pixels) of the new GL window configuration. By default, gl.simple.viewer sets a procedure that changes viewport and projection matrix apopropriately; you need to change it only if you want a different behavior.

Function: simple-viewer-grid &optional grid-proc
Function: simple-viewer-axis &optional axis-proc

Gets/sets a procedure to draw a grid and axes. You can change these procedures any time, even while the viewer thread is running.

The grid-proc and axis-proc are called with no arguments every time the GL window is redrawn, before the display thunk is invoked. The matrix mode is MODELVIEW, the camera transformation is already applied, and lighting is disabled.

The default grid proc draws 10x10 grid on X-Z plane centered at the origin. The default axis proc draws a red line from origin to +X, a green line from origin to +Y, and a blue line from origin to +Z.

You can pass #f to disable grid and/or axis display.

If no argument is given, returns the current grid/axis proc, respectively.

Function: simple-viewer-set-key! key handler …

Even number of arguments must be given; the first of every two specifies the key, and the next one specifies the action when the key is pressed.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Shiro Kawai on June, 7 2008 using texi2html 1.78.