Op Engl
Op Engl
Abstract
OpenGL is the industry-leading, cross-platform graphics application programming interface (API), and the only
major API with support for virtually all operating systems. Many languages, such as Fortran, Java, Tcl/Tk, and
Python, have OpenGL bindings to take advantage of OpenGL visualization power. In this article, we present Ch
OpenGL Toolkit, a truly platform-independent Ch binding to OpenGL for computer graphics. Ch is an embeddable C/
C++ interpreter for cross-platform scripting, shell programming, numerical computing, and embedded scripting. Ch
extends C with salient numerical and plotting features. Like some mathematical software packages, such as MATLAB,
Ch has built-in support for two and three-dimensional graphical plotting, computational arrays for vector and matrix
computation, and linear system analysis with advanced numerical analysis functions based on LAPACK. Ch OpenGL
Toolkit allows OpenGL application developers to write applications in a cross-platform environment, and all of the
OpenGL application source code can readily run on different platforms without compilation and linking processes. In
addition, the syntax of Ch OpenGL Toolkit is identical to C interface to OpenGL. Ch OpenGL Toolkit saves OpenGL
programmers’ energies for solving problems without struggling with mastering new language syntax. Ch OpenGL
Toolkit is embeddable. Embedded Ch OpenGL graphics engine enables graphical application developers or users to
dynamically generate and manipulate graphics at run-time. The truly platform independent, scriptable, and embeddable
features of Ch OpenGL Toolkit make it a good candidate for rapid prototyping, mobile graphics applications, Web-
based applications, and classroom interactive presentation. The design issues of Ch OpenGL Toolkit and its potential
applications are presented in the article. A methodology that can be used to implement a Web-based visualization
system based on Ch OpenGL and Ch CGI is also introduced. The method described in the article can be easily followed
to create a Web-based visualization system at low cost and with minimal effort. The software packages Ch and Ch CGI
Toolkit are freely available and can be downloaded from the Internet.
r 2005 Elsevier Ltd. All rights reserved.
Keywords: Methodology and techniques—interaction techniques; Graphics utilities—software support; Graphics systems—
distributed/network graphics
0097-8493/$ - see front matter r 2005 Elsevier Ltd. All rights reserved.
doi:10.1016/j.cag.2005.03.002
ARTICLE IN PRESS
332 B. Chen, H.H. Cheng / Computers & Graphics 29 (2005) 331–339
system, window system, and hardware platform inde- The Fortran 90 Interface to OpenGL, f90gl [3], is a
pendent API for the development of 2D/3D graphics. public domain implementation of the official Fortran 90
Since OpenGL API was introduced in 1992, many bindings to OpenGL. The interface is responsible for
applications, such as CAD, CAM, and game develop- ensuring the interoperability between Fortran and C,
ment, have benefited from its cross-platform accessi- such as matching Fortran data types to C data types,
bility. OpenGL has become a premier environment for choosing subroutines and functions in Fortran for
developing portable 2D/3D graphics applications. It is different C functions, and dealing with array arguments.
also widely used for teaching and learning computer Most vendor implementations of the Fortran interface
graphics. The features of device independence and to OpenGL are for a specific system with specific
portability make OpenGL a strategic interface for Fortran and C compliers. Although the Fortran 90
courses on computer graphics. Computer platforms Interface is much more robust and portable than the
vary from instructor to student and from school to Fortran 77 interface because the Fortran/C interface is
student home. By using OpenGL, programs developed contained entirely inside the Fortran 90 interface to
on a machine can be debugged and graded on other OpenGL and hidden from the user, some potential
machines with different platforms, and the resulting problems still exist. For example, the user is responsible
graphics are the same. for choosing appropriate compilers that provide a
Since OpenGL is one of the most popular industry sufficient inter-language calling convention, ensuring
standard graphical software packages, many languages, the persistence of function arguments that are assigned
such as Fortran, Java, Tcl/Tk, and Python, have to C pointers internally, dealing with unsigned int data
OpenGL bindings to take advantage of OpenGL type in C functions, and paying special attention to the
visualization power. The information about these array order difference in Fortran and C [4].
language bindings to OpenGL is introduced in Section The JOGL Project is a reference implementation of
2. We have developed Ch OpenGL Toolkit [2]. Ch is an the Java bindings for OpenGL API, and is designed to
embeddable C/C++ interpreter. Ch OpenGL Toolkit provide hardware-supported 3D graphics to applica-
further enhances the portability of OpenGL API. tions written in Java [5]. Jogl provides access to the latest
Usually, OpenGL application programs have to be OpenGL routines (OpenGL 1.4 with vendor extensions)
compiled and linked before running these programs on as well as platform-independent access to hardware-
different platforms. Ch OpenGL Toolkit makes accelerated off-screen rendering (‘‘pbuffers’’). JOGL
OpenGL applications truly portable across different was designed for the most recent version of the Java
platforms. With Ch OpenGL, OpenGL application platform, J2SE 1.4 and later. It only supports true color
source code can readily run on different platforms (15 bits per pixel and higher) rendering, and it does not
without compilation and linking processes. support color-indexed modes. Since JODE is an ongoing
The design issues of Ch OpenGL Toolkit and its project, the coverage of accessing OpenGL functionality
potential applications are presented in the article. A is limited, and there are some issues remain on different
comparison of Ch OpenGL Toolkit to similar attempts platforms, which can be found in JOGL user’s guide on
is also given and the novel features of Ch OpenGL are the Web page [5].
highlighted. The remainder of the article is organized as Scripting languages have been increasingly used for
follows. Section 2 reviews major language bindings to rapid prototyping, dynamic manipulating components,
OpenGL. Section 3 introduces Ch and Ch OpenGL. and Web-based applications. The reason for the
Several design issues related to Ch OpenGL Toolkit are increasing popularity of scripting languages in these
discussed. Section 4 presents some potential applications application areas is that scripting languages provide
of Ch OpenGL. Section 5 summarizes different rapid turnaround during development by eliminating
approaches of Web-based visualization systems and compile times and allow users to dynamically program
demonstrates how to implement a Web-based visualiza- the applications at run-time [6]. Most scripting lan-
tion system based on Ch OpenGL and Ch CGI. Section guages embed different binary libraries inside the
6 discusses planned future work for portable and Web- language allowing users access to the library’s function-
based animation. Section 7 summarizes the presented ality in scripts. For example, many scripting languages
work. have bindings to OpenGL providing interpretive access
to the OpenGL libraries so that developers can perform
interactive 3D graphics in scripts. As examples, Tcl/Tk
2. Related work and Python bindings to OpenGL are described below.
There are several Tcl/Tk bindings to OpenGL. The
OpenGL is supported by major operating systems and most popular two implementations are TKOGL and
window systems, and it is callable from many program- Togl. TKOGL [7], a Tk OpenGL widget, enables the
ming languages. This section introduces different creation and display of 3D graphics using the OpenGL
language bindings to OpenGL. API. The advantage of this implementation is that the
ARTICLE IN PRESS
B. Chen, H.H. Cheng / Computers & Graphics 29 (2005) 331–339 333
3D graphics widget behaves like a Tk 2D widget, such as complex numbers, variable length arrays, IEEE
enabling both the experienced and novice users to floating-point arithmetic and type-generic mathematical
generate and display 3D models in a concise manner. functions first implemented in Ch were adopted in C99,
The drawback of the system is that it ties OpenGL a new C standard ratified in 1999. In addition, Ch
commands to the widget and destroys one of the main supports classes in C++ for object-based program-
advantages of OpenGL, window system independence. ming. Like other mathematical software packages, such
Togl [8] is also a Tk widget for OpenGL rendering. as MATLAB, Ch has built-in support for two and three-
Although Togl provides the means to open a window for dimensional graphical plotting, computational arrays
displaying OpenGL graphics, it does not include Tcl for vector and matrix computation, and linear system
bindings for any of the OpenGL rendering functions. A analysis with advanced numerical analysis functions
typical Togl program has Tcl code for managing the user based on LAPACK. With the power of computational
interface and a C program for computations and arrays, we can specify vector and matrix operations
OpenGL rendering. To use Togl effectively, one should directly in expressions in the same way as scalars in both
be familiar with Tcl, Tk, OpenGL, and C programming. interactive execution and programs. Fig. 1 shows
PyOpenGL [9] is the Python binding to OpenGL and interactive execution of programming statements in a
related APIs. It is an influential scriptable package for Ch shell. The type declarators array and int declare
3D graphics and has received some practical applica- variables A and B as computational arrays of int type.
tions. However, like other script language bindings to The header file array.h needs to be included in a
OpenGL, the syntax of functions in PyOpenGL has program to use computational arrays.
been modified from standard OpenGL API. Further- Ch OpenGL is a Ch binding to OpenGL. It provides
more, the calling method and functionality of some access to the full functionality of OpenGL, GLU,
functions appeared in PyOpenGL 2 are different from GLUT, and GLAUX. Ch OpenGL has many novel
that of the C counterparts because of the differences features. First, Ch is a cross platform C/C++ inter-
between C and Python. For example, each PyOpenGL preter. As a part of Ch distribution, Ch OpenGL allows
function call, which has an argument of an array pointer OpenGL application developers to write applications in
or array element, has different format from a C function a cross-platform environment. All of the OpenGL
call due to the difference between C and Python in the application source code can readily run on different
way that they access arrays. platforms without compilation and linking processes as
shown in Fig. 2. Second, the syntax of Ch OpenGL is
exactly the same as C interface to OpenGL. There is no
3. Ch and Ch OpenGL need of learning new syntax. Third, Ch OpenGL is
embeddable. Embedding Ch into graphics applications
Ch, originally developed by Cheng [10,11], is an allows developers or users to dynamically generate and
embeddable C/C++ interpreter for cross-platform manipulate graphics at run-time. Finally, computational
scripting, shell programming, numerical computing, array in Ch makes vector and matrix operations carried
and embedded scripting. It supports all features of the out in 3D graphics more concise. We believe that the
C language standard ratified in 1990. Many new features computational efficiency of graphics applications can be
running source code immediately without recompilation embedded into each host to which mobile graphical code
and linking. may migrate. Ch OpenGL is a good candidate for this
kind of applications.
4.2. Mobile graphics
4.3. Web and network applications
In internet-based distributed cooperative graphics
systems, there is a need to send graphics to different Many existing visualization applications were
hosts in the system. Instead of sending graphics data, we written in C/C++ using OpenGL. However, difficulty
may send code to the target systems to generate and in interfacing with Web servers impeded their ap-
manipulate graphics locally. This approach reduces the plications in Web-based visualization systems. As a
network traffic dramatically in heavy graphics transmis- superset of C, Ch is suitable for Web-based applications
sion systems. Graphical mobile programs can be because of its interpretive nature. Many Web-based
dynamically generated by application programs online applications, such as a Web-based system for control
in one machine and run on other machines with different system design and analysis [14], have been developed in
platforms. In order to support the execution of the Ch. With Ch OpenGL and Ch CGI [15], C/C++
mobile graphical code in a heterogeneous network, a OpenGL programs can be used in Web-based visualiza-
scriptable graphical engine, such as Ch [11], should be tion systems.
ARTICLE IN PRESS
336 B. Chen, H.H. Cheng / Computers & Graphics 29 (2005) 331–339
Fig. 7. Commonly used approaches for Web-based visualization systems: (a) Server-based visualization—transfer VRML files, (b)
server-based visualization—transfer image files, (c) client-based visualization—transfer data, (d) client-based visualization—transfer
software and data.
ARTICLE IN PRESS
B. Chen, H.H. Cheng / Computers & Graphics 29 (2005) 331–339 337
users through Ch CGI. The Ch OpenGL based Web pose. The prototype of function ChCreateImage() is as
visualization system offers the following features: follows:
int ChCreateImage(unsigned char *curpix, int pixelsize,
1. C/C++ visualization programs can directly be used unsigned int width,
in Web-based visualization systems. unsigned int height, int outputtype, y
2. Ch OpenGL visualization server is application /* int imagetype, char *imagename */).
transparent. It runs existing OpenGL application
programs without any modifications. Moreover,
server programs are platform-independent. This function takes the pointer to the off-screen image
3. From the user’s point of view, server visualization buffer, the number of bytes for each pixel, the image
power can be accessed with the lowest requirements— width, and the image height as the first four arguments.
only a Web browser is needed. The function can create two output image formats, and
4. From the system developer’s point of view, Ch the output format is decided by the fifth argument.
OpenGL visualization server programs are easy to When the value of the fifth argument is CH_IMAGE_-
develop and maintain. Furthermore, Ch OpenGL STREAM, the function simply outputs the resulting
based Web visualization system avoids costly new image to the standard output stream. If the value of the
hardware and software investments. Existing compu- fifth argument is CH_IMAGE_FILE, the function saves
ter facilities and network infrastructure can be used the image to a file. The image file format and name are
to deliver server visualization power to remote users. specified by the arguments imagetype and imagename. In
The software packages Ch, Ch OpenGL, and Ch CGI the current implementation, image files can be saved to
toolkit are freely available and can be downloaded on either PPM or PNG format. Two macros, CH_IMA-
the Web [11]. GE_PPM and CH_IMAGE_PNG, represent PPM
format and PNG format, respectively.
5.2. Off-screen rendering interface and image file creation 5.3. Implementation of Ch OpenGL based visualization
server
The graphics generated by OpenGL is normally
rendered into a window. For Internet-based applica- The implementation of Ch OpenGL based visualiza-
tions, however, the generated images are required to tion server is based on an HTTP Web server as shown in
render into an image buffer instead of a window. This is Fig. 8. By using a standard Web server, there is no need
called off-screen rendering. Off-screen rendering is for developing new server programs. Users’ requests are
generally provided by OpenGL extensions to a native sent to the server as HTML documents. Ch
window system, such as GLX in Unix or WGL in CGI programs extract parameters that are encoded in
Windows. The portability and performance trade-offs of HTML documents and invoke corresponding OpenGL
the off-screen rendering facilities for WGL, GLX and
Mesa were described in [17].
Mesa [18] is a 3D graphics library with APIs identical
to those of OpenGL. Mesa’s off-screen rendering
interface called OSMesa is an operating system and
window system independent facility for off-screen
rendering. The OSMesa is quite simple. It provides 3
functions for making off-screen renderings. Function
OSMesaCreateContext() creates an RGBA-mode con-
text. Function OSMesaMakeCurrent() binds an image
buffer to the context and makes it current. Function
OSMesaDestroyContext() destroys the context. The
default maximum image size is 1280 1024 and it is
specified by macros MAX_WIDTH and MAX_-
HEIGHT in the header file src/config.h. If users want
to generate images larger than the default size, they have
to edit the src/config.h file to change MAX_WIDTH
and MAX_HEIGHT to the desired values and recom-
pile Mesa.
Since Mesa does not provide facilities for writing
image files from an off-screen image buffer, we created a
function called ChCreateImage() to achieve this pur- Fig. 8. Modular structure of the visualization server.
ARTICLE IN PRESS
338 B. Chen, H.H. Cheng / Computers & Graphics 29 (2005) 331–339
programs. Since we want to display resulting graphics member functions of these classes can directly be
images in the user’s browser, the off-screen rendering integrated into OpenGL C application programs. The
method described in the previous subsection has been user’s inputs, through a fill-out form on the Web page,
used in the visualization server implementation. Instead are extracted by Ch CGI programs and passed to
of rendering 3D graphics generated by an OpenGL OpenGL application programs directly. Similarly, out-
program to a window, we use Mesa off-screen rendering put images created by OpenGL application programs
interface to save it to an off-screen image buffer. After can be used to generate dynamic Web pages inside the
an image is created in the image buffer, a Ch image application programs.
creation API is used to convert image data to the PNG
format, and the resulting image is displayed on the user’s 5.4. Web-based interactive 2D/3D graphics
Web page through Ch CGI.
CGI enables a Web server to receive clients’ requests, Fig. 9 shows an example of using Ch OpenGL based
execute application programs on the server, and send Web visualization system to interactively configure 3D
execution results back to clients. HTML documents that graphics. The source code for this example is included in
a Web server retrieves are static. With CGI, Web servers Ch OpenGL distribution. End-users can set the dimen-
are able to output dynamic information on Web pages sions of the output image, the colors of torus, cone,
according to clients’ requests. Although a CGI program sphere, and the background through a fill-out form.
can be written in any language that allows it to be These parameters are encoded by the client browser and
executed on the host, many people prefer to write CGI decoded by the Ch CGI member function CRequest::-
programs using scripting languages since they are easier getFormNameValue(). Since Ch CGI toolkit is a set of
to debug, modify, and maintain than a typical compiled C++ classes, we can integrate CGI programs with
program. With Ch, C/C++ programs become scripts. OpenGL application programs so that the system
In addition, Ch CGI toolkit [15] provides four easy-to- parameters submitted by end-users can be passed to
use classes, namely, CResponse, CRequest, CServer, application programs without any additional interface.
and CCookie, for CGI programming in Ch. These four Once a user activates an application program, the
classes provide member functions similar to API in application program creates a new off-screen image
active server page (ASP) and Java server page (JSP). The according to new parameters and sends the output
resulting image as a standard output stream to a CGI prototyping, mobile graphics applications, Web-based
program to create a new Web page for display. applications, and classroom interactive presentation.
A methodology that can be used to implement a Web-
based visualization system based on Ch OpenGL and
6. Future work Ch CGI was introduced in the article. The method
described in the article can be easily followed to create a
Ch supports X11/Motif and Win32 graphical APIs. Web-based visualization system at low cost and with
Ch OpenGL graphical animation in a standalone minimal effort. The software packages Ch and Ch CGI
computer works both in Unix and Windows platforms. toolkit are freely available and can be downloaded from
However, X11/Motif and Win32 graphical APIs are the Web [11]. With Ch and Ch CGI, C/C++
platform dependent. Although GLUT is portable to visualization programs can directly be used in Web-
different platforms, its graphical user interface is very based visualization systems. Ch OpenGL based Web
limited. GTK+ is a powerful multi-platform toolkit for visualization system offers the possibility for new forms
creating graphical user interface. The current version of of customer service, such as interactive 3D product
Ch only contains GTK+ version 1.2.8, which does not configuration, ordering, and customer training. It is also
support GTKGLArea, a widget that allows OpenGL an ideal environment for teaching and learning compu-
commands within a GTK drawing area. When the Ch ter graphics.
binding to the latest GTK+ is available, graphical user
interface, such as menus, tool bars, entries, and dialogs,
can be integrated into OpenGL applications and allows
References
the user to dynamically manipulate graphics in hetero-
geneous platforms. [1] Woo M, Neider J, Davis T, Shreiner D. OpenGL
The Ch OpenGL Web application example given in programming guide: the official guide to learning OpenGL,
the article is based on Ch CGI. CGI is a very simple version 1.2, 3 ed. Reading: Addison-Wesley; 1999.
interface for writing dynamic Web pages. The interface [2] Ch OpenGL toolkit, http://www.softintegration.com/
is not meant to be high performance and complicated. products/toolkit/opengl/, Softintegration, Inc.
Applications with animation cannot be performed by [3] f90gl Fortran interface for OpenGL and GLUT, http://
simple CGI interface. We are investigating appropriate math.nist.gov/f90gl/.
approaches to solve this problem. One possible solution [4] A Fortran 90 Interface for OpenGL: revised January 1998.
is to develop a browser plug-in with an embedded Ch to NISTIR 6134, February 1998.
[5] The SUN/SGI Java/OpenGL bindings, https://jogl.dev.ja-
perform visualization on the client machine as shown in
va.net/.
Fig. 7(d). Security is the major concern for this [6] Ousterhout JK. Scripting—higher level programming for
approach. Safe Ch [11] and encryption might be the 21st century. Computer 1998;31(3):23–30.
necessary for both code transmission and execution. [7] Esperanca C. A Tk OpenGL widget. Proceedings of
USENIX Fifth Annual Tcl/TK Workshop. 1997.
[8] Paul B, Bederson B. Togl—a Tk OpenGL widget, http://
7. Conclusions togl.sourceforge.net/.
[9] PyOpenGL 2.0–the PyOpenGL binding, http://pyopengl.
OpenGL is the most popular industry standard sourceforge.net/.
graphical API for developing portable 2D/3D graphics [10] Cheng HH. Scientific computing in the Ch programming
language. Scientific Programming 1993;2(3):49–75.
applications. Ch OpenGL further enhances the port-
[11] Ch—an embeddable C/C++ interpreter, http://www.
ability of OpenGL. Ch OpenGL allows OpenGL softintegration.com/, Softintegration, Inc.
application developers to rapidly develop and deploy [12] Robins N. OpenGL tutorial, http://www.xmission.com/
applications across different platforms. It is the only nate/tutors.html.
implementation that enables C/C++ OpenGL pro- [13] Ch OpenGL toolkit demos, http://iel.ucdavis.edu/projects/
grams to run on different platforms without compilation chopengl/.
and linking processes. In addition, the syntax of Ch [14] Yu Q, Chen B, Cheng HH. Web-based control system
OpenGL Toolkit is identical to C interface to OpenGL. design and analysis. IEEE Control Systems Magazine
Ch OpenGL saves OpenGL programmers’ energies in 2004;24(3):45–57.
[15] Ch CGI toolkit, http://www.softintegration.com/products/
problem solving without struggling with mastering new
toolkit/cgi, Softintegration, Inc.
language syntax. Ch OpenGL Toolkit is embeddable.
[16] Ames AL, Nadeau DR, Moreland JL. The VRML
Embedded Ch OpenGL graphics engine allows graphi- Sourcebook. New York: Wiley; 1996.
cal application developers or users to dynamically [17] Paul B. SIGGRAPH’97 Course 24: OpenGL and window
generate and manipulate graphics at run-time. Because system integration—OpenGL/Mesa off-screen rendering,
Ch OpenGL is truly platform independent, scriptable, 1997, http://www.mesa3d.org/brianp/sig97/offscrn.htm.
and embeddable, it is a good candidate for rapid [18] The Mesa 3D graphics library, http://www.mesa3d.org.