[gambit-list] passing floats in Gambit-C's FFI

Edward Tate cmalune at gmail.com
Sun Mar 16 10:06:10 EDT 2008


I have this code in file "gl-object.ss":

(c-define-type float-ptr (pointer float))

(c-define-type gl-obj (struct "GLObject"))
(c-define-type gl-obj-ptr (pointer gl-obj))

(c-declare "extern void gl_obj_set_pos();")

(define gl-obj-set-pos
  (c-lambda (gl-obj-ptr float float float) void "gl_obj_set_pos"))

which calls this C code in "gl_object.c":

void gl_obj_set_pos(struct GLObject *pobj, float x, float y, float z)
{
  printf("got x: %1.1f, y: %1.1f, z: %1.1f\n", x, y, z);
  gl_obj_mx_set(pobj, 3, 0, x);
  gl_obj_mx_set(pobj, 3, 1, y);
  gl_obj_mx_set(pobj, 3, 2, z);
}

using this code in "gl-object-test.ss":

(define (gl-object-test)
  (let ((new-quad (dynamic-gl-obj-alloc)))
    (gl-obj-set-id new-quad 1)
    (gl-obj-set-pos new-quad 0.5 0.0 0.0)                 <<-- relevant function
    (gl-obj-set-col new-quad 1.0 1.0 1.0 0.5f0)
    (gl-obj-print new-quad)
    (dynamic-gl-obj-free new-quad)))

(gl-object-test)

the result when the object is printed via the print function:

(print function)
void gl_obj_print(struct GLObject *pobj)
{
  printf("object id: %d\n", pobj->_id);
  printf("object mx:\n");
  int i, j;
  for(i = 0; i < 4; i++)
    {
      for(j = 0; j < 4; j++)
	printf("%3.1f ", gl_obj_mx_aref(pobj, i, j));
      printf("\n");
    }
  printf("object col: ");
  for(i = 0; i < 4; i++)
    printf("%3.1f ", pobj->_col[i]);
}

(result)
object id: 1
object mx:
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 1.8 0.0 0.0
object col: 0.0 1.9 0.0 1.9

is not as expected. :(

Is there a reason why the floats are getting borked? I've tried with
float32, float64 but this didn't seem to make a difference. Any help
would be greatly appreciated.



More information about the Gambit-list mailing list