Hello,
I was trying out the opengl library from the wiki.
Some of the functions in the glut library are used to specify callbacks. For example, glutReshapeFunc.
In the glut libraries of Ypsilon, Larceny, and Chicken, I'm able to pass a Scheme procedure to the 'glutReshapeFunc' procedure.
Is this doable with Gambit? From the sound of the manual, it seems like this is not yet supported.
Ed
Afficher les réponses par date
Eduardo Cavazos wrote:
I was trying out the opengl library from the wiki.
Some of the functions in the glut library are used to specify callbacks. For example, glutReshapeFunc.
In the glut libraries of Ypsilon, Larceny, and Chicken, I'm able to pass a Scheme procedure to the 'glutReshapeFunc' procedure.
Is this doable with Gambit? From the sound of the manual, it seems like this is not yet supported.
I'm thinking about going with a workaround along these lines.
This is how 'glutReshapeFunc' is defined in the library:
(define glutReshapeFunc (c-lambda ( (function (int int) void) ) void "glutReshapeFunc"))
So maybe define a helper procedure which *can* be passed to 'glutReshapeFunc':
(c-define (basic-reshape-func width height) (int int) void "basicReshapeFunc" "" (glut-reshape-func width height))
That calls a procedure stored in a variable which may be set interactively at a REPL:
(define glut-reshape-func #f)
Ed
On 21-Mar-09, at 5:12 PM, Eduardo Cavazos wrote:
Eduardo Cavazos wrote:
I was trying out the opengl library from the wiki.
Some of the functions in the glut library are used to specify callbacks. For example, glutReshapeFunc.
In the glut libraries of Ypsilon, Larceny, and Chicken, I'm able to pass a Scheme procedure to the 'glutReshapeFunc' procedure.
Is this doable with Gambit? From the sound of the manual, it seems like this is not yet supported.
I'm thinking about going with a workaround along these lines.
This is how 'glutReshapeFunc' is defined in the library:
(define glutReshapeFunc (c-lambda ( (function (int int) void) ) void "glutReshapeFunc"))
So maybe define a helper procedure which *can* be passed to 'glutReshapeFunc':
(c-define (basic-reshape-func width height) (int int) void "basicReshapeFunc" "" (glut-reshape-func width height))
That calls a procedure stored in a variable which may be set interactively at a REPL:
(define glut-reshape-func #f)
Ed
Indeed, for complete portability, the only Scheme procedures that can be passed to a C function are the Scheme procedures defined with c- define.
On some platforms (processor and operating system dependent) that can execute dynamically generated machine code (32 bit intel, power-pc, sparc), you can pass any procedure including closures. This feature is enabled if USE_dynamic_code_gen is defined in lib/os_dyn.h and this is the default in recent versions of Gambit. Give it a try... it may work to pass a closure on your platform... but don't count on it to be portable!
Marc
Marc Feeley wrote:
On 21-Mar-09, at 5:12 PM, Eduardo Cavazos wrote:
Eduardo Cavazos wrote:
I was trying out the opengl library from the wiki.
Some of the functions in the glut library are used to specify callbacks. For example, glutReshapeFunc.
In the glut libraries of Ypsilon, Larceny, and Chicken, I'm able to pass a Scheme procedure to the 'glutReshapeFunc' procedure.
Is this doable with Gambit? From the sound of the manual, it seems like this is not yet supported.
I'm thinking about going with a workaround along these lines.
This is how 'glutReshapeFunc' is defined in the library:
(define glutReshapeFunc (c-lambda ( (function (int int) void) ) void "glutReshapeFunc"))
So maybe define a helper procedure which *can* be passed to 'glutReshapeFunc':
(c-define (basic-reshape-func width height) (int int) void "basicReshapeFunc" "" (glut-reshape-func width height))
That calls a procedure stored in a variable which may be set interactively at a REPL:
(define glut-reshape-func #f)
Ed
Indeed, for complete portability, the only Scheme procedures that can be passed to a C function are the Scheme procedures defined with c-define.
On some platforms (processor and operating system dependent) that can execute dynamically generated machine code (32 bit intel, power-pc, sparc), you can pass any procedure including closures. This feature is enabled if USE_dynamic_code_gen is defined in lib/os_dyn.h and this is the default in recent versions of Gambit. Give it a try... it may work to pass a closure on your platform... but don't count on it to be portable!
Thanks for the tip Marc. I didn't know about USE_dynamic_code_gen. I am aiming for portability so I will stick with 'c-define' for now.
Ed
Eduardo Cavazos wrote:
I was trying out the opengl library from the wiki.
Some of the functions in the glut library are used to specify callbacks. For example, glutReshapeFunc.
In the glut libraries of Ypsilon, Larceny, and Chicken, I'm able to pass a Scheme procedure to the 'glutReshapeFunc' procedure.
Is this doable with Gambit? From the sound of the manual, it seems like this is not yet supported.
OK, the workaround strategy I mentioned is working great.
In my hacked version of the opengl library, this is the code for glutReshapeFunc:
---------------------------------------------------------------------- (define *glut-reshape-func* #f)
(c-define (basic-reshape-func width height) (int int) void "basicReshapeFunc" "" (*glut-reshape-func* width height))
(define (glutReshapeFunc proc) (set! *glut-reshape-func* proc) ((c-lambda () void " glutReshapeFunc ( basicReshapeFunc ) ; "))) ----------------------------------------------------------------------
glutDisplayFunc is another essential function. The code is similar:
---------------------------------------------------------------------- (define *glut-display-func* #f)
(c-define (basid-display-func) () void "basicDisplayFunc" "" (*glut-display-func*))
(define (glutDisplayFunc proc) (set! *glut-display-func* proc) ((c-lambda () void " glutDisplayFunc ( basicDisplayFunc ) ; "))) ----------------------------------------------------------------------
Given those, Gambit (gsi or gsc) is able to run this test program:
---------------------------------------------------------------------- (load "/root/abstracting/support/gambit/gl/gl")
(load "/root/abstracting/support/gambit/glut/glut")
(basic-glut-init)
(define display-func (lambda ()
(glClearColor 0.0 0.0 0.0 1.0)
(glClear GL_COLOR_BUFFER_BIT)
(glColor4d 1.0 1.0 1.0 1.0)
(glBegin GL_LINES) (glVertex2d 10.0 10.0) (glVertex2d 90.0 90.0) (glEnd)
(glFlush)))
(define reshape-func (lambda (w h) (glViewport 0 0 w h) (glMatrixMode GL_PROJECTION) (glLoadIdentity) (glOrtho 0.0 (+ w 0.0) 0.0 (+ h 0.0) -10.0 10.0) (glMatrixMode GL_MODELVIEW)))
(glutInitWindowPosition 100 100) (glutInitWindowSize 500 500)
(glutCreateWindow "Hello GLUT")
(glutDisplayFunc display-func)
(glutReshapeFunc reshape-func)
(glutMainLoop) ----------------------------------------------------------------------
Ed