Togl — a Tk OpenGL widget

Copyright (C) 1996-2002 Brian Paul and Ben Bederson


Contents


Introduction

Togl is a Tk widget for OpenGL rendering. Togl was originally based on OGLTK, written by Benjamin Bederson at the University of New Mexico. Togl adds the new features:

Togl allows one to create and manage a special Tk/OpenGL widget with Tcl and render into it with a C program. That is, a typical Togl program will have Tcl code for managing the user interface and a C program for computations and OpenGL rendering.

Togl is copyrighted by Brian Paul (brian_e_paul@yahoo.com) and Benjamin Bederson (bederson@cs.umd.edu). See the LICENSE file for details.

The Togl project and home page are hosted by SourceForge.

Prerequisites

You should have Tcl and Tk installed on your computer. Togl works with Tcl/Tk version 8.0 and up. The Mac OS X version requires version 8.4.

You must also have OpenGL or Mesa (a free alternative to OpenGL) installed on your computer.

One should be familiar with Tcl, Tk, OpenGL, and C programming to use Togl effectively.

Getting Togl

The current version of Togl is 1.7. Togl can be downloaded from SourceForge.

Mailing list

See the Togl project at SourceForge for mailing list information.

Using Togl With Your Application

There are basically two ways of using Togl with your application:

Since Togl is compiled into a shared library using the Tcl/Tk stubs-interface, the same binary can be used with any version of Tck/Tk from 8.06 and up. See README.stubs for more info.

Unix/X11 usage

Unix/X systems only need the togl.c, togl.h and the public Tcl/Tk include files.

Windows 95/NT/2000/XP usage

Windows platforms need tkWinInt.h and other internal Tk header files. So you need a Tcl/Tk source distribution in addition to the Togl distribution (or copy over the various include files).

Here's the minimal way to build Togl with Tcl/Tk using the gcc that is distributed as part of the cygwin tools (Microsoft's compilers work too):

VER=8.4.12
SRCDIR=`pwd`

cd $SRCDIR/tcl$VER/win
env 'CC=gcc -mno-cygwin' ./configure --enable-threads
make libtclstub84.a

cd $SRCDIR/tk$VER/win
env 'CC=gcc -mno-cygwin' ./configure --enable-threads
make libtkstub84.a

cd $SRCDIR/Togl
env 'CC=gcc -mno-cygwin' ./configure --with-tcl=../tcl$VER/win --with-tk=../tk$VER/win

make
The resulting Togl17.dll and pkgIndex.tcl should be installed into your Tcl distribution just like any other package.

Mac OS X usage

These special instructions are for building the Aqua version of Togl. Mac OS X needs tkMacOSXInt.h and other internal Tk header files. Unfortunately, the Tcl and Tk frameworks that Apple distributes are missing the internal headers. So you need a Tcl/Tk source distribution in addition to the Togl distribution (or copy over the various include files). You would probably want a newer version of Tcl and Tk anyway because each minor revision of 8.4 has many Aqua bug fixes.

Here's one way to build Tcl, Tk, and Togl on Mac OS X (assuming they are all in the same directory) to install in your home directory:

VER=8.4.12

mkdir -p ~/bin
make -C tcl$VER/macosx install PREFIX="${HOME}" INSTALL_PATH="${HOME}/Library/Frameworks"
make -C tk$VER/macosx install PREFIX="${HOME}" INSTALL_PATH="${HOME}/Library/Frameworks"

(cd Togl; ./configure --prefix="${HOME}")
make -C Togl install

C Togl Functions

These are the Togl functions one may call from a C program.

#include "togl.h"

For portability, you should include the togl.h header before any other OpenGL header so that various Windows 95/NT/2000/XP stuff falls into place.

Setup and Initialization Functions

int Togl_Init(Tcl_Interp *interp)
Initializes the Togl module. This is typically called from the Tk_Main() function or via Tcl's package require command.
void Togl_CreateFunc(Togl_Callback *proc)
void Togl_DisplayFunc(Togl_Callback *proc)
void Togl_ReshapeFunc(Togl_Callback *proc)
void Togl_DestroyFunc(Togl_Callback *proc)
Register C functions to be called by Tcl/Tk when a widget is realized, must be redrawn, is resized, or is destroyed respectively.

Each C callback must be of the form:

	void callback(Togl *togl)
	{
	   ...your code...
	}
void Togl_TimerFunc(Togl_Callback *proc)
Register a C timer callback function which will be called every n milliseconds. The interval n is specified by the -time option to the Togl Tcl command.

The C callback must be of the form:

	void my_timer_callback(Togl *togl)
	{
	   ...your code...
	}
void Togl_ResetDefaultCallbacks(void)
Reset all default callback pointers to NULL.
void Togl_CreateCommand(char *cmd_name, Togl_CmdProc *cmd_proc)
Used to create a new Togl sub-command. The C function which implements the command must be of the form:

	int callback(Togl *togl, int argc, char *argv[])
	{
	   ...your code...
	   return TCL_OK or TCL_ERROR;
	}

Drawing-related Commands

void Togl_PostRedisplay(Togl *togl)
Signals that the widget should be redrawn. When Tk is next idle the user's C render callback will be invoked. This is typically called from within a Togl sub-command which was registered with Togl_CreateCommand().
void Togl_SwapBuffers(const Togl *togl)
Swaps the front and back color buffers for a double-buffered widget. glFlush() is executed if the window is single-buffered. This is typically called in the rendering function which was registered with Togl_DisplayFunc().
void Togl_MakeCurrent(const Togl *togl)
Sets the current rendering context to the given widget. This is done automatically before the Togl callback functions are called. So the call is only needed if you have multiple widgets with separate OpenGL contexts. If the argument is NULL, then the rendering context is cleared and subsequent OpenGL commands will fail.

Query Functions

char *Togl_Ident(const Togl *togl)
Returns a pointer to the identification string associated with a Togl widget or NULL if there's no identifier string.
int Togl_Width(const Togl *togl)
Returns the width of the given Togl widget. Typically called in the function registered with Togl_ReshapeFunc().
int Togl_Height(const Togl *togl)
Returns the height of the given Togl widget. Typically called in the function registered with Togl_ReshapeFunc().
Tcl_Interp *Togl_Interp(const Togl *togl)
Returns the Tcl interpreter associated with the given Togl widget.
Tk_Window Togl_TkWin(const Togl *togl)
Returns the Tk window associated with the given Togl widget.

Color Index Mode Functions

These functions are only used for color index mode.

unsigned long Togl_AllocColor(Togl *togl, float red, float green, float blue)
Allocate a color from a read-only colormap. Given a color specified by red, green, and blue return a colormap index (aka pixel value) whose entry most closely matches the red, green, blue color. Red, green, and blue are values in [0,1]. This function is only used in color index mode when the -privatecmap option is false.
void Togl_FreeColor(Togl *togl, unsigned long index)
Free a color in a read-only colormap. Index is a value which was returned by the Togl_AllocColor() function. This function is only used in color index mode when the -privatecmap option is false.
void Togl_SetColor(Togl *togl, int index, float red, float green, float blue)
Load the colormap entry specified by index with the given red, green and blue values. Red, green, and blue are values in [0,1]. This function is only used in color index mode when the -privatecmap option is true.

Font Functions

GLuint Togl_LoadBitmapFont(Togl *togl, const char *fontname)
Load the named font as a set of glBitmap display lists. fontname may be one of Zero is returned if this function fails.
After Togl_LoadBitmapFont() has been called, returning fontbase, you can render a string s with:
glListBase(fontbase);
glCallLists(strlen(s), GL_BYTE, s);
To maximize the portability of your application it is best to use one of the predefined TOGL_BITMAP_* fonts.
void Togl_UnloadBitmapFont(Togl *togl, GLuint fontbase)
Destroys the bitmap display lists created by by Togl_LoadBitmapFont().

Client Data Functions

void Togl_SetClientData(Togl *togl, ClientData clientData)
clientData is a pointer to an arbitrary user data structure. Each Togl struct has such a pointer. This function sets the Togl widget's client data pointer.
ClientData Togl_GetClientData(const Togl *togl)
clientData is a pointer to an arbitrary user data structure. Each Togl struct has such a pointer. This function returns the Togl widget's client data pointer.
void Togl_ClientData(ClientData clientData)
clientData is a pointer to an arbitrary user data structure. Set default client data pointer for subsequent new Togl widgets. Default value is NULL.

Overlay Functions

These functions are modelled after GLUT's overlay sub-API.

void Togl_UseLayer(Togl *togl, int layer)
Select the layer into which subsequent OpenGL rendering will be directed. layer may be either TOGL_OVERLAY or TOGL_NORMAL.
void Togl_ShowOverlay(Togl *togl)
Display the overlay planes, if any.
void Togl_HideOverlay(Togl *togl)
Hide the overlay planes, if any.
void Togl_PostOverlayRedisplay(Togl *togl)
Signal that the overlay planes should be redraw. When Tk is next idle the user's C overlay display callback will be invoked. This is typically called from within a Togl sub-command which was registered with Togl_CreateCommand().
void Togl_OverlayDisplayFunc(Togl_Callback *proc)
Registers the C callback function which should be called to redraw the overlay planes. This is the function which will be called in response to Togl_PostOverlayRedisplay(). The callback must be of the form:

	void RedrawOverlay(Togl *togl)
	{
	   ...your code...
	}
int Togl_ExistsOverlay(Togl *togl)
Returns 1 if overlay planes exist, 0 otherwise.
int Togl_GetOverlayTransparentValue(const Togl *togl)
Returns the color index of the overlay's transparent pixel value.
int Togl_IsMappedOverlay(const Togl *togl)
Returns 1 if the overlay planes are currently displayed, 0 otherwise.
unsigned long Togl_AllocColorOverlay(const Togl *togl, float red, float green, float blue)
Allocate a color in the overlay planes. Red, green, and blue are values in [0,1]. Return the color index or -1 if the allocation fails.
void Togl_FreeColorOverlay(const Togl *togl, unsigned long index)
Free a color which was allocated with Togl_AllocColorOverlay().

X11-only Functions

These functions are only implemented on systems using the X Window System. We recommend that you avoid using these functions in your application since they are not portable to other operating/window systems (use Togl_TkWin() and normal Tk functions instead).

Display *Togl_Display(const Togl *togl)
Returns the X Display of a Togl widget.
Screen *Togl_Screen(const Togl *togl)
Returns the X Screen of a Togl widget.
int Togl_ScreenNumber(const Togl *togl)
Returns the X screen number of a Togl widget.
Colormap Togl_Colormap(const Togl *togl)
Returns the X Colormap used by a Togl widget.

Postscript Output

int Togl_DumpToEpsFile(const Togl *togl, const char *filename, int rgbFlag, void (*user_redraw)())
Generate an encapsulated Postscript file of the image in a Togl widget. filename is the name of the file to generate. If rgbFlag is non-zero then an RGB image file is written, else a grayscale image file is written. user_redraw is a pointer to the function which will render the desired image. This will typically be the same as the function passed to Togl_DisplayFunc().

Tcl Togl commands

These are the Togl commands one may call from a Tcl program.

togl pathName [options]
Creates a new togl widget with name pathName and an optional list of configuration options. Options include:

Option Default Comments
-width 400 Width of widget in pixels.
-height 400 Height of widget in pixels.
 
-ident "" A user identification string. This is used match widgets for the -sharecontext and the -sharelist options (see below). This is also useful in your callback functions to determine which Togl widget is the caller.
 
-rgba true If true, use RGB(A) mode, otherwise use Color Index mode.
-redsize 1 Minimum number of bits in red component.
-greensize 1 Minimum number of bits in green component.
-bluesize 1 Minimum number of bits in blue component.
-alpha 1 If true and -rgba is true, request an alpha channel.
-alphasize 1 Minimum number of bits in alpha component.
 
-double false If true, request a double-buffered window, otherwise request a single-buffered window.
 
-depth false If true, request a depth buffer.
-depthsize 1 Minimum number of bits in depth buffer.
 
-accum false If true, request an accumulation buffer.
-accumredsize 1 Minimum number of bits in accumulation buffer red component.
-accumgreensize 1 Minimum number of bits in accumulation buffer green component.
-accumbluesize 1 Minimum number of bits in accumulation buffer blue component.
-accumalphasize 1 Minimum number of bits in accumulation buffer alpha component.
 
-stencil false If true, request a stencil buffer.
-stencilsize 1 Minimum number of bits in stencil component.
 
-auxbuffers 0 Desired number of auxiliary buffers.
 
-privatecmap false Only applicable in color index mode. If false, use a shared read-only colormap. If true, use a private read/write colormap.
 
-overlay false If true, request overlay planes.
 
-stereo false If true, request a stereo-capable window.
-oldstereo false On SGI workstations only: if true, request divided-screen stereo.
 
-time 1 Specifies the interval, in milliseconds, for calling the C timer callback function which was registered with Togl_TimerFunc.
 
-sharelist "" Name of an existing Togl widget with which to share display lists.
-sharecontext "" Name of an existing Togl widget with which to share the OpenGL context. NOTE: most other attributes such as double buffering, RGBA vs CI, ancillary buffer specs, etc are then ignored.
 
-indirect false If present, request an indirect rendering context. A direct rendering context is normally requested. Only significant on Unix/X11.
 
-cursor "" Set the cursor in the widget window.
 
-pixelformat 0 Set the pixel format to the (platform-dependent) given value.

pathName configure
Returns all configuration records for the named togl widget.
pathName configure -option
Returns configuration information for the specified option which may be one of:
-width
Returns the width configuration of the widget in the form:
-width width Width W w
where W is the default width in pixels and w is the current width in pixels
-height
Returns the height configuration of the widget in the form:
-height height Height H h
where H is the default height in pixels and h is the current height in pixels
-extensions
Returns a list of OpenGL extensions available. For example: GL_EXT_polygon_offset GL_EXT_vertex_array
pathName configure -option value
Reconfigure a Togl widget. option may be any one of the options listed in the togl command above.
pathName render
Causes the render callback function to be called for pathName.
pathName swapbuffers
Causes front/back buffers to be swapped if in double buffer mode. And flushes the OpenGL command buffer if in single buffer mode. (So this is appropriate to call after every frame is drawn.)
pathName makecurrent
Make the widget specified by pathName and its OpenGL context the current ones.

Demo Programs

There are six demo programs:

double.tcl — compares single vs double buffering with two Togl widgets
texture.tcl — lets you play with texture mapping options
index.tcl — demo of using color index mode
overlay.tcl — example of using overlay planes (requires overlay hardware)
stereo.tcl — stereo example
gears.tcl — spinning gears demo

To compile the demos, edit the Makefile to suit your system, then type make demos. The demos are compiled into shared libraries, that can are loaded into the Tcl interpreter as Tcl/Tk-extensions. Demos are started by running the corresponding Tcl script. To run a demo just type ./double.tcl or ./texture.tcl etc.

Stereo Rendering

Quad-buffered stereo-in-a-window is supported. Quad-buffer stereo is only available on workstation-class graphics cards (3Dlabs Wildcat series, ATI FireGL series, NVidia Quadro series, and SGI workstations). Legacy support for divided-screen stereo on SGI workstations is available via the -oldstereo option. Developers for SGI workstations might also like the autostereo package to automatically switch the display in and out of stereo (other systems already do it automatically).

Full-screen stereo that gaming graphics cards support (ATI Radeon, NVidia GeForce) is not supported.

Common Questions and Problems

If you have something to add to this section please let us know.

Bad Match X errors on Sun systems

There's a bug in Sun's XmuLookupStandardColormap X library function. If you compile togl.c with the SOLARIS_BUG symbol defined (-DSOLARIS_BUG) this function call will be omitted.

Reporting Bugs

There is a bug database on the Togl Project Page. You may also discuss bugs on the mailing list.

When reporting bugs please provide as much information as possible. Also, it's very helpful to us if you can provide an example program which demonstrates the problem.

Version History

Version 1.0 — March, 1996

Version 1.1 (never officially released)

Version 1.2 — November, 1996

Version 1.3 — May 2, 1997

Version 1.4 — September 17, 1997

Version 1.5 — September 18, 1998

Version 1.6 — May 7, 2003

Version 1.7 — Jan 2006

Version 2.0 — ??? 2006


Future plans


Contributors

Several people have contributed new features to Togl. Among them are:

Many others have contributed bug fixes. Thanks for your contributions!

Last edited on 25 October 2005 by Greg Couch.