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.
Afficher les réponses par date
On 16-Mar-08, at 10:06 AM, Edward Tate wrote:
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); }
Does the Scheme code include the C prototype for gl_obj_set_pos ? Typically you would put that prototype in a C header file, for example:
/* File: "gl_object.h" */ void gl_obj_set_pos(struct GLObject *pobj, float x, float y, float z);
and then in your Scheme file you would have:
(c-declare "#include "gl_object.h"")
(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"))
In other words the c-lambda's types simply indicate the types of the *actual parameters* that are passed in the generated *call* to gl_obj_set_pos. In principle your C function (or macro or inline C code) could have formal parameters of a different type (for example "int"), and the C compiler will do the appropriate conversions. A prototype can't be generated automatically because then you couldn't call C macros.
As a matter of style, I would write:
(c-define-type float* (pointer float))
Marc