# Togl - a Tk OpenGL widget

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

## Contents

- Introduction
- Prerequisites
- Getting Togl
- Mailing List
- Using Togl with your Application
- C Togl Functions
- Tcl Togl Functions
- Demo Programs
- Stereo Rendering
- Common Questions and Problems
- Reporting Bugs
- Version History
- Contributors

## 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:

- color-index mode support including color allocation functions
- support for requesting stencil, accumulation, alpha buffers, etc
- multiple OpenGL drawing widgets
- OpenGL extension testing from Tcl
- simple, portable font support
- overlay plane support

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 ver. 8.0 and up.

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.6. 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:

- Link or "compile in" Togl with your executable or shared library. In this case you must call Togl_Init() from your C code to initialize Togl. This is the way the included Togl examples are built.
- Compile Togl into a shared library of it's own. In this case, you will typically load the Togl shared library into wish and create a blank Togl widget, then call functions in your application code (also a compiled into a shared library and loaded into wish) to do the OpenGL rendering. In this way you can manage redraws and buffer swapping from the Tcl level.

If you compile Togl into a shared library using the Tcl/Tk stubs-interface (use compile flags `-DUSE_TCL_STUBS -DUSE_TK_STUBS` and link with `-ltclstub8.x -ltkstub8.x`, this is the default) then 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 usage

Windows 95/NT systems also need `tkPlatDecls.h`. This is a public file but due to an installation bug it's is missing from the include directory of Tcl/Tk distributions earlier than 8.4. This file is included with Togl.

Also, you'll have to copy the `tkInt.8.0.h` file to `tkInt.h`.

### Macintosh usage

XXX need info

## C Togl Functions

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

`#include "togl.h"`

You should include the togl.h header before any others so that various WindowsNT/95 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() callback function.

`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( struct 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( struct 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( struct Togl *togl, int argc, char *argv[] )
      	{
      	   ...your code...
      	   return TCL_OK or TCL_ERROR;
      	}

### Drawing-related Commands

`void Togl_PostRedisplay( struct 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( struct 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().

### Query Functions

`char *Togl_Ident( struct 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( struct Togl *togl )`
  Returns the width of the given Togl widget. Typically called in the function registered with Togl_ReshapeFunc().

`int Togl_Height( struct Togl *togl )`
  Returns the height of the given Togl widget. Typically called in the function registered with Togl_ReshapeFunc().

`Tcl_Interp *Togl_Interp( struct Togl *togl )`
  Returns the Tcl interpreter associated with the given Togl widget.

### Color Index Mode Functions

These functions are only used for color index mode.

`unsigned long Togl_AllocColor( struct 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( struct 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( struct 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( struct Togl *togl, const char *fontname )`
  Load the named font as a set of glBitmap display lists. fontname may be one of `
  - TOGL_BITMAP_8_BY_13
  - TOGL_BITMAP_9_BY_15
  - TOGL_BITMAP_TIMES_ROMAN_10
  - TOGL_BITMAP_TIMES_ROMAN_24
  - TOGL_BITMAP_HELVETICA_10
  - TOGL_BITMAP_HELVETICA_12
  - TOGL_BITMAP_HELVETICA_18
  `
  - or any X11 font name 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( struct Togl *togl, GLuint fontbase )`
  Destroys the bitmap display lists created by by Togl_LoadBitmapFont().

### Client Data Functions

`void Togl_SetClientData( struct 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 struct 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( struct 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( struct Togl *togl )`
  Display the overlay planes, if any.

`void Togl_HideOverlay( struct Togl *togl )`
  Hide the overlay planes, if any.

`void Togl_PostOverlayRedisplay( struct 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( struct Togl *togl )
      	{
      	   ...your code...
      	}

`int Togl_ExistsOverlay( struct Togl *togl )`
  Returns 1 if overlay planes exist, 0 otherwise.

`int Togl_GetOverlayTransparentValue( const struct Togl *togl )`
  Returns the color index of the overlay's transparent pixel value.

`int Togl_IsMappedOverlay( const struct Togl *togl )`
  Returns 1 if the overlay planes are currently displayed, 0 otherwise.

`unsigned long Togl_AllocColorOverlay( const struct 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 struct 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.

If you choose to use these functions in your application your must first define the TOGL_X11 preprocessor symbol before including the Togl header file:

        #define TOGL_X11
        #include "Togl.h"

`Display *Togl_Display( const struct Togl *togl )`
  Returns the X Display of a Togl widget.

`Screen *Togl_Screen( const struct Togl *togl )`
  Returns the X Screen of a Togl widget.

`int Togl_ScreenNumber( const struct Togl *togl )`
  Returns the X screen number of a Togl widget.

`Colormap Togl_Colormap( const struct Togl *togl )`
  Returns the X Colormap used by a Togl widget.

### Postscript Output

`int Togl_DumpToEpsFile( const struct 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 ignored by togl.
      			This can be useful in your C callback functions
      			to determine which Togl widget is the caller.

      -rgba		true	If true, use RGB(A) mode
      			If false, use Color Index mode

      -redsize	1	Min bits per red component
      -greensize	1	Min bits per green component
      -bluesize	1	Min bits per blue component

      -double		false	If false, request a single buffered window
      			If true, request double buffered window

      -depth		false	If true, request a depth buffer
      -depthsize	1	Min bits of depth buffer

      -accum		false	If true, request an accumulation buffer
      -accumredsize	1	Min bits per accum red component
      -accumgreensize	1	Min bits per accum green component
      -accumbluesize	1	Min bits per accum blue component
      -accumalphasize	1	Min bits per accum alpha component

      -alpha		false	If true and -rgba is true, request an alpha
      			channel
      -alphasize	1	Min bits per alpha component

      -stencil	false	If true, request a stencil buffer
      -stencilsize	1	Min number of stencil bits

      -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.

      -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.
                              NOT YET IMPLEMENTED FOR WINDOWS 95/NT.

      -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.
                              NOT YET IMPLEMENTED FOR WINDOWS 95/NT.

      -indirect       false   If present, request an indirect rendering context.
                              A direct rendering context is normally requested.
                              NOT SIGNIFICANT FOR WINDOWS 95/NT.

`pathName configure`
  Returns all configuration records for the named togl widget.

`pathName configure -option`
  Returns configuration information for the specifed 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 one of:
  `-width`
    Resize the widget to value pixels wide
  `-height`
    Resize the widget to value pixels high

`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.

`pathName makecurrent`
  Make the widget specified by pathName the current one.

## 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 - SGI-only stereo example
- gears.tcl - spinning gears demo

To compile the demos, edit the Makefile to suit your system, then type "make". The stock Makefile is setup for Linux. 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 corrsponding tcl script To run a demo just type `./double.tcl` or `./texture.tcl` etc.

## Stereo Rendering

Togl on SGI systems supports full-screen stereo. Quad-buffered stereo-in-a-window is not supported at this time. To enable stereo support you must add -DSTEREO to the cc compiler flags. Reportedly, SGI O2's need patches 1979 and 2447 in order to use stereo. Also, the frame buffer must be set to the 32+32 pixel format and the monitor set to 1024x768_96s mode.

## 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

- Initial version

### Version 1.1 (never officially released)

- Added Togl_LoadBitmapFont function
- Fixed a few bugs

### Version 1.2 - November, 1996

- added swapbuffers and makecurrent Tcl commands
- More bug fixes
- Upgraded to suport Tcl 7.6 and Tk 4.2
- Added stereo and overlay plane support
- Added Togl_Get/SetClientData() functions
- Added Togl_DestroyFunc()

### Version 1.3 - May 2, 1997

- fixed a bug in Togl_Configure()
- fixed a compilation problem in using Tcl_PkgProvide() with Tcl < 7.4
- new overlay functions: Togl_ExistsOverlay, Togl_GetOverlayTransparentValue, Togl_IsMappedOverlay, Togl_AllocColorOverlay, Togl_FreeColorOverlay
- added X11 functions: Togl_Display, Togl_Screen, Togl_ScreenNumber, Togl_Colormap
- added Togl_DumpToEpsFile function
- fixed a C++ compilation problem
- more robust overlay code
- added timers (Togl_TimerFunc) from Peter Dern and Elmar Gerwalin

### Version 1.4 - September 17, 1997

- Ported to Windows NT (Robert Casto)
- Updated for Tcl/Tk 8.0
- Added many config flags (-redsize, -depthsize, etc) (Matthias Ott)
- Added Togl_Set*Func() functions to reassign callback functions (Matthias Ott)
- Added Togl_ResetDefaultCallbacks() and Togl_ClientData() functions (Greg Couch)

### Version 1.5 - September 18, 1998

- Fixed a few Unix and Windows compilation bugs
- Added Ben Evan's SGI stereo functions
- Multiple expose events now reduced to one redraw
- Destroying Togl widgets caused problems, patched by Adrian J. Chung
- Added Togl_TkWin() function
- Updated for Tcl/Tk 8.0p2
- Added gears demo from Philip Quaife
- Added `-sharelist` and `-sharecontext` config flags
- Fixed a few overlay update bugs
- Added `-indirect` config flag

### Version 1.6 - December ??, 2000

- Added Togl_SetTimerFunc function
- Updated for Tcl/Tk 8.0.5 and 8.1
- Context sharing added for Windows
- Macintosh support (by Paul Thiessen)
- Tcl/Tk stubs support (by Jonas Beskow)

## Contributors

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

- Ramon Ramsan - overlay plane support
- Miguel A. De Riera Pasenau - more overlay functions, X11 functions and EPS output
- Peter Dern and Elmar Gerwalin - Togl_TimerFunc and related code
- Robert Casto - Windows NT port
- Geza Groma - Windows 95/NT patches
- Ben Evans - SGI stereo support
- Paul Thiessen - Macintosh support
- Jonas Beskow - Tcl/Tk stubs support

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

Last edited on 20 December 2001 by Jonas Beskow.