NAME

OpenGL - Parrot extension for OpenGL bindings

SYNOPSIS

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 libary and a helper library for calling glutInit
    load_bytecode 'library/OpenGL.pbc'
    load_bytecode 'library/NCI/call_toolkit_init.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'], '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

DESCRIPTION

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.

Initialization

The initialization routines are mostly for internal use only. They include:

_opengl_init()
At module load time, calls the other initialization routines in the proper order.
_load_opengl_libs()
Opens all relevent OpenGL system libraries. For portability, tries each of the known different filenames for each library in turn before giving up.
_load_lib_with_fallbacks(string friendly_name, pmc fallback_list)
This function is more generally useful than just for this module -- it implements the search for a particular libary 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. .dll or .so). The friendly_name is only used to fill in the error message in case no match can be found on the system.
_wrap_all_opengl_entry_points()
Create NCI wrappers for all GL, GLU, and GLUT functions
_wrap_nci_list(pmc namespace, pmc library, pmc nci_list)
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.

Symbol Export

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.

_export_all_functions(pmc to_namespace :optional)
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.