OpenGL - Parrot extension for OpenGL bindings
This covers only the basic OpenGL and GLUT initialization. For more, look in examples/opengl/, starting with examples/opengl/triangle.pir.
# Include OpenGL constants .include 'opengl_defines.pasm' .sub main :main .param pmc argv # Load OpenGL library and a helper library for calling glutInit load_bytecode 'OpenGL.pbc' load_bytecode 'NCI/Utils.pbc' # Import all OpenGL/GLU/GLUT functions .local pmc import_gl import_gl = get_global ['OpenGL'], '_export_all_functions' import_gl() # Initialize GLUT .local pmc call_toolkit_init call_toolkit_init = get_global ['NCI';'Utils'], 'call_toolkit_init' .const 'Sub' glutInit = 'glutInit' argv = call_toolkit_init(glutInit, argv) # Set display mode, create GLUT window, save window handle .local int mode mode = .GLUT_DOUBLE | .GLUT_RGBA glutInitDisplayMode(mode) .local pmc window window = new 'Integer' window = glutCreateWindow('My Window Title') set_global 'glut_window', window # Set up GLUT callbacks .const 'Sub' draw = 'draw' .const 'Sub' idle = 'idle' .const 'Sub' keyboard = 'keyboard' glutDisplayFunc (draw) glutIdleFunc (idle) glutKeyboardFunc(keyboard) # Enter the GLUT main loop glutMainLoop() .end
This library is a straightforward Parrot NCI wrapper for OpenGL, GLU, and GLUT. It is still a work in progress; work will generally start with the oldest, most widely supported functions and progress to the most recently standardized calls. Generally you will find programming GLUT in PIR to be similar to GLUT in C -- for heavy drawing code, it is sometimes difficult to tell the difference except for the leading period on PIR constant names and lack of trailing semicolons on each line.
The following sections describe features of Parrot's OpenGL bindings that are not part of the core C binding, or where they differ non-trivially.
The initialization routines are mostly for internal use only. They include:
At module load time, calls the other initialization routines in the proper order.
Opens all relevant OpenGL system libraries. For portability, tries each of the known different filenames for each library in turn before giving up.
This function is more generally useful than just for this module -- it implements the search for a particular library that may appear under any of several different filenames. The
fallback_list should be a simple array of strings, each naming one of the possible filenames, without the trailing shared library extension (e.g.
friendly_name is only used to fill in the error message in case no match can be found on the system.
Create NCI wrappers for all GL, GLU, and GLUT functions
Create NCI wrappers for every
library entry point in
nci_list, and store the results in
namespace . The list should consist of alternating function names and Parrot NCI signatures.
These routines ensure that all NCI thunks necessary for calling OpenGL functions are available to Parrot.
These routines allow OpenGL symbols to exported to other namespaces to more directly replicate the normal OpenGL coding style. Most calling programs will want to use at least one of these, probably immediately after loading this library.
Marks all OpenGL/GLU/GLUT functions as exported (to the default export tags, currently ALL and DEFAULT). Unmangles callback names, so that the importing namespaces see the standard names instead of the mangled versions. Called at :load time by _opengl_init().
Export all OpenGL/GLU/GLUT functions to the target
namespace. Unmangles callback names, so that the receiving namespace sees the standard names instead of the mangled versions. If
to_namespace is missing, then the caller's namespace is assumed.