[racket] OpenGL sgl GL_TEXTURE_3D glTexImage3D

From: Ben Goetter (goetter at mazama.net)
Date: Mon Nov 29 19:14:00 EST 2010


glTexImage3D is not a GLUT entry point, but rather is a standard GL 1.2 
entry point.  However, Windows uses GL 1.1, and only defines the 1.1 
entry points in its system DLLs opengl32.dll and glu32.dll.  Hence you 
must use wglGetProcAddress to access glTexImage3D as a non-standard (on 
1.1) entry point.

(The following example uses DrScheme 4.2 FFI syntax, for which I 
apologize-- I have not tracked the changes in Racket 5.0 and beyond, nor 
even had a chance to use it.  I believe that the Racket FFI is pretty 
similar to its ancestor.  If not, then I apologize again for the on-list 
noise, and hope that somebody else will chime in and correct me.)

First, we dynamically load wglGetProcAddress from the Windows 
implementation of OpenGL:

  (define wglGetProcAddress
   (get-ffi-obj 'wglGetProcAddress (ffi-lib "opengl32") (_fun #:abi 
'stdcall _string -> _fpointer)) )

Add a little sauce to convert the code address returned by that Windows 
syscall into a Scheme procedure:

  (define (gl-load-extension name ftype)
   (let ((f (wglGetProcAddress name)))
    (if f (ptr-ref f ftype) (lambda x (DIE-HORRIBLY)))))

(For (lambda x (DIE-HORRIBLY)), substitute the abend or log output of 
your choice signaling the lack of the extension.  (lambda x #f) works, too.)

Now we use this to access the implementation of glTexImage3D in your 
video card's driver, assuming that the entry point exists therein:

(define glTexImage3D
  (gl-load-extension "glTexImage3D"
   (_fun #:abi 'stdcall _gl-enum _gl-int _gl-int _gl-sizei _gl-sizei 
_gl-sizei _gl-int _gl-enum _gl-enum _gl-voidv ->) ))

And voila, there's your glTexImage3D procedure.  (Or so I hope.  To echo 
Noel's caveat, the above is totally untested but Should Work.)

For further reading, please see the "FFI" chapter in the Racket 
documentation.  You will find it quite enlightening.  The FFI is a 
marvelous thing.

Good luck.

On 11/27/2010 11:08 AM, mosi wrote:
> Backup plan: is there a good book on MZScheme, FFI, dynamic loading of 
> DLL functions ?
> [...]
>             (glTexImage3D GL_TEXTURE_3D 0 3 width height depth 0 
> GL_RGB GL_UNSIGNED_BYTE new-img-vec)             ;; this is not 
> supported in my Racket ! ? !

Posted on the users mailing list.